-
Notifications
You must be signed in to change notification settings - Fork 1
/
flash.cpp
197 lines (158 loc) · 5.02 KB
/
flash.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "flash.hpp"
#include "globals.hpp"
#include <libopencm3/stm32/flash.h>
#include <string.h>
#include <mculib/printk.hpp>
#define FLASH_PAGE_SIZE 2048
uint32_t flash_program_data(uint32_t dst, uint8_t *src, uint32_t bytes) {
uint32_t flash_status = 0;
// check if start_address is in proper range
if(dst < FLASH_BASE)
return -1;
// calculate current page address
if(dst % FLASH_PAGE_SIZE)
return -1;
flash_unlock();
if(FLASH_CR & FLASH_CR_LOCK) {
printk("flash_program_data: flash_unlock did not unlock hw\n");
printk("&FLASH_KEYR = 0x%x\n", (uint32_t) &FLASH_KEYR);
printk("&FLASH_CR = 0x%x\n", (uint32_t) &FLASH_CR);
printk("FLASH_CR = 0x%x\n", (uint32_t) FLASH_CR);
return -2;
}
// Erase pages
uint32_t curr = dst;
uint32_t end = dst + bytes;
while(curr < end) {
//flash_erase_page(curr);
while(FLASH_SR & FLASH_SR_BSY);
FLASH_CR |= FLASH_CR_PER;
FLASH_AR = curr;
FLASH_CR |= FLASH_CR_STRT;
while(FLASH_SR & FLASH_SR_BSY);
flash_status = FLASH_SR;
if(flash_status != FLASH_SR_EOP) {
printk("flash_program_data: erase error: flash status %d\n", flash_status);
printk("flash_program_data: while erasing page 0x%x\n", curr);
printk("flash_program_data: dst = 0x%x\n", dst);
return -2;
}
curr += FLASH_PAGE_SIZE;
}
// programming flash memory
for(uint32_t iter=0; iter<bytes; iter += 4)
{
// programming word data
uint32_t word = *(uint32_t*)(src + iter);
flash_program_word(dst+iter, word);
flash_status = flash_get_status_flags();
if(flash_status != FLASH_SR_EOP) {
printk("flash_program_data: write error: flash status %d\n", flash_status);
return -2;
}
// verify if correct data is programmed
uint32_t readback = *(volatile uint32_t*)(dst + iter);
if(readback != word) {
printk("flash_program_data: verify failed: wrote %x, read %x\n", word, readback);
return -3;
}
}
return 0;
}
static uint32_t checksum(const void *start, size_t len) {
uint32_t *p = (uint32_t*)start;
uint32_t *tail = (uint32_t*)(((uint8_t*)start) + len);
uint32_t value = 0;
while (p < tail)
value ^= *p++;
return value;
}
int flash_caldata_save(int id) {
// the size of the properties structure must be an integer multiple of
// the flash word size, or else we will read past the end of the structure.
static_assert((sizeof(current_props) % 4) == 0,
"the size of the properties structure must be an integer multiple of 4");
static_assert(SAVEAREA_BYTES >= sizeof(current_props),
"SAVEAREA_BYTES is too small");
uint8_t *src = (uint8_t*)¤t_props;
uint32_t dst = SAVEAREA(id);
if (id < 0 || id >= SAVEAREA_MAX)
return -1;
current_props.magic = CONFIG_MAGIC;
current_props.checksum = 0;
current_props.checksum = checksum(¤t_props, sizeof current_props);
int ret = flash_program_data(dst, src, sizeof(current_props));
printk("save caldata %d, ret = %d\n", id, ret);
printk("src magic: %x, dst magic = %x\n", *(uint32_t*)src, *(uint32_t*)dst);
lastsaveid = id;
return ret;
}
int flash_caldata_recall(int id) {
properties_t *src;
void *dst = ¤t_props;
if (id < 0 || id >= SAVEAREA_MAX)
return -1;
// point to saved area on the flash memory
src = (properties_t*)SAVEAREA(id);
if (src->magic != CONFIG_MAGIC) {
printk("caldata_recall: incorrect magic %x, should be %x\n", src->magic, CONFIG_MAGIC);
return -2;
}
int ck;
if ((ck = checksum(src, sizeof(properties_t))) != 0) {
printk("caldata_recall: incorrect checksum %d, should be 0\n", ck);
return -3;
}
/* active configuration points to save data on flash memory */
active_props = src;
lastsaveid = id;
/* duplicated saved data onto sram to be able to modify marker/trace */
memcpy(dst, src, sizeof(properties_t));
return 0;
}
const properties_t *caldata_reference(void) {
const properties_t *src;
src = (const properties_t*)SAVEAREA(lastsaveid);
if (src->magic != CONFIG_MAGIC)
return nullptr;
if (checksum(src, sizeof(properties_t)) != 0)
return nullptr;
return src;
}
int flash_config_save(void) {
uint8_t *src = (uint8_t*)&config;
uint32_t dst = CONFIGAREA_BEGIN;
uint32_t bytes = sizeof(config);
static_assert(CONFIGAREA_BYTES >= sizeof(config),
"CONFIGAREA_BYTES is too small");
config.magic = CONFIG_MAGIC;
config.checksum = 0;
config.checksum = checksum(&config, bytes);
return flash_program_data(dst, src, bytes);
}
int flash_config_recall(void) {
const config_t *src = (const config_t*)CONFIGAREA_BEGIN;
void *dst = &config;
if (src->magic != CONFIG_MAGIC) {
printk("config_recall: incorrect magic %x, should be %x\n", src->magic, CONFIG_MAGIC);
return -1;
}
int ck;
if ((ck = checksum(src, sizeof(config_t))) != 0) {
printk("config_recall: incorrect checksum %d, should be 0\n", ck);
return -1;
}
/* duplicated saved data onto sram to be able to modify marker/trace */
memcpy(dst, src, sizeof(config_t));
return 0;
}
void flash_clear_user(void) {
flash_unlock();
// erase flash pages
uint8_t *p = (uint8_t*)USERFLASH_BEGIN;
uint8_t *tail = (uint8_t*)board::USERFLASH_END;
while (p < tail) {
flash_erase_page((uint32_t)p);
p += FLASH_PAGE_SIZE;
}
}