CRC Computations(3)

NAME
CRC Computations -

Detailed Description

#include <avr/crc16.h>

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

References:.RS 4

.RS 4

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.

.RS 4

Jack Crenshaw's 'Impementing 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.

  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)

Function Documentation

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

Optimized CRC-16 calcutation.

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

This CRC is normally used in disk-drive controllers.

  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.

    uint16_t
    crc_ccitt_update (uint16_t crc, uint8_t data)
    {
        data ^= lo8 (crc);
        data ^= data << 4;
        return ((((uint16_t)data << 8) | hi8 (crc)) ^ (uint8_t)(data >> 4)
                ^ ((uint16_t)data << 3));
    }

  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.

    uint16_t
    crc_xmodem_update (uint16_t crc, uint8_t data)
    {
        int i;
        crc = crc ^ ((uint16_t)data << 8);
        for (i=0; i<8; i++)
        {
            if (crc & 0x8000)
                crc = (crc << 1) ^ 0x1021;
            else
                crc <<= 1;
        }
        return crc;
    }