Ymodem protocol description

 

    During the firmware burning process of the module, the firmware file is sent by the Term (sender), and the module (receiver) receives and writes the MCU. The data transmission protocol used during this period is the ymodem protocol.

I. Overview

    The ymodem protocol is a send-and-wait protocol, that is, after the sender sends a data packet, it must wait for the receiver's confirmation. If it is an ACK signal, continue to send new packets; if it is a NAK signal, then retransmit the packet or exit with an error.

Second, the firmware transfer process

    According to the protocol requirements, the firmware transfer process consists of 2 parts:

    1. Transfer the file name.

    2. Transfer files.

3. The process of receiving the file by the recipient

    The specific steps of the file receiving process are summarized as follows:

    1. The module (receiver) starts the transmission, that is, the receiver sends a capital letter 'C' to start the transmission and enters the waiting state.

    2. After Term (sender) receives 'C', it starts to send data packets in the following format:

    Start signal (SOH) + serial number (00H) + complement code (FFH) + file name + space + file size + fill 128 bytes (calculated from the file name) + CRC check (2 bytes)

    Then enter the waiting ACK state.

    3. After the module (receiver) receives the data package, it verifies that the "serial number (00H)" and "complement code (FFH)" in the package are indeed complements of each other, then the reception is considered correct, and the "file name" and "file name" are extracted. "File size", otherwise enter error processing.

    If the "file size" meets the MCU requirements, ACK is sent. If the "file size" is larger than the MCU requirement, send 2 'CA's to terminate the transfer.

    4. At this point, the file transfer process completes the transfer of the file name, and then the file transfer is officially started. Likewise, 'C' is sent by the module (receiver) to start the transmission.

    5. After Term (sender) receives 'C', it starts to send data packets in the following format:

    Start signal (SOH) + serial number (01H) + complement code (FEH) + 128 bytes of data + CRC check (2 bytes)

    or

    Start signal (STX) + serial number (01H) + complement code (FEH) + 1024 bytes of data + CRC check (2 bytes)

    The above two formats can be arbitrarily selected. Among them, the "sequence number" is accumulated packet by packet according to the number of data packets, which is determined by the sender. Each time a packet is sent, the sender waits for an ACK response.

    6. After the module (receiver) receives a packet of data, it also verifies that the "serial number" and the "complement code" in the packet are indeed complements of each other, and it is considered that the reception is correct, otherwise it will enter into error processing.

    7. If the module (receiver) finds that the "sequence number" in the received data packet is abnormal, that is, discontinuous or jumps, it will send a NAK response.

    8. After the module (receiver) confirms that the data packet is correct, it will write the packet data to the MCU, then send ACK, and wait for the next packet of data to be received. If there is an error in the process of writing data to the MCU, the module sends 2 'CA' to terminate the transfer.

    9. Term (sender) sends an 'EOT' signal when all files are transferred.

    10. After the module (receiver) receives 'EOT', it also responds with ACK.

    11. After the Term (sender) receives the ACK, it sends a packet with 128 0s in the format as follows:

    Start signal (SOH) + serial number + complement + 128 0 + CRC check (2 bytes)

    12. After the module receives it, it responds with ACK to complete the entire transmission process.

Fourth, the sender sends the file process

    Since the file transfer process is initiated by the receiver, the sender needs to give a sending request in advance. The operations are as follows:

    First, the control module runs the booloader program, and then Term sends '1', which is to inform the module that the firmware is about to be downloaded to it. After the module receives the notification, it will respond to 'C' to start the transmission.

    The specific steps of the file sending process are summarized as follows:

    1. After Term (sender) receives 'C', it packs the file name and file size into a packet of data and sends it in the following format:

    Start signal (SOH) + serial number (00H) + complement code (FFH) + file name + space + file size + fill 128 bytes (calculated from the file name) + CRC check (2 bytes)

    After sending, Term waits for the receiver to reply.

    2. After the module (receiver) receives the data packet, it will send an ACK response if the verification is valid, and continue to send 'C' to start the file transfer.

    3. After the Term (receiver) receives the ACK, it waits to receive 'C' to start sending packets. Depending on the settings, the following 2 formats can be selected:

    Start signal (SOH) + serial number (01H) + complement code (FEH) + 128 bytes of data + CRC check (2 bytes)

    or

    Start signal (STX) + serial number (01H) + complement code (FEH) + 1024 bytes of data + CRC check (2 bytes)

    Among them, the "serial number" is accumulated one by one according to the number of data packets, which is determined by the sender. Each time a packet is sent, the sender waits for an ACK response.

    4. When all the file data is sent, Term (sender) will send 'EOT', indicating that the file sending is complete.

    5. After the module (receiver) receives 'EOT', it returns an ACK response.

    6. After the Term (sender) receives the ACK response, it will send a packet of data with all 0s. The format is as follows:

    Start signal (SOH) + serial number + complement + 128 0 + CRC check (2 bytes)

    7. The module (receiver) returns an ACK response to end the entire transmission process.

5. Description

    1. It can be seen from the steps of the transmission process that some operations are not strictly performed according to the requirements of the ymodem protocol. Among them: After the module (receiver) receives a packet of data, it does not verify whether the CRC is correct, but confirms whether the packet data is valid by verifying whether the mathematical relationship between "serial number" and "complement" is correct.

    2. During the firmware transmission process, once an error occurs (data transmission error, "serial number" error, calculation error between "serial number" and "complement code", etc.), the module (receiver) will restart the firmware transmission process, that is, resend the 'C '.

    3. If the sequence number in the data packet received by the module (receiver) is incorrect (discontinuous), it will respond to NAK, and the Term (sender) will resend the packet after receiving the NAK. If the sender receives NAK several times in a row, the transmission is terminated.

    4. During the firmware transfer process, if the module (receiver) has errors several times in a row, the receiver will send 2 'CA's to terminate the transfer. After the Term (sender) receives the 'CA', it enters the termination transmission process, that is, it also sends two 'CA's and ends.

6. Use of symbols

    #define SOH (0x01) // 128-byte packet start flag

    #define STX (0x02) // 1024 byte packet start flag

    #define EOT (0x04) // File transfer end flag

    #define ACK    (0x06)

    #define NAK (0x15)

    #define CA     (0x18)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325140488&siteId=291194637