From 84969194a18ec015d2508db14be66457f123f687 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 29 Nov 2021 13:01:51 +1100 Subject: [PATCH] stm32/flashbdev: Support generic flash storage config via link symbols. A board can now define the following linker symbols to configure its flash storage layout: _micropy_hw_internal_flash_storage_start _micropy_hw_internal_flash_storage_end _micropy_hw_internal_flash_storage_ram_cache_start _micropy_hw_internal_flash_storage_ram_cache_end And optionally have a second flash segment by configuring MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE_SEGMENT2 to 1 and defining: _micropy_hw_internal_flash_storage2_start _micropy_hw_internal_flash_storage2_end Signed-off-by: Damien George --- ports/stm32/flashbdev.c | 30 ++++++++++++++++++++++++++++-- ports/stm32/mpconfigboard_common.h | 5 +++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/ports/stm32/flashbdev.c b/ports/stm32/flashbdev.c index 3b7609d4d2..ce535d0c00 100644 --- a/ports/stm32/flashbdev.c +++ b/ports/stm32/flashbdev.c @@ -133,11 +133,35 @@ extern uint8_t _ram_fs_cache_end[]; #define FLASH_MEM_SEG1_NUM_BLOCKS ((&_flash_fs_end - &_flash_fs_start) / 512) #else -#error "no internal flash storage support for this MCU" + +// Generic configuration where the linker script specifies flash storage and RAM cache locations. + +extern uint8_t _micropy_hw_internal_flash_storage_start; +extern uint8_t _micropy_hw_internal_flash_storage_end; +extern uint8_t _micropy_hw_internal_flash_storage2_start; +extern uint8_t _micropy_hw_internal_flash_storage2_end; +extern uint8_t _micropy_hw_internal_flash_storage_ram_cache_start[]; +extern uint8_t _micropy_hw_internal_flash_storage_ram_cache_end[]; + +#define CACHE_MEM_START_ADDR \ + ((uintptr_t)&_micropy_hw_internal_flash_storage_ram_cache_start[0]) +#define FLASH_SECTOR_SIZE_MAX \ + (&_micropy_hw_internal_flash_storage_ram_cache_end[0] - &_micropy_hw_internal_flash_storage_ram_cache_start[0]) +#define FLASH_MEM_SEG1_START_ADDR \ + ((long)&_micropy_hw_internal_flash_storage_start) +#define FLASH_MEM_SEG1_NUM_BLOCKS \ + ((&_micropy_hw_internal_flash_storage_end - &_micropy_hw_internal_flash_storage_start) / 512) + +#if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE_SEGMENT2 +#define FLASH_MEM_SEG2_START_ADDR \ + ((long)&_micropy_hw_internal_flash_storage2_start) +#define FLASH_MEM_SEG2_NUM_BLOCKS \ + ((&_micropy_hw_internal_flash_storage2_end - &_micropy_hw_internal_flash_storage2_start) / 512) +#endif + #endif #if !defined(FLASH_MEM_SEG2_START_ADDR) -#define FLASH_MEM_SEG2_START_ADDR (0) // no second segment #define FLASH_MEM_SEG2_NUM_BLOCKS (0) // no second segment #endif @@ -220,9 +244,11 @@ static uint32_t convert_block_to_flash_addr(uint32_t block) { if (block < FLASH_MEM_SEG1_NUM_BLOCKS) { return FLASH_MEM_SEG1_START_ADDR + block * FLASH_BLOCK_SIZE; } + #ifdef FLASH_MEM_SEG2_START_ADDR if (block < FLASH_MEM_SEG1_NUM_BLOCKS + FLASH_MEM_SEG2_NUM_BLOCKS) { return FLASH_MEM_SEG2_START_ADDR + (block - FLASH_MEM_SEG1_NUM_BLOCKS) * FLASH_BLOCK_SIZE; } + #endif // can add more flash segments here if needed, following above pattern // bad block diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 6dd51e913f..f8342f51b9 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -57,6 +57,11 @@ #define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (1) #endif +// If internal flash storage is enabled, whether to use a second segment of flash. +#ifndef MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE +#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE_SEGMENT2 (0) +#endif + // Whether to enable the RTC, exposed as pyb.RTC #ifndef MICROPY_HW_ENABLE_RTC #define MICROPY_HW_ENABLE_RTC (0)