2022-02-03 01:32 AM
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();
Solved! Go to Solution.
2022-02-03 07:39 AM
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.
2022-02-03 06:57 AM
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.
2022-02-03 07:39 AM
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.