Additional crc functions

This commit is contained in:
Martin Budden 2017-08-12 10:21:41 +01:00
parent 981df1d544
commit 4ba8669cc3
12 changed files with 117 additions and 46 deletions

View File

@ -5,6 +5,7 @@ COMMON_SRC = \
$(TARGET_DIR_SRC) \
main.c \
common/bitarray.c \
common/crc.c \
common/encoding.c \
common/filter.c \
common/huffman.c \

79
src/main/common/crc.c Normal file
View File

@ -0,0 +1,79 @@
/*
* This file is part of Cleanflight.
*
* Cleanflight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cleanflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include "streambuf.h"
uint16_t crc16_ccitt(uint16_t crc, unsigned char a)
{
crc ^= (uint16_t)a << 8;
for (int ii = 0; ii < 8; ++ii) {
if (crc & 0x8000) {
crc = (crc << 1) ^ 0x1021;
} else {
crc = crc << 1;
}
}
return crc;
}
uint16_t crc16_ccitt_update(uint16_t crc, const void *data, uint32_t length)
{
const uint8_t *p = (const uint8_t *)data;
const uint8_t *pend = p + length;
for (; p != pend; p++) {
crc = crc16_ccitt(crc, *p);
}
return crc;
}
uint8_t crc8_dvb_s2(uint8_t crc, unsigned char a)
{
crc ^= a;
for (int ii = 0; ii < 8; ++ii) {
if (crc & 0x80) {
crc = (crc << 1) ^ 0xD5;
} else {
crc = crc << 1;
}
}
return crc;
}
uint8_t crc8_dvb_s2_update(uint8_t crc, const void *data, uint32_t length)
{
const uint8_t *p = (const uint8_t *)data;
const uint8_t *pend = p + length;
for (; p != pend; p++) {
crc = crc8_dvb_s2(crc, *p);
}
return crc;
}
void crc8_dvb_s2_sbuf_append(sbuf_t *dst, uint8_t *start)
{
uint8_t crc = 0;
const uint8_t *end = dst->ptr;
for (uint8_t *ptr = start; ptr < end; ++ptr) {
crc = crc8_dvb_s2(crc, *ptr);
}
sbufWriteU8(dst, crc);
}

25
src/main/common/crc.h Normal file
View File

@ -0,0 +1,25 @@
/*
* This file is part of Cleanflight.
*
* Cleanflight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cleanflight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
uint16_t crc16_ccitt(uint16_t crc, unsigned char a);
uint16_t crc16_ccitt_update(uint16_t crc, const void *data, uint32_t length);
uint8_t crc8_dvb_s2(uint8_t crc, unsigned char a);
uint8_t crc8_dvb_s2_update(uint8_t crc, const void *data, uint32_t length);
struct sbuf_s;
void crc8_dvb_s2_sbuf_append(struct sbuf_s *dst, uint8_t *start);

View File

@ -345,41 +345,3 @@ int16_t qMultiply(fix12_t q, int16_t input) {
fix12_t qConstruct(int16_t num, int16_t den) {
return (num << 12) / den;
}
uint16_t crc16_ccitt(uint16_t crc, unsigned char a)
{
crc ^= (uint16_t)a << 8;
for (int ii = 0; ii < 8; ++ii) {
if (crc & 0x8000) {
crc = (crc << 1) ^ 0x1021;
} else {
crc = crc << 1;
}
}
return crc;
}
uint16_t crc16_ccitt_update(uint16_t crc, const void *data, uint32_t length)
{
const uint8_t *p = (const uint8_t *)data;
const uint8_t *pend = p + length;
for (; p != pend; p++) {
crc = crc16_ccitt(crc, *p);
}
return crc;
}
uint8_t crc8_dvb_s2(uint8_t crc, unsigned char a)
{
crc ^= a;
for (int ii = 0; ii < 8; ++ii) {
if (crc & 0x80) {
crc = (crc << 1) ^ 0xD5;
} else {
crc = crc << 1;
}
}
return crc;
}

View File

@ -142,6 +142,3 @@ static inline float constrainf(float amt, float low, float high)
else
return amt;
}
uint16_t crc16_ccitt(uint16_t crc, unsigned char a);
uint16_t crc16_ccitt_update(uint16_t crc, const void *data, uint32_t length);
uint8_t crc8_dvb_s2(uint8_t crc, unsigned char a);

View File

@ -23,7 +23,7 @@
#include "build/build_config.h"
#include "common/maths.h"
#include "common/crc.h"
#include "common/utils.h"
#include "config/config_eeprom.h"

View File

@ -23,7 +23,7 @@
#include "rx_spi.h"
#include "rx_nrf24l01.h"
#include "common/maths.h"
#include "common/crc.h"
static const uint8_t xn297_data_scramble[30] = {

View File

@ -27,6 +27,7 @@
#include "build/build_config.h"
#include "build/debug.h"
#include "common/crc.h"
#include "common/maths.h"
#include "common/utils.h"

View File

@ -29,6 +29,7 @@
#include "config/parameter_group.h"
#include "config/parameter_group_ids.h"
#include "common/crc.h"
#include "common/maths.h"
#include "common/streambuf.h"
#include "common/utils.h"

View File

@ -163,7 +163,8 @@ rc_controls_unittest_SRC := \
rx_crsf_unittest_SRC := \
$(USER_DIR)/rx/crsf.c \
$(USER_DIR)/common/maths.c
$(USER_DIR)/common/crc.c \
$(USER_DIR)/common/streambuf.c
rx_ibus_unittest_SRC := \
@ -186,12 +187,15 @@ rx_rx_unittest_SRC := \
scheduler_unittest_SRC := \
$(USER_DIR)/scheduler/scheduler.c
$(USER_DIR)/scheduler/scheduler.c \
$(USER_DIR)/common/crc.c \
$(USER_DIR)/common/streambuf.c
telemetry_crsf_unittest_SRC := \
$(USER_DIR)/rx/crsf.c \
$(USER_DIR)/telemetry/crsf.c \
$(USER_DIR)/common/crc.c \
$(USER_DIR)/common/maths.c \
$(USER_DIR)/common/streambuf.c \
$(USER_DIR)/common/gps_conversion.c \

View File

@ -26,7 +26,7 @@ extern "C" {
#include "build/debug.h"
#include "common/maths.h"
#include "common/crc.h"
#include "common/utils.h"
#include "io/serial.h"

View File

@ -27,6 +27,7 @@ extern "C" {
#include "build/debug.h"
#include "common/axis.h"
#include "common/crc.h"
#include "common/filter.h"
#include "common/gps_conversion.h"
#include "common/maths.h"