From 783d4371c9d062eae74ed1faf44661ca5007af5f Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Wed, 20 Jan 2021 16:41:33 +0100 Subject: [PATCH] Add CRC classes CRC8, CRC16, CRC32, CRC64 (#3) * version 0.1.1 --- CRC.h | 19 ++-- CRC16.cpp | 96 ++++++++++++++++++ CRC16.h | 50 ++++++++++ CRC32.cpp | 96 ++++++++++++++++++ CRC32.h | 50 ++++++++++ CRC64.cpp | 97 +++++++++++++++++++ CRC64.h | 50 ++++++++++ CRC8.cpp | 94 ++++++++++++++++++ CRC8.h | 50 ++++++++++ README.md | 74 +++++++++++--- examples/CRC16_test/CRC16_test.ino | 62 ++++++++++++ examples/CRC32_test/CRC32_test.ino | 57 +++++++++++ examples/CRC64_test/CRC64_test.ino | 58 +++++++++++ examples/CRC8_test/CRC8_test.ino | 63 ++++++++++++ examples/CRC_performance/CRC_performance.ino | 17 ++-- .../CRC_reference_test/CRC_reference_test.ino | 87 ++++++++--------- examples/CRC_test/CRC_test.ino | 43 ++++++++ keywords.txt | 10 +- library.json | 2 +- library.properties | 4 +- test/unit_test_001.cpp | 94 +++++++++--------- 21 files changed, 1040 insertions(+), 133 deletions(-) create mode 100644 CRC16.cpp create mode 100644 CRC16.h create mode 100644 CRC32.cpp create mode 100644 CRC32.h create mode 100644 CRC64.cpp create mode 100644 CRC64.h create mode 100644 CRC8.cpp create mode 100644 CRC8.h create mode 100644 examples/CRC16_test/CRC16_test.ino create mode 100644 examples/CRC32_test/CRC32_test.ino create mode 100644 examples/CRC64_test/CRC64_test.ino create mode 100644 examples/CRC8_test/CRC8_test.ino create mode 100644 examples/CRC_test/CRC_test.ino diff --git a/CRC.h b/CRC.h index f98835b..8d92502 100644 --- a/CRC.h +++ b/CRC.h @@ -2,17 +2,16 @@ // // FILE: CRC.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 +// VERSION: 0.1.1 // PURPOSE: Arduino library fir CRC8, CRC16, CRC16-CCITT, CRC32 // URL: https://github.com/RobTillaart/CRC // - #include "Arduino.h" -#define CRC_LIB_VERSION (F("0.1.0")) +#define CRC_LIB_VERSION (F("0.1.1")) //////////////////////////////////////////////////////////////// @@ -68,7 +67,7 @@ uint64_t reverse64(uint64_t in) /////////////////////////////////////////////////////////////////////////////////// // CRC POLYNOME = x8 + x5 + x4 + 1 = 1001 1000 = 0x8C -uint8_t CRC8(uint8_t *array, uint8_t length, uint8_t polynome = 0xD5, uint8_t startmask = 0x00, uint8_t endmask = 0x00, bool reverseIn = false, bool reverseOut = false) +uint8_t crc8(uint8_t *array, uint8_t length, uint8_t polynome = 0xD5, uint8_t startmask = 0x00, uint8_t endmask = 0x00, bool reverseIn = false, bool reverseOut = false) { uint8_t crc = startmask; while (length--) @@ -96,7 +95,7 @@ uint8_t CRC8(uint8_t *array, uint8_t length, uint8_t polynome = 0xD5, uint8_t st // CRC POLYNOME = x15 + 1 = 1000 0000 0000 0001 = 0x8001 -uint16_t CRC16(uint8_t *array, uint8_t length, uint16_t polynome = 0x8001, uint16_t startmask = 0x0000, uint16_t endmask = 0x0000, bool reverseIn = false, bool reverseOut = false) +uint16_t crc16(uint8_t *array, uint8_t length, uint16_t polynome = 0x8001, uint16_t startmask = 0x0000, uint16_t endmask = 0x0000, bool reverseIn = false, bool reverseOut = false) { uint16_t crc = startmask; while (length--) @@ -124,14 +123,14 @@ uint16_t CRC16(uint8_t *array, uint8_t length, uint16_t polynome = 0x8001, uint1 // CRC-CCITT POLYNOME = x13 + X5 + 1 = 0001 0000 0010 0001 = 0x1021 -uint16_t CRC16_CCITT(uint8_t *array, uint8_t length) +uint16_t crc16_CCITT(uint8_t *array, uint8_t length) { - return CRC16(array, length, 0x1021, 0xFFFF); + return crc16(array, length, 0x1021, 0xFFFF); } // CRC-32 POLYNOME = x32 + ..... + 1 -uint32_t CRC32(uint8_t *array, uint8_t length, uint32_t polynome = 0x04C11DB7, uint32_t startmask = 0, uint32_t endmask = 0, bool reverseIn = false, bool reverseOut = false) +uint32_t crc32(uint8_t *array, uint8_t length, uint32_t polynome = 0x04C11DB7, uint32_t startmask = 0, uint32_t endmask = 0, bool reverseIn = false, bool reverseOut = false) { uint32_t crc = startmask; while (length--) @@ -159,7 +158,7 @@ uint32_t CRC32(uint8_t *array, uint8_t length, uint32_t polynome = 0x04C11DB7, u // CRC-CCITT POLYNOME = x64 + ..... + 1 -uint64_t CRC64(uint8_t *array, uint8_t length, uint64_t polynome, uint64_t startmask, uint64_t endmask, bool reverseIn, bool reverseOut) +uint64_t crc64(uint8_t *array, uint8_t length, uint64_t polynome, uint64_t startmask, uint64_t endmask, bool reverseIn, bool reverseOut) { uint64_t crc = startmask; while (length--) @@ -185,7 +184,5 @@ uint64_t CRC64(uint8_t *array, uint8_t length, uint64_t polynome, uint64_t start return crc; } -/////////////////////////////////////////////////////////////// - // -- END OF FILE -- diff --git a/CRC16.cpp b/CRC16.cpp new file mode 100644 index 0000000..fef7dbb --- /dev/null +++ b/CRC16.cpp @@ -0,0 +1,96 @@ +// +// FILE: CRC16.cpp +// AUTHOR: Rob Tillaart +// PURPOSE: Arduino class for CRC16 +// URL: https://github.com/RobTillaart/CRC + + +#include "CRC16.h" + + +CRC16::CRC16() +{ + reset(); +} + + +void CRC16::reset() +{ + _polynome = CRC16_DEFAULT_POLYNOME; + _startMask = 0; + _endMask = 0; + _crc = 0; + _reverseIn = false; + _reverseOut = false; + _started = false; + _count = 0; +} + + +void CRC16::restart() +{ + _started = true; + _crc = _startMask; + _count = 0; +} + + +void CRC16::add(uint8_t value) +{ + _count++; + _update(value); +} + + +void CRC16::add(uint8_t * array, uint32_t length) +{ + _count += length; + while (length--) + { + yield(); + uint8_t data = *array++; + _update(data); + } +} + + +uint16_t CRC16::getCRC() +{ + uint16_t rv = _crc; + if (_reverseOut) rv = _reverse(rv); + rv ^= _endMask; + return rv; +} + + +void CRC16::_update(uint8_t value) +{ + if (!_started) restart(); + if (_reverseIn) value = _reverse(value); + _crc ^= ((uint16_t)value) << 8;; + for (uint8_t i = 8; i; i--) + { + if (_crc & (1UL << 15)) + { + _crc <<= 1; + _crc ^= _polynome; + } + else + { + _crc <<= 1; + } + } +} + + +uint16_t CRC16::_reverse(uint16_t in) +{ + uint16_t x = in; + x = (((x & 0XAAAA) >> 1) | ((x & 0X5555) << 1)); + x = (((x & 0xCCCC) >> 2) | ((x & 0X3333) << 2)); + x = (((x & 0xF0F0) >> 4) | ((x & 0X0F0F) << 4)); + x = (( x >> 8) | (x << 8)); + return x; +} + +// -- END OF FILE -- \ No newline at end of file diff --git a/CRC16.h b/CRC16.h new file mode 100644 index 0000000..cfd0870 --- /dev/null +++ b/CRC16.h @@ -0,0 +1,50 @@ +#pragma once +// +// FILE: CRC16.h +// AUTHOR: Rob Tillaart +// PURPOSE: Arduino class for CRC16 +// URL: https://github.com/RobTillaart/CRC + + +#include "Arduino.h" + +#define CRC16_DEFAULT_POLYNOME 0x1021 + + +class CRC16 +{ +public: + CRC16(); + + // set parameters to default + void reset(); // set all to constructor defaults + void restart(); // reset crc with same parameters. + + // set parameters + void setPolynome(uint16_t polynome) { _polynome = polynome; }; + void setStartXOR(uint16_t start) { _startMask = start; }; + void setEndXOR(uint16_t end) { _endMask = end; }; + void setReverseIn(bool reverseIn) { _reverseIn = reverseIn; }; + void setReverseOut(bool reverseOut) { _reverseOut = reverseOut; }; + + void add(uint8_t value); + void add(uint8_t * array, uint32_t length); + + uint16_t getCRC(); // returns CRC + uint32_t count() { return _count; }; + +private: + uint16_t _reverse(uint16_t value); + void _update(uint8_t value); + + uint16_t _polynome; + uint16_t _startMask; + uint16_t _endMask; + uint16_t _crc; + bool _reverseIn; + bool _reverseOut; + bool _started; + uint32_t _count; +}; + +// -- END OF FILE -- \ No newline at end of file diff --git a/CRC32.cpp b/CRC32.cpp new file mode 100644 index 0000000..fd089b3 --- /dev/null +++ b/CRC32.cpp @@ -0,0 +1,96 @@ +// +// FILE: CRC32.cpp +// AUTHOR: Rob Tillaart +// PURPOSE: Arduino class for CRC32; +// URL: https://github.com/RobTillaart/CRC + + +#include "CRC32.h" + + +CRC32::CRC32() +{ + reset(); +} + + +void CRC32::reset() +{ + _polynome = CRC32_DEFAULT_POLYNOME; + _startMask = 0; + _endMask = 0; + _crc = 0; + _reverseIn = false; + _reverseOut = false; + _started = false; + _count = 0; +} + + +void CRC32::restart() +{ + _started = true; + _crc = _startMask; + _count = 0; +} + + +void CRC32::add(uint8_t value) +{ + _count++; + _update(value); +} + + +void CRC32::add(uint8_t * array, uint32_t length) +{ + _count += length; + while (length--) + { + yield(); + _update(*array++); + } +} + + +uint32_t CRC32::getCRC() +{ + uint32_t rv = _crc; + if (_reverseOut) rv = _reverse(rv); + rv ^=_endMask; + return rv; +} + + +void CRC32::_update(uint8_t value) +{ + if (!_started) restart(); + if (_reverseIn) value = _reverse(value); + _crc ^= ((uint32_t)value) << 24;; + for (uint8_t i = 8; i; i--) + { + if (_crc & (1UL << 31)) + { + _crc <<= 1; + _crc ^= _polynome; + } + else + { + _crc <<= 1; + } + } +} + + +uint32_t CRC32::_reverse(uint32_t in) +{ + uint32_t x = in; + x = (((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1)); + x = (((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2)); + x = (((x & 0xF0F0F0F0) >> 4) | ((x & 0x0F0F0F0F) << 4)); + x = (((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8)); + x = (x >> 16) | (x << 16); + return x; +} + +// -- END OF FILE -- \ No newline at end of file diff --git a/CRC32.h b/CRC32.h new file mode 100644 index 0000000..c416537 --- /dev/null +++ b/CRC32.h @@ -0,0 +1,50 @@ +#pragma once +// +// FILE: CRC32.h +// AUTHOR: Rob Tillaart +// PURPOSE: Arduino class for CRC32 +// URL: https://github.com/RobTillaart/CRC + + +#include "Arduino.h" + +#define CRC32_DEFAULT_POLYNOME 0x04C11DB7 + + +class CRC32 +{ +public: + CRC32(); + + // set parameters to default + void reset(); // set all to constructor defaults + void restart(); // reset crc with same parameters. + + // set parameters + void setPolynome(uint32_t polynome) { _polynome = polynome; }; + void setStartXOR(uint32_t start) { _startMask = start; }; + void setEndXOR(uint32_t end) { _endMask = end; }; + void setReverseIn(bool reverseIn) { _reverseIn = reverseIn; }; + void setReverseOut(bool reverseOut) { _reverseOut = reverseOut; }; + + void add(uint8_t value); + void add(uint8_t * array, uint32_t length); + + uint32_t getCRC(); // returns CRC + uint32_t count() { return _count; }; + +private: + uint32_t _reverse(uint32_t value); + void _update(uint8_t value); + + uint32_t _polynome; + uint32_t _startMask; + uint32_t _endMask; + uint32_t _crc; + bool _reverseIn; + bool _reverseOut; + bool _started; + uint32_t _count; +}; + +// -- END OF FILE -- \ No newline at end of file diff --git a/CRC64.cpp b/CRC64.cpp new file mode 100644 index 0000000..1c0a6e3 --- /dev/null +++ b/CRC64.cpp @@ -0,0 +1,97 @@ +// +// FILE: CRC64.cpp +// AUTHOR: Rob Tillaart +// PURPOSE: Arduino class for CRC64; +// URL: https://github.com/RobTillaart/CRC + + +#include "CRC64.h" + + +CRC64::CRC64() +{ + reset(); +} + + +void CRC64::reset() +{ + _polynome = CRC64_DEFAULT_POLYNOME; + _startMask = 0; + _endMask = 0; + _crc = 0; + _reverseIn = false; + _reverseOut = false; + _started = false; + _count = 0; +} + + +void CRC64::restart() +{ + _started = true; + _crc = _startMask; + _count = 0; +} + + +void CRC64::add(uint8_t value) +{ + _count++; + _update(value); +} + + +void CRC64::add(uint8_t * array, uint32_t length) +{ + _count += length; + while (length--) + { + yield(); + _update(*array++); + } +} + + +uint64_t CRC64::getCRC() +{ + uint64_t rv = _crc; + if (_reverseOut) rv = _reverse(rv); + rv ^=_endMask; + return rv; +} + + +void CRC64::_update(uint8_t value) +{ + if (!_started) restart(); + if (_reverseIn) value = _reverse(value); + _crc ^= ((uint64_t)value) << 56;; + for (uint8_t i = 8; i; i--) + { + if (_crc & (1ULL << 63)) + { + _crc <<= 1; + _crc ^= _polynome; + } + else + { + _crc <<= 1; + } + } +} + + +uint64_t CRC64::_reverse(uint64_t in) +{ + uint64_t x = in; + x = (((x & 0xAAAAAAAAAAAAAAAA) >> 1) | ((x & 0x5555555555555555) << 1)); + x = (((x & 0xCCCCCCCCCCCCCCCC) >> 2) | ((x & 0x3333333333333333) << 2)); + x = (((x & 0xF0F0F0F0F0F0F0F0) >> 4) | ((x & 0x0F0F0F0F0F0F0F0F) << 4)); + x = (((x & 0xFF00FF00FF00FF00) >> 8) | ((x & 0x00FF00FF00FF00FF) << 8)); + x = (((x & 0xFFFF0000FFFF0000) >> 16) | ((x & 0x0000FFFF0000FFFF) << 16)); + x = (x >> 32) | (x << 32); + return x; +} + +// -- END OF FILE -- \ No newline at end of file diff --git a/CRC64.h b/CRC64.h new file mode 100644 index 0000000..1b95422 --- /dev/null +++ b/CRC64.h @@ -0,0 +1,50 @@ +#pragma once +// +// FILE: CRC64.h +// AUTHOR: Rob Tillaart +// PURPOSE: Arduino class for CRC64 +// URL: https://github.com/RobTillaart/CRC + + +#include "Arduino.h" + +#define CRC64_DEFAULT_POLYNOME 0x814141AB // TODO + + +class CRC64 +{ +public: + CRC64(); + + // set parameters to default + void reset(); // set all to constructor defaults + void restart(); // reset crc with same parameters. + + // set parameters + void setPolynome(uint64_t polynome) { _polynome = polynome; }; + void setStartXOR(uint64_t start) { _startMask = start; }; + void setEndXOR(uint64_t end) { _endMask = end; }; + void setReverseIn(bool reverseIn) { _reverseIn = reverseIn; }; + void setReverseOut(bool reverseOut) { _reverseOut = reverseOut; }; + + void add(uint8_t value); + void add(uint8_t * array, uint32_t length); + + uint64_t getCRC(); // returns CRC + uint64_t count() { return _count; }; + +private: + uint64_t _reverse(uint64_t value); + void _update(uint8_t value); + + uint64_t _polynome; + uint64_t _startMask; + uint64_t _endMask; + uint64_t _crc; + bool _reverseIn; + bool _reverseOut; + bool _started; + uint64_t _count; +}; + +// -- END OF FILE -- \ No newline at end of file diff --git a/CRC8.cpp b/CRC8.cpp new file mode 100644 index 0000000..3a36c3a --- /dev/null +++ b/CRC8.cpp @@ -0,0 +1,94 @@ +// +// FILE: CRC8.cpp +// AUTHOR: Rob Tillaart +// PURPOSE: Arduino class for CRC8; +// URL: https://github.com/RobTillaart/CRC + + +#include "CRC8.h" + + +CRC8::CRC8() +{ + reset(); +} + + +void CRC8::reset() +{ + _polynome = CRC8_DEFAULT_POLYNOME; + _startMask = 0; + _endMask = 0; + _crc = 0; + _reverseIn = false; + _reverseOut = false; + _started = false; + _count = 0; +} + + +void CRC8::restart() +{ + _started = true; + _crc = _startMask; + _count = 0; +} + + +void CRC8::add(uint8_t value) +{ + _count++; + _update(value); +} + + +void CRC8::add(uint8_t * array, uint32_t length) +{ + _count += length; + while (length--) + { + yield(); + _update(*array++); + } +} + + +uint8_t CRC8::getCRC() +{ + uint8_t rv = _crc; + if (_reverseOut) rv = _reverse(rv); + rv ^= _endMask; + return rv; +} + + +void CRC8::_update(uint8_t value) +{ + if (!_started) restart(); + if (_reverseIn) value = _reverse(value); + _crc ^= value; + for (uint8_t i = 8; i; i--) + { + if (_crc & (1 << 7)) + { + _crc <<= 1; + _crc ^= _polynome; + } + else + { + _crc <<= 1; + } + } +} + + +uint8_t CRC8::_reverse(uint8_t in) +{ + uint8_t x = in; + x = (((x & 0xAA) >> 1) | ((x & 0x55) << 1)); + x = (((x & 0xCC) >> 2) | ((x & 0x33) << 2)); + x = ((x >> 4) | (x << 4)); + return x; +} + +// -- END OF FILE -- \ No newline at end of file diff --git a/CRC8.h b/CRC8.h new file mode 100644 index 0000000..3f6eb5d --- /dev/null +++ b/CRC8.h @@ -0,0 +1,50 @@ +#pragma once +// +// FILE: CRC8.h +// AUTHOR: Rob Tillaart +// PURPOSE: Arduino class for CRC8 +// URL: https://github.com/RobTillaart/CRC + + +#include "Arduino.h" + +#define CRC8_DEFAULT_POLYNOME 0x07 + + +class CRC8 +{ +public: + CRC8(); + + // set parameters to default + void reset(); // set all to constructor defaults + void restart(); // reset crc with same parameters. + + // set parameters + void setPolynome(uint8_t polynome) { _polynome = polynome; }; + void setStartXOR(uint8_t start) { _startMask = start; }; + void setEndXOR(uint8_t end) { _endMask = end; }; + void setReverseIn(bool reverseIn) { _reverseIn = reverseIn; }; + void setReverseOut(bool reverseOut) { _reverseOut = reverseOut; }; + + void add(uint8_t value); + void add(uint8_t * array, uint32_t length); + + uint8_t getCRC(); // returns CRC + uint32_t count() { return _count; }; + +private: + uint8_t _reverse(uint8_t value); + void _update(uint8_t value); + + uint8_t _polynome; + uint8_t _startMask; + uint8_t _endMask; + uint8_t _crc; + bool _reverseIn; + bool _reverseOut; + bool _started; + uint32_t _count; +}; + +// -- END OF FILE -- \ No newline at end of file diff --git a/README.md b/README.md index a6b006e..e03cdb4 100644 --- a/README.md +++ b/README.md @@ -11,42 +11,88 @@ Arduino library with CRC8, CRC16, CRC32 and CRC64 functions ## Description -Implements the generic CRC functions. +Implements the generic CRC functions and classes. + +The added value of the CRCx class is that it allows one to see intermediate CRC values calculated so far, but also + + +Goal of this library is to have a flexible and portable set of functions. + +**Note** the classes have same names as the static functions, except the class +is UPPER case. So CRC8 is a class and **crc8()** is the function. Deeper tech info -https://en.wikipedia.org/wiki/Cyclic_redundancy_check and many other websites. -Goal of this library is to have a flexible and portable set of functions. +## Interface CRC classes + +These interfaces are very similar for CRC8, CRC16, CRC32 and CRC64 class. +The only difference is the datatype for polynome, start- and endmask, and the returned CRC. + +Use **\#include "CRC8.h"** -## Interface +- **CRC8()** Constructor +- **void reset()** set all internals to constructor defaults. +- **void restart()** reset CRC and count; use same parameters again. +- **void setPolynome(polynome)** set polynome, reset sets a default polynome. +- **void setStartXOR(start)** set startmask, default 0. +- **void setEndXOR(end)** set endmask, default 0. +- **void setReverseIn(bool reverseIn)** reverse the bitpattern of input data (MSB vs LSB). +- **void setReverseOut(bool reverseOut)** reverse the bitpattern of CRC (MSB vs LSB). +- **void add(value)** add a single value to CRC calculation. +- **void add(array, uint32_t length)** add an array of values to the CRC. In case of a warning/error use casting to (uint8_t \*). +- **uint8_t getCRC()** returns CRC calculated so far. This allows to check the CRC of a really large stream at intermediate moments, e.g. to link multiple packets. +- **uint32_t count()** returns number of values added sofar. Default 0. + + +### Example snippet + +A minimal usage only needs +- the constructor, the add() function and the getCRC() function. + +```cpp +CRC32 crc; + ... + while (Serial.available()) + { + int c = Serial.read(); + crc.add(c); + } + Serial.println(crc.getCRC()); +``` + + +## Interface static functions + +Use **\#include "CRC.h"** Most functions have a default polynome, same start and end masks, and default there is no reversing. However these parameters allow one to tweak the CRC in all aspects known. In all the examples encountered the reverseflags were set both to false or both to true. For flexibility both parameters are kept available. -- **uint8_t CRC8(array, length, polynome = 0xD5, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome -- **uint16_t CRC16(array, length, polynome = 0xA001, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome -- **uint16_t CRC16-CCITT(array, length)** fixed polynome **0x1021**, non zero start / end masks -- **uint32_t CRC32(array, length, polynome = 0x04C11DB7, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome -- **uint64_t CRC64(array, length, polynome, start, end, reverseIn, reverseOut)** - experimental version, no reference found except on Wikipedia - +- **uint8_t crc8(array, length, polynome = 0xD5, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome +- **uint16_t crc16(array, length, polynome = 0xA001, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome +- **uint16_t crc16-CCITT(array, length)** fixed polynome **0x1021**, non zero start / end masks. +- **uint32_t crc32(array, length, polynome = 0x04C11DB7, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome +- **uint64_t crc64(array, length, polynome, start, end, reverseIn, reverseOut)** - experimental version, no reference found except on Wikipedia -There are many other CRC's but the most used ones can be handled with these functions. +Note these functions are limited to one call per block of data. For more flexibility use the classes. ## Future -- should become classes with both incremental and bulk options, baseclass CRC. -- table versions for perforrmane? +- extend examples. +- table versions for performance? (performance - memory discussion) +- example showing multiple packages of data linked by their CRC. - stream version - 4 classes class? #### Exotic CRC's ? - **CRC1()** // parity :) -- **CRC4(array, length, polynome, start, end, reverseIN, reverseOut)** +- **CRC4(array, length, polynome, start, end, reverseIn, reverseOut)** - default polynome 0x03 @@ -58,4 +104,4 @@ There are many other CRC's but the most used ones can be handled with these func ## Operational -See examples. (to be extended) +See examples. diff --git a/examples/CRC16_test/CRC16_test.ino b/examples/CRC16_test/CRC16_test.ino new file mode 100644 index 0000000..a2b10cc --- /dev/null +++ b/examples/CRC16_test/CRC16_test.ino @@ -0,0 +1,62 @@ +// +// FILE: CRC16_test.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo +// DATE: 2021-01-20 +// (c) : MIT + +#include "CRC16.h" +#include "CRC.h" + +char str[24] = "123456789"; + +CRC16 crc; + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + // Serial.println("Verified with - https://crccalc.com/\n"); + + test(); +} + + +void loop() +{ +} + +void test() +{ + Serial.println(crc16((uint8_t *) str, 9, 0x1021, 0, 0, false, false), HEX); + + crc.setPolynome(0x1021); + crc.add((uint8_t*)str, 9); + Serial.println(crc.getCRC(), HEX); + + crc.reset(); + crc.setPolynome(0x1021); + for (int i = 0; i < 9; i++) + { + crc.add(str[i]); + Serial.print(i); + Serial.print("\t"); + Serial.println(crc.getCRC(), HEX); + } + + crc.restart(); + for (int i = 0; i < 9; i++) + { + crc.add(str[i]); + } + Serial.println(crc.getCRC(), HEX); + for (int i = 0; i < 9; i++) + { + crc.add(str[i]); + } + Serial.println(crc.getCRC(), HEX); + Serial.println(crc.count()); +} + +// -- END OF FILE -- diff --git a/examples/CRC32_test/CRC32_test.ino b/examples/CRC32_test/CRC32_test.ino new file mode 100644 index 0000000..57aa41c --- /dev/null +++ b/examples/CRC32_test/CRC32_test.ino @@ -0,0 +1,57 @@ +// +// FILE: CRC32_test.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo +// DATE: 2021-01-20 +// (c) : MIT + +#include "CRC32.h" + +char str[24] = "123456789"; + +CRC32 crc; + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + // Serial.println("Verified with - https://crccalc.com/\n"); + + test(); +} + + +void loop() +{ +} + +void test() +{ + crc.setPolynome(0x04C11DB7); + crc.add((uint8_t*)str, 9); + Serial.println(crc.getCRC(), HEX); + + crc.reset(); + crc.setPolynome(0x04C11DB7); + for (int i = 0; i < 9; i++) + { + crc.add(str[i]); + } + Serial.println(crc.getCRC(), HEX); + + crc.restart(); + for (int i = 0; i < 9; i++) + { + crc.add(str[i]); + } + Serial.println(crc.getCRC(), HEX); + for (int i = 0; i < 9; i++) + { + crc.add(str[i]); + } + Serial.println(crc.getCRC(), HEX); + Serial.println(crc.count()); +} + +// -- END OF FILE -- diff --git a/examples/CRC64_test/CRC64_test.ino b/examples/CRC64_test/CRC64_test.ino new file mode 100644 index 0000000..7561618 --- /dev/null +++ b/examples/CRC64_test/CRC64_test.ino @@ -0,0 +1,58 @@ +// +// FILE: CRC64_test.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo +// DATE: 2021-01-20 +// (c) : MIT + +#include "CRC64.h" +#include "printHelpers.h" // https://github.com/RobTillaart/printHelpers + +char str[24] = "123456789"; + +CRC64 crc; + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + // Serial.println("Verified with - https://crccalc.com/\n"); + + test(); +} + + +void loop() +{ +} + +void test() +{ + crc.setPolynome(0x07); + crc.add((uint8_t*)str, 9); + Serial.println(print64(crc.getCRC(), HEX)); + + crc.reset(); + crc.setPolynome(0x07); + for (int i = 0; i < 9; i++) + { + crc.add(str[i]); + } + Serial.println(print64(crc.getCRC(), HEX)); + + crc.restart(); + for (int i = 0; i < 9; i++) + { + crc.add(str[i]); + } + Serial.println(print64(crc.getCRC(), HEX)); + for (int i = 0; i < 9; i++) + { + crc.add(str[i]); + } + Serial.println(print64(crc.getCRC(), HEX)); + Serial.println(print64(crc.count())); +} + +// -- END OF FILE -- diff --git a/examples/CRC8_test/CRC8_test.ino b/examples/CRC8_test/CRC8_test.ino new file mode 100644 index 0000000..f839aa1 --- /dev/null +++ b/examples/CRC8_test/CRC8_test.ino @@ -0,0 +1,63 @@ +// +// FILE: CRC8_test.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo +// DATE: 2021-01-20 +// (c) : MIT + + +#include "CRC8.h" + +#include "CRC.h" + + +char str[24] = "123456789"; + +CRC8 crc; + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + // Serial.println("Verified with - https://crccalc.com/\n"); + + test(); +} + + +void loop() +{ +} + +void test() +{ + Serial.println(crc8((uint8_t *)str, 9, 0x07), HEX); + + crc.setPolynome(0x07); + crc.add((uint8_t*)str, 9); + Serial.println(crc.getCRC(), HEX); + + crc.reset(); + crc.setPolynome(0x07); + for (int i = 0; i < 9; i++) + { + crc.add(str[i]); + } + Serial.println(crc.getCRC(), HEX); + + crc.restart(); + for (int i = 0; i < 9; i++) + { + crc.add(str[i]); + } + Serial.println(crc.getCRC(), HEX); + for (int i = 0; i < 9; i++) + { + crc.add(str[i]); + } + Serial.println(crc.getCRC(), HEX); + Serial.println(crc.count()); +} + +// -- END OF FILE -- diff --git a/examples/CRC_performance/CRC_performance.ino b/examples/CRC_performance/CRC_performance.ino index e6943ba..834a13b 100644 --- a/examples/CRC_performance/CRC_performance.ino +++ b/examples/CRC_performance/CRC_performance.ino @@ -1,7 +1,6 @@ // // FILE: CRC_performance.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 // PURPOSE: demo // DATE: 2020 // (c) : MIT @@ -26,7 +25,7 @@ void setup() delay(100); start = micros(); - uint8_t x8 = CRC8(data, len, 0x07, 0x00, 0x00, false, false); + uint8_t x8 = crc8(data, len, 0x07, 0x00, 0x00, false, false); stop = micros(); Serial.print("CRC8:\t"); Serial.println(x8, HEX); @@ -35,7 +34,7 @@ void setup() delay(100); start = micros(); - x8 = CRC8(data, len, 0x07, 0x00, 0x00, true, true); + x8 = crc8(data, len, 0x07, 0x00, 0x00, true, true); stop = micros(); Serial.print("CRC8:\t"); Serial.println(x8, HEX); @@ -46,7 +45,7 @@ void setup() start = micros(); - uint16_t x16 = CRC16(data, len, 0x1021, 0xFFFF, 0x0000, false, false ); + uint16_t x16 = crc16(data, len, 0x1021, 0xFFFF, 0x0000, false, false ); stop = micros(); Serial.print("CRC16:\t"); Serial.println(x16, HEX); @@ -55,7 +54,7 @@ void setup() delay(100); start = micros(); - x16 = CRC16(data, len, 0x1021, 0xFFFF, 0x0000, true, true ); + x16 = crc16(data, len, 0x1021, 0xFFFF, 0x0000, true, true ); stop = micros(); Serial.print("CRC16:\t"); Serial.println(x16, HEX); @@ -66,7 +65,7 @@ void setup() start = micros(); - uint32_t x32 = CRC32(data, len, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false); + uint32_t x32 = crc32(data, len, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false); stop = micros(); Serial.print("CRC32:\t"); Serial.println(x32, HEX); @@ -75,7 +74,7 @@ void setup() delay(100); start = micros(); - x32 = CRC32(data, len, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true); + x32 = crc32(data, len, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true); stop = micros(); Serial.print("CRC32:\t"); Serial.println(x32, HEX); @@ -85,7 +84,7 @@ void setup() delay(100); start = micros(); - uint64_t x64 = CRC64(data, len, 0x814141AB, 0x00000000, 0x00000000, false, false); + uint64_t x64 = crc64(data, len, 0x814141AB, 0x00000000, 0x00000000, false, false); stop = micros(); Serial.print("CRC64:\t"); Serial.print((uint32_t)(x64 >> 32), HEX); @@ -95,7 +94,7 @@ void setup() delay(100); start = micros(); - x64 = CRC64(data, len, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true); + x64 = crc64(data, len, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true); stop = micros(); Serial.print("CRC64:\t"); Serial.print((uint32_t)(x64 >> 32), HEX); diff --git a/examples/CRC_reference_test/CRC_reference_test.ino b/examples/CRC_reference_test/CRC_reference_test.ino index ebeb035..526850e 100644 --- a/examples/CRC_reference_test/CRC_reference_test.ino +++ b/examples/CRC_reference_test/CRC_reference_test.ino @@ -1,7 +1,6 @@ // // FILE: CRC_reference_test.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 // PURPOSE: demo // DATE: 2020 // (c) : MIT @@ -25,61 +24,61 @@ void setup() Serial.print("TEST:\t"); Serial.println(str); Serial.println("\nCRC8\n--------------------"); - Serial.println(CRC8(data, 9, 0x07), HEX); - Serial.println(CRC8(data, 9, 0x9B, 0xFF), HEX); - Serial.println(CRC8(data, 9, 0x39, 0x00, 0x00, true, true), HEX); - Serial.println(CRC8(data, 9, 0xD5), HEX); - Serial.println(CRC8(data, 9, 0x1D, 0xFF, 0x00, true, true), HEX); - Serial.println(CRC8(data, 9, 0x1D, 0xFD), HEX); - Serial.println(CRC8(data, 9, 0x07, 0x00, 0x55), HEX); - Serial.println(CRC8(data, 9, 0x31, 0x00, 0x00, true, true), HEX); - Serial.println(CRC8(data, 9, 0x07, 0xFF, 0x00, true, true), HEX); - Serial.println(CRC8(data, 9, 0x9B, 0x00, 0x00, true, true), HEX); + Serial.println(crc8(data, 9, 0x07), HEX); + Serial.println(crc8(data, 9, 0x9B, 0xFF), HEX); + Serial.println(crc8(data, 9, 0x39, 0x00, 0x00, true, true), HEX); + Serial.println(crc8(data, 9, 0xD5), HEX); + Serial.println(crc8(data, 9, 0x1D, 0xFF, 0x00, true, true), HEX); + Serial.println(crc8(data, 9, 0x1D, 0xFD), HEX); + Serial.println(crc8(data, 9, 0x07, 0x00, 0x55), HEX); + Serial.println(crc8(data, 9, 0x31, 0x00, 0x00, true, true), HEX); + Serial.println(crc8(data, 9, 0x07, 0xFF, 0x00, true, true), HEX); + Serial.println(crc8(data, 9, 0x9B, 0x00, 0x00, true, true), HEX); Serial.println(); Serial.println("CRC16\n--------------------"); - Serial.println(CRC16(data, 9, 0x1021, 0xFFFF, 0x0000, false, false ), HEX); - Serial.println(CRC16(data, 9, 0x8005, 0x0000, 0x0000, true, true ), HEX); - Serial.println(CRC16(data, 9, 0x1021, 0x1D0F, 0x0000, false, false ), HEX); - Serial.println(CRC16(data, 9, 0x8005, 0x0000, 0x0000, false, false ), HEX); - Serial.println(CRC16(data, 9, 0xC867, 0xFFFF, 0x0000, false, false ), HEX); - Serial.println(CRC16(data, 9, 0x8005, 0x800D, 0x0000, false, false ), HEX); - Serial.println(CRC16(data, 9, 0x0589, 0x0000, 0x0001, false, false ), HEX); - Serial.println(CRC16(data, 9, 0x0589, 0x0000, 0x0000, false, false ), HEX); - Serial.println(CRC16(data, 9, 0x3D65, 0x0000, 0xFFFF, true, true ), HEX); - Serial.println(CRC16(data, 9, 0x3D65, 0x0000, 0xFFFF, false, false ), HEX); - Serial.println(CRC16(data, 9, 0x1021, 0xFFFF, 0xFFFF, false, false ), HEX); - Serial.println(CRC16(data, 9, 0x8005, 0x0000, 0xFFFF, true, true ), HEX); - Serial.println(CRC16(data, 9, 0x1021, 0xFFFF, 0x0000, true, true ), HEX); - Serial.println(CRC16(data, 9, 0x1021, 0xB2AA, 0x0000, true, true ), HEX); - Serial.println(CRC16(data, 9, 0x8BB7, 0x0000, 0x0000, false, false ), HEX); - Serial.println(CRC16(data, 9, 0xA097, 0x0000, 0x0000, false, false ), HEX); - Serial.println(CRC16(data, 9, 0x1021, 0x89EC, 0x0000, true, true ), HEX); - Serial.println(CRC16(data, 9, 0x8005, 0xFFFF, 0xFFFF, true, true ), HEX); - Serial.println(CRC16(data, 9, 0x1021, 0xC6C6, 0x0000, true, true ), HEX); - Serial.println(CRC16(data, 9, 0x1021, 0x0000, 0x0000, true, true ), HEX); - Serial.println(CRC16(data, 9, 0x8005, 0xFFFF, 0x0000, true, true ), HEX); - Serial.println(CRC16(data, 9, 0x1021, 0xFFFF, 0xFFFF, true, true ), HEX); - Serial.println(CRC16(data, 9, 0x1021, 0x0000, 0x0000, false, false ), HEX); + Serial.println(crc16(data, 9, 0x1021, 0xFFFF, 0x0000, false, false ), HEX); + Serial.println(crc16(data, 9, 0x8005, 0x0000, 0x0000, true, true ), HEX); + Serial.println(crc16(data, 9, 0x1021, 0x1D0F, 0x0000, false, false ), HEX); + Serial.println(crc16(data, 9, 0x8005, 0x0000, 0x0000, false, false ), HEX); + Serial.println(crc16(data, 9, 0xC867, 0xFFFF, 0x0000, false, false ), HEX); + Serial.println(crc16(data, 9, 0x8005, 0x800D, 0x0000, false, false ), HEX); + Serial.println(crc16(data, 9, 0x0589, 0x0000, 0x0001, false, false ), HEX); + Serial.println(crc16(data, 9, 0x0589, 0x0000, 0x0000, false, false ), HEX); + Serial.println(crc16(data, 9, 0x3D65, 0x0000, 0xFFFF, true, true ), HEX); + Serial.println(crc16(data, 9, 0x3D65, 0x0000, 0xFFFF, false, false ), HEX); + Serial.println(crc16(data, 9, 0x1021, 0xFFFF, 0xFFFF, false, false ), HEX); + Serial.println(crc16(data, 9, 0x8005, 0x0000, 0xFFFF, true, true ), HEX); + Serial.println(crc16(data, 9, 0x1021, 0xFFFF, 0x0000, true, true ), HEX); + Serial.println(crc16(data, 9, 0x1021, 0xB2AA, 0x0000, true, true ), HEX); + Serial.println(crc16(data, 9, 0x8BB7, 0x0000, 0x0000, false, false ), HEX); + Serial.println(crc16(data, 9, 0xA097, 0x0000, 0x0000, false, false ), HEX); + Serial.println(crc16(data, 9, 0x1021, 0x89EC, 0x0000, true, true ), HEX); + Serial.println(crc16(data, 9, 0x8005, 0xFFFF, 0xFFFF, true, true ), HEX); + Serial.println(crc16(data, 9, 0x1021, 0xC6C6, 0x0000, true, true ), HEX); + Serial.println(crc16(data, 9, 0x1021, 0x0000, 0x0000, true, true ), HEX); + Serial.println(crc16(data, 9, 0x8005, 0xFFFF, 0x0000, true, true ), HEX); + Serial.println(crc16(data, 9, 0x1021, 0xFFFF, 0xFFFF, true, true ), HEX); + Serial.println(crc16(data, 9, 0x1021, 0x0000, 0x0000, false, false ), HEX); Serial.println(); Serial.println("CRC32\n--------------------"); - Serial.println(CRC32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true), HEX); - Serial.println(CRC32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false), HEX); - Serial.println(CRC32(data, 9, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true), HEX); - Serial.println(CRC32(data, 9, 0xA833982B, 0xFFFFFFFF, 0xFFFFFFFF, true, true), HEX); - Serial.println(CRC32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, false, false), HEX); - Serial.println(CRC32(data, 9, 0x04C11DB7, 0x00000000, 0xFFFFFFFF, false, false), HEX); - Serial.println(CRC32(data, 9, 0x814141AB, 0x00000000, 0x00000000, false, false), HEX); - Serial.println(CRC32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, true, true), HEX); - Serial.println(CRC32(data, 9, 0x000000AF, 0x00000000, 0x00000000, false, false), HEX); + Serial.println(crc32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true), HEX); + Serial.println(crc32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false), HEX); + Serial.println(crc32(data, 9, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true), HEX); + Serial.println(crc32(data, 9, 0xA833982B, 0xFFFFFFFF, 0xFFFFFFFF, true, true), HEX); + Serial.println(crc32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, false, false), HEX); + Serial.println(crc32(data, 9, 0x04C11DB7, 0x00000000, 0xFFFFFFFF, false, false), HEX); + Serial.println(crc32(data, 9, 0x814141AB, 0x00000000, 0x00000000, false, false), HEX); + Serial.println(crc32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, true, true), HEX); + Serial.println(crc32(data, 9, 0x000000AF, 0x00000000, 0x00000000, false, false), HEX); Serial.println(); Serial.println("\nCRC64 - no reference\n--------------------"); - uint64_t t = CRC64(data, 9, 0x814141AB, 0x00000000, 0x00000000, false, false); + uint64_t t = crc64(data, 9, 0x814141AB, 0x00000000, 0x00000000, false, false); Serial.println(print64(t, HEX)); diff --git a/examples/CRC_test/CRC_test.ino b/examples/CRC_test/CRC_test.ino new file mode 100644 index 0000000..f45cc02 --- /dev/null +++ b/examples/CRC_test/CRC_test.ino @@ -0,0 +1,43 @@ +// +// FILE: CRC_test.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo +// DATE: 2020 +// (c) : MIT +// + +#include "CRC.h" +#include "printHelpers.h" // for the 64 bit... + +char str[24] = "123456789"; + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + uint8_t * data = (uint8_t *) &str[0]; + + Serial.println("Verified with - https://crccalc.com/\n"); + + Serial.print("TEST:\t"); + Serial.println(str); + Serial.print("CRC8:\t"); + Serial.println(crc8(data, 9, 0x07, 0x00, 0x00, false, false), HEX); + Serial.print("CRC16:\t"); + Serial.println(crc16(data, 9, 0x1021, 0x0000, 0x0000, false, false ), HEX); + Serial.print("CRC32:\t"); + Serial.println(crc32(data, 9, 0x04C11DB7, 0x00000000, 0x00000000, false, false), HEX); +// Serial.print("*CRC64:\t"); +// uint64_t t = crc64(data, 9, 0x814141AB, 0x00000000, 0x00000000, false, false); +// Serial.println(print64(t, HEX)); + + Serial.println("\n\nDone..."); +} + +void loop() +{ + +} + +// -- END OF FILE -- diff --git a/keywords.txt b/keywords.txt index ef8b486..1a7bdff 100644 --- a/keywords.txt +++ b/keywords.txt @@ -3,11 +3,11 @@ # Datatypes (KEYWORD1) # Methods and Functions (KEYWORD2) -CRC8 KEYWORD2 -CRC16 KEYWORD2 -CRC16_CCITT KEYWORD2 -CRC32 KEYWORD2 -CRC64 KEYWORD2 +crc8 KEYWORD2 +crc16 KEYWORD2 +crc16_CCITT KEYWORD2 +crc32 KEYWORD2 +crc64 KEYWORD2 # Instances (KEYWORD2) diff --git a/library.json b/library.json index fb03c14..0708e3f 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/CRC" }, - "version":"0.1.0", + "version":"0.1.1", "frameworks": "arduino", "platforms": "*" } diff --git a/library.properties b/library.properties index 301c94c..9f43715 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=CRC -version=0.1.0 +version=0.1.1 author=Rob Tillaart maintainer=Rob Tillaart sentence=Library for CRC for Arduino @@ -7,5 +7,5 @@ paragraph=CRC8, CRC16, CRC16-CCITT, CRC32, CRC64 category=Data Processing url=https://github.com/RobTillaart/CRC architectures=* -includes=CRC.h +includes=CRC.h,CRC8.h,CRC16.h,CRC32.h,CRC64.h depends= diff --git a/test/unit_test_001.cpp b/test/unit_test_001.cpp index 2062e90..06194e0 100644 --- a/test/unit_test_001.cpp +++ b/test/unit_test_001.cpp @@ -1,4 +1,4 @@ - // +// // FILE: unit_test_001.cpp // AUTHOR: Rob Tillaart // DATE: 2021-01-01 @@ -50,70 +50,70 @@ unittest_teardown() } -unittest(test_CRC8) +unittest(test_crc8) { fprintf(stderr, "VERSION: %s\n", CRC_LIB_VERSION); - assertEqual(0xF4, CRC8(data, 9, 0x07)); - assertEqual(0xDA, CRC8(data, 9, 0x9B, 0xFF)); - assertEqual(0x15, CRC8(data, 9, 0x39, 0x00, 0x00, true, true)); - assertEqual(0xBC, CRC8(data, 9, 0xD5)); - assertEqual(0x97, CRC8(data, 9, 0x1D, 0xFF, 0x00, true, true)); - assertEqual(0x7E, CRC8(data, 9, 0x1D, 0xFD)); - assertEqual(0xA1, CRC8(data, 9, 0x07, 0x00, 0x55)); - assertEqual(0xA1, CRC8(data, 9, 0x31, 0x00, 0x00, true, true)); - assertEqual(0xD0, CRC8(data, 9, 0x07, 0xFF, 0x00, true, true)); - assertEqual(0x25, CRC8(data, 9, 0x9B, 0x00, 0x00, true, true)); + assertEqual(0xF4, crc8(data, 9, 0x07)); + assertEqual(0xDA, crc8(data, 9, 0x9B, 0xFF)); + assertEqual(0x15, crc8(data, 9, 0x39, 0x00, 0x00, true, true)); + assertEqual(0xBC, crc8(data, 9, 0xD5)); + assertEqual(0x97, crc8(data, 9, 0x1D, 0xFF, 0x00, true, true)); + assertEqual(0x7E, crc8(data, 9, 0x1D, 0xFD)); + assertEqual(0xA1, crc8(data, 9, 0x07, 0x00, 0x55)); + assertEqual(0xA1, crc8(data, 9, 0x31, 0x00, 0x00, true, true)); + assertEqual(0xD0, crc8(data, 9, 0x07, 0xFF, 0x00, true, true)); + assertEqual(0x25, crc8(data, 9, 0x9B, 0x00, 0x00, true, true)); } -unittest(test_CRC16) +unittest(test_crc16) { fprintf(stderr, "VERSION: %s\n", CRC_LIB_VERSION); - assertEqual(0x29B1, CRC16(data, 9, 0x1021, 0xFFFF, 0x0000, false, false )); - assertEqual(0xBB3D, CRC16(data, 9, 0x8005, 0x0000, 0x0000, true, true )); - assertEqual(0xE5CC, CRC16(data, 9, 0x1021, 0x1D0F, 0x0000, false, false )); - assertEqual(0xFEE8, CRC16(data, 9, 0x8005, 0x0000, 0x0000, false, false )); - assertEqual(0x4C06, CRC16(data, 9, 0xC867, 0xFFFF, 0x0000, false, false )); - assertEqual(0x9ECF, CRC16(data, 9, 0x8005, 0x800D, 0x0000, false, false )); - assertEqual(0x007E, CRC16(data, 9, 0x0589, 0x0000, 0x0001, false, false )); - assertEqual(0x007F, CRC16(data, 9, 0x0589, 0x0000, 0x0000, false, false )); - assertEqual(0xEA82, CRC16(data, 9, 0x3D65, 0x0000, 0xFFFF, true, true )); - assertEqual(0xC2B7, CRC16(data, 9, 0x3D65, 0x0000, 0xFFFF, false, false )); - assertEqual(0xD64E, CRC16(data, 9, 0x1021, 0xFFFF, 0xFFFF, false, false )); - assertEqual(0x44C2, CRC16(data, 9, 0x8005, 0x0000, 0xFFFF, true, true )); - assertEqual(0x6F91, CRC16(data, 9, 0x1021, 0xFFFF, 0x0000, true, true )); - assertEqual(0x63D0, CRC16(data, 9, 0x1021, 0xB2AA, 0x0000, true, true )); - assertEqual(0xD0DB, CRC16(data, 9, 0x8BB7, 0x0000, 0x0000, false, false )); - assertEqual(0x0FB3, CRC16(data, 9, 0xA097, 0x0000, 0x0000, false, false )); - assertEqual(0x26B1, CRC16(data, 9, 0x1021, 0x89EC, 0x0000, true, true )); - assertEqual(0xB4C8, CRC16(data, 9, 0x8005, 0xFFFF, 0xFFFF, true, true )); - assertEqual(0xBF05, CRC16(data, 9, 0x1021, 0xC6C6, 0x0000, true, true )); - assertEqual(0x2189, CRC16(data, 9, 0x1021, 0x0000, 0x0000, true, true )); - assertEqual(0x4B37, CRC16(data, 9, 0x8005, 0xFFFF, 0x0000, true, true )); - assertEqual(0x906E, CRC16(data, 9, 0x1021, 0xFFFF, 0xFFFF, true, true )); - assertEqual(0x31C3, CRC16(data, 9, 0x1021, 0x0000, 0x0000, false, false )); + assertEqual(0x29B1, crc16(data, 9, 0x1021, 0xFFFF, 0x0000, false, false )); + assertEqual(0xBB3D, crc16(data, 9, 0x8005, 0x0000, 0x0000, true, true )); + assertEqual(0xE5CC, crc16(data, 9, 0x1021, 0x1D0F, 0x0000, false, false )); + assertEqual(0xFEE8, crc16(data, 9, 0x8005, 0x0000, 0x0000, false, false )); + assertEqual(0x4C06, crc16(data, 9, 0xC867, 0xFFFF, 0x0000, false, false )); + assertEqual(0x9ECF, crc16(data, 9, 0x8005, 0x800D, 0x0000, false, false )); + assertEqual(0x007E, crc16(data, 9, 0x0589, 0x0000, 0x0001, false, false )); + assertEqual(0x007F, crc16(data, 9, 0x0589, 0x0000, 0x0000, false, false )); + assertEqual(0xEA82, crc16(data, 9, 0x3D65, 0x0000, 0xFFFF, true, true )); + assertEqual(0xC2B7, crc16(data, 9, 0x3D65, 0x0000, 0xFFFF, false, false )); + assertEqual(0xD64E, crc16(data, 9, 0x1021, 0xFFFF, 0xFFFF, false, false )); + assertEqual(0x44C2, crc16(data, 9, 0x8005, 0x0000, 0xFFFF, true, true )); + assertEqual(0x6F91, crc16(data, 9, 0x1021, 0xFFFF, 0x0000, true, true )); + assertEqual(0x63D0, crc16(data, 9, 0x1021, 0xB2AA, 0x0000, true, true )); + assertEqual(0xD0DB, crc16(data, 9, 0x8BB7, 0x0000, 0x0000, false, false )); + assertEqual(0x0FB3, crc16(data, 9, 0xA097, 0x0000, 0x0000, false, false )); + assertEqual(0x26B1, crc16(data, 9, 0x1021, 0x89EC, 0x0000, true, true )); + assertEqual(0xB4C8, crc16(data, 9, 0x8005, 0xFFFF, 0xFFFF, true, true )); + assertEqual(0xBF05, crc16(data, 9, 0x1021, 0xC6C6, 0x0000, true, true )); + assertEqual(0x2189, crc16(data, 9, 0x1021, 0x0000, 0x0000, true, true )); + assertEqual(0x4B37, crc16(data, 9, 0x8005, 0xFFFF, 0x0000, true, true )); + assertEqual(0x906E, crc16(data, 9, 0x1021, 0xFFFF, 0xFFFF, true, true )); + assertEqual(0x31C3, crc16(data, 9, 0x1021, 0x0000, 0x0000, false, false )); } -unittest(test_CRC32) +unittest(test_crc32) { fprintf(stderr, "VERSION: %s\n", CRC_LIB_VERSION); - assertEqual(0xCBF43926, CRC32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true)); - assertEqual(0xFC891918, CRC32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false)); - assertEqual(0xE3069283, CRC32(data, 9, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true)); - assertEqual(0x87315576, CRC32(data, 9, 0xA833982B, 0xFFFFFFFF, 0xFFFFFFFF, true, true)); - assertEqual(0x0376E6E7, CRC32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, false, false)); - assertEqual(0x765E7680, CRC32(data, 9, 0x04C11DB7, 0x00000000, 0xFFFFFFFF, false, false)); - assertEqual(0x3010BF7F, CRC32(data, 9, 0x814141AB, 0x00000000, 0x00000000, false, false)); - assertEqual(0x340BC6D9, CRC32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, true, true)); - assertEqual(0xBD0BE338, CRC32(data, 9, 0x000000AF, 0x00000000, 0x00000000, false, false)); + assertEqual(0xCBF43926, crc32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true)); + assertEqual(0xFC891918, crc32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false)); + assertEqual(0xE3069283, crc32(data, 9, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true)); + assertEqual(0x87315576, crc32(data, 9, 0xA833982B, 0xFFFFFFFF, 0xFFFFFFFF, true, true)); + assertEqual(0x0376E6E7, crc32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, false, false)); + assertEqual(0x765E7680, crc32(data, 9, 0x04C11DB7, 0x00000000, 0xFFFFFFFF, false, false)); + assertEqual(0x3010BF7F, crc32(data, 9, 0x814141AB, 0x00000000, 0x00000000, false, false)); + assertEqual(0x340BC6D9, crc32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, true, true)); + assertEqual(0xBD0BE338, crc32(data, 9, 0x000000AF, 0x00000000, 0x00000000, false, false)); } -unittest(test_CRC64) +unittest(test_crc64) { fprintf(stderr, "VERSION: %s\n", CRC_LIB_VERSION);