cancel
Showing results for 
Search instead for 
Did you mean: 

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

NFern.1
Associate III
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)
1 REPLY 1
NFern.1
Associate III