Skip to main content
Associate II
January 12, 2024
Question

NUCLEO-G071RB flash unlock hardfault

  • January 12, 2024
  • 4 replies
  • 1373 views

Hello,

I'm new in ARM programming, I'm trying to study how CHIP work using the documentation.
I wanna unlock the flash memory and according with documentation:

1. Write KEY1 = 0x4567 0123 in the FLASH key register (FLASH_KEYR)
2. Write KEY2 = 0xCDEF 89AB in the FLASH key register (FLASH_KEYR).
Any wrong sequence locks the FLASH_CR registers until the next system reset. In the case
of a wrong key sequence, a bus error is detected and a Hard Fault interrupt is generated.

 

I write this simple code:

void unlock_flash()
{
   FLASH_TypeDef *flash;
   flash->KEYR = 0x45670123;
   flash->KEYR = 0xCDEF89AB;
}
But after registre write I have an HARDFAULT...
Please could you help me?

Thanks a lot!

This topic has been closed for replies.

4 replies

ST Technical Moderator
January 12, 2024

Hello @dmrsim 

In case of an unsuccessful unlock operation, this Lock bit in FLASH_CR remains set until the next system reset. You may need to read this bit before authorizing the access and after that you can wait to check if flash unlock is complete.

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL
dmrsimAuthor
Associate II
January 12, 2024

Hello, 

thanks for your reply.

Before unlock I have tried also in debug mode, to read flash->CR bit, but also this read cause HardFault(I cannot read Flash registers using my code)....

Could you help me?

dmrsimAuthor
Associate II
January 12, 2024

Really sorry, my big mistake...I forgot to init: FLASH_TypeDef *flash = FLASH;

ST Technical Moderator
January 12, 2024

Hi @dmrsim 

You may check the faults view to see why it is triggered. We don't have all the inputs needed to help further. You can check clocks in RCC register view, or you can simply see the implementation of our HAL driver.

To give better visibility on the answered topics, please click on "Best answer" on the reply which solved your issue or answered your question.Best regards,FBL