cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble writing option byte in STM32G030

EKoec
Associate II

I'm trying to write the option byte in my STM32G030 uC to be able to start the internal boot loader by pulling BOOT0 high (set nBOOT0_SEL = 0). The problem is I'm getting a WRPERR while writing. All other related registers are in default state. If I use HAL_FLASH_Program(..) instead, I get the same error at the same step. Any ideas?

const uint64_t u64_option_byte_boot = 0x00000000DEFFE1AA;
 
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
 
FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
 
// set PG
FLASH->CR |= 0x01;
 
*p_u32_option_bytes = (uint32_t) (u64_option_byte_boot); // <<< after this step FLASH->SR.CFGBSY is set
*(p_u32_option_bytes + 1U) = (uint32_t) (u64_option_byte_boot >> 32); // <<< after this step FLASH->SR.WRPERR is set  
 
FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
 
SET_BIT(FLASH->CR, FLASH_CR_OPTSTRT);
 
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();

1 ACCEPTED SOLUTION

Accepted Solutions
EKoec
Associate II

Okay, thanks for the hint. This is not very good documented. The solution seems to be:

    FLASH_OBProgramInitTypeDef user_bytes;
 
    HAL_FLASHEx_OBGetConfig(&user_bytes);
 
    user_bytes.USERConfig &= ~(0x1 << 24);
 
    HAL_FLASH_Unlock();
    HAL_FLASH_OB_Unlock();
    HAL_FLASHEx_OBProgram(&user_bytes);
    HAL_FLASH_OB_Launch();
    HAL_FLASH_OB_Lock();
    HAL_FLASH_Lock();

Thanks again.

View solution in original post

2 REPLIES 2
TDK
Guru

Use HAL_FLASHEx_OBProgram to manage option bytes, or duplicate its functionality.

Check and clear flash error flags prior to first access. Sometimes they are set.

If you feel a post has answered your question, please click "Accept as Solution".
EKoec
Associate II

Okay, thanks for the hint. This is not very good documented. The solution seems to be:

    FLASH_OBProgramInitTypeDef user_bytes;
 
    HAL_FLASHEx_OBGetConfig(&user_bytes);
 
    user_bytes.USERConfig &= ~(0x1 << 24);
 
    HAL_FLASH_Unlock();
    HAL_FLASH_OB_Unlock();
    HAL_FLASHEx_OBProgram(&user_bytes);
    HAL_FLASH_OB_Launch();
    HAL_FLASH_OB_Lock();
    HAL_FLASH_Lock();

Thanks again.