cancel
Showing results for 
Search instead for 
Did you mean: 

Hard fault when writing to FLASH->OPTKEYR

BCoch
Senior

Wondering why I'm getting a hard fault when writing to the FLASH option key register on an STM32G071RBT6 (Nucleo board).

I first make this call, (running from flash):

LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_FLASH);

Then, running from RAM, do the following:

if (READ_BIT(FLASH->OPTR, FLASH_OPTR_nBOOT_SEL) != 0)

{

// Unlock flash option bytes

WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY1);

WRITE_REG(FLASH->OPTKEYR, FLASH_OPTKEY2);

The READ_BIT operation works fine, but I get a fault writing the OPTKEYR register.

The key values are defined as follows:

#define FLASH_OPTKEY1 0x08192A3B

#define FLASH_OPTKEY2 0x4C5D6E7F

The register address is resolving to 0x4002200C, which I believe is correct.

Any thoughts?

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
BCoch
Senior

The solution is to unlock the flash before attempting to unlock the flash option register, (as detailed in the reference manual).

View solution in original post

2 REPLIES 2
BCoch
Senior

The solution is to unlock the flash before attempting to unlock the flash option register, (as detailed in the reference manual).

MPage.1
Associate II

Confirmed on L471. I ported working M7 code to L4 and this is one of the differences. The other is that OB Launch triggers a reset.