Collection of different CRC calculations

Dependents:   Waldo_Embed_V2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers crc.cpp Source File

crc.cpp

00001 
00002 #define CRC32_POLYNOMIAL 0xEDB88320L
00003 
00004 void CRC32Value(unsigned long &CRC, unsigned char c)
00005 {
00006     /////////////////////////////////////////////////////////////////////////////////////
00007     //CRC must be initialized as zero 
00008     //c is a character from the sequence that is used to form the CRC
00009     //this code is a modification of the code from the Novatel OEM615 specification
00010     /////////////////////////////////////////////////////////////////////////////////////
00011     unsigned long ulTemp1 = ( CRC >> 8 ) & 0x00FFFFFFL;
00012     unsigned long ulCRC = ((int) CRC ^ c ) & 0xff ;
00013     for (int  j = 8 ; j > 0; j-- )
00014     {
00015         if ( ulCRC & 1 )
00016         {
00017             ulCRC = ( ulCRC >> 1 ) ^ CRC32_POLYNOMIAL;
00018         }
00019         else
00020         {
00021             ulCRC >>= 1;
00022         }
00023     }
00024     CRC = ulTemp1 ^ ulCRC;
00025 } 
00026 
00027 /* --------------------------------------------------------------------------
00028 Calculates the CRC-32 of a block of data all at once
00029 //the CRC is from the complete message (header plus data) 
00030 //but excluding (of course) the CRC at the end
00031 -------------------------------------------------------------------------- */
00032 unsigned long CalculateBlockCRC32(
00033         unsigned long ulCount,    /* Number of bytes in the data block */
00034         unsigned char *ucBuffer ) /* Data block */
00035 {
00036     //////////////////////////////////////////////////////////////////////
00037     //the below code tests the CRC32Value procedure used in a markov form
00038     //////////////////////////////////////////////////////////////////////
00039     unsigned long CRC = 0;
00040     for (int i = 0; i<ulCount; i++)
00041     {
00042         CRC32Value( CRC, *ucBuffer++ );
00043     }
00044     return  CRC;
00045 }
00046 
00047 /*
00048 unsigned long CalculateBlockCRC32(
00049         unsigned long ulCount, 
00050         unsigned char *ucBuffer )
00051 {
00052 ////////////////////////////////////////////
00053 //original code from the OEM615 manual
00054 ////////////////////////////////////////////
00055     unsigned long ulTemp1;
00056     unsigned long ulTemp2;
00057     unsigned long ulCRC = 0;
00058     while ( ulCount-- != 0 )
00059     {
00060         ulTemp1 = ( ulCRC >> 8 ) & 0x00FFFFFFL;
00061         ulTemp2 = CRC32Value( ((int) ulCRC ^ *ucBuffer++ ) & 0xff );
00062         ulCRC = ulTemp1 ^ ulTemp2;
00063     }
00064     return( ulCRC );
00065 }
00066 */