Single-chip microcomputer upgrading method, control device and storage medium
By using a unique identification code in a microcontroller device to determine the matching of the program upgrade code and performing a rollback operation when the match is not matched, the problem of the problem of the upgrade program code errors or exceptions in the prior art is solved, ensuring the stable operation of the device.
Patent Information
- Application Number
- CN202411899309.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-19
- Publication Date
- 2025-05-06
AI Technical Summary
The prior art cannot effectively detect and deal with the problem of equipment not being able to use normally due to upgrade program code errors or exceptions.
By pre-storing the unique identification code in the microcontroller device, it is determined whether the code identification of the program upgrade code matches the unique identification code, and when running the target program, whether the program upgrade code can be run on the microcontroller device based on the unique identification code. If not, reset operation and rollback operation will be performed.
It can detect whether the upgrade program code matches the microcontroller device and performs rollback operations when it cannot run normally to ensure the safe and stable operation of the device.
Smart Images

Figure CN119938071A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of program upgrade, and in particular to a single chip computer upgrade method, a control device and a storage medium. Background Art
[0002] OTA (Over-the-Air) upgrade function is a technology that remotely updates device software or firmware through wireless networks (such as Wi-Fi, cellular networks, etc.). This technology enables device manufacturers to push new software versions or firmware updates to devices remotely after the device has been delivered to the customer, without the need for users to manually intervene or take the device to a repair center for update.
[0003] The existing OTA rollback function is designed for situations where the upgrade code transmission is interrupted. When the upgrade data is interrupted, the system automatically performs an OTA rollback to restore the device to the previous version of the program. However, for other situations that cause program failure, such as upgrade program code errors or data anomalies, it may not be able to be effectively identified and processed, causing the device to run in the wrong program and be unable to be used normally. Summary of the invention
[0004] The technical problem to be solved by the embodiments of the present invention is to provide a single-chip microcomputer upgrade method, a control device and a storage medium to solve the problem in the prior art that the device cannot be used normally due to an inability to detect an upgrade program code error or anomaly.
[0005] The present invention discloses a single-chip microcomputer upgrade method, which is applied to a single-chip microcomputer device, wherein the single-chip microcomputer device is connected to a host computer for communication, and the memory of the single-chip microcomputer device includes a boot program area, a data storage area, a program running area and a program backup area; wherein the boot program area is used to run a boot program, and the program running area is used to run a target program, and includes a storage unit for storing a unique identification code; the data storage area is provided with an upgrade data flag bit;
[0006] The single chip computer upgrade method comprises the following steps:
[0007] When the boot program is running, the upgrade content of the upgrade data flag is read;
[0008] If the upgrade content is an upgrade mark indicating that an upgrade is required, obtaining the program upgrade code from the host computer and storing the program upgrade code in the program backup area;
[0009] Determining whether the code identifier in the program upgrade code matches the unique identification code;
[0010] If yes, copying the program upgrade code from the second backup area to the program running area, and modifying the upgrade content to a copy mark indicating that the program upgrade code copying is completed;
[0011] Running the target program, reading the upgrade content, and when the upgrade content is a copy mark, determining whether the program upgrade code can be run on the single-chip microcomputer device based on the unique identification code;
[0012] If not, a reset operation is performed and the boot program is run to perform a rollback operation.
[0013] Optionally, the number of the program backup areas is at least two, and the data storage area is provided with an upgrade copy flag;
[0014] After the step of reading the upgrade content of the upgrade data flag bit when the boot program is run, the method further comprises:
[0015] If the upgrade content is a copy mark indicating that the upgrade program copy is completed, a rollback operation is performed to obtain a first backup area storing a previous version of the program code in at least two of the program backup areas based on the content of the upgrade copy mark;
[0016] The previous version of the program code in the first backup area is copied to the program running area, and the upgrade content is modified to a normal mark indicating that the program is running normally.
[0017] Optionally, the step of storing the program upgrade code in the program backup area includes:
[0018] Based on the content of the upgrade copy flag, a second backup area for storing program upgrade codes in at least two of the program backup areas is obtained, and the program upgrade codes are stored in the second backup area.
[0019] Optionally, the step of determining whether the program upgrade code can be run on the single-chip microcomputer device based on the unique identification code includes:
[0020] If yes, modify the upgrade content to the normal flag, and continue to run the program upgrade code;
[0021] The first backup area is used as a new second backup area, and the original second backup area is used as a new first backup area.
[0022] Optionally, after the step of continuing to run the program upgrade code, the step further includes:
[0023] An upgrade instruction sent by a host computer is received, and the upgrade content is modified into the upgrade mark according to the upgrade instruction.
[0024] Optionally, after the step of continuing to run the program upgrade code, the step further includes:
[0025] An upgrade instruction sent by a host computer is received, and the upgrade content is modified into the upgrade mark according to the upgrade instruction.
[0026] Optionally, the step of obtaining the program upgrade code from the host computer further includes:
[0027] If the data interaction with the host computer fails or the acquisition of the program upgrade code times out, the upgrade content is modified to the normal mark; and / or
[0028] After the step of determining whether the code identifier in the program upgrade code matches the unique identification code, the method further comprises:
[0029] If not, the upgrade content is modified to the normal flag.
[0030] Optionally, the step of storing the program upgrade code in the program backup area includes:
[0031] Based on the content of the upgrade copy flag, a second backup area for storing program upgrade codes in at least two of the program backup areas is obtained, and the program upgrade codes are stored in the second backup area.
[0032] Optionally, when the boot program is run, the step of reading the upgrade content of the upgrade data flag bit includes:
[0033] When the upgrade content is a transparent transmission flag indicating that transparent transmission is required, receiving transparent transmission data sent by the host computer, and performing a transparent transmission operation according to the transparent transmission data;
[0034] After the transparent transmission operation is completed, the upgrade content is modified to the normal flag.
[0035] Optionally, when the boot program is run, the step of reading the upgrade content of the upgrade data flag bit includes:
[0036] When the upgrade content is a burning mark indicating that a program needs to be burned, the first backup area is selected from the at least two program backup areas, and the running code in the program running area is copied to the first backup area;
[0037] The first backup area is recorded in the upgrade copy flag, and the upgrade content is modified to the normal flag.
[0038] The present invention also discloses a computer-readable storage medium, which is characterized by storing a computer program. When the computer program is executed by a processor, the processor executes the steps of the method described above.
[0039] The present invention also discloses a control device, including a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the above method.
[0040] Compared with the prior art, the beneficial effect of the single-chip microcomputer upgrade method provided by the embodiment of the present invention is that: a unique identification code uniquely corresponding to the single-chip microcomputer device is pre-stored. During the upgrade process, for the upgrade program code, it is first determined whether the code identifier in the program upgrade code matches the unique identification code. If it matches, it indicates that the upgrade program code is suitable for the type of single-chip microcomputer device. When running the target program, it is determined based on the unique identification code whether the program upgrade code can run on the single-chip microcomputer device; if not, a reset operation is performed, and the boot program is run to perform a rollback operation. It can be detected based on the unique identification code whether the upgrade program code matches the single-chip microcomputer device and whether it can run on the single-chip microcomputer device. When the upgrade program code cannot run on the single-chip microcomputer device, a rollback operation can be performed to ensure the safe and stable operation of the single-chip microcomputer device. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] The scheme of the present invention will be further described in detail below with reference to the accompanying drawings and embodiments, in which:
[0042] Figure 1 It is a flow chart of an embodiment of a single chip computer upgrading method provided by the present invention;
[0043] Figure 2 It is a schematic diagram of the partitioning of the memory of the single-chip microcomputer device provided by the present invention;
[0044] Figure 3 It is a flow chart of a second embodiment of the single chip computer upgrading method provided by the present invention;
[0045] Figure 4 It is a flow chart of a third embodiment of the single chip computer upgrading method provided by the present invention;
[0046] Figure 5 1 is a flow chart of a fourth embodiment of a single chip microcomputer upgrading method provided by the present invention;
[0047] Figure 6 is a flow chart of a fifth embodiment of a single chip computer upgrading method provided by the present invention;
[0048] Figure 7 is a flow chart of a fifth embodiment of a single chip computer upgrading method provided by the present invention;
[0049] Figure 8 It is a flow chart of the sixth embodiment of the single chip microcomputer upgrading method provided by the present invention. DETAILED DESCRIPTION
[0050] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present application can be combined with each other. Now, in conjunction with the accompanying drawings, a preferred embodiment of the present invention is described in detail.
[0051] Please refer to Figure 1 and Figure 2 , Figure 1 is a flow chart of an embodiment of a single chip microcomputer upgrading method provided by the present invention, Figure 2 The present invention provides a schematic diagram of the partitioning of the memory of the single-chip microcomputer device.
[0052] like Figure 2 As shown, the memory of the single-chip microcomputer device includes a boot program area (boot program area), a data storage area, a program running area and a program backup area. Among them, the boot program area is used to run the boot program, and the program running area is used to run the target program, and the program code of the target program is stored. The program backup area is used to store the program upgrade code received from the host computer, that is, the program upgrade code is first stored in the program backup area, and after passing the verification, it will be copied to the program running area to realize the upgrade of the target program. The program running area includes a storage unit for storing a unique identification code, and the unique identification code is used to determine whether the program upgrade code matches the single-chip microcomputer device and whether it can run normally on the single-chip microcomputer device.
[0053] The boot program is the first program to run when the system starts. Its main task is to initialize the system, check the system status, and then decide whether to start the application or perform other operations. When the microcontroller starts, the boot program will be executed first. Once the boot program completes its task, it will pass control to the program running area, where the program code of the target program is stored, thereby realizing the operation of the target program.
[0054] The boot program is not only responsible for the boot and initialization of the system, but also for the upgrade of the target program. This means that the boot program can detect and process upgrade requests from the outside, such as receiving new firmware through the serial port or network, and writing it to the memory of the microcontroller, thereby realizing the remote upgrade of the target program.
[0055] In one implementation scenario, the number of program backup areas is at least two (e.g., Figure 2In the figure, there are program backup area A and program backup area B), at least two program backup areas are used to store the program upgrade code of the target program in turn, and after the program upgrade code is verified, it can be copied to the program running area to run the program upgrade code to realize the remote upgrade of the target program. By storing the program upgrade code of the target program in turn in at least two program backup areas, it can be ensured that at least one program backup area stores the previous version of the program code, and at least one program backup area can store the program upgrade code. In this way, once a problem occurs in the program upgrade code (for example, it does not match the current single-chip microcomputer device), the previous version of the program code stored in the program backup area can be rolled back to the previous stable version to ensure the normal operation of the single-chip microcomputer device.
[0056] In order to distinguish which program backup area stores the previous version of program code, an upgrade copy flag is set in the data storage area, which can indicate the program backup area currently used to store program upgrade code and / or the previous version of program code.
[0057] In this embodiment, the data storage area also sets an upgrade data flag, and the content in the upgrade data flag will change according to the specific situation of the current target program upgrade. When running the target program, the upgrade data flag will be read first, and based on the upgrade content of the upgrade data flag, it will be determined whether the target program can be run smoothly or whether a rollback operation can be chosen.
[0058] In this embodiment, the single-chip microcomputer device is communicatively connected with the host computer, so that it can receive the program upgrade code provided by the host computer for upgrading, and receive the instruction sent by the host computer, and perform corresponding operations according to the instruction.
[0059] The single chip computer upgrading method provided by the present invention comprises the following steps:
[0060] S101: When the boot program is running, the upgrade content of the upgrade data flag is read.
[0061] In a specific implementation scenario, the boot program is first run, and the boot program reads the upgrade content of the upgrade data flag, and performs subsequent operations according to the upgrade content. The upgrade content represents the upgrade status of the current target program. For example, if the code in the current program running area can run normally, the upgrade content is a normal flag. For another example, if an upgrade instruction is currently received from the host computer, the upgrade content is an upgrade flag that needs to be upgraded. The upgrade content can be specific information content or a simple identifier, such as "01" to indicate that an upgrade is required and "00" to indicate that the program is running normally.
[0062] S102: If the upgrade content is an upgrade mark indicating that an upgrade is required, a program upgrade code is obtained from the host computer, and the program upgrade code is stored in a program backup area.
[0063] In a specific implementation scenario, when the target program in the single-chip microcomputer needs to be upgraded, the user operates the single-chip microcomputer device through the host computer. After the user inputs the upgrade instruction in the host computer, the host computer sends the upgrade instruction to the single-chip microcomputer device, and the single-chip microcomputer device modifies the upgrade data flag bit to the upgrade flag that needs to be upgraded according to the received upgrade instruction. When the upgrade content is detected as the upgrade flag, it is necessary to receive the program upgrade code transmitted by the host computer, and store the program upgrade code in a program backup area.
[0064] In other implementation scenarios, there are at least two program backup areas in the single-chip microcomputer device, and it is necessary to select a second backup area to store the program upgrade code. Read the upgrade copy flag bit, and obtain the second backup area according to the upgrade copy flag bit, for example, Figure 2 In the scenario shown, there are program backup area A and program backup area B. The program in the current program running area is copied from program backup area A, so program backup area B is the second backup area for storing program upgrade code. Correspondingly, program backup area A can be used as the first backup area. If the program upgrade code is verified and can run normally, the program in program backup area A can no longer be retained, and the second backup area is modified to the first backup area. The corresponding first backup area will be modified to the second backup area. In this implementation scenario, that is, program backup area A becomes the second backup area, and program backup area B becomes the first backup area. Then, when an upgrade is required next time, the program upgrade code will be saved in program backup area A. In this way, if the new program upgrade code stored in program backup area A fails and needs to be rolled back, the program code in program backup area B is the code that can run normally, and the program code in program backup area B can be used.
[0065] If the program upgrade code fails to pass the verification, for example, the program upgrade code is not suitable for the single-chip device, or the program upgrade code is wrong, the current upgrade fails, and the program backup area B is still the second backup area during the next upgrade. In this way, if the new program upgrade code stored in the program backup area B fails and needs to be rolled back, the program code in the program backup area A is the code that can run normally and can be used.
[0066] Through this solution, one of the two program backup areas can be used to store the previous version of the program code, and the other can be used to receive the program upgrade code at different stages. In this way, during the program upgrade process, there is no need to frequently copy the upgrade program code or the previous version of the program code to other places, reducing the number of data copies to the Flash. The backup and upgrade process of the target program can be effectively managed, reducing the problems caused by abnormal data copying and improving the efficiency of the upgrade process. At the same time, by exchanging the program backup areas, it is ensured that the previous reliable program version can be quickly rolled back during the upgrade process, enhancing the stability and reliability of the system.
[0067] In this implementation scenario, a second backup area is selected from at least two program backup areas according to the upgrade copy flag. The upgrade copy flag may indicate the second backup area currently used to store the program upgrade code, or may support the first backup area storing the currently normally running program code (compared to the previous version of the program upgrade code), or may indicate both the second backup area and the first backup area. After the program upgrade code is obtained from the host computer, the program upgrade code is stored in the selected second backup area.
[0068] In other implementation scenarios, there may be multiple program backup areas, such as program backup area A, program backup area B and program backup area C. In this case, the upgrade copy mark needs to indicate both the second backup area and the first backup area. Specifically, the program that can currently run normally is stored in program backup area A. When the target program needs to be upgraded, either program backup area B or program backup area C can be used as the second backup area, and the program upgrade code is stored in the second backup area (for example, program backup area B). Program backup area A is the first backup area. If the program upgrade code can run normally, it is copied from the second backup area to the program running area, and program backup area B is used as the first backup area, and one of program backup area A and program backup area C is selected as the second backup area. For example, program backup area C is selected as the second backup area. Then, during the next upgrade, the program upgrade code is stored in program backup area C. If the program upgrade code cannot run normally, the previous version of the program code is read from the first backup area, that is, program backup area B, and copied to the program running area to implement rollback, thereby ensuring the normal and stable operation of the single-chip microcomputer device.
[0069] Based on the above description, it can be seen that when there are multiple program backup areas, you only need to upgrade the copy mark to point to both the second backup area and the first backup area to successfully complete the program backup. When a rollback operation is required, you can quickly and correctly obtain the previous version of the program code, avoiding excessive copying operations of the program code and improving work efficiency.
[0070] S103: Determine whether the code identifier in the program upgrade code matches the unique identification code; if so, execute step S104.
[0071] In a specific implementation scenario, a storage unit is provided in the program running area, and the storage unit is used to store a unique identification code, which can be a specific numerical value, a string, or other forms of data. The code identification in the program upgrade code is obtained, and it is determined whether the code identification in the program upgrade code matches the unique identification code. If the two match, it means that the program upgrade code matches the type of the single-chip device, and subsequent detection can be performed. If they do not match, it means that the program upgrade code cannot be used to upgrade the single-chip device, and the upgrade process is terminated.
[0072] In this way, the BOOT program can directly determine the correctness of the program upgrade code from the bottom layer of the MCU without relying on the host computer or manual operation. It is simple and efficient, improves the security and stability of the system, and ensures that there will be no misoperation or introduction of wrong firmware versions during the upgrade process, thereby ensuring the normal operation of the system.
[0073] S104: copying the program upgrade code from the program backup area to the program running area, and modifying the upgrade content to a copy mark indicating that the program upgrade code copying is completed.
[0074] In a specific implementation scenario, if the code identifier in the program upgrade code matches the unique identifier, the program upgrade code can be copied from the second backup area to the program running area, overwriting the original application code, and the boot program can monitor the copying process to ensure that the program upgrade code is correctly written to the program running area. Once the copying is completed, the boot program can start the new program code in the program running area, so that the new program code can be run.
[0075] In this scenario, after the copying is completed, the upgrade content is modified to a copy mark indicating that the copying of the program upgrade code is completed, thereby recording the current upgrade step.
[0076] S105: Run the target program and read the upgrade content. When the upgrade content is a copy mark, determine whether the program upgrade code can be run on the single-chip microcomputer device based on the unique identification code; if not, execute step S106.
[0077] In a specific implementation scenario, after the copy is completed, the target program is run to run the new program code in the program running area. When the target program starts running, the upgrade content of the upgrade data flag is read first. When the upgrade content is a copy flag, it needs to be further processed in combination with the internal logic of the program. Read the unique identification code, and based on the unique identification code, determine whether the new program code (that is, the program upgrade code) in the current program running area can run in the hardware of the current single-chip microcomputer device. This mainly solves the problem of mutual upgrades of programs of different models during production on the production line, ensuring that hardware of different models can only run corresponding program versions.
[0078] S106: Perform a reset operation and run the boot program to perform a rollback operation.
[0079] In a specific implementation scenario, if the program upgrade code cannot run on the MCU device, a reset operation is performed to re-enter the boot program, and the boot program performs a rollback operation. In this way, when the upgrade program code does not match the MCU device, a rollback operation can be performed to ensure that the MCU device runs smoothly.
[0080] Specifically, after re-entering the boot program, the upgrade content of the upgrade data flag is read. If the upgrade content is a copy flag indicating that the program upgrade code copy is complete, it means that the program code copy was completed in the last upgrade but failed to run, and a rollback operation is required. Based on the content of the upgrade copy flag, the first backup area storing the previous version of the program code in at least two program backup areas is obtained, and the previous version of the program code in the first backup area is copied to the program running area. Since the previous version of the program code has been verified to be able to run normally, the upgrade content can be modified to a normal flag indicating that the program is running normally. For details, please refer to Figure 3 .
[0081] From the above description, it can be seen that in this embodiment, a unique identification code uniquely corresponding to the single-chip microcomputer device is pre-stored. During the upgrade process, for the upgrade program code, it is first determined whether the code identifier in the program upgrade code matches the unique identification code. If it matches, it indicates that the upgrade program code is suitable for the type of single-chip microcomputer device. When running the target program, it is determined whether the program upgrade code can run on the single-chip microcomputer device based on the unique identification code; if not, a reset operation is performed and the boot program is run to perform a rollback operation. It can be detected based on the unique identification code whether the upgrade program code matches the single-chip microcomputer device and whether it can run on the single-chip microcomputer device. When the upgrade program code cannot run on the single-chip microcomputer device, a rollback operation can be performed to ensure the safe and stable operation of the single-chip microcomputer device.
[0082] Please refer to Figure 3 , Figure 31 is a flow chart of a second embodiment of a single chip microcomputer upgrade method provided by the present invention. The single chip microcomputer upgrade method provided by the present invention comprises the following steps:
[0083] S201: When the boot program is running, the upgrade content of the upgrade data flag is read.
[0084] In a specific implementation scenario, step S201 is substantially the same as step S101 in the first embodiment of the single chip microcomputer upgrade method provided by the present invention, and will not be described in detail herein.
[0085] In this implementation scenario, the boot program can be the first boot program to be entered after the microcontroller is powered on and starts working, or it can be step S106 in the first embodiment of the microcontroller upgrade method provided by the invention, in which it is determined based on the unique identification code that the program upgrade code cannot be run on the microcontroller device, and a reset operation is performed to run the boot program.
[0086] S202: If the upgrade content is a copy mark indicating that the upgrade program copy is completed, a rollback operation is performed to obtain a first backup area storing the previous version of the program code in at least two program backup areas based on the content of the upgrade copy mark.
[0087] In a specific implementation scenario, the upgrade content is a copy mark indicating that the upgrade program copy is complete, indicating that the program upgrade code has been copied to the program running area, but the running fails or cannot be run. At this time, a rollback operation needs to be performed to overwrite the current program upgrade code that cannot be run with the previous version of the program code before the upgrade, so that the microcontroller device can run normally.
[0088] It is necessary to obtain the first backup area where the previous version of the program code is located. In this embodiment, it is possible to obtain which of the at least two program backup areas is the first backup area according to the content of the upgrade copy flag. The specific process of how to select and identify the at least two program backup areas has been described above and will not be repeated here.
[0089] S203: Copy the previous version of the program code in the first backup area to the program running area, and modify the upgraded content to a normal mark indicating that the program is running normally.
[0090] In a specific implementation scenario, the previous version of the program code in the first backup area is copied to the program running area. Since the previous version of the program code can run normally, when the code in the program running area is the previous version of the program code, the target program can run smoothly. Therefore, there is no need to verify the previous version of the program code, and the upgrade content can be modified to a normal sign indicating that the program is running normally. At this time, the running code in the program running area is the previous version of the program code, and the rollback operation is successfully implemented, ensuring that the single-chip microcomputer device runs successfully and stably.
[0091] When the copying is completed, the target program can be run to obtain the upgrade content of the upgrade data flag. If the upgrade content is a normal flag, the target program can continue to execute.
[0092] When the MCU device restarts, after entering the boot program, it obtains the upgrade content of the upgrade data flag. If the upgrade content is a normal flag, it enters the target program. The target program obtains the upgrade content of the upgrade data flag. If the upgrade content is a normal flag, the target program can continue to execute.
[0093] From the above description, it can be seen that in this embodiment, the number of program backup areas is at least two, and the content of the upgrade copy flag is used to indicate the second backup area and the first backup area in the program backup area. The first backup area stores the previous version of the program code. When a rollback operation is required, the previous version of the program code can be quickly and correctly obtained according to the upgrade copy flag, thereby quickly implementing the rollback operation and ensuring the safe and stable operation of the single-chip microcomputer device.
[0094] Please refer to Figure 4 , Figure 4 1 is a flow chart of a third embodiment of a single chip microcomputer upgrade method provided by the present invention. The single chip microcomputer upgrade method provided by the present invention comprises the following steps:
[0095] S301: When the boot program is running, the upgrade content of the upgrade data flag is read.
[0096] In a specific implementation scenario, step S301 is substantially the same as step S101 in the first embodiment of the single chip microcomputer upgrade method provided by the present invention, and will not be described in detail here.
[0097] S302: If the upgrade content is an upgrade flag indicating that an upgrade is required, a second backup area for storing program upgrade codes in at least two program backup areas is obtained based on the content of the upgrade copy flag.
[0098] In a specific implementation scenario, the single-chip microcomputer device modifies the upgrade data flag to an upgrade flag that needs to be upgraded according to the received upgrade instruction. When the upgrade content is detected to be an upgrade flag, it is necessary to receive the program upgrade code transmitted by the host computer and store the program upgrade code in the second backup area.
[0099] Read the upgrade copy flag, obtain the second backup area according to the upgrade copy flag, use the program backup area that does not store the previous version of the program code compared to the current program version that needs to be upgraded as the second backup area, and use the program backup area that stores the previous version of the program code as the first backup area. In this way, the upgrade program code will be stored in the second backup area. If the upgrade program code does not match the single-chip microcomputer device, you can directly perform a rollback operation, obtain the previous version of the program code from the first backup area, and overwrite it to the program running area to ensure that the code in the program running area can run normally, so that the single-chip microcomputer device can run normally and stably.
[0100] S303: Obtain the program upgrade code from the host computer. If the data interaction with the host computer fails or the acquisition of the program upgrade code times out, the upgrade content is modified to a normal flag.
[0101] In a specific implementation scenario, it is determined whether there is a failure in data interaction with the host computer or a timeout in obtaining the program upgrade code. The single-chip microcomputer device can detect whether there is a communication failure or a data interaction timeout based on its own timer. For example, if the data interaction with the host computer is not completed after the set time, it is considered an interaction failure. If the complete program upgrade code is not obtained after the set time, it is considered a timeout. For another example, when the data sent by the host computer fails to be checked by CRC (Cyclic Redundancy Check) and other types of checks on the single-chip microcomputer device side, this indicates that the received data may have been tampered with or damaged, and it is considered that the data interaction with the host computer has failed. For another example, if the length of the data sent by the host computer exceeds the maximum length that the device can handle, it may cause data transmission failure or data parsing error, which is considered a failure in data interaction with the host computer.
[0102] When an interaction failure or timeout occurs, the upgrade operation is considered to have failed. Since the program upgrade code cannot be successfully obtained, there is no code in the second backup area that can be copied to the program running area. The code in the program running area is the previous version of the code and can run normally. Therefore, the upgrade content is modified to a normal sign.
[0103] When the MCU device restarts, after entering the boot program, it obtains the upgrade content of the upgrade data flag. If the upgrade content is a normal flag, it enters the target program. The target program obtains the upgrade content of the upgrade data flag. If the upgrade content is a normal flag, the target program can continue to execute with the previous version of the code.
[0104] S304: If the program upgrade code is successfully obtained, the program upgrade code is stored in the second backup area.
[0105] S305: Determine whether the code identifier in the program upgrade code matches the unique identifier; if so, execute step S306. If not, execute step S309.
[0106] S306: Copy the program upgrade code from the second backup area to the program running area, and modify the upgrade content to a copy mark indicating that the program upgrade code copying is completed.
[0107] S307: Run the target program and read the upgrade content. When the upgrade content is a copy mark, determine whether the program upgrade code can be run on the single-chip microcomputer device based on the unique identification code; if so, execute step S308.
[0108] In a specific implementation scenario, steps S304-S307 are substantially consistent with steps S102-S105 in the first embodiment of the single chip microcomputer upgrade method provided by the present invention, and will not be described in detail here.
[0109] S308: Modify the upgrade content to a normal flag, and continue to run the program upgrade code, use the first backup area as the second backup area, and use the second backup area as the first backup area.
[0110] In a specific implementation scenario, it is determined based on the unique identification code that the program upgrade code can run on the single-chip microcomputer device, so the program upgrade code can continue to run, and the upgrade content is modified to a normal flag, thus completing the upgrade of the target program.
[0111] Since it has been confirmed that the current program upgrade code can run normally, the first backup area is used as the second backup area, and the second backup area is used as the first backup area. During the next upgrade, the new program upgrade code will overwrite the previous version of the program code, but will not overwrite the current program upgrade code. If the new program upgrade code cannot run normally, the current program upgrade code can be obtained from the first backup area for rollback to ensure that the single-chip microcomputer device can run normally.
[0112] When the MCU device restarts, after entering the boot program, it obtains the upgrade content of the upgrade data flag. If the upgrade content is a normal flag, it enters the target program. The target program obtains the upgrade content of the upgrade data flag. If the upgrade content is a normal flag, the target program can continue to execute.
[0113] S309: Modify the upgrade content to a normal flag.
[0114] In a specific implementation scenario, if the code identifier in the program upgrade code does not match the unique identifier, the program upgrade code cannot be run, so the program upgrade code is not copied to the program running area, and the program running area is still the previous version of the code that can run normally, so the upgrade content can be modified to a normal flag. Enter the target program, the target program obtains the upgrade content of the upgrade data flag, the upgrade content is a normal flag, and the target program can continue to execute.
[0115] When the MCU device restarts, after entering the boot program, it obtains the upgrade content of the upgrade data flag. If the upgrade content is a normal flag, it enters the target program. The target program obtains the upgrade content of the upgrade data flag. If the upgrade content is a normal flag, the target program can continue to execute.
[0116] From the above description, it can be seen that in this embodiment, if the code identifier in the program upgrade code does not match the unique identification code, the program upgrade code cannot run, and the upgrade content is modified to a normal flag. In this way, the target program is entered, and the target program obtains the upgrade content of the upgrade data flag. The upgrade content is a normal flag, and the target program can continue to execute, thereby achieving stable operation of the single-chip microcomputer device.
[0117] Please refer to Figure 5 , Figure 5 1 is a flow chart of a fourth embodiment of a single chip microcomputer upgrade method provided by the present invention. The single chip microcomputer upgrade method provided by the present invention comprises the following steps:
[0118] S401: When the boot program is running, the upgrade content of the upgrade data flag is read.
[0119] In a specific implementation scenario, step S401 is substantially consistent with step S101 in the first embodiment of the single chip microcomputer upgrade method provided by the present invention, and will not be described in detail here.
[0120] S402: When the upgrade content is a transparent transmission flag indicating that transparent transmission is required, transparent transmission data sent by the host computer is received, and a transparent transmission operation is performed according to the transparent transmission data.
[0121] In a specific implementation scenario, the transparent data sent by the host computer can be received through serial communication, network communication or other appropriate communication methods. After receiving the transparent data, it should be processed accordingly according to the format and content of the data, such as parsing the data packet, verifying the integrity of the data, and performing specific transparent operations. According to the content and requirements of the transparent data, corresponding transparent operations can be performed, such as controlling external devices, updating status information, and performing specific functions. After completing the transparent operation, the microcontroller device can send a confirmation message or status update to the host computer to ensure communication and data synchronization between the two parties.
[0122] S403: After the transparent transmission operation is completed, the upgrade content is modified to a normal flag.
[0123] In a specific implementation scenario, after the transparent transmission operation is completed, the upgrade content is modified to a normal flag. When the transparent transmission operation is completed, the target program is entered, and the target program obtains the upgrade content of the upgrade data flag. If the upgrade content is a normal flag, the target program can continue to execute.
[0124] When the MCU device is started again, after entering the boot program, the upgrade content of the upgrade data flag is obtained. If the upgrade content is a normal flag, the target program is entered. The target program obtains the upgrade content of the upgrade data flag. If the upgrade content is a normal flag, the target program can continue to execute.
[0125] From the above description, it can be seen that in this embodiment, when the upgrade content is a transparent transmission flag indicating that transparent transmission is required, the transparent transmission data sent by the upper computer is received, and the transparent transmission operation is performed according to the transparent transmission data. After the transparent transmission operation is completed, the upgrade content is modified to a normal flag, so that the target program can continue to run normally after the transparent transmission task is completed.
[0126] Please refer to Figure 6 , Figure 6 1 is a flow chart of a fifth embodiment of a single chip microcomputer upgrade method provided by the present invention. The single chip microcomputer upgrade method provided by the present invention comprises the following steps:
[0127] S501: When the boot program is running, the upgrade content of the upgrade data flag is read.
[0128] In a specific implementation scenario, step S501 is substantially consistent with step S101 in the first embodiment of the single chip microcomputer upgrade method provided by the present invention, and will not be described in detail here.
[0129] S502: When the upgrade content is a burning mark indicating that a program needs to be burned, a first backup area is selected from at least two program backup areas, and the running code in the program running area is copied to the first backup area.
[0130] In a specific implementation scenario, when the production line burns the program to the single-chip microcomputer through the program burner, the running code of the target program is usually burned directly into the program running area, so that the running code can be directly run in the program running area. At this time, at least two program backup areas are not used. In order to ensure the stability and reliability of the system, one program backup area is selected from at least two program backup areas as the first backup area. Once the program burning in the program running area is completed and verified to be correct, the same code content needs to be copied to the first backup area. In this way, when a problem occurs in the program running area, the running code copied in the first backup area can be quickly restored to the initial version to ensure the normal operation of the single-chip microcomputer device.
[0131] When the single-chip microcomputer device is being burned with a program, the upgrade content is set to the burn mark of the program to be burned. When the upgrade content is the burn mark, it is necessary to select a program backup area from at least two program backup areas as the first backup area for storing the running code to be copied. The running code in the program running area is read and the running code in the program running area is copied to the selected first backup area.
[0132] The selection of the first backup area can be any selection, or can be selected according to a preset sorting, which is not limited here. In this implementation scenario, the running code is running in the program running area, so it can be determined that the running code can run smoothly, so that the running code is copied to the first backup area. In the case of only two program backup areas, the other program backup area is automatically used as the second backup area, or in the case of multiple program backup areas, the second backup area can be specified, so that when the program is upgraded next time, the program upgrade code can be directly stored in the second backup area. If the program upgrade code cannot run normally, the rollback operation can be performed through the running code in the first backup area to ensure the normal and stable operation of the single-chip microcomputer device.
[0133] S503: Record the first backup area in the upgrade copy flag, and modify the upgrade content to a normal flag.
[0134] In a specific implementation scenario, the second backup area is recorded in the upgrade copy flag, for example Figure 2 In the scenario shown, program backup area A is selected as the first backup area, and the first backup area is recorded in the upgrade copy mark area as program backup area A. Since the program running area is already running normally, the upgrade content is modified to the normal mark, and the target program continues to run normally.
[0135] From the above description, it can be seen that in this embodiment, when the upgrade content is a burning mark that requires burning the program, a first backup area is selected from at least two program backup areas, and the running code in the program running area is copied to the first backup area, so that the burned running code can be backed up, which is convenient for quick rollback operation when necessary later.
[0136] Please refer to Figure 7 and Figure 8 , Figure 7 It is a flow chart of the sixth embodiment of the single chip microcomputer upgrading method provided by the present invention. Figure 8 1 is a flow chart of the seventh embodiment of the single chip microcomputer upgrade method provided by the present invention. The single chip microcomputer upgrade method provided by the present invention comprises the following steps:
[0137] S601: When the boot program is running, the upgrade content of the upgrade data flag is read.
[0138] S602: If the upgrade content is a copy mark indicating that the upgrade program copy is completed, a rollback operation is performed.
[0139] S603: Obtaining a first backup area storing a previous version of program code in at least two program backup areas based on the content of the upgrade copy flag.
[0140] S604: Copy the previous version of the program code in the first backup area to the program running area.
[0141] S605: The upgrade content is modified to a normal sign indicating that the program is running normally.
[0142] S606: When the upgrade content is a burning mark indicating that a program needs to be burned, a first backup area is selected from at least two program backup areas, and the running code in the program running area is copied to the first backup area.
[0143] S607: Execute step S605 after recording the first backup area in the upgrade copy flag.
[0144] S608: When the upgrade content is a transparent transmission flag indicating that transparent transmission is required, transparent transmission data sent by the host computer is received, and a transparent transmission operation is performed according to the transparent transmission data.
[0145] S609: After the transparent transmission operation is completed, execute step S605.
[0146] S610: If the upgrade content is an upgrade flag indicating that an upgrade is required, a second backup area for storing program upgrade codes in at least two program backup areas is obtained based on the content of the upgrade copy flag.
[0147] S611: Obtain the program upgrade code from the host computer.
[0148] S612: If the data interaction with the host computer fails or the acquisition of the program upgrade code times out, execute step S605.
[0149] S613: If the program upgrade code is successfully obtained, the program upgrade code is stored in the second backup area.
[0150] S614: Determine whether the code identifier in the program upgrade code matches the unique identifier; if so, execute step S615. If not, execute step S605.
[0151] S615: Copy the program upgrade code from the second backup area to the program running area, and modify the upgrade content to a copy mark indicating that the program upgrade code copying is completed.
[0152] S616: The upgraded content is a normal sign indicating that the program is running normally.
[0153] S617: Run the target program
[0154] Steps S601-S617 in this embodiment have been described in the first embodiment to the fifth embodiment provided by the present invention, and will not be repeated here.
[0155] The single chip computer upgrade method provided by the present invention also includes the following steps:
[0156] S701: Run the target program and read the upgrade content.
[0157] S702: The upgrade content is marked as a copy.
[0158] S703: Determine whether the program upgrade code can be run on the single-chip microcomputer device based on the unique identification code. If yes, execute step S704, if not, execute step S706.
[0159] S704: Modify the upgrade content to a normal flag, use the first backup area as the second backup area, and use the second backup area as the first backup area.
[0160] S705: Continue to run the program upgrade code.
[0161] S706: Perform a reset operation and run the boot program.
[0162] S707: The upgrade content is normal.
[0163] S708: receiving the upgrade instruction sent by the host computer, modifying the upgrade content into the upgrade mark, and then executing step S706.
[0164] Steps S701 to S708 in this embodiment have been described in the first embodiment to the fifth embodiment provided by the present invention, and will not be described again here.
[0165] The present invention also provides a power supply device. The power supply device includes a processor and a memory. The processor is coupled to the memory. A computer program is stored in the memory, and the processor executes the computer program when working to implement the above method. The detailed steps can be found above and will not be repeated here.
[0166] The present invention also provides a computer-readable storage medium in which at least one computer program is stored, and the computer program is used to be executed by a processor to implement the above method. The detailed steps can be referred to above and will not be repeated here. In one embodiment, the computer-readable storage medium can be a storage chip, a hard disk, or a mobile hard disk or a USB flash drive, an optical disk, or other readable and writable storage tools in a terminal, or a server, etc.
[0167] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the program can be stored in a non-volatile computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
[0168] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0169] It should be understood that the above embodiments are only used to illustrate the technical solutions of the present invention rather than to limit it. For those skilled in the art, the technical solutions described in the above embodiments can be modified, or some of the technical features therein can be replaced by equivalents; and all these modifications and replacements should fall within the scope of protection of the claims attached to the present invention.
Claims
1. A method for upgrading a single chip microcomputer, characterized in that: Applied to a single-chip microcomputer device, the single-chip microcomputer device is communicatively connected with a host computer, and the memory of the single-chip microcomputer device includes a boot program area, a data storage area, a program running area and a program backup area; wherein the boot program area is used to run the boot program, and the program running area is used to run the target program, including a storage unit for storing a unique identification code; the data storage area is provided with an upgrade data flag; The single chip computer upgrade method comprises the following steps: When the boot program is running, the upgrade content of the upgrade data flag is read; If the upgrade content is an upgrade mark indicating that an upgrade is required, obtaining the program upgrade code from the host computer and storing the program upgrade code in the program backup area; Determining whether the code identifier in the program upgrade code matches the unique identification code; If yes, copying the program upgrade code from the program backup area to the program running area, and modifying the upgrade content to a copy mark indicating that the program upgrade code copying is completed; Running the target program, reading the upgrade content, and when the upgrade content is a copy mark, determining whether the program upgrade code can be run on the single-chip microcomputer device based on the unique identification code; If not, a reset operation is performed and the boot program is run to perform a rollback operation.
2. The single chip computer upgrade method according to claim 1, characterized in that: The number of the program backup areas is at least two, and the data storage area is provided with an upgrade copy flag; After the step of reading the upgrade content of the upgrade data flag bit when the boot program is run, the method further comprises: If the upgrade content is a copy mark indicating that the upgrade program copy is completed, a rollback operation is performed to obtain a first backup area storing a previous version of the program code in at least two of the program backup areas based on the content of the upgrade copy mark; The previous version of the program code in the first backup area is copied to the program running area, and the upgrade content is modified to a normal mark indicating that the program is running normally.
3. The single chip computer upgrade method according to claim 2, characterized in that: The step of storing the program upgrade code in the program backup area comprises: Based on the content of the upgrade copy flag, a second backup area for storing program upgrade codes in at least two of the program backup areas is obtained, and the program upgrade codes are stored in the second backup area.
4. The single chip computer upgrade method according to claim 3, characterized in that: The step of judging whether the program upgrade code can be run on the single-chip microcomputer device based on the unique identification code comprises: If yes, modify the upgrade content to the normal flag, and continue to run the program upgrade code; The first backup area is used as a new second backup area, and the original second backup area is used as a new first backup area.
5. The single chip computer upgrade method according to claim 2, characterized in that: After the step of continuing to run the program upgrade code, the method further comprises: An upgrade instruction sent by a host computer is received, and the upgrade content is modified into the upgrade mark according to the upgrade instruction.
6. The single chip computer upgrade method according to claim 2, characterized in that: The step of obtaining the program upgrade code from the host computer also includes: If the data interaction with the host computer fails or the acquisition of the program upgrade code times out, the upgrade content is modified to the normal mark; and / or After the step of determining whether the code identifier in the program upgrade code matches the unique identification code, the method further comprises: If not, the upgrade content is modified to the normal flag.
7. The single chip computer upgrade method according to any one of claims 2 to 6, characterized in that: When the boot program is running, the step of reading the upgrade content of the upgrade data flag bit includes: When the upgrade content is a transparent transmission flag indicating that transparent transmission is required, receiving transparent transmission data sent by the host computer, and performing a transparent transmission operation according to the transparent transmission data; After the transparent transmission operation is completed, the upgrade content is modified to the normal flag.
8. The single chip computer upgrade method according to any one of claims 2 to 6, characterized in that: When the boot program is running, the step of reading the upgrade content of the upgrade data flag bit includes: When the upgrade content is a burning mark indicating that a program needs to be burned, the first backup area is selected from the at least two program backup areas, and the running code in the program running area is copied to the first backup area; The first backup area is recorded in the upgrade copy flag, and the upgrade content is modified to the normal flag.
9. A computer-readable storage medium, characterized in that: A computer program is stored, and when the computer program is executed by a processor, the processor is caused to perform the steps of the method according to any one of claims 1 to 8.
10. A control device, characterized in that: The method comprises a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor executes the steps of the method according to any one of claims 1 to 8.