Alexandre Lemay
/
APP4_FunTimes
first
CRC.cpp@9:8f479f7c1b54, 2017-10-25 (annotated)
- Committer:
- ThierryLeonard
- Date:
- Wed Oct 25 05:43:13 2017 +0000
- Revision:
- 9:8f479f7c1b54
- Parent:
- 6:ac7c0ccf9b5d
final???;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ThierryLeonard | 6:ac7c0ccf9b5d | 1 | #include "CRC.h" |
ThierryLeonard | 6:ac7c0ccf9b5d | 2 | |
ThierryLeonard | 6:ac7c0ccf9b5d | 3 | const unsigned short CRCPOLYNOM = 0x8005; |
ThierryLeonard | 6:ac7c0ccf9b5d | 4 | |
ThierryLeonard | 6:ac7c0ccf9b5d | 5 | // The last two bytes of data corresponds to the remainder. If they are of 0,0, this function will return the remainder to put into them. |
ThierryLeonard | 6:ac7c0ccf9b5d | 6 | // If the two last bytes contains the crc received, the return should be of 0 |
ThierryLeonard | 6:ac7c0ccf9b5d | 7 | int crc16Remainder(std::vector<unsigned char> data, unsigned short generator) |
ThierryLeonard | 6:ac7c0ccf9b5d | 8 | { |
ThierryLeonard | 6:ac7c0ccf9b5d | 9 | // shift generator |
ThierryLeonard | 6:ac7c0ccf9b5d | 10 | unsigned int polynom = generator << 16; |
ThierryLeonard | 6:ac7c0ccf9b5d | 11 | |
ThierryLeonard | 6:ac7c0ccf9b5d | 12 | if (data.empty()) |
ThierryLeonard | 6:ac7c0ccf9b5d | 13 | { |
ThierryLeonard | 6:ac7c0ccf9b5d | 14 | return -1; |
ThierryLeonard | 6:ac7c0ccf9b5d | 15 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 16 | |
ThierryLeonard | 6:ac7c0ccf9b5d | 17 | unsigned int remainder = data[0] << 24 | data[1] << 16; |
ThierryLeonard | 6:ac7c0ccf9b5d | 18 | |
ThierryLeonard | 6:ac7c0ccf9b5d | 19 | for (int i = 2; i < data.size(); i++) |
ThierryLeonard | 6:ac7c0ccf9b5d | 20 | { |
ThierryLeonard | 6:ac7c0ccf9b5d | 21 | remainder = remainder | data[i] << 8; |
ThierryLeonard | 6:ac7c0ccf9b5d | 22 | for (int j = 0; j < 8; j++) |
ThierryLeonard | 6:ac7c0ccf9b5d | 23 | { |
ThierryLeonard | 6:ac7c0ccf9b5d | 24 | // if msb is 1, shift and xor |
ThierryLeonard | 6:ac7c0ccf9b5d | 25 | if (remainder >> 31 ==1) |
ThierryLeonard | 6:ac7c0ccf9b5d | 26 | { |
ThierryLeonard | 6:ac7c0ccf9b5d | 27 | remainder = (remainder << 1) ^ polynom; |
ThierryLeonard | 6:ac7c0ccf9b5d | 28 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 29 | else |
ThierryLeonard | 6:ac7c0ccf9b5d | 30 | { |
ThierryLeonard | 6:ac7c0ccf9b5d | 31 | remainder = remainder << 1; |
ThierryLeonard | 6:ac7c0ccf9b5d | 32 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 33 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 34 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 35 | // Calculates remainder of last 16 bits |
ThierryLeonard | 6:ac7c0ccf9b5d | 36 | for (int i = 0; i < 16; i++) |
ThierryLeonard | 6:ac7c0ccf9b5d | 37 | { |
ThierryLeonard | 6:ac7c0ccf9b5d | 38 | // if msb is 1, shift and xor |
ThierryLeonard | 6:ac7c0ccf9b5d | 39 | if (remainder >> 31 == 1) |
ThierryLeonard | 6:ac7c0ccf9b5d | 40 | { |
ThierryLeonard | 6:ac7c0ccf9b5d | 41 | remainder = (remainder << 1) ^ polynom; |
ThierryLeonard | 6:ac7c0ccf9b5d | 42 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 43 | else |
ThierryLeonard | 6:ac7c0ccf9b5d | 44 | { |
ThierryLeonard | 6:ac7c0ccf9b5d | 45 | remainder = remainder << 1; |
ThierryLeonard | 6:ac7c0ccf9b5d | 46 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 47 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 48 | return remainder >> 16; |
ThierryLeonard | 6:ac7c0ccf9b5d | 49 | |
ThierryLeonard | 6:ac7c0ccf9b5d | 50 | } |