An input/output controller for virtual pinball machines, with plunger position tracking, accelerometer-based nudge sensing, button input encoding, and feedback device control.

Dependencies:   USBDevice mbed FastAnalogIn FastIO FastPWM SimpleDMA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers crc32.cpp Source File

crc32.cpp

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