flash: Autodetect stm32f103 flash page size

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2022-05-05 11:33:29 -04:00 committed by Eric Callahan
parent 00a65c1570
commit 5279c33ea0
4 changed files with 27 additions and 23 deletions

View File

@ -33,7 +33,7 @@
#define REQUEST_SIG 0x5984E3FA6CA1589B // Random request sig #define REQUEST_SIG 0x5984E3FA6CA1589B // Random request sig
static uint8_t page_buffer[CONFIG_FLASH_PAGE_SIZE]; static uint8_t page_buffer[CONFIG_MAX_FLASH_PAGE_SIZE];
static uint8_t cmd_buf[CMD_BUF_SIZE]; static uint8_t cmd_buf[CMD_BUF_SIZE];
static uint8_t cmd_pos = 0; static uint8_t cmd_pos = 0;
static uint16_t page_count = 0; static uint16_t page_count = 0;
@ -126,7 +126,7 @@ static void
write_page(uint16_t page) write_page(uint16_t page)
{ {
flash_write_page(page, (uint16_t*)page_buffer); flash_write_page(page, (uint16_t*)page_buffer);
memset(page_buffer, 0xFF, CONFIG_FLASH_PAGE_SIZE); memset(page_buffer, 0xFF, sizeof(page_buffer));
page_pos = 0; page_pos = 0;
} }
@ -137,7 +137,8 @@ process_page(void) {
if (page_pos == last_page_pos) { if (page_pos == last_page_pos) {
return; return;
} }
if (page_pos == CONFIG_FLASH_PAGE_SIZE) uint32_t flash_page_size = flash_get_page_size();
if (page_pos == flash_page_size)
write_page(page_count++); write_page(page_count++);
if (page_pos % CONFIG_BLOCK_SIZE == 0) { if (page_pos % CONFIG_BLOCK_SIZE == 0) {
current_state = CMD_PENDING; current_state = CMD_PENDING;
@ -204,10 +205,10 @@ canboot_process_rx(uint32_t id, uint32_t len, uint8_t *data)
break; break;
case RX_BLOCK: case RX_BLOCK:
// read into into the page buffer // read into into the page buffer
if (page_pos >= CONFIG_FLASH_PAGE_SIZE) if (page_pos >= sizeof(page_buffer))
return; return;
else if (page_pos + len > CONFIG_FLASH_PAGE_SIZE) else if (page_pos + len > sizeof(page_buffer))
len = CONFIG_FLASH_PAGE_SIZE - cmd_pos; len = sizeof(page_buffer) - page_pos;
memcpy(&page_buffer[page_pos], data, len); memcpy(&page_buffer[page_pos], data, len);
page_pos += len; page_pos += len;
break; break;

View File

@ -167,21 +167,10 @@ config STM32F0_TRIM
Default is 16 (use factory default). Each increment increases Default is 16 (use factory default). Each increment increases
the clock rate by ~240KHz. the clock rate by ~240KHz.
choice config MAX_FLASH_PAGE_SIZE
depends on MACH_STM32F103
prompt "Flash Page Size"
config STM32F1_PAGE_SIZE_400
bool "Low/Medium Density (1024 KiB Page Size)"
config STM32F1_PAGE_SIZE_800
bool "High Density (2048 KiB Page Size)"
endchoice
config FLASH_PAGE_SIZE
hex hex
default 0x400 if MACH_STM32F042 default 0x400 if MACH_STM32F042
default 0x800 if MACH_STM32F072 default 0x800 if MACH_STM32F072 || MACH_STM32F103
default 0x400 if MACH_STM32F103 && STM32F1_PAGE_SIZE_400
default 0x800 if MACH_STM32F103 && STM32F1_PAGE_SIZE_800
default 0x400 default 0x400
config BLOCK_SIZE config BLOCK_SIZE

View File

@ -8,6 +8,18 @@
#include "autoconf.h" #include "autoconf.h"
#include "internal.h" #include "internal.h"
uint32_t
flash_get_page_size(void)
{
if (CONFIG_MACH_STM32F103) {
// Check for a 1K page size on the stm32f103
uint16_t *flash_size = (void*)FLASHSIZE_BASE;
if (*flash_size < 256)
return 0x400;
}
return CONFIG_MAX_FLASH_PAGE_SIZE;
}
static void static void
unlock_flash(void) unlock_flash(void)
{ {
@ -28,8 +40,9 @@ void
flash_write_page(uint16_t page_index, uint16_t *data) flash_write_page(uint16_t page_index, uint16_t *data)
{ {
// A page_index of 0 is the first page of the application area // A page_index of 0 is the first page of the application area
uint32_t flash_page_size = flash_get_page_size();
uint16_t* page_addr = (uint16_t*)(CONFIG_APPLICATION_START + uint16_t* page_addr = (uint16_t*)(CONFIG_APPLICATION_START +
(page_index * CONFIG_FLASH_PAGE_SIZE)); (page_index * flash_page_size));
// make sure flash is unlocked // make sure flash is unlocked
if (FLASH->CR & FLASH_CR_LOCK) if (FLASH->CR & FLASH_CR_LOCK)
@ -46,7 +59,7 @@ flash_write_page(uint16_t page_index, uint16_t *data)
// Write page // Write page
FLASH->CR |= FLASH_CR_PG; FLASH->CR |= FLASH_CR_PG;
for (uint16_t i = 0; i < CONFIG_FLASH_PAGE_SIZE / 2; i++) for (uint16_t i = 0; i < flash_page_size / 2; i++)
{ {
page_addr[i] = data[i]; page_addr[i] = data[i];
while (FLASH->SR & FLASH_SR_BSY); while (FLASH->SR & FLASH_SR_BSY);
@ -66,4 +79,4 @@ flash_read_block(uint16_t block_index, uint32_t *buffer)
for (uint8_t i = 0; i < CONFIG_BLOCK_SIZE / 4; i++) for (uint8_t i = 0; i < CONFIG_BLOCK_SIZE / 4; i++)
buffer[i] = block_addr[i]; buffer[i] = block_addr[i];
} }

View File

@ -3,8 +3,9 @@
#include <stdint.h> #include <stdint.h>
uint32_t flash_get_page_size(void);
void flash_complete(void); void flash_complete(void);
void flash_write_page(uint16_t page_index, uint16_t *data); void flash_write_page(uint16_t page_index, uint16_t *data);
void flash_read_block(uint16_t block_index, uint32_t *buffer); void flash_read_block(uint16_t block_index, uint32_t *buffer);
#endif #endif