-
Notifications
You must be signed in to change notification settings - Fork 49
/
flash.c
140 lines (118 loc) · 3.15 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
/*
* 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 "nanosdr.h"
#include <string.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;
}
static 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 = len;
while (p < tail)
value ^= *p++;
return value;
}
#define FLASH_PAGESIZE 0x800
// last page of flash memory. assume STM32F303CBT6 flash 128k Device
const uint32_t save_config_area = 0x0801f800;
const uint32_t save_config_prop_area_size = 0x800;
int
config_save(void)
{
uint16_t *src = (uint16_t*)&config;
uint16_t *dst = (uint16_t*)save_config_area;
int count = sizeof(config_t) / sizeof(uint16_t);
config.magic = CONFIG_MAGIC;
config.checksum = 0;
config.checksum = checksum(&config, sizeof config);
flash_unlock();
/* erase flash pages */
flash_erase_page((uint32_t)dst);
/* write to flahs */
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_area;
void *dst = &config;
if (src->magic != CONFIG_MAGIC)
return -1;
if (checksum(src, sizeof(config_t)) != 0)
return -1;
/* duplicated saved data onto sram to be able to modify marker/trace */
memcpy(dst, src, sizeof(config_t));
return 0;
}
void
clear_all_config_prop_data(void)
{
flash_unlock();
/* erase flash pages */
void *p = (void*)save_config_area;
void *tail = p + save_config_prop_area_size;
while (p < tail) {
flash_erase_page((uint32_t)p);
p += FLASH_PAGESIZE;
}
}