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