60 #ifndef HAL_USE_EMPTY_STORAGE 62 #ifndef BOARD_FLASH_SIZE 63 #error "You must define BOARD_FLASH_SIZE in kbyte" 66 #define KB(x) ((x*1024)) 68 #define STM32_FLASH_BASE 0x08000000 69 #define STM32_FLASH_SIZE KB(BOARD_FLASH_SIZE) 72 #define STM32_FLASH_DISABLE_ISR 0 75 #define STM32_FLASH_BANK2_START (STM32_FLASH_BASE+0x00080000) 78 #if BOARD_FLASH_SIZE == 512 79 #define STM32_FLASH_NPAGES 7 80 static const uint32_t flash_memmap[STM32_FLASH_NPAGES] = {
KB(16),
KB(16),
KB(16),
KB(16),
KB(64),
81 KB(128),
KB(128),
KB(128) };
83 #elif BOARD_FLASH_SIZE == 1024 84 #define STM32_FLASH_NPAGES 12 85 static const uint32_t flash_memmap[STM32_FLASH_NPAGES] = {
KB(16),
KB(16),
KB(16),
KB(16),
KB(64),
88 #elif BOARD_FLASH_SIZE == 2048 89 #define STM32_FLASH_NPAGES 24 90 static const uint32_t flash_memmap[STM32_FLASH_NPAGES] = {
KB(16),
KB(16),
KB(16),
KB(16),
KB(64),
101 #define FLASH_KEY1 0x45670123 102 #define FLASH_KEY2 0xCDEF89AB 110 __asm__ __volatile__(
"\tldrh %0, [%1]\n\t" :
"=r"(retval) :
"r"(addr));
115 static inline void putreg16(uint16_t val,
unsigned int addr)
117 __asm__ __volatile__(
"\tstrh %0, [%1]\n\t": :
"r"(val),
"r"(addr));
122 while (FLASH->SR & FLASH_SR_BSY) {
131 if (FLASH->CR & FLASH_CR_LOCK) {
138 FLASH->ACR &= ~FLASH_ACR_DCEN;
144 FLASH->CR |= FLASH_CR_LOCK;
147 FLASH->ACR |= FLASH_ACR_DCRST;
148 FLASH->ACR &= ~FLASH_ACR_DCRST;
149 FLASH->ACR |= FLASH_ACR_DCEN;
159 if (page >= STM32_FLASH_NPAGES) {
167 for (i = 0; i < STM32_FLASH_NPAGES; i++) {
182 return flash_memmap[page];
190 return STM32_FLASH_NPAGES;
198 if (page >= STM32_FLASH_NPAGES) {
203 count; count--, addr++) {
204 if ((*(
volatile uint8_t *)(addr)) != 0xff) {
217 if (page >= STM32_FLASH_NPAGES) {
221 #if STM32_FLASH_DISABLE_ISR 222 syssts_t sts = chSysGetStatusAndLockX();
233 uint8_t snb = (((page % 12) << 3) | ((page / 12) << 7));
235 FLASH->CR = FLASH_CR_PSIZE_1 | snb | FLASH_CR_SER;
236 FLASH->CR |= FLASH_CR_STRT;
244 #if STM32_FLASH_DISABLE_ISR 245 chSysRestoreStatusX(sts);
251 #if STM32_FLASH_DISABLE_ISR 252 chSysRestoreStatusX(sts);
261 uint16_t *hword = (uint16_t *)buf;
262 uint32_t written =
count;
275 if (!(RCC->CR & RCC_CR_HSION)) {
279 #if STM32_FLASH_DISABLE_ISR 280 syssts_t sts = chSysGetStatusAndLockX();
289 FLASH->CR &= ~(FLASH_CR_PSIZE);
290 FLASH->CR |= FLASH_CR_PSIZE_0 | FLASH_CR_PG;
292 for (;
count; count -= 2, hword++, addr += 2) {
302 FLASH->CR &= ~(FLASH_CR_PG);
307 FLASH->CR &= ~(FLASH_CR_PG);
312 FLASH->CR &= ~(FLASH_CR_PG);
315 #if STM32_FLASH_DISABLE_ISR 316 chSysRestoreStatusX(sts);
322 #if STM32_FLASH_DISABLE_ISR 323 chSysRestoreStatusX(sts);
328 #endif // HAL_USE_EMPTY_STORAGE static void stm32_flash_wait_idle(void)
uint32_t stm32_flash_getnumpages()
static void stm32_flash_unlock(void)
static uint16_t getreg16(unsigned int addr)
static bool flash_pageaddr_initialised
static void putreg16(uint16_t val, unsigned int addr)
uint32_t stm32_flash_getpagesize(uint32_t page)
static bool stm32_flash_ispageerased(uint32_t page)
bool stm32_flash_erasepage(uint32_t page)
static uint32_t flash_pageaddr[STM32_FLASH_NPAGES]
uint32_t stm32_flash_getpageaddr(uint32_t page)
int32_t stm32_flash_write(uint32_t addr, const void *buf, uint32_t count)
void stm32_flash_lock(void)