2025-05-12 2:41 AM
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:
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.