0

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

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

  while (FLASH->SR & FLASH_SR_BSY1);

  FLASH->OPTKEYR = 0x08192A3B;
  FLASH->OPTKEYR = 0x4C5D6E7F;

  while (FLASH->SR & FLASH_SR_BSY1);

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

  while (FLASH->SR & FLASH_SR_BSY1);
  FLASH->CR |= FLASH_CR_OPTSTRT;
  while (FLASH->SR & FLASH_SR_BSY1);
  FLASH->CR |= FLASH_CR_OPTLOCK;
  while (FLASH->SR & FLASH_SR_BSY1);
  FLASH->CR |= FLASH_CR_LOCK;
}

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?

red15530
  • 21
  • 2
  • 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? – neoxic Feb 23 '23 at 23:02
  • 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.:) – neoxic Feb 23 '23 at 23:11

1 Answers1

1

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