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