From 218b1573f89e40ed866b795124e92e6fdaf7865b Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Fri, 14 Sep 2018 19:22:18 +0100 Subject: [PATCH] Added CRC16 MODBUS Added ETL_ prefic to extern const arrays. --- include/etl/crc16.h | 4 +- include/etl/crc16_ccitt.h | 4 +- include/etl/crc16_kermit.h | 4 +- include/etl/crc16_modbus.h | 109 ++++++++++++++++++++++++++++++++ include/etl/crc32.h | 4 +- include/etl/version.h | 12 ++-- src/crc16.cpp | 2 +- src/crc16_ccitt.cpp | 2 +- src/crc16_kermit.cpp | 2 +- src/crc16_modbus.cpp | 76 ++++++++++++++++++++++ src/crc32.cpp | 2 +- src/crc32_c.cpp | 6 +- support/Release notes.txt | 4 ++ test/test_crc.cpp | 57 +++++++++++++++++ test/vs2017/etl.vcxproj | 2 + test/vs2017/etl.vcxproj.filters | 6 ++ 16 files changed, 275 insertions(+), 21 deletions(-) create mode 100644 include/etl/crc16_modbus.h create mode 100644 src/crc16_modbus.cpp diff --git a/include/etl/crc16.h b/include/etl/crc16.h index 9fa21d8b..e517c77b 100644 --- a/include/etl/crc16.h +++ b/include/etl/crc16.h @@ -51,7 +51,7 @@ namespace etl /// CRC16 table /// \ingroup crc16 //*************************************************************************** - extern const uint16_t CRC16[]; + extern const uint16_t ETL_CRC16[]; //*************************************************************************** /// CRC16 policy. @@ -68,7 +68,7 @@ namespace etl inline uint16_t add(uint16_t crc, uint8_t value) const { - return (crc >> 8) ^ CRC16[(crc ^ value) & 0xFF]; + return (crc >> 8) ^ ETL_CRC16[(crc ^ value) & 0xFF]; } inline uint16_t final(uint16_t crc) const diff --git a/include/etl/crc16_ccitt.h b/include/etl/crc16_ccitt.h index 28ef7054..a9f6c7e9 100644 --- a/include/etl/crc16_ccitt.h +++ b/include/etl/crc16_ccitt.h @@ -51,7 +51,7 @@ namespace etl /// CRC-CCITT table /// \ingroup crc16_ccitt //*************************************************************************** - extern const uint16_t CRC_CCITT[]; + extern const uint16_t ETL_CRC_CCITT[]; //*************************************************************************** /// CRC16 CCITT policy. @@ -68,7 +68,7 @@ namespace etl inline uint16_t add(uint16_t crc, uint8_t value) const { - return (crc << 8) ^ CRC_CCITT[((crc >> 8) ^ value) & 0xFF]; + return (crc << 8) ^ ETL_CRC_CCITT[((crc >> 8) ^ value) & 0xFF]; } inline uint16_t final(uint16_t crc) const diff --git a/include/etl/crc16_kermit.h b/include/etl/crc16_kermit.h index dffc6ed2..baa1456d 100644 --- a/include/etl/crc16_kermit.h +++ b/include/etl/crc16_kermit.h @@ -51,7 +51,7 @@ namespace etl /// CRC Kermit table /// \ingroup crc //*************************************************************************** - extern const uint16_t CRC_KERMIT[]; + extern const uint16_t ETL_CRC_KERMIT[]; //*************************************************************************** /// CRC16 Kermit policy. @@ -68,7 +68,7 @@ namespace etl inline uint16_t add(uint16_t crc, uint8_t value) const { - return (crc >> 8) ^ CRC_KERMIT[(crc ^ value) & 0xFF]; + return (crc >> 8) ^ ETL_CRC_KERMIT[(crc ^ value) & 0xFF]; } inline uint16_t final(uint16_t crc) const diff --git a/include/etl/crc16_modbus.h b/include/etl/crc16_modbus.h new file mode 100644 index 00000000..f962664a --- /dev/null +++ b/include/etl/crc16_modbus.h @@ -0,0 +1,109 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2018 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#ifndef ETL_CRC16_MODBUS_INCLUDED +#define ETL_CRC16_MODBUS_INCLUDED + +#include + +#include "platform.h" +#include "frame_check_sequence.h" + +#include "stl/iterator.h" + +#if defined(ETL_COMPILER_KEIL) +#pragma diag_suppress 1300 +#endif + +///\defgroup crc16_modbus 16 bit CRC MODBUS calculation +///\ingroup crc + +namespace etl +{ + //*************************************************************************** + /// CRC-MODBUS table + /// \ingroup crc16_modbus + //*************************************************************************** + extern const uint16_t ETL_CRC_MODBUS[]; + + //*************************************************************************** + /// CRC16 MODBUS policy. + /// Calculates CRC16 MODBUS using polynomial 0x + //*************************************************************************** + struct crc_policy_16_modbus + { + typedef uint16_t value_type; + + inline uint16_t initial() const + { + return 0xFFFF; + } + + inline uint16_t add(uint16_t crc, uint8_t value) const + { + return (crc >> 8) ^ ETL_CRC_MODBUS[(crc ^ value) & 0xFF]; + } + + inline uint16_t final(uint16_t crc) const + { + return crc; + } + }; + + //************************************************************************* + /// CRC16 MODBUS + //************************************************************************* + class crc16_modbus : public etl::frame_check_sequence + { + public: + + //************************************************************************* + /// Default constructor. + //************************************************************************* + crc16_modbus() + { + this->reset(); + } + + //************************************************************************* + /// Constructor from range. + /// \param begin Start of the range. + /// \param end End of the range. + //************************************************************************* + template + crc16_modbus(TIterator begin, const TIterator end) + { + this->reset(); + this->add(begin, end); + } + }; +} + +#endif diff --git a/include/etl/crc32.h b/include/etl/crc32.h index 8f78e429..141c32b4 100644 --- a/include/etl/crc32.h +++ b/include/etl/crc32.h @@ -51,7 +51,7 @@ namespace etl /// CRC32 table /// \ingroup crc32 //*************************************************************************** - extern const uint32_t CRC32[]; + extern const uint32_t ETL_CRC32[]; //*************************************************************************** /// CRC32 policy. @@ -68,7 +68,7 @@ namespace etl inline uint32_t add(uint32_t crc, uint8_t value) const { - return (crc >> 8) ^ CRC32[(crc ^ value) & 0xFF]; + return (crc >> 8) ^ ETL_CRC32[(crc ^ value) & 0xFF]; } inline uint32_t final(uint32_t crc) const diff --git a/include/etl/version.h b/include/etl/version.h index 7b124c71..cdf62feb 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -37,13 +37,13 @@ SOFTWARE. /// Definitions of the ETL version ///\ingroup utilities -#define ETL_VERSION "11.17.1" -#define ETL_VERSION_W L"11.17.1" -#define ETL_VERSION_U16 u"11.17.1" -#define ETL_VERSION_U32 U"11.17.1" +#define ETL_VERSION "11.18.0" +#define ETL_VERSION_W L"11.18.0" +#define ETL_VERSION_U16 u"11.18.0" +#define ETL_VERSION_U32 U"11.18.0" #define ETL_VERSION_MAJOR 11 -#define ETL_VERSION_MINOR 17 -#define ETL_VERSION_PATCH 1 +#define ETL_VERSION_MINOR 18 +#define ETL_VERSION_PATCH 0 #define ETL_VERSION_VALUE ((ETL_VERSION_MAJOR * 10000) + (ETL_VERSION_MINOR * 100) + ETL_VERSION_PATCH) #endif diff --git a/src/crc16.cpp b/src/crc16.cpp index 9ac1d710..512b6ae9 100644 --- a/src/crc16.cpp +++ b/src/crc16.cpp @@ -38,7 +38,7 @@ namespace etl /// CRC16 table /// \ingroup crc16 //*************************************************************************** - extern const uint16_t CRC16[] = + extern const uint16_t ETL_CRC16[] = { 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, diff --git a/src/crc16_ccitt.cpp b/src/crc16_ccitt.cpp index 12b79a76..09cced20 100644 --- a/src/crc16_ccitt.cpp +++ b/src/crc16_ccitt.cpp @@ -38,7 +38,7 @@ namespace etl /// CRC-CCITT table /// \ingroup crc16_ccitt //*************************************************************************** - extern const uint16_t CRC_CCITT[] = + extern const uint16_t ETL_CRC_CCITT[] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, diff --git a/src/crc16_kermit.cpp b/src/crc16_kermit.cpp index 3c44e043..5e89a96f 100644 --- a/src/crc16_kermit.cpp +++ b/src/crc16_kermit.cpp @@ -38,7 +38,7 @@ namespace etl /// CRC Kermit table /// \ingroup crc16_kermit //*************************************************************************** - extern const uint16_t CRC_KERMIT[] = + extern const uint16_t ETL_CRC_KERMIT[] = { 0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF, 0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, 0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E, 0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876, diff --git a/src/crc16_modbus.cpp b/src/crc16_modbus.cpp new file mode 100644 index 00000000..43f02df3 --- /dev/null +++ b/src/crc16_modbus.cpp @@ -0,0 +1,76 @@ +///\file + +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/etl +https://www.etlcpp.com + +Copyright(c) 2018 jwellbelove + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files(the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions : + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, ExPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +******************************************************************************/ + +#include + +#include "etl/platform.h" + +namespace etl +{ + //*************************************************************************** + /// CRC-MODBUS table + /// \ingroup crc16_modbus + //*************************************************************************** + extern const uint16_t ETL_CRC_MODBUS[] = + { + 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, + 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, + 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, + 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, + 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, + 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, + 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, + 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040, + 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, + 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, + 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, + 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, + 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, + 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40, + 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, + 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041, + 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, + 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, + 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, + 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, + 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, + 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, + 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, + 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, + 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, + 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440, + 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, + 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, + 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, + 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, + 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, + 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040 + }; +} diff --git a/src/crc32.cpp b/src/crc32.cpp index c9217577..771f2ad3 100644 --- a/src/crc32.cpp +++ b/src/crc32.cpp @@ -38,7 +38,7 @@ namespace etl /// CRC32 table /// \ingroup crc32 //*************************************************************************** - extern const uint32_t CRC32[] = + extern const uint32_t ETL_CRC32[] = { 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, diff --git a/src/crc32_c.cpp b/src/crc32_c.cpp index 1c54c3f6..b255af0d 100644 --- a/src/crc32_c.cpp +++ b/src/crc32_c.cpp @@ -35,10 +35,10 @@ SOFTWARE. namespace etl { //*************************************************************************** - /// CRC32_C table - /// \ingroup CRC32_C + /// ETL_CRC32_C table + /// \ingroup ETL_CRC32_C //*************************************************************************** - extern const uint32_t CRC32_C[] = + extern const uint32_t ETL_CRC32_C[] = { 0x00000000L, 0xF26B8303L, 0xE13B70F7L, 0x1350F3F4L, 0xC79A971FL, 0x35F1141CL, 0x26A1E7E8L, 0xD4CA64EBL, 0x8AD958CFL, 0x78B2DBCCL, 0x6BE22838L, 0x9989AB3BL, 0x4D43CFD0L, 0xBF284CD3L, 0xAC78BF27L, 0x5E133C24L, diff --git a/support/Release notes.txt b/support/Release notes.txt index a00ad58c..8d08d7b8 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +11.18.0 +Added CRC16 MODBUS + =============================================================================== 11.17.1 Swapped event and current state parameters for transition constructor. diff --git a/test/test_crc.cpp b/test/test_crc.cpp index 9cb3bc5b..c5adaf02 100644 --- a/test/test_crc.cpp +++ b/test/test_crc.cpp @@ -37,6 +37,7 @@ SOFTWARE. #include "etl/crc16.h" #include "etl/crc16_ccitt.h" #include "etl/crc16_kermit.h" +#include "etl/crc16_modbus.h" #include "etl/crc32.h" #include "etl/crc64_ecma.h" @@ -268,6 +269,62 @@ namespace CHECK_EQUAL(crc1, crc3); } + //************************************************************************* + TEST(test_crc16_modbus) + { + std::string data("123456789"); + + uint16_t crc = etl::crc16_modbus(data.begin(), data.end()); + + CHECK_EQUAL(0x4B37, crc); + } + + //************************************************************************* + TEST(test_crc16_modbus_add_values) + { + std::string data("123456789"); + + etl::crc16_modbus crc_calculator; + + for (size_t i = 0; i < data.size(); ++i) + { + crc_calculator.add(data[i]); + } + + uint16_t crc = crc_calculator; + + CHECK_EQUAL(0x4B37, crc); + } + + //************************************************************************* + TEST(test_crc16_modbus_add_range) + { + std::string data("123456789"); + + etl::crc16_modbus crc_calculator; + + crc_calculator.add(data.begin(), data.end()); + + uint16_t crc = crc_calculator.value(); + + CHECK_EQUAL(0x4B37, crc); + } + + //************************************************************************* + TEST(test_crc16_modbus_add_range_endian) + { + std::vector data1 = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; + std::vector data2 = { 0x04030201, 0x08070605 }; + std::vector data3 = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 }; + + uint16_t crc1 = etl::crc16_modbus(data1.begin(), data1.end()); + uint16_t crc2 = etl::crc16_modbus((uint8_t*)&data2[0], (uint8_t*)(&data2[0] + data2.size())); + CHECK_EQUAL(crc1, crc2); + + uint16_t crc3 = etl::crc16_modbus(data3.rbegin(), data3.rend()); + CHECK_EQUAL(crc1, crc3); + } + //************************************************************************* TEST(test_crc32) { diff --git a/test/vs2017/etl.vcxproj b/test/vs2017/etl.vcxproj index fd081b11..628b95d8 100644 --- a/test/vs2017/etl.vcxproj +++ b/test/vs2017/etl.vcxproj @@ -366,6 +366,7 @@ + @@ -555,6 +556,7 @@ + diff --git a/test/vs2017/etl.vcxproj.filters b/test/vs2017/etl.vcxproj.filters index 32db94a6..454cab6a 100644 --- a/test/vs2017/etl.vcxproj.filters +++ b/test/vs2017/etl.vcxproj.filters @@ -696,6 +696,9 @@ ETL\Frameworks + + ETL\Maths + @@ -1142,6 +1145,9 @@ Source Files + + ETL\Maths +