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.
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- The relationship between FLASH, RAM and ROM
- Introduction to MSP430 development tools (JTAG SBW BS)
- There is nothing that cannot be embedded, only imagination can be used. Popular Science of Embedded Systems
- Partial Discharge in Switching Power Supplies
- The experiment got me all greasy:lol
- EEWORLD University Hall----Live Replay: Supporting 'Dual Carbon' - ADI Smart Factory Solution
- Multi-clock design strategy for FPGA in large-scale design.pdf
- [Mil MYD-YA15XC-T Review] + Unboxing and Power-on Test
- The first case in China! A 72-year-old high-level paraplegic patient eats fried dough sticks and plays mahjong with his mind. Zhejiang University's brain-computer interface has achieved many firsts
- I made a website to download Keil, Cube and other resources. Does anyone want to try it? It's free and has no speed limit.