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

c_api/crc16.c

Committer:
Overdrivr
Date:
2016-03-09
Revision:
5:cd94bb58e096

File content as of revision 5:cd94bb58e096:

#include "crc16.h"

uint16_t crc16(uint8_t* data, uint32_t len)
{
    uint16_t rem  = 0;
    uint16_t i=0;
    for(i = 0 ; i < len ; i++)
    {
        rem = crc16_recursive(data[i],rem);
    }
  return rem;
 }

uint16_t crc16_recursive(uint8_t byte, uint16_t remainder)
{
    uint16_t n = 16;

    remainder  = remainder ^ (byte << (n-8));
    uint16_t j = 0;
    for(j = 1 ; j < 8 ; j++)
    {
        if(remainder & 0x8000)
        {
            remainder  = (remainder << 1) ^ 0x1021;
        }
        else
        {
            remainder  = remainder << 1;
        }
        remainder &= 0xffff;
    }

    return remainder;
}