📄 smallcrc8.c
字号:
/*****************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC1010 *
* *** + + *** CUL - smallcrc *
* *** +++ *** *
* *** *** *
* *********** *
* ********* *
* *
*****************************************************************************
* *
*****************************************************************************
* Author: JOL *
*****************************************************************************
* Revision history: *
* *
* $Log: smallcrc8.c,v $
* Revision 1.1 2002/10/14 12:26:12 tos
* Initial version in CVS.
*
* *
****************************************************************************/
#include <chipcon/cul.h>
//----------------------------------------------------------------------------
// byte culSmallCRC8(...)
//
// Description:
// A CRC-8 (DOW) implementation optimized for small code size.
// The function should be called once for each byte in the data
// the CRC is to be performed on. For the invocation on the first byte
// the value CRC8_INIT should be given for _crcReg_. The value returned
// is the CRC-8 of the data supplied so far. This CRC-value should be
// added at the end of the data to facilitate a later CRC check. During
// checking the check should be performed on all the data AND the CRC-16
// value appended to it. The data is intact if the value returned is 0.
//
// Arguments:
// byte crcData
// The data to perform the CRC-8 operation on.
// byte crcReg
// The current value of the CRC register. For the first byte the
// value CRC8_INIT should be supplied. For each additional byte the
// value returned for the last invocation should be supplied.
//
// Return value:
// The updated value of the CRC8 register. This corresponds to the
// CRC-8 of the data supplied so far. During CRC checking, after working
// through all the data and the appended CRC-8 value, the value will be
// 0 if the data is intact.
//----------------------------------------------------------------------------
byte culSmallCRC8(byte crcData, byte crcReg) {
byte i;
for (i = 0; i < 8; i++) {
if ((crcReg & 0x80) ^ (crcData & 0x80))
crcReg = (crcReg << 1) ^ CRC8_POLY;
else
crcReg = (crcReg << 1);
crcData <<= 1;
}
return crcReg;
} // culSmallCRC8
//----------------------------------------------------------------------------
// byte culSmallCRC8Block(...)
//
// Description:
// A CRC-8 (DOW) implementation optimized for small code size on blocks
// of data. For the invocation on the first and/or only block the value
// CRC8_INIT should be given for _crcReg_. The value returned
// is the CRC-8 of the data supplied so far. This CRC-value should be
// added at the end of the data to facilitate a later CRC check. During
// checking the check should be performed on all the data AND the CRC-8
// value appended to it. The data is intact if the value returned is 0.
//
// Arguments:
// byte* crcData
// A pointer to the block of data to perform the CRC-8 operation on.
// word length
// The number of bytes in this block.
// byte crcReg
// The current value of the CRC register. For the first block the
// value CRC8_INIT should be supplied. For each additional block the
// value returned for the last invocation should be supplied.
//
// Return value:
// The updated value of the CRC8 register. This corresponds to the
// CRC-8 of the data supplied so far. During CRC checking, after working
// through all the data and the appended CRC-8 value, the value will be
// 0 if the data is intact.
//----------------------------------------------------------------------------
byte culSmallCRC8Block(byte* crcData, word length, byte crcReg) {
word j;
byte i, dataByte;
for (j = 0; j < length; j++) {
dataByte = *crcData++;
for (i = 0; i < 8; i++) {
if ((crcReg & 0x80) ^ (dataByte & 0x80))
crcReg = (crcReg << 1) ^ CRC8_POLY;
else
crcReg = (crcReg << 1);
dataByte <<= 1;
}
}
return crcReg;
} // culSmallCRC8Block
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -