STM32 Bootloader Principle and Design

Publisher:大头玩家Latest update time:2018-08-26 Source: eefocusKeywords:STM32  Bootloader Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Many people want to upgrade the firmware of a product without disassembling it, which is not only convenient but also saves energy and cost. So how to complete this task? The Bootloader introduced below can complete this task, and complete the firmware upgrade through the Bootloader. Let's briefly analyze the STM32 Bootloader design.

First, let's talk about the differences and connections between ISP and IAP of stm32.

ISP (In-System Programming) means that blank devices on the circuit board can be programmed with end-user code without removing the device from the circuit board. Programmed devices can also be erased or reprogrammed using ISP. IAP (In-Application Programming ) means that the MCU can obtain new code in the system and reprogram itself, that is, the program can be changed by the program. ISP and IAP technologies are the future development direction of instruments and meters.

  1 Working Principles of ISP and IAP
  The implementation of ISP is relatively simple. Generally, the internal memory can be rewritten by the software of the host computer through the serial port. For the single-chip microcomputer , the data sent by the host computer can be received through SPI or other serial interfaces and written into the memory. Therefore, even if we solder the chip on the circuit board, as long as the serial port for the interface with the host computer is reserved, the internal memory of the chip can be rewritten without removing the chip. 
  The implementation of IAP is relatively complicated. When implementing the IAP function, there must be two storage areas inside the single-chip microcomputer, one of which is generally called the BOOT area and the other is called the storage area. The single-chip microcomputer is powered on and runs in the BOOT area. If the conditions for external rewriting of the program are met, the program in the storage area is rewritten. If the conditions for external rewriting of the program are not met, the program pointer jumps to the storage area and starts to execute the program placed in the storage area, thus realizing the IAP function. 
  2 Advantages of ISP and IAP 
  The advantage of ISP technology is that it can be used for experiments and development of single-chip microcomputers without a programmer. The single-chip microcomputer chip can be directly soldered to the circuit board, and the finished product is completed after debugging. It eliminates the inconvenience caused by frequent insertion and removal of chips to the chip and circuit board during debugging. 
  IAP technology maps the Flash memory into two storage bodies from a structural perspective. When the user program on one storage body is running, the other storage body can be reprogrammed, and then the program is transferred from one storage body to another. 
  The implementation of ISP generally requires very little external circuit assistance, while the implementation of IAP is more flexible. It can usually use the serial port of the single-chip microcomputer to connect to the RS232 port of the computer, and program the internal memory through a specially designed firmware program. Remote upgrades and maintenance can be easily achieved through the existing INTERNET or other communication methods.

IAP writing process

Design Thoughts

        The Bootloader is responsible for detecting whether there is a BIN file required for firmware update in the SD card. If the required BIN file is detected, it will start copying the file to update the firmware. After the update is completed, it will jump to the specified address to start executing the latest program.

  Key points

  The starting address of the internal FLASH of STM32 is 0X08000000, and the Bootloader program file is written from this address. The first address of the APP program is set immediately after the Bootloader. When the program starts to execute, the Bootloader program runs first. At this time, the Bootloader detects the BIN file in the SD card and copies it to the APP area to update the firmware. After the firmware update is completed, it is necessary to jump to the APP program to start executing the new program. To complete this last step, you need to understand the interrupt vector table of Cortex-M3:


  After the program starts, it will first take out the reset interrupt vector from the "interrupt vector table" to execute the reset interrupt program to complete the startup. After the reset interrupt program is completed, it will jump to the main function. It can be seen that in the last step of the design, it is necessary to set the stack top address according to the starting address of the APP program and the interrupt vector table, and obtain the reset interrupt address to jump to the reset interrupt program. Next, we will start to analyze the program design steps.

  Bootloader Programming

  1. Determine the first address where the APP program is stored

  #define FLASH_APP_ADDR 0x08010000 //Application program start address (stored in FLASH) The previous code is 0X08010000. It can be seen that the storage space reserved for the Bootloader program is 64K. The start address for storing the APP program is 0X08010000.

  2.Bootloader detects whether there is a BIN file

  gCheckFat = f_open(&FP_Struct,"/APP/LIKLON.BIN",FA_READ); //Read gCheckFat to determine whether the above code is to detect whether the file liklon.bin exists, where the liklon.bin file is the BIN file required for firmware upgrade.

  3. Copy the file to the specified address

  In the previous step, if gCheckFat is 0, it means that the required BIN file exists, and this step can be executed. f_read (&FP_Struct,ReadAppBuffer,512,(UINT *)&ReadNum); //Read 512 bytes Convert 512 bytes into 256 16-bit data and store them in the ChangeBuffer array, ready to write to FLASH. FlashWrite(FLASH_APP_ADDR + i * 512,ChangeBuffer,256); //Write the read data to the specified address Write 512 bytes of data to the APP program area. By reading and writing in this way, the update of the APP program area can be completed.

