diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..341c30b --- /dev/null +++ b/LICENSE @@ -0,0 +1,166 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..8e4f658 --- /dev/null +++ b/README.md @@ -0,0 +1,39 @@ +# Arduino IDE based CCITT CRC16 calculator - uCRC16Lib # + +## What is this repository for? ## + +Tiny and cross-device compatible CCITT CRC16 calculation. + +Supports any microcontroller. + + + +## Usage ## + +This library is used with only static methods, you don't need to (and can't) initialize any class object. + +You have these methods: + - *uCRC16Lib::calculate(char * data_to_be_encoded, uint16_t size);* : Returns CRC16 in a uint16_t + - *uCRC16Lib::crc_ok* : uint16_t with CRC16 value of "T 0xD9 0xE4" (without spaces) + +## How do I get set up? ## + +You can get it from Arduino libraries directly, searching by uCRC16Lib. + +For manual installation: + + * Get the ZIP from releases link: https://github.com/Naguissa/uCRC16Lib/releases + * Rename to uCRC16Lib.zip + * Install library on Arduino + +## Examples ## + +Included on example folder, available on Arduino IDE. + + + +## Who do I talk to? ## + + * [Naguissa](https://github.com/Naguissa) + * https://www.foroelectro.net/electronica-digital-microcontroladores-f8/utimerlib-libreria-arduino-para-eventos-temporizad-t191.html + diff --git a/contributors.txt b/contributors.txt new file mode 100644 index 0000000..8c5b0e6 --- /dev/null +++ b/contributors.txt @@ -0,0 +1,2 @@ +Naguissa - https://github.com/Naguissa + diff --git a/examples/uCRC16Lib_example/uCRC16Lib_example.ino b/examples/uCRC16Lib_example/uCRC16Lib_example.ino new file mode 100644 index 0000000..e19b3e0 --- /dev/null +++ b/examples/uCRC16Lib_example/uCRC16Lib_example.ino @@ -0,0 +1,61 @@ +/** + * Tiny and cross-device compatible CCITT CRC16 calculator library - uCRC16Lib - Example + * + * @author Naguissa + * @url https://www.github.com/Naguissa/uCRC16Lib + * @url https://www.foroelectro.net + */ + +#include "Arduino.h" +#include "uCRC16Lib.h" + + + + +void setup() { + Serial.begin(57600); + while (!Serial) { + ; // wait for serial port to connect. Needed for Leonardo only + } + + uint16_t crc; + char string[40]; + string[0] = 'T'; + string[1] = (unsigned char)0xd9; + string[2] = (unsigned char)0xe4; + string[3] = NULL; + + Serial.print("The crc of \"T\" is 0xD9E4. crc16 returned "); + Serial.println(uCRC16Lib::calculate(string, 1), HEX); + Serial.println(); + + Serial.print("The crc of \"T 0xD9 0xE4\" is "); + Serial.print(uCRC16Lib::calculate(string, 3), HEX); + Serial.print(". The value of crc_ok is "); + Serial.println(uCRC16Lib::crc_ok, HEX); + Serial.println(); + + strcpy(string, "THE,QUICK,BROWN,FOX,0123456789"); + Serial.print("The crc of \""); + Serial.print(string); + Serial.print("\" is 0x6E20. crc16 returned "); + Serial.println(uCRC16Lib::calculate(string, strlen(string)), HEX); + Serial.println(); + + string[0] = (unsigned char)0x03; + string[1] = (unsigned char)0x3F; + + Serial.print("CCITT Recommendation X.25 (1984) Appendix I example:"); + Serial.print("\tThe crc of 0x03 0x3F is 0x5BEC. crc16 returned "); + Serial.println(uCRC16Lib::calculate(string, 2), HEX); + Serial.println(); + + Serial.println(" - END -"); + Serial.println(); + +} + + +void loop() { +} + diff --git a/extras/data sources.txt b/extras/data sources.txt new file mode 100644 index 0000000..898fbef --- /dev/null +++ b/extras/data sources.txt @@ -0,0 +1,2 @@ +Base algorithm + - http://www.drdobbs.com/implementing-the-ccitt-cyclical-redundan/199904926 diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..cbf2eed --- /dev/null +++ b/library.properties @@ -0,0 +1,11 @@ +name=uCRC16Lib +version=1.0.0 +author=Naguissa +maintainer=Naguissa +sentence=Tiny and cross-device compatible CCITT CRC16 calculator library - uCRC16Lib +paragraph=Supports any architecture +category=Data Processing +url=https://github.com/Naguissa/uCRC16Lib +architectures=* +includes=uCRC16Lib.h + diff --git a/src/uCRC16Lib.cpp b/src/uCRC16Lib.cpp new file mode 100644 index 0000000..02ba918 --- /dev/null +++ b/src/uCRC16Lib.cpp @@ -0,0 +1,50 @@ +/** + * Tiny and cross-device compatible CCITT CRC16 calculator library - uCRC16Lib + * + * @copyright Naguissa + * @author Naguissa + * @email naguissa@foroelectro.net + * @version 1.0.0 + * @created 2018-04-21 + */ +#include "uCRC16Lib.h" + +/** + * Constructor + * + * Nothing to do here + */ +uCRC16Lib::uCRC16Lib() {} + + +/** + * Calculate CRC16 function + * + * @param data_p *char Pointer to data + * @param length uint16_t Length, in bytes, of data to calculate CRC16 of. Should be the same or inferior to data pointer's length. + */ +uint16_t uCRC16Lib::calculate(char *data_p, uint16_t length) { + uint8_t i; + uint16_t data; + uint16_t crc = 0xffff; + + if (length == 0) { + return (~crc); + } + + do { + for (i = 0, data = (uint16_t) 0xff & *data_p++; i < 8; i++, data >>= 1) { + if ((crc & 0x0001) ^ (data & 0x0001)) { + crc = (crc >> 1) ^ uCRC16Lib_POLYNOMIAL; + } else { + crc >>= 1; + } + } + } while (--length); + crc = ~crc; + data = crc; + crc = (crc << 8) | (data >> 8 & 0xFF); + return (crc); +} + + diff --git a/src/uCRC16Lib.h b/src/uCRC16Lib.h new file mode 100644 index 0000000..242827b --- /dev/null +++ b/src/uCRC16Lib.h @@ -0,0 +1,29 @@ +/** + * Tiny and cross-device compatible CCITT CRC16 calculator library. + * + * @copyright Naguissa + * @author Naguissa + * @email naguissa@foroelectro.net + * @version 1.0.0 + * @created 2018-04-21 + */ +#ifndef _uCRC16Lib_ + #define _uCRC16Lib_ + + #include + + #define uCRC16Lib_POLYNOMIAL 0x8408 + + class uCRC16Lib { + public: + static uint16_t calculate(char *, uint16_t); + const static uint16_t crc_ok = 0x470F; + + private: + // Static library, no need to construct objects + uCRC16Lib(); + + }; + +#endif +