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