forked from hugen79/NanoVNA-H
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flash.c
189 lines (168 loc) · 4.96 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
/*
* Copyright (c) 2019-2020, Dmitry (DiSlord) [email protected]
* Based on 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>
uint16_t lastsaveid = 0;
#if SAVEAREA_MAX >= 8
#error "Increase checksum_ok type for save more cache slots"
#endif
// properties CRC check cache (max 8 slots)
static uint8_t checksum_ok = 0;
static inline void 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;
}
static inline void flash_unlock(void)
{
// unlock sequence
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
}
static void flash_erase_pages(uint32_t page_address, uint32_t size)
{
// Unlock for erase
flash_unlock();
chSysLock();
// erase flash pages
size+=page_address;
for (; page_address < size; page_address+=FLASH_PAGESIZE)
flash_erase_page0(page_address);
chSysUnlock();
}
static void flash_program_half_word_buffer(uint16_t* dst, uint16_t *data, uint16_t size)
{
uint32_t i;
// unlock, and erase flash pages for buffer (aligned to FLASH_PAGESIZE)
flash_erase_pages((uint32_t)dst, size);
// Save buffer
__IO uint16_t* p = dst;
for (i = 0; i < size/sizeof(uint16_t); i++){
flash_wait_for_last_operation();
FLASH->CR |= FLASH_CR_PG;
p[i] = data[i];
flash_wait_for_last_operation();
FLASH->CR &= ~FLASH_CR_PG;
}
}
static uint32_t
checksum(const void *start, size_t len)
{
uint32_t *p = (uint32_t*)start;
uint32_t value = 0;
// align by sizeof(uint32_t)
len = (len + sizeof(uint32_t)-1)/sizeof(uint32_t);
while (len-- > 0)
value = __ROR(value, 31) + *p++;
return value;
}
int
config_save(void)
{
// Apply magic word and calculate checksum
config.magic = CONFIG_MAGIC;
config.checksum = checksum(&config, sizeof config - sizeof config.checksum);
// write to flash
flash_program_half_word_buffer((uint16_t*)SAVE_CONFIG_ADDR, (uint16_t*)&config, sizeof(config_t));
return 0;
}
int
config_recall(void)
{
const config_t *src = (const config_t*)SAVE_CONFIG_ADDR;
if (src->magic != CONFIG_MAGIC || checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
return -1;
// duplicated saved data onto sram to be able to modify marker/trace
memcpy(&config, src, sizeof(config_t));
return 0;
}
int
caldata_save(uint32_t id)
{
if (id >= SAVEAREA_MAX)
return -1;
// Apply magic word and calculate checksum
current_props.magic = PROPS_MAGIC;
current_props.checksum = checksum(¤t_props, sizeof current_props - sizeof current_props.checksum);
// write to flash
uint16_t *dst = (uint16_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
flash_program_half_word_buffer(dst, (uint16_t*)¤t_props, sizeof(properties_t));
lastsaveid = id;
return 0;
}
const properties_t *
get_properties(uint32_t id)
{
if (id >= SAVEAREA_MAX)
return NULL;
// point to saved area on the flash memory
properties_t *src = (properties_t*)(SAVE_PROP_CONFIG_ADDR + id * SAVE_PROP_CONFIG_SIZE);
// Check crc cache mask (made it only 1 time)
if (checksum_ok&(1<<id))
return src;
if (src->magic != PROPS_MAGIC || checksum(src, sizeof *src - sizeof src->checksum) != src->checksum)
return NULL;
checksum_ok|=1<<id;
return src;
}
int
caldata_recall(uint32_t id)
{
lastsaveid = NO_SAVE_SLOT;
// point to saved area on the flash memory
const properties_t *src = get_properties(id);
if (src == NULL){
load_default_properties();
return 1;
}
// active configuration points to save data on flash memory
lastsaveid = id;
// duplicated saved data onto sram to be able to modify marker/trace
memcpy(¤t_props, src, sizeof(properties_t));
return 0;
}
// Used in interpolate, get current calibration slot data
const properties_t *
caldata_reference(void)
{
return get_properties(lastsaveid);
}
void
clear_all_config_prop_data(void)
{
lastsaveid = NO_SAVE_SLOT;
checksum_ok = 0;
// unlock and erase flash pages
flash_erase_pages(SAVE_CONFIG_ADDR, SAVE_FULL_AREA_SIZE);
}