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.
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?