⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smallcrc16.c

📁 ti-Chipcon CC1010 1G以下Soc源码库。包括rf,powermodes,clockmodes,flashRW,interrupts,timer,pwm,uart...所有底层驱动源码
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                            CHIPCON CC1010                    *
 *      ***   + +   ***                     CUL - smallcrc                   *
 *      ***   +++   ***                                                      *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 *                                                                           *
 *****************************************************************************
 * Author:              JOL                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 *                                                                           *
 * $Log: smallcrc16.c,v $
 * Revision 1.1  2002/10/14 12:26:12  tos
 * Initial version in CVS.
 *
 *                                                                           *
 ****************************************************************************/


#include <chipcon/cul.h>


//----------------------------------------------------------------------------
//	word culSmallCRC16(...)
//
//	Description:
//		A CRC-16/CCITT 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 CRC16_INIT should be given for _crcReg_. The value returned
//		is the CRC-16 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-16 operation on.
//		word crcReg
//			The current value of the CRC register. For the first byte the
//			value CRC16_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 CRC16 register. This corresponds to the 
//		CRC-16 of the data supplied so far. During CRC checking, after working
//		through all the data and the appended CRC-16 value, the value will be
//		0 if the data is intact.
//----------------------------------------------------------------------------
word culSmallCRC16(byte crcData, word crcReg) {
	byte i;

	for (i = 0; i < 8; i++) {
		if (((crcReg & 0x8000) >> 8) ^ (crcData & 0x80))
			crcReg = (crcReg << 1) ^ CRC16_POLY;
		else
			crcReg = (crcReg << 1);
		crcData <<= 1;
	}
	return crcReg;
} // culSmallCRC16




//----------------------------------------------------------------------------
//  word culSmallCRC16Block(...)
//
//	Description:
//	    A CRC-16/CCITT implementation optimized for small code size on blocks
//      of data. For the invocation on the first and/or only block the value
//      CRC16_INIT should be given for _crcReg_. The value returned
//		is the CRC-16 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.
//      It is important that the CRC value is appended to the data in BIG
//      ENDIAN byte order. (Use the CRC16Append(...) function for this.)
//
//	Arguments:
//		byte* crcData
//			A pointer to the block of data to perform the CRC-16 operation on.
//      lword length
//          The number of bytes in this block.
//		word crcReg
//			The current value of the CRC register. For the first block the
//			value CRC16_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 CRC16 register. This corresponds to the 
//		CRC-16 of the data supplied so far. During CRC checking, after working
//		through all the data and the appended CRC-16 value, the value will be
//		0 if the data is intact.
//----------------------------------------------------------------------------
word culSmallCRC16Block(byte* crcData, word length, word crcReg) {
    word j;
	byte i, dataByte;

    for (j = 0; j < length; j++) {
        dataByte = *crcData++;
	    for (i = 0; i < 8; i++) {
		    if (((crcReg & 0x8000) >> 8) ^ (dataByte & 0x80))
			    crcReg = (crcReg << 1) ^ CRC16_POLY;
		    else
			    crcReg = (crcReg << 1);
		    dataByte <<= 1;
	    }
    }

    return crcReg;
} // culSmallCRC16Block




//----------------------------------------------------------------------------
//  void culCRC16Append(...)
//
//	Description:
//	    Appends a CRC value to a block of data in BIG ENDIAN byte order.
//
//	Arguments:
//		byte* dataPtr
//			A pointer to where to insert the CRC value.
//		word crcValue
//			The CRC value to insert.
//
//	Return value:
//      void
//----------------------------------------------------------------------------
void culCRC16Append(byte* dataPtr, word crcValue) {
    dataPtr[0] = (byte)(crcValue >> 8);
    dataPtr[1] = (byte)(crcValue & 0xFF);
} // culCRC16Append

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -