Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0baf2b8

Browse files
andreagilardonipennam
authored andcommitted
C33 kvstore: added example
1 parent 4c6df4f commit 0baf2b8

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Microcontroller startup counter example with Portenta c33 kvstore library
3+
* This simple example demonstrates using the KVStore library to store how many times
4+
* the microcontroller has booted. The KVStore library is based on mbed OS KVStore library
5+
*
6+
* This example is based on Martin Sloup (Arcao) StartCounter example for arduino-esp32
7+
*/
8+
9+
#include <KVStore.h>
10+
#include <TDBStore.h>
11+
#include <MBRBlockDevice.h>
12+
13+
auto root = BlockDevice::get_default_instance();
14+
MBRBlockDevice bd(root, 3);
15+
TDBStore kvstore(&bd);
16+
17+
void setup() {
18+
Serial.begin(115200);
19+
Serial.println();
20+
21+
while(!Serial);
22+
23+
// Init KVStore
24+
if (kvstore.init() != KVSTORE_SUCCESS) {
25+
Serial.println("Cannot initialize kvstore");
26+
while(1) {};
27+
}
28+
29+
// Remove all values stored in the kvstore
30+
// kvstore.reset();
31+
32+
// Or remove the counter key only
33+
// kvstore.remove("counter");
34+
35+
// Get the counter value, if it doesn't exist it returns KVSTORE_ERROR_ITEM_NOT_FOUND
36+
unsigned int counter;
37+
auto res = kvstore.get("counter", (void*)&counter, sizeof(counter));
38+
39+
if (res == KVSTORE_ERROR_ITEM_NOT_FOUND) {
40+
counter = 0;
41+
} else if (res == KVSTORE_SUCCESS) {
42+
// Increase counter by 1
43+
counter++;
44+
} else {
45+
Serial.print("Error getting counter from kvstore: ");
46+
Serial.println(res);
47+
}
48+
49+
// Print the counter to Serial Monitor
50+
Serial.print("Current counter value: ");
51+
Serial.println(counter);
52+
53+
// Store the updated counter value to the kvstore
54+
if (kvstore.set("counter",(void*)&counter, sizeof(counter), 0) != KVSTORE_SUCCESS) {
55+
Serial.println("Error setting counter from kvstore");
56+
}
57+
58+
// Close the kvstore
59+
if (kvstore.deinit() != KVSTORE_SUCCESS) {
60+
Serial.println("Cannot deinitialize kvstore");
61+
while(1) {};
62+
}
63+
64+
// Wait 10 seconds
65+
Serial.println("Restarting in 10 seconds...");
66+
delay(10000);
67+
68+
// Reset
69+
NVIC_SystemReset();
70+
}
71+
72+
void loop() {}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /