Skip to main content
Visitor
July 9, 2026
Question

Issue when erasing BANK2 in STM32G0C1VET6

  • July 9, 2026
  • 1 reply
  • 59 views

Hello,

 

I wrote a code to erase BANK2 flash memory for STM32G0C1VET6. The thing is that in some cases the erasing part works but in some cases it fails.

I wrote the code to be ran in BANK1,the production code will use the proper start address and size, for this testing I’m forcing the erasing to happen in BANK2 for pages 0 and 1. As I commented, in some cases the erasing works, but sometimes there is an error returned by HAL_FLASHEx_Erase, which is HAL_TIMEOUT. I added proper refresh to watchdog and also some PREFETCH calls but even with that the issue persist. Attached is an image of the SR register once the HAL_TIMEOUT is returned. If I continue the execution of the code there is a hard fault caused when calling the HAL_FLASH_Lock (image attached). What could be wrong?

I'm using STM32CubeIDE along with Stlink - V3.

If the flash is already erased, calling the Erase function could cause the problem?

Could it be related to some FLASH latency configurations?

bool __attribute__((optimize("O0"))) FlashDrv_EraseApp(const uint32_t e_mem_addr, const uint32_t n_img_len)
{
vip_error_code_t flash_drv_error = VIP_E_INVALID_PARAMS;

if((FLASH_DRV_APP_START_ADDRESS == e_mem_addr) && (FLASH_DRV_APP_SIZE == n_img_len))
{
uint32_t page_error = 0U;
HAL_StatusTypeDef hal_error = HAL_OK;
FLASH_EraseInitTypeDef erase_init;

FLASH->SR = FLASH_SR_CLEAR; /* Clean any pending errors */
erase_init.TypeErase = FLASH_TYPEERASE_PAGES;
erase_init.Banks = FLASH_BANK_2;
erase_init.Page = 0;
erase_init.NbPages = 1U;

if(HAL_OK == HAL_FLASH_Unlock())
{
MX_IWDG_Refresh();
__HAL_FLASH_PREFETCH_BUFFER_DISABLE();
hal_error = HAL_FLASHEx_Erase(&erase_init, &page_error);
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();

if((HAL_OK == hal_error) && (FLASH_DRV_ERASE_SUCCESS == page_error))
{
MX_IWDG_Refresh();
erase_init.TypeErase = FLASH_TYPEERASE_PAGES;
erase_init.Banks = FLASH_BANK_2;
erase_init.Page = 1U;
erase_init.NbPages = 1U;
MX_IWDG_Refresh();
__HAL_FLASH_PREFETCH_BUFFER_DISABLE();
hal_error = HAL_FLASHEx_Erase(&erase_init, &page_error);
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();

if((HAL_OK == hal_error) && (FLASH_DRV_ERASE_SUCCESS == page_error))
{
flash_drv_error = VIP_E_SUCCESS;
}
else
{
flash_drv_error = VIP_E_FLASH_ERASING_FAILED;
}
}
else
{
flash_drv_error = VIP_E_FLASH_ERASING_FAILED;
}

if(HAL_OK != HAL_FLASH_Lock())
{
flash_drv_error = VIP_E_FLASH_LOCKING_FAILED;
}
}
else
{
flash_drv_error = VIP_E_FLASH_LOCKING_FAILED;
}
}

return ((VIP_E_SUCCESS == flash_drv_error) ? true : false);
}

 

1 reply

mƎALLEm
ST Technical Moderator
July 9, 2026

Hello ​@JDambrocio and welcome to the ST community,

You’ve posted that thread in “Automotive MCUs” forum board which is not the right forum according the device you are using.

That post should be located in “STM32 MCUs Embedded software” forum board.

I move the post then.

Good luck.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.