Skip to main content
Visitor II
August 23, 2025
Solved

Can't Erase Flash on STM32H5

  • August 23, 2025
  • 2 replies
  • 386 views

I am trying to erase a sector of the flash memory on my STM32H503RB.  I have successfully validated that I can write to the flash, but when I attempt to erase the sector that I wrote, the contents that I wrote stay the same.  I have a feeling that I am just erasing the wrong sector, but I can't seem to find where I went wrong.  I'm pretty sure that I can erase, because if I put in a deliberately wrong sector number (sector 4, which corresponds to the location of the LL drivers) the device hard faults (which is what you expect when you erase the drivers).

I am trying to erase the sector starting at 0x0801_0000, which I would expect to be sector 8. (sectors are 8 kiB, correct?)  I have tried erasing sector 8 of bank 1, sector 8 of bank 2, and sector 0 of bank 2, but none of these seem to work.  Am I misinterpreting how the banks and sectors interact?

Code:

#define FLASH_BASE_ADDR (0x08000000UL)
// FLASH_SECTOR_SIZE is 0x2000U
FLASH_EraseInitTypeDef erase = { 0 };
uint32_t addr = 0x08010000;
erase.TypeErase = FLASH_TYPEERASE_SECTORS;
erase.Banks = FLASH_BANK_1; // I have also tried FLASH_BANK_2
erase.Sector = (addr - FLASH_BASE_ADDR) / FLASH_SECTOR_SIZE;
erase.NbSectors = 1;
uint32_t error;
HAL_FLASH_Unlock();
HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&erase, &error); // status is HAL_OK
HAL_FLASH_Lock();
    This topic has been closed for replies.
    Best answer by TDK

    > uint32_t addr = 0x08010000;

    Try erasing sector 0 of bank 2. Also ensure banks are not swapped.

    TDK_0-1755960388767.png

     

    Try the same thing with STM32CubeProgrammer to see if it you get success there.

    2 replies

    TDKAnswer
    Super User
    August 23, 2025

    > uint32_t addr = 0x08010000;

    Try erasing sector 0 of bank 2. Also ensure banks are not swapped.

    TDK_0-1755960388767.png

     

    Try the same thing with STM32CubeProgrammer to see if it you get success there.

    colin0325Author
    Visitor II
    August 23, 2025

    I'm not sure what I did the first time I tried Bank 2 Sector 0, but it seems to have worked this time.