cancel
Showing results for 
Search instead for 
Did you mean: 

Setting Option Bytes on a new MCU fails on STM32L451RCT6

AH.6
Associate

Hello eveyone,

In my code i try to set the BOR level with the following function:

/**
 * Set proper BOR level.
 */
void setBorLevel(void)
{
    FLASH_OBProgramInitTypeDef optionBytesInit = {0U};
    bool_t isBorLevelConfigured;
    uint32_t borLevel = OB_BOR_LEVEL_4;
 
    HAL_FLASHEx_OBGetConfig(&optionBytesInit);
 
    isBorLevelConfigured = ((optionBytesInit.USERConfig & FLASH_OPTR_BOR_LEV_Msk) == borLevel) ? TRUE : FALSE;
 
    /* This part is only executed when BOR level is not set, or is changed */
    if (FALSE == isBorLevelConfigured)
    {
        optionBytesInit.USERType |= OB_USER_BOR_LEV;
        optionBytesInit.USERConfig &= ~FLASH_OPTR_BOR_LEV_Msk;
        optionBytesInit.USERConfig |= borLevel;
 
        HAL_FLASH_Unlock();
        HAL_FLASH_OB_Unlock();
        HAL_FLASHEx_OBProgram(&optionBytesInit);
        HAL_FLASH_OB_Launch();
        HAL_FLASH_OB_Lock();
        HAL_FLASH_Lock();
 
        System_resetCpu();
    }
}

This code fails on a brand new MCU. I used IAR debugger to investigate. Option byte is read on line 10 using HAL_FLASHEx_OBGetConfig. After that, with the debugger I can see that in optionBytesInit the WRPStartOffset field is set to 255, WRPEndOffset is set to 0, and WRPArea is set to 0.

Then optionBytesInit is used without modifying the aforementioned fields to program Option bytes. Inside HAL_FLASHEx_OBProgram, FLASH_OB_WRPConfig is called with those 3 fields (WRPArea, WRPStartOffset, and WRPEndOffset in optionBytesInit) as its arguments. The argument are checked and assert_param(IS_FLASH_PAGE(WRPStartOffset)) fails.

The workaroiund I have found is to set WRPStartOffset to 0 using the debugger.

I found a question about a somewhat related issue with an L1 MCU on the forums.

Can't set Option Byte with the attached function for the Micro STM32L151CBT6A Only when the controller is new from factory. If we erase and reset the option bytes the STM32 afterwards new flashing it works.

 But the workaround proposed by the OP, i.e. erasing the chip completely, and reflashing it, didn't solve my problem.

0 REPLIES 0