Framework for reading and writing variables in real time on any MBED platform.
DistantIO
This is the C implementation of the DistantIO slave framework.
Library is working but slight API breaks may occur in the future. C++ version is also in development.
To get the master-side implementation, see https://github.com/Overdrivr/DistantIO
crc.cpp@6:72d46dbdbe7a, 2015-10-16 (annotated)
- Committer:
- Overdrivr
- Date:
- Fri Oct 16 16:16:19 2015 +0000
- Revision:
- 6:72d46dbdbe7a
- Parent:
- 1:aaffeb93f99b
removed 'static' keyword in front arrays that just increase RAM usage (premature optimization)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Overdrivr | 1:aaffeb93f99b | 1 | //From : https://en.wikipedia.org/wiki/Computation_of_cyclic_redundancy_checks |
Overdrivr | 0:c4676d32d381 | 2 | |
Overdrivr | 0:c4676d32d381 | 3 | #include "crc.h" |
Overdrivr | 0:c4676d32d381 | 4 | |
Overdrivr | 0:c4676d32d381 | 5 | uint16_t crc16(uint8_t* data, uint16_t len) |
Overdrivr | 0:c4676d32d381 | 6 | { |
Overdrivr | 0:c4676d32d381 | 7 | uint16_t rem = 0; |
Overdrivr | 0:c4676d32d381 | 8 | uint16_t n = 16; |
Overdrivr | 0:c4676d32d381 | 9 | // A popular variant complements rem here |
Overdrivr | 0:c4676d32d381 | 10 | for(uint16_t i = 0 ; i < len ; i++) |
Overdrivr | 0:c4676d32d381 | 11 | { |
Overdrivr | 0:c4676d32d381 | 12 | rem = rem ^ (data[i] << (n-8)); // n = 16 in this example |
Overdrivr | 0:c4676d32d381 | 13 | |
Overdrivr | 0:c4676d32d381 | 14 | for(uint16_t j = 1 ; j < 8 ; j++) // Assuming 8 bits per byte |
Overdrivr | 0:c4676d32d381 | 15 | { |
Overdrivr | 0:c4676d32d381 | 16 | |
Overdrivr | 0:c4676d32d381 | 17 | if(rem & 0x8000) |
Overdrivr | 0:c4676d32d381 | 18 | { |
Overdrivr | 0:c4676d32d381 | 19 | // if leftmost (most significant) bit is set |
Overdrivr | 0:c4676d32d381 | 20 | rem = (rem << 1) ^ 0x1021; |
Overdrivr | 0:c4676d32d381 | 21 | } |
Overdrivr | 0:c4676d32d381 | 22 | else |
Overdrivr | 0:c4676d32d381 | 23 | { |
Overdrivr | 0:c4676d32d381 | 24 | rem = rem << 1; |
Overdrivr | 0:c4676d32d381 | 25 | } |
Overdrivr | 0:c4676d32d381 | 26 | rem &= 0xffff; // Trim remainder to 16 bits |
Overdrivr | 0:c4676d32d381 | 27 | } |
Overdrivr | 0:c4676d32d381 | 28 | } |
Overdrivr | 0:c4676d32d381 | 29 | // A popular variant complements rem here |
Overdrivr | 0:c4676d32d381 | 30 | return rem; |
Overdrivr | 0:c4676d32d381 | 31 | } |
Overdrivr | 0:c4676d32d381 | 32 | |
Overdrivr | 0:c4676d32d381 | 33 |