Port of teensy 3 FastCRC library, uses hardware CRC

Dependents:   fastCRCperf

Port of teensy 3 FastCRC library, uses hardware CRC on K64F. About 30 times faster than Arduino CRC (crc16.h).

https://github.com/FrankBoesing/FastCRC

teensy forum discussions on FastCRC https://forum.pjrc.com/threads/25699-Fast-CRC-library-(uses-the-built-in-crc-module-in-Teensy3)

Committer:
manitou
Date:
Fri Apr 22 17:07:33 2016 +0000
Revision:
1:1ce0f4ee7357
Parent:
0:7343f324d853
table-drive bug fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
manitou 0:7343f324d853 1 /* FastCRC library code is placed under the MIT license
manitou 0:7343f324d853 2 * Copyright (c) 2014,2015 Frank Bösing
manitou 0:7343f324d853 3 *
manitou 0:7343f324d853 4 * Permission is hereby granted, free of charge, to any person obtaining
manitou 0:7343f324d853 5 * a copy of this software and associated documentation files (the
manitou 0:7343f324d853 6 * "Software"), to deal in the Software without restriction, including
manitou 0:7343f324d853 7 * without limitation the rights to use, copy, modify, merge, publish,
manitou 0:7343f324d853 8 * distribute, sublicense, and/or sell copies of the Software, and to
manitou 0:7343f324d853 9 * permit persons to whom the Software is furnished to do so, subject to
manitou 0:7343f324d853 10 * the following conditions:
manitou 0:7343f324d853 11 *
manitou 0:7343f324d853 12 * The above copyright notice and this permission notice shall be
manitou 0:7343f324d853 13 * included in all copies or substantial portions of the Software.
manitou 0:7343f324d853 14 *
manitou 0:7343f324d853 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
manitou 0:7343f324d853 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
manitou 0:7343f324d853 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
manitou 0:7343f324d853 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
manitou 0:7343f324d853 19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
manitou 0:7343f324d853 20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
manitou 0:7343f324d853 21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
manitou 0:7343f324d853 22 * SOFTWARE.
manitou 0:7343f324d853 23 */
manitou 0:7343f324d853 24
manitou 0:7343f324d853 25
manitou 0:7343f324d853 26 // Teensy 3.0, Teensy 3.1:
manitou 0:7343f324d853 27 // See K20P64M72SF1RM.pdf (Kinetis), Pages 638 - 641 for documentation of CRC Device
manitou 0:7343f324d853 28 // See KINETIS_4N30D.pdf for Errata (Errata ID 2776)
manitou 0:7343f324d853 29 //
manitou 0:7343f324d853 30 // So, ALL HW-calculations are done as 32 bit.
manitou 0:7343f324d853 31 //
manitou 0:7343f324d853 32 //
manitou 0:7343f324d853 33 //
manitou 0:7343f324d853 34 // Thanks to:
manitou 0:7343f324d853 35 // - Catalogue of parametrised CRC algorithms, CRC RevEng
manitou 0:7343f324d853 36 // http://reveng.sourceforge.net/crc-catalogue/
manitou 0:7343f324d853 37 //
manitou 0:7343f324d853 38 // - Danjel McGougan (CRC-Table-Generator)
manitou 0:7343f324d853 39 //
manitou 0:7343f324d853 40 #include "mbed.h"
manitou 0:7343f324d853 41
manitou 0:7343f324d853 42 // Set this to 0 for smaller 32BIT-CRC-Tables:
manitou 0:7343f324d853 43 #define CRC_BIGTABLES 1
manitou 0:7343f324d853 44
manitou 0:7343f324d853 45
manitou 0:7343f324d853 46 #if !defined(FastCRC_h)
manitou 0:7343f324d853 47 #define FastCRC_h
manitou 0:7343f324d853 48 #include "inttypes.h"
manitou 0:7343f324d853 49
manitou 0:7343f324d853 50
manitou 0:7343f324d853 51 // ================= DEFINES ===================
manitou 0:7343f324d853 52 #if defined(__CORTEX_M4) || defined(__MK20DX256__)
manitou 0:7343f324d853 53 #define CRC_SW 0
manitou 0:7343f324d853 54 #define CRC_FLAG_NOREFLECT (((1<<31) | (1<<30)) | ((0<<29) | (0<<28))) //refin=false refout=false
manitou 0:7343f324d853 55 #define CRC_FLAG_REFLECT (((1<<31) | (0<<30)) | ((1<<29) | (0<<28))) //Reflect in- and outgoing bytes (refin=true refout=true)
manitou 0:7343f324d853 56 #define CRC_FLAG_XOR (1<<26) //Perform XOR on result
manitou 0:7343f324d853 57 #define CRC_FLAG_NOREFLECT_8 (0) //For 8-Bit CRC
manitou 0:7343f324d853 58 #define CRC_FLAG_REFLECT_SWAP (((1<<31) | (0<<30)) | ((0<<29) | (1<<28))) //For 16-Bit CRC (byteswap)
manitou 0:7343f324d853 59 #else
manitou 0:7343f324d853 60 #define CRC_SW 1
manitou 0:7343f324d853 61 #endif
manitou 0:7343f324d853 62 // ================= 8-BIT CRC ===================
manitou 0:7343f324d853 63
manitou 0:7343f324d853 64 class FastCRC8
manitou 0:7343f324d853 65 {
manitou 0:7343f324d853 66 public:
manitou 0:7343f324d853 67 FastCRC8();
manitou 0:7343f324d853 68 uint8_t smbus(const uint8_t *data, const uint16_t datalen); // Alias CRC-8
manitou 0:7343f324d853 69 uint8_t maxim(const uint8_t *data, const uint16_t datalen); // Equivalent to _crc_ibutton_update() in crc16.h from avr_libc
manitou 0:7343f324d853 70
manitou 0:7343f324d853 71 uint8_t smbus_upd(const uint8_t *data, uint16_t datalen); // Call for subsequent calculations with previous seed.
manitou 0:7343f324d853 72 uint8_t maxim_upd(const uint8_t *data, uint16_t datalen); // Call for subsequent calculations with previous seed.
manitou 0:7343f324d853 73 #if !CRC_SW
manitou 0:7343f324d853 74 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)
manitou 0:7343f324d853 75 #endif
manitou 0:7343f324d853 76 private:
manitou 0:7343f324d853 77 #if CRC_SW
manitou 0:7343f324d853 78 uint8_t seed;
manitou 0:7343f324d853 79 #else
manitou 0:7343f324d853 80 uint8_t update(const uint8_t *data, const uint16_t datalen);
manitou 0:7343f324d853 81 #endif
manitou 0:7343f324d853 82 };
manitou 0:7343f324d853 83
manitou 0:7343f324d853 84 // ================= 16-BIT CRC ===================
manitou 0:7343f324d853 85
manitou 0:7343f324d853 86 class FastCRC16
manitou 0:7343f324d853 87 {
manitou 0:7343f324d853 88 public:
manitou 0:7343f324d853 89 FastCRC16();
manitou 0:7343f324d853 90 uint16_t ccitt(const uint8_t *data, const uint16_t datalen); // Alias "false CCITT"
manitou 0:7343f324d853 91 uint16_t mcrf4xx(const uint8_t *data,const uint16_t datalen); // Equivalent to _crc_ccitt_update() in crc16.h from avr_libc
manitou 0:7343f324d853 92 uint16_t kermit(const uint8_t *data, const uint16_t datalen); // Alias CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-CCITT
manitou 0:7343f324d853 93 uint16_t modbus(const uint8_t *data, const uint16_t datalen); // Equivalent to _crc_16_update() in crc16.h from avr_libc
manitou 0:7343f324d853 94 uint16_t xmodem(const uint8_t *data, const uint16_t datalen); // Alias ZMODEM, CRC-16/ACORN
manitou 0:7343f324d853 95 uint16_t x25(const uint8_t *data, const uint16_t datalen); // Alias CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-B
manitou 0:7343f324d853 96
manitou 0:7343f324d853 97 uint16_t ccitt_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
manitou 0:7343f324d853 98 uint16_t mcrf4xx_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
manitou 0:7343f324d853 99 uint16_t kermit_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
manitou 0:7343f324d853 100 uint16_t modbus_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
manitou 0:7343f324d853 101 uint16_t xmodem_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
manitou 0:7343f324d853 102 uint16_t x25_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
manitou 0:7343f324d853 103 #if !CRC_SW
manitou 0:7343f324d853 104 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)
manitou 0:7343f324d853 105 #endif
manitou 0:7343f324d853 106 private:
manitou 0:7343f324d853 107 #if CRC_SW
manitou 0:7343f324d853 108 uint16_t seed;
manitou 0:7343f324d853 109 #else
manitou 0:7343f324d853 110 uint16_t update(const uint8_t *data, const uint16_t datalen);
manitou 0:7343f324d853 111 #endif
manitou 0:7343f324d853 112 };
manitou 0:7343f324d853 113
manitou 0:7343f324d853 114 // ================= 32-BIT CRC ===================
manitou 0:7343f324d853 115
manitou 0:7343f324d853 116 class FastCRC32
manitou 0:7343f324d853 117 {
manitou 0:7343f324d853 118 public:
manitou 0:7343f324d853 119 FastCRC32();
manitou 0:7343f324d853 120 uint32_t crc32(const uint8_t *data, const uint16_t datalen); // Alias CRC-32/ADCCP, PKZIP, Ethernet, 802.3
manitou 0:7343f324d853 121 uint32_t cksum(const uint8_t *data, const uint16_t datalen); // Alias CRC-32/POSIX
manitou 0:7343f324d853 122
manitou 0:7343f324d853 123 uint32_t crc32_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
manitou 0:7343f324d853 124 uint32_t cksum_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
manitou 0:7343f324d853 125 #if !CRC_SW
manitou 0:7343f324d853 126 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)
manitou 0:7343f324d853 127 #endif
manitou 0:7343f324d853 128 private:
manitou 0:7343f324d853 129 #if CRC_SW
manitou 0:7343f324d853 130 uint32_t seed;
manitou 0:7343f324d853 131 #else
manitou 0:7343f324d853 132 uint32_t update(const uint8_t *data, const uint16_t datalen);
manitou 0:7343f324d853 133 #endif
manitou 0:7343f324d853 134 };
manitou 0:7343f324d853 135
manitou 0:7343f324d853 136 #endif