cancel
Showing results for 
Search instead for 
Did you mean: 

Erasing flash during RTOS task execution on STM32F4

SGasp.1
Senior

Hi ..

I am using this function to erase some sectors on the flash

bool FLASH_EraseSectors(uint32_t starting_sector , uint8_t number_of_sectors)
{
	FLASH_EraseInitTypeDef erase_init;
	uint32_t page_error;
 
    /* Erase the user Flash area */
	erase_init.Banks        = FLASH_BANK_1;
	erase_init.TypeErase   	= FLASH_TYPEERASE_SECTORS;
	erase_init.NbSectors 	= number_of_sectors;
	erase_init.Sector     	= starting_sector;
	erase_init.VoltageRange = FLASH_VOLTAGE_RANGE_3;
 
	/* Unlock the Flash to enable the flash control register access *************/
	FLASH_Unlock();
 
	if (HAL_FLASHEx_Erase(&erase_init, &page_error) != HAL_OK)
	{
		FLASH_Lock();
		return false;
	}
	if (HAL_FLASHEx_Erase(&erase_init, &page_error) == HAL_OK)
	{
		FLASH_Lock();
		return true;
	}
 
}

I would like to erase some sectors without breaking the 2 tasks running...

The point is that it seems to destroy the tasks...

Is there a safe way to delete the sectors in flash during a task execution in FREERTOS ?

Thanks a lot

1 ACCEPTED SOLUTION

Accepted Solutions
Manny
Associate III

@SGasp.1​ , flashing 8 bytes will not take much time, the time taker is the sector erase process that need to be done before writing any byte in that sector.

For your uart data, if you disable interrupts, the uart receive interrupts will also disable and thus you will loss data as uart can only hold a single byte at a time, case is different if you are using DMA(I don't have experience with DMA). You can implement a strategy to ensure that no data is loss on the communication side. For example you wait for an acknowledgment for the mcu before sending the next byte of data to be programed.

See the DFU(Device firmware update) protocol application note to use as a reference to design your own bootloader protocol.

link:

https://www.st.com/resource/en/application_note/cd00264342-usart-protocol-used-in-the-stm32-bootloader-stmicroelectronics.pdf

View solution in original post

31 REPLIES 31

Hello @SGasp.1​ ,

As flash erasing is a blocking and time intensive task, it should on a lower priority task.

Usually, if the erasure is not needed immediately, it is to be performed page per page within a lower task priority.

Otherwise, if you have activated a watchdog, don't forget to feed it between page erasures or it might trigger a reset depending on the flash range to be erased and how much time it would take.

I hope this helps you.

BeST Regards,

Walid

SGasp.1
Senior

Thanks Walid..

But it is enough to call it one or maybe inside a loop in this way ?

  __disable_irq();
  while (FLASH_EraseSectors(2 ,1) == false)
  {
 
  }
  __enable_irq();

Thanks a lot

Manny
Associate III

What do you mean by 'destroy the tasks'?

SGasp.1
Senior

Hi Aharm.. thannks for your answer..

I mean can I call these instructions inside a task

  __disable_irq();
  while (FLASH_EraseSectors(2 ,1) == false)
  {
 
  }
  __enable_irq();

without blocking a Higher priority task which is responsible for the communication with another board?

Thanks

The FreeRtos Scheduler uses ISR to perform its task switching, if you disable the interrupts globally, the timer(used by rtos) interrupt will also stop, thus no task switching.

Ok so If I am able to enter in this task where is located the following code

 while (FLASH_EraseSectors(2 ,1) == false)
  {
 
  }

I should be able to erase the sectors..

because I tried without inside a while loop but I wasn't able to delete the sectors calling it just once..

Is it correct ?

Thanks for your huge support ...

Simone

if you want to other task(the communication task) to run make those task higher priority then this task(the flash erase). which Scheduler shame are you using? Cooperative or premtive?

#define configUSE_PREEMPTION           0

Cooperative

Other access to FLASH will stall the processor, for 128KB sectors don't they spec 2seconds worst case.

C​ode footprint needs to run entirely from RAM, or different Flash BANK

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