cancel
Showing results for 
Search instead for 
Did you mean: 

Hardfault (FORCED, IMPRECISERR) when erasing flash page. [STM32WB55]

TomC
Senior

Hi there,

I am receiving a hardfault when erasing pages of flash, seemingly randomly. I can recreate the issue somewhat consistently if I rapidly erase flash pages. I have followed the example project from the STM32WB firmware package (erase flash example). 

This is my function for erasing flash.

 

flash_status_t user_flash_erase_page(uint8_t page_number, uint8_t number_of_pages)
{
//  static FLASH_EraseInitTypeDef EraseInitStruct = {0};
  static FLASH_EraseInitTypeDef EraseInitStruct = {0};

  if(HAL_FLASH_Unlock()!= HAL_OK)
  {
    return FLASH_ERROR;
  }

  /* Clear OPTVERR bit set on virgin samples */
  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);

  EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  EraseInitStruct.Page = page_number;
  EraseInitStruct.NbPages = number_of_pages;

  uint32_t page_error = 0;

  if (HAL_FLASHEx_Erase(&EraseInitStruct, &page_error) != HAL_OK)
  {
    HAL_FLASH_Lock();
    return FLASH_ERROR;
  }
  else
  {
    HAL_FLASH_Lock();
    return FLASH_OK;
  }

  return FLASH_OK;
}

 

 

Specifically I will hardfault when trying to perform HAL_FLASH_Lock( ) when HAL_FLASHEx_Erase fails (line 23 in the code snippet above)

 

if (HAL_FLASHEx_Erase(&EraseInitStruct, &page_error) != HAL_OK)
{
  HAL_FLASH_Lock();
  return FLASH_ERROR;
}

 

 
From the reference manual (RM0434 - Section 3.10.5 FLASH_CR) for the STM32WB I see that 

"This register (FLASH_CR) must not be modified when CFGBSY in Flash memory status register
(FLASH_SR) is set. This would result in... a bus error, if CFGBSY is set due to a double-word programming sequence and the write to FLASH_CR is performed after the first word is sent for programming and before
the second."

The hardfault I receive is a bus error

TomC_0-1710470822320.png

Further, when I inspect the CFGBSY bit in the FLASH_SR register I find that it is set

TomC_1-1710470921502.png

TomC_2-1710471950862.png

 

So I figure the fault occurs because the program tries to modify the FLASH_CR register when the CFGBSY bit is set in the HAL_FLASH_Lock( ) function

 

/**
  * @brief  Lock the FLASH control register access.
  * @retval HAL Status
  */
HAL_StatusTypeDef HAL_FLASH_Lock(void)
{
  HAL_StatusTypeDef status = HAL_OK;

  /* Set the LOCK Bit to lock the FLASH Registers access */
  /* @Note  The lock and unlock procedure is done only using CR registers even from CPU2 */
  SET_BIT(FLASH->CR, FLASH_CR_LOCK);

  /* verify Flash is locked */
  if (READ_BIT(FLASH->CR, FLASH_CR_LOCK) == 0U)
  {
    status = HAL_ERROR;
  }

  return status;
}

 


The specific pages of flash that I am modifying (erasing) are 167 - 170. According to the memory map for the STM32WB55 this is okay

TomC_3-1710473337783.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Why is HAL_FLASHEx_Erase failing? By that I mean what is in pFlash.ErrorCode and what address is in page_error when the call returns HAL_ERROR?

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

View solution in original post

4 REPLIES 4
TDK
Guru

Why is HAL_FLASHEx_Erase failing? By that I mean what is in pFlash.ErrorCode and what address is in page_error when the call returns HAL_ERROR?

If you feel a post has answered your question, please click "Accept as Solution".
Joe WILLIAMS
ST Employee

Hi TomC

Your case is being reviewed through the online support portal.

Thank you for your patience.

 

Kind Regards

Joe WILLIAMS

STMicro Support

Hi TDK,

Thank you for your response, I believe the problem was caused by me overwritting some memory with memcpy() somewhere else hence the IMPRECISERR. I will keep in mind to check the error code next time this happens.

Hi Joe,

I believe I caused this problem by overwritting memory with a memcpy () elsewhere in my program. Thank you for your response.