cancel
Showing results for 
Search instead for 
Did you mean: 

Can not erase flash memory page using HAL Drivers

Radoslav Mar
Associate II
Posted on June 18, 2018 at 12:23

Hello All.

MCU: STM32F031G6U6

IDE: System Workbench

API: STM32F0 HAL and low-layer drivers

I am trying to erase page in the flash memory at address: 0x8002000.

Here is the code:

https://community.st.com/tags♯/?tags=code

‌

FLASH_EraseInitTypeDef EraseInitStruct;

uint32_t SECTORError = 0;;

bool result = true;

/* Unlock the Flash to enable the flash control register access *************/

HAL_FLASH_Unlock();

//addr = 0x08007800 just for testing

/* Erase the user Flash area

(area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

/* Fill EraseInit structure*/

EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;

EraseInitStruct.PageAddress = Address; // start address

EraseInitStruct.NbPages = 1;

if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)

{

/*

Error occurred while sector erase.

User can add here some code to deal with this error.

SECTORError will contain the faulty sector and then to know the code error on this sector,

user can call function 'HAL_FLASH_GetError()'

*/

/* Infinite loop */

while (1)

{

;// error, BALAGAN!!!

}

}

The program crashes at this line 

if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)

Nothing after this function call is executed.

Any Ideas?

#code #code::blocks
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on June 18, 2018 at 15:12

https://community.st.com/0D50X00009XkWCsSAN

Are you erasing memory your code is residing in currently? Do the routines need to be run from RAM?

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

View solution in original post

10 REPLIES 10
Posted on June 18, 2018 at 15:12

https://community.st.com/0D50X00009XkWCsSAN

Are you erasing memory your code is residing in currently? Do the routines need to be run from RAM?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 18, 2018 at 20:17

No. It says in the manual that pipelines are stalled during the flashing operations (erase, write).

I'm not sure, but I think it means the flash buffers, and when the instruction pipeline is exhausted, and nothing is coming in from the flash, also the instruction pipeline gets stalled.

I guess I'll find out in the near future. (I need to implement a flasher inside a SW). :D

Note that erasing a sector can take seconds.

From RM0091:

On the contrary, during a program/erase operation to the Flash memory, any attempt to read

the Flash memory will stall the bus. The read

operation will proceed

correctly once the

program/erase operation has completed. This means that code or data fetches cannot be

made while a program/erase operation is ongoing.

Posted on June 18, 2018 at 20:29

(It seems to be impossible to continue writing afret quote as the last item.)

So you should be able to erase the program you're running. :D

Posted on June 19, 2018 at 00:00

So you should be able to erase the program you're running

But do you erase the same page where your program is currently executing? (hint: use debugger or look at the linker map)

If yes, you cannot possibly expect that the program continues nicely.

-- pa

Posted on June 19, 2018 at 06:00

No, the program would 'suddenly' become a long stream of far branches. :D

Posted on June 20, 2018 at 09:43

Yes that is the case.I have been erasing a memory that executes.

Hello Clive,

Is there an example or application note to achieve this? I am trying to erase the entire flash but can't do that while running from flash. I need to run a routine from RAM as you have suggested.

I don't know, I generally view this stuff as core competency.

You've got to create a small self-contained block of code that can be copied to RAM and jumped too. You'll need to have no interrupts running, and you'll need to have a vector table to catch faults. You could create a whole loader, and that could manage interrupts, but it depends on the level of complexity you're comfortable with.

What STM32 part?

What is going to happen after you've erased the whole part?

What is going to happen if someone resets the blank part?

I could create an example, what are you offering?

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

Hello Clive,

Apologies for assuming that you are an employee of STM. I will discuss with STM regarding the application note.

regards