Skip to main content
Associate II
March 6, 2025
Question

Error: failed to erase memory

  • March 6, 2025
  • 2 replies
  • 1348 views

Hello,

So, i have created a small project in which the diagnostic information was saved in the internal flash of STM32F429ZET6 at the time of a crash. So, to test this project i have triggered usage-fault exception in an infinite loop by dividing a number via 0.
In the exception handler, i'm capturing and storing the diagnostic information in sector 0 of internal flash (I have modified the linker script to reserve this area, and configured sector 0 as non-executable area via MPU). The below is the logic i am using to store the diagnostic info.

 

FLASH_OBProgramInitTypeDef OBInit;

HAL_FLASH_OB_Unlock();

OBInit.OptionType = OPTIONBYTE_WRP;
OBInit.WRPSector = OB_WRP_SECTOR_0; 
OBInit.WRPState = OB_WRPSTATE_DISABLE; 

HAL_FLASHEx_OBProgram(&OBInit);
 
HAL_FLASH_OB_Launch();
HAL_FLASH_OB_Lock();


HAL_FLASH_Unlock();

HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, list_ad, p_gnl[i]);

HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, diag_ad, p_gnl[i])

HAL_FLASH_Lock();

HAL_FLASH_OB_Unlock();

OBInit.OptionType = OPTIONBYTE_WRP;
OBInit.WRPState = OB_WRPSTATE_ENABLE;
OBInit.WRPSector = OB_WRP_SECTOR_0;

HAL_FLASHEx_OBProgram(&OBInit);
HAL_FLASH_OB_Launch();
HAL_FLASH_OB_Lock();

 

And now, after i flash this code, i could not flash other code, this is the error i'm getting :

Error: failed to erase memory

 

 

Error: failed to erase memory

Encountered Error when opening C:\ST\STM32CubeIDE_1.17.0\STM32CubeIDE\plugins\com.st.stm32cube.ide.mcu.externaltools.cubeprogrammer.win32_2.2.0.202409170845\tools\bin\STM32_Programmer_CLI.exe

Error in STM32CubeProgrammer

Shutting down...

Exit.

 

Please help me fix this issue.

Thanks in advance.

 

2 replies

TDK
Super User
March 6, 2025

Dont do stuff with flash in the first 1s of a program running or the debugger will have a hard time maintaining a connection.

Hold BOOT0 high during reset to boot in the bootloader and erase the chip to recover.

"If you feel a post has answered your question, please click ""Accept as Solution""."
pozeetiveAuthor
Associate II
March 11, 2025

Hello @TDK,

I have followed your suggestion, i have provided 3.3v to BOOT0 pin and in STM CUBE Programmer and in ST-LINK Configuration i have kept the mode 'under reset', and i have tried to connect via resetting the MCU, but the connection failed and it says "the target was held under reset", but when i try to connect without resetting it is connected but i'm unable to perform chip erase/flash operations. 

 

TDK
Super User
March 11, 2025

> but i'm unable to perform chip erase/flash operations. 

Why not? What happens when you try?

"If you feel a post has answered your question, please click ""Accept as Solution""."
pozeetiveAuthor
Associate II
March 11, 2025

I'm getting an error like unable to perform sector erase as the sector is protected something like this, i have also tried to disable the nWRP for all sectors but it is also failing and i'm getting error like this operation is initiated after OB_LAUNCH.