Port of teensy 3 FastCRC library, uses hardware CRC
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)
FastCRChw.cpp@1:1ce0f4ee7357, 2016-04-22 (annotated)
- 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?
User | Revision | Line number | New 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 | // HW-calculations are 32BIT |
manitou | 0:7343f324d853 | 27 | // |
manitou | 0:7343f324d853 | 28 | // Thanks to: |
manitou | 0:7343f324d853 | 29 | // - Catalogue of parametrised CRC algorithms, CRC RevEng |
manitou | 0:7343f324d853 | 30 | // http://reveng.sourceforge.net/crc-catalogue/ |
manitou | 0:7343f324d853 | 31 | // |
manitou | 0:7343f324d853 | 32 | // - Danjel McGougan (CRC-Table-Generator) |
manitou | 0:7343f324d853 | 33 | // |
manitou | 0:7343f324d853 | 34 | |
manitou | 0:7343f324d853 | 35 | |
manitou | 0:7343f324d853 | 36 | #if defined(__CORTEX_M4) || defined(__MK20DX256__) |
manitou | 0:7343f324d853 | 37 | |
manitou | 0:7343f324d853 | 38 | |
manitou | 0:7343f324d853 | 39 | #include "FastCRC.h" |
manitou | 0:7343f324d853 | 40 | |
manitou | 0:7343f324d853 | 41 | // =============================================== |
manitou | 0:7343f324d853 | 42 | |
manitou | 0:7343f324d853 | 43 | typedef struct { |
manitou | 0:7343f324d853 | 44 | union { |
manitou | 0:7343f324d853 | 45 | uint32_t CRC; //CRC Data register |
manitou | 0:7343f324d853 | 46 | struct { |
manitou | 0:7343f324d853 | 47 | uint16_t CRC16; |
manitou | 0:7343f324d853 | 48 | uint16_t CRC16_1; |
manitou | 0:7343f324d853 | 49 | }; |
manitou | 0:7343f324d853 | 50 | struct { |
manitou | 0:7343f324d853 | 51 | uint8_t CRC8; |
manitou | 0:7343f324d853 | 52 | uint8_t CRC8_1; |
manitou | 0:7343f324d853 | 53 | uint8_t CRC8_2; |
manitou | 0:7343f324d853 | 54 | uint8_t CRC8_3; |
manitou | 0:7343f324d853 | 55 | }; |
manitou | 0:7343f324d853 | 56 | }; |
manitou | 0:7343f324d853 | 57 | uint32_t GPOLY; //CRC Polynomial register |
manitou | 0:7343f324d853 | 58 | uint32_t CTRL; //CRC Control register |
manitou | 0:7343f324d853 | 59 | } CRC_T; |
manitou | 0:7343f324d853 | 60 | |
manitou | 0:7343f324d853 | 61 | static volatile CRC_T * const rCRC = (CRC_T *)0x40032000; |
manitou | 0:7343f324d853 | 62 | |
manitou | 0:7343f324d853 | 63 | #define CRC_CTRL_WAS 25 // Write CRC Data Register As Seed(1) / Data(0) |
manitou | 0:7343f324d853 | 64 | #define CRC_CTRL_TCRC 24 // Width of CRC protocol (0=16 BIT, 1=32 BIT) |
manitou | 0:7343f324d853 | 65 | #define CRC_CTRL_TOTR1 29 // TOTR[1] |
manitou | 0:7343f324d853 | 66 | |
manitou | 0:7343f324d853 | 67 | |
manitou | 0:7343f324d853 | 68 | // ================= 8-BIT CRC =================== |
manitou | 0:7343f324d853 | 69 | |
manitou | 0:7343f324d853 | 70 | /** Constructor |
manitou | 0:7343f324d853 | 71 | * Enables CRC-clock |
manitou | 0:7343f324d853 | 72 | */ |
manitou | 0:7343f324d853 | 73 | FastCRC8::FastCRC8(){ |
manitou | 0:7343f324d853 | 74 | SIM_SCGC6 |= SIM_SCGC6_CRC_MASK; |
manitou | 0:7343f324d853 | 75 | } |
manitou | 0:7343f324d853 | 76 | |
manitou | 0:7343f324d853 | 77 | /** SMBUS CRC |
manitou | 0:7343f324d853 | 78 | * aka CRC-8 |
manitou | 0:7343f324d853 | 79 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 80 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 81 | * @return CRC value |
manitou | 0:7343f324d853 | 82 | */ |
manitou | 0:7343f324d853 | 83 | uint8_t FastCRC8::smbus(const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 84 | { |
manitou | 0:7343f324d853 | 85 | // poly=0x07 init=0x00 refin=false refout=false xorout=0x00 check=0xf4 |
manitou | 0:7343f324d853 | 86 | return generic(0x07, 0, CRC_FLAG_NOREFLECT, data, datalen); |
manitou | 0:7343f324d853 | 87 | } |
manitou | 0:7343f324d853 | 88 | |
manitou | 0:7343f324d853 | 89 | /** MAXIM 8-Bit CRC |
manitou | 0:7343f324d853 | 90 | * equivalent to _crc_ibutton_update() in crc16.h from avr_libc |
manitou | 0:7343f324d853 | 91 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 92 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 93 | * @return CRC value |
manitou | 0:7343f324d853 | 94 | */ |
manitou | 0:7343f324d853 | 95 | uint8_t FastCRC8::maxim(const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 96 | { |
manitou | 0:7343f324d853 | 97 | // poly=0x31 init=0x00 refin=true refout=true xorout=0x00 check=0xa1 |
manitou | 0:7343f324d853 | 98 | return generic(0x31, 0, CRC_FLAG_REFLECT, data, datalen); |
manitou | 0:7343f324d853 | 99 | } |
manitou | 0:7343f324d853 | 100 | |
manitou | 0:7343f324d853 | 101 | /** Update |
manitou | 0:7343f324d853 | 102 | * Call for subsequent calculations with previous seed |
manitou | 0:7343f324d853 | 103 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 104 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 105 | * @return CRC value |
manitou | 0:7343f324d853 | 106 | */ |
manitou | 0:7343f324d853 | 107 | uint8_t FastCRC8::update(const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 108 | { |
manitou | 0:7343f324d853 | 109 | |
manitou | 0:7343f324d853 | 110 | const uint8_t *src = data; |
manitou | 0:7343f324d853 | 111 | const uint8_t *target = src + datalen; |
manitou | 0:7343f324d853 | 112 | |
manitou | 0:7343f324d853 | 113 | while (((uintptr_t)src & 0x03) != 0 && (src < target)) { |
manitou | 0:7343f324d853 | 114 | rCRC->CRC8_3 = *src++; //Write 8 BIT |
manitou | 0:7343f324d853 | 115 | } |
manitou | 0:7343f324d853 | 116 | |
manitou | 0:7343f324d853 | 117 | while (src <= target-4) { |
manitou | 0:7343f324d853 | 118 | rCRC->CRC = *( uint32_t *)src; //Write 32 BIT |
manitou | 0:7343f324d853 | 119 | src += 4; |
manitou | 0:7343f324d853 | 120 | } |
manitou | 0:7343f324d853 | 121 | |
manitou | 0:7343f324d853 | 122 | while (src < target) { |
manitou | 0:7343f324d853 | 123 | rCRC->CRC8_3 = *src++; //Write 8 Bit |
manitou | 0:7343f324d853 | 124 | } |
manitou | 0:7343f324d853 | 125 | |
manitou | 0:7343f324d853 | 126 | if (rCRC->CTRL & (1<<CRC_CTRL_TOTR1)) |
manitou | 0:7343f324d853 | 127 | return rCRC->CRC8; |
manitou | 0:7343f324d853 | 128 | else |
manitou | 0:7343f324d853 | 129 | return rCRC->CRC8_3; |
manitou | 0:7343f324d853 | 130 | } |
manitou | 0:7343f324d853 | 131 | |
manitou | 0:7343f324d853 | 132 | /** generic function for all 8-Bit CRCs |
manitou | 0:7343f324d853 | 133 | * @param polynom Polynom |
manitou | 0:7343f324d853 | 134 | * @param seed Seed |
manitou | 0:7343f324d853 | 135 | * @param flags Flags |
manitou | 0:7343f324d853 | 136 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 137 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 138 | * @return CRC value |
manitou | 0:7343f324d853 | 139 | */ |
manitou | 0:7343f324d853 | 140 | uint8_t FastCRC8::generic(const uint8_t polynom, const uint8_t seed, const uint32_t flags, const uint8_t *data,const uint16_t datalen) |
manitou | 0:7343f324d853 | 141 | { |
manitou | 0:7343f324d853 | 142 | |
manitou | 0:7343f324d853 | 143 | rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC) | (1<<CRC_CTRL_WAS); // 32Bit Mode, Prepare to write seed(25) |
manitou | 0:7343f324d853 | 144 | rCRC->GPOLY = ((uint32_t)polynom)<<24; // Set polynom |
manitou | 0:7343f324d853 | 145 | rCRC->CRC = ((uint32_t)seed<<24); // Write seed |
manitou | 0:7343f324d853 | 146 | rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC); // Clear WAS Bit - prepare to write data |
manitou | 0:7343f324d853 | 147 | |
manitou | 0:7343f324d853 | 148 | return update(data, datalen); |
manitou | 0:7343f324d853 | 149 | } |
manitou | 0:7343f324d853 | 150 | uint8_t FastCRC8::smbus_upd(const uint8_t *data, uint16_t datalen){return update(data, datalen);} |
manitou | 0:7343f324d853 | 151 | uint8_t FastCRC8::maxim_upd(const uint8_t *data, uint16_t datalen){return update(data, datalen);} |
manitou | 0:7343f324d853 | 152 | |
manitou | 0:7343f324d853 | 153 | // ================= 16-BIT CRC =================== |
manitou | 0:7343f324d853 | 154 | |
manitou | 0:7343f324d853 | 155 | /** Constructor |
manitou | 0:7343f324d853 | 156 | * Enables CRC-clock |
manitou | 0:7343f324d853 | 157 | */ |
manitou | 0:7343f324d853 | 158 | FastCRC16::FastCRC16(){ |
manitou | 0:7343f324d853 | 159 | SIM_SCGC6 |= SIM_SCGC6_CRC_MASK; |
manitou | 0:7343f324d853 | 160 | } |
manitou | 0:7343f324d853 | 161 | |
manitou | 0:7343f324d853 | 162 | /** CCITT |
manitou | 0:7343f324d853 | 163 | * Alias "false CCITT" |
manitou | 0:7343f324d853 | 164 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 165 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 166 | * @return CRC value |
manitou | 0:7343f324d853 | 167 | */ |
manitou | 0:7343f324d853 | 168 | uint16_t FastCRC16::ccitt(const uint8_t *data,const uint16_t datalen) |
manitou | 0:7343f324d853 | 169 | { |
manitou | 0:7343f324d853 | 170 | // poly=0x1021 init=0xffff refin=false refout=false xorout=0x0000 check=0x29b1 |
manitou | 0:7343f324d853 | 171 | return generic(0x1021, 0XFFFF, CRC_FLAG_NOREFLECT, data, datalen); |
manitou | 0:7343f324d853 | 172 | } |
manitou | 0:7343f324d853 | 173 | |
manitou | 0:7343f324d853 | 174 | /** MCRF4XX |
manitou | 0:7343f324d853 | 175 | * equivalent to _crc_ccitt_update() in crc16.h from avr_libc |
manitou | 0:7343f324d853 | 176 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 177 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 178 | * @return CRC value |
manitou | 0:7343f324d853 | 179 | */ |
manitou | 0:7343f324d853 | 180 | uint16_t FastCRC16::mcrf4xx(const uint8_t *data,const uint16_t datalen) |
manitou | 0:7343f324d853 | 181 | { |
manitou | 0:7343f324d853 | 182 | // poly=0x1021 init=0xffff refin=true refout=true xorout=0x0000 check=0x6f91 |
manitou | 0:7343f324d853 | 183 | return generic(0x1021, 0XFFFF, CRC_FLAG_REFLECT, data, datalen); |
manitou | 0:7343f324d853 | 184 | } |
manitou | 0:7343f324d853 | 185 | |
manitou | 0:7343f324d853 | 186 | /** MODBUS |
manitou | 0:7343f324d853 | 187 | * equivalent to _crc_16_update() in crc16.h from avr_libc |
manitou | 0:7343f324d853 | 188 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 189 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 190 | * @return CRC value |
manitou | 0:7343f324d853 | 191 | */ |
manitou | 0:7343f324d853 | 192 | uint16_t FastCRC16::modbus(const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 193 | { |
manitou | 0:7343f324d853 | 194 | // poly=0x8005 init=0xffff refin=true refout=true xorout=0x0000 check=0x4b37 |
manitou | 0:7343f324d853 | 195 | return generic(0x8005, 0XFFFF, CRC_FLAG_REFLECT, data, datalen); |
manitou | 0:7343f324d853 | 196 | } |
manitou | 0:7343f324d853 | 197 | |
manitou | 0:7343f324d853 | 198 | /** KERMIT |
manitou | 0:7343f324d853 | 199 | * Alias CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-CCITT |
manitou | 0:7343f324d853 | 200 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 201 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 202 | * @return CRC value |
manitou | 0:7343f324d853 | 203 | */ |
manitou | 0:7343f324d853 | 204 | uint16_t FastCRC16::kermit(const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 205 | { |
manitou | 0:7343f324d853 | 206 | // poly=0x1021 init=0x0000 refin=true refout=true xorout=0x0000 check=0x2189 |
manitou | 0:7343f324d853 | 207 | // sometimes byteswapped presentation of result |
manitou | 0:7343f324d853 | 208 | return generic(0x1021, 0x00, CRC_FLAG_REFLECT, data, datalen); |
manitou | 0:7343f324d853 | 209 | } |
manitou | 0:7343f324d853 | 210 | |
manitou | 0:7343f324d853 | 211 | /** XMODEM |
manitou | 0:7343f324d853 | 212 | * Alias ZMODEM, CRC-16/ACORN |
manitou | 0:7343f324d853 | 213 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 214 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 215 | * @return CRC value |
manitou | 0:7343f324d853 | 216 | */ |
manitou | 0:7343f324d853 | 217 | uint16_t FastCRC16::xmodem(const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 218 | { |
manitou | 0:7343f324d853 | 219 | //width=16 poly=0x1021 init=0x0000 refin=false refout=false xorout=0x0000 check=0x31c3 |
manitou | 0:7343f324d853 | 220 | return generic(0x1021, 0, CRC_FLAG_NOREFLECT, data, datalen); |
manitou | 0:7343f324d853 | 221 | } |
manitou | 0:7343f324d853 | 222 | |
manitou | 0:7343f324d853 | 223 | /** X25 |
manitou | 0:7343f324d853 | 224 | * Alias CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-B |
manitou | 0:7343f324d853 | 225 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 226 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 227 | * @return CRC value |
manitou | 0:7343f324d853 | 228 | */ |
manitou | 0:7343f324d853 | 229 | uint16_t FastCRC16::x25(const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 230 | { |
manitou | 0:7343f324d853 | 231 | // poly=0x1021 init=0xffff refin=true refout=true xorout=0xffff check=0x906e |
manitou | 0:7343f324d853 | 232 | return generic(0x1021, 0XFFFF, CRC_FLAG_REFLECT | CRC_FLAG_XOR, data, datalen); |
manitou | 0:7343f324d853 | 233 | } |
manitou | 0:7343f324d853 | 234 | |
manitou | 0:7343f324d853 | 235 | /** Update |
manitou | 0:7343f324d853 | 236 | * Call for subsequent calculations with previous seed |
manitou | 0:7343f324d853 | 237 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 238 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 239 | * @return CRC value |
manitou | 0:7343f324d853 | 240 | */ |
manitou | 0:7343f324d853 | 241 | uint16_t FastCRC16::update(const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 242 | { |
manitou | 0:7343f324d853 | 243 | const uint8_t *src = data; |
manitou | 0:7343f324d853 | 244 | const uint8_t *target = src + datalen; |
manitou | 0:7343f324d853 | 245 | |
manitou | 0:7343f324d853 | 246 | while (((uintptr_t)src & 0x03) !=0 && (src < target)) { |
manitou | 0:7343f324d853 | 247 | rCRC->CRC8_3 = *src++; //Write 8 BIT |
manitou | 0:7343f324d853 | 248 | } |
manitou | 0:7343f324d853 | 249 | |
manitou | 0:7343f324d853 | 250 | while (src <= target-4) { |
manitou | 0:7343f324d853 | 251 | rCRC->CRC = *( uint32_t *)src; //Write 32 BIT |
manitou | 0:7343f324d853 | 252 | src += 4; |
manitou | 0:7343f324d853 | 253 | } |
manitou | 0:7343f324d853 | 254 | |
manitou | 0:7343f324d853 | 255 | while (src < target) { |
manitou | 0:7343f324d853 | 256 | rCRC->CRC8_3 = *src++; //Write 8 Bit |
manitou | 0:7343f324d853 | 257 | } |
manitou | 0:7343f324d853 | 258 | |
manitou | 0:7343f324d853 | 259 | if (rCRC->CTRL & (1<<CRC_CTRL_TOTR1)) |
manitou | 0:7343f324d853 | 260 | return rCRC->CRC16; |
manitou | 0:7343f324d853 | 261 | else |
manitou | 0:7343f324d853 | 262 | return rCRC->CRC16_1; |
manitou | 0:7343f324d853 | 263 | } |
manitou | 0:7343f324d853 | 264 | |
manitou | 0:7343f324d853 | 265 | /** generic function for all 16-Bit CRCs |
manitou | 0:7343f324d853 | 266 | * @param polynom Polynom |
manitou | 0:7343f324d853 | 267 | * @param seed Seed |
manitou | 0:7343f324d853 | 268 | * @param flags Flags |
manitou | 0:7343f324d853 | 269 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 270 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 271 | * @return CRC value |
manitou | 0:7343f324d853 | 272 | */ |
manitou | 0:7343f324d853 | 273 | uint16_t FastCRC16::generic(const uint16_t polynom, const uint16_t seed, const uint32_t flags, const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 274 | { |
manitou | 0:7343f324d853 | 275 | |
manitou | 0:7343f324d853 | 276 | rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC) | (1<<CRC_CTRL_WAS);// 32-Bit Mode, prepare to write seed(25) |
manitou | 0:7343f324d853 | 277 | rCRC->GPOLY = ((uint32_t)polynom)<<16; // set polynom |
manitou | 0:7343f324d853 | 278 | rCRC->CRC = ((uint32_t)seed<<16); // this is the seed |
manitou | 0:7343f324d853 | 279 | rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC); // Clear WAS Bit - prepare to write data |
manitou | 0:7343f324d853 | 280 | |
manitou | 0:7343f324d853 | 281 | return update(data, datalen); |
manitou | 0:7343f324d853 | 282 | } |
manitou | 0:7343f324d853 | 283 | |
manitou | 0:7343f324d853 | 284 | uint16_t FastCRC16::ccitt_upd(const uint8_t *data, uint16_t len) {return update(data, len);} |
manitou | 0:7343f324d853 | 285 | uint16_t FastCRC16::mcrf4xx_upd(const uint8_t *data, uint16_t len){return update(data, len);} |
manitou | 0:7343f324d853 | 286 | uint16_t FastCRC16::kermit_upd(const uint8_t *data, uint16_t len) {return update(data, len);} |
manitou | 0:7343f324d853 | 287 | uint16_t FastCRC16::modbus_upd(const uint8_t *data, uint16_t len) {return update(data, len);} |
manitou | 0:7343f324d853 | 288 | uint16_t FastCRC16::xmodem_upd(const uint8_t *data, uint16_t len) {return update(data, len);} |
manitou | 0:7343f324d853 | 289 | uint16_t FastCRC16::x25_upd(const uint8_t *data, uint16_t len) {return update(data, len);} |
manitou | 0:7343f324d853 | 290 | |
manitou | 0:7343f324d853 | 291 | |
manitou | 0:7343f324d853 | 292 | |
manitou | 0:7343f324d853 | 293 | // ================= 32-BIT CRC =================== |
manitou | 0:7343f324d853 | 294 | |
manitou | 0:7343f324d853 | 295 | /** Constructor |
manitou | 0:7343f324d853 | 296 | * Enables CRC-clock |
manitou | 0:7343f324d853 | 297 | */ |
manitou | 0:7343f324d853 | 298 | FastCRC32::FastCRC32(){ |
manitou | 0:7343f324d853 | 299 | SIM_SCGC6 |= SIM_SCGC6_CRC_MASK; |
manitou | 0:7343f324d853 | 300 | } |
manitou | 0:7343f324d853 | 301 | |
manitou | 0:7343f324d853 | 302 | /** CRC32 |
manitou | 0:7343f324d853 | 303 | * Alias CRC-32/ADCCP, PKZIP, Ethernet, 802.3 |
manitou | 0:7343f324d853 | 304 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 305 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 306 | * @return CRC value |
manitou | 0:7343f324d853 | 307 | */ |
manitou | 0:7343f324d853 | 308 | uint32_t FastCRC32::crc32(const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 309 | { |
manitou | 0:7343f324d853 | 310 | // poly=0x04c11db7 init=0xffffffff refin=true refout=true xorout=0xffffffff check=0xcbf43926 |
manitou | 0:7343f324d853 | 311 | return generic(0x04C11DB7L, 0XFFFFFFFFL, CRC_FLAG_REFLECT | CRC_FLAG_XOR, data, datalen); |
manitou | 0:7343f324d853 | 312 | } |
manitou | 0:7343f324d853 | 313 | |
manitou | 0:7343f324d853 | 314 | /** CKSUM |
manitou | 0:7343f324d853 | 315 | * Alias CRC-32/POSIX |
manitou | 0:7343f324d853 | 316 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 317 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 318 | * @return CRC value |
manitou | 0:7343f324d853 | 319 | */ |
manitou | 0:7343f324d853 | 320 | uint32_t FastCRC32::cksum(const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 321 | { |
manitou | 0:7343f324d853 | 322 | // width=32 poly=0x04c11db7 init=0x00000000 refin=false refout=false xorout=0xffffffff check=0x765e7680 |
manitou | 0:7343f324d853 | 323 | return generic(0x04C11DB7L, 0, CRC_FLAG_NOREFLECT | CRC_FLAG_XOR, data, datalen); |
manitou | 0:7343f324d853 | 324 | } |
manitou | 0:7343f324d853 | 325 | |
manitou | 0:7343f324d853 | 326 | /** Update |
manitou | 0:7343f324d853 | 327 | * Call for subsequent calculations with previous seed |
manitou | 0:7343f324d853 | 328 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 329 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 330 | * @return CRC value |
manitou | 0:7343f324d853 | 331 | */ |
manitou | 0:7343f324d853 | 332 | //#pragma GCC diagnostic ignored "-Wpointer-arith" |
manitou | 0:7343f324d853 | 333 | uint32_t FastCRC32::update(const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 334 | { |
manitou | 0:7343f324d853 | 335 | |
manitou | 0:7343f324d853 | 336 | const uint8_t *src = data; |
manitou | 0:7343f324d853 | 337 | const uint8_t *target = src + datalen; |
manitou | 0:7343f324d853 | 338 | |
manitou | 0:7343f324d853 | 339 | while (((uintptr_t)src & 0x03) != 0 && (src < target)) { |
manitou | 0:7343f324d853 | 340 | rCRC->CRC8_3 = *src++; //Write 8 BIT |
manitou | 0:7343f324d853 | 341 | } |
manitou | 0:7343f324d853 | 342 | |
manitou | 0:7343f324d853 | 343 | while (src <= target-4) { |
manitou | 0:7343f324d853 | 344 | rCRC->CRC = *( uint32_t *)src; //Write 32 BIT |
manitou | 0:7343f324d853 | 345 | src += 4; |
manitou | 0:7343f324d853 | 346 | } |
manitou | 0:7343f324d853 | 347 | |
manitou | 0:7343f324d853 | 348 | while (src < target) { |
manitou | 0:7343f324d853 | 349 | rCRC->CRC8_3 = *src++; //Write 8 Bit |
manitou | 0:7343f324d853 | 350 | } |
manitou | 0:7343f324d853 | 351 | |
manitou | 0:7343f324d853 | 352 | return rCRC->CRC; |
manitou | 0:7343f324d853 | 353 | } |
manitou | 0:7343f324d853 | 354 | |
manitou | 0:7343f324d853 | 355 | /** generic function for all 32-Bit CRCs |
manitou | 0:7343f324d853 | 356 | * @param polynom Polynom |
manitou | 0:7343f324d853 | 357 | * @param seed Seed |
manitou | 0:7343f324d853 | 358 | * @param flags Flags |
manitou | 0:7343f324d853 | 359 | * @param data Pointer to Data |
manitou | 0:7343f324d853 | 360 | * @param datalen Length of Data |
manitou | 0:7343f324d853 | 361 | * @return CRC value |
manitou | 0:7343f324d853 | 362 | */ |
manitou | 0:7343f324d853 | 363 | uint32_t FastCRC32::generic(const uint32_t polynom, const uint32_t seed, const uint32_t flags, const uint8_t *data, const uint16_t datalen) |
manitou | 0:7343f324d853 | 364 | { |
manitou | 0:7343f324d853 | 365 | |
manitou | 0:7343f324d853 | 366 | rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC) | (1<<CRC_CTRL_WAS); // 32Bit Mode, prepare to write seed(25) |
manitou | 0:7343f324d853 | 367 | rCRC->GPOLY = polynom; // Set polynom |
manitou | 0:7343f324d853 | 368 | rCRC->CRC = seed; // This is the seed |
manitou | 0:7343f324d853 | 369 | rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC); // Clear WAS Bit - prepare to write data |
manitou | 0:7343f324d853 | 370 | |
manitou | 0:7343f324d853 | 371 | return update(data, datalen); |
manitou | 0:7343f324d853 | 372 | } |
manitou | 0:7343f324d853 | 373 | |
manitou | 0:7343f324d853 | 374 | uint32_t FastCRC32::crc32_upd(const uint8_t *data, uint16_t len){return update(data, len);} |
manitou | 0:7343f324d853 | 375 | uint32_t FastCRC32::cksum_upd(const uint8_t *data, uint16_t len){return update(data, len);} |
manitou | 0:7343f324d853 | 376 | |
manitou | 0:7343f324d853 | 377 | #endif // __MK20DX128__ || __MK20DX256__ |