I am using STM32G030F6P6 microcontroller. I tried to make flash read protection:

if ((FLASH->OPTR & FLASH_OPTR_RDP) != 0xbb)
  FLASH->KEYR = 0x45670123;

  while (FLASH->SR & FLASH_SR_BSY1);

  FLASH->OPTKEYR = 0x08192A3B;

  while (FLASH->SR & FLASH_SR_BSY1);

  FLASH->OPTR |= (0xbb << FLASH_OPTR_RDP_Pos); //read protection of memories active

  while (FLASH->SR & FLASH_SR_BSY1);
  while (FLASH->SR & FLASH_SR_BSY1);
  while (FLASH->SR & FLASH_SR_BSY1);

After a power reset, I see that my program is not running and I no longer have access to the microcontroller (tried with STM32CubeIDE, STM32 ST-LINK Utility, STM32CubeProgrammer). I have tried to swap every possible Mode and Reset Mode.

enter image description here enter image description here

I noticed in the reference manual (RM0454 Rev 5), page 68 it says:

"Note: Any modification of the value of one option is automatically performed by erasing user option byte pages first, and then programming all the option bytes with the values contained in the Flash memory option registers.

The complementary values are automatically computed and written into the complemented option bytes upon setting the OPTSRT bit."

If I understand correctly, when writing the RDP value to the OPTR register, I could change the other bits of the register. Could this lead to this result? As I understand it, my Boot configuration was changed. Because I see that my program is not running.

How can I get everything back? Why is this happening? How can I regain access to my microcontroller?

  • \$\begingroup\$ Have you tried connecting to the MCU under reset? I mean, setting the Mode in the STLINK utility to "Connect Under Reset", pressing the reset button, clicking the Connect button and releasing the reset button? What happens? \$\endgroup\$
    – neoxic
    Feb 23, 2023 at 23:02
  • \$\begingroup\$ Regardless of what state your MCU is in right now, it's very dangerous to make things like this FLASH->OPTR |= (0xbb << FLASH_OPTR_RDP_Pos) when it comes to flash protection level. Note that you are OR'ing the previous value and 0xbb. Fortunately, if the previous value was 0xaa, i.e. Level 0, OR'ing it with 0xbb still gives 0xbb (which is pure luck), so Level 1 was set. But in general, it was quite reckless.:) \$\endgroup\$
    – neoxic
    Feb 23, 2023 at 23:11

1 Answer 1


use unlock utility provided by segger "JLinkSTM32.exe" to unlock the device


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.