A software upgrade method based on domestic single-chip microcomputer GD32

By designing an IAP upgrade method based on CAN bus, using the YMODEM protocol and CAN message structure, the batch problem of microcontroller software upgrade is solved, and a simple and fast multi-device upgrade is achieved, reducing costs and time overhead.

CN113885910BActive Publication Date: 2024-12-03TIANJIN JINHANG COMP TECH RES INST
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111126036.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-09-24
Publication Date
2024-12-03
Estimated Expiration
2041-09-24

AI Technical Summary

Technical Problem

In the existing microcontroller software upgrade methods, the use of emulator upgrade requires disassembly of equipment, time and economic costs, and IAP upgrades usually can only upgrade one device separately, lacking batch upgrade solutions.

Method used

Design an IAP upgrade method based on CAN bus, batch software upgrade on the CAN bus through the YMODEM protocol, use the CAN message structure and interrupt callback function to process data, and complete firmware upgrade by writing to the flash memory function to realize program jump.

Benefits of technology

It realizes simple and fast batch software upgrades, reduces time and economic costs, and supports multiple devices to upgrade at the same time.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113885910B_ABST
    Figure CN113885910B_ABST
Patent Text Reader

Abstract

The invention discloses a software upgrade method based on a domestic single-chip microcomputer GD32. The IAP upgrade method based on the CAN bus is used to connect all the devices to be upgraded to the bus, and a simple and fast batch upgrade is performed by using the YMODEM protocol. The invention hangs all the devices to be upgraded on the CAN bus, and sends the upgrade file to each device to be upgraded through the YMODEM protocol. The upgrade method is simple, fast, and can perform batch software upgrades.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the technical field of computer control and relates to a software upgrading method based on a domestic single-chip microcomputer GD32. Background Art

[0002] Software in the industrial control field usually needs to be upgraded again after the first deployment. After the software has been used for a period of time, it is necessary to burn the software in the chip again due to bugs, increased user demand, and module performance upgrades. The software upgrade methods include ISP (in-system programming) and IAP (in-application programming).

[0003] The ISP upgrade method generally uses an emulator to upgrade the software. It is rarely used now. The main disadvantage of this method is that it requires disassembling the device and connecting the emulator, which has high time and economic costs.

[0004] The current mainstream method is the IAP upgrade method. Traditionally, the serial port is usually used for upgrade. When the system enters the BootLoader, the upgrade file is written into the FLASH through the serial port and jumped.

[0005] Common MCU software upgrades include using an emulator to upgrade and using IAP to upgrade the software. The domestic MCU GD32 can also be upgraded using an emulator or using IAP. For the method of upgrading using an emulator (using JTag or SWD for burning), it is necessary to connect a dedicated burning emulator each time. For product design, a separate upgrade interface circuit needs to be designed, and only one device can be upgraded each time. Summary of the invention

[0006] (I) Purpose of the invention

[0007] The purpose of the present invention is to provide a software upgrade method based on a domestic single-chip microcomputer GD32, using an IAP upgrade method based on a CAN bus, connecting all devices to be upgraded to the bus, and performing simple and fast batch upgrades by using a YMODEM protocol.

[0008] (II) Technical solution

[0009] In order to solve the above technical problems, the present invention provides a software upgrade method based on a domestic single-chip microcomputer GD32, comprising the following process:

[0010] (1) Design the CAN bus communication application layer protocol, design the CAN message structure CAN_RECV_DATA in the CAN data processing process, and configure the 29-bit extended frame of the CAN bus.

[0011] (2) Initialize the MCU system, configure the clock information and interrupt information, configure the bus communication rate information, the mask value and filter value of the bus ID, and define the variables and ping-pong buffer used.

[0012] (3) In the CAN bus interrupt callback function CAN0_RX0_IRQHandler, the received CAN bus data is stored in the ping-pong buffer in sequence.

[0013] (4) According to the CAN bus communication protocol defined in step 1, the CAN bus data is parsed, the ID value of the acquired data frame is assigned to the receive frame identification variable RecvID, the acquired data length is assigned to the receive frame length variable RecvDLC, and the acquired up to 8 data values ​​are assigned to the receive data pointer RecvData.

[0014] (5) In the main function, the received CAN bus data is parsed, the parsed data is extracted, and the updated firmware information is written to the Flash using the write flash function WriteFlash(); the application startup address APP_ADDR_FLASH is read and it is determined whether the highest bit of the data stored at the address is 1. If it is 1, it means that the firmware has been successfully written to the Flash.

