Why Flash is not erased Properly using HAL API? What would be the flash value after erasing? should it return 0xFF?
I am working on STM32H743 series, and I want to do Write operation continuously. When the flash is erased by STM32CubeProgrammer it returns 0xFF, but after erasing with HAL API (HAL_FLASHEx_Erase()), I got the value other than 0xFF (i.e previously flashed value). Does it mean it is not erased properly? if Yes then what is missing here?
Here is the code that i am using to erase the flash.
#define APP_ADDRESS ((uint32_t)0x08100000)
HAL_FLASH_Unlock();
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.Banks = FLASH_BANK_2;
EraseInitStruct.Sector = APP_ADDRESS;
EraseInitStruct.NbSectors = 1;
FlashStatus = FLASH_WaitForLastOperation(HAL_MAX_DELAY, flash_ptr);
printf("FlashStatus: %d\r\n", FlashStatus);
if (FlashStatus != HAL_OK) {
printf("Failed to complete last operation\r\n");
} else {
printf(" Last operation completed\r\n");
}
while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET) { }
while((*(__IO uint32_t*)flash_ptr) != 0xFFFFFFFF) {
printf("Sector not Erased.. Read %lx\r\n", (*(__IO uint32_t*)flash_ptr));
FlashStatus = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
if (FlashStatus != HAL_OK) {
HAL_FLASH_Lock();
return FlashStatus;
} else {
printf("Sector Erased.. Read %lx\r\n", (*(__IO uint32_t*)flash_ptr));
printf("Erase - No error flags\r\n");
HAL_FLASH_Lock();
return FlashStatus;
}
}
