diff --git a/src/canboot_main.c b/src/canboot_main.c index bd3301f..b9133dc 100644 --- a/src/canboot_main.c +++ b/src/canboot_main.c @@ -33,7 +33,7 @@ #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_pos = 0; static uint16_t page_count = 0; @@ -126,7 +126,7 @@ static void write_page(uint16_t page) { 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; } @@ -137,7 +137,8 @@ process_page(void) { if (page_pos == last_page_pos) { 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++); if (page_pos % CONFIG_BLOCK_SIZE == 0) { current_state = CMD_PENDING; @@ -204,10 +205,10 @@ canboot_process_rx(uint32_t id, uint32_t len, uint8_t *data) break; case RX_BLOCK: // read into into the page buffer - if (page_pos >= CONFIG_FLASH_PAGE_SIZE) + if (page_pos >= sizeof(page_buffer)) return; - else if (page_pos + len > CONFIG_FLASH_PAGE_SIZE) - len = CONFIG_FLASH_PAGE_SIZE - cmd_pos; + else if (page_pos + len > sizeof(page_buffer)) + len = sizeof(page_buffer) - page_pos; memcpy(&page_buffer[page_pos], data, len); page_pos += len; break; diff --git a/src/stm32/Kconfig b/src/stm32/Kconfig index 0d909b3..59d0228 100644 --- a/src/stm32/Kconfig +++ b/src/stm32/Kconfig @@ -167,21 +167,10 @@ config STM32F0_TRIM Default is 16 (use factory default). Each increment increases the clock rate by ~240KHz. -choice -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 +config MAX_FLASH_PAGE_SIZE hex default 0x400 if MACH_STM32F042 - default 0x800 if MACH_STM32F072 - default 0x400 if MACH_STM32F103 && STM32F1_PAGE_SIZE_400 - default 0x800 if MACH_STM32F103 && STM32F1_PAGE_SIZE_800 + default 0x800 if MACH_STM32F072 || MACH_STM32F103 default 0x400 config BLOCK_SIZE diff --git a/src/stm32/flash.c b/src/stm32/flash.c index 16a4b18..e7ecf56 100644 --- a/src/stm32/flash.c +++ b/src/stm32/flash.c @@ -8,6 +8,18 @@ #include "autoconf.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 unlock_flash(void) { @@ -28,8 +40,9 @@ void flash_write_page(uint16_t page_index, uint16_t *data) { // 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 + - (page_index * CONFIG_FLASH_PAGE_SIZE)); + (page_index * flash_page_size)); // make sure flash is unlocked if (FLASH->CR & FLASH_CR_LOCK) @@ -46,7 +59,7 @@ flash_write_page(uint16_t page_index, uint16_t *data) // Write page 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]; 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++) buffer[i] = block_addr[i]; -} \ No newline at end of file +} diff --git a/src/stm32/flash.h b/src/stm32/flash.h index 4d8afbe..04045ef 100644 --- a/src/stm32/flash.h +++ b/src/stm32/flash.h @@ -3,8 +3,9 @@ #include +uint32_t flash_get_page_size(void); void flash_complete(void); void flash_write_page(uint16_t page_index, uint16_t *data); void flash_read_block(uint16_t block_index, uint32_t *buffer); -#endif \ No newline at end of file +#endif