[0015] (6) Get the value stored in the 4-byte address offset of APP_ADDR_FLASH and convert it into a function pointer type. Use the stack pointer initialization function: set_MSP() function, with the parameter APP_ADDR_FLASH to initialize the user's stack pointer; run the entity pointed to by the pFunction function pointer to perform a program jump, complete the jump from the startup program to the application program, and complete the program upgrade.

[0016] (III) Beneficial effects

[0017] The software upgrade method based on the domestic single-chip microcomputer GD32 provided by the above technical solution hangs all the devices to be upgraded on the CAN bus, and sends the upgrade file to each device to be upgraded through the YMODEM protocol. This upgrade method is simple and fast, and can perform batch software upgrades. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] Figure 1 The present invention is a flowchart of a software upgrade method based on a domestic single-chip microcomputer GD32 according to an embodiment of the present invention. DETAILED DESCRIPTION

[0019] 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.

[0020] Reference Figure 1 As shown, the software upgrade method based on the domestic single-chip microcomputer GD32 in this embodiment includes the following steps:

[0021] S1: Define the CAN bus communication application layer protocol, which includes a two-byte frame header, a two-byte frame length, a one-byte destination address, a one-byte source address, a one-byte total number of frames for the upgrade file, a one-byte current frame sequence number, 1K bytes of YMODEM protocol upgrade file content, and a two-byte CRC check. The two bytes of the frame header are 0x55 and 0xAA respectively.

[0022] Design the CAN message structure CAN_RECV_DATA in the data processing process. This structure includes ① extended frame ID value ExtID, type is unsigned long integer; ② data length DLC, type is unsigned short integer; ③ array Data, length is 8, type is unsigned char.

[0023] Configure the 29-bit extended frame ID of the CAN bus, use the 8 bits from bit 14 to bit 21 as the CAN bus source node domain, and the 8 bits from bit 22 to bit 29 as the CAN bus destination node domain. Define the CAN bus source node ID value as SRCID, the destination node ID value as DSTID, and assign the SRCID value and DSTID value to the destination node domain and source node domain of the CAN bus respectively.

[0024] S2: Use the initialization function SystemInit() provided by the official library to initialize the basic functions of the GD32 board. The configuration process includes using the system clock configuration function SetSysClock() to initialize the system clock, enable the off-chip high-speed clock HSE, configure the clock division factor (including the AHB high-speed bus division factor, APB1 low-speed bus division factor, and APB2 low-speed bus division factor), turn on the phase-locked loop (PLL) clock, and configure the interrupt vector NVIC. Initialize the CAN bus, including setting the baud rate to 1M and the frame type to extended frame. Enable CAN bus interrupts. Set the mask value (CAN_Mask) of the CAN device to 0x1FE00000 (all high 8 bits are set to 1), set the filter value (CAN_Filter) of the CAN device to SRCID, and set the frame type to data frame.

[0025] Define the ping-pong buffer Rx_CAN_Buffer, which is a two-dimensional array of unsigned character type. The number of rows in the array is the number of ping-pong buffers (set to 2), and the number of columns is the length of the buffer Buffer_Size (the length is designed to be 2096). At the same time, define Rx_CAN_Ptr, which is used to write the buffer data, and the initial value is set to 0. Define the current frame length Cur_Len, and the initial value is set to 0. The reception completion flag Recv_Flag, and the initial value is set to 0.

[0026] S3: In the CAN bus interrupt callback function CAN0_RX0_IRQHandler, define the variable RecvID, which is an unsigned integer type, the variable RecvDLC, which is an unsigned character type, and the variable RecvData, which is an unsigned character array type with a length of 8. Use the can_message_receive() function in the GD32 standard library to obtain the data received on the CAN bus, with the parameters CAN0 (indicating the first CAN) and CAN_FIFO0 (the first FIFO buffer). Assign the ID value of the acquired data frame to RecvID, the acquired data length to RecvDLC, and the acquired data values ​​up to 8 to RecvData.

[0027] Define the CAN message structure (CAN_RECV_DATA) variable CANData, assign the extended frame ID value in CANData to RecvID, assign the data length DLC to RecvDLC, determine the length of the array Data that needs to be assigned based on the data length DLC, and assign the value.

[0028] The data received by each interruption is stored in Rx_CAN_Buffer[i] in sequence. The initial value of i is 0. The value of i increases in sequence each time data is received, and the number of data stored in the buffer Rx_CAN_Ptr value is incremented by the data length DLC. The value of Rx_CAN_Ptr is updated.

