-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFlashKeyValue.ino
137 lines (112 loc) · 4.05 KB
/
FlashKeyValue.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <FlashIAPBlockDevice.h>
#include <TDBStore.h>
using namespace mbed;
// Get limits of the In Application Program (IAP) flash, ie. the internal MCU flash.
#include "FlashIAPLimits.h"
auto iapLimits { getFlashIAPLimits() };
// Create a block device on the available space of the FlashIAP
FlashIAPBlockDevice blockDevice(iapLimits.start_address, iapLimits.available_size);
// Create a key/value store on the Flash IAP block device
TDBStore store(&blockDevice);
// Dummy sketch stats for demonstration purposes
struct SketchStats {
uint32_t startupTime;
uint32_t randomValue;
uint32_t runCount;
};
void setup()
{
Serial.begin(115200);
while (!Serial);
// Wait for terminal to come up
delay(1000);
Serial.println("FlashIAPBlockDevice + TDBStore Test");
// Feed the RNG for later content generation
srand(micros());
// Initialize the flash IAP block device and print the memory layout
blockDevice.init();
Serial.print("FlashIAP block device size: ");
Serial.println(blockDevice.size());
Serial.print("FlashIAP block device read size: ");
Serial.println(blockDevice.get_read_size());
Serial.print("FlashIAP block device program size: ");
Serial.println(blockDevice.get_program_size());
Serial.print("FlashIAP block device erase size: ");
Serial.println(blockDevice.get_erase_size());
// Deinitialize the device
blockDevice.deinit();
// Initialize the key/value store
Serial.print("Initializing TDBStore: ");
auto result = store.init();
Serial.println(result == MBED_SUCCESS ? "OK" : "KO");
if (result != MBED_SUCCESS)
while (true);
// An example key name for the stats on the store
const char statsKey[] { "stats" };
// Keep track of the number of sketch executions
uint32_t runCount { 0 };
// Previous stats
SketchStats previousStats;
// Get previous run stats from the key/value store
Serial.println("Retrieving Sketch Stats");
result = getSketchStats(statsKey, &previousStats);
if (result == MBED_SUCCESS) {
Serial.println("Previous Stats");
Serial.print("\tStartup Time: ");
Serial.println(previousStats.startupTime);
Serial.print("\tRandom Value: ");
Serial.println(previousStats.randomValue);
Serial.print("\tRun Count: ");
Serial.println(previousStats.runCount);
runCount = previousStats.runCount;
} else if (result == MBED_ERROR_ITEM_NOT_FOUND) {
Serial.println("First execution");
} else {
Serial.println("Error reading from key/value store.");
while (true);
}
//Update the stats and save them to the store
SketchStats currentStats { millis(), rand(), ++runCount };
result = setSketchStats(statsKey, currentStats);
if (result == MBED_SUCCESS) {
Serial.println("Sketch Stats updated");
Serial.println("Current Stats");
Serial.print("\tStartup Time: ");
Serial.println(currentStats.startupTime);
Serial.print("\tRandom Value: ");
Serial.println(currentStats.randomValue);
Serial.print("\tRun Count: ");
Serial.println(currentStats.runCount);
} else {
Serial.println("Error storing to key/value store");
while (true);
}
}
void loop()
{
// Do nothing
}
// Retrieve a SketchStats from the k/v store
int getSketchStats(const char* key, SketchStats* stats)
{
// Retrieve key/value info
TDBStore::info_t info;
auto result = store.get_info(key, &info);
if (result == MBED_ERROR_ITEM_NOT_FOUND)
return result;
// Allocate space for the value
uint8_t buffer[info.size] {};
size_t actual_size;
// Get the value
result = store.get(key, buffer, sizeof(buffer), &actual_size);
if (result != MBED_SUCCESS)
return result;
memcpy(stats, buffer, sizeof(SketchStats));
return result;
}
// Store a SketchStats to the the k/v store
int setSketchStats(const char* key, SketchStats stats)
{
auto result = store.set(key, reinterpret_cast<uint8_t*>(&stats), sizeof(SketchStats), 0);
return result;
}