cancel
Showing results for 
Search instead for 
Did you mean: 

stm32h7 CPU halts when I try to erase Flash sector 0 in Bank 1

AAndy.2
Associate

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();

3 REPLIES 3
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".
Pavel A.
Evangelist III

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,

AAndy.2
Associate

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.