Mehdi Sadeghi
Mehdi Sadeghi

Reputation: 1

Programming Option Bytes STM32L4xx using Hal API

I am trying to implement a bank swap mechanism for STM32L471RET6. my Code Doesn't seem to have errors for setting and resetting Option bytes but Nothing Happens when Calling the bank Swap Functions. after I checked the option bytes using STM32CubeProgrammer, I realized that the option bytes didn't changed. Here is my bank swap function.

HAL_StatusTypeDef SwapBank(void)
{
    FLASH_OBProgramInitTypeDef OB;
    HAL_FLASHEx_OBGetConfig(&OB);

if (OB.USERConfig & OB_DUALBANK_DUAL)
{
    if (OB.USERConfig & OB_BFB2_ENABLE)
    {
        HAL_FLASH_Unlock();
        HAL_FLASH_OB_Unlock();
        OB.OptionType = OPTIONBYTE_USER;
            OB.USERType = OB_USER_BFB2;
            OB.USERConfig = OB_BFB2_DISABLE;
    }
    else
    {
            OB.OptionType = OPTIONBYTE_USER;
            OB.USERType = OB_USER_BFB2;
            OB.USERConfig = OB_BFB2_ENABLE;
    }
    if (HAL_FLASHEx_OBProgram(&OB) != HAL_OK)
    {
            HAL_FLASH_OB_Lock();
        HAL_FLASH_Lock();
        return HAL_ERROR;
    }
    HAL_FLASH_OB_Launch();
    HAL_FLASH_OB_Lock();
    HAL_FLASH_Lock();   
    NVIC_SystemReset();
        return HAL_OK;
    }
    return HAL_ERROR;
}

I checked The STM32L4xx reference manual for programming Option bytes. I searched for it and asked for help from AI tools But I failed. I had written this program and mechanism for STM32L0 successfully and it is working but I don't Know what's wrong with STM32L471RET6.

Upvotes: 0

Views: 45

Answers (1)

Mehdi Sadeghi
Mehdi Sadeghi

Reputation: 1

Fixed...

HAL_StatusTypeDef SwapBank(void)
{
    FLASH_OBProgramInitTypeDef OB;
    HAL_FLASHEx_OBGetConfig(&OB);

if (OB.USERConfig & OB_DUALBANK_DUAL)
{
    HAL_FLASH_Unlock();
    HAL_FLASH_OB_Unlock();
    if (OB.USERConfig & OB_BFB2_ENABLE)
    {
        OB.OptionType = OPTIONBYTE_USER;
        OB.USERType = OB_USER_BFB2;
        OB.USERConfig = OB_BFB2_DISABLE;
    }
    else
    {
        OB.OptionType = OPTIONBYTE_USER;
        OB.USERType = OB_USER_BFB2;
        OB.USERConfig = OB_BFB2_ENABLE;
    }
    if (HAL_FLASHEx_OBProgram(&OB) != HAL_OK)
    {
        HAL_FLASH_OB_Lock();
        HAL_FLASH_Lock();
        return HAL_ERROR;
    }
    
    HAL_FLASH_OB_Launch();
    HAL_FLASH_OB_Lock();
    HAL_FLASH_Lock();   
    NVIC_SystemReset();
    return HAL_OK;
    }
    return HAL_ERROR;
}

Upvotes: 0

Related Questions