latest BAE code 10 dec

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of RAJANGAM_REVIEW_BAE_CODE by Team Fox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers crc.h Source File

crc.h

00001 //EDITS
00002 //changed the initial remainder from 0x0000 to 0xffff according to the standards
00003 //made two seperate functions crc16_gen and crc8_gen
00004 
00005 #define TOPBIT16 (1 << 15)
00006 #define TOPBIT8 (1 << 7)
00007 #define POLYNOMIAL16 0x1021
00008 #define POLYNOMIAL8 0xEA
00009 
00010 namespace CRC
00011 {
00012      typedef uint16_t crctype16; 
00013     crctype16 crc16_gen(const unsigned char message[], unsigned int nBytes)
00014         {
00015             crctype16 remainder = 0xffff;
00016             int byte;
00017             char bit;
00018         
00019             for( byte = 0 ; byte < nBytes ; byte++ )
00020                 {
00021                     /*
00022                     Bring the data byte by byte
00023                     each time only one byte is brought
00024                     0 xor x = x
00025                     */
00026                     remainder = remainder ^ ( message[byte] << 8 );
00027             
00028                     for( bit = 8 ; bit > 0 ; bit--)
00029                         {
00030                             /*
00031                             for each bit, xor the remainder with polynomial
00032                             if the MSB is 1
00033                             */
00034                             if(remainder & TOPBIT16)
00035                                 {
00036                                     remainder = (remainder << 1) ^ POLYNOMIAL16;
00037                                     /*
00038                                     each time the remainder is xor-ed with polynomial, the MSB is made zero
00039                                     hence the first digit of the remainder is ignored in the loop
00040                                     */
00041                                 }
00042                             else
00043                                 {
00044                                     remainder = (remainder << 1);
00045                                 }
00046                         }           
00047                 }
00048         
00049             return remainder;
00050         }
00051 
00052   
00053     typedef uint8_t crctype8;
00054     crctype8 crc8_gen(const unsigned char message[], unsigned int nBytes)
00055         {
00056             crctype8 remainder = 0xff;
00057         
00058             for(int byte = 0 ; byte < nBytes ; byte++ )
00059                 {
00060                     /*
00061                     Bring the data byte by byte
00062                     each time only one byte is brought
00063                     0 xor x = x
00064                     */
00065                     remainder = remainder ^ ( message[byte] );
00066             
00067                     for(int bit = 8 ; bit > 0 ; bit--)
00068                         {
00069                             /*
00070                             for each bit, xor the remainder with polynomial
00071                             if the MSB is 1
00072                             */
00073                             if(remainder & TOPBIT8)
00074                                 {
00075                                     remainder = (remainder << 1) ^ POLYNOMIAL8;
00076                                     /*
00077                                     each time the remainder is xor-ed with polynomial, the MSB is made zero
00078                                     hence the first digit of the remainder is ignored in the loop
00079                                     */
00080                                 }
00081                             else
00082                                 {
00083                                     remainder = (remainder << 1);
00084                                 }
00085                         }
00086                 }
00087         
00088             return remainder;
00089         }
00090 }