Rapid backup upgrading method based on GD32
By dividing the GD32 internal FLASH into four regions and using the CRC verification function verification file, the rapid backup upgrade of GD32 in the application is realized, solving the problem of system start -up time delay caused by the traditional upgrade method, ensuring the upgrade of the upgrade. reliability.
Patent Information
- Application Number
- CN202411470899.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-21
- Publication Date
- 2025-01-28
AI Technical Summary
The traditional GD32 upgrade method waits for external upgrade instructions in the bootloader, resulting in delayed system startup time.
By dividing the internal FLASH inside of the GD32 into four areas, defining the structure of the structure storage and upgrading package information, using the CRC verification function to verify the file, determine the need to upgrade and perform power re -restoration to realize the upgrade in the application.
The problem of waiting for long -term operation in BOOT has been solved, and the dual redundant backups of applications and backup applications have been realized to ensure the reliability of upgrade.
Smart Images

Figure CN119376771A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of computer control and relates to a rapid backup and upgrade method based on GD32. Background Art
[0002] BOOT is a program that runs before the operating system kernel runs. It initializes the system hardware and loads the operating system or application into the memory for execution. It includes hardware initialization, boot device selection, including flash memory, external memory interface or serial port, etc., loading the operating system or application, executing the loaded program, error handling and rollback, upgrade support, etc.
[0003] For some MCUs with small memory and simple functions (such as GD32), firmware upgrades can also be completed through the bootloader. In order to realize the online upgrade (IAP) function of STM32, the FLASH of STM32 is usually divided into two parts: BOOT and APP. BOOT is the boot program that boots APP. When online upgrade is required, it can be achieved through BOOT. Summary of the invention
[0004] 1. Technical issues to be resolved
[0005] Traditionally, GD32 upgrades are completely performed in the bootloader. When the MCU is powered on, it waits for external upgrade instructions to complete the transmission and writing of upgrade files. In this way, regardless of whether an upgrade is required, a certain instruction waiting time is required in the bootloader, which seriously delays the system startup time.
[0006] (II) Technical solution
[0007] In order to solve the above technical problems, the present invention provides a fast backup and upgrade method based on GD32, which comprises the following steps:
[0008] S1: Divide the internal FLASH of GD32 into four areas;
[0009] S2: define the structure to store the upgrade package information;
[0010] S3: Use the CRC check function CRCCAL to check the file, obtain the check value calcrc, compare calcrc with the file check value gcrc in the structure in step S2, and if the comparison values are the same, determine that an upgrade is required, and power on and restart the MCU; if the comparison values are not the same, do not perform the upgrade operation;
[0011] S4: After the MCU is powered on, it first enters the BOOT program, waits for the upgrade flag in the BOOT program, and uses the program jump instruction to jump to the application program execution.
[0012] In step S1, the four areas are: a BOOT program area, with a starting address of BOOT_ADDRESS; an application program area APP_ADDRESS; a backup program area BACKUP_ADDRESS; and an application program information area INFO_ADDRESS.
[0013] In step S1, the application information is divided into: a software version segment, with a starting address of VERSION_ADDRESS; an upgrade file size segment; a starting address segment SIZE_ADDRESS; an upgrade file check segment, with a starting address of CRC_ADDRESS; and an upgrade flag segment FLAG_ADDRESS.
[0014] In step S2, the structure includes: version number FILEVERSION, type is unsigned integer; file size information FILELENGTH, type is unsigned integer; file name FILENAME, type is character pointer type; file address FILEADDR, type is unsigned character pointer; file check value FILECRC.
[0015] In step S2, the MCU waits for the upgrade command in a low priority task during the application program execution. When the version information gversion, file size glength, file name gname and file checksum gcrc are received, the FLASH read function STMFLASH_ReadWord function is used in the software version segment to read the VERSION_ADDRESS address data and compare it with gversion to determine whether to upgrade.
[0016] In step S2, if the VERSION_ADDRESS address data and gversion are the same, the current version is the latest version and no upgrade operation is performed.
[0017] In step S2, when the VERSION_ADDRESS address data is different from gversion, the version number gversion, file size information glength, file name information gname, and file check value gcrc are respectively assigned to the version number FILEVERSION, file size information FILELENGTH, and file name information FILENAME of the corresponding structure in step S2, and the dynamic memory allocation function malloc is used to apply for a space of size glength, and the return value is assigned to the FILEADDR in the structure of step S2; the upgrade package is received through the bus, and after the reception is completed, each frame of data received is checked to verify whether the total number of frames of the upgrade package is correct, and the received data is moved to the space with the starting address FILEADDR using the memory move function memmove.
[0018] In step S3, the process of determining that an upgrade is required and powering on and restarting the MCU is as follows:
[0019] (1) First, erase the non-0xFFFFFFFF addresses in the FLASH address, one sector at a time, and determine whether there is a failure through the return value of the erase function HAL_FLASHEx_Erase. If it fails, it returns directly and reports an error. Otherwise, all data in the backup program area BACKUP_ADDRESS is erased;
[0020] (2) Use the FLASH operation wait instruction FLASH_WaitForLastOperation to determine whether the erase is complete;
[0021] (3) Use the FLASH data writing function HAL_FLASH_Program to loop write the data in the structure with FILEADDR as the first address and file size information FILELENGTH as the length to the backup program area BACKUP_ADDRESS in the FLASH;
[0022] (4) Use HAL_FLASH_Program to write the FILEVERSION value in the structure to VERSION_ADDRESS, the FILELENGTH value to the address SIZE_ADDRESS, the FILECRC value to the address CRC_ADDRESS, and the value 1 to the upgrade marker segment FLAG_ADDRESS, indicating that an upgrade is currently required and powering on and restarting the MCU.
[0023] In step S4, ReadFlashData is used to read the value of the SIZE_ADDRESS address, the value of the VERSION_ADDRESS address, and the value of the CRC_ADDRESS address, and are respectively assigned to the three member variables of the structure, namely, the version number FILEVERSION, the file size information FILELENGTH, and the file check value FILECRC; the dynamic memory application function malloc is used to apply for a memory space of a size of SIZE_LENGTH length, and the return value is assigned to the file address FILEADDR member variable, and ReadFlashData is used to read data of a length of SIZE_LENGTH, and the read data is stored in the address space of the first address FILEADDR;
[0024] Use the CRC check function CRCCAL again to check the data in the FILEADDR address space and get the check value calcrc; if they are consistent, use the FLASH data write function HAL_FLASH_Program to write the data with FILEADDR as the first address and length FILELENGTH to the application area with APP_ADDRESS as the first address, and write the value of the FLAG_ADDRESS address to 0.
[0025] (III) Beneficial effects
[0026] The GD32-based fast backup upgrade method provided by the above technical solution realizes the upgrade method in the application, solves the problem of long waiting time for operation in BOOT, and realizes dual redundant backup of the application and backup application by dividing the GD32 FLASH into regions, thereby ensuring the reliability of the upgrade. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] Figure 1 The figure is a flow chart of the fast backup and upgrade method based on GD32 of the present invention. DETAILED DESCRIPTION
[0028] In order to make the purpose, content and advantages of the present invention more clear, the specific implementation methods of the present invention are further described in detail below in conjunction with the drawings and examples.
[0029] Reference Figure 1 As shown, the fast backup and upgrade method based on GD32 in this embodiment includes the following steps:
[0030] S1: Divide the GD32 internal FLASH into four areas
[0031] The four areas are: BOOT program area, starting at BOOT_ADDRESS; application program area APP_ADDRESS; backup program area BACKUP_ADDRESS; application information area INFO_ADDRESS;
[0032] The application information area is divided into: software version segment, starting address is VERSION_ADDRESS; upgrade file size segment; starting address segment SIZE_ADDRESS; upgrade file checksum segment, starting address is CRC_ADDRESS; upgrade flag segment FLAG_ADDRESS.
[0033] S2: Define the structure to store the upgrade package information
[0034] The structure includes: version number FILEVERSION, type is unsigned integer; file size information FILELENGTH, type is unsigned integer; file name FILENAME, type is character pointer type; file address FILEADDR, type is unsigned character pointer; file check value FILECRC.
[0035] When the MCU is running, it waits for the upgrade command in a low-priority task. When it receives the version information gversion, file size glength, file name gname and file checksum gcrc, it uses the FLASH read function STMFLASH_ReadWord function in the software version segment to read the VERSION_ADDRESS address data and compare it with gversion.
[0036] If they are the same, the current version is the latest version and no upgrade is performed.
[0037] If the comparison is different, the version number gversion, file size information glength, file name information gname, and file check value gcrc are respectively assigned to the version number FILEVERSION, file size information FILELENGTH, and file name information FILENAME of the corresponding structure in step S2, and the dynamic memory allocation function malloc is used to apply for a space of size glength, and the return value is assigned to the FILEADDR in the step S2 structure; the upgrade package is received through the bus, and after the reception is completed, each frame of data received is checked to verify whether the total number of frames of the upgrade package is correct, and the received data is moved to the space with the starting address of FILEADDR using the memory movement function memmove.
[0038] S3: Use the CRC check function CRCCAL to check the file, obtain the check value calcrc, compare calcrc with the file check value gcrc in the structure in step S2, and if the comparison values are the same, determine that an upgrade is required, and power on and restart the MCU; if the comparison values are not the same, do not perform the upgrade operation.
[0039] The process of determining that an upgrade is required and powering on and restarting the MCU is as follows:
[0040] (1) First, erase the addresses other than 0xFFFFFFFF in the FLASH address, one sector at a time, and determine whether there is a failure through the return value of the erase function HAL_FLASHEx_Erase. If it fails, it returns directly and reports an error. Otherwise, all data in the backup program area BACKUP_ADDRESS is erased.
[0041] (2) Use the FLASH operation wait instruction FLASH_WaitForLastOperation to determine whether the erase is complete.
[0042] (3) Use the FLASH data writing function HAL_FLASH_Program to loop write the data in the structure with FILEADDR as the first address and file size information FILELENGTH as the length to the backup program area BACKUP_ADDRESS in the FLASH.
[0043] (4) Use HAL_FLASH_Program to write the FILEVERSION value in the structure to VERSION_ADDRESS, the FILELENGTH value to the address SIZE_ADDRESS, the FILECRC value to the address CRC_ADDRESS, and the value 1 to the upgrade mark segment FLAG_ADDRESS, indicating that an upgrade is currently required. Power on and restart the MCU. Go to S4
[0044] S4: After the MCU is powered on, it first enters the BOOT program and reads the value of the FLAG_ADDRESS address in the BOOT program. If the value is 1, it means waiting for upgrade.
[0045] Among them, use ReadFlashData to read the value of SIZE_ADDRESS address, the value of VERSION_ADDRESS address, and the value of CRC_ADDRESS address, and assign them to the three member variables of the structure: version number FILEVERSION, file size information FILELENGTH, file check value FILECRC. Use the dynamic memory application function malloc to apply for a memory space of SIZE_LENGTH length, and assign the return value to the file address FILEADDR member variable, and use ReadFlashData to read data of SIZE_LENGTH length. Store the read data in the address space with the first address FILEADDR.
[0046] Use the CRC check function CRCCAL again to check the data in the FILEADDR address space and get the check value calcrc. If they are consistent, use the FLASH data write function HAL_FLASH_Program to write the data with FILEADDR as the first address and length FILELENGTH to the application area with APP_ADDRESS as the first address, and write the value of the FLAG_ADDRESS address to 0.
[0047] At this time, use the program jump instruction to jump to the application program execution.
[0048] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the technical principles of the present invention. These improvements and modifications should also be regarded as the scope of protection of the present invention.
Claims
1. A fast backup and upgrade method based on GD32, characterized in that: The following steps are involved: S1: Divide the internal FLASH of GD32 into four areas; S2: define the structure to store the upgrade package information; S3: Use the CRC check function CRCCAL to check the file, obtain the check value calcrc, compare calcrc with the file check value gcrc in the structure in step S2, and if the comparison values are the same, determine that an upgrade is required, and power on and restart the MCU; if the comparison values are not the same, do not perform the upgrade operation; S4: After the MCU is powered on, it first enters the BOOT program, waits for the upgrade flag in the BOOT program, and uses the program jump instruction to jump to the application program execution.
2. The GD32-based fast backup and upgrade method according to claim 1, characterized in that: In step S1, the four areas are: a BOOT program area, the starting address of which is BOOT_ADDRESS; an application program area APP_ADDRESS; a backup program area BACKUP_ADDRESS; and an application program information area INFO_ADDRESS.
3. The GD32-based fast backup and upgrade method according to claim 2, characterized in that: In step S1, the application information is divided into: a software version segment, with a starting address of VERSION_ADDRESS; an upgrade file size segment; a starting address segment SIZE_ADDRESS; an upgrade file check segment, with a starting address of CRC_ADDRESS; and an upgrade flag segment FLAG_ADDRESS.
4. The GD32-based fast backup and upgrade method according to claim 3, characterized in that: In step S2, the structure includes: version number FILEVERSION, type is unsigned integer; file size information FILELENGTH, type is unsigned integer; file name FILENAME, type is character pointer type; file address FILEADDR, type is unsigned character pointer; file check value FILECRC.
5. The GD32-based fast backup and upgrade method according to claim 4, characterized in that: In step S2, the MCU waits for the upgrade command in a low priority task during the application program execution. When the version information gversion, file size glength, file name gname and file checksum gcrc are received, the FLASH read function STMFLASH_ReadWord function is used in the software version segment to read the VERSION_ADDRESS address data and compare it with gversion to determine whether to upgrade.
6. The GD32-based fast backup and upgrade method according to claim 5, characterized in that: In step S2, if the VERSION_ADDRESS address data and gversion are the same, the current version is the latest version and no upgrade operation is performed.
7. The GD32-based fast backup and upgrade method according to claim 6, characterized in that: In step S2, when the VERSION_ADDRESS address data is different from gversion, the version number gversion, file size information glength, file name information gname, and file check value gcrc are respectively assigned to the version number FILEVERSION, file size information FILELENGTH, and file name information FILENAME of the corresponding structure in step S2, and the dynamic memory allocation function malloc is used to apply for a space of size glength, and the return value is assigned to the FILEADDR in the structure of step S2; the upgrade package is received through the bus, and after the reception is completed, each frame of data received is checked to verify whether the total number of frames of the upgrade package is correct, and the received data is moved to the space with the starting address FILEADDR using the memory move function memmove.
8. The GD32-based fast backup and upgrade method according to claim 7, characterized in that: In step S3, the process of determining that an upgrade is required and powering on and restarting the MCU is as follows: (1) First, erase the non-0xFFFFFFFF addresses in the FLASH address, one sector at a time, and determine whether there is a failure through the return value of the erase function HAL_FLASHEx_Erase. If it fails, it returns directly and reports an error. Otherwise, all data in the backup program area BACKUP_ADDRESS is erased; (2) Use the FLASH operation wait instruction FLASH_WaitForLastOperation to determine whether the erase is complete; (3) Use the FLASH data writing function HAL_FLASH_Program to loop write the data in the structure with FILEADDR as the first address and file size information FILELENGTH as the length to the backup program area BACKUP_ADDRESS in the FLASH; (4) Use HAL_FLASH_Program to write the FILEVERSION value in the structure to VERSION_ADDRESS, the FILELENGTH value to the address SIZE_ADDRESS, the FILECRC value to the address CRC_ADDRESS, and the value 1 to the upgrade marker segment FLAG_ADDRESS, indicating that an upgrade is currently required and powering on and restarting the MCU.
9. The GD32-based fast backup and upgrade method according to claim 8, characterized in that: In step S4, ReadFlashData is used to read the value of the SIZE_ADDRESS address, the value of the VERSION_ADDRESS address, and the value of the CRC_ADDRESS address, and are respectively assigned to the three member variables of the structure, namely, the version number FILEVERSION, the file size information FILELENGTH, and the file check value FILECRC; the dynamic memory application function malloc is used to apply for a memory space of a size of SIZE_LENGTH length, and the return value is assigned to the file address FILEADDR member variable, and ReadFlashData is used to read data of a length of SIZE_LENGTH, and the read data is stored in the address space of the first address FILEADDR; Use the CRC check function CRCCAL again to check the data in the FILEADDR address space and get the check value calcrc; if they are consistent, use the FLASH data write function HAL_FLASH_Program to write the data with FILEADDR as the first address and length FILELENGTH to the application area with APP_ADDRESS as the first address, and write the value of the FLAG_ADDRESS address to 0.
10. Application of the GD32-based rapid backup and upgrade method according to any one of claims 1 to 9 in the field of computer control technology.