Skip to content

Commit

Permalink
stm32/mboot: Generate FLASH_LAYOUT_STR at runtime on H5 MCUs.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dpgeorge committed Feb 20, 2024
1 parent 3db2910 commit 802a88c
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion ports/stm32/mboot/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 802a88c

Please sign in to comment.