2022-03-24 12:30 PM
I have a problem with erasing sector 0 in Bank 1 of a stm32h7 CPU. See my code snipped below...
When I run the code in the debugger, the CPU never returns from the call to HAL_FLASHEx_Erase. When I interrupt the programm the PC is set to 0xFFFFFFFE which I learned, means the CPU has stopped.
The code is called from BL_SBSFU bootloader and runs in a complete copy of the bootloader in RAM. The code seems to be correct, because when I try to erase sector 1 or bank 2 the code runs without an error and the sector is erased.
Only when I try to erase sector 0 the CPU stops.
The code runs completely in RAM and does not access the FLASH so I think there should be no problem in deleting sector 0.
Has anyone an idea why the CPU stops when I try to erase sector 0 ?
__disable_irq();
if (HAL_FLASH_Unlock() == HAL_OK)
{
FLASH_EraseInitTypeDef p_erase_init;
p_erase_init.TypeErase = FLASH_TYPEERASE_SECTORS;
p_erase_init.Banks = FLASH_BANK_1;
p_erase_init.Sector = 0;
p_erase_init.NbSectors = 1U;
p_erase_init.VoltageRange = FLASH_VOLTAGE_RANGE_3;
if (HAL_FLASHEx_Erase(&p_erase_init, &page_error) != HAL_OK)
{
ret = SFU_ERROR;
}
}
__enable_irq();
2022-03-24 12:45 PM
I would guess you're not running entirely from RAM. Perhaps your code is, but constants and other read-only variables may still be in FLASH.
If erasing an unused page works, that's likely the issue. If not, then step through and debug that.
2022-03-24 04:24 PM
Looks similar to this issue.
If unsure that the code runs entirely from RAM, disable any access to the flash (0x08000000 - 0x081FFFFF) in MPU while erasing,
2022-03-25 07:10 AM
Thanks for the advices. I looked deeper in the code of the erase function and eliminated everything not necessary. Now the erase function works.
But now I have another problem with the Flash function. The behaviour is very strange. If I let the program run I get an ECC error on the first call of HAL_FLASH_Program. When I debug the program it is possible to flash two or three 128 bit chunks before the ECC error occurs. The number of calls that are working in the debugger is not constant and usually less than 5. Therefore I think there must be another reason than accessing addresses in the (now erased) flash.