AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page User Manual Reference FAQ Example Projects
Functions

<util/crc16.h>: CRC Computations

Functions

static __inline__ uint16_t _crc16_update (uint16_t __crc, uint8_t __data)
static __inline__ uint16_t _crc_xmodem_update (uint16_t __crc, uint8_t __data)
static __inline__ uint16_t _crc_ccitt_update (uint16_t __crc, uint8_t __data)
static __inline__ uint8_t _crc_ibutton_update (uint8_t __crc, uint8_t __data)
static __inline__ uint8_t _crc8_ccitt_update (uint8_t __crc, uint8_t __data)

Detailed Description

#include <util/crc16.h>

This header file provides a optimized inline functions for calculating cyclic redundancy checks (CRC) using common polynomials.

References:

See the Dallas Semiconductor app note 27 for 8051 assembler example and general CRC optimization suggestions. The table on the last page of the app note is the key to understanding these implementations.

Jack Crenshaw's "Implementing CRCs" article in the January 1992 isue of Embedded Systems Programming. This may be difficult to find, but it explains CRC's in very clear and concise terms. Well worth the effort to obtain a copy.

A typical application would look like:

// Dallas iButton test vector.
uint8_t serno[] = { 0x02, 0x1c, 0xb8, 0x01, 0, 0, 0, 0xa2 };

int checkcrc(void) { uint8_t crc = 0, i;

for (i = 0; i < sizeof serno / sizeof serno[0]; i++) crc = _crc_ibutton_update(crc, serno[i]);

return crc; // must be 0 }

Function Documentation

static __inline__ uint16_t _crc16_update ( uint16_t __crc,
uint8_t __data
)
static

Optimized CRC-16 calculation.

Polynomial: x^16 + x^15 + x^2 + 1 (0xa001)
Initial value: 0xffff

This CRC is normally used in disk-drive controllers.

The following is the equivalent functionality written in C.

011 uint16_t 022 crc16_update(uint16_t crc, uint8_t a) 033{ 044 int i; 055 066 crc ^= a; 077 for (i = 0; i < 8; ++i) 088{ 099if (crc & 1) 1010crc = (crc >> 1) ^ 0xA001; 1111else 1212crc = (crc >> 1); 1313} 1414 1515 return crc; 1616}
static __inline__ uint8_t _crc8_ccitt_update ( uint8_t __crc,
uint8_t __data
)
static

Optimized CRC-8-CCITT calculation.

Polynomial: x^8 + x^2 + x + 1 (0xE0)

For use with simple CRC-8
Initial value: 0x0

For use with CRC-8-ROHC
Initial value: 0xff
Reference: http://tools.ietf.org/html/rfc3095#section-5.9.1

For use with CRC-8-ATM/ITU
Initial value: 0xff
Final XOR value: 0x55
Reference: http://www.itu.int/rec/T-REC-I.432.1-199902-I/en

The C equivalent has been originally written by Dave Hylands. Assembly code is based on _crc_ibutton_update optimization.

The following is the equivalent functionality written in C.

011 uint8_t 022_crc8_ccitt_update (uint8_t inCrc, uint8_t inData) 033{ 044uint8_t i; 055uint8_t data; 066 077data = inCrc ^ inData; 088 099for ( i = 0; i < 8; i++ ) 1010{ 1111if (( data & 0x80 ) != 0 ) 1212{ 1313data <<= 1; 1414data ^= 0x07; 1515} 1616else 1717{ 1818data <<= 1; 1919} 2020} 2121return data; 2222}
static __inline__ uint16_t _crc_ccitt_update ( uint16_t __crc,
uint8_t __data
)
static

Optimized CRC-CCITT calculation.

Polynomial: x^16 + x^12 + x^5 + 1 (0x8408)
Initial value: 0xffff

This is the CRC used by PPP and IrDA.

See RFC1171 (PPP protocol) and IrDA IrLAP 1.1

Note
Although the CCITT polynomial is the same as that used by the Xmodem protocol, they are quite different. The difference is in how the bits are shifted through the alorgithm. Xmodem shifts the MSB of the CRC and the input first, while CCITT shifts the LSB of the CRC and the input first.

The following is the equivalent functionality written in C.

011 uint16_t 022 crc_ccitt_update (uint16_t crc, uint8_t data) 033{ 044data ^= lo8 (crc); 055data ^= data << 4; 066 077return ((((uint16_t)data << 8) | hi8 (crc)) ^ (uint8_t)(data >> 4) 088^ ((uint16_t)data << 3)); 099}
static __inline__ uint8_t _crc_ibutton_update ( uint8_t __crc,
uint8_t __data
)
static

Optimized Dallas (now Maxim) iButton 8-bit CRC calculation.

Polynomial: x^8 + x^5 + x^4 + 1 (0x8C)
Initial value: 0x0

See http://www.maxim-ic.com/appnotes.cfm/appnote_number/27

The following is the equivalent functionality written in C.

011 uint8_t 022_crc_ibutton_update(uint8_t crc, uint8_t data) 033{ 044 uint8_t i; 055 066 crc = crc ^ data; 077 for (i = 0; i < 8; i++) 088{ 099if (crc & 0x01) 1010crc = (crc >> 1) ^ 0x8C; 1111else 1212crc >> = 1; 1313} 1414 1515 return crc; 1616}
static __inline__ uint16_t _crc_xmodem_update ( uint16_t __crc,
uint8_t __data
)
static

Optimized CRC-XMODEM calculation.

Polynomial: x^16 + x^12 + x^5 + 1 (0x1021)
Initial value: 0x0

This is the CRC used by the Xmodem-CRC protocol.

The following is the equivalent functionality written in C.

011 uint16_t 022 crc_xmodem_update (uint16_t crc, uint8_t data) 033{ 044int i; 055 066crc = crc ^ ((uint16_t)data << 8); 077for (i=0; i<8; i++) 088{ 099if (crc & 0x8000) 1010crc = (crc << 1) ^ 0x1021; 1111else 1212crc <<= 1; 1313} 1414 1515return crc; 1616}