![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
kgkjgkj
Dependencies: FreescaleIAP mbed-rtos mbed
Fork of CDMS_SD_MNG_OVERDRIVE by
crc.h@1:aa24178260d2, 2015-12-16 (annotated)
- Committer:
- pradeepvk2208
- Date:
- Wed Dec 16 11:55:46 2015 +0000
- Revision:
- 1:aa24178260d2
- Parent:
- 0:bcbd76c86cde
ffjfiug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cholletisaik777 | 0:bcbd76c86cde | 1 | //EDITS |
cholletisaik777 | 0:bcbd76c86cde | 2 | //changed the initial remainder from 0x0000 to 0xffff according to the standards |
cholletisaik777 | 0:bcbd76c86cde | 3 | //made two seperate functions crc16_gen and crc8_gen |
cholletisaik777 | 0:bcbd76c86cde | 4 | |
cholletisaik777 | 0:bcbd76c86cde | 5 | #define TOPBIT16 (1 << 15) |
cholletisaik777 | 0:bcbd76c86cde | 6 | #define TOPBIT8 (1 << 7) |
cholletisaik777 | 0:bcbd76c86cde | 7 | #define POLYNOMIAL16 0x1021 |
cholletisaik777 | 0:bcbd76c86cde | 8 | #define POLYNOMIAL8 0xEA |
cholletisaik777 | 0:bcbd76c86cde | 9 | |
cholletisaik777 | 0:bcbd76c86cde | 10 | namespace CRC{ |
cholletisaik777 | 0:bcbd76c86cde | 11 | typedef uint16_t crctype16; |
cholletisaik777 | 0:bcbd76c86cde | 12 | crctype16 crc16_gen(const unsigned char message[], unsigned int nBytes){ |
cholletisaik777 | 0:bcbd76c86cde | 13 | crctype16 remainder = 0xffff; |
cholletisaik777 | 0:bcbd76c86cde | 14 | int byte; |
cholletisaik777 | 0:bcbd76c86cde | 15 | char bit; |
cholletisaik777 | 0:bcbd76c86cde | 16 | |
cholletisaik777 | 0:bcbd76c86cde | 17 | for( byte = 0 ; byte < nBytes ; byte++ ){ |
cholletisaik777 | 0:bcbd76c86cde | 18 | /* |
cholletisaik777 | 0:bcbd76c86cde | 19 | Bring the data byte by byte |
cholletisaik777 | 0:bcbd76c86cde | 20 | each time only one byte is brought |
cholletisaik777 | 0:bcbd76c86cde | 21 | 0 xor x = x |
cholletisaik777 | 0:bcbd76c86cde | 22 | */ |
cholletisaik777 | 0:bcbd76c86cde | 23 | remainder = remainder ^ ( message[byte] << 8 ); |
cholletisaik777 | 0:bcbd76c86cde | 24 | |
cholletisaik777 | 0:bcbd76c86cde | 25 | for( bit = 8 ; bit > 0 ; bit--){ |
cholletisaik777 | 0:bcbd76c86cde | 26 | /* |
cholletisaik777 | 0:bcbd76c86cde | 27 | for each bit, xor the remainder with polynomial |
cholletisaik777 | 0:bcbd76c86cde | 28 | if the MSB is 1 |
cholletisaik777 | 0:bcbd76c86cde | 29 | */ |
cholletisaik777 | 0:bcbd76c86cde | 30 | if(remainder & TOPBIT16){ |
cholletisaik777 | 0:bcbd76c86cde | 31 | remainder = (remainder << 1) ^ POLYNOMIAL16; |
cholletisaik777 | 0:bcbd76c86cde | 32 | /* |
cholletisaik777 | 0:bcbd76c86cde | 33 | each time the remainder is xor-ed with polynomial, the MSB is made zero |
cholletisaik777 | 0:bcbd76c86cde | 34 | hence the first digit of the remainder is ignored in the loop |
cholletisaik777 | 0:bcbd76c86cde | 35 | */ |
cholletisaik777 | 0:bcbd76c86cde | 36 | } |
cholletisaik777 | 0:bcbd76c86cde | 37 | else{ |
cholletisaik777 | 0:bcbd76c86cde | 38 | remainder = (remainder << 1); |
cholletisaik777 | 0:bcbd76c86cde | 39 | } |
cholletisaik777 | 0:bcbd76c86cde | 40 | } |
cholletisaik777 | 0:bcbd76c86cde | 41 | } |
cholletisaik777 | 0:bcbd76c86cde | 42 | |
cholletisaik777 | 0:bcbd76c86cde | 43 | return remainder; |
cholletisaik777 | 0:bcbd76c86cde | 44 | } |
cholletisaik777 | 0:bcbd76c86cde | 45 | |
cholletisaik777 | 0:bcbd76c86cde | 46 | typedef uint8_t crctype8; |
cholletisaik777 | 0:bcbd76c86cde | 47 | crctype8 crc8_gen(const unsigned char message[], unsigned int nBytes){ |
cholletisaik777 | 0:bcbd76c86cde | 48 | |
cholletisaik777 | 0:bcbd76c86cde | 49 | crctype8 remainder = 0xff; |
cholletisaik777 | 0:bcbd76c86cde | 50 | |
cholletisaik777 | 0:bcbd76c86cde | 51 | for(int byte = 0 ; byte < nBytes ; byte++ ){ |
cholletisaik777 | 0:bcbd76c86cde | 52 | /* |
cholletisaik777 | 0:bcbd76c86cde | 53 | Bring the data byte by byte |
cholletisaik777 | 0:bcbd76c86cde | 54 | each time only one byte is brought |
cholletisaik777 | 0:bcbd76c86cde | 55 | 0 xor x = x |
cholletisaik777 | 0:bcbd76c86cde | 56 | */ |
cholletisaik777 | 0:bcbd76c86cde | 57 | remainder = remainder ^ ( message[byte] ); |
cholletisaik777 | 0:bcbd76c86cde | 58 | |
cholletisaik777 | 0:bcbd76c86cde | 59 | for(int bit = 8 ; bit > 0 ; bit--){ |
cholletisaik777 | 0:bcbd76c86cde | 60 | /* |
cholletisaik777 | 0:bcbd76c86cde | 61 | for each bit, xor the remainder with polynomial |
cholletisaik777 | 0:bcbd76c86cde | 62 | if the MSB is 1 |
cholletisaik777 | 0:bcbd76c86cde | 63 | */ |
cholletisaik777 | 0:bcbd76c86cde | 64 | if(remainder & TOPBIT8){ |
cholletisaik777 | 0:bcbd76c86cde | 65 | remainder = (remainder << 1) ^ POLYNOMIAL8; |
cholletisaik777 | 0:bcbd76c86cde | 66 | /* |
cholletisaik777 | 0:bcbd76c86cde | 67 | each time the remainder is xor-ed with polynomial, the MSB is made zero |
cholletisaik777 | 0:bcbd76c86cde | 68 | hence the first digit of the remainder is ignored in the loop |
cholletisaik777 | 0:bcbd76c86cde | 69 | */ |
cholletisaik777 | 0:bcbd76c86cde | 70 | } |
cholletisaik777 | 0:bcbd76c86cde | 71 | else{ |
cholletisaik777 | 0:bcbd76c86cde | 72 | remainder = (remainder << 1); |
cholletisaik777 | 0:bcbd76c86cde | 73 | } |
cholletisaik777 | 0:bcbd76c86cde | 74 | } |
cholletisaik777 | 0:bcbd76c86cde | 75 | } |
cholletisaik777 | 0:bcbd76c86cde | 76 | |
cholletisaik777 | 0:bcbd76c86cde | 77 | return remainder; |
cholletisaik777 | 0:bcbd76c86cde | 78 | } |
cholletisaik777 | 0:bcbd76c86cde | 79 | } |