The STM32G0 series microcontroller enters undescribed mode.
I have been sitting with this question for more than a week, I have already run out of strength, I cannot understand how it works and what I am doing wrong ...
And so, I have an STM32G030F6P6 microcontroller. My program needs to do two things with flash memory, namely:
1) Save 8 bit number. I do it like this:
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
while (FLASH->SR & FLASH_SR_BSY1);
FLASH->SR |= FLASH_SR_OPTVERR;
FLASH->SR |= FLASH_SR_FASTERR;
FLASH->SR |= FLASH_SR_MISERR;
FLASH->SR |= FLASH_SR_PGSERR;
FLASH->SR |= FLASH_SR_SIZERR;
FLASH->SR |= FLASH_SR_PGAERR;
FLASH->SR |= FLASH_SR_WRPERR;
FLASH->SR |= FLASH_SR_PROGERR;
FLASH->SR |= FLASH_SR_OPERR;
FLASH->CR |= FLASH_CR_PER; //Page erase enable
FLASH->CR |= (15<<FLASH_CR_PNB_Pos); //Page number selection
FLASH->CR |= FLASH_CR_EOPIE; //End-of-operation interrupt enable
FLASH->CR |= FLASH_CR_STRT; // Start erase operation
while (FLASH->SR & FLASH_SR_BSY1);
if (FLASH->SR & FLASH_SR_EOP)
{
FLASH->SR |= FLASH_SR_EOP;
}
FLASH->CR &= ~FLASH_CR_PER; //Page erase disabled
//--------------------------------------------------
while (FLASH->SR & FLASH_SR_BSY1);
FLASH->SR |= FLASH_SR_OPTVERR;
FLASH->SR |= FLASH_SR_FASTERR;
FLASH->SR |= FLASH_SR_MISERR;
FLASH->SR |= FLASH_SR_PGSERR;
FLASH->SR |= FLASH_SR_SIZERR;
FLASH->SR |= FLASH_SR_PGAERR;
FLASH->SR |= FLASH_SR_WRPERR;
FLASH->SR |= FLASH_SR_PROGERR;
FLASH->SR |= FLASH_SR_OPERR;
FLASH->CR |= FLASH_CR_PG; //Flash memory programming enable
FLASH->CR |= (15<<FLASH_CR_PNB_Pos); //Page number selection
FLASH->CR |= FLASH_CR_EOPIE; //End-of-operation interrupt enable
*(__IO uint32_t*)0x08007800=(uint32_t)mydata;
*(__IO uint32_t*)0x08007804=0;
while (FLASH->SR & FLASH_SR_BSY1);
if (FLASH->SR & FLASH_SR_EOP)
{
FLASH->SR |= FLASH_SR_EOP;
}
FLASH->CR &= ~FLASH_CR_PG; //Page programming disabled
FLASH->CR &= ~FLASH_CR_EOPIE; //End-of-operation interrupt disabled
FLASH->CR |= FLASH_CR_LOCK;2) Set read protection memory. I do it like this:
if ((FLASH->OPTR & FLASH_OPTR_RDP) != 0xBB)
{
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
FLASH->OPTKEYR = 0x08192A3B;
FLASH->OPTKEYR = 0x4C5D6E7F;
while (FLASH->SR & FLASH_SR_BSY1);
FLASH->OPTR |= (0xBB << FLASH_OPTR_RDP_Pos);
while (FLASH->SR & FLASH_SR_BSY1);
FLASH->CR |= FLASH_CR_OPTSTRT;
while (FLASH->SR & FLASH_SR_BSY1);
FLASH->CR |= FLASH_CR_LOCK;
}After, if you restart the power supply, the microcontroller goes into some kind of incomprehensible state and it can no longer be programmed. It generally ceases to be determined by the programmer through the SWD interface. I also see that my program is not running. (I know this because it should flash the LED)
At the same time, I noticed that after I program the microcontroller, but do not turn off the power, I have the opportunity to connect to it and even erase the memory or write a new program to it. But no matter what I do after a power reset, everything leads to one result - the inability to connect to this microcontroller in any way.
I also noticed that after programming with this program, when I am in debug mode, sometimes (I did not find a pattern) the program can go into the address space> 0x1fff0000 and crashes there.
I don’t know what exactly affects the microcontroller writing to memory or setting read protection. But if I remove these two functions from the program, everything works without problems.
Thus, I have already spoiled more than 10 microcontrollers, and in order to continue to deal with this problem, I do not want to sacrifice new ones.
I use the original ST LINK V2 programmer and original STM32G030F6P6 microcontrollers, as well as the latest versions of STM32CubeIDE 1.11.2, STM32CubeProgrammer v2.12.0 and STM32 ST-LINK Utility.
The programmer is connected according to this scheme
If I forgot to mention something, I am ready to answer any questions. If you need to provide any diagrams, code snippets or any other information, just ask.
I welcome any idea, any opportunity to get to the bottom of the truth! If you need to "kill" more controllers - I'm ready!
If anyone has any ideas how to bring broken microcontrollers back to life (any idea) I'm willing to try.