[0029] S4: Define the current data length Cur_Len, set the type to unsigned short integer, define the receive completion flag Recv_Flag, and loop through the main function to determine the value of Rx_CAN_Ptr. When the length is greater than or equal to 8 bytes, take out the third and fourth bytes, which are short integer data, and assign this value to Cur_Len. Determine whether the value of Rx_CAN_Ptr is greater than or equal to the value of Cur_Len. If the condition is met, take out the current data, perform CRC check from the first byte to Cur_Len-2 bytes, and if successful, parse the YMODEM main body data; if not, discard this frame of data.

[0030] S5: In the main function, parse the received data and extract the data. After receiving, set the receiving completion flag Recv_Flag to 1. Use the flash read function ReadFlash() in the standard library to read the firmware information in the Flash and print it out. Use the flash write function WriteFlash() to write the updated firmware information to the Flash. Read the application startup address APP_ADDR_FLASH (the value set in this example is 0x8002000), and determine whether the highest bit of the data stored at the address is 1. If it is 1, it means that the firmware has been successfully written to the Flash.

[0031] S6: Define a function pointer pFunction, the parameter and return value types of the function pointed to by the pointer are both void, restore the default values ​​of the peripherals, wait for 3 seconds, obtain the value stored in the 4-byte address offset of the APP_ADDR_FLASH address and convert it to the pFunction type, use the stack pointer initialization function __set_MSP() function, and the parameter is APP_ADDR_FLASH to initialize the user's stack pointer. Run the entity pointed to by the pFunction function pointer to jump the program, complete the jump from the startup program to the application program, and complete the program upgrade.

[0032] 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 software upgrade method based on the domestic single-chip microcomputer GD32, It is characterized in that The following steps are involved: S1: Design the CAN bus communication application layer protocol, design the CAN message structure CAN_RECV_DATA in the data processing process, and configure the CAN bus 29-bit extended frame ID; S2: Initialize the MCU system, configure the clock information and interrupt information, configure the bus communication rate information, the mask value and filter value of the bus ID, and define the variables and ping-pong buffer used; S3: In the CAN bus interrupt callback function CAN0_RX0_IRQHandler, the received CAN bus data is stored in the ping-pong buffer in sequence; S4: parse the CAN bus data according to the CAN bus communication protocol defined in step 1, assign the ID value of the acquired data frame to the receiving frame identification variable RecvID, assign the acquired data length to the receiving frame length variable RecvDLC, and assign up to 8 acquired data values ​​to the receiving data pointer RecvData; S5: In the main function, the received CAN bus data is parsed, the parsed data is extracted, and the updated firmware information is written into the Flash using the write flash function WriteFlash(); the application startup address APP_ADDR_FLASH is read and it is determined whether the highest bit of the data stored at the address is 1. If it is 1, it means that the firmware has been successfully written into the Flash, indicating that the upgrade is successful; S6: Get the value stored in the 4-byte address offset of APP_ADDR_FLASH and convert it into a function pointer type, use the stack pointer initialization function set_MSP() function, and use the parameter APP_ADDR_FLASH to initialize the user's stack pointer; Run the entity pointed to by the pFunction function pointer to perform program jump, complete the jump from the startup program to the application program, and complete the program upgrade; In the step S1, the CAN bus communication application layer protocol includes a two-byte frame header, a two-byte frame length, a one-byte destination address, a one-byte source address, a one-byte total number of frames of the upgrade file, a one-byte current frame sequence number, 1K bytes of YMODEM protocol upgrade file content and a two-byte CRC check; the two bytes of the frame header are 0x55 and 0xAA respectively; In the step S1, the CAN message structure CAN_RECV_DATA includes ① an extended frame ID value ExtID, which is an unsigned long integer; ② a data length DLC, which is an unsigned short integer; ③ an array Data, which is 8 in length and is an unsigned char type; In the step S1, in the 29-bit extended frame ID of the CAN bus, 8 bits from the 14th to the 21st are used as the CAN bus source node domain, and 8 bits from the 22nd to the 29th are used as the CAN bus destination node domain; the CAN bus source node ID value is defined as SRCID, and the destination node ID value is DSTID, and the SRCID value and the DSTID value are assigned to the destination node domain and the source node domain of the CAN bus respectively; In the step S3, in the CAN bus interrupt callback function CAN0_RX0_IRQHandler, define a variable RecvID, whose type is an unsigned integer, define a variable RecvDLC, whose type is an unsigned character, and define a variable RecvData, whose type is an unsigned character array type and whose length is 8; use the can_message_receive() function in the GD32 standard library to obtain the data received on the CAN bus, and the parameters are CAN0 and CAN_FIFO0; assign the ID value of the obtained data frame to RecvID, assign the obtained data length to RecvDLC, and assign the obtained up to 8 data values ​​to RecvData; Define the CAN message structure CAN_RECV_DATA variable CANData, assign the extended frame ID value in CANData to RecvID, assign the data length DLC to RecvDLC, determine the length of the array Data to be assigned according to the data length DLC, and assign the value; The data received by each interrupt is stored in Rx_CAN_Buffer[i] in turn. The initial value of i is 0. The value of i increases each time data is received. The number of data stored in the buffer Rx_CAN_Ptr is incremented by the data length DLC, and the value of Rx_CAN_Ptr is updated.

