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 III

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

4 REPLIES 4
TDK
Super User

> 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 III

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 

Hi I am having a similar problem with same device, void SysTick_Handler(void) is not getting called at all. I dont have any other interrupts enabled or anything, just a blink led.

As an added weird thing, when I press the reset button, the program will not restart.

I only seem to have this problem with the STM32G070RB (code generated by cubeMx). I am also using a STM32G0B0CE in same configuration without issues (code generated by cubeMx as well).

Did you end up solving the issue?

b1063n
Associate

Well this is a bit silly since I just asked the question a few moments ago, but I fixed the issue and I wanted to share how I did it, because ***, I spent all day on this nonsense.

So, I reprogrammed the nBOOT_SEL, nBOOT1 and nBOOT0 accordingly (using cubeprogrammer) a few times back and forth to be sure (paranoid), erased full memory, and systick works normally now. That was it, magic. I suppose it had something todo with the vector table and booting from flash or ram.

That did it!