[ summary of the invention ]
The technical problems to be solved by the invention are as follows:
in the traditional upgrading mode, FLASH of a single chip microcomputer is divided into an application program area, an upgrading backup area and an upgrading guide area, the upgrading guide area occupies more FLASH, and standby programs need to be completely moved to the application program area, so that more copied contents exist, and time waste is caused.
The invention achieves the above purpose by the following technical scheme:
in a first aspect, the present invention provides a method for upgrading a monolithic processor online, which divides a FLASH of the monolithic processor into an interrupt vector area, an application a area, and an application B area, wherein the interrupt vector area is a common area of the application a area and the application B area, and the method includes:
the single chip microcomputer initializes an upgrading variable after receiving an upgrading starting command and replies a currently running application program area to the upper computer upgrading software so that the upper computer upgrading software can determine to generate an upgrading Bin file needing to be transmitted according to the currently running area of the single chip microcomputer;
the single chip microcomputer receives an upgrade Bin file transmitted by upgrade software of an upper computer, and stores the upgrade Bin file in an application program area which is not operated by the single chip microcomputer FLASH currently;
and the single chip microcomputer closes the interruption after receiving the upgrading skip command, copies the interruption vector area of the upgrading program to the interruption vector area of the single chip microcomputer, and restarts the single chip microcomputer software and runs a new program to complete upgrading.
Preferably, the upper computer upgrading software determines the upgrade Bin file to be transmitted according to the current operation area of the single chip microcomputer, and specifically includes: according to the currently running application program area replied by the single chip microcomputer, the upper computer upgrading software judges whether the upgrading program is in the same area as the currently running application program area of the single chip microcomputer, and if the upgrading program is in the different area, the current upgrading Bin file is directly transmitted to the single chip microcomputer; if the two-zone upgrading file is the same zone, the upper computer upgrading software converts the current upgrading Bin file into another zone upgrading Bin file and then transmits the converted upgrading Bin file to the single chip microcomputer.
Preferably, the upper computer upgrade software converts the current upgrade Bin file into another region upgrade Bin file, specifically:
calculating an address offset according to the converted region;
reading a byte of the current upgrade Bin file, obtaining the number N of bytes contained in the corresponding instruction through table lookup, and analyzing whether the instruction is related to the address or not;
if the instruction is related to the address, adding or subtracting an address offset to the address corresponding to the instruction to generate a new address and replace the original address byte; if the instruction is not related to the address, the N bytes are directly copied to a new Bin file.
Preferably, the method includes the steps that the single chip microcomputer receives an upgrade Bin file transmitted by upgrade software of an upper computer, and stores the upgrade Bin file in an application program area which is not currently operated by the single chip microcomputer FLASH, and specifically includes:
the single chip microcomputer receives a data packet of an upgrading Bin file transmitted by upgrading software of an upper computer, calculates and judges whether a check value of the data packet is correct or not, and if the check value is correct, writes the data packet into an application program area which is not operated at present in the single chip microcomputer FLASH; if the first Bin file data packet is upgraded, judging whether the area of the upgrading program is correct, and calculating and judging whether the check value of the data packet is correct on the premise of correctness.
Preferably, before the single chip receives an upgrade start command, the method further includes: and importing the Hex file generated by the compiler into upper computer upgrading software, and converting the Hex file into a Bin file required by upgrading by the upper computer upgrading software.
Preferably, the upgrade Bin file consists of an interrupt vector area and a program code area, and a file information area is arranged in the interrupt vector area of the upgrade Bin file and used for storing the file length and the check value of the upgrade Bin file; and a file identification area is arranged in the program code area of the upgrade Bin file and is used for judging whether the upgrade Bin file is a required upgrade file.
Preferably, for the application program a area and the application program B area, the file identification area of the upgrade Bin file is located at a fixed address, and specifically: for the upgrade Bin files of the two application program areas, the address difference value of the file identification area is consistent with the address offset between the two application program areas, and the difference value between the starting address of the file identification area and the starting address of each application program area is also consistent.
Preferably, before the single chip microcomputer shuts down the interrupt after receiving the upgrade jump command, the method further includes:
the single chip microcomputer receives the upgrading verification command, verifies the received upgrading Bin file and then returns an upgrading verification result to the upper computer upgrading software; the verification of the upgrade Bin file specifically comprises the following steps: respectively verifying the file identification area of the upgraded Bin file, the file length and a verification value written into the FLASH application program; if the three are verified correctly, the upgrade verification is successful; if any one of the verification errors exists, upgrading the verification failure.
Preferably, after the single chip microcomputer is in soft restart and runs a new program, the single chip microcomputer reports the currently running application program area to the upper computer upgrading software, and the upper computer upgrading software judges whether upgrading is successful according to the reported currently running area.
In a second aspect, the present invention further provides a system for upgrading a single chip microcomputer online, which is used for implementing the method for upgrading a single chip microcomputer online in the first aspect, and the system comprises asingle chip microcomputer 1, upper computer upgradingsoftware 2 and acommunication interface 3, wherein a FLASH of thesingle chip microcomputer 1 is divided into an interrupt vector area, an application program a area and an application program B area, and the interrupt vector area is a common area of the application program a area and the application program B area;
the upper computer upgradingsoftware 2 is used for converting the upgrading Bin file in the same area into an effective upgrading Bin file in another area according to the running area reported by thesinglechip 1 and completing online upgrading;
thecommunication interface 3 is used for realizing the communication connection between thesingle chip microcomputer 1 and the upper computer upgradingsoftware 2.
The invention has the beneficial effects that:
in the method and the system for upgrading the single chip microcomputer on line, FLASH of the single chip microcomputer is divided into an interrupt vector area, an application program A area and an application program B area, the interrupt vector area occupies a lot of bytes compared with a traditional upgrading guide area, the interrupt vector area of the upgrading program is copied to the interrupt vector area of the single chip microcomputer during upgrading, and the interrupt vector area is only copied, so that the FLASH and the program moving time are greatly saved; meanwhile, the upper computer upgrading software can convert the generated upgrading Bin file into an effective upgrading Bin file of another area according to the current operation area reported by the single chip microcomputer, and the online upgrading is smoothly realized under the condition of not interrupting the service.
[ detailed description ] embodiments
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
In addition, the technical features involved in the embodiments of the present invention described below may be combined with each other as long as they do not conflict with each other. The invention will be described in detail below with reference to the figures and examples.
Example 1:
the embodiment of the invention provides a method for upgrading a singlechip on line, which is characterized in that before the method is used for upgrading, firstly referring to fig. 2, a FLASH of the singlechip is divided into an interrupt vector area, an application program A area and an application program B area; wherein the interrupt vector area is a common area of the application program A area and the application program B area. The useful part in the interrupt vector area has 300 bytes at most, and is usually more than 100 bytes, so the byte occupied by the interrupt vector area is usually less than 512 bytes, which is much smaller than that of the traditional upgrade boot area, and thus the code space allocated to the application program area A and the application program area B is greatly increased.
In the embodiment of the present invention, for example, a 51-chip microcomputer, the interrupt vector area occupies the first 512 bytes (code 0 × 0000-0 × 01 FF). The application program A area and the application program B area are both composed of an interrupt vector area and a program code area, and the interrupt vector area also occupies 512 bytes. The code distribution of the application program area a and the application program area B is respectively as shown in the left diagram and the right diagram in fig. 3, except for the shared interrupt vector area, the code distribution of the application program area a is in the first half area of the FLASH, and the code distribution of the application program area B is in the second half area of the FLASH. In this embodiment, a compiler is used to generate codes, for example, a Keil C51 compiler is used to fill a corresponding address Range in Code Range, and when the Code is generated by compilation, the Code can be generated in the corresponding address Range. In order to reserve Code space for the interrupt vectors of each region, the partition address of each region should be 512 bytes smaller than the start address of Code Range. As shown in fig. 3, in the application program a area, a code range of 0 × 0200 to 0 × 03FF is reserved for the interrupt vector area, and a range of the program code area is 0 × 0400 to 0 × 7 FFF; in the application program B area, a code range of 0 x 8000-0 x 81FF is reserved for the interrupt vector area, and the range of the program code area is 0 x 8200-0 x FFFF.
Referring to fig. 4, the method for upgrading a single chip microcomputer according to the embodiment of the present invention includes the following steps:
step 201, after receiving an upgrade start command, the single chip initializes an upgrade variable, and replies a currently running application program area to the upper computer upgrade software, so that the upper computer upgrade software determines an upgrade Bin file to be transmitted according to the currently running area of the single chip.
Because the application program is divided into an application program area A and an application program area B, the Bin file which needs to be upgraded is determined according to the actual operation area of the single chip microcomputer, so that two application programs need to be managed, the management is troublesome, and errors are easy to occur during upgrading. To solve this problem, the upper computer upgrade software of the present invention has a function of converting an upgrade Bin file of one area into an upgrade Bin file of another area, so that only one application program needs to be managed. The method specifically comprises the following steps: according to the currently running application program area replied by the single chip microcomputer, the upper computer upgrading software judges whether the upgrading program is in the same area as the currently running application program area of the single chip microcomputer, and if the upgrading program is in the different area, the current upgrading Bin file is directly transmitted to the single chip microcomputer for upgrading; if the two-zone upgrading method is the same zone, the upper computer upgrading software converts the current upgrading Bin file into another zone upgrading Bin file, and then transmits the converted upgrading Bin file to the single chip microcomputer for upgrading. In this embodiment, taking the current operation of the single chip in the application program a area as an example, the upgrade must be performed in the application program B area, and if the upgrade Bin file is in the application program B area, the upgrade Bin file may be directly transmitted for upgrade; and if the upgrade Bin file is in the application program A area, firstly converting the upgrade Bin file corresponding to the A area into the upgrade Bin file corresponding to the B area, and then upgrading.
Step 202, the single chip microcomputer receives an upgrade Bin file transmitted by upgrade software of an upper computer, and stores the upgrade Bin file in an application program area which is not operated by the single chip microcomputer FLASH currently. Taking the current operation of the single chip microcomputer in the application program area A as an example, the upgrade software of the upper computer is finally transmitted through conversion to be the upgrade Bin file corresponding to the area B, and then the single chip microcomputer stores the received upgrade Bin file in the application program area B of the single chip microcomputer FLASH so as to be upgraded in the application program area B in the following.
And step 203, the single chip microcomputer closes the interruption after receiving the upgrading skip command, copies the interruption vector area of the upgrading program to the interruption vector area of the single chip microcomputer, and softly restarts the single chip microcomputer and runs a new program to finish upgrading.
When all interrupts are set to be opened in the program, all types of interrupts of the singlechip enter respective interrupt programs to execute work under specific time and conditions. If the interrupt is closed, all types of interrupts will not enter the interrupt routine regardless of condition and time. In thestep 202, if the upgrade Bin file is stored in the application program B area of the single chip microcomputer, the upgrade program is written into the application program B area, and correspondingly, the interrupt vector area of the upgrade program is correspondingly written into the interrupt vector area (0 × 8000-0 × 81FF) of the application program B area of the single chip microcomputer; after all interrupts are closed, copying and transferring the interrupt vector area of the written upgrading program to the interrupt vector area of the single chip microcomputer, namely copying the interrupt vector area (0 multiplied by 8000-0 multiplied by 81FF) of the updated application program B area to the interrupt vector area (0 multiplied by 0000-0 multiplied by 01FF) of the single chip microcomputer. After copying, the single chip microcomputer is soft restarted to run a new program, the single chip microcomputer reports the currently running application program area to the upper computer upgrading software, and the upper computer upgrading software judges whether upgrading is successful according to the reported currently running area: if the current operation area is the application program B area, the operation area is correct, and the upgrading is successful; otherwise, the upgrade fails.
In the single chip microcomputer online upgrading method provided by the invention, the FLASH of the single chip microcomputer is divided into an interrupt vector area, an application program A area and an application program B area, the interrupt vector area occupies a lot of bytes compared with the traditional upgrading guide area, the interrupt vector area of the upgrading program is copied to the interrupt vector area of the single chip microcomputer during upgrading, and the space of the FLASH and the program moving time are greatly saved as only the interrupt vector area needs to be copied; meanwhile, the upper computer upgrading software can convert the generated upgrading Bin file into an effective upgrading Bin file of another area according to the current operation area reported by the single chip microcomputer, and the online upgrading is smoothly realized under the condition of not interrupting the service.
Referring to fig. 5, in the embodiment of the present invention, the upgrade Bin file is also composed of an interrupt vector area and a program code area, and the interrupt vector area also occupies 512 bytes. A file information area is arranged in the interrupt vector area of the upgrade Bin file and used for storing file information of the upgrade Bin file, such as file length and a check value; and a file identification area is arranged in the program code area of the upgrade Bin file, and file identification bytes are stored in the file identification area and used for judging whether the upgrade Bin file is a required upgrade file. The file information can be stored in the second half of the interrupt vector area, because the useful part of the interrupt vector has 300 bytes at most, and the latter 200 bytes are all invalid codes, the file information can be stored in the last 128 bytes of the interrupt vector area so as to be transmitted to the singlechip for upgrade check when upgrading.
In the embodiment of the invention, in order to prevent the wrong file from being upgraded, the file identification byte is used in the upgrade Bin file. After the updated Bin file is generated, before the updated Bin file is sent, whether the updated file is valid can be judged according to the file identification byte. Specifically, a C file can be defined in the application program, only one Code array is defined in the C file, and is initialized to a special identification byte, and finally, the address location of the Code array is defined. The file identification areas of the upgrade Bin files in the application program area A and the application program area B are located at the same position, namely fixed addresses, when the addresses are defined, for the upgrade Bin files in the two application program areas, the address difference value of the file identification areas is consistent with the address offset between the two application program areas, and the difference values between the starting addresses of the file identification areas and the starting addresses of the respective application program areas are also consistent. For example, for the upgrade Bin file in the application a area, the file identification area is located at 0 × 0500-0 × 050F, and for the upgrade Bin file in the application B area, the file identification area is located at 0 × 8300-0 × 830F. The file identification area occupies a small memory, and generally 16 bytes are enough.
In thestep 203, the upgrade program is written into the application program B area of the single chip microcomputer, that is, the upgrade Bin file corresponding to the B area is written into the application program B area of the single chip microcomputer, and then the interrupt vector area (0 × 8000-0 × 81FF) of the application program B area of the single chip microcomputer is copied into the interrupt vector area (0 × 0000-0 × 01FF) of the single chip microcomputer. After the upgrade is completed, the FLASH of the single chip microcomputer is as shown in fig. 6, where the first 512 bytes (0 × 0000-0 × 01FF) are the interrupt vector area of the current running area, and the application a area and the application B area are both composed of 512-byte interrupt vector areas and program code areas.
Generally, only a Hex file can be generated by compiling by adopting Keil C51, and general upgrade files are Bin files, so in the embodiment of the invention, before an upgrade start command is sent to a single chip microcomputer, the upgrade software of an upper computer is led into the upgrade Hex file generated by a compiler, and then the Hex file generated by the compiler is converted into the upgrade Bin file required by upgrading, so that the Hex file in an application program area a or an application program area B is uniformly generated into the Bin file. Then, the upper computer upgrading software judges whether the generated upgrading Bin file is valid or not, and if the generated upgrading Bin file is valid, the upgrading software continues to send an upgrading starting command to the single chip microcomputer; if the upgrade is invalid, the upgrade fails, and the upgrade is directly finished.
Fig. 7 is referred to in the process of converting a Hex file into a Bin file by the upper computer upgrade software, and the conversion method is the same as the conversion of a general Hex file into a Bin file, but the difference is that: in order to make the Bin format generated by the application A area and the application B area consistent, if the data address is not in the address range (0 x 0000-0 x 01FF) of the interrupt vector area, subtracting an address offset from the data address; wherein, the address offset of the application program A area is 0, and the address offset of the application program B area is the address difference of the two areas, namely 0 × 7E 00; after the offset, the corresponding array content and file length are updated with the new address. After the conversion is completed, file information of the Bin file, such as the file length and the check value of the file, needs to be generated.
After generating a Bin file according to the above steps, if the upgrade program is in the same region as the current operation region of the single chip microcomputer in step 201, the upgrade software of the upper computer needs to convert the current upgrade Bin file into an upgrade Bin file of another region, and the specific process refers to fig. 8:
firstly, calculating address offset, namely address difference between two application program intervals according to the converted area; defining a file character string and assigning a value to be null, opening a current upgrade Bin file and setting a file index as a file start;
reading a byte of the current upgrade Bin file, obtaining the byte number N contained in the corresponding instruction by inquiring the singlechip assembly language instruction table, and analyzing whether the instruction is related to the address or not; if the instruction is related to the address, adding (transferring the application program area A to the application program area B) or subtracting (transferring the application program area B to the application program area A) an address offset to the address corresponding to the instruction to generate a new address and replace the original address byte, and adding the instruction to the tail of the file string according to the original sequence; if the instruction is not related to the address, the remaining N-1 bytes are directly read, and N bytes are added to the tail of the file string. The codes of the file information area and the file identification area are not required to be analyzed through table lookup, and byte data are directly added to the tail of the file character string;
and continuously and circularly and repeatedly reading the bytes in the current upgrade Bin file, analyzing and copying according to the second step until the reading length is 0, and finishing the conversion of the Bin file. Then replacing the old file information bytes with 0 XFF in the file character string, generating the file information of a new file according to the new code, and filling the file information into the corresponding position of the file character string; and finally, writing the file character string into a new Bin file, and generating the Bin file of the new area successfully.
Instep 202, the single chip receives the upgrade Bin file transmitted by the upgrade software of the upper computer, and stores the upgrade Bin file in an application program area where the FLASH of the single chip is not currently running, and the specific implementation manner may refer to fig. 9, and the steps are as follows: the single chip microcomputer receives a data packet of an upgrading Bin file transmitted by upgrading software of an upper computer, calculates and judges whether a check value of the data packet is correct or not, and if the check value is correct, the upgrading file data packet is written into an application program area which is not operated at present of the single chip microcomputer FLASH, namely an application program area B; if the error occurs, the reception of the upgrade file fails. If the update result is the first update Bin file data packet, judging whether the area of the update program is correct or not according to the relevant bytes of the code after receiving the data packet, namely whether the update program is the update Bin file in the application program B area or not, if so, calculating and judging whether the check value of the data packet is correct or not; if the region is incorrect, receiving the upgrade file fails.
Generally, between thestep 202 and thestep 203, the method further comprises the step of verifying the upgrade file: and the single chip microcomputer receives the upgrading verification command, verifies the received upgrading Bin file and then replies an upgrading verification result to the upper computer upgrading software. The verification of the upgrade Bin file specifically comprises the following steps: respectively verifying the file identification area of the upgraded Bin file, the file length and a verification value written into the FLASH application program; if the three are verified correctly, the upgrade verification is successful; if any one of the verification errors exists, upgrading the verification failure. Referring to fig. 10, firstly, it may be determined whether the upgrade Bin file is a required upgrade file according to the code of the file identification area, that is, whether the application program is correct, if not, the upgrade verification fails, and if correct, the verification continues; secondly, judging whether the length of the received application program is correct or not according to the file length of the file information area, if not, failing to update and verify, and if so, continuing to verify; and finally, calculating a check value of the application program received by the writing FLASH, judging whether the application program written into the FLASH is correct or not according to the check value of the file information area, if not, failing to update and check, and if so, successfully updating and checking.
In the embodiment of the present invention, the whole upgrade process of the single chip microcomputer may refer to fig. 11, and by combining the above description of the steps, the whole upgrade process of the single chip microcomputer may be divided into four processes, i.e., an upgrade start process, an upgrade file transfer process, an upgrade check process, and an upgrade skip process. In the upgrading starting stage, the single chip microcomputer receives the instruction and reports the current operation area, so that the upgrading software of the upper computer determines whether to convert the upgrading Bin file into another upgrading Bin file according to the operation area; in the upgrade file transmission stage, the upgrade software of the upper computer transmits the upgrade Bin file to the single chip microcomputer, and the single chip microcomputer stores the upgrade Bin file in a current non-operation area; in the upgrading verification stage, respectively verifying a file identification area, a file length and an application program verification value written into FLASH; and in the stage of upgrading skip, closing the interrupt, copying the interrupt vector area of the upgrading program to the interrupt vector area of the single chip microcomputer, and running a new program after the single chip microcomputer is soft restarted. Through the steps, the on-line upgrading of the single chip microcomputer can be completed under the condition that the service is not influenced.
Example 2:
on the basis of the foregoingembodiment 1, an embodiment of the present invention further provides an on-line upgrade system for a single chip microcomputer, which is used for executing the on-line upgrade method for a single chip microcomputer inembodiment 1, and as shown in fig. 12, the upgrade system includes asingle chip microcomputer 1, uppercomputer upgrade software 2, and acommunication interface 3. Referring to fig. 2, the FLASH of thesingle chip microcomputer 1 is divided into an interrupt vector area, an application program a area and an application program B area; wherein the interrupt vector area is a common area of the application program A area and the application program B area. The interrupt vector area here occupies the first 512 bytes (code 0 x 0000-0 x 01FF), which is much smaller than the conventional upgrade boot area, so that the code space allocated to the application a area and the application B area is greatly increased. The application program A area and the application program B area are both composed of a 512-byte interrupt vector area and a program code area. By the partition mode, when the upgrading system provided by the embodiment of the invention is adopted for upgrading, only the interrupt vector area of the upgrading program needs to be copied to the interrupt vector area of the single chip microcomputer, and the FLASH and program moving time is greatly saved as only the interrupt vector area needs to be copied.
The uppercomputer upgrading software 2 mainly has three functions: firstly, the Hex file generated by the compiler can be converted into the Bin file required for upgrading, i.e. the steps shown in fig. 7 are executed; secondly, the generated upgrade Bin file can be converted to generate an effective upgrade Bin file of another area according to the operation area reported by thesingle chip microcomputer 1, namely, the steps shown in fig. 8 are executed; and thirdly, implementing an online upgrading function. The generated upgrade Bin file is converted to generate an effective upgrade Bin file of another area, although the application program is divided into an application program area A and an application program area B, only one application program needs to be managed, the trouble brought by managing the two application programs is avoided, and online upgrade is smoothly realized under the condition of not interrupting service.
Thecommunication interface 3 is used for realizing communication connection between thesingle chip microcomputer 1 and the uppercomputer upgrading software 2, and the uppercomputer upgrading software 2 can send related commands to thesingle chip microcomputer 1 through thecommunication interface 3, so that thesingle chip microcomputer 1 can realize corresponding operations according to the commands, such as executing steps shown in fig. 9 when receiving an upgrading file and steps shown in fig. 10 when upgrading is checked; meanwhile, thesingle chip microcomputer 1 can reply information to the uppercomputer upgrading software 2 through thecommunication interface 3, so that the uppercomputer upgrading software 2 carries out corresponding operation according to a reply result. Thecommunication interface 3 may be a serial port, I2C or a network port, and is not particularly limited.
Specifically, referring to fig. 11, in a flow chart portion in a dashed line frame of thesingle chip microcomputer 1, corresponding operation steps in the dashed line frame may be implemented by thesingle chip microcomputer 1; correspondingly, in the flow chart part in the dashed line frame of the uppercomputer upgrading software 2, the corresponding operation steps in the dashed line frame can be completed by the uppercomputer upgrading software 2. The specific function implementation processes of thesingle chip microcomputer 1 and the uppercomputer upgrade software 2 can refer to the descriptions of the steps and the drawings inembodiment 1, and are not described herein again.
In the on-line upgrading system of the single chip microcomputer, the FLASH of the single chip microcomputer is divided into an interrupt vector area, an application program A area and an application program B area, the interrupt vector area occupies a plurality of bytes smaller than that of a traditional upgrading guide area, and the interrupt vector area of the upgrading program is only copied to the interrupt vector area of the single chip microcomputer during upgrading, so that the FLASH and program moving time is greatly saved; meanwhile, the upper computer upgrading software can convert the generated upgrading Bin file into an effective upgrading Bin file of another area according to the current operation area reported by the single chip microcomputer, and the online upgrading is smoothly realized under the condition of not interrupting the service.
The above description is only for the purpose of illustrating the preferred embodiments of the present invention and is not to be construed as limiting the invention, and any modifications, equivalents and improvements made within the spirit and principle of the present invention are intended to be included within the scope of the present invention.