Skip to main content
Stefano.Patassa
Associate II
September 1, 2021
Question

STM32G4 Flash Erase trouble at address greater than half flash

  • September 1, 2021
  • 4 replies
  • 2451 views

Hello,

i have an issue with erasing flash on STM32G4.

When i try to erase addresses above half of the flash (above 0x8010000), it does not erase anything. It works when i try to erase below this address.

However the function HAL_FLASHEx_Erase returns HAL_OK with PageError = 0xFFFFFFFF meaning that the the operation was well performed.

I've tried multiple combination of page number and bank number but same behaviour...

I am using an old project made for STM32G473VET (512k) but the actual MCU i'm using in the board is STM32G473VBT6 (128k). Obiviously i've changed the linker script to reflect the new flash size but i have no idea if this changes something in how all the toolchain handles it... Should it matter?

Any help will be much appreciated.

This topic has been closed for replies.

4 replies

TDK
September 1, 2021

Are you in dual bank mode?

Show the code.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Stefano.Patassa
Associate II
September 3, 2021

I should be in dual bank mode, i did not change anything from default configuration.

Code is quite simple:

osKernelLock();
	__disable_irq();
 
	if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)){
		__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
	}
 
	if (HAL_FLASH_Unlock() == HAL_OK) {
		EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
		EraseInitStruct.NbPages = 1;
		EraseInitStruct.Page = 56; //should be 0x801C000
		EraseInitStruct.Banks = FLASH_BANK_1;
 
		if (HAL_FLASHEx_Erase(&EraseInitStruct, &page_error) == HAL_OK) {
			//Programming code
			}
		}
		HAL_FLASH_Lock();
	}
	__enable_irq();
	osKernelUnlock();

PWaser
Associate
October 20, 2021

In dual bank mode your 128k device has 64 pages.

to erase pages 32 to 63 you have to select bank2.

osKernelLock();
	__disable_irq();
 
	if(__HAL_FLASH_GET_FLAG(FLASH_FLAG_OPTVERR)){
		__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
	}
 int page = 56;
	if (HAL_FLASH_Unlock() == HAL_OK) {
		EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
		EraseInitStruct.NbPages = 1;
 if (page < 32) {
		 EraseInitStruct.Page = page;
		 EraseInitStruct.Banks = FLASH_BANK_1;
 } else {
 EraseInitStruct.Page = page - 32;
 EraseInitStruct.Banks = FLASH_BANK_2;
 }
 
		if (HAL_FLASHEx_Erase(&EraseInitStruct, &page_error) == HAL_OK) {
			//Programming code
			}
		}
		HAL_FLASH_Lock();
	}
	__enable_irq();
	osKernelUnlock();

schwil
Associate II
October 26, 2021

I'm running a STM32G473CB with 128kb flash, and I'm getting some issues erasing bank2 pages as well. 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();

PWaser
Associate
October 26, 2021

Is your device in single bank mode DBANK=0 or write protection set?

TDK
October 26, 2021

Is FLASH_OPTR_DBANK #defined somewhere in your project?

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