cancel
Showing results for 
Search instead for 
Did you mean: 

In STM32G473RB how to jump into system boot area using software.

NBhat.2
Associate

Hello, I am trying jump into system boot area using software. and try to update firware.

But this function is not working please help. After calling this function controller get reset and again start from main().

void firmware_update(void)

{

HAL_DeInit();

//usb_deinit();

HAL_RCC_DeInit();

SystemInit();

FLASH_OBProgramInitTypeDef pOBInit;

  /* Get the Option byte configuration */

HAL_FLASHEx_OBGetConfig(&pOBInit);

  /* BOOT_SEL = 0 */

  pOBInit.UserConfig &= ~(OB_USER_nSWBOOT0);

  /* nBOOT0=1 */

  pOBInit.UserConfig |= OB_USER_nBOOT0;

  /** HAL_FLASHEx_OBProgram && HAL_FLASHEx_OBErase

   * HAL_FLASH_OB_Unlock() should be called before to unlock the options bytes

   */

  if(HAL_FLASH_OB_Unlock() != HAL_OK)

  {

    return HAL_ERROR;

  }

  /*We need to erase option bytes before write. */

//   if(HAL_FLASHEx_OBErase() != HAL_OK)

//   {

//     return HAL_ERROR;

//   }

  /* Write changed option bytes */

  if(HAL_FLASHEx_OBProgram(&pOBInit))

  {

    return HAL_ERROR;

  }

  //Map the "system" memory to 0x00000000 as required by the ST BSL.

 SYSCFG->MEMRMP = SYSCFG_MEMRMP_MEM_MODE_0;

 // Restore the reset setting of VTOR (location of vector table), possibly required by the ST BSL.

 SCB->VTOR = 0;

 // Set the stack pointer and PC as if reset itself did it to induce the ST BSL.

 __set_MSP(*(uint32_t*)0x1FFF0000);

 __NO_RETURN void (*StBsl)() = (void *)(*(uint32_t*)0x1FFF0004);

 StBsl();

 HAL_Delay(1000);

}

2 REPLIES 2
Uwe Bonnes
Principal III

It was often a subject here, scan old messages. In short: Very early in program start, check for some magic in RAM that is not erased by reset and not set up by other code. If you see the magic, unset the magic,setup to jump to the system bootloader and jump to the system bootloader. In your program to start the bootloader: Set the magic and do a programmatically system reset.

Uwe Bonnes
Principal III

The bootloader switch is easiest done before anything else and before HAL setup.