cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G4 Flash Erase trouble at address greater than half flash

Stefano.Patassa
Associate II

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.

7 REPLIES 7
TDK
Guru

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

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 II

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();

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();

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

DBANK=1 in FLASH->OPTR, and as far as I can tell WRP is not set (given that WRPxy_STRT > WRPxy_END for all banks and areas)

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