Skip to main content
BCoch
Senior
August 14, 2019
Solved

Hard fault when writing to FLASH->OPTKEYR

  • August 14, 2019
  • 2 replies
  • 1472 views

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.
Best answer by BCoch

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
August 15, 2019

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

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.