A software firmware online burning method, device and computer readable storage medium
By initializing the input/output ports and bus after the STM32 microcontroller's software program starts, detecting the power-down register value, and sending the burning command, the high cost and instability of remote firmware burning for STM32 microcontrollers are solved, realizing a low-cost and efficient online firmware burning solution.
Patent Information
- Application Number
- CN202211432619.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-11-16
- Publication Date
- 2026-08-25
- Estimated Expiration
- 2042-11-16
AI Technical Summary
Existing software programming solutions for STM32 microcontrollers are costly and suffer from excessively long communication lines when communicating over long distances, making it difficult to achieve stable and efficient online firmware upgrades.
After the microcontroller's software program starts, it initializes the general-purpose input/output ports and bus, checks whether the power-down register value points to the bootloader, and sends flash unlock, erase, and write commands in the interrupt state to control the microcontroller to execute the main program's programming task. The host computer sends the programming completion command to achieve online programming.
It enables low-cost online firmware burning, improves stability and efficiency, is suitable for firmware upgrades over long distances, and enhances production and R&D efficiency.
Smart Images

Figure CN115934114B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of microcontroller technology, and in particular to a method, device, and computer-readable storage medium for online firmware programming. Background Technology
[0002] Currently, ST-Link programmers are commonly used for programming STM32 microcontrollers. However, in mass production, using ST-Link programmers requires a dedicated interface on the microcontroller hardware for software programming; furthermore, ST-Link programmers are expensive, and the communication line cannot be too long during programming. Therefore, the above solution is not suitable for applications such as online firmware upgrades during after-sales service.
[0003] Furthermore, existing STM32 microcontroller bootloaders often use serial ports for data exchange. However, serial communication also suffers from drawbacks such as low data transmission efficiency and short transmission distance. Therefore, when programming via serial communication, the communication line cannot be too long.
[0004] Therefore, how to achieve online firmware burning over long distances at a lower cost has become an urgent technical problem to be solved. Summary of the Invention
[0005] To address the aforementioned technical deficiencies in the prior art, this invention proposes a method for online firmware flashing, the method comprising:
[0006] After the microcontroller's software program starts, the general-purpose input / output ports and bus are initialized, and the host computer detects whether the power-down register value of the microcontroller points to the microcontroller's bootloader program through the input / output ports and the bus.
[0007] If the power-down register value points to the bootloader, then the receive interrupt of the bus is enabled, and the host computer sends flash unlock, flash erase, and flash write instructions to the microcontroller through the loop of the receive interrupt;
[0008] The microcontroller is controlled to execute the main program burning task by the instructions for unlocking, erasing, and writing to the flash memory, and the host computer sends the burning completion instruction to the microcontroller.
[0009] When the bootloader receives the burning completion instruction, the software program jumps to the main program and runs it.
[0010] Optionally, after the microcontroller's software program starts, the general-purpose input / output ports and bus are initialized, and the host computer detects whether the power-down register value of the microcontroller points to the microcontroller's bootloader through the input / output ports and the bus, before the following steps are taken:
[0011] The bootloader references the HAL library functions of the microcontroller, and the bootloader is compiled at the starting address 0x8000000 by a preset programming language compiler.
[0012] The space occupied by the bootloader is determined to be 16KB, and the starting address of the main program is determined to be 0x8004000.
[0013] Optionally, after the microcontroller's software program starts, the general-purpose input / output ports and bus are initialized, and the host computer detects whether the power-down register value of the microcontroller points to the microcontroller's bootloader through the input / output ports and the bus, the process further includes:
[0014] The underlying program of the bootloader is created using the HAL library functions, and the underlying program is divided into bus driver, lamp driver and flash driver.
[0015] The bus driver configures the registers for bus communication, the lamp driver configures the lamp pins on the printed circuit board, and the flash driver configures the functions for unlocking, erasing, and writing to the flash memory.
[0016] Optionally, after the microcontroller's software program starts, the general-purpose input / output ports and bus are initialized, and the host computer detects whether the power-down register value of the microcontroller points to the microcontroller's bootloader through the input / output ports and the bus, the process further includes:
[0017] Create the application layer program of the bootloader, wherein the application layer program includes bootloader function, program status reporting function and bus signal transmission and reception processing function;
[0018] The bootloader function executes instruction parsing and writes the version number to flash memory. The program status reporting function reports the software and hardware version number based on the received instructions from the host computer. The bus signal transceiver processing function executes bus receive interrupt handling and bus transmit.
[0019] Optionally, after the microcontroller's software program starts, the general-purpose input / output ports and bus are initialized, and the host computer detects whether the power-down register value of the microcontroller points to the microcontroller's bootloader through the input / output ports and the bus, including:
[0020] If the power-down register value does not point to the bootloader, then it is determined that the main program has been burned to the specified address;
[0021] The program jumps to the main program and runs.
[0022] Optionally, the step of initializing the general-purpose input / output ports and bus after the microcontroller's software program starts, and having the host computer detect whether the power-down register value of the microcontroller points to the microcontroller's bootloader via the input / output ports and the bus, further includes:
[0023] The bus driver initializes the general-purpose input / output ports and the bus.
[0024] The host computer detects whether the power-down register value of the microcontroller points to the microcontroller's bootloader program through the input / output port and the bus.
[0025] Optionally, if the power-down register value points to the bootloader, then the receive interrupt of the bus is enabled, and the host computer sends flash unlock, flash erase, and flash write instructions to the microcontroller through the loop of the receive interrupt, including:
[0026] If the power-down register value points to the bootloader, then the bus receive interrupt is enabled through the bus driver;
[0027] The host computer sends flash memory unlock, flash memory erase, and flash memory write commands to the microcontroller through the interrupt reception loop.
[0028] Optionally, the step of controlling the microcontroller to execute the main program programming task through the instructions for unlocking, erasing, and writing to the flash memory, and having the host computer send a programming completion instruction to the microcontroller, includes:
[0029] The bootloader function parses the instructions for unlocking, erasing, and writing to the flash memory.
[0030] After completing the main program's programming task, the program status reporting function receives the host computer's instructions and returns the hardware and software version number. The host computer then sends a programming completion instruction to the microcontroller.
[0031] The present invention also proposes a software firmware online flashing device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the computer program is executed by the processor, it implements the steps of the software firmware online flashing method as described in any of the preceding claims.
[0032] The present invention also proposes a computer-readable storage medium storing a software firmware online flashing program, which, when executed by a processor, implements the steps of the software firmware online flashing method as described in any of the preceding claims.
[0033] The software firmware online flashing method, device, and computer-readable storage medium of this invention, after the microcontroller's software program starts, initializes the general-purpose input / output ports and bus. The host computer then detects whether the power-down register value of the microcontroller points to the microcontroller's bootloader through the input / output ports and the bus. If the power-down register value points to the bootloader, the host computer enables the bus's receive interrupt and sends flash unlock, flash erase, and flash write instructions to the microcontroller through a loop of the receive interrupt. The host computer controls the microcontroller to execute the main program's flashing task using these instructions and sends a flashing completion instruction to the microcontroller. When the bootloader receives the flashing completion instruction, the software program jumps to the main program and runs it. This provides a low-cost online software firmware flashing solution, making online software firmware flashing more stable and efficient over long distances, greatly improving production and R&D efficiency. Attached Figure Description
[0034] The present invention will be further described below with reference to the accompanying drawings and embodiments. In the accompanying drawings:
[0035] Figure 1 This is the first flowchart of the online firmware flashing method of the present invention;
[0036] Figure 2 This is a software architecture diagram of the bootloader of the online firmware flashing method of the present invention;
[0037] Figure 3 This is a flowchart of the second embodiment of the online firmware flashing method of the present invention;
[0038] Figure 4 This is a flowchart of the third embodiment of the online firmware flashing method of the present invention;
[0039] Figure 5 This is a flowchart of the fourth embodiment of the online firmware flashing method of the present invention;
[0040] Figure 6 This is a flowchart of the fifth embodiment of the online firmware flashing method of the present invention;
[0041] Figure 7 This is a flowchart of the sixth embodiment of the online firmware flashing method of the present invention;
[0042] Figure 8 This is a flowchart of the seventh embodiment of the online firmware flashing method of the present invention;
[0043] Figure 9 This is a flowchart of the eighth embodiment of the online firmware flashing method of the present invention. Detailed Implementation
[0044] It should be understood that the specific embodiments described herein are for illustrative purposes only and are not intended to limit the scope of the invention.
[0045] In the following description, the use of suffixes such as "module," "part," or "unit" to denote elements is solely for the purpose of illustrative purposes and has no specific meaning in itself. Therefore, "module," "part," or "unit" may be used interchangeably.
[0046] Please refer to Figure 1 The diagram shows a first flowchart of the online firmware flashing method of the present invention. This embodiment proposes an online firmware flashing method, which includes:
[0047] S1. After the microcontroller's software program starts, the general-purpose input / output ports and bus are initialized, and the host computer detects whether the power-down register value of the microcontroller points to the microcontroller's bootloader program through the input / output ports and the bus.
[0048] S2. If the power-down register value points to the bootloader, then the bus receive interrupt is enabled, and the host computer sends flash unlock, flash erase, and flash write instructions to the microcontroller through the loop of the receive interrupt.
[0049] S3. The microcontroller is controlled to execute the main program burning task by the instructions of unlocking the flash memory, erasing the flash memory, and writing the flash memory, and the host computer sends the burning completion instruction to the microcontroller.
[0050] S4. When the bootloader receives the burning completion instruction, the software program jumps to the main program and runs it.
[0051] In this embodiment, the software first initializes GPIO (General Purpose Input Output) and CAN (Controller Area Network, an internationally standardized serial communication protocol). Then, the software reads the value of the backup register to determine whether the main program has been programmed. If the main program has been programmed to the specified address, the program jumps directly to the main program. If the main program has not yet been programmed to the specified address, the program enters the Bootloader program. Next, the CAN receive interrupt is enabled, and the FLASH flash memory is unlocked, erased, and written by receiving instructions from the host computer via the CAN bus. Finally, when the main program file is programmed and the host computer issues a programming completion command, the Bootloader program jumps to run the main program.
[0052] Please refer to Figure 2 The diagram shows the software architecture of the bootloader for the online firmware flashing method of the present invention. In this embodiment, the bootloader includes a low-level program and an application-level program. The low-level program is divided into a CAN driver, which configures CAN communication-related registers, including parameters such as baud rate and CAN filter; an LED driver, which configures the relevant pins of the LEDs on the PCB; and a FLASH driver, which encapsulates functions for unlocking, erasing, and writing FLASH data. Optionally, in this embodiment, all the above low-level driver functions reference the HAL library functions of the STM32 microcontroller and perform corresponding functions by manipulating registers through the HAL library functions. Optionally, the flashing program in this embodiment is compiled using GCC (GNU Compiler Collection) to support both Windows and Linux platforms.
[0053] Please continue to refer to this. Figure 2 The bootloader in this embodiment also includes an application layer program, which includes a Bootloader function, a Bootloader instruction parsing function, and a version number writing function to FLASH, etc.; the application layer program also includes a program status reporting function, which receives instructions from the host computer to report the hardware and software version number; the application layer program also includes a CAN signal transceiver processing function, which performs CAN receive interrupt processing and CAN transmit operations, etc.
[0054] Optionally, in this embodiment, please refer to Figure 3After the microcontroller's software program starts, the general-purpose input / output ports and bus are initialized, and the host computer detects whether the power-down register value of the microcontroller points to the microcontroller's bootloader through the input / output ports and the bus. This process includes the following steps:
[0055] S01. The bootloader references the HAL library functions of the microcontroller and compiles the bootloader at the starting address 0x8000000 using a preset programming language compiler.
[0056] S02. Determine that the space occupied by the bootloader is 16KB, and determine that the starting address of the main program is 0x8004000.
[0057] In this embodiment, when the bootloader is compiled at a non-starting address, the starting address of the main program is determined based on the compilation address of the bootloader and the space occupied by the bootloader.
[0058] Optionally, in this embodiment, please refer to Figure 4 After the microcontroller's software program starts, the general-purpose input / output ports and bus are initialized, and the host computer detects whether the power-down register value of the microcontroller points to the microcontroller's bootloader through the input / output ports and the bus. Prior to this, the process also includes:
[0059] S03. Create the underlying program of the bootloader through the HAL library functions, and divide the underlying program into bus driver, lamp driver and flash driver;
[0060] S04. Configure the registers for bus communication through the bus driver, configure the lamp pins on the printed circuit board through the lamp driver, and configure the function functions for unlocking, erasing, and writing to the flash memory through the flash memory driver.
[0061] In this embodiment, the corresponding light signal is determined based on whether the power-down register value points to the state of the microcontroller's bootloader, and the corresponding light is driven by the light to display the corresponding signal light; optionally, the signal light displays one or more of the following: the current working state of the bootloader, the state of the flash memory unlock, the state of the flash memory erase and the state of the flash memory write, and the state of jumping from the current program to the main program.
[0062] Optionally, in this embodiment, please refer to Figure 5 After the microcontroller's software program starts, the general-purpose input / output ports and bus are initialized, and the host computer detects whether the power-down register value of the microcontroller points to the microcontroller's bootloader through the input / output ports and the bus. Prior to this, the process also includes:
[0063] S05. Create the application layer program of the bootloader, wherein the application layer program includes bootloader function, program status reporting function and bus signal transmission and reception processing function.
[0064] S06. The bootloader function executes instruction parsing and writes the version number to flash memory. The program status reporting function reports back the hardware and software version number based on the received instructions from the host computer. The bus signal transceiver processing function executes bus receive interrupt processing and bus transmit.
[0065] In this embodiment, when it is determined that the power-down register value points to the bootloader, the program status reporting function first feeds back the hardware and software version number according to the instructions received from the host computer; then the bootloader function performs instruction parsing and writes the version number to flash memory.
[0066] Optionally, in this embodiment, please refer to Figure 6 The process of initializing the general-purpose input / output ports and bus after the microcontroller's software program starts, and having the host computer detect whether the power-down register value of the microcontroller points to the microcontroller's bootloader through the input / output ports and the bus, includes:
[0067] S11. If the power-down register value does not point to the bootloader, then it is determined that the main program has been burned to the specified address;
[0068] S12. The software program jumps to the main program and runs.
[0069] In this embodiment, if the power-down register value does not point to the bootloader, it is determined that the main program has been burned to the specified address. Further, the program status reporting function feeds back the hardware and software version number according to the instructions received from the host computer. If it is determined that the version number is consistent with the main program to be written, the software program jumps to the main program and runs it.
[0070] Optionally, in this embodiment, please refer to Figure 7 The process of initializing the general-purpose input / output ports and bus after the microcontroller's software program starts, and having the host computer detect whether the power-down register value of the microcontroller points to the microcontroller's bootloader via the input / output ports and the bus, further includes:
[0071] S13. Initialize the general-purpose input / output ports and the bus through the bus driver;
[0072] S14. The host computer detects whether the power-down register value of the microcontroller points to the bootloader program of the microcontroller through the input / output port and the bus.
[0073] In this embodiment, the program status reporting function feeds back the hardware and software version number based on the instructions received from the host computer. If it is determined that the version number is inconsistent with the main program version number to be written, the general-purpose input / output port and bus are initialized through the bus driver, and the subsequent main program re-writing steps are started.
[0074] Optionally, in this embodiment, please refer to Figure 8 If the power-down register value points to the bootloader, then the receive interrupt of the bus is enabled, and the host computer sends flash unlock, flash erase, and flash write instructions to the microcontroller through the loop of the receive interrupt, including:
[0075] S21. If the power-down register value points to the bootloader, then enable the receive interrupt of the bus through the bus driver;
[0076] S22. Through the cycle of receiving interrupts, the host computer sends flash unlock, flash erase, and flash write commands to the microcontroller.
[0077] In this embodiment, the bus signal transceiver processing function handles the corresponding bus receive interrupt in the receive interrupt loop, and the host computer sends flash unlock, flash erase, and flash write instructions to the microcontroller.
[0078] Optionally, in this embodiment, please refer to Figure 9 The instructions for unlocking, erasing, and writing to the flash memory control the microcontroller to execute the main program's programming task, and the host computer sends a programming completion instruction to the microcontroller, including:
[0079] S31. The instructions for unlocking the flash memory, erasing the flash memory, and writing to the flash memory are parsed through the bootloader function;
[0080] S32. After completing the main program's programming task, the host computer receives the host computer's instructions and returns the hardware and software version number through the program status reporting function, and the host computer sends a programming completion instruction to the microcontroller.
[0081] In this embodiment, after the main program of the bus sends the task through the above signal transmission and reception processing function, the instructions for unlocking the flash memory, erasing the flash memory, and writing the flash memory are parsed through the boot loading function, so that the microcontroller executes the corresponding flash memory unlocking, flash memory erasing, and flash memory writing tasks, thereby achieving the burning task of the main program.
[0082] The beneficial effect of this embodiment is that, after the microcontroller's software program starts, the general-purpose input / output ports and bus are initialized, and the host computer detects whether the power-down register value of the microcontroller points to the microcontroller's bootloader through the input / output ports and the bus; if the power-down register value points to the bootloader, the receive interrupt of the bus is enabled, and the host computer sends flash unlock, flash erase, and flash write instructions to the microcontroller through the loop of the receive interrupt; the flash unlock, flash erase, and flash write instructions control the microcontroller to execute the main program's burning task, and the host computer sends a burning completion instruction to the microcontroller; when the bootloader receives the burning completion instruction, the software program jumps to the main program and runs it. This achieves a low-cost online software firmware burning scheme, making online software firmware burning more stable and efficient over long distances, greatly improving production and R&D efficiency.
[0083] Based on the above embodiments, the present invention also proposes a software firmware online flashing device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the computer program is executed by the processor, it implements the steps of the software firmware online flashing method as described in any of the above embodiments.
[0084] It should be noted that the above-described device embodiments and method embodiments belong to the same concept. The specific implementation process can be found in the method embodiments, and the technical features in the method embodiments are also applicable to the device embodiments, which will not be repeated here.
[0085] Based on the above embodiments, the present invention also proposes a computer-readable storage medium storing a software firmware online flashing program, wherein when the software firmware online flashing program is executed by a processor, it implements the steps of the software firmware online flashing method as described in any of the above embodiments.
[0086] It should be noted that the above-described medium embodiments and method embodiments belong to the same concept. The specific implementation process can be found in the method embodiments, and the technical features in the method embodiments are also applicable to the medium embodiments, which will not be repeated here.
[0087] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0088] The sequence numbers of the above embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0089] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes several instructions to cause a terminal (which may be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in the various embodiments of the present invention.
[0090] The embodiments of the present invention have been described above with reference to the accompanying drawings. However, the present invention is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of the present invention without departing from the spirit and scope of the claims. All of these forms are within the protection scope of the present invention.
Claims
1. A method for online firmware flashing, characterized in that, The method includes: The microcontroller's bootloader is compiled at the starting address using a pre-defined programming language compiler. The space occupied by the bootloader is determined, and the starting address of the main program executed by the microcontroller is determined by combining the starting address of the bootloader. The bootloader references the HAL library functions of the microcontroller; The underlying program of the bootloader is created using the HAL library functions, and the underlying program is divided into bus driver, lamp driver and flash driver. The bus driver configures the registers for bus communication, the lamp driver configures the lamp pins on the printed circuit board, and the flash driver configures the functions for flash unlocking, flash erasing, and flash writing. Create the application layer program of the bootloader, wherein the application layer program includes a bootloader function, a program status reporting function, and a bus signal transmission and reception processing function; The bootloader function executes instruction parsing and writes the version number to flash memory; the program status reporting function reports back the hardware and software version number based on the received instructions from the host computer; and the bus signal transceiver processing function executes bus receive interrupt processing and bus transmit. After the microcontroller's software program starts, the general-purpose input / output ports and bus are initialized through the bus driver, and the host computer detects whether the power-down register value of the microcontroller points to the bootloader program through the input / output ports and the bus. If the power-down register value points to the bootloader, then the bus receive interrupt is enabled by the bus driver, and the bus signal transceiver processing function sends flash unlock, flash erase, and flash write instructions to the microcontroller in the loop of processing the receive interrupt; If the power-down register value does not point to the bootloader, it is determined that the main program has been burned to the specified address, and the software program jumps to the main program and runs it; The microcontroller is controlled to execute the main program burning task by the instructions for unlocking, erasing, and writing the flash memory, and the host computer sends the burning completion instruction to the microcontroller. When the bootloader receives the burning completion instruction, the software program jumps to the main program and runs it.
2. The online firmware flashing method according to claim 1, characterized in that, After the microcontroller's software program starts, the general-purpose input / output ports and bus are initialized. The host computer then checks whether the power-down register value of the microcontroller points to the microcontroller's bootloader via the input / output ports and the bus. This process includes the following steps: The bootloader is compiled at address 0x8000000 using the programming language compiler. The space occupied by the bootloader is determined to be 16KB, and the starting address of the main program is determined to be 0x8004000.
3. The online firmware flashing method according to claim 1, characterized in that, The instructions for unlocking, erasing, and writing to the flash memory control the microcontroller to execute the main program programming task, and the host computer sends a programming completion instruction to the microcontroller, including: The bootloader function parses the instructions for unlocking, erasing, and writing to the flash memory. After completing the main program's programming task, the program status reporting function receives the host computer's instructions and returns the hardware and software version number. The host computer then sends a programming completion instruction to the microcontroller.
4. A software firmware online flashing device, characterized in that, The device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When executed by the processor, the computer program implements the steps of the software firmware online flashing method as described in any one of claims 1 to 3.
5. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a software firmware online flashing program, which, when executed by a processor, implements the steps of the software firmware online flashing method as described in any one of claims 1 to 3.
Citation Information
Patent Citations
Bootloader method for supporting simultaneous upgrading of multiple ECUs of the same type and allowing ECUs to have program mismatching preventing function
CN108920168A