Skip to main content
NFern.1
Associate III
June 28, 2025
Solved

can't erase correct Flash Bank when attempting to switch from Bank2 to Bank1

  • June 28, 2025
  • 2 replies
  • 454 views
Title shortened from:
"With the STM32U545RE, unable to erase the correct Flash Bank, using HAL_FLASHEx_Erase(), when attempting to switch from Bank2 to Bank1".

 
Testing is done with the Nulceo-U545RE-Q board having 512KB Flash organized in two Banks. Each Bank has 32 pages of 8KB each. Flash Bank1: 0-31 pages. Flash Bank2: 32-63 pages.
 
I could switch-over from Firmware-Version1 executing in Bank1 to Firmware-Version2 executing in Bank2.
I verified that Firmware-Version2 was executing correctly even after hardware reset and power cycle.
Attempting to switch-over  from Firmware-Version2 executing in Bank2 to Firmware-Version3 executing in Bank1 fails as I end up erasing the Bank with the currently executing software(Firmware-Version2).
 
I checked the setting of the SWAP_BANK bit and then erased the Bank, using the code below:
 
	 /* Get the boot configuration status */
		HAL_FLASHEx_OBGetConfig(&OBInit);
		/* Check Swap Flash banks status */
		if ((OBInit.USERConfig & OB_SWAP_BANK_ENABLE) == OB_SWAP_BANK_DISABLE)
		{
			BankNumber=FLASH_BANK_2; // bank for erase
		}
		else
		{
			BankNumber=FLASH_BANK_1; // bank for erase
		}

		FirstPage=0;
		NbOfPages=32;
		/* Fill EraseInit structure*/
		EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
		EraseInitStruct.Banks = BankNumber;
		EraseInitStruct.Page = FirstPage;
		EraseInitStruct.NbPages = NbOfPages;

		if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
		{
		 FotaErrorOnErase=1;
		}

Program execution halts immediately.

Using CUBEPROGRAMMER, I verified that the SWAP_BANK bit is set (checked).

Using CUBEPROGRAMMER, I noticed that Bank1 has been Erased! This had Firmware-Version2.

Now assuming that with the SWAP_BANK bit as set, Bank2 with Firmware_Version2 is now mapped as Bank1. And so Bank1 with Firmware_Version1 is now mapped as Bank2. 

And so I implied that maybe I must always erase Bank2. So I modified the code as below:

 BankNumber=FLASH_BANK_2;
		FirstPage=0;
		NbOfPages=32;
		/* Fill EraseInit structure*/
		EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
		EraseInitStruct.Banks = BankNumber;
		EraseInitStruct.Page = FirstPage;
		EraseInitStruct.NbPages = NbOfPages;

		if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
		{
		 FotaErrorOnErase=1;
		}
But this does not work either and checking in CUBEPROGRAMMER, I noticed that Bank1 has been erased again! This had the currently executing code (Firmware-Version2 as the SWAP_BANK bit is checked)
 
I disabled the Erase Logic and calls to HAL_FLASHEx_Erase() in the code and manually erased
BANK2 using the CUBEPROGRAMMER and I could do Firmware updates as per the sequence below.
1. Using CUBEPROGRAMMER - Manually did "Full Chip Erase" and set the SWAP_BANK bit to cleared(unchecked) and using CUBEIDE, downloaded Firmware-Version1 to Bank1
2. Firmware-Version1 executing in Bank1 loads Firmware-Version2 to  Bank2
3. Using CUBEPROGRAMMER,  manually "Erase Selected Sectors": 32-63
4. Firmware-Version2 executing in Bank2 loads Firmware-Version3 to  Bank1
5. Using CUBEPROGRAMMER  manually "Erase Selected Sectors": 32-63
6. Firmware-Version3 executing in Bank1 loads Firmware-Version4 to  Bank2
 
So I conclude that I am not correctly using the HAL_FLASHEx_Erase() function.
 
I request ST for proper documentation and examples for the STM32U545RE - explaining the correct use of the HAL_FLASHEx_Erase() function when executing from Bank2 and attempting to erase Bank1  (ie with the SWAP_BANK bit set)
Best answer by Nff

Unable to access my old account @NFern.1

Final got to work on this again after nearly a year. It works now. Updating from Bank1(version1)->Bank2(version2)->Bank1(version3)->Bank2(version4)->Bank1(version5)… There may have been some logical errors in my earlier code.

Thanks to ​@rulin  and ​@FBL for support across multiple related discussion topics. 

Working code is as below.

		if ((OBInit.USERConfig & OB_SWAP_BANK_ENABLE) == OB_SWAP_BANK_DISABLE)
{
ActiveFlashBank=1; //Bank1 is active
BankNumber=FLASH_BANK_2; // bank for erase
}
else
{
ActiveFlashBank=2; //Bank2 is Active
BankNumber=FLASH_BANK_1; // bank for erase
}

EraseInitStruct.TypeErase = FLASH_TYPEERASE_MASSERASE; // trying with bank erase
EraseInitStruct.Banks = BankNumber;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
{
/*
Error occurred while page erase.
User can add here some code to deal with this error.
PageError will contain the faulty page and then to know the code error on this page,
user can call function 'HAL_FLASH_GetError()'
*/
}


//set the start address to be used by HAL_FLASH_Program()
//to 0x08040000 for U545 while writing to both Bank2 and Bank1
FlashStartAddressForWrite=FLASH_BANK2_START_ADDR; //0x08040000

Note the addresses of Bank1,2 as per the Swap_Bank option bit, referring the STM32H5 Reference Manual RM0481

 

2 replies

NFern.1
NFern.1Author
Associate III
July 2, 2025
NffBest answer
Visitor II
June 24, 2026

Unable to access my old account @NFern.1

Final got to work on this again after nearly a year. It works now. Updating from Bank1(version1)->Bank2(version2)->Bank1(version3)->Bank2(version4)->Bank1(version5)… There may have been some logical errors in my earlier code.

Thanks to ​@rulin  and ​@FBL for support across multiple related discussion topics. 

Working code is as below.

		if ((OBInit.USERConfig & OB_SWAP_BANK_ENABLE) == OB_SWAP_BANK_DISABLE)
{
ActiveFlashBank=1; //Bank1 is active
BankNumber=FLASH_BANK_2; // bank for erase
}
else
{
ActiveFlashBank=2; //Bank2 is Active
BankNumber=FLASH_BANK_1; // bank for erase
}

EraseInitStruct.TypeErase = FLASH_TYPEERASE_MASSERASE; // trying with bank erase
EraseInitStruct.Banks = BankNumber;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
{
/*
Error occurred while page erase.
User can add here some code to deal with this error.
PageError will contain the faulty page and then to know the code error on this page,
user can call function 'HAL_FLASH_GetError()'
*/
}


//set the start address to be used by HAL_FLASH_Program()
//to 0x08040000 for U545 while writing to both Bank2 and Bank1
FlashStartAddressForWrite=FLASH_BANK2_START_ADDR; //0x08040000

Note the addresses of Bank1,2 as per the Swap_Bank option bit, referring the STM32H5 Reference Manual RM0481