From 51ad2f71c765948d33027f6c9d0768003371aace Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Tue, 21 Jun 2022 20:51:47 +1000 Subject: [PATCH] crc32 --- source/Core/BSP/Magic/crc32.h | 29 +++++++++++++++++++++++++++++ source/Core/Drivers/crc16.cpp | 19 ------------------- source/Core/Drivers/crc16.hpp | 6 ------ 3 files changed, 29 insertions(+), 25 deletions(-) create mode 100644 source/Core/BSP/Magic/crc32.h delete mode 100644 source/Core/Drivers/crc16.cpp delete mode 100644 source/Core/Drivers/crc16.hpp diff --git a/source/Core/BSP/Magic/crc32.h b/source/Core/BSP/Magic/crc32.h new file mode 100644 index 00000000..c8bc9c09 --- /dev/null +++ b/source/Core/BSP/Magic/crc32.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +#define DEFAULT_POLY 0x973afb51 + +template struct CRC32Table { + constexpr CRC32Table() : table() { + for (uint32_t i = 0; i < 256; i++) { + uint32_t c = i; + for (auto j = 0; j < 8; j++) { + if (c & 1) { + c = polynomial ^ (c >> 1); + } else { + c >>= 1; + } + } + table[i] = c; + } + } + uint32_t table[256]; + uint32_t computeCRC32(uint32_t initial, const uint8_t *buf, int len) { + uint32_t c = initial ^ 0xFFFFFFFF; + for (auto i = 0; i < len; ++i) { + c = table[(c ^ buf[i]) & 0xFF] ^ (c >> 8); + } + return c ^ 0xFFFFFFFF; + } +}; diff --git a/source/Core/Drivers/crc16.cpp b/source/Core/Drivers/crc16.cpp deleted file mode 100644 index 9966c9f0..00000000 --- a/source/Core/Drivers/crc16.cpp +++ /dev/null @@ -1,19 +0,0 @@ - -#include "crc16.hpp" -#define POLYNOM 0x8005 -unsigned int crc16(unsigned int crcValue, unsigned char newByte) { - unsigned char i; - - for (i = 0; i < 8; i++) { - - if (((crcValue & 0x8000) >> 8) ^ (newByte & 0x80)) { - crcValue = (crcValue << 1) ^ POLYNOM; - } else { - crcValue = (crcValue << 1); - } - - newByte <<= 1; - } - - return crcValue; -} \ No newline at end of file diff --git a/source/Core/Drivers/crc16.hpp b/source/Core/Drivers/crc16.hpp deleted file mode 100644 index b598c6fb..00000000 --- a/source/Core/Drivers/crc16.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef DRIVERS_CRC16_H_ -#define DRIVERS_CRC16_H_ - -unsigned int crc16(unsigned int crcValue, unsigned char newByte); - -#endif \ No newline at end of file