Team Fox / Mbed 2 deprecated CDMS_QM_03MAR2017_Flash_with_obsrs

Dependencies:   mbed mbed-rtos SimpleDMA FreescaleIAP eeprom

Fork of CDMS_CODE_FM_28JAN2017 by samp Srinivasan

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers crc.h Source File

crc.h

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