HAL Flash Sector Erase Fails in STM32F767
I am implementing a custom bootloader for STM32F767 controller. Bootloader and application are in different sectors and on an update bootloader erases the application's sector and writes a new one. This works well, but sometimes the bootloader fails to clear the application's sector properly. Further attempts to clear it keep on failing. I can manually clear that sector with STVP and bootloader is then able to flash a new update there just fine.
Any help is appreciated.
Thanks
if(HAL_FLASH_Unlock() != HAL_OK)
return FALSE;
FLASH_EraseInitTypeDef flashErase =
{
.TypeErase = FLASH_TYPEERASE_SECTORS,
.Sector = FLASH_SECTOR_5,
.NbSectors = 1,
.VoltageRange = FLASH_VOLTAGE_RANGE_4
};
HAL_FLASHEx_Erase_IT(&flashErase);