Skip to content

Commit 1f36c56

Browse files
Storage rework to load from linkerscript (#1)
* made storage load from profile section instead of hardcoded * cleanup old hardcoded values * Added note for profiles in linkerscript
1 parent 352eda4 commit 1f36c56

File tree

5 files changed

+51
-14
lines changed

5 files changed

+51
-14
lines changed

linkerscript.ld

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Memories definitions
1818
MEMORY
1919
{
2020
rom (rx) : org = 0x00000000 + 8k, len = 256k - 8k - 32k
21+
/* Note: The profiles need to be sector alligned. The
22+
storage erases the whole sector and writes the entries
23+
back if we changed something. All other data in this
24+
sector will be lost on changing the entries */
2125
profiles (r) : org = 0x00000000 + 256k - 32k, len = 32k
2226
ram (rwx) : org = 0x10000000, len = 16k
2327
ram1 (rwx) : org = 0x2007C000, len = 16k
@@ -112,6 +116,17 @@ SECTIONS
112116
PROVIDE(__fini_array_end = .);
113117
} > rom
114118

119+
.profile (NOLOAD) :
120+
{
121+
/* provide the start and the end of the profile section */
122+
PROVIDE(__profiles_start = .);
123+
124+
/* mark the whole section as filled with data */
125+
. = . + LENGTH(profiles);
126+
127+
PROVIDE(__profiles_end = .);
128+
} > profiles
129+
115130
/* Stack segment */
116131
.stack (NOLOAD) :
117132
{

main.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,8 @@ int main() {
142142
// using for writing to flash
143143
using flash = target::io::flash;
144144

145-
// the range where we store the keys is 0x38000 - 0x40000
146-
// TODO: use the region from the linkerscript
147-
constexpr static uint32_t key_start = 0x38000;
148-
constexpr static uint32_t key_end = 0x40000;
149-
150145
// using for the storage
151-
using storage = storage::storage<flash, key_start, key_end>;
146+
using storage = storage::storage<flash>;
152147

153148
// create the popup first as some other screens use it
154149
menu::numeric_popup<fb_t> numeric_popup = {};

readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ More pictures can be found [here](./img/)
3737
* support for different intervals
3838
* support for 8 digit keys
3939
* add support for more than 32 profiles (needs a rework if more profiles are required. The profiles are copied to ram and there is not enough for more at the moment. If we leave them in flash we can store a lot more)
40-
* support for reading the profile locations from the linkerscript instead of hardcoded
4140
* rework the calibration and time settings screens
4241

4342
### Compiling

storage.hpp

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
#include <klib/dynamic_array.hpp>
77
#include <klib/string.hpp>
88

9+
extern "C" {
10+
// Start of the profile section. Definition is done in the
11+
// linkerscript. Only the address of the variable should be used. The
12+
// address points to the correct location of the variable
13+
extern uint32_t __profiles_start;
14+
15+
// End of the profile section. Definition is done in the
16+
// linkerscript. Only the address of the variable should be used. The
17+
// address points to the correct location of the variable
18+
extern uint32_t __profiles_end;
19+
}
20+
921
namespace storage {
1022
enum class digit: uint8_t {
1123
digits_6 = 6,
@@ -53,7 +65,7 @@ namespace storage {
5365
* @brief Storage class that reads the entries
5466
*
5567
*/
56-
template <typename Flash, uint32_t Start, uint32_t End>
68+
template <typename Flash>
5769
class storage {
5870
public:
5971
// the max amount of entries we support for now
@@ -63,6 +75,9 @@ namespace storage {
6375
// array to store all the entries
6476
static inline klib::dynamic_array<entry, max_entries> entries = {};
6577

78+
// start address used in writing
79+
static inline uint32_t start_address = 0xffffffff;
80+
6681
public:
6782
/**
6883
* @brief Read the memory and add them to the entries array
@@ -71,11 +86,13 @@ namespace storage {
7186
* @param end
7287
* @param key
7388
*/
74-
static void init(const std::span<uint8_t> key) {
75-
uint32_t address = Start;
89+
static void init(const std::span<uint8_t> key, const uint32_t start, const uint32_t end) {
90+
// update the start address and set the address we should
91+
// start reading from
92+
uint32_t address = start_address = start;
7693

7794
// get all the entries
78-
for (uint32_t i = 0; (i < max_entries) && ((address + sizeof(entry)) < End); i++) {
95+
for (uint32_t i = 0; (i < max_entries) && ((address + sizeof(entry)) < end); i++) {
7996
// TODO: decrypt the keys here
8097
const auto& e = *reinterpret_cast<const entry*>(address);
8198

@@ -110,8 +127,14 @@ namespace storage {
110127
*
111128
*/
112129
static void write() {
130+
// make sure we are initialized
131+
if (start_address == 0xffffffff) {
132+
// do not write if the start address is wrong
133+
return;
134+
}
135+
113136
// erase the sector
114-
Flash::erase(Flash::erase_mode::sector, Start);
137+
Flash::erase(Flash::erase_mode::sector, start_address);
115138

116139
// write in 1024 byte chunks
117140
for (uint32_t i = 0; i < ((max_entries * sizeof(entry)) / 1024); i++) {
@@ -120,7 +143,7 @@ namespace storage {
120143
};
121144

122145
// write the new data
123-
Flash::write(Start + (i * 1024), p);
146+
Flash::write(start_address + (i * 1024), p);
124147
}
125148
}
126149
};

ui/splash.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include <klib/graphics/bitmap.hpp>
44

5+
#include <storage.hpp>
6+
57
#include "screen.hpp"
68

79
namespace menu {
@@ -90,7 +92,10 @@ namespace menu {
9092
Rtc::init();
9193

9294
// init the storage for all the keys
93-
Storage::init({});
95+
Storage::init({},
96+
reinterpret_cast<uint32_t>(&__profiles_start),
97+
reinterpret_cast<uint32_t>(&__profiles_end)
98+
);
9499

95100
// init the usb driver + device
96101
Usb::init();

0 commit comments

Comments
 (0)