forked from ttrftech/NanoVNA
-
Notifications
You must be signed in to change notification settings - Fork 53
/
flash.c
279 lines (239 loc) · 6.67 KB
/
flash.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright (c) 2014-2015, TAKAHASHI Tomohiro (TTRFTECH) [email protected]
* All rights reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* The software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "ch.h"
#include "hal.h"
#include "nanovna.h"
#include <string.h>
#include "usbcfg.h"
static int flash_wait_for_last_operation(void)
{
while (FLASH->SR == FLASH_SR_BSY) {
//WWDG->CR = WWDG_CR_T;
}
return FLASH->SR;
}
static void flash_erase_page0(uint32_t page_address)
{
flash_wait_for_last_operation();
FLASH->CR |= FLASH_CR_PER;
FLASH->AR = page_address;
FLASH->CR |= FLASH_CR_STRT;
flash_wait_for_last_operation();
FLASH->CR &= ~FLASH_CR_PER;
}
int flash_erase_page(uint32_t page_address)
{
chSysLock();
flash_erase_page0(page_address);
chSysUnlock();
return 0;
}
void flash_program_half_word(uint32_t address, uint16_t data)
{
flash_wait_for_last_operation();
FLASH->CR |= FLASH_CR_PG;
*(__IO uint16_t*)address = data;
flash_wait_for_last_operation();
FLASH->CR &= ~FLASH_CR_PG;
}
void flash_unlock(void)
{
// unlock sequence
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
}
uint32_t
checksum(const void *start, size_t len)
{
uint32_t *p = (uint32_t*)start;
uint32_t *tail = (uint32_t*)(start + len);
uint32_t value = 0;
while (p < tail) {
value = __ROR(value, 31) + *p;
// if (SDU1.config->usbp->state == USB_ACTIVE) shell_printf("%x, %x\r\n", *p, value);
p++;
}
return value;
}
int
config_save(void)
{
uint16_t *src = (uint16_t*)&config;
uint16_t *dst = (uint16_t*)SAVE_CONFIG_ADDR;
int count = sizeof(config_t) / sizeof(uint16_t);
config.magic = CONFIG_MAGIC;
config.checksum = checksum(&config, sizeof config - sizeof config.checksum);
flash_unlock();
/* erase flash pages */
flash_erase_page((uint32_t)dst);
#if SAVE_CONFIG_SIZE > FLASH_PAGESIZE // two flash pages
flash_erase_page(((uint32_t)dst) + FLASH_PAGESIZE);
#endif
/* write to flash */
while (count-- > 0) {
flash_program_half_word((uint32_t)dst, *src++);
dst++;
}
return 0;
}
int
config_recall(void)
{
const config_t *src = (const config_t*)SAVE_CONFIG_ADDR;
void *dst = &config;
if (src->magic != CONFIG_MAGIC)
return -1;
// if (SDU1.config->usbp->state == USB_ACTIVE) shell_printf("Checksum %x\r\n", src->checksum);
if (checksum(src, sizeof config - sizeof config.checksum) != src->checksum)
return -1;
/* duplicated saved data onto sram to be able to modify marker/trace */
memcpy(dst, src, sizeof(config_t));
return 0;
}
//int16_t lastsaveid = 0;
int
caldata_save(uint16_t id)
{
if (id >= SAVEAREA_MAX)
return -1;
uint16_t *src = (uint16_t*)&setting;
uint16_t *dst;
int count = sizeof(setting_t) / sizeof(uint16_t);
dst = (uint16_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
setting.magic = SETTING_MAGIC;
setting.checksum = 0x12345678;
setting.checksum = checksum(
&setting,
// (sizeof (setting)) - sizeof setting.checksum
(void *)&setting.checksum - (void *) &setting
);
flash_unlock();
/* erase flash pages */
void *p = dst;
void *tail = p + sizeof(setting_t);
while (p < tail) {
flash_erase_page((uint32_t)p);
p += FLASH_PAGESIZE;
}
/* write settings to flash */
while (count-- > 0) {
flash_program_half_word((uint32_t)dst, *src++);
dst++;
}
#if 0
// Flash stored trace to flash
count = sizeof(stored_t) / sizeof(uint16_t);
src = (uint16_t*)&stored_t[0];
while (count-- > 0) {
flash_program_half_word((uint32_t)dst, *src++);
dst++;
}
#endif
/* after saving data, make active configuration points to flash */
// active_props = (setting_t*)saveareas[id];
// lastsaveid = id;
return 0;
}
setting_t *
caldata_pointer(uint16_t id)
{
setting_t *src;
if (id >= SAVEAREA_MAX)
return NULL;
// point to saved area on the flash memory
src = (setting_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
if (src->magic != SETTING_MAGIC)
return NULL;
// if (SDU1.config->usbp->state == USB_ACTIVE) shell_printf("Checksum %x\r\n", src->checksum);
if (checksum(src,
// (sizeof (setting)) - sizeof src->checksum
(void *)&setting.checksum - (void *) &setting
) != src->checksum)
return NULL;
return src;
}
int
caldata_recall(uint16_t id)
{
setting_t *src;
void *dst = &setting;
if (id >= SAVEAREA_MAX)
return -1;
// point to saved area on the flash memory
src = (setting_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
if (src->magic != SETTING_MAGIC)
return -1;
// if (SDU1.config->usbp->state == USB_ACTIVE) shell_printf("Checksum %x\r\n", src->checksum);
if (checksum(src,
// (sizeof (setting)) - sizeof src->checksum
(void *)&setting.checksum - (void *) &setting
) != src->checksum)
return -1;
/* 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(setting_t));
#if 0
// Restore stored trace
src = &(src[1]);
memcpy(stored_t, src, sizeof(stored_t));
#endif
update_min_max_freq();
update_frequencies();
set_scale(setting.scale);
set_reflevel(setting.reflevel);
set_level_meter_or_waterfall();
update_rbw();
sweep_mode = SWEEP_ENABLE;
#ifdef __ULTRA__
ultra_start = (config.ultra_start == ULTRA_AUTO ? ULTRA_THRESHOLD : config.ultra_start);
#endif
// if (setting.show_stored)
// enableTracesAtComplete(TRACE_STORED_FLAG);
return 0;
}
#if 0
const setting_t *
caldata_ref(int id)
{
const setting_t *src;
if (id < 0 || id >= SAVEAREA_MAX)
return NULL;
src = (const setting_t*)saveareas[id];
if (src->magic != SETTING_MAGIC)
return NULL;
if (checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
return NULL;
return src;
}
#endif
void
clear_all_config_prop_data(void)
{
flash_unlock();
/* erase flash pages */
void *p = (void*)SAVE_CONFIG_ADDR;
void *tail = p + SAVE_CONFIG_AREA_SIZE;
while (p < tail) {
flash_erase_page((uint32_t)p);
p += FLASH_PAGESIZE;
}
}