cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F407VE sector 4 erase problems

BKuhn
Associate III

I made a mistake a few days ago that resulted in my program erasing part of itself.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

I'm doing a bare metal bootloader on a STM32F407VE and have run into some weird behaviour after erasing sector 4.

Here's the loop I use to do the erasing:

#define APP_first_sector 2
#define LAST_SECTOR 7
HAL_FLASH_Unlock();
for (uint32_t i =  APP_first_sector; i <= LAST_SECTOR; i++) {
  print("Erasing sector: %d\n",(uint16_t)i);
  FLASH_Erase_Sector(i, VOLTAGE_RANGE_3);
print("finished sector: %d\n",(uint16_t)i);
}
HAL_FLASH_Lock();

All print statements executed after erasing sector 4 hang. The hang is in the processing of the VA_ARGS list. It never completes.

I can get around this by adding a '\0' to all print statements executed after the erase sector command. This explicitly adds the terminating code to the va_args list but gives compile warnings.

print("Erasing sector: %d\n\0",(uint16_t)i);

I've done a lot of variations on the loop and the only time there are problems is immediately after erasing sector 4. I can erase sectors 2,3,5, 6 or 7 without issues.

I'm using Ozone to debug the program. I don't see anything unusual/different in the FLASH ACR or status registers after the sector erases.

I've thrown a lot of things into and around the loop to see if anything helps. Here's some of the things I've unsucessfully tried:

  • diabled IRQs before and enabled IRQs after
  • delays of up to 60 seconds before and after the erase
  • moving the lock/unlock into and out of the loop
  • removing the print statements from the loop
  • disable/flush all the advanced FLASH features before the erase and then flush/restore them after

The weirdest thing is this failing mode is only cleared when I use Ozone to rewrite the bootloader image. Pressing the reset button, issuing a reset via Ozone and even removing power from the board doesn't fix this. A diff of the FLASH when in the failing mode and after a new download didn't show any problems/differences.

I'm confused.

 Edited by moderation team to adhere to community guidelines.

4 REPLIES 4

Please edit and expand on the details of the problem(s)

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

Is any of your code in the sector you are erasing?

Do you have the VCAP pins wired properly, with the right capacitors?

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

Bingo - I was erasing part of my program.

FBL
ST Employee

Hello @BKuhn​,

The user area should be in another bank than the code.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.