2023-02-15 04:21 AM
I am using stm32g030f6p9 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 through STM32CubeIDE, STM32 ST-LINK Utility, STM32CubeProgrammer).
I have tried every possible Mode and Reset Mode.
Why is this happening? How can I regain access to my microcontroller?
2023-02-15 06:40 AM
2023-02-15 07:45 AM
I can't even go into option bytes to change something there. And as you can see from the piece of code, I set 0xbb there.
2023-02-15 07:54 AM
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 remaining bits of the register. Could this lead to this result? As I understand it, my Boot configuration could change. Because I see that my program is not running. How can I get everything back?
2023-02-16 06:49 AM
Maybe the BOOT_LOCK bit is explanation of the problem. Do you remember setting it?
JW
2023-02-16 09:04 AM
I haven't touched the boot registers at all, all I do is set read protection in my code. I also write my variables to the last page of flash memory. Maybe that's the thing...
2023-02-16 09:17 AM
Multiple threads here
The G0 is definitely not going to be supported by ST-LINK Utilities, it hasn't been updated in years now.
You'd want to be using STM32 Cube Programmer
2023-02-16 09:34 AM
> I haven't touched the boot registers at all
I don't know what do you mean by "boot registers".
You are writing to option registers in FLASH, namely into the one at 0x1FFF7800 which is read into FLASH_OPTR (FLASH-registers offset 0x20, absolute address of FLASH-registers is 0x40022000 so this register is at 0x40022020) after reset.
BOOT_LOCK is in a different option register, at address 0x1FFF7870, read into FLASH_SECR (FLASH-registers offset 0x80). This register is officially not present in the 'G0x0, so you probably can't see/modify it through today's CubeProgrammer. However, as the 'G0x0 physically have the same chip as the 'G0x1, this option word is there and it can be - probably inadvertently (maybe through some error in code? maybe the "last page write", but maybe some other? maybe using STLinkUtility which does not support 'G0?) - modified.
So, what you should maybe do next time is, before programming RDP to Level1, read out FLASH_SECR and check BOOT_LOCK.
I don't say I am sure this is the mechanism of the problem you are encountering, but that this is one of the possible scenarios.
JW