Skip to main content
Visitor II
October 26, 2021
Question

Flash Erase on STM32G4 doesn't work as intended

  • October 26, 2021
  • 2 replies
  • 2493 views

I'm running a STM32G473CB with 128kb flash, and I'm getting some issues erasing pages in bank2 of the flash. Using HAL_FLASHEx_Erase the way it's meant to be used, like in the following code that's supposed to erase pages 30 and 31 of bank2, I don't get any result. No errors arise, yet the pages remain unerased.

HAL_FLASH_Unlock();
 
FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t PageError = 0;
 
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Banks = FLASH_BANK_2;
EraseInitStruct.Page = 30;
EraseInitStruct.NbPages = 2;
 
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
{
 ...
}
 
HAL_FLASH_Lock();

Whereas if I do the following (an erase through the bank boundary from bank1) the correct flash pages are erased and no errors are thrown.

HAL_FLASH_Unlock();
 
FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t PageError = 0;
 
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
EraseInitStruct.Banks = FLASH_BANK_1;
EraseInitStruct.Page = 62;
EraseInitStruct.NbPages = 2;
 
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
{
 ...
}
 
HAL_FLASH_Lock();

I have made sure that the flash is configured with dual bank mode on, and that no write protection areas are active.

I would appreciate if someone with a 128kb g4 device on hand could test the bank2 page erase to see if the problem is universal.

Thanks

    This topic has been closed for replies.

    2 replies

    Super User
    October 26, 2021

    Is FLASH_OPTR_DBANK #defined somewhere in your project?

    schwilAuthor
    Visitor II
    October 26, 2021

    yes, it's defined in the cmsis device header file "stm32g473xx.h"

    Super User
    October 26, 2021

    I would step through the code when it gets to FLASH_PageErase and verify the contents of the FLASH registers just prior to it setting FLASH_CR_STRT. In particular, verify the FLASH_CR_BKER bit is set.

    Visitor II
    March 31, 2023

    I do not have an answer unfortunately but can confirm I am having the exact same issue with STM32G473CC. I initially developed on the NUCLEO-STM32G4 which has a STM32G474RE (512 KB flash) and was able to write to erase and write no problem. Then we switched to the STM32G473CC and no luck. But as I said I can confirm reproducing exactly the issue in this post. I would greatly appreciate any help on this.