AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page FAQ Library Reference Additional Documentation Example Projects

<util/crc16.h>: CRC 計算


Detailed Description

#include <util/crc16.h>

このヘッダファイルは一般的な多項式を用いて16bit-CRC(cyclic edundary checks)を計算する最適化されたインライン関数を提供します。

References:
Dallas Semiconductor app note 27にある、8051アセンブラ用のCRC最適化のためのsuggestionをお読みください。最終ページの表はこれらの実装を理解するキーになります。
Jack Crenshawの文献 "Impementing CRCs" (January 1992 isue of Embedded Systems Programming).
これは入手困難かもしれませんが、CRCを大変明快に平易な言葉で説明しています。コピーを得るために骨を折るだけの価値があります。

典型的な例は以下のようになります。

    // 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
    }

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)

Function Documentation

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

最適化された CRC-16 計算.

多項式: x^16 + x^15 + x^2 + 1 (0xa001)
初期値: 0xffff

このCRCは通常ディスクドライブのコントローラに用いられます。

static __inline__ uint16_t _crc_ccitt_update uint16_t  __crc,
uint8_t  __data
[static]
 

最適化された CRC-CCITT 計算..

多項式: x^16 + x^12 + x^5 + 1 (0x8408)
初期値: 0xffff

これはPPPやIrDA.で使われるCRCです。

RFC1171 (PPP protocol) 、IrDA IrLAP 1.1 も参照ください。

Note:
CCITT 多項式はXmodemプロトコルで使われているものとほぼ同じです。違いはアルゴリズム中でのビットのシフトの仕方だけです。XmodemはMSBをシフトしinput firstですが、CCITTはLSBをシフトし、input firstです。
以下は、同等のものを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__ uint8_t _crc_ibutton_update uint8_t  __crc,
uint8_t  __data
[static]
 

最適化された Dallas (now Maxim) iButton 8-bit CRC 計算.

多項式: x^8 + x^5 + x^4 + 1 (0x8C)
I初期値: 0x0

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

以下は、同等のものをCで書いたものです。

    uint8_t
    _crc_ibutton_update(uint8_t crc, uint8_t data)
    {
        uint8_t i;

        crc = crc ^ data;
        for (i = 0; i < 8; i++)
        {
            if (crc & 0x01)
                crc = (crc >> 1) ^ 0x8C;
            else
                crc >>= 1;
        }

        return crc;
    }

static __inline__ uint16_t _crc_xmodem_update uint16_t  __crc,
uint8_t  __data
[static]
 

最適化された CRC-XMODEM 計算.

多項式: x^16 + x^12 + x^5 + 1 (0x1021)
I初期値: 0x0

これはXmodem-CRC プロトコルで使われているCRCです.

以下は、同等のものを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;
    }


Automatically generated by Doxygen 1.4.1 on 23 Jan 2006.