cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G070RB Boot problem and Option bytes not setting right. No option for Boot Lock in code.

CJami
Associate II

Hi,

I am using STM32G070RB for a modbus based product. The code is complete but i am facing issues that on some of the controller the code does not work and it gets stuck in the HAL_DELAY() function because the systick interrupt are not getting through.

Strange and concerning thing is that the code works on many controllers without problem but on few of them random controllers we are facing this issue.

I have tried setting the nBOOT_SEL, nBOOT1 and nBOOT0 in the code so that the vector table address get set correctly.

void ConfigureBootToUserFlash(void)
{
    // Unlock the Flash memory control register
    if (HAL_FLASH_Unlock() == HAL_OK)
    {
        // Unlock the Option Bytes
        if (HAL_FLASH_OB_Unlock() == HAL_OK)
        {
            FLASH_OBProgramInitTypeDef obInit = {0};

            // Read current option bytes
            HAL_FLASHEx_OBGetConfig(&obInit);

            // Check if nBOOT_SEL and nBOOT0 are already configured
            if (!(obInit.USERConfig & FLASH_OPTR_nBOOT_SEL) ||  // nBOOT_SEL not set
                (obInit.USERConfig & FLASH_OPTR_nBOOT0))       // nBOOT0 not cleared
            {
                // Configure the option bytes to boot from Flash memory
                obInit.OptionType = OPTIONBYTE_USER;
                obInit.USERConfig |= FLASH_OPTR_nBOOT_SEL; // Set nBOOT_SEL
                obInit.USERConfig &= ~FLASH_OPTR_nBOOT0;  // Clear nBOOT0

                // Apply the new configuration
                if (HAL_FLASHEx_OBProgram(&obInit) == HAL_OK)
                {
                    // Launch the option bytes loading process (only if changes were made)
                    HAL_FLASH_OB_Launch();
                }
                else
                {
                    // Error: Option byte programming failed
                    Error_Handler();
                }
            }
            else
            {
                // Option bytes are already correctly configured; no reset required
            }

            // Lock the Option Bytes
            HAL_FLASH_OB_Lock();
        }
        else
        {
            // Error: Option byte unlock failed
            Error_Handler();
        }

        // Lock the Flash memory control register
        HAL_FLASH_Lock();
    }
    else
    {
        // Error: Flash unlock failed
        Error_Handler();
    }
}

 

Even after setting this function i am facing the same issue, the issue gets resolved by setting the BOOT_LOCK using the stm32cubeprogrammer. I am unable to find option to set it in the code.

 

First question is why controllers are bhaving differently?

Second is how to resolve it in the code, because production just flash the firmware and i can not rely on them to turn on the boot lock everytime? 

It never happened before, so why now!!

Regards,

Hamza Jamil

2 REPLIES 2
TDK
Guru

> I have tried setting the nBOOT_SEL, nBOOT1 and nBOOT0 in the code so that the vector table address get set correctly.

These have no effect on whether or not SysTick is happening.

 

When HAL_Delay "isn't working", pause the code, examine why. Ensure:

  • Interrupts are enabled globally (i.e. no __disable_irq() statements).
  • SysTick interrupt is higher priority (numerically lower) than the current interrupt handler, if any.

If those check out, show screenshots proving it. Has to be an issue somewhere that isn't being spotted.

If you feel a post has answered your question, please click "Accept as Solution".
CJami
Associate II

Interrupt are enabled globally, i intentionally called __enable_irq() to check out this.

Systick interrupt is higher priority, and HAL_Delay() is called before using any peripherals so no other interrupts are happening at that time. In test code i used to to just blink the LED. So this checks out as well.

I have tested out whatever i could BEFORE POSTING QUESTION here.

What type of screenshots you need?

 

Regards,

Hamza Jamil