cancel
Showing results for 
Search instead for 
Did you mean: 

Bank switch does not always work

KamilWierzbicki
Associate II

Hello everyone!

I'm facing a problem when trying to switch a bank on STM32H745 MCU. The issue happens only when the debugger is disconnected. Our implementation uses the HAL library to perform Bank switch like this:

HAL_FLASH_Unlock();
        HAL_FLASH_OB_Unlock();

        FLASH_OBProgramInitTypeDef info;
        HAL_FLASHEx_OBGetConfig(&info);

        // swap banks in option bytes
        info.OptionType = OPTIONBYTE_USER;
        info.USERType   = OB_USER_SWAP_BANK;
        if (info.USERConfig & OB_SWAP_BANK_ENABLE)
            info.USERConfig &= ~OB_SWAP_BANK_ENABLE;
        else
            info.USERConfig |= OB_SWAP_BANK_ENABLE;
        __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS_BANK1);
        __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS_BANK2);
        HAL_StatusTypeDef ret = HAL_FLASHEx_OBProgram(&info);


        if (HAL_OK == ret)
        {
            // launch new configuration
            ret = HAL_FLASH_OB_Launch();
        }

        (void)HAL_FLASH_OB_Lock();
        (void)HAL_FLASH_Lock();
        __HAL_RCC_CLEAR_RESET_FLAGS();
        NVIC_SystemReset();

In some of the builds it does not work and after reset it keeps running the previous image.
I found that 

HAL_FLASHEx_OBProgram function returns HAL_ERROR which was caused by 
FLASH_WaitForLastOperation and error flag set. The 
FLASH_FLAG_PGSERR_BANK2 causes the problem so I tried to clear all of the flags just before switching with
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS_BANK2) however it does not solve the problem.
 
 
0 REPLIES 0