Skip to main content
schwil
Associate II
Oct 26, 2021 2:19 PM UTC
Question

Flash Erase on STM32G4 doesn't work as intended

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.

5 replies

TDK
Oct 26, 2021 2:21 PM UTC

Is FLASH_OPTR_DBANK #defined somewhere in your project?

"If you feel a post has answered your question, please click ""Accept as Solution""."
schwil
schwilAuthor
Associate II
Oct 26, 2021 2:24 PM UTC

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

TDK
Oct 26, 2021 2:35 PM UTC

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.

"If you feel a post has answered your question, please click ""Accept as Solution""."
IDalr.1
Visitor II
Mar 31, 2023 9:21 PM UTC

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.