cancel
Showing results for 
Search instead for 
Did you mean: 

stm32c011 in-application option byte programming

AndreasS
Associate

Good morning,

I'm trying to program the user option bytes of the STM32C011 using the HAL. The objective of the test is to toggle the BOR bit.

 

 

static HAL_StatusTypeDef testOptionBytes()
{
    FLASH_OBProgramInitTypeDef obInit = {0};
    FLASH_OBProgramInitTypeDef obProgram = {0};
    FLASH_OBProgramInitTypeDef obCmp = {0};
    HAL_StatusTypeDef status = HAL_OK;

    HAL_FLASHEx_OBGetConfig(&obInit);

    status = HAL_FLASH_Unlock();
    assert(status == HAL_OK);
    status = HAL_FLASH_OB_Unlock();
    assert(status == HAL_OK);

    obProgram.OptionType = OPTIONBYTE_USER;
    obProgram.USERType = OB_USER_BOR_EN;
    obProgram.USERConfig = (obInit.USERConfig & OB_USER_BOR_EN) ^ OB_USER_BOR_EN;
    status = HAL_FLASHEx_OBProgram(&obProgram);
    assert(status == HAL_OK);

    HAL_FLASH_OB_Lock();
    HAL_FLASH_Lock();

    HAL_FLASHEx_OBGetConfig(&obCmp);
    assert(obInit.USERConfig != obCmp.USERConfig);

    return status;
}

 

When I step through the HAL code everything looks fine, the FLASH->OPTR register is written with the correct value, but the final assert() always fails.

 

Have I overseen something here?

Thanks in advance, Andreas

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

You need to call HAL_FLASH_OB_Launch()  after HAL_FLASHEx_OBProgram()

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.

View solution in original post

1 REPLY 1
SofLit
ST Employee

You need to call HAL_FLASH_OB_Launch()  after HAL_FLASHEx_OBProgram()

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.