flash erase fail
hello:
I use STM32H7A3VGT6 in our board, two flash regions are available in the MCU and their start address are 0x08000000 and 0x08080000, all in bank1, when I use STM32CubeProgrammer to erase more than 3 sectors from 0x08080000, it works well, but if I do the coding and erase over 3 sectors from 0x08080000,it fails when erase at the 3rd sector, crash in FLASH_WaitForLastOperation, the flash erase code is below, can you help me find out the casue? Thanks.
Bill
static uint32_t GetSector(uint32_t Address)
{
uint32_t sector = 0;
if (Address < (FLASH_BASE + FLASH_BANK_SIZE))
{
sector = (Address - FLASH_BASE) / FLASH_SECTOR_SIZE;
}
else
{
sector = (Address - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_SECTOR_SIZE;
}
return sector;
}
HAL_StatusTypeDef flash_erase(uint32_t dest, uint32_t size)
{
uint32_t FirstSector = 0, NbOfSectors = 0, SECTORError = 0;
HAL_StatusTypeDef status = HAL_OK;
FLASH_EraseInitTypeDef EraseInitStruct;
if(size > FLASH_BANK_SIZE
|| dest < FLASH_BANK1_BASE
|| dest >= FLASH_BANK2_BASE)
{
return HAL_ERROR;
}
/* Disable instruction cache prior to internal cacheable memory update */
SCB_DisableICache();
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
/* Get the 1st sector to erase */
FirstSector = GetSector(dest);
/* Get the number of sector to erase from 1st sector*/
NbOfSectors = GetSector(dest + size) - FirstSector + 1;
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.Banks = FLASH_BANK_1;
EraseInitStruct.Sector = FirstSector;
EraseInitStruct.NbSectors = NbOfSectors;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
{
/*
Error occurred while sector erase.
User can add here some code to deal with this error.
SECTORError will contain the faulty sector and then to know the code error on this sector,
user can call function 'HAL_FLASH_GetError()'
*/
status = HAL_ERROR;
}
/* Lock the Flash to disable the flash control register access (recommended
to protect the FLASH memory against possible unwanted operation) *********/
HAL_FLASH_Lock();
/* Enable instruction cache prior to internal cacheable memory update */
SCB_EnableICache();
return status;
}
