2024-03-14 08:29 PM
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
Further, when I inspect the CFGBSY bit in the FLASH_SR register I find that it is set
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
Solved! Go to Solution.
2024-03-15 06:55 AM
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?
2024-03-15 06:55 AM
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?
2024-03-15 09:49 PM
Hi TomC
Your case is being reviewed through the online support portal.
Thank you for your patience.
Kind Regards
Joe WILLIAMS
STMicro Support
2024-03-17 01:40 PM
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.
2024-03-17 01:40 PM
Hi Joe,
I believe I caused this problem by overwritting memory with a memcpy () elsewhere in my program. Thank you for your response.