This commit is contained in:
Michal Moskal 2020-04-13 09:47:51 -07:00
Родитель 0d4338f0c2
Коммит 67bf6cd0d9
4 изменённых файлов: 77 добавлений и 1 удалений

Просмотреть файл

@ -1,6 +1,7 @@
MEMORY MEMORY
{ {
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 32K /* Last 1k is for settings */
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 32K - 1K
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 4K RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 4K
} }
INCLUDE ld/gcc_arm.ld INCLUDE ld/gcc_arm.ld

Просмотреть файл

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "hwconfig.h"
#define RAM_FUNC __attribute__((noinline, long_call, section(".data"))) #define RAM_FUNC __attribute__((noinline, long_call, section(".data")))
// exti.c // exti.c
@ -58,3 +60,8 @@ int target_in_irq(void);
// init.c // init.c
bool clk_is_pll(void); bool clk_is_pll(void);
void clk_set_pll(int on); void clk_set_pll(int on);
// flash.c
void flash_program(void *dst, const void *src, uint32_t len);
void flash_erase(void *page_addr);

52
stm32/flash.c Normal file
Просмотреть файл

@ -0,0 +1,52 @@
#include "jdstm.h"
#define WAIT_BUSY() \
while (FLASH->SR & FLASH_SR_BSY) \
;
static void unlock() {
WAIT_BUSY();
if (FLASH->CR & FLASH_CR_LOCK) {
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
}
}
static void lock() {
FLASH->CR &= ~(FLASH_CR_PG | FLASH_CR_PER);
FLASH->CR |= FLASH_CR_LOCK;
}
static void check_eop() {
WAIT_BUSY();
if (FLASH->SR & FLASH_SR_EOP)
FLASH->SR = FLASH_SR_EOP;
else
jd_panic();
}
void flash_program(void *dst, const void *src, uint32_t len) {
if ((((uint32_t)dst) & 1) || (((uint32_t)src) & 1) || (len & 1))
jd_panic();
unlock();
FLASH->CR |= FLASH_CR_PG; // enable programming
while (len--) {
*(__IO uint16_t *)(dst) = *(uint16_t *)src;
check_eop();
dst = (uint16_t *)dst + 1;
src = (uint16_t *)src + 1;
}
lock();
}
void flash_erase(void *page_addr) {
unlock();
FLASH->CR |= FLASH_CR_PER;
FLASH->AR = (uint32_t)page_addr;
FLASH->CR |= FLASH_CR_STRT;
check_eop();
lock();
}

16
stm32/hwconfig.h Normal file
Просмотреть файл

@ -0,0 +1,16 @@
#pragma once
#if defined(STM32F030x4)
#define STM32F030x6 1 // HAL requires that
#define FLASH_PAGE_SIZE 1024
#define FLASH_PAGES 16
#elif defined(STM32F031x6)
#define FLASH_PAGE_SIZE 1024
#define FLASH_PAGES 32
#else
#error "define flash size"
#endif
#define FLASH_SIZE (FLASH_PAGE_SIZE * FLASH_PAGES)
#define SETTINGS_START (FLASH_SIZE - FLASH_PAGE_SIZE)
#define SETTINGS_SIZE FLASH_PAGE_SIZE