tom dunigan / FastCRC

Dependents:   fastCRCperf

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FastCRC.h Source File

FastCRC.h

00001 /* FastCRC library code is placed under the MIT license
00002  * Copyright (c) 2014,2015 Frank Bösing
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining
00005  * a copy of this software and associated documentation files (the
00006  * "Software"), to deal in the Software without restriction, including
00007  * without limitation the rights to use, copy, modify, merge, publish,
00008  * distribute, sublicense, and/or sell copies of the Software, and to
00009  * permit persons to whom the Software is furnished to do so, subject to
00010  * the following conditions:
00011  *
00012  * The above copyright notice and this permission notice shall be
00013  * included in all copies or substantial portions of the Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00016  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00018  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00019  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00020  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00022  * SOFTWARE.
00023  */
00024 
00025 
00026 // Teensy 3.0, Teensy 3.1:
00027 // See K20P64M72SF1RM.pdf (Kinetis), Pages 638 - 641 for documentation of CRC Device
00028 // See KINETIS_4N30D.pdf for Errata (Errata ID 2776)
00029 //
00030 // So, ALL HW-calculations are done as 32 bit.
00031 //
00032 //
00033 //
00034 // Thanks to:
00035 // - Catalogue of parametrised CRC algorithms, CRC RevEng
00036 // http://reveng.sourceforge.net/crc-catalogue/
00037 //
00038 // - Danjel McGougan (CRC-Table-Generator)
00039 //
00040 #include "mbed.h"
00041 
00042 // Set this to 0 for smaller 32BIT-CRC-Tables:
00043 #define CRC_BIGTABLES 1
00044 
00045 
00046 #if !defined(FastCRC_h)
00047 #define FastCRC_h
00048 #include "inttypes.h"
00049 
00050 
00051 // ================= DEFINES ===================
00052 #if defined(__CORTEX_M4) || defined(__MK20DX256__)
00053 #define CRC_SW 0
00054 #define CRC_FLAG_NOREFLECT         (((1<<31) | (1<<30)) | ((0<<29) | (0<<28))) //refin=false refout=false
00055 #define CRC_FLAG_REFLECT           (((1<<31) | (0<<30)) | ((1<<29) | (0<<28))) //Reflect in- and outgoing bytes (refin=true refout=true)
00056 #define CRC_FLAG_XOR               (1<<26)                                     //Perform XOR on result
00057 #define CRC_FLAG_NOREFLECT_8       (0)                                         //For 8-Bit CRC
00058 #define CRC_FLAG_REFLECT_SWAP      (((1<<31) | (0<<30)) | ((0<<29) | (1<<28))) //For 16-Bit CRC (byteswap)
00059 #else
00060 #define CRC_SW 1
00061 #endif
00062 // ================= 8-BIT CRC ===================
00063 
00064 class FastCRC8
00065 {
00066 public:
00067   FastCRC8();
00068   uint8_t smbus(const uint8_t *data, const uint16_t datalen);       // Alias CRC-8
00069   uint8_t maxim(const uint8_t *data, const uint16_t datalen);       // Equivalent to _crc_ibutton_update() in crc16.h from avr_libc
00070   
00071   uint8_t smbus_upd(const uint8_t *data, uint16_t datalen);         // Call for subsequent calculations with previous seed.
00072   uint8_t maxim_upd(const uint8_t *data, uint16_t datalen);         // Call for subsequent calculations with previous seed.
00073 #if !CRC_SW
00074   uint8_t generic(const uint8_t polyom, const uint8_t seed, const uint32_t flags, const uint8_t *data, const uint16_t datalen); //Not available in non-hw-variant (not T3.x)
00075 #endif
00076 private:
00077 #if CRC_SW
00078   uint8_t seed;
00079 #else
00080   uint8_t update(const uint8_t *data, const uint16_t datalen);
00081 #endif
00082 };
00083 
00084 // ================= 16-BIT CRC ===================
00085 
00086 class FastCRC16
00087 {
00088 public:
00089   FastCRC16();
00090   uint16_t ccitt(const uint8_t *data, const uint16_t datalen);      // Alias "false CCITT"
00091   uint16_t mcrf4xx(const uint8_t *data,const uint16_t datalen);     // Equivalent to _crc_ccitt_update() in crc16.h from avr_libc
00092   uint16_t kermit(const uint8_t *data, const uint16_t datalen);     // Alias CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-CCITT
00093   uint16_t modbus(const uint8_t *data, const uint16_t datalen);     // Equivalent to _crc_16_update() in crc16.h from avr_libc
00094   uint16_t xmodem(const uint8_t *data, const uint16_t datalen);     // Alias ZMODEM, CRC-16/ACORN
00095   uint16_t x25(const uint8_t *data, const uint16_t datalen);        // Alias CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-B
00096   
00097   uint16_t ccitt_upd(const uint8_t *data, uint16_t len);            // Call for subsequent calculations with previous seed
00098   uint16_t mcrf4xx_upd(const uint8_t *data, uint16_t len);          // Call for subsequent calculations with previous seed
00099   uint16_t kermit_upd(const uint8_t *data, uint16_t len);           // Call for subsequent calculations with previous seed
00100   uint16_t modbus_upd(const uint8_t *data, uint16_t len);           // Call for subsequent calculations with previous seed
00101   uint16_t xmodem_upd(const uint8_t *data, uint16_t len);           // Call for subsequent calculations with previous seed
00102   uint16_t x25_upd(const uint8_t *data, uint16_t len);              // Call for subsequent calculations with previous seed
00103 #if !CRC_SW
00104   uint16_t generic(const uint16_t polyom, const uint16_t seed, const uint32_t flags, const uint8_t *data, const uint16_t datalen); //Not available in non-hw-variant (not T3.x)
00105 #endif
00106 private:
00107 #if CRC_SW
00108   uint16_t seed;
00109 #else
00110   uint16_t update(const uint8_t *data, const uint16_t datalen);
00111 #endif
00112 };
00113 
00114 // ================= 32-BIT CRC ===================
00115 
00116 class FastCRC32
00117 {
00118 public:
00119   FastCRC32();
00120   uint32_t crc32(const uint8_t *data, const uint16_t datalen);      // Alias CRC-32/ADCCP, PKZIP, Ethernet, 802.3
00121   uint32_t cksum(const uint8_t *data, const uint16_t datalen);      // Alias CRC-32/POSIX
00122 
00123   uint32_t crc32_upd(const uint8_t *data, uint16_t len);            // Call for subsequent calculations with previous seed
00124   uint32_t cksum_upd(const uint8_t *data, uint16_t len);            // Call for subsequent calculations with previous seed
00125 #if !CRC_SW
00126   uint32_t generic(const uint32_t polyom, const uint32_t seed, const uint32_t flags, const uint8_t *data, const uint16_t datalen); //Not available in non-hw-variant (not T3.x)
00127 #endif
00128 private:
00129 #if CRC_SW
00130   uint32_t seed;
00131 #else
00132   uint32_t update(const uint8_t *data, const uint16_t datalen);
00133 #endif
00134 };
00135 
00136 #endif