From 802a88c3b1599777d53cadb0e4439348fe298a8e Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 14 Feb 2024 16:51:02 +1100 Subject: [PATCH] stm32/mboot: Generate FLASH_LAYOUT_STR at runtime on H5 MCUs. The size of the flash varies among MCU variants. Instead of requiring a build-time variable to configure this, compute it at runtime using the special device information word accessible through the FLASH_SIZE macro. This feature is currently only implemented for H5 MCUs, but can be extended to others. Signed-off-by: Damien George --- ports/stm32/mboot/main.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index 2424b37da3..39492b4e54 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -435,7 +435,7 @@ void mp_hal_pin_config_speed(uint32_t port_pin, uint32_t speed) { #elif defined(STM32G0) #define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/256*02Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT #elif defined(STM32H5) -#define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/256*08Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT +#define FLASH_LAYOUT_TEMPLATE "@Internal Flash /0x08000000/???*08Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT #elif defined(STM32H743xx) #define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/16*128Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT #elif defined(STM32H750xx) @@ -444,6 +444,25 @@ void mp_hal_pin_config_speed(uint32_t port_pin, uint32_t speed) { #define FLASH_LAYOUT_STR "@Internal Flash /0x08000000/256*04Kg" MBOOT_SPIFLASH_LAYOUT MBOOT_SPIFLASH2_LAYOUT #endif +#if !defined(FLASH_LAYOUT_STR) + +#define FLASH_LAYOUT_STR_ALLOC (sizeof(FLASH_LAYOUT_TEMPLATE)) + +// Build the flash layout string from a template with total flash size inserted. +static size_t build_flash_layout_str(char *buf) { + size_t len = FLASH_LAYOUT_STR_ALLOC - 1; + memcpy(buf, FLASH_LAYOUT_TEMPLATE, len + 1); + unsigned int num_sectors = FLASH_SIZE / FLASH_SECTOR_SIZE; + buf += 31; // location of "???" in FLASH_LAYOUT_TEMPLATE + for (unsigned int i = 0; i < 3; ++i) { + *buf-- = '0' + num_sectors % 10; + num_sectors /= 10; + } + return len; +} + +#endif + static bool flash_is_modifiable_addr_range(uint32_t addr, uint32_t len) { return addr + len < (uint32_t)&_mboot_protected_flash_start || addr >= (uint32_t)&_mboot_protected_flash_end_exclusive; @@ -756,8 +775,12 @@ void i2c_slave_process_rx_end(i2c_slave_t *i2c) { } else if (buf[0] == I2C_CMD_RESET && len == 0) { dfu_context.leave_dfu = true; } else if (buf[0] == I2C_CMD_GETLAYOUT && len == 0) { + #if defined(FLASH_LAYOUT_STR) len = strlen(FLASH_LAYOUT_STR); memcpy(buf, FLASH_LAYOUT_STR, len); + #else + len = build_flash_layout_str(buf); + #endif } else if (buf[0] == I2C_CMD_MASSERASE && len == 0) { len = do_mass_erase(); } else if (buf[0] == I2C_CMD_PAGEERASE && len == 4) { @@ -1161,7 +1184,15 @@ static uint8_t *pyb_usbdd_StrDescriptor(USBD_HandleTypeDef *pdev, uint8_t idx, u } case USBD_IDX_CONFIG_STR: + #if defined(FLASH_LAYOUT_STR) USBD_GetString((uint8_t *)FLASH_LAYOUT_STR, str_desc, length); + #else + { + char buf[FLASH_LAYOUT_STR_ALLOC]; + build_flash_layout_str(buf); + USBD_GetString((uint8_t *)buf, str_desc, length); + } + #endif return str_desc; case MBOOT_ERROR_STR_OVERWRITE_BOOTLOADER_IDX: