Skip to content

Instantly share code, notes, and snippets.

@davidbuzz
Last active April 9, 2019 01:02
Show Gist options
  • Select an option

  • Save davidbuzz/fbdce76db05f9d8910fc0df14eba1852 to your computer and use it in GitHub Desktop.

Select an option

Save davidbuzz/fbdce76db05f9d8910fc0df14eba1852 to your computer and use it in GitHub Desktop.
buzz simple crc_check cross-check
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
// AP_Math/crc.cpp
/*
xmodem CRC thanks to avr-liberty
https://github.com/dreamiurg/avr-liberty
*/
uint16_t crc_xmodem_update(uint16_t crc, uint8_t data)
{
crc = crc ^ ((uint16_t)data << 8);
for (uint16_t i=0; i<8; i++)
{
if(crc & 0x8000) {
crc = (crc << 1) ^ 0x1021;
} else {
crc <<= 1;
}
}
return crc;
}
uint16_t crc_xmodem(const uint8_t *data, uint16_t len)
{
uint16_t crc = 0;
for (uint16_t i=0; i<len; i++) {
crc = crc_xmodem_update(crc, data[i]);
}
return crc;
}
// libraries/AP_HAL/utility/srxl.cpp
static uint16_t srxl_crc16 (uint16_t crc, uint8_t new_byte)
{
uint8_t loop;
crc = crc ^ (uint16_t)new_byte << 8;
for(loop = 0; loop < 8; loop++) {
crc = (crc & 0x8000) ? (crc << 1) ^ 0x1021 : (crc << 1);
}
return crc;
}
//libraries/AP_HAL/utility/sumd.cpp
static uint16_t sumd_crc16(uint16_t crc, uint8_t value)
{
int i;
crc ^= (uint16_t)value << 8;
for (i = 0; i < 8; i++) {
crc = (crc & 0x8000) ? (crc << 1) ^ 0x1021 : (crc << 1);
}
return crc;
}
int main() {
srand (time(NULL));
uint16_t b = 0;
uint16_t f1 = 0;
uint16_t f2 = 0;
uint16_t f3 = 0;
for (b = 1 ; b<1000 ; b++ ) {
printf("%d\n",b);
uint16_t data = rand();
f1 = srxl_crc16(f1,data);
f2 = crc_xmodem_update(f2,data);
f3 = sumd_crc16(f3,data);
}
printf("%d\n",f1);
printf("%d\n",f2);
printf("%d\n",f3);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment