First class data visualization and communication library with embedded devices. Code is maintained at github.com/Overdrivr/Telemetry

Dependents:   telemetry_car_demo telemetry_demo_FRDM-TFC telemetry_example_01 telemetry_indexed_data_demo ... more

Committer:
Overdrivr
Date:
Mon Feb 22 17:17:52 2016 +0000
Revision:
3:37d2d127bc83
Using the newly generated API

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Overdrivr 3:37d2d127bc83 1 #include "crc16.h"
Overdrivr 3:37d2d127bc83 2
Overdrivr 3:37d2d127bc83 3 uint16_t crc16(uint8_t* data, uint32_t len)
Overdrivr 3:37d2d127bc83 4 {
Overdrivr 3:37d2d127bc83 5 uint16_t rem = 0;
Overdrivr 3:37d2d127bc83 6 for(uint16_t i = 0 ; i < len ; i++)
Overdrivr 3:37d2d127bc83 7 {
Overdrivr 3:37d2d127bc83 8 rem = crc16_recursive(data[i],rem);
Overdrivr 3:37d2d127bc83 9 }
Overdrivr 3:37d2d127bc83 10 return rem;
Overdrivr 3:37d2d127bc83 11 }
Overdrivr 3:37d2d127bc83 12
Overdrivr 3:37d2d127bc83 13 uint16_t crc16_recursive(uint8_t byte, uint16_t remainder)
Overdrivr 3:37d2d127bc83 14 {
Overdrivr 3:37d2d127bc83 15 uint16_t n = 16;
Overdrivr 3:37d2d127bc83 16
Overdrivr 3:37d2d127bc83 17 remainder = remainder ^ (byte << (n-8));
Overdrivr 3:37d2d127bc83 18
Overdrivr 3:37d2d127bc83 19 for(uint16_t j = 1 ; j < 8 ; j++)
Overdrivr 3:37d2d127bc83 20 {
Overdrivr 3:37d2d127bc83 21 if(remainder & 0x8000)
Overdrivr 3:37d2d127bc83 22 {
Overdrivr 3:37d2d127bc83 23 remainder = (remainder << 1) ^ 0x1021;
Overdrivr 3:37d2d127bc83 24 }
Overdrivr 3:37d2d127bc83 25 else
Overdrivr 3:37d2d127bc83 26 {
Overdrivr 3:37d2d127bc83 27 remainder = remainder << 1;
Overdrivr 3:37d2d127bc83 28 }
Overdrivr 3:37d2d127bc83 29 remainder &= 0xffff;
Overdrivr 3:37d2d127bc83 30 }
Overdrivr 3:37d2d127bc83 31
Overdrivr 3:37d2d127bc83 32 return remainder;
Overdrivr 3:37d2d127bc83 33 }