cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_FLASHEx_Erase() fails using FreeRTOS

Gde C
Associate

Hi all,

On an STM32G070, I would like to erase flash sector during execution of freertos but I always have an error HAL_FLASH_ERROR_PGA, and when i use the function HAL_FLASH_Lock() the app crash into HardFault_Handler().

My code

erase_conf.Banks = FLASH_BANK_1;

   erase_conf.TypeErase = FLASH_TYPEERASE_PAGES;

   erase_conf.Page = cg_flash_get_page(0x08012000);

   erase_conf.NbPages = cg_flash_get_page(g_prog_addr[INDEX_TMP_APP] + FLASH_APP_MAX_SIZE - 1) - cg_flash_get_page(g_prog_addr[INDEX_TMP_APP]);

   __disable_irq();

   HAL_FLASH_Unlock();

   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR);

   HAL_FLASHEx_Erase(&erase_conf, &pageError);

   state = HAL_FLASH_GetError();

   HAL_FLASH_Lock();

   //erasure failed (0xFFFFFFFF means that all the pages have been correctly erased)

   if(pageError != 0xFFFFFFFF){

       cg_error_handler();

   }

   __enable_irq();

I have tried this code without disabling irq and with susending other task but iwdg task but nothing works.

I also tried this code without freertos and this time it works.

I think there is some kind of conflict beetween freertos and flash erase/write but I don't know where it comes from.

Do you have any ideas?

2 REPLIES 2
TDK
Guru

Why are you clearing flags prior to HAL_FLASHEx_Erase? If flags are set, I would run those to ground rather than silently ignoring them.

> when i use the function HAL_FLASH_Lock() the app crash into HardFault_Handler().

The HAL_FLASH_Lock code is absurdly simple. Unlikely that's causing the issue. Debug the hard fault, examine SCB registers, etc.

/**
  * @brief  Locks the FLASH control register access
  * @retval HAL Status
  */
HAL_StatusTypeDef HAL_FLASH_Lock(void)
{
  /* Set the LOCK Bit to lock the FLASH Registers access */
  FLASH->CR |= FLASH_CR_LOCK;
  
  return HAL_OK;  
}

If you feel a post has answered your question, please click "Accept as Solution".
Gde C
Associate

Hi,

Thanks for the reply, I'm clearing the flags because on some forums, I saw those commands.

I have check the SCB and when my app crash I get this state and the only one who change is PENDSTSET0693W00000Hqs5WQAR.png0693W00000Hqs5CQAR.png