2026-05-14 11:51 AM - edited 2026-05-14 11:58 PM
Hi,
I am building a program that can flash new firmware onto a STM32H743ZIT6 via CAN. It does so by utilizing the dual bank feature and ping-ponging between bank 1 and bank 2. It receives the firmware via CAN and buffers it. Before writing it to the inactive bank it first erases said bank.
When running form bank 1 this works just fine and it is able to reboot to the newly flashed bank. When going from bank 2 back to bank 1 however, it encounters an issue. It crashes in the erase step and will not restart.
After I manually disable the SWAP_BANK bit using STM32CubeProgrammer it runs again on bank 1 without flashing again. This (I think) tells me it is deleting bank 2 while also running on it.
Attached you find the function in question, do you have any idea where i went wrong?
void foc_erase_inactive_bank() {
// Determine inactive bank from SWAP_BANK bit
uint8_t inactive_bank = (FLASH->OPTCR & FLASH_OPTCR_SWAP_BANK) ? 1 : 2;
uint32_t bank_to_erase = (inactive_bank == 1) ? FLASH_BANK_1 : FLASH_BANK_2;
LOG_INFO("FOC [ERASE] Starting mass erase of bank %u", inactive_bank);
// unlock flash
if (HAL_FLASH_Unlock() != HAL_OK) {
LOG_ERROR("FOC [ERASE] HAL_FLASH_Unlock failed");
foc.state = FOC_STATE_ERROR;
return;
}
// Disable instruction cache prior to flash operation
SCB_DisableICache();
// Configure and execute mass erase
FLASH_EraseInitTypeDef erase_init = {
.TypeErase = FLASH_TYPEERASE_MASSERASE,
.Banks = bank_to_erase,
.VoltageRange = FLASH_VOLTAGE_RANGE_3,
};
uint32_t erase_error = 0U;
HAL_StatusTypeDef erase_result = HAL_FLASHEx_Erase(&erase_init, &erase_error);
// Re-enable cache and lock flash
SCB_EnableICache();
HAL_FLASH_Lock();
// handle error
if (erase_result != HAL_OK) {
uint32_t hal_error = HAL_FLASH_GetError();
LOG_ERROR(
"FOC [ERASE] Mass erase failed with status %lu, HAL error: 0x%08X",
(unsigned long)erase_result, hal_error);
foc.state = FOC_STATE_ERROR;
return;
}
LOG_INFO("FOC [ERASE] Successfully erased bank %u", inactive_bank);
}
2026-05-14 3:37 PM
Is this STM32H743ZIT6 ?
2026-05-14 11:57 PM
Yes, absolutely, i mistyped. It's the STM32H743ZIT6. Thanks!
2026-05-15 2:11 AM - edited 2026-05-15 2:21 AM
Hello @Joni
Please refer to the application note and the article below:
STM32 in-application programming (IAP) using the USART - Application note
How to configure swap bank on STM32H7 part 1 - STMicroelectronics Community