Skip to main content
Associate
May 12, 2025
Question

Problem for perform a switch bank on STM32U385Rg

  • May 12, 2025
  • 5 replies
  • 668 views

Hello all,

I allow to send this message after a long attempt to perform a switch bank on my STM32U385RG.

This is part of my code:

/*--------------------------------------------------------*/
void OptionBytes_SwapBanks(void)
/*--------------------------------------------------------*/
{
 FLASH_OBProgramInitTypeDef OBInit;
 HAL_StatusTypeDef status;

 swo_printf("Current USERConfig: 0x%08lx\n", OBInit.USERConfig);

 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR | FLASH_FLAG_PROGERR | FLASH_FLAG_WRPERR |
 FLASH_FLAG_PGAERR | FLASH_FLAG_SIZERR | FLASH_FLAG_PGSERR |
 FLASH_FLAG_OPTWERR);
 uint32_t optr = FLASH->OPTR;

 if (optr & FLASH_OPTR_DUALBANK)
 {
 swo_printf("Dual Bank is ENABLED\n");
 }
 else
 {
 swo_printf("Dual Bank is DISABLED\n");
 }

 if (optr & FLASH_OPTR_SWAP_BANK)
 {
 	 swo_printf("Swap Bank is ENABLED\n");
	OBInit.OptionType = OPTIONBYTE_USER;
 OBInit.USERType = OB_USER_SWAP_BANK;
 OBInit.USERConfig = OB_SWAP_BANK_DISABLE;
 }
 else
 {
 	 swo_printf("Swap Bank is DISABLED\n");
	OBInit.OptionType = OPTIONBYTE_USER;
 	OBInit.USERType = OB_USER_SWAP_BANK;;
 	OBInit.USERConfig = OB_SWAP_BANK_ENABLE;
 }
 status = HAL_FLASHEx_OBProgram(&OBInit);
 if (status != HAL_OK)
 {
 swo_printf("Error in programming Option Bytes: %ld\n", HAL_FLASH_GetError());
 HAL_FLASH_OB_Lock();
 HAL_FLASH_Lock();
 return;
 }
}


You can view some logs in attached files:

Capture d'écran 2025-05-12 113546.png           Capture d'écran 2025-05-12 113554.png

I can do the swap directly with STMCubeprogrammer but I cannot do it with code so i suppose that it's an error of configuration or code.

This topic has been closed for replies.

5 replies

TDK
May 12, 2025

Do you have CPU_IN_SECURE_STATE defined? My guess is something with that.

"If you feel a post has answered your question, please click ""Accept as Solution""."
SixelaAuthor
Associate
May 12, 2025

i don't know how i can active or deactivate the CPU_IN_SECURE_STATE .
I had already deactivate all OptionBytes for security (TZEN,WRP,...) and i don't use the MPU actually

KDJEM.1
ST Technical Moderator
May 12, 2025

Hello @Sixela and welcome to the community;

 

I recommend you to refer to FLASH_SwapBanks example and check your code. This example presents a guide through the configuration steps to program internal Flash memory bank 1 and bank 2, and to swap between both of them by mean of the FLASH HAL API. This example has been tested with NUCLEO-U385RG-Q board. Also, I advise you to look at the README.md to run this example.

 

Thank you.

Kaouthar

Latest commit

 

 

 

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.
SixelaAuthor
Associate
May 12, 2025

Hello,

I have already converted the code from the example into a unified code, but it's not working either.
I think there may be something interfering with the exchange.

I don't know why perforrm the swapping is impossible for this one.