Skip to main content
Visitor II
August 2, 2020
Solved

STM32F429 Flash erase not working on specific sectors.

  • August 2, 2020
  • 2 replies
  • 880 views

Hey,

I'm adding a bootloader to my project that sits in the first 2 sectors of internal flash.

while trying to preform the update it seems that i can't erase sector 3 all other sectors that follow this sector are erased properly.

this is the code I'm using to erase the specified sectors:

 uint32_t SectorError = 0;
 FLASH_EraseInitTypeDef EraseInitStruct;
 HAL_StatusTypeDef flashStatus = HAL_OK;
 
 /* Fill EraseInit structure*/
 EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
 EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
 EraseInitStruct.NbSectors = 8;
 EraseInitStruct.Sector = FLASH_SECTOR_3;
 
 /* Perform Erase Sector */
 do
 {
 flashStatus = HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);
 } while(flashStatus != HAL_OK);

I also tried disabling write protection with:

 FLASH_OBProgramInitTypeDef OBStruct= {0};
 HAL_StatusTypeDef status = HAL_OK;
 HAL_FLASH_Unlock();
 HAL_FLASH_OB_Unlock();
 
 /* Bank 1 */
 OBStruct.Banks = FLASH_BANK_1;
 OBStruct.WRPSector = OB_WRP_SECTOR_All;
 OBStruct.OptionType = OPTIONBYTE_WRP;
 OBStruct.WRPState = OB_WRPSTATE_DISABLE;
 
 status = HAL_FLASHEx_OBProgram(&OBStruct);
 
 HAL_FLASH_OB_Lock();
 HAL_FLASH_Lock();
 

All help will be appreciated.

    This topic has been closed for replies.
    Best answer by TDK

    Flash sectors are numbered starting with 0. The third flash sector is FLASH_SECTOR_2 = 2.

    2 replies

    TDKAnswer
    Super User
    August 2, 2020

    Flash sectors are numbered starting with 0. The third flash sector is FLASH_SECTOR_2 = 2.

    NYulz.1Author
    Visitor II
    August 2, 2020

    Thank you very much for finding my stupid mistake.