forked from nanovna-v2/NanoVNA2-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.cpp
201 lines (169 loc) · 5.37 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
198
199
200
201
#include "flash.hpp"
#include "globals.hpp"
#include <libopencm3/stm32/flash.h>
#include <string.h>
#include <mculib/printk.hpp>
#define FLASH_PAGE_SIZE 2048
// Use for cache config check
static uint16_t crc_cache = 0;
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 inline uint32_t __ROR(uint32_t op1, uint32_t op2) {
return (op1 >> op2) | (op1 << (32 - op2));
}
static uint32_t checksum(const void *start, size_t len) {
uint32_t *p = (uint32_t*)start;
uint32_t value = 0;
len>>=2;
while (len--)
value = __ROR(value, 31) + *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 = checksum(¤t_props, sizeof(current_props) - 8);
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;
crc_cache|=1<<id;
return ret;
}
int flash_caldata_recall(int id) {
const properties_t *src;
if (id < 0 || id >= SAVEAREA_MAX)
return -1;
// point to saved area on the flash memory
src = (properties_t*)SAVEAREA(id);
// Need check it
if ((crc_cache&(1<<id)) == 0){
if (src->magic != CONFIG_MAGIC) {
printk("caldata_recall: incorrect magic %x, should be %x\n", src->magic, CONFIG_MAGIC);
return -2;
}
if (checksum(src, sizeof(current_props) - 8) != src->checksum) {
printk("caldata_recall: incorrect checksum %08x\n", src->checksum);
return -3;
}
}
/* active configuration points to save data on flash memory */
lastsaveid = id;
crc_cache|=1<<id;
/* duplicated saved data onto sram to be able to modify marker/trace */
memcpy(¤t_props, src, sizeof(properties_t));
return 0;
}
const properties_t *caldata_reference(void) {
const properties_t *src = (const properties_t*)SAVEAREA(lastsaveid);
// Return cached result
if (crc_cache&(1<<lastsaveid)) return src;
if (src->magic != CONFIG_MAGIC)
return nullptr;
if (checksum(src, sizeof(current_props) - 8) != src->checksum)
return nullptr;
crc_cache|=1<<lastsaveid;
return src;
}
int flash_config_save(void) {
uint8_t* src = (uint8_t*)&config;
uint32_t dst = CONFIGAREA_BEGIN;
static_assert(CONFIGAREA_BYTES >= sizeof(config),
"CONFIGAREA_BYTES is too small");
config.magic = CONFIG_MAGIC;
config.checksum = checksum(&config, sizeof(config) - 8);
crc_cache|=1<<15;
return flash_program_data(dst, src, sizeof(config));
}
int flash_config_recall(void) {
const config_t *src = (const config_t*)CONFIGAREA_BEGIN;
void *dst = &config;
// Check cache
if ((crc_cache&(1<<15)) == 0){
if (src->magic != CONFIG_MAGIC) {
printk("config_recall: incorrect magic %x, should be %x\n", src->magic, CONFIG_MAGIC);
return -1;
}
if (checksum(src, sizeof(config) - 8) != src->checksum) {
printk("config_recall: incorrect checksum %08x\n", src->checksum);
return -2;
}
}
crc_cache|=1<<15;
/* 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();
crc_cache = 0;
// 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;
}
}