diff --git a/libraries/CRC/CRC.h b/libraries/CRC/CRC.h index ad7d7083..61360764 100644 --- a/libraries/CRC/CRC.h +++ b/libraries/CRC/CRC.h @@ -2,7 +2,7 @@ // // FILE: CRC.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.3 +// VERSION: 0.1.4 // PURPOSE: Arduino library fir CRC8, CRC16, CRC16-CCITT, CRC32 // URL: https://github.com/RobTillaart/CRC // @@ -11,7 +11,7 @@ #include "Arduino.h" -#define CRC_LIB_VERSION (F("0.1.3")) +#define CRC_LIB_VERSION (F("0.1.4")) //////////////////////////////////////////////////////////////// @@ -64,6 +64,7 @@ uint64_t reverse64(uint64_t in) return x; } + /////////////////////////////////////////////////////////////////////////////////// // CRC POLYNOME = x8 + x5 + x4 + 1 = 1001 1000 = 0x8C @@ -186,3 +187,4 @@ uint64_t crc64(uint8_t *array, uint8_t length, uint64_t polynome, uint64_t start // -- END OF FILE -- + diff --git a/libraries/CRC/CRC16.cpp b/libraries/CRC/CRC16.cpp index cbeab997..13f5993a 100644 --- a/libraries/CRC/CRC16.cpp +++ b/libraries/CRC/CRC16.cpp @@ -93,6 +93,7 @@ uint16_t CRC16::_reverse(uint16_t in) return x; } + uint8_t CRC16::_reverse8(uint8_t in) { uint8_t x = in; @@ -102,4 +103,6 @@ uint8_t CRC16::_reverse8(uint8_t in) return x; } -// -- END OF FILE -- \ No newline at end of file + +// -- END OF FILE -- + diff --git a/libraries/CRC/CRC16.h b/libraries/CRC/CRC16.h index 9a8dd005..60a8c838 100644 --- a/libraries/CRC/CRC16.h +++ b/libraries/CRC/CRC16.h @@ -48,4 +48,6 @@ private: uint32_t _count; }; -// -- END OF FILE -- \ No newline at end of file + +// -- END OF FILE -- + diff --git a/libraries/CRC/CRC32.cpp b/libraries/CRC/CRC32.cpp index 4c5d3f20..f6e0f289 100644 --- a/libraries/CRC/CRC32.cpp +++ b/libraries/CRC/CRC32.cpp @@ -93,6 +93,7 @@ uint32_t CRC32::_reverse(uint32_t in) return x; } + uint8_t CRC32::_reverse8(uint8_t in) { uint8_t x = in; @@ -102,4 +103,6 @@ uint8_t CRC32::_reverse8(uint8_t in) return x; } -// -- END OF FILE -- \ No newline at end of file + +// -- END OF FILE -- + diff --git a/libraries/CRC/CRC32.h b/libraries/CRC/CRC32.h index 5b0ed766..ea5dc691 100644 --- a/libraries/CRC/CRC32.h +++ b/libraries/CRC/CRC32.h @@ -48,4 +48,6 @@ private: uint32_t _count; }; -// -- END OF FILE -- \ No newline at end of file + +// -- END OF FILE -- + diff --git a/libraries/CRC/CRC64.cpp b/libraries/CRC/CRC64.cpp index c3e47f52..d0b135a6 100644 --- a/libraries/CRC/CRC64.cpp +++ b/libraries/CRC/CRC64.cpp @@ -94,6 +94,7 @@ uint64_t CRC64::_reverse(uint64_t in) return x; } + uint8_t CRC64::_reverse8(uint8_t in) { uint8_t x = in; @@ -103,4 +104,6 @@ uint8_t CRC64::_reverse8(uint8_t in) return x; } -// -- END OF FILE -- \ No newline at end of file + +// -- END OF FILE -- + diff --git a/libraries/CRC/CRC64.h b/libraries/CRC/CRC64.h index 87c22f32..6c7f2780 100644 --- a/libraries/CRC/CRC64.h +++ b/libraries/CRC/CRC64.h @@ -48,4 +48,6 @@ private: uint64_t _count; }; -// -- END OF FILE -- \ No newline at end of file + +// -- END OF FILE -- + diff --git a/libraries/CRC/CRC8.cpp b/libraries/CRC/CRC8.cpp index 3a36c3ac..3bdc41dd 100644 --- a/libraries/CRC/CRC8.cpp +++ b/libraries/CRC/CRC8.cpp @@ -91,4 +91,6 @@ uint8_t CRC8::_reverse(uint8_t in) return x; } -// -- END OF FILE -- \ No newline at end of file + +// -- END OF FILE -- + diff --git a/libraries/CRC/CRC8.h b/libraries/CRC/CRC8.h index 3f6eb5da..7385c372 100644 --- a/libraries/CRC/CRC8.h +++ b/libraries/CRC/CRC8.h @@ -47,4 +47,6 @@ private: uint32_t _count; }; -// -- END OF FILE -- \ No newline at end of file + +// -- END OF FILE -- + diff --git a/libraries/CRC/LICENSE b/libraries/CRC/LICENSE index a8b455d0..5777550e 100644 --- a/libraries/CRC/LICENSE +++ b/libraries/CRC/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021-2021 Rob Tillaart +Copyright (c) 2021-2022 Rob Tillaart Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/libraries/CRC/README.md b/libraries/CRC/README.md index b9da0d50..58a786cd 100644 --- a/libraries/CRC/README.md +++ b/libraries/CRC/README.md @@ -8,21 +8,26 @@ # CRC -Arduino library with CRC8, CRC16, CRC32 and CRC64 functions +Arduino library with CRC8, CRC16, CRC32 and CRC64 functions. ## Description -Goal of this library is to have a flexible and portable set of generic CRC functions and classes. +Goal of this library is to have a flexible and portable set of generic +CRC functions and classes. -The CRCx classes have a number of added values. Most important is that they allow one to verify intermediate CRC values. This is useful if one sends a "train of packets" which include a CRC so far. This detects both errors in one packet but also missing packets, or injected packages. +The CRCx classes have a number of added values. Most important is that +they allow one to verify intermediate CRC values. This is useful if one +sends a "train of packets" which include a CRC so far. This detects both +errors in one packet but also missing packets, or injected packages. -Another trick one can do is change the polynome or the reverse flag during the process. -This makes it harder to imitate. +Another trick one can do is change the polynome or the reverse flag during +the process. This makes it harder to imitate. Furthermore the class allows to add values in single steps and continue too. -Finally the class version gives more readable code (imho) as the parameters are explicitly set. +Finally the class version gives more readable code (imho) as the parameters +are explicitly set. **Note** the classes have same names as the static functions, except the class @@ -35,27 +40,31 @@ and many other websites. ## Interface CRC classes These interfaces are very similar for CRC8, CRC16, CRC32 and CRC64 class. -The only difference is the data type for polynome, start- and end-mask, and the returned CRC. +The only difference is the data type for polynome, start- and end-mask, +and the returned CRC. Use **\#include "CRC8.h"** - **CRC8()** Constructor - **void reset()** set all internals to constructor defaults. -- **void restart()** reset internal CRC and count only; reuse values for other e.g polynome, XOR masks and reverse flags. +- **void restart()** reset internal CRC and count only; reuse values for other +e.g polynome, XOR masks and reverse flags. - **void setPolynome(polynome)** set polynome, note reset sets a default polynome. - **void setStartXOR(start)** set start-mask, default 0. - **void setEndXOR(end)** set end-mask, default 0. - **void setReverseIn(bool reverseIn)** reverse the bit pattern of input data (MSB vs LSB). - **void setReverseOut(bool reverseOut)** reverse the bit pattern 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. +- **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 so far. Default 0. ### Example snippet -A minimal usage only needs +A minimal usage only needs: - the constructor, the add() function and the getCRC() function. ```cpp @@ -82,15 +91,26 @@ However these parameters allow one to tweak the CRC in all aspects known. In all the examples encountered the reverse flags 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 +- **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 +- **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. Note these functions are limited to one call per block of data. For more flexibility use the classes. +## Operational + +See examples. + + +## Links + +- https://en.wikipedia.org/wiki/Cyclic_redundancy_check - generic background. +- https://crccalc.com/ - online CRC calculator to verify. + + ## Future - extend examples. @@ -105,16 +125,12 @@ Note these functions are limited to one call per block of data. For more flexibi #### Exotic CRC's ? - **CRC1()** // parity :) -- **CRC4(array, length, polynome, start, end, reverseIn, reverseOut)** +- **CRC4(array, length, polynome, start, end, reverseIn, reverseOut)** nibbles? - default polynome 0x03 -#### Magic \#defines for "common" polynomes? ? +#### Magic \#defines for "common" polynomes? verify ? - \#define CRC_ISO64 0x000000000000001B - \#define CRC_ECMA64 0x42F0E1EBA9EA3693 - -## Operational - -See examples. diff --git a/libraries/CRC/examples/CRC16_test/CRC16_test.ino b/libraries/CRC/examples/CRC16_test/CRC16_test.ino index a2b10cc4..954601e7 100644 --- a/libraries/CRC/examples/CRC16_test/CRC16_test.ino +++ b/libraries/CRC/examples/CRC16_test/CRC16_test.ino @@ -5,6 +5,7 @@ // DATE: 2021-01-20 // (c) : MIT + #include "CRC16.h" #include "CRC.h" @@ -12,6 +13,7 @@ char str[24] = "123456789"; CRC16 crc; + void setup() { Serial.begin(115200); @@ -27,6 +29,7 @@ void loop() { } + void test() { Serial.println(crc16((uint8_t *) str, 9, 0x1021, 0, 0, false, false), HEX); @@ -59,4 +62,6 @@ void test() Serial.println(crc.count()); } + // -- END OF FILE -- + diff --git a/libraries/CRC/examples/CRC32_test/CRC32_test.ino b/libraries/CRC/examples/CRC32_test/CRC32_test.ino index 57aa41c6..23225a03 100644 --- a/libraries/CRC/examples/CRC32_test/CRC32_test.ino +++ b/libraries/CRC/examples/CRC32_test/CRC32_test.ino @@ -5,12 +5,14 @@ // DATE: 2021-01-20 // (c) : MIT + #include "CRC32.h" char str[24] = "123456789"; CRC32 crc; + void setup() { Serial.begin(115200); @@ -26,6 +28,7 @@ void loop() { } + void test() { crc.setPolynome(0x04C11DB7); @@ -54,4 +57,6 @@ void test() Serial.println(crc.count()); } + // -- END OF FILE -- + diff --git a/libraries/CRC/examples/CRC64_test/CRC64_test.ino b/libraries/CRC/examples/CRC64_test/CRC64_test.ino index 75616189..38847f3b 100644 --- a/libraries/CRC/examples/CRC64_test/CRC64_test.ino +++ b/libraries/CRC/examples/CRC64_test/CRC64_test.ino @@ -5,6 +5,7 @@ // DATE: 2021-01-20 // (c) : MIT + #include "CRC64.h" #include "printHelpers.h" // https://github.com/RobTillaart/printHelpers @@ -12,6 +13,7 @@ char str[24] = "123456789"; CRC64 crc; + void setup() { Serial.begin(115200); @@ -27,6 +29,7 @@ void loop() { } + void test() { crc.setPolynome(0x07); @@ -55,4 +58,6 @@ void test() Serial.println(print64(crc.count())); } + // -- END OF FILE -- + diff --git a/libraries/CRC/examples/CRC8_test/CRC8_test.ino b/libraries/CRC/examples/CRC8_test/CRC8_test.ino index f839aa14..87c74f5e 100644 --- a/libraries/CRC/examples/CRC8_test/CRC8_test.ino +++ b/libraries/CRC/examples/CRC8_test/CRC8_test.ino @@ -7,7 +7,6 @@ #include "CRC8.h" - #include "CRC.h" @@ -15,6 +14,7 @@ char str[24] = "123456789"; CRC8 crc; + void setup() { Serial.begin(115200); @@ -30,6 +30,7 @@ void loop() { } + void test() { Serial.println(crc8((uint8_t *)str, 9, 0x07), HEX); @@ -60,4 +61,6 @@ void test() Serial.println(crc.count()); } + // -- END OF FILE -- + diff --git a/libraries/CRC/examples/CRC_performance/CRC_performance.ino b/libraries/CRC/examples/CRC_performance/CRC_performance.ino index 834a13bf..833e71d3 100644 --- a/libraries/CRC/examples/CRC_performance/CRC_performance.ino +++ b/libraries/CRC/examples/CRC_performance/CRC_performance.ino @@ -6,13 +6,14 @@ // (c) : MIT // + #include "CRC.h" char str[122] = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"; - uint32_t start, stop; + void setup() { Serial.begin(115200); @@ -104,13 +105,14 @@ void setup() Serial.println("============================="); delay(100); - Serial.println("\n\nDone..."); } + void loop() { - } + // -- END OF FILE -- + diff --git a/libraries/CRC/examples/CRC_reference_test/CRC_reference_test.ino b/libraries/CRC/examples/CRC_reference_test/CRC_reference_test.ino index 526850e1..abfc1519 100644 --- a/libraries/CRC/examples/CRC_reference_test/CRC_reference_test.ino +++ b/libraries/CRC/examples/CRC_reference_test/CRC_reference_test.ino @@ -6,11 +6,13 @@ // (c) : MIT // + #include "CRC.h" #include "printHelpers.h" // for the 64 bit... char str[24] = "123456789"; + void setup() { Serial.begin(115200); @@ -85,9 +87,11 @@ void setup() Serial.println("\n\nDone..."); } + void loop() { - } + // -- END OF FILE -- + diff --git a/libraries/CRC/examples/CRC_test/CRC_test.ino b/libraries/CRC/examples/CRC_test/CRC_test.ino index f45cc02e..9b441c4c 100644 --- a/libraries/CRC/examples/CRC_test/CRC_test.ino +++ b/libraries/CRC/examples/CRC_test/CRC_test.ino @@ -6,11 +6,13 @@ // (c) : MIT // + #include "CRC.h" #include "printHelpers.h" // for the 64 bit... char str[24] = "123456789"; + void setup() { Serial.begin(115200); @@ -35,9 +37,11 @@ void setup() Serial.println("\n\nDone..."); } + void loop() { - } + // -- END OF FILE -- + diff --git a/libraries/CRC/keywords.txt b/libraries/CRC/keywords.txt index f1db0ab6..efeff667 100644 --- a/libraries/CRC/keywords.txt +++ b/libraries/CRC/keywords.txt @@ -15,11 +15,13 @@ crc64 KEYWORD2 reset KEYWORD2 restart KEYWORD2 + setPolynome KEYWORD2 setStartXOR KEYWORD2 setEndXOR KEYWORD2 setReverseIn KEYWORD2 setReverseOut KEYWORD2 + add KEYWORD2 getCRC KEYWORD2 count KEYWORD2 diff --git a/libraries/CRC/library.json b/libraries/CRC/library.json index 4972a436..d6f8d350 100644 --- a/libraries/CRC/library.json +++ b/libraries/CRC/library.json @@ -15,8 +15,9 @@ "type": "git", "url": "https://github.com/RobTillaart/CRC" }, - "version": "0.1.3", + "version": "0.1.4", "license": "MIT", "frameworks": "arduino", - "platforms": "*" + "platforms": "*", + "headers": "CRC.h" } diff --git a/libraries/CRC/library.properties b/libraries/CRC/library.properties index 7104af67..b0041622 100644 --- a/libraries/CRC/library.properties +++ b/libraries/CRC/library.properties @@ -1,5 +1,5 @@ name=CRC -version=0.1.3 +version=0.1.4 author=Rob Tillaart maintainer=Rob Tillaart sentence=Library for CRC for Arduino diff --git a/libraries/CRC/test/unit_test_001.cpp b/libraries/CRC/test/unit_test_001.cpp index 06194e0f..7a862205 100644 --- a/libraries/CRC/test/unit_test_001.cpp +++ b/libraries/CRC/test/unit_test_001.cpp @@ -43,6 +43,7 @@ uint8_t * data = (uint8_t *) str; unittest_setup() { + fprintf(stderr, "CRC_LIB_VERSION: %s\n", (char *) CRC_LIB_VERSION); } unittest_teardown() @@ -52,8 +53,6 @@ unittest_teardown() 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)); @@ -69,8 +68,6 @@ unittest(test_crc8) 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 )); @@ -99,8 +96,6 @@ unittest(test_crc16) 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)); @@ -115,8 +110,6 @@ unittest(test_crc32) unittest(test_crc64) { - fprintf(stderr, "VERSION: %s\n", CRC_LIB_VERSION); - fprintf(stderr, "no reference yet\n"); assertEqual(1, 1); } diff --git a/libraries/CRC/test/unit_test_crc8.cpp b/libraries/CRC/test/unit_test_crc8.cpp index 2d3b60cc..172964dc 100644 --- a/libraries/CRC/test/unit_test_crc8.cpp +++ b/libraries/CRC/test/unit_test_crc8.cpp @@ -45,6 +45,7 @@ unittest_setup() { } + unittest_teardown() { }