cdms_update

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of CDMS_SD_MNG_OVERDRIVE by saikiran cholleti

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     typedef uint16_t crctype16; 
00012     crctype16 crc16_gen(const unsigned char message[], unsigned int nBytes){
00013         crctype16 remainder = 0xffff;
00014         int byte;
00015         char bit;
00016         
00017         for( byte = 0 ; byte < nBytes ; byte++ ){
00018             /*
00019             Bring the data byte by byte
00020             each time only one byte is brought
00021             0 xor x = x
00022             */
00023             remainder = remainder ^ ( message[byte] << 8 );
00024             
00025             for( bit = 8 ; bit > 0 ; bit--){
00026                 /*
00027                 for each bit, xor the remainder with polynomial
00028                 if the MSB is 1
00029                 */
00030                 if(remainder & TOPBIT16){
00031                     remainder = (remainder << 1) ^ POLYNOMIAL16;
00032                     /*
00033                     each time the remainder is xor-ed with polynomial, the MSB is made zero
00034                     hence the first digit of the remainder is ignored in the loop
00035                     */
00036                 }
00037                 else{
00038                     remainder = (remainder << 1);
00039                 }
00040             }
00041         }
00042         
00043         return remainder;
00044     }
00045     
00046     typedef uint8_t crctype8;
00047     crctype8 crc8_gen(const unsigned char message[], unsigned int nBytes){
00048         
00049         crctype8 remainder = 0xff;
00050         
00051         for(int byte = 0 ; byte < nBytes ; byte++ ){
00052             /*
00053             Bring the data byte by byte
00054             each time only one byte is brought
00055             0 xor x = x
00056             */
00057             remainder = remainder ^ ( message[byte] );
00058             
00059             for(int bit = 8 ; bit > 0 ; bit--){
00060                 /*
00061                 for each bit, xor the remainder with polynomial
00062                 if the MSB is 1
00063                 */
00064                 if(remainder & TOPBIT8){
00065                     remainder = (remainder << 1) ^ POLYNOMIAL8;
00066                     /*
00067                     each time the remainder is xor-ed with polynomial, the MSB is made zero
00068                     hence the first digit of the remainder is ignored in the loop
00069                     */
00070                 }
00071                 else{
00072                     remainder = (remainder << 1);
00073                 }
00074             }
00075         }
00076         
00077         return remainder;
00078     }
00079 }