1
\$\begingroup\$

It might be a silly question but I can't figure out in what cases a flash mass erase from in-application programming (IAP) is relevant, as it would erase the user code as well?

For STM32 devices, ST provides example code in reference manuals like this one:

A.2.4 Mass erase sequence code example

/* (1) Set the MER bit in the FLASH_CR register to enable mass erasing */
/* (2) Set the STRT bit in the FLASH_CR register to start the erasing */
/* (3) Wait until the BSY bit is reset in the FLASH_SR register */
/* (4) Check the EOP flag in the FLASH_SR register */
/* (5) Clear EOP flag by software by writing EOP at 1 */
/* (6) Reset the PER Bit to disable the mass erase */
FLASH->CR |= FLASH_CR_MER; /* (1) */
FLASH->CR |= FLASH_CR_STRT; /* (2) */
while ((FLASH->SR & FLASH_SR_BSY) != 0) /* (3) */
{
 /* For robust implementation, add here time-out management */
}
if ((FLASH->SR & FLASH_SR_EOP) != 0) /* (4)*/
{
 FLASH->SR = FLASH_SR_EOP; /* (5) */
}
else
{
 /* Manage the error cases */
}
FLASH->CR &= ~FLASH_CR_MER; /* (6) */

I don't understand how the CPU can reach the instructions from (3) to (6).

Shouldn't they be erased already?

\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

The program might not be running from flash. The STM32 can run code from SRAM or external memory (via FMC); code running from these locations could safely* erase flash memory.

Some STM32 parts also have two banks of flash memory. Code could be running from one bank and erasing the other bank.

*: Well, mostly safely. A power failure during an erase/program cycle would leave the part blank.

\$\endgroup\$
2
  • \$\begingroup\$ "Do not power down device during update". Of course that statement is worthless some in industrial applications \$\endgroup\$
    – Hans
    Commented Sep 17, 2017 at 8:33
  • \$\begingroup\$ if you do your system design correctly then you will have on board power storage (bulk capacitance, battery, ...) that will survive this use case, if it is required as a valid use case. Industrial or not...if the requirements were not done and/or the system engineering wasnt done then sure you can end up in a situation where the power goes down during update. and in that case you don tdo a mass erase, you do partial erases and design the software such that it knows which block to choose to run from on the next boot, or second or third boot. \$\endgroup\$
    – old_timer
    Commented Sep 17, 2017 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.