2025-04-11 9:28 AM - edited 2025-04-12 3:17 AM
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 :
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.
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 :
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.
Solved! Go to Solution.
2025-04-11 10:38 AM
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);
2025-04-11 10:37 AM - edited 2025-04-11 10:44 AM
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.
2025-04-11 10:38 AM
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);
2025-04-11 12:38 PM
This solved it thank you so much!
2025-04-11 1:07 PM
The address of FLASH_SECTOR_7 is 0x08060000.
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!