Solved
Trouble writing option byte in STM32G030
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();