STM32F407VE erase sector issues

152 views Asked by At

Never mind ...

I shot myself in the foot. I didn't select all the options properly and the program ended up erasing part of itself.

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

I'm doing a bare metal bootloader and am having some strange results when erasing FLASH sectors. I've found a work around but I'd like to know the root cause.

I started with this loop:

#define APP_first_sector 2
#define LAST_SECTOR 7
HAL_FLASH_Unlock();
for (uint32_t i =  APP_first_sector; i <= LAST_SECTOR; i++) {
  FLASH_Erase_Sector(i, VOLTAGE_RANGE_3);
}
HAL_FLASH_Lock();

The result was sometimes it worked but took minutes to complete and sometimes it would erase the first two or three sectors (sectors 2, 3 & 4) and then get lost.

Adding a print statement to the loop seemed to solved both issues.

#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);
}
HAL_FLASH_Lock();

A couple of days ago I came back to the project, made some changes in another area, and found that it now would erase sectors 2,3 & 4 and then hang. Backing out the changes didn't fix the problem.

The hang turned out to be in the sprintf function. It was processing the va_args list but never finding the end.

I was able to 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);

The only active interrupt in the system is Systick. All exception and isr vectors are populated. Only reset and hard fault have unique handlers. All others just return.

I've tried adding delays to the loop, moving the LOCK/UNLOCK into the loop, putting the print after the erase command, adding "while busy" loops but nothing affected the behavior.

Came to find out that all FLASH accesses are suspended when a sector erase is in progress. Since this is executing out of FLASH, the program halts. I assume that the timers/clocks still keep going.

1

There are 1 answers

0
Bob K On

Unintentional self-modifying code is not good!