cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 series: Bootloader from system memory

Lavanya1
Associate II

Hi, I'm using an STM32L4P5ZG microcontroller, trying to do a bootloader program. Entering into bootloader mode, exit after a fraction of a second and jump to the main loop. Not staying in the bootloader mode. I tried enabling through software to PH3 pin as pull-high state as well tried the last option by making nBoot1 =1, nBoot0& nSWBoot0=0. Both returned same, not staying in bootloader mode. Whenever I do manually PH3 boot0 pin as pull-high, entering into bootloader mode. 

Lavanya1_0-1696396826542.png

 

Code: 

HAL_StatusTypeDef SystemBootloaderJump(void)

{

  typedef void (*pFunction)(void);

 

  FLASH_OBProgramInitTypeDef OBInit;

 

  uint32_t  JumpAddress = SYS_BOOTLOADER_ADDR;  // This will be aliased to 0x00000000

  pFunction JumpToSystemBoot = (void (*)(void))(JumpAddress);

 

  // Delete the signature bin file

  strcat(path, "CFC.bin");

  f_unlink (path);

 

  // Alias the system memory from the boot memory space starting @0x1FFF0000

  HAL_FLASHEx_OBGetConfig(&OBInit);                                 // OBInit.USERConfig holds OPTR data

  OBInit.OptionType = OPTIONBYTE_USER;

  OBInit.USERType |= OB_USER_nBOOT1;

  OBInit.USERType &= ~(OB_USER_nSWBOOT0 | OB_USER_nBOOT0);          // System memory is selected as boot area

 

  // Set new values for the Options page and launch

  if (HAL_FLASHEx_OBProgram(&OBInit) == HAL_OK)

  {

    if (HAL_FLASH_OB_Launch() == HAL_OK)

    {

       // Prepare to jump to System Boot Loader

       GPIOH->MODER &= ~(GPIO_MODER_MODE3_1 | GPIO_MODER_MODE3_0);  // PH3 as input (BOOT0)

       GPIOH->PUPDR &= ~(GPIO_PUPDR_PUPD3_1 | GPIO_PUPDR_PUPD3_0);  // Pull-up enabled for PE3 input

       GPIOH->PUPDR |=  GPIO_PUPDR_PUPD3_0;

       GPIOB->MODER &= ~(GPIO_MODER_MODE1_1);                       // PB1 (Drive_MODE0) as output

       GPIOB->MODER |= GPIO_MODER_MODE1_0;

       HAL_GPIO_WritePin(GPIOB, GPIO_ODR_OD1, GPIO_PIN_SET);        // Set PB1 high that drives BOOT0

       //HAL_RCC_DeInit();

       //__disable_irq();

            while (1)

            {

          Drv_LED(LED_HB, DRV_TOGGLE);

            }

            SysTick->CTRL = 0;

       SysTick->LOAD = 0;

            SysTick->VAL = 0;

            //__set_MSP(*(__IO uint32_t*)JumpAddress );

            //JumpToSystemBoot = (void (*)(void))(*((uint32_t *)(JumpAddress + 4)));

            //__set_MSP(DEF_STACK_STRT_ADDR);

           JumpToSystemBoot();

        }

    }

    // Unable to configure the option register / launch the new configuration

    return HAL_ERROR;

}

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Your code is a bit of a mess of commented out statements. It should be clear why it doesn't work in its current state as the __set_MSP line is commented out.

Perhaps take a look at known working code:

https://community.st.com/t5/stm32-mcus/how-to-jump-to-system-bootloader-from-application-code-on-stm32/ta-p/49424

 

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

View solution in original post

2 REPLIES 2
TDK
Guru

Your code is a bit of a mess of commented out statements. It should be clear why it doesn't work in its current state as the __set_MSP line is commented out.

Perhaps take a look at known working code:

https://community.st.com/t5/stm32-mcus/how-to-jump-to-system-bootloader-from-application-code-on-stm32/ta-p/49424

 

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

That code is known to be a broken bloatware as all of the code from the HAL team. The first comment on that article explains and shows it very clearly!