cancel
Showing results for 
Search instead for 
Did you mean: 

Forced to mass erase after successfully programming flash on STM32F401RE

themarcman
Associate III

Hello,

I am trying program the flash memory on an STM32f401RE.

After successfully programming sector 7 I cannot flash a new application to my board, instead I am met with :

20250411_18h11m05s_grim.png

Error: ST-LINK error (DEV_TARGET_NOT_HALTED)

 

I can flash new code after a mass erase with cube programmer but this shouldn't be necessary.

20250411_18h01m02s_grim.png20250411_18h01m18s_grim.png

 

Here is the code I am using.

  FLASH_EraseInitTypeDef erase_op;
  erase_op.TypeErase = FLASH_TYPEERASE_SECTORS;
  erase_op.Sector = FLASH_SECTOR_7;
  erase_op.NbSectors = 1;
  erase_op.VoltageRange = FLASH_VOLTAGE_RANGE_3;

  uint32_t err;
  if(HAL_FLASH_Unlock() == HAL_OK)
	  printf("Unlocked flash!\r\n");

  if(HAL_FLASHEx_Erase(&erase_op, &err) == HAL_OK)
	  printf("Erased memory!\r\n");

  char str[] = "Hell";
  uint64_t data = 0;
  memcpy(&data, str, 4);

  if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x08060000, data) == HAL_OK)
	  printf("Flash programming was successful!\r\n");

  printf("Flash error : %lu\r\n", HAL_FLASH_GetError());

  if(HAL_FLASH_Lock() == HAL_OK)
	  printf("Locked flash!\r\n");

 

Here is the layout in memory of my application :

20250411_18h22m51s_grim.png

Like I said, there isn't anything here that be affected with me erasing sector 7.

I've already written to flash on a STM32L031K6 and had no issues, I'm not sure what I'm doing wrong on this one.

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Don't do any erase/programming of FLASH during the first 1 second of your program. Doing so will cause the debugger to fail to connect during a debugging session. You can add a 1s delay with the following:

HAL_Delay(1000);
If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

4 REPLIES 4

What is the address/value of FLASH_SECTOR_7 in this context?

The F401RE has 512 KB of FLASH

Is "STM32F401xE" defined on the command line? ie building for right part

Not sure if you have other code dependencies for data in this region, or disabling SWD/JTAG or PA13/PA14 pins

Check "Connect under Reset" if NRST is available on your debug harness.

Perhaps add some delays in Reset_Handler path to main() so debugger can wrestle control of the part.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TDK
Guru

Don't do any erase/programming of FLASH during the first 1 second of your program. Doing so will cause the debugger to fail to connect during a debugging session. You can add a 1s delay with the following:

HAL_Delay(1000);
If you feel a post has answered your question, please click "Accept as Solution".

This solved it thank you so much!

The address of FLASH_SECTOR_7 is 0x08060000.

20250411_21h43m42s_grim.png

 

sector_7_addr.png

Connect under reset was enabled and I was not using the SWD pins for anything else.

I'm working on a nucleo so NRST was present.

Yes the delay at the start did the trick thank you!

 

I'm assuming that the debugger is running into a race condition with the program currently running on the MCU after the chip is reset.

They cannot both access the flash at the same time and since the MCU is erasing the flash first then the debugger gives up.

Come to think of it I've encountered this on another STM32F4 before and had not figured out what it was.

 

Thanks again for your help I appreciate you both!