IAP firmware upgrading method based on serial port
Through USART serial communication and self-made file transfer tools, efficient and accurate updates of embedded firmware are achieved, which solves the problems of large data packet storage area and error-prone transmission in existing methods and improves upgrade efficiency and correctness.
Patent Information
- Application Number
- CN202410301647.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-16
- Publication Date
- 2025-09-16
AI Technical Summary
Existing embedded firmware upgrade methods mostly rely on storage media or network downloads, resulting in large data packet storage area occupation, prone to errors during transmission, and low upgrade efficiency.
USART serial communication is used to realize data transmission. The embedded program Bin file is sent through a self-made upper computer file transfer tool, which is divided into packets and sent with a check bit added. The lower computer performs data verification and writes it to the FLASH area. The firmware upgrade flag is used to control program jumps to reduce data loss.
It improves the efficiency of firmware upgrades and the accuracy of data transmission, simplifies the program update process, and prevents data loss and data loss.
Smart Images

Figure CN120653274A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a firmware upgrade method, in particular to an IAP firmware upgrade method based on a serial port. Background Art
[0002] With the development of technology, a variety of embedded devices have emerged one after another, with increasingly powerful functions and increasingly complex software. In this situation, embedded firmware inevitably has problems, which requires continuous firmware iteration to make the product more and more stable. This provides a broad application prospect for embedded firmware upgrade technology.
[0003] IAP (In Application Programming) is a method in which the user's own program is used to burn part of the User Flash area during operation. The purpose is to be able to conveniently update and upgrade the firmware program in the product through the reserved communication port after the product is released. The current embedded firmware upgrade method is mostly to use storage media to directly upgrade the firmware, or to use a computer to download the firmware upgrade data through the network and then send it to the device for firmware upgrade. The IAP firmware upgrade method adopted in this article mainly receives the data packet sent by the upper layer through the serial port and then directly stores it in the program execution area waiting to jump to execute the data packet file. Compared with the traditional firmware upgrade, it reduces the data packet storage area and the number of data packets in the transmission process, thereby improving the efficiency of firmware upgrade and the accuracy of data transmission. Summary of the Invention
[0004] The bootloader and user application in the APP1 area can be burned through an emulator such as ST-Link or CMSIS-DAP. During the burning process, set the first address of the FLASH area of the bootloader and application to ensure that the program is burned to the specified FLASH area to prevent the FLASH area from overlapping during the program burning process.
[0005] Bootloader: The program executed after each power-on reset of the microcontroller. Therefore, the program should be stored at the first address 0x08000000 in the FLASH area by default, and a certain FLASH interval size should be selected to store the Bootloader program. During the execution of the program, a firmware upgrade identification judgment will be performed, and according to the corresponding flag bit, the program will be jumped to a different FLASH area to execute the user-written program. The internal program jump execution flow chart of the microcontroller is shown in the attached figure. Figure 3 shown.
[0006] APP1 area user application: The APP1 area user application needs to include upper and lower computer serial port communication, FLASH read and write operations, jump execution program and the user's own application part.
[0007] After executing the application program in the APP1 area, the embedded program data is transmitted and updated.
[0008] The present invention mainly realizes data transmission through USART serial port communication. First, before sending embedded data, the upper computer sends a firmware upgrade request command to the lower computer. After the lower computer receives the command from the upper computer, it first analyzes the size of the updated program sent this time. If the size meets the storage space size of the firmware upgrade program storage area, the upper computer is notified that the update can be normally performed, and the upper computer starts to send the updated embedded program data. If the size does not meet the size, the update cannot be normally performed. During the program data transmission process, the embedded program Bin file is sent through a self-made upper computer file transmission tool. During the sending process, the embedded program Bin file is divided into multiple packets of fixed-length data. At the same time, the total number of packets sent and the current number of packets sent are added to the head and tail of each packet of data sent as check bits, so that the lower computer can analyze and verify the received data to see if it is correct. At the same time, in order to prevent the program data from being lost during the transmission process, the sending adopts CRC check to perform data verification. After receiving the embedded program, the lower computer performs data packet verification. After the data packet is analyzed and processed, the head and tail are removed and the valid data is retained. When the complete IK data is received, it is written to the specified FLASH area (APP2 area). After the write is successful, the content of the written FLASH area is read and compared with the written data to ensure the accuracy of the data written to the FLASH area. This cycle is repeated until it is successfully received and written. If the last packet of data is not complete, a half packet is sent. At the same time, the FLASH area of the page should be erased as a whole page and then the remaining data is written, because the writing and erasing of the data in the FLASH area must be erased and written as a whole page; after the lower computer receives the program data, the upper computer sends an execution update program command. After receiving the command, the lower computer first writes a specific firmware upgrade flag (program jump execution APP2 area flag) in the public variable storage area to ensure that it can jump from the Bootloader area to the APP2 area after the next program reset, execute the new program in the APP2 area, and complete a firmware upgrade task.
[0009] After executing the application program in the APP2 area, the embedded program data is transmitted and updated.
[0010] The transmission of embedded programs in the APP2 area is the same as that in the APP1 area. The embedded program Bin file is sent through a self-made upper computer file transfer tool. The lower computer parses and processes each packet of data received. After receiving the complete IK data, it is written into the designated FLASH area (APP1 area), overwriting the program in the APP1 area before the update program, and using it as the next update program storage area. In this invention, both the APP1 area and the APP2 area can be used as the next update program storage area. The processing of half-packet data also adopts the method of erasing the entire page of the FLASH area and then writing the received half-packet data; after the embedded program data is completely received, the upper computer sends an execution update program command. After receiving the command, the lower computer first erases the original flag bit in the public variable storage area to ensure that the next time the program is reset, it can jump from the Bootloader area to execute the new program in the APP1 area, completing a firmware upgrade task.
[0011] After executing the updated program, it jumps back to the previous original program for execution.
[0012] When the MCU needs to jump back to the previous original program after executing the update program, the host computer only needs to send an execute update program command, without the need for the host computer to send the previous program embedded program Bin file for update. After the host computer sends the execute update program command, the lower computer will modify the contents of the firmware upgrade flag bit in the public variable storage area. The program will then jump back to the Bootloader program area to execute the Bootloader program. The program jump direction is determined within the Bootloader, completing the program jump from the executed update program to the previous original program. This operation mainly completes the program jump between different FLASH sections by modifying the firmware upgrade flag bit in the public variable storage area, reducing data loss during transmission. It is simple, convenient and practical.
[0013] The data sent during the embedded program update process is incorrect and the data verification failure processing mechanism.
[0014] This invention has a mechanism for multiple failures in the data receiving process. When the upper computer sends embedded package data, if the lower computer fails to verify the data, it will not reply to the upper computer with a successful data reception command. The upper computer will continue to send the package data. If the lower computer fails to verify multiple times, the program will automatically reset and execute. This mechanism can prevent the program from running away during execution. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Figure 1 This is a module diagram of the FLASH storage area of the single-chip microcomputer in an embodiment of the present invention;
[0016] Figure 2 It is a flow chart of the present invention's method for upgrading firmware using a serial port IAP;
[0017] Figure 3 This is a schematic diagram of the implementation of the serial port IAP firmware upgrade method within a single-chip microcomputer according to the present invention;
[0018] Figure 4 1 is a flow chart of an embodiment of the present invention in which a serial port IAP firmware upgrade method is applied; DETAILED DESCRIPTION
[0019] Detailed description with reference to the accompanying drawings
[0020] Combine Figure 4Note: The diagram shown shows the execution process when the microcontroller is powered on for the first time. The program executed on the first power-on is the source code burned by the manufacturer. Therefore, when performing the firmware upgrade process, it is necessary to manually burn the boot program - Bootloader and user application (APP1 area program) separately to replace the manufacturer's source code. In this method, the user needs to develop the boot program by himself to ensure that the Bootloader program is executed first after the microcontroller is powered on. The first step is to power on the microcontroller and execute the Bootloader program. During the execution, the firmware upgrade flag is checked to point to the first address of the FLASH area program. When the firmware is updated for the first time, the default flag points to the first address of the APP1 area program in the FLASH. Then the program jumps to the starting address of the interrupt vector table in the APP1 area through the jump function, and finally jumps to the entry of the main function of the APP1 area program through the interrupt vector function. During the jump process, the general interrupt should be blocked to prevent interrupt interference abnormalities during the program jump process; the second step is to jump to the user application in the APP1 area and turn on the general interrupt at the same time. When the serial port of the microcontroller receives the firmware upgrade command from the host computer, the lower computer first parses and processes the command. If the requirements are met, it replies to the host computer to start sending program data. The lower computer parses and processes each package of program data received, and writes the received program data to the APP2 area address of the FLASH through the FLASH read and write operation; the third step is that when all the program data are written to the APPA2 area, the host computer sends the execution firmware upgrade command, and the lower computer program starts to execute the jump function to jump to the starting address of the interrupt vector table in the APP2 area, and finally The interrupt vector function jumps to the entry of the main function of the program in the APP2 area. During the jump, the general interrupt should be disabled to prevent interrupt interference abnormalities during the program jump. The fourth step is to jump to the user application in the APP2 area and enable the general interrupt to complete a firmware upgrade process. The fifth step is to execute the user application in the APP2 area. When the serial port of the single-chip microcomputer receives the firmware upgrade command sent by the host computer, it also parses and processes the command. If the storage space size is met, it requests the host computer to send program data. The lower computer parses and processes each packet of data sent by the host computer. At the same time, the lower computer writes the received data to the APP1 area address of the FLASH through certain operations to replace the original program data. The sixth step is to wait for the host computer to send the execution firmware upgrade command after receiving the command. After receiving the command, the lower computer executes the jump function to jump to the starting address of the interrupt vector table in the APP1 area. Finally, it jumps to the entry of the main function of the program in the APP1 area through the interrupt vector function. At the same time, the general interrupt is disabled during the jump process. The seventh step is to jump to the user application in the APP1 area and enable the general interrupt to complete another firmware upgrade process.
Claims
1. A serial port-based IAP firmware upgrade method, characterized in that: The following steps are involved: (1) After the microcontroller is powered on for the first time, the boot program - Bootloader and user application (APP1 area program) need to be manually burned to replace the manufacturer's source code. In the method described above, the user needs to develop the boot program himself to ensure that the Bootloader program is executed first after the microcontroller is powered on; (2) The MCU executes the Bootloader program and checks the firmware upgrade flag during execution. It determines the first address of the FLASH area to which the program should jump based on the flag. (3) The program jumps to the starting address of the interrupt vector table in the FLASH area of the application through the jump function, and then jumps to the entry of the program main function through the interrupt vector function.
2. The embedded firmware upgrade method according to claim 1, wherein: The following steps are also included: (1) After the application is executed, the serial port receiving end receives the program data packet sent by the upper computer. The lower computer verifies and analyzes the received data packet, and resends the data packet if the verification fails. (2) After receiving the 1K data packet, extract the useful data and write it into the FLASH area; (3) After receiving all the data packets, check whether the total number of packets is missed or missed. If the verification is passed, reset the firmware upgrade flag.
Citation Information
Cited By
Satellite-borne equipment embedded system software on-orbit reconstruction method and satellite-borne equipment embedded system
CN121560366A