cancel
Showing results for 
Search instead for 
Did you mean: 

Problem for perform a switch bank on STM32U385Rg

Sixela
Associate II

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.

5 REPLIES 5
TDK
Super User

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".
KDJEM.1
ST Employee

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 Accept as Solution on the reply which solved your issue or answered your question.

Sixela
Associate II

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

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.

Sixela
Associate II

Hi,

I had to check the CPU_IN_SECURE_STATE macro in the file that is present:

#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
#define CPU_IN_SECURE_STATE
#endif

It's defined in stm32u385xx.h in the CMSIS folder, but not in other files as stm32u3xx_HAL_def.h.

/* To keep after #include "stm32u3xx.h" to have CPU_IN_SECURE_STATE available */
#if defined (CPU_IN_SECURE_STATE) // symbol not found
#include <arm_cmse.h>
#endif /* CPU_IN_SECURE_STATE */

I suspect that the code is not wrong because i have the same that the exemple available on the Git Repository, but that a remaining parameter blocks the programming of the OB.

Do you have an idea for that ?