cancel
Showing results for 
Search instead for 
Did you mean: 

Flash Erase on STM32G4 doesn't work as intended

schwil
Associate II

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

5 REPLIES 5
TDK
Guru

Is FLASH_OPTR_DBANK #defined somewhere in your project?

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

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

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".

Alright I watched the FLASH->CR register as the code went through the low level erase steps. All the bits seem to be set as expected, both for the intended method of page erase and for my hack (BKER is set for the intended method, not set for the hack). But again only the hack seems to produce the desired result.

IDalr.1
Associate

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.