Skip to main content
BCoch
Senior
Aug 14, 2019 11:09 PM UTC
Solved

Hard fault when writing to FLASH->OPTKEYR

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.

This topic has been closed for replies.
BCoch
BCochBest answer
Senior
Aug 15, 2019 6:37 PM UTC

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

2 replies

BCoch
BCochAuthorBest answer
Senior
Aug 15, 2019 6:37 PM UTC

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
May 10, 2021 8:05 PM UTC

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.