// The CRC parameters. Currently configured for CCITT.
// Simply modify these to switch to another CRC standard.
// The width of the CRC calculation and result.
// Modify the typedef for an 8 or 32-bit CRC standard.
// An array containing the pre-computed intermediate result for each possible byte of input.
// This is used to speed up the computation.
#include <stdio.h>
/***************************************************************************
* Function : crcInit()
*
* Description : Initialize the CRC lookup table. This table is used by
crcCompute() to make CRC computation faster.
* Notes : The mod-2 binary long division is implemented here.
* Returns : None defined.
***************************************************************************/
void crcInit()
{
width wRemainder = 0;
width wDividend = 0;
long int nBit = 0;
// Perform binary long division, a bit at a time.
for( wDividend = 0; wDividend < 256; wDividend++ )
{
// Initialize the remainder.
wRemainder = wDividend <<( WIDTH -8 );
// Shift and XOR with the polynomial.
for( nBit = 0; nBit < 8; nBit++ )
{
// Try to divide the current data bit.
if( wRemainder & TOPBIT ) wRemainder = ( wRemainder <<1 ) ^POLYNOMIAL;
else wRemainder = wRemainder <<1;
}
// Save the result in the table.
g_wCrcTable[wDividend] = wRemainder;
}
return;
}
/***************************************************************************
* Function : crcCompute()
*
* Description : Compute the CRC checksum of a binary message block.
* Notes : This function expects that crcInit() has been called
* first to initialize the CRC lookup table.
* Returns : The CRC of the data.
***************************************************************************/
width crcCompute( unsigned char *szMessage, unsigned int nBytes )
{
unsigned int nOffSet = 0;
unsigned char cByte = 0;
width wRemainder = INITIAL_REMAINDER;
// Divide the message by the polynomial, a byte at time.
for( nOffSet = 0; nOffSet < nBytes; nOffSet++ )
{
cByte = ( wRemainder >>( WIDTH -8 ) ) ^szMessage[nOffSet];
wRemainder = g_wCrcTable[cByte] ^( wRemainder <<8 );
}
// The final remainder is the CRC result.
return ( wRemainder ^FINAL_XOR_VALUE );
}
void main()
{
// ============================= TEST START ===================================
unsigned char cData = 11;
width wData = 0;
long int nDataSize = sizeof( cData );
printf( "=============== TEST CRC ================ \n\n" );
printf( " TEST cData = %d \n wData = %d \n cDataSize = %d \n\n", cData, wData, nDataSize );
crcInit();
wData = crcCompute( &cData, nDataSize );
printf( "crcCompute : \n");
printf( " TEST cData = %d \n wData = %d \n cDataSize = %d \n\n", cData, wData, nDataSize );
// ============================= TEST END ===================================
// ============================= TEST2 START ==================== ===============
unsigned char cData2 = 10;
width wData2 = 0;
long int nDataSize2 = sizeof( cData2 );
printf( "=============== TEST2 CRC ================ \n\n" );
printf( " TEST cData2 = %d \n wData2 = %d \n cDataSize2 = %d \n\n", cData2, wData2, nDataSize2 );
crcInit();
wData2 = crcCompute( &cData2, nDataSize2 );
printf( "crcCompute : \n");
printf( " TEST2 cData2 = %d \n wData2 = %d \n cDataSize2 = %d \n\n", cData2, wData2, nDataSize2 );
// ============================= TEST2 END ===================================
}