2021-10-07 03:07 AM
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
Solved! Go to Solution.
2021-10-19 09:43 PM
@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:
2021-10-07 03:39 AM
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
2021-10-07 03:49 AM
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
2021-10-07 04:00 AM
What do you mean by 'destroy the tasks'?
2021-10-07 04:08 AM
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
2021-10-07 04:14 AM
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.
2021-10-07 04:18 AM
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
2021-10-07 04:18 AM
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?
2021-10-07 04:23 AM
#define configUSE_PREEMPTION 0
Cooperative
2021-10-07 05:01 AM
Other access to FLASH will stall the processor, for 128KB sectors don't they spec 2seconds worst case.
Code footprint needs to run entirely from RAM, or different Flash BANK