2. The software upgrade method based on the domestic single-chip microcomputer GD32 as claimed in claim 1, It is characterized in that In the step S2, the initialization function SystemInit() is used to perform basic function initialization operations on the GD32 board. The configuration process includes using the system clock configuration function SetSysClock() function to initialize the system clock, enable the off-chip high-speed clock HSE, configure the clock division factor, turn on the phase-locked loop PLL clock, and configure the interrupt vector NVIC; initialize the CAN bus, including setting the baud rate to 1M and the frame type to the extended frame; enable the CAN bus interrupt, set the mask value CAN_Mask of the CAN device to 0x1FE00000, set the filter value CAN_Filter of the CAN device to SRCID, and set the frame type to the data frame; the clock division factor includes the AHB high-speed bus division factor, the APB1 low-speed bus division factor, and the APB2 low-speed bus division factor.

3. The software upgrade method based on the domestic single-chip microcomputer GD32 as claimed in claim 2, It is characterized in that In the step S2, the ping-pong buffer Rx_CAN_Buffer is a two-dimensional array of unsigned character type, the number of rows of the array is the number of ping-pong buffers, the number of columns is the length of the buffer Buffer_Size, and Rx_CAN_Ptr is defined as the location for writing buffer data, and the initial value is set to 0; the current frame length Cur_Len is defined, and the initial value is set to 0, and the reception completion flag Recv_Flag is set to 0.

4. The software upgrade method based on the domestic single-chip microcomputer GD32 as claimed in claim 3, It is characterized in that In the step S4, the current data length Cur_Len is defined, the type is set to unsigned short integer, the receiving completion flag Recv_Flag is defined, and the value of Rx_CAN_Ptr is cyclically determined in the main function. When the length is greater than or equal to 8 bytes, the third and fourth bytes are taken out, which are short integer data, and this value is assigned to Cur_Len; it is determined whether the value of Rx_CAN_Ptr is greater than or equal to the value of Cur_Len at this time. If the condition is met, the current data is taken out, and a CRC check is performed from the first byte to Cur_Len-2 bytes. If successful, the YMODEM main data is parsed; if not, this frame of data is discarded.

5. The software upgrade method based on the domestic single-chip microcomputer GD32 as claimed in claim 4, It is characterized in that In the step S5, the received data is parsed in the main function, the data is extracted, and after the reception is completed, the reception completion flag Recv_Flag is set to 1; the flash memory read function ReadFlash() in the standard library is used to read the firmware information in the Flash and print a prompt; the flash memory write function WriteFlash() is used to write the updated firmware information to the Flash; the application startup address APP_ADDR_FLASH is read, and it is determined whether the highest bit of the data stored at the address is 1. If it is 1, it means that the firmware has been successfully written to the Flash.

6. The software upgrade method based on the domestic single-chip microcomputer GD32 as claimed in claim 5, It is characterized in that In the step S6, a function pointer pFunction is defined, the parameter and return value types of the function pointed to by the pointer are both of the null type, the default value of the peripheral is restored, after waiting for 3 seconds, the value stored in the 4-byte address offset of the APP_ADDR_FLASH address is obtained and converted to the pFunction type, and the stack pointer initialization function __set_MSP() function is used, and the parameter is APP_ADDR_FLASH to initialize the user's stack pointer; Run the entity pointed to by the pFunction function pointer to perform program jump, complete the jump from the startup program to the application program, and complete the program upgrade.