cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F051 that does not erase flash page despite use of ST code

jbaumer
Associate
Posted on August 24, 2016 at 13:48

I have been working on a STM32F051 for a while now. Though despite using reference code from the manual (page 936 in RM0091) for erasing flash, the device does not erase the page. I have traced it into the erase routine where the Flash->FAR (Page address of page to erase). This is not set in the debugger, though I do not understand why. Accoring to reference manual I have added the unlock functionality as well - still no success.

Does anyone have a clue on what to look for? - E.g. is it necessary to disable interrupts during flashing or are they just not processed during the flashing process?

Thanks in advance

Ideas on what to look for.
3 REPLIES 3
slimen
Senior
Posted on August 24, 2016 at 14:37

Hi,

May be this example can help you as describes how to configure and use the FLASH HAL API to erase and program the internal FLASH memory:

STM32Cube_FW_F4_V1.11.0\Projects\STM32091C_EVAL\Examples\FLASH\FLASH_EraseProgram

Regards

Posted on August 24, 2016 at 15:06

Tracing with a debugger is not recommended, it is generally more invasive than you want.

Instrument the code so you can output status/progress via a serial connection without having to single-step or break-point.

Providing code that demonstrates the issue completely might be more illustrative than describing the problem.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jbaumer
Associate
Posted on August 25, 2016 at 09:11

Usage of the debugger is just done hence I noticed that the functionality was not running.

It seems that the flash registers are all set (except for the FAR register (Flash Address Register)). I know that it is marked as write only, but how can I make sure that it is actually set, when the process fails? The code that I turned to use is as previously mentioned the code from the reference manual:

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

I might need to mention that the MCU is running an OS, but the OS is stopped during flash operation. Is it required to disable interrupts as well? (I have tried with that too with same poor result, but I am not sure if it is required.)