
AVR350: Xmodem CRC Receive Utility for AVR
®
Features
• Programmable Baud Rate
• Half Duplex
• 128-byte Data Packets
• CRC Data Verification
• Framing Error Detection
• OverRun Detection
• Less than 1K Bytes of Code Space
• C High-level Language Code
1 Introduction
The Xmodem protocol was created years ago as a simple means of having two
computers talk to each other. With its half-duplex mode of operation, 128-byte
packets, ACK/NACK responses and CRC data checking, the Xmodem protocol has
found its way into many applications. In fact most communication packages found
in the PC today have a Xmodem protocol available to the user.
8-bit
Microcontrollers
Application Note
Rev. 1472D-AVR-01/08

2 Theory of Operation
Xmodem is a half-duplex communication protocol. The Receiver, after receiving a
packet, will either acknowledge (ACK) or not acknowledge (NACK) the packet. The
original Xmodem protocol used a standard checksum method to verify the 128-byte
data packet. The CRC extension to the original protocol uses a more robust 16-bit
CRC to validate the data block and is used here. Xmodem can be considered to be
receiver driven. That is, the Receiver sends an initial character “C” to the sender
indicating that it’s ready to receive data in CRC mode. The Sender then sends a 133byte packet, the Receiver validates it and responds with an ACK or a NACK at which
time the sender will either send the next packet or re-send the last packet. This
process is continued until an EOT is received at the Receiver side and is properly
ACKed to the Sender. After the initial handshake the receiver controls the flow of data
through ACKing and NACKing the Sender.
3 Definitions
Table 2-1
Byte 1 Byte 2 Byte 3 Bytes 4 - 131 Bytes 132 - 133
Start of Header Packet Number ~(Packet Number) Packet Data 16 bit CRC
The following defines are used for protocol flow control.
Table 3-1
Symbol Description Value
SOH Start of Header 0x01
EOT End of Transmission 0x04
ACK Acknowledge 0x06
NACK Not Acknowledge 0x15
C ASCII “C” 0x43
Byte one of the XmodemCRC packet can only have a value of SOH or EOT, anything
else is an error. Bytes two and three form a packet number with checksum, add the
two bytes together and they should always equal 0xff. Please note that the packet
number starts out at “1” and rolls over to “0” if there are more than 255 packets to be
received. Bytes 4 - 131 form the data packet and can be anything. Bytes 132 and 133
form the 16-bit CRC. The high byte of the CRC is located in byte 132.
. XmodemCRC Packet Format
. Protocol Flow Control
4 Synchronization
2
AVR350
The Receiver starts by sending an ASCII “C” (0x43) character to the sender indicating
it wishes to use the CRC method of block validating. After sending the initial “C” the
receiver waits for either a three second time out or until a buffer full flag is set. If the
receiver is timed out then another “C” is sent to the sender and the three second time
out starts again. This process continues until the receiver receives a complete 133byte packet.
1472D-AVR-01/08

5 Receiver Considerations
This protocol NACKs the following conditions:
1. Framing error on any byte
2. OverRun error on any byte
3. CRC error
4. Receiver timed out (didn't receive packet within one second)
On any NACK, the sender will re-transmit the last packet. Items one and two should
be considered serious hardwar e fa ilu res. Verify that sender and receiver are usin g the
samebaud rate, start bits and stop bits. Item three is found in noisy environments, and
the last issue should be self-correcting after the receiver NACKs the sender.
6 Data Flow Diagram
The data flow diagram below simulates a 5-packet file being sent.
AVR350
Table 6-1
Sender Receiver
<--- “C”
Times Out after Three Seconds
<--- “C”
SOH 0x01 0xFE Data CRC ---> Packet OK
<--- ACK
SOH 0x02 0xFD Data CRC ---> (Line Hit during Data Transmission)
<--- NACK
SOH 0x02 0xFD Data CRC ---> Packet OK
SOH 0x03 0xFC Data CRC ---> Packet OK
SOH 0x03 0xFC Data CRC ---> Duplicate Packet
<--- ACK
SOH 0x04 0xFB Data CRC ---> (UART Framing Error on Any Byte)
<--- NACK
SOH 0x04 0xFB Data CRC ---> Packet OK
<--- ACK
SOH 0x05 0xFA Data CRC ---> (UART Overrun Error on Any Byte)
<--- NACK
SOH 0x05 0xFA Data CRC ---> Packet OK
<--- ACK
. Data Flow Diagram
<--- ACK
(ACK Gets Garbled) <--- ACK
EOT ---> Packet OK
(ACK Gets Garbled) <--- ACK
EOT ---> Packet OK
Finished <--- ACK
1472D-AVR-01/08
3