Skip to main content
FRieb.1
Associate II
November 4, 2021
Solved

STM32G0B1 programming Option Bytes stopped after OBL_Launch

  • November 4, 2021
  • 5 replies
  • 2994 views

The MCU stops when the program reaches this line (FLASH->CR |= FLASH_CR_OBL_LAUNCH;) in the function below.

The option bytes I want to write are at the moment still unchanged to the delivery state. Later they will be changed to RDP Level 1.

void Optionbytes_Config(void)
{
 Flash_Unlock();
 Optionbytes_Unlock();
 FLASH->OPTR = 0xFFFFFEAA;
 
 while((FLASH ->SR & FLASH_SR_BSY1) == FLASH_SR_BSY1);
 
 FLASH->CR |= FLASH_CR_OPTSTRT;
 
 while((FLASH ->SR & FLASH_SR_BSY1) == FLASH_SR_BSY1);
 
 FLASH->CR &= ~FLASH_CR_OPTSTRT;
 
 FLASH->CR |= FLASH_CR_OBL_LAUNCH;
 
 while((FLASH ->SR & FLASH_SR_BSY1) == FLASH_SR_BSY1);
 
 FLASH->CR |= FLASH_CR_LOCK;
 FLASH->CR |= FLASH_CR_OPTLOCK;
}

Thank you for your help.

    This topic has been closed for replies.
    Best answer by FRieb.1

    I made it. My workaround is that I now set the optionbytes directly at the end of the hex file.

    Only the two marked lines need to be inserted.

    0693W00000GXHfRQAX.png

    5 replies

    Andreas Bolsch
    Lead III
    November 4, 2021

    See RM0444, 5.1.2: "Option byte loader reset

    The option byte loader reset is generated when the OBL_LAUNCH bit (bit 27) is set in the

    FLASH_CR register. This bit is used to launch the option byte loading by software."

    BTW: "FLASH->CR &= ~FLASH_CR_OPTSTRT;" does nothing, OPSTSRT is reset automatically.

    FRieb.1
    FRieb.1Author
    Associate II
    November 5, 2021

    Thank you for your reply.

    I have removed this line, but it does not change anything.

    Andreas Bolsch
    Lead III
    November 5, 2021

    The point is, setting OBL_LAUNCH causes a RESET.

    Of course, removing this line won't change anything.

    FRieb.1
    FRieb.1AuthorBest answer
    Associate II
    November 11, 2021

    I made it. My workaround is that I now set the optionbytes directly at the end of the hex file.

    Only the two marked lines need to be inserted.

    0693W00000GXHfRQAX.png

    Martin M.
    Associate II
    December 9, 2021

    I experienced a similar issue with an STM32F030, which just "hangs" after setting FLASH_CR_OBL_LAUNCH bit in FLASH->CR register.

    According to the reference manual (RM0360, 3.5.5, page 61) the STM32F030 should reset after setting FLASH_CR_OBL_LAUNCH, but it just "hangs" and stops executing any further instruction. (A following power cycle will set the option bytes though.)

    My workaround is the usage of the independent watchdog (IWDG) which generates a system reset after setting FLASH_CR_OBL_LAUNCH in FLASH->CR register, which luckily also sets the option bytes as expected.

    There's another solution shown in this thread, using standby mode and RTC wakeup from standby.

    The whole thing might be an undocumented errata (at least of the STM32F0 series)(?)

    Just my 2 cents.