a serial library to communicate with pebble time's smart strap interface
Dependents: xadow_smartstrap_for_pebble
crc.c@0:e4dad9e53f06, 2015-11-04 (annotated)
- Committer:
- KillingJacky
- Date:
- Wed Nov 04 09:58:41 2015 +0000
- Revision:
- 0:e4dad9e53f06
initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
KillingJacky | 0:e4dad9e53f06 | 1 | #include "crc.h" |
KillingJacky | 0:e4dad9e53f06 | 2 | |
KillingJacky | 0:e4dad9e53f06 | 3 | void crc8_calculate_byte_streaming(const uint8_t data, uint8_t *crc) { |
KillingJacky | 0:e4dad9e53f06 | 4 | // Optimal polynomial chosen based on |
KillingJacky | 0:e4dad9e53f06 | 5 | // http://users.ece.cmu.edu/~koopman/roses/dsn04/koopman04_crc_poly_embedded.pdf |
KillingJacky | 0:e4dad9e53f06 | 6 | // Note that this is different than the standard CRC-8 polynomial, because the |
KillingJacky | 0:e4dad9e53f06 | 7 | // standard CRC-8 polynomial is not particularly good. |
KillingJacky | 0:e4dad9e53f06 | 8 | |
KillingJacky | 0:e4dad9e53f06 | 9 | // nibble lookup table for (x^8 + x^5 + x^3 + x^2 + x + 1) |
KillingJacky | 0:e4dad9e53f06 | 10 | static const uint8_t lookup_table[] = |
KillingJacky | 0:e4dad9e53f06 | 11 | { 0, 47, 94, 113, 188, 147, 226, 205, 87, 120, 9, 38, 235, 196, |
KillingJacky | 0:e4dad9e53f06 | 12 | 181, 154 }; |
KillingJacky | 0:e4dad9e53f06 | 13 | |
KillingJacky | 0:e4dad9e53f06 | 14 | int i; |
KillingJacky | 0:e4dad9e53f06 | 15 | for (i = 2; i > 0; i--) { |
KillingJacky | 0:e4dad9e53f06 | 16 | uint8_t nibble = data; |
KillingJacky | 0:e4dad9e53f06 | 17 | if (i % 2 == 0) { |
KillingJacky | 0:e4dad9e53f06 | 18 | nibble >>= 4; |
KillingJacky | 0:e4dad9e53f06 | 19 | } |
KillingJacky | 0:e4dad9e53f06 | 20 | int index = nibble ^ (*crc >> 4); |
KillingJacky | 0:e4dad9e53f06 | 21 | *crc = lookup_table[index & 0xf] ^ (*crc << 4); |
KillingJacky | 0:e4dad9e53f06 | 22 | } |
KillingJacky | 0:e4dad9e53f06 | 23 | } |