STM32G031K6 - self-locking procedure and cannot reach MCU anymore through SWD
Hello,
Having an issue with STM32G031K6Ux MCU. Below added function that checks Options bytes, and if needed - makes changes (power re-cycle is needed after it).
This function CheckOptionBytes() works OK on several MCU's. But on this MCU we have an issue, that sometimes after flashing - we cannot reach MCU anymore, and we are thinking that it locks to Readout Protection Level 2. But it's random. We can reflash the same MCU N amounts, and then it locks in a random moment.
I cannot see anything on Errata about it. Only about the BOOT0 pin issue on RDP1 (2.1.3 section), but it's not our case, we are not using it and it will not block reaching MCU again through SWD.
Does anyone see a possible issue with the function or know another possible cause?
Thank You!
Code below:
static void CheckOptionBytes(void)
{
FLASH_OBProgramInitTypeDef OBInit =
{ 0 };
/* Read Option bytes */
HAL_FLASHEx_OBGetConfig(&OBInit);
/* Check BOR, BOOT0 and RDP Option Bytes */
if (((OBInit.USERConfig & OB_BOR_ENABLE) != OB_BOR_ENABLE)
|| ((OBInit.USERConfig & FLASH_OPTR_nBOOT_SEL) != OB_BOOT0_FROM_OB)
|| ((OBInit.USERConfig & FLASH_OPTR_nBOOT0) != OB_nBOOT0_SET)
|| (OBInit.RDPLevel != OB_RDP_LEVEL_1))
{
/* Lock */
OBInit.OptionType = (OPTIONBYTE_USER | OPTIONBYTE_RDP);
OBInit.USERType = (OB_USER_BOR_EN | OB_USER_BOR_LEV | OB_USER_nBOOT_SEL | OB_USER_nBOOT0);
OBInit.USERConfig = (OB_BOR_ENABLE | OB_BOR_LEVEL_FALLING_2 | OB_BOR_LEVEL_RISING_3 | OB_BOOT0_FROM_OB | OB_nBOOT0_SET);
OBInit.RDPLevel = OB_RDP_LEVEL_1;
/* Unlock the FLASH & Option Bytes Registers access */
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
/* Clear error programming flags */
__HAL_FLASH_CLEAR_FLAG(FLASH_SR_ERRORS);
/* Program Option bytes */
if (HAL_FLASHEx_OBProgram(&OBInit) == HAL_OK)
{
/* Change OB and Reset MCU */
HAL_FLASH_OB_Launch();
}
/* Lock */
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
}
}Update.
Using:
- STM32Cube FW_G0 V1.5.0,
- STM32CubeIDE 1.7.0
- GNU11, GNU Tools for STM32 (9-2020-q2-update)
- Configuration: Release (Optimize for Size (-Os))
