Skip to content

Commit 6e3b047

Browse files
committed
Storage folder
1 parent ea1062e commit 6e3b047

File tree

2 files changed

+184
-133
lines changed

2 files changed

+184
-133
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Helper functions for calculating limits for the FlashIAP block device
3+
* */
4+
5+
#pragma once
6+
7+
#include <Arduino.h>
8+
#include <FlashIAP.h>
9+
#include <FlashIAPBlockDevice.h>
10+
11+
using namespace mbed;
12+
13+
// An helper struct for FlashIAP limits
14+
struct FlashIAPLimits {
15+
size_t flash_size;
16+
uint32_t start_address;
17+
uint32_t available_size;
18+
};
19+
20+
// Get the actual start address and available size for the FlashIAP Block Device
21+
// considering the space already occupied by the sketch.
22+
FlashIAPLimits getFlashIAPLimits()
23+
{
24+
// Alignment lambdas
25+
auto align_down = [](uint64_t val, uint64_t size) { return (((val) / size)) * size; };
26+
auto align_up = [](uint32_t val, uint32_t size) { return (((val - 1) / size) + 1) * size; };
27+
28+
size_t flash_size;
29+
uint32_t flash_start_address;
30+
uint32_t start_address;
31+
FlashIAP flash;
32+
33+
auto result = flash.init();
34+
if (result != 0)
35+
return { };
36+
37+
// Find the start of first sector after text area
38+
int sector_size = flash.get_sector_size(FLASHIAP_APP_ROM_END_ADDR);
39+
start_address = align_up(FLASHIAP_APP_ROM_END_ADDR, sector_size);
40+
flash_start_address = flash.get_flash_start();
41+
flash_size = flash.get_flash_size();
42+
43+
result = flash.deinit();
44+
45+
int available_size = flash_start_address + flash_size - start_address;
46+
if (available_size % (sector_size * 2)) {
47+
available_size = align_down(available_size, sector_size * 2);
48+
}
49+
50+
return { flash_size, start_address, available_size };
51+
}
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,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-
}
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

Comments
 (0)