4. Jump to the new program to run

  After updating the program, you need to jump to the new program and start running. For specific implementation, see the following code:

  typedef void (*iapfun)(void); //Define a parameter of function type
  iapfun jump2app;
  __asm ​​void MSR_MSP(u32 addr) //Set the stack pointer
  {
        MSR MSP, r0
        BX r14
  }
  //Jump to the application segment
  //appxaddr: the starting address of the user code.
  void iap_load_app(u32 appxaddr)
  {
        if(((*(vu32*)appxaddr)&0x2FFE0000)==0x20000000) //Check whether the top address of the stack is legal.
        {
              jump2app = (iapfun)*(vu32*)(appxaddr+4);//The second word in the user code area is the program start address (reset address). Here we can see from the interrupt vector table that
              MSR_MSP(*(vu32*)appxaddr);//Initialize the APP stack pointer (the first word in the user code area is used to store the top address of the stack)
              jump2app(); //Jump to APP and execute reset interrupt program
        }
  }


  APP Programming Notes

  1. Compiling the software requires settings:


  The starting address of the APP program storage has been specified as 0x08010000 in the Bootloader program, so when designing the APP program, you need to set the compiler software here to modify the starting address and size.

  2. Modify the system_stm32f10x.c file


  The offset here is also modified according to the change of the starting address of the APP, as shown in the figure above.

  This article only briefly introduces the design of the Bootloader program. As a starting point, you can go deeper and add data verification and program encryption.

/// ...

1. Function prototype:

void Jump_Address(void)

{

if (((*(volatile u32*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)

{

test = (*(volatile u32*)ApplicationAddress);

JumpAddress = *(volatile u32*) (ApplicationAddress + 4);

Jump_To_Application = (pFunction) JumpAddress;

__set_MSP(*(volatile u32*) ApplicationAddress);

                Jump_To_Application();

}

}

2、if (((*(volatile u32*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)分析:

ApplicationAddress stores the first address of the user program Flash. (*(volatile u32*)ApplicationAddress) means to take the data in the first address of the user program. This data is the stack address of the user code. The stack address points to the RAM, and the starting address of the RAM is 0x20000000. Therefore, the above judgment statement is executed to determine whether the stack address of the user code falls in the interval of: 0x20000000~0x2001ffff. The size of this interval is 128K. The author consulted the RAM size of each STM32 model. At present, the maximum capacity of RAM can be 192K+4K, and the clock frequency is 168MHZ. In general, most of the chips we use fall in the interval of <128K RAM, so there is no big problem with the above judgment statement.


3. After the analysis in 2, test saves the stack address (and it is the top address of the application stack). Looking at the vector table of STM32, we can know that the top address of the stack + 4 stores the reset address, so JumpAddress stores the reset address.


4. After calling the __set_MSP function, the stack top address of the user code will be set to the stack top pointer


5. Jump_To_Application(); means setting the PC pointer to the reset address.


After the CORTEX-M3 is powered on, it detects the level of the BOOT pin to determine the location of the PC. For example, if BOOT is set to FLASH boot, the CPU will first take two addresses after booting: one is the top address of the stack, and the other is the reset address. Therefore, the writing of points 4 and 5 is possible.


Keywords:STM32  Bootloader Reference address:STM32 Bootloader Principle and Design

Previous article:S3C2440 -- Startup file and Makefile analysis
Next article:STM32 low power consumption--stop mode

Recommended ReadingLatest update time:2024-11-17 08:23

STM32 USB slave HID analysis
Overview Initialize the STM32 USB as a USB slave and use the standard HID protocol. The control board comes with VBUS power supply, so VBUS and GND pins are not required. Just connect 2 data cables to the computer. Source code analysis When connected to the computer via USB cable, a USB reset packet is receive
[Microcontroller]
Using STM32's Systick interrupt to achieve key debounce
Initialize systick when the system is initialized  void SysTickConfig(void) {   /* Set SysTick Timer for 100us interrupts  */   if (SysTick_Config(SystemCoreClock / 10000))   {     /* Capture error */     while (1);   }   /* Configure the SysTick handler priority */   NVIC_SetPriority(SysTick_IRQn, 0x0); } Add in st
[Microcontroller]
Detailed explanation of general timer based on stm32
The TM32 timer is a powerful module. The frequency of the timer is also very high. The timer can do some basic timing, as well as PWM output or input capture functions. Clock source problem: There are eight TIMx, of which TIM1 and TIM8 are connected to the APB2 bus, while TIM2-TIM7 are connected to the On the APB1 bus
[Microcontroller]
Detailed explanation of general timer based on stm32
STM32's five new products are launched, and heavy investment is made to actively expand production capacity!
After a long epidemic, ST held its first press conference in Beijing this year, launching a number of products, covering the full range of products in the STM32 product family. "These product launches represent ST entering a new era - the era of cloud connectivity and intelligent edge," said Julian, vice
[Microcontroller]
STM32's five new products are launched, and heavy investment is made to actively expand production capacity!
STM32 study notes: independent watchdog
1. The role of the watchdog The microcontroller system may run away due to external interference, resulting in an infinite loop. The watchdog circuit is to avoid this. If there is a problem with the system and the watchdog is not fed, the watchdog will reset the CPU due to timeout. 2. Independent watchdog of STM32
[Microcontroller]
STM32 study notes: independent watchdog
Design of transient motion parameter storage test system for STM32
Abstract: In the transient motion parameter test, higher requirements are put forward for the real-time performance and power consumption of the storage test system. A design scheme of an embedded storage test system based on STM32 is proposed, and the software and hardware design of the key parts of the system are in
[Test Measurement]
Design of transient motion parameter storage test system for STM32
STM32 AFIO
Maybe you think IO and AFIO are very simple. In fact, there are several misunderstandings that many people may not have noticed. When you only use a ready-made development board to learn, others have already helped you allocate resources, and all peripheral function learning is done according to the routines given to y
[Microcontroller]
STM32 AFIO
stm32 bootloader serial port upgrade program framework
1. IAP IAP is the abbreviation of In Application Programming. IAP means that the user's own program burns part of the User Flash area during operation. The purpose is to easily update the firmware program in the product through the reserved communication port after the product is released . Usually when users need t
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号