I2C code testing

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE_pl123 by Siva ram

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 0x07
00009 
00010 typedef uint16_t crctype16; 
00011 crctype16 crc16_gen(const unsigned char message[], unsigned int nBytes){
00012     crctype16 remainder = 0xffff;
00013     int byte;
00014     char bit;
00015     
00016     for( byte = 0 ; byte < nBytes ; byte++ ){
00017         /*
00018         Bring the data byte by byte
00019         each time only one byte is brought
00020         0 xor x = x
00021         */
00022         remainder = remainder ^ ( message[byte] << 8 );
00023         
00024         for( bit = 8 ; bit > 0 ; bit--){
00025             /*
00026             for each bit, xor the remainder with polynomial
00027             if the MSB is 1
00028             */
00029             if(remainder & TOPBIT16){
00030                 remainder = (remainder << 1) ^ POLYNOMIAL16;
00031                 /*
00032                 each time the remainder is xor-ed with polynomial, the MSB is made zero
00033                 hence the first digit of the remainder is ignored in the loop
00034                 */
00035             }
00036             else{
00037                 remainder = (remainder << 1);
00038             }
00039         }
00040     }
00041     
00042     return remainder;
00043 }
00044 
00045 typedef uint8_t crctype8;
00046 crctype8 crc8_gen(const unsigned char message[], unsigned int nBytes){
00047     
00048     crctype8 remainder = 0xff;
00049     
00050     for(int byte = 0 ; byte < nBytes ; byte++ ){
00051         /*
00052         Bring the data byte by byte
00053         each time only one byte is brought
00054         0 xor x = x
00055         */
00056         remainder = remainder ^ ( message[byte] );
00057         
00058         for(int bit = 8 ; bit > 0 ; bit--){
00059             /*
00060             for each bit, xor the remainder with polynomial
00061             if the MSB is 1
00062             */
00063             if(remainder & TOPBIT8){
00064                 remainder = (remainder << 1) ^ POLYNOMIAL8;
00065                 /*
00066                 each time the remainder is xor-ed with polynomial, the MSB is made zero
00067                 hence the first digit of the remainder is ignored in the loop
00068                 */
00069             }
00070             else{
00071                 remainder = (remainder << 1);
00072             }
00073         }
00074     }
00075     
00076     return remainder;
00077 }
00078 
00079 uint16_t crc16_gen1( char message[], unsigned int nBytes){
00080     crctype16 remainder = 0xffff;
00081     int byte;
00082     char bit;
00083     
00084     for( byte = 0 ; byte < nBytes ; byte++ ){
00085         /*
00086         Bring the data byte by byte
00087         each time only one byte is brought
00088         0 xor x = x
00089         */
00090         remainder = remainder ^ ( message[byte] << 8 );
00091         
00092         for( bit = 8 ; bit > 0 ; bit--){
00093             /*
00094             for each bit, xor the remainder with polynomial
00095             if the MSB is 1
00096             */
00097             if(remainder & TOPBIT16){
00098                 remainder = (remainder << 1) ^ POLYNOMIAL16;
00099                 /*
00100                 each time the remainder is xor-ed with polynomial, the MSB is made zero
00101                 hence the first digit of the remainder is ignored in the loop
00102                 */
00103             }
00104             else{
00105                 remainder = (remainder << 1);
00106             }
00107         }
00108     }
00109     
00110     return remainder;
00111 }
00112