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.cpp

Committer:
Overdrivr
Date:
2016-04-12
Revision:
7:d224bddd5405
Parent:
6:f5e1b079bffd

File content as of revision 7:d224bddd5405:

#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;
}