From 78d82a8da15a28e6863e40f2cbcdce3df164e704 Mon Sep 17 00:00:00 2001 From: Michael du Breuil Date: Tue, 2 Mar 2021 11:48:34 -0700 Subject: [PATCH] AP_Math: Add fletcher16 CRC --- libraries/AP_Math/crc.cpp | 14 +++++++++++++- libraries/AP_Math/crc.h | 4 +++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/libraries/AP_Math/crc.cpp b/libraries/AP_Math/crc.cpp index 9027477166..8aab8d78e0 100644 --- a/libraries/AP_Math/crc.cpp +++ b/libraries/AP_Math/crc.cpp @@ -354,7 +354,7 @@ uint16_t crc16_ccitt_GDL90(const uint8_t *buf, uint32_t len, uint16_t crc) * @param [in] len size of buffer * @return CRC value */ -uint16_t calc_crc_modbus(uint8_t *buf, uint16_t len) +uint16_t calc_crc_modbus(const uint8_t *buf, uint16_t len) { uint16_t crc = 0xFFFF; for (uint16_t pos = 0; pos < len; pos++) { @@ -372,6 +372,18 @@ uint16_t calc_crc_modbus(uint8_t *buf, uint16_t len) return crc; } +// fletcher 16 implementation +uint16_t crc_fletcher16(const uint8_t *buffer, uint32_t len) { + uint16_t c0 = 0; + uint16_t c1 = 0; + for (uint32_t i = 0; i < len; i++) { + c0 = (c0 + buffer[i]) % 255; + c1 = (c1 + c0) % 255; + } + + return (c1 << 8) | c0; +} + // FNV-1a implementation #define FNV_1_PRIME_64 1099511628211UL void hash_fnv_1a(uint32_t len, const uint8_t* buf, uint64_t* hash) diff --git a/libraries/AP_Math/crc.h b/libraries/AP_Math/crc.h index dc8daaaf6d..c5d06a93cf 100644 --- a/libraries/AP_Math/crc.h +++ b/libraries/AP_Math/crc.h @@ -42,7 +42,9 @@ uint16_t crc16_ccitt(const uint8_t *buf, uint32_t len, uint16_t crc); // https://www.faa.gov/nextgen/programs/adsb/archival/media/gdl90_public_icd_reva.pdf uint16_t crc16_ccitt_GDL90(const uint8_t *buf, uint32_t len, uint16_t crc); -uint16_t calc_crc_modbus(uint8_t *buf, uint16_t len); +uint16_t calc_crc_modbus(const uint8_t *buf, uint16_t len); + +uint16_t crc_fletcher16(const uint8_t * buffer, uint32_t len); // generate 64bit FNV1a hash from buffer #define FNV_1_OFFSET_BASIS_64 14695981039346656037UL