Skip to content

Commit b070a83

Browse files
C33 kvstore: added example
1 parent c8140b8 commit b070a83

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
12+
TDBStore kvstore(BlockDevice::get_default_instance());
13+
14+
void setup() {
15+
Serial.begin(115200);
16+
Serial.println();
17+
18+
while(!Serial);
19+
20+
// Init KVStore
21+
if (kvstore.init() != KVSTORE_SUCCESS) {
22+
Serial.println("Cannot initialize kvstore");
23+
while(1) {};
24+
}
25+
26+
// Remove all values stored in the kvstore
27+
// kvstore.reset();
28+
29+
// Or remove the counter key only
30+
// kvstore.remove("counter");
31+
32+
// Get the counter value, if it doesn't exist it returns KVSTORE_ERROR_ITEM_NOT_FOUND
33+
unsigned int counter;
34+
auto res = kvstore.get("counter", (void*)&counter, sizeof(counter));
35+
36+
if (res == KVSTORE_ERROR_ITEM_NOT_FOUND) {
37+
counter = 0;
38+
} else if (res == KVSTORE_SUCCESS) {
39+
// Increase counter by 1
40+
counter++;
41+
} else {
42+
Serial.print("Error getting counter from kvstore: ");
43+
Serial.println(res);
44+
}
45+
46+
// Print the counter to Serial Monitor
47+
Serial.print("Current counter value: ");
48+
Serial.println(counter);
49+
50+
// Store the updated counter value to the kvstore
51+
if (kvstore.set("counter",(void*)&counter, sizeof(counter), 0) != KVSTORE_SUCCESS) {
52+
Serial.println("Error setting counter from kvstore");
53+
}
54+
55+
// Close the kvstore
56+
if (kvstore.deinit() != KVSTORE_SUCCESS) {
57+
Serial.println("Cannot deinitialize kvstore");
58+
while(1) {};
59+
}
60+
61+
// Wait 10 seconds
62+
Serial.println("Restarting in 10 seconds...");
63+
delay(10000);
64+
65+
// Reset
66+
NVIC_SystemReset();
67+
}
68+
69+
void loop() {}

0 commit comments

Comments
 (0)