Initial commit: Livox SDK2 source (excluding build artifacts)
This commit is contained in:
Vendored
+190
@@ -0,0 +1,190 @@
|
||||
/* FastCRC library code is placed under the MIT license
|
||||
* Copyright (c) 2014 - 2021 Frank Bösing
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
// Teensy 3.0, Teensy 3.1:
|
||||
// See K20P64M72SF1RM.pdf (Kinetis), Pages 638 - 641 for documentation of CRC Device
|
||||
// See KINETIS_4N30D.pdf for Errata (Errata ID 2776)
|
||||
//
|
||||
// So, ALL HW-calculations are done as 32 bit.
|
||||
//
|
||||
//
|
||||
//
|
||||
// Thanks to:
|
||||
// - Catalogue of parametrised CRC algorithms, CRC RevEng
|
||||
// http://reveng.sourceforge.net/crc-catalogue/
|
||||
//
|
||||
// - Danjel McGougan (CRC-Table-Generator)
|
||||
//
|
||||
|
||||
|
||||
// Set this to 0 for smaller 32BIT-CRC-Tables:
|
||||
#if !defined(CRC_BIGTABLES)
|
||||
#define CRC_BIGTABLES 1
|
||||
#endif
|
||||
|
||||
#if !defined(FastCRC_h)
|
||||
#define FastCRC_h
|
||||
|
||||
#if defined(ARDUINO)
|
||||
#include <Arduino.h>
|
||||
#endif
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <cstddef>
|
||||
|
||||
|
||||
// ================= DEFINES ===================
|
||||
#if defined(KINETISK)
|
||||
#define CRC_SW 0
|
||||
#define CRC_FLAG_NOREFLECT (((1<<31) | (1<<30)) | ((0<<29) | (0<<28))) //refin=false refout=false
|
||||
#define CRC_FLAG_REFLECT (((1<<31) | (0<<30)) | ((1<<29) | (0<<28))) //Reflect in- and outgoing bytes (refin=true refout=true)
|
||||
#define CRC_FLAG_XOR (1<<26) //Perform XOR on result
|
||||
#define CRC_FLAG_NOREFLECT_8 (0) //For 8-Bit CRC
|
||||
#define CRC_FLAG_REFLECT_SWAP (((1<<31) | (0<<30)) | ((0<<29) | (1<<28))) //For 16-Bit CRC (byteswap)
|
||||
#else
|
||||
#define CRC_SW 1
|
||||
#endif
|
||||
|
||||
// ================= 7-BIT CRC ===================
|
||||
class FastCRC7
|
||||
{
|
||||
public:
|
||||
FastCRC7();
|
||||
uint8_t crc7(const uint8_t *data, const size_t datalen); // (MultiMediaCard interface)
|
||||
uint8_t crc7_upd(const uint8_t *data, const size_t datalen); // Call for subsequent calculations with previous seed.
|
||||
#if !CRC_SW
|
||||
uint8_t generic(const uint8_t polyom, const uint8_t seed, const uint32_t flags, const uint8_t *data, const size_t datalen); //Not available in non-hw-variant (not T3.x)
|
||||
#endif
|
||||
private:
|
||||
#if CRC_SW
|
||||
uint8_t seed;
|
||||
#else
|
||||
uint8_t update(const uint8_t *data, const size_t datalen);
|
||||
#endif
|
||||
};
|
||||
|
||||
// ================= 8-BIT CRC ===================
|
||||
|
||||
class FastCRC8
|
||||
{
|
||||
public:
|
||||
FastCRC8();
|
||||
uint8_t smbus(const uint8_t *data, const size_t datalen); // Alias CRC-8
|
||||
uint8_t maxim(const uint8_t *data, const size_t datalen); // Equivalent to _crc_ibutton_update() in crc16.h from avr_libc
|
||||
|
||||
uint8_t smbus_upd(const uint8_t *data, size_t datalen); // Call for subsequent calculations with previous seed.
|
||||
uint8_t maxim_upd(const uint8_t *data, size_t datalen); // Call for subsequent calculations with previous seed.
|
||||
#if !CRC_SW
|
||||
uint8_t generic(const uint8_t polyom, const uint8_t seed, const uint32_t flags, const uint8_t *data, const size_t datalen); //Not available in non-hw-variant (not T3.x)
|
||||
#endif
|
||||
private:
|
||||
#if CRC_SW
|
||||
uint8_t seed;
|
||||
#else
|
||||
uint8_t update(const uint8_t *data, const size_t datalen);
|
||||
#endif
|
||||
};
|
||||
|
||||
// ================= 14-BIT CRC ===================
|
||||
|
||||
class FastCRC14
|
||||
{
|
||||
public:
|
||||
#if !CRC_SW //NO Software-implemenation so far
|
||||
FastCRC14();
|
||||
uint16_t darc(const uint8_t *data, const size_t datalen);
|
||||
uint16_t gsm(const uint8_t *data, const size_t datalen);
|
||||
uint16_t eloran(const uint8_t *data, const size_t datalen);
|
||||
uint16_t ft4(const uint8_t *data, const size_t datalen);
|
||||
|
||||
uint16_t darc_upd(const uint8_t *data, size_t len);
|
||||
uint16_t gsm_upd(const uint8_t *data, size_t len);
|
||||
uint16_t eloran_upd(const uint8_t *data, size_t len);
|
||||
uint16_t ft4_upd(const uint8_t *data, size_t len);
|
||||
#endif
|
||||
#if !CRC_SW
|
||||
uint16_t generic(const uint16_t polyom, const uint16_t seed, const uint32_t flags, const uint8_t *data, const size_t datalen); //Not available in non-hw-variant (not T3.x)
|
||||
#endif
|
||||
private:
|
||||
#if CRC_SW
|
||||
uint16_t seed;
|
||||
#else
|
||||
uint16_t update(const uint8_t *data, const size_t datalen);
|
||||
#endif
|
||||
};
|
||||
|
||||
// ================= 16-BIT CRC ===================
|
||||
|
||||
class FastCRC16
|
||||
{
|
||||
public:
|
||||
FastCRC16();
|
||||
uint16_t ccitt(const uint8_t *data, const size_t datalen); // Alias "false CCITT"
|
||||
uint16_t mcrf4xx(const uint8_t *data,const size_t datalen); // Equivalent to _crc_ccitt_update() in crc16.h from avr_libc
|
||||
uint16_t kermit(const uint8_t *data, const size_t datalen); // Alias CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-CCITT
|
||||
uint16_t modbus(const uint8_t *data, const size_t datalen); // Equivalent to _crc_16_update() in crc16.h from avr_libc
|
||||
uint16_t xmodem(const uint8_t *data, const size_t datalen); // Alias ZMODEM, CRC-16/ACORN
|
||||
uint16_t x25(const uint8_t *data, const size_t datalen); // Alias CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-B
|
||||
|
||||
uint16_t ccitt_upd(const uint8_t *data, size_t len); // Call for subsequent calculations with previous seed
|
||||
uint16_t mcrf4xx_upd(const uint8_t *data, size_t len); // Call for subsequent calculations with previous seed
|
||||
uint16_t kermit_upd(const uint8_t *data, size_t len); // Call for subsequent calculations with previous seed
|
||||
uint16_t modbus_upd(const uint8_t *data, size_t len); // Call for subsequent calculations with previous seed
|
||||
uint16_t xmodem_upd(const uint8_t *data, size_t len); // Call for subsequent calculations with previous seed
|
||||
uint16_t x25_upd(const uint8_t *data, size_t len); // Call for subsequent calculations with previous seed
|
||||
#if !CRC_SW
|
||||
uint16_t generic(const uint16_t polyom, const uint16_t seed, const uint32_t flags, const uint8_t *data, const size_t datalen); //Not available in non-hw-variant (not T3.x)
|
||||
#endif
|
||||
private:
|
||||
#if CRC_SW
|
||||
uint16_t seed;
|
||||
#else
|
||||
uint16_t update(const uint8_t *data, const size_t datalen);
|
||||
#endif
|
||||
};
|
||||
|
||||
// ================= 32-BIT CRC ===================
|
||||
|
||||
class FastCRC32
|
||||
{
|
||||
public:
|
||||
FastCRC32();
|
||||
uint32_t crc32(const uint8_t *data, const size_t datalen); // Alias CRC-32/ADCCP, PKZIP, Ethernet, 802.3
|
||||
uint32_t cksum(const uint8_t *data, const size_t datalen); // Alias CRC-32/POSIX
|
||||
|
||||
uint32_t crc32_upd(const uint8_t *data, size_t len); // Call for subsequent calculations with previous seed
|
||||
uint32_t cksum_upd(const uint8_t *data, size_t len); // Call for subsequent calculations with previous seed
|
||||
#if !CRC_SW
|
||||
uint32_t generic(const uint32_t polyom, const uint32_t seed, const uint32_t flags, const uint8_t *data, const size_t datalen); //Not available in non-hw-variant (not T3.x)
|
||||
#endif
|
||||
private:
|
||||
#if CRC_SW
|
||||
uint32_t seed;
|
||||
#else
|
||||
uint32_t update(const uint8_t *data, const size_t datalen);
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif
|
||||
Vendored
+1596
File diff suppressed because it is too large
Load Diff
Vendored
+567
@@ -0,0 +1,567 @@
|
||||
/* FastCRC library code is placed under the MIT license
|
||||
* Copyright (c) 2014 - 2021 Frank Bösing
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
//
|
||||
// HW-calculations are 32BIT
|
||||
//
|
||||
// Thanks to:
|
||||
// - Catalogue of parametrised CRC algorithms, CRC RevEng
|
||||
// http://reveng.sourceforge.net/crc-catalogue/
|
||||
//
|
||||
// - Danjel McGougan (CRC-Table-Generator)
|
||||
//
|
||||
|
||||
#if defined(ARDUINO)
|
||||
#include <Arduino.h>
|
||||
#endif
|
||||
#if defined(KINETISK)
|
||||
|
||||
#include "mk20dx128.h"
|
||||
#include "FastCRC.h"
|
||||
|
||||
// ===============================================
|
||||
|
||||
typedef struct {
|
||||
union {
|
||||
uint32_t CRC; //CRC Data register
|
||||
struct {
|
||||
uint16_t CRC16;
|
||||
uint16_t CRC16_1;
|
||||
};
|
||||
struct {
|
||||
uint8_t CRC8;
|
||||
uint8_t CRC8_1;
|
||||
uint8_t CRC8_2;
|
||||
uint8_t CRC8_3;
|
||||
};
|
||||
};
|
||||
uint32_t GPOLY; //CRC Polynomial register
|
||||
uint32_t CTRL; //CRC Control register
|
||||
} CRC_T;
|
||||
|
||||
static volatile CRC_T * const rCRC = (CRC_T *)0x40032000;
|
||||
|
||||
#define CRC_CTRL_WAS 25 // Write CRC Data Register As Seed(1) / Data(0)
|
||||
#define CRC_CTRL_TCRC 24 // Width of CRC protocol (0=16 BIT, 1=32 BIT)
|
||||
#define CRC_CTRL_TOTR1 29 // TOTR[1]
|
||||
|
||||
// ================= 7-BIT CRC ===================
|
||||
/** Constructor
|
||||
* Enables CRC-clock
|
||||
*/
|
||||
FastCRC7::FastCRC7(){
|
||||
SIM_SCGC6 |= SIM_SCGC6_CRC;
|
||||
}
|
||||
|
||||
/** CRC 7
|
||||
* MultiMediaCard interface
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint8_t FastCRC7::crc7(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x09 init=0x00 refin=false refout=false xorout=0x00 check=0x75
|
||||
return (generic(0x09, 0, CRC_FLAG_NOREFLECT, data, datalen));
|
||||
}
|
||||
|
||||
/** Update
|
||||
* Call for subsequent calculations with previous seed
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint8_t FastCRC7::update(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
|
||||
const uint8_t *src = data;
|
||||
const uint8_t *target = src + datalen;
|
||||
|
||||
while (((uintptr_t)src & 0x03) != 0 && (src < target)) {
|
||||
rCRC->CRC8_3 = *src++; //Write 8 BIT
|
||||
}
|
||||
|
||||
while (src <= target-4) {
|
||||
rCRC->CRC = *( uint32_t *)src; //Write 32 BIT
|
||||
src += 4;
|
||||
}
|
||||
|
||||
while (src < target) {
|
||||
rCRC->CRC8_3 = *src++; //Write 8 Bit
|
||||
}
|
||||
|
||||
//TODO: Check handling of CRC_CTRL_TOTR1 for other CRC7s
|
||||
/*
|
||||
if (rCRC->CTRL & (1<<CRC_CTRL_TOTR1))
|
||||
return rCRC->CRC8 >> 1;
|
||||
else
|
||||
*/
|
||||
return rCRC->CRC8_3 >> 1;
|
||||
}
|
||||
|
||||
/** generic function for all 7-Bit CRCs
|
||||
* @param polynom Polynom
|
||||
* @param seed Seed
|
||||
* @param flags Flags
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint8_t FastCRC7::generic(const uint8_t polynom, const uint8_t seed, const uint32_t flags, const uint8_t *data,const size_t datalen)
|
||||
{
|
||||
|
||||
rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC) | (1<<CRC_CTRL_WAS); // 32Bit Mode, Prepare to write seed(25)
|
||||
rCRC->GPOLY = ((uint32_t)polynom)<<(24 + 1); // Set polynom
|
||||
rCRC->CRC = ((uint32_t)seed<<(24 + 1)); // Write seed
|
||||
rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC); // Clear WAS Bit - prepare to write data
|
||||
|
||||
return update(data, datalen);
|
||||
}
|
||||
|
||||
uint8_t FastCRC7::crc7_upd(const uint8_t *data, size_t datalen){return update(data, datalen);}
|
||||
|
||||
|
||||
|
||||
// ================= 8-BIT CRC ===================
|
||||
|
||||
/** Constructor
|
||||
* Enables CRC-clock
|
||||
*/
|
||||
FastCRC8::FastCRC8(){
|
||||
SIM_SCGC6 |= SIM_SCGC6_CRC;
|
||||
}
|
||||
|
||||
|
||||
/** SMBUS CRC
|
||||
* aka CRC-8
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint8_t FastCRC8::smbus(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x07 init=0x00 refin=false refout=false xorout=0x00 check=0xf4
|
||||
return generic(0x07, 0, CRC_FLAG_NOREFLECT, data, datalen);
|
||||
}
|
||||
|
||||
/** MAXIM 8-Bit CRC
|
||||
* equivalent to _crc_ibutton_update() in crc16.h from avr_libc
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint8_t FastCRC8::maxim(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x31 init=0x00 refin=true refout=true xorout=0x00 check=0xa1
|
||||
return generic(0x31, 0, CRC_FLAG_REFLECT, data, datalen);
|
||||
}
|
||||
|
||||
/** Update
|
||||
* Call for subsequent calculations with previous seed
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint8_t FastCRC8::update(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
|
||||
const uint8_t *src = data;
|
||||
const uint8_t *target = src + datalen;
|
||||
|
||||
while (((uintptr_t)src & 0x03) != 0 && (src < target)) {
|
||||
rCRC->CRC8_3 = *src++; //Write 8 BIT
|
||||
}
|
||||
|
||||
while (src <= target-4) {
|
||||
rCRC->CRC = *( uint32_t *)src; //Write 32 BIT
|
||||
src += 4;
|
||||
}
|
||||
|
||||
while (src < target) {
|
||||
rCRC->CRC8_3 = *src++; //Write 8 Bit
|
||||
}
|
||||
|
||||
if (rCRC->CTRL & (1<<CRC_CTRL_TOTR1))
|
||||
return rCRC->CRC8;
|
||||
else
|
||||
return rCRC->CRC8_3;
|
||||
}
|
||||
|
||||
/** generic function for all 8-Bit CRCs
|
||||
* @param polynom Polynom
|
||||
* @param seed Seed
|
||||
* @param flags Flags
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint8_t FastCRC8::generic(const uint8_t polynom, const uint8_t seed, const uint32_t flags, const uint8_t *data,const size_t datalen)
|
||||
{
|
||||
|
||||
rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC) | (1<<CRC_CTRL_WAS); // 32Bit Mode, Prepare to write seed(25)
|
||||
rCRC->GPOLY = ((uint32_t)polynom)<<24; // Set polynom
|
||||
rCRC->CRC = ((uint32_t)seed<<24); // Write seed
|
||||
rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC); // Clear WAS Bit - prepare to write data
|
||||
|
||||
return update(data, datalen);
|
||||
}
|
||||
uint8_t FastCRC8::smbus_upd(const uint8_t *data, size_t datalen){return update(data, datalen);}
|
||||
uint8_t FastCRC8::maxim_upd(const uint8_t *data, size_t datalen){return update(data, datalen);}
|
||||
|
||||
|
||||
|
||||
// ================= 14-BIT CRC ===================
|
||||
|
||||
/** Constructor
|
||||
* Enables CRC-clock
|
||||
*/
|
||||
FastCRC14::FastCRC14(){
|
||||
SIM_SCGC6 |= SIM_SCGC6_CRC;
|
||||
}
|
||||
|
||||
/** CRC-14/DARC
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC14::darc(const uint8_t *data,const size_t datalen)
|
||||
{
|
||||
// poly=0x0805 init=0x0000 refin=true refout=true xorout=0x0000 check=0x082d residue=0x0000
|
||||
return generic(0x0805, 0x0000, CRC_FLAG_REFLECT, data, datalen);
|
||||
}
|
||||
|
||||
/** CRC-14/GSM
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC14::gsm(const uint8_t *data,const size_t datalen)
|
||||
{
|
||||
// poly=0x202d init=0x0000 refin=false refout=false xorout=0x3fff check=0x30ae residue=0x031e
|
||||
return generic(0x202d, 0x0000, CRC_FLAG_NOREFLECT | CRC_FLAG_XOR, data, datalen);
|
||||
}
|
||||
|
||||
/** CRC-14/ELORAN
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC14::eloran(const uint8_t *data,const size_t datalen)
|
||||
{
|
||||
// poly=0x60b1 init=0x0000 refin=false refout=false xorout=0x0000 check=0x38d1
|
||||
return generic(0x60b1, 0x0, CRC_FLAG_NOREFLECT , data, datalen);
|
||||
}
|
||||
|
||||
/** CRC-14/ft4 : TODO
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
/*
|
||||
uint16_t FastCRC14::ft4(const uint8_t *data,const size_t datalen)
|
||||
{
|
||||
|
||||
return generic(, , , data, datalen);
|
||||
}
|
||||
*/
|
||||
|
||||
/** Update
|
||||
* Call for subsequent calculations with previous seed
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC14::update(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
const uint8_t *src = data;
|
||||
const uint8_t *target = src + datalen;
|
||||
|
||||
while (((uintptr_t)src & 0x03) !=0 && (src < target)) {
|
||||
rCRC->CRC8_3 = *src++; //Write 8 BIT
|
||||
}
|
||||
|
||||
while (src <= target-4) {
|
||||
rCRC->CRC = *( uint32_t *)src; //Write 32 BIT
|
||||
src += 4;
|
||||
}
|
||||
|
||||
while (src < target) {
|
||||
rCRC->CRC8_3 = *src++; //Write 8 Bit
|
||||
}
|
||||
|
||||
if (rCRC->CTRL & (1<<CRC_CTRL_TOTR1))
|
||||
return rCRC->CRC16;
|
||||
else
|
||||
return rCRC->CRC >> (32 - 14);
|
||||
}
|
||||
|
||||
/** generic function for all 14-Bit CRCs
|
||||
* @param polynom Polynom
|
||||
* @param seed Seed
|
||||
* @param flags Flags
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC14::generic(const uint16_t polynom, const uint16_t seed, const uint32_t flags, const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
|
||||
rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC) | (1<<CRC_CTRL_WAS);// 32-Bit Mode, prepare to write seed(25)
|
||||
rCRC->GPOLY = ((uint32_t)polynom) << (32 - 14); // set polynom
|
||||
rCRC->CRC = ((uint32_t)seed << (32 - 14) ); // this is the seed
|
||||
rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC); // Clear WAS Bit - prepare to write data
|
||||
|
||||
return update(data, datalen);
|
||||
}
|
||||
|
||||
uint16_t FastCRC14::darc_upd(const uint8_t *data, size_t len) {return update(data, len);}
|
||||
uint16_t FastCRC14::gsm_upd(const uint8_t *data, size_t len) {return update(data, len);}
|
||||
uint16_t FastCRC14::eloran_upd(const uint8_t *data, size_t len) {return update(data, len);}
|
||||
//uint16_t FastCRC14::ft4(const uint8_t *data, size_t len) {return update(data, len);}
|
||||
|
||||
|
||||
// ================= 16-BIT CRC ===================
|
||||
|
||||
/** Constructor
|
||||
* Enables CRC-clock
|
||||
*/
|
||||
FastCRC16::FastCRC16(){
|
||||
SIM_SCGC6 |= SIM_SCGC6_CRC;
|
||||
}
|
||||
|
||||
/** CCITT
|
||||
* Alias "false CCITT"
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::ccitt(const uint8_t *data,const size_t datalen)
|
||||
{
|
||||
// poly=0x1021 init=0xffff refin=false refout=false xorout=0x0000 check=0x29b1
|
||||
return generic(0x1021, 0XFFFF, CRC_FLAG_NOREFLECT, data, datalen);
|
||||
}
|
||||
|
||||
/** MCRF4XX
|
||||
* equivalent to _crc_ccitt_update() in crc16.h from avr_libc
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::mcrf4xx(const uint8_t *data,const size_t datalen)
|
||||
{
|
||||
// poly=0x1021 init=0xffff refin=true refout=true xorout=0x0000 check=0x6f91
|
||||
return generic(0x1021, 0XFFFF, CRC_FLAG_REFLECT , data, datalen);
|
||||
}
|
||||
|
||||
/** MODBUS
|
||||
* equivalent to _crc_16_update() in crc16.h from avr_libc
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::modbus(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x8005 init=0xffff refin=true refout=true xorout=0x0000 check=0x4b37
|
||||
return generic(0x8005, 0XFFFF, CRC_FLAG_REFLECT, data, datalen);
|
||||
}
|
||||
|
||||
/** KERMIT
|
||||
* Alias CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-CCITT
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::kermit(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x1021 init=0x0000 refin=true refout=true xorout=0x0000 check=0x2189
|
||||
// sometimes byteswapped presentation of result
|
||||
return generic(0x1021, 0x00, CRC_FLAG_REFLECT, data, datalen);
|
||||
}
|
||||
|
||||
/** XMODEM
|
||||
* Alias ZMODEM, CRC-16/ACORN
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::xmodem(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
//width=16 poly=0x1021 init=0x0000 refin=false refout=false xorout=0x0000 check=0x31c3
|
||||
return generic(0x1021, 0, CRC_FLAG_NOREFLECT, data, datalen);
|
||||
}
|
||||
|
||||
/** X25
|
||||
* Alias CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-B
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::x25(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x1021 init=0xffff refin=true refout=true xorout=0xffff check=0x906e
|
||||
return generic(0x1021, 0XFFFF, CRC_FLAG_REFLECT | CRC_FLAG_XOR, data, datalen);
|
||||
}
|
||||
|
||||
/** Update
|
||||
* Call for subsequent calculations with previous seed
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::update(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
const uint8_t *src = data;
|
||||
const uint8_t *target = src + datalen;
|
||||
|
||||
while (((uintptr_t)src & 0x03) !=0 && (src < target)) {
|
||||
rCRC->CRC8_3 = *src++; //Write 8 BIT
|
||||
}
|
||||
|
||||
while (src <= target-4) {
|
||||
rCRC->CRC = *( uint32_t *)src; //Write 32 BIT
|
||||
src += 4;
|
||||
}
|
||||
|
||||
while (src < target) {
|
||||
rCRC->CRC8_3 = *src++; //Write 8 Bit
|
||||
}
|
||||
|
||||
if (rCRC->CTRL & (1<<CRC_CTRL_TOTR1))
|
||||
return rCRC->CRC16;
|
||||
else
|
||||
return rCRC->CRC16_1;
|
||||
}
|
||||
|
||||
/** generic function for all 16-Bit CRCs
|
||||
* @param polynom Polynom
|
||||
* @param seed Seed
|
||||
* @param flags Flags
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::generic(const uint16_t polynom, const uint16_t seed, const uint32_t flags, const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
|
||||
rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC) | (1<<CRC_CTRL_WAS);// 32-Bit Mode, prepare to write seed(25)
|
||||
rCRC->GPOLY = ((uint32_t)polynom)<<16; // set polynom
|
||||
rCRC->CRC = ((uint32_t)seed<<16); // this is the seed
|
||||
rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC); // Clear WAS Bit - prepare to write data
|
||||
|
||||
return update(data, datalen);
|
||||
}
|
||||
|
||||
uint16_t FastCRC16::ccitt_upd(const uint8_t *data, size_t len) {return update(data, len);}
|
||||
uint16_t FastCRC16::mcrf4xx_upd(const uint8_t *data, size_t len){return update(data, len);}
|
||||
uint16_t FastCRC16::kermit_upd(const uint8_t *data, size_t len) {return update(data, len);}
|
||||
uint16_t FastCRC16::modbus_upd(const uint8_t *data, size_t len) {return update(data, len);}
|
||||
uint16_t FastCRC16::xmodem_upd(const uint8_t *data, size_t len) {return update(data, len);}
|
||||
uint16_t FastCRC16::x25_upd(const uint8_t *data, size_t len) {return update(data, len);}
|
||||
|
||||
|
||||
|
||||
// ================= 32-BIT CRC ===================
|
||||
|
||||
/** Constructor
|
||||
* Enables CRC-clock
|
||||
*/
|
||||
FastCRC32::FastCRC32(){
|
||||
SIM_SCGC6 |= SIM_SCGC6_CRC;
|
||||
}
|
||||
|
||||
/** CRC32
|
||||
* Alias CRC-32/ADCCP, PKZIP, Ethernet, 802.3
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint32_t FastCRC32::crc32(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x04c11db7 init=0xffffffff refin=true refout=true xorout=0xffffffff check=0xcbf43926
|
||||
return generic(0x04C11DB7L, 0XFFFFFFFFL, CRC_FLAG_REFLECT | CRC_FLAG_XOR, data, datalen);
|
||||
}
|
||||
|
||||
/** CKSUM
|
||||
* Alias CRC-32/POSIX
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint32_t FastCRC32::cksum(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// width=32 poly=0x04c11db7 init=0x00000000 refin=false refout=false xorout=0xffffffff check=0x765e7680
|
||||
return generic(0x04C11DB7L, 0, CRC_FLAG_NOREFLECT | CRC_FLAG_XOR, data, datalen);
|
||||
}
|
||||
|
||||
/** Update
|
||||
* Call for subsequent calculations with previous seed
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
//#pragma GCC diagnostic ignored "-Wpointer-arith"
|
||||
uint32_t FastCRC32::update(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
|
||||
const uint8_t *src = data;
|
||||
const uint8_t *target = src + datalen;
|
||||
|
||||
while (((uintptr_t)src & 0x03) != 0 && (src < target)) {
|
||||
rCRC->CRC8_3 = *src++; //Write 8 BIT
|
||||
}
|
||||
|
||||
while (src <= target-4) {
|
||||
rCRC->CRC = *( uint32_t *)src; //Write 32 BIT
|
||||
src += 4;
|
||||
}
|
||||
|
||||
while (src < target) {
|
||||
rCRC->CRC8_3 = *src++; //Write 8 Bit
|
||||
}
|
||||
|
||||
return rCRC->CRC;
|
||||
}
|
||||
|
||||
/** generic function for all 32-Bit CRCs
|
||||
* @param polynom Polynom
|
||||
* @param seed Seed
|
||||
* @param flags Flags
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint32_t FastCRC32::generic(const uint32_t polynom, const uint32_t seed, const uint32_t flags, const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
|
||||
rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC) | (1<<CRC_CTRL_WAS); // 32Bit Mode, prepare to write seed(25)
|
||||
rCRC->GPOLY = polynom; // Set polynom
|
||||
rCRC->CRC = seed; // This is the seed
|
||||
rCRC->CTRL = flags | (1<<CRC_CTRL_TCRC); // Clear WAS Bit - prepare to write data
|
||||
|
||||
return update(data, datalen);
|
||||
}
|
||||
|
||||
uint32_t FastCRC32::crc32_upd(const uint8_t *data, size_t len){return update(data, len);}
|
||||
uint32_t FastCRC32::cksum_upd(const uint8_t *data, size_t len){return update(data, len);}
|
||||
#endif // #if defined(KINETISK)
|
||||
Vendored
+541
@@ -0,0 +1,541 @@
|
||||
/* FastCRC library code is placed under the MIT license
|
||||
* Copyright (c) 2014 - 2021 Frank Bösing
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
//
|
||||
// Thanks to:
|
||||
// - Catalogue of parametrised CRC algorithms, CRC RevEng
|
||||
// http://reveng.sourceforge.net/crc-catalogue/
|
||||
//
|
||||
// - Danjel McGougan (CRC-Table-Generator)
|
||||
//
|
||||
|
||||
#if defined(ARDUINO)
|
||||
#include <Arduino.h>
|
||||
#endif
|
||||
|
||||
#if !defined(KINETISK)
|
||||
|
||||
#if !defined(ARDUINO)
|
||||
#define PROGMEM
|
||||
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
|
||||
template <typename T>
|
||||
static constexpr unsigned short pgm_read_word(T addr) noexcept {
|
||||
return *(const unsigned short *)(addr);
|
||||
};
|
||||
template <typename T>
|
||||
static constexpr unsigned long pgm_read_dword(T addr) noexcept {
|
||||
return *(const unsigned long *)(addr);
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#include "FastCRC.h"
|
||||
#include "FastCRC_tables.h"
|
||||
|
||||
|
||||
static inline
|
||||
uint32_t REV16( uint32_t value)
|
||||
{
|
||||
return (value >> 8) | ((value & 0xff) << 8);
|
||||
}
|
||||
|
||||
static inline
|
||||
uint32_t REV32( uint32_t value)
|
||||
{
|
||||
value = (value >> 16) | ((value & 0xffff) << 16);
|
||||
return ((value >> 8) & 0xff00ff) | ((value & 0xff00ff) << 8);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ================= 7-BIT CRC ===================
|
||||
|
||||
/** Constructor
|
||||
*/
|
||||
FastCRC7::FastCRC7(){}
|
||||
|
||||
/** SMBUS CRC
|
||||
* aka CRC-8
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint8_t FastCRC7::crc7_upd(const uint8_t *data, size_t datalen)
|
||||
{
|
||||
uint8_t crc = seed;
|
||||
if (datalen) do {
|
||||
crc = pgm_read_byte(&crc_table_crc7[crc ^ *data]);
|
||||
data++;
|
||||
} while (--datalen);
|
||||
seed = crc;
|
||||
return crc >> 1;
|
||||
}
|
||||
|
||||
uint8_t FastCRC7::crc7(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x09 init=0x00 refin=false refout=false xorout=0x00 check=0x75
|
||||
seed = 0x00;
|
||||
return crc7_upd(data, datalen);
|
||||
}
|
||||
|
||||
// ================= 8-BIT CRC ===================
|
||||
|
||||
/** Constructor
|
||||
*/
|
||||
FastCRC8::FastCRC8(){}
|
||||
|
||||
/** SMBUS CRC
|
||||
* aka CRC-8
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint8_t FastCRC8::smbus_upd(const uint8_t *data, size_t datalen)
|
||||
{
|
||||
uint8_t crc = seed;
|
||||
if (datalen) do {
|
||||
crc = pgm_read_byte(&crc_table_smbus[crc ^ *data]);
|
||||
data++;
|
||||
} while (--datalen);
|
||||
seed = crc;
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint8_t FastCRC8::smbus(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x07 init=0x00 refin=false refout=false xorout=0x00 check=0xf4
|
||||
seed = 0x00;
|
||||
return smbus_upd(data, datalen);
|
||||
}
|
||||
|
||||
/** MAXIM 8-Bit CRC
|
||||
* equivalent to _crc_ibutton_update() in crc16.h from avr_libc
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint8_t FastCRC8::maxim_upd(const uint8_t *data, size_t datalen)
|
||||
{
|
||||
uint8_t crc = seed;
|
||||
if (datalen) do {
|
||||
crc = pgm_read_byte(&crc_table_maxim[crc ^ *data]);
|
||||
data++;
|
||||
} while (--datalen);
|
||||
seed = crc;
|
||||
return crc;
|
||||
}
|
||||
uint8_t FastCRC8::maxim(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x31 init=0x00 refin=true refout=true xorout=0x00 check=0xa1
|
||||
seed = 0x00;
|
||||
return maxim_upd(data, datalen);
|
||||
}
|
||||
|
||||
// ================= 16-BIT CRC ===================
|
||||
/** Constructor
|
||||
*/
|
||||
FastCRC16::FastCRC16(){}
|
||||
|
||||
#define crc_n4(crc, data, table) crc ^= data; \
|
||||
crc = pgm_read_word(&table[(crc & 0xff) + 0x300]) ^ \
|
||||
pgm_read_word(&table[((crc >> 8) & 0xff) + 0x200]) ^ \
|
||||
pgm_read_word(&table[((data >> 16) & 0xff) + 0x100]) ^ \
|
||||
pgm_read_word(&table[data >> 24]);
|
||||
|
||||
/** CCITT
|
||||
* Alias "false CCITT"
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::ccitt_upd(const uint8_t *data, size_t len)
|
||||
{
|
||||
|
||||
uint16_t crc = seed;
|
||||
while (((uintptr_t)data & 3) && len) {
|
||||
crc = (crc >> 8) ^ pgm_read_word(&crc_table_ccitt[(crc & 0xff) ^ *data++]);
|
||||
len--;
|
||||
}
|
||||
|
||||
while (len >= 16) {
|
||||
len -= 16;
|
||||
crc_n4(crc, ((uint32_t *)data)[0], crc_table_ccitt);
|
||||
crc_n4(crc, ((uint32_t *)data)[1], crc_table_ccitt);
|
||||
crc_n4(crc, ((uint32_t *)data)[2], crc_table_ccitt);
|
||||
crc_n4(crc, ((uint32_t *)data)[3], crc_table_ccitt);
|
||||
data += 16;
|
||||
}
|
||||
|
||||
while (len--) {
|
||||
crc = (crc >> 8) ^ pgm_read_word(&crc_table_ccitt[(crc & 0xff) ^ *data++]);
|
||||
}
|
||||
|
||||
seed = crc;
|
||||
crc = REV16(crc);
|
||||
|
||||
return crc;
|
||||
}
|
||||
uint16_t FastCRC16::ccitt(const uint8_t *data,const size_t datalen)
|
||||
{
|
||||
// poly=0x1021 init=0xffff refin=false refout=false xorout=0x0000 check=0x29b1
|
||||
seed = 0xffff;
|
||||
return ccitt_upd(data, datalen);
|
||||
}
|
||||
|
||||
/** MCRF4XX
|
||||
* equivalent to _crc_ccitt_update() in crc16.h from avr_libc
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
|
||||
uint16_t FastCRC16::mcrf4xx_upd(const uint8_t *data, size_t len)
|
||||
{
|
||||
|
||||
uint16_t crc = seed;
|
||||
|
||||
while (((uintptr_t)data & 3) && len) {
|
||||
crc = (crc >> 8) ^ pgm_read_word(&crc_table_mcrf4xx[(crc & 0xff) ^ *data++]);
|
||||
len--;
|
||||
}
|
||||
|
||||
while (len >= 16) {
|
||||
len -= 16;
|
||||
crc_n4(crc, ((uint32_t *)data)[0], crc_table_mcrf4xx);
|
||||
crc_n4(crc, ((uint32_t *)data)[1], crc_table_mcrf4xx);
|
||||
crc_n4(crc, ((uint32_t *)data)[2], crc_table_mcrf4xx);
|
||||
crc_n4(crc, ((uint32_t *)data)[3], crc_table_mcrf4xx);
|
||||
data += 16;
|
||||
}
|
||||
|
||||
while (len--) {
|
||||
crc = (crc >> 8) ^ pgm_read_word(&crc_table_mcrf4xx[(crc & 0xff) ^ *data++]);
|
||||
}
|
||||
|
||||
seed = crc;
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint16_t FastCRC16::mcrf4xx(const uint8_t *data,const size_t datalen)
|
||||
{
|
||||
// poly=0x1021 init=0xffff refin=true refout=true xorout=0x0000 check=0x6f91
|
||||
seed = 0;
|
||||
return mcrf4xx_upd(data, datalen);
|
||||
}
|
||||
|
||||
/** MODBUS
|
||||
* equivalent to _crc_16_update() in crc16.h from avr_libc
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::modbus_upd(const uint8_t *data, size_t len)
|
||||
{
|
||||
|
||||
uint16_t crc = seed;
|
||||
|
||||
while (((uintptr_t)data & 3) && len) {
|
||||
crc = (crc >> 8) ^ pgm_read_word(&crc_table_modbus[(crc & 0xff) ^ *data++]);
|
||||
len--;
|
||||
}
|
||||
|
||||
while (len >= 16) {
|
||||
len -= 16;
|
||||
crc_n4(crc, ((uint32_t *)data)[0], crc_table_modbus);
|
||||
crc_n4(crc, ((uint32_t *)data)[1], crc_table_modbus);
|
||||
crc_n4(crc, ((uint32_t *)data)[2], crc_table_modbus);
|
||||
crc_n4(crc, ((uint32_t *)data)[3], crc_table_modbus);
|
||||
data += 16;
|
||||
}
|
||||
|
||||
while (len--) {
|
||||
crc = (crc >> 8) ^ pgm_read_word(&crc_table_modbus[(crc & 0xff) ^ *data++]);
|
||||
}
|
||||
|
||||
seed = crc;
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint16_t FastCRC16::modbus(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x8005 init=0xffff refin=true refout=true xorout=0x0000 check=0x4b37
|
||||
seed = 0xffff;
|
||||
return modbus_upd(data, datalen);
|
||||
}
|
||||
|
||||
/** KERMIT
|
||||
* Alias CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-CCITT
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::kermit_upd(const uint8_t *data, size_t len)
|
||||
{
|
||||
|
||||
uint16_t crc = seed;
|
||||
|
||||
while (((uintptr_t)data & 3) && len) {
|
||||
crc = (crc >> 8) ^ pgm_read_word(&crc_table_kermit[(crc & 0xff) ^ *data++]);
|
||||
len--;
|
||||
}
|
||||
|
||||
while (len >= 16) {
|
||||
len -= 16;
|
||||
crc_n4(crc, ((uint32_t *)data)[0], crc_table_kermit);
|
||||
crc_n4(crc, ((uint32_t *)data)[1], crc_table_kermit);
|
||||
crc_n4(crc, ((uint32_t *)data)[2], crc_table_kermit);
|
||||
crc_n4(crc, ((uint32_t *)data)[3], crc_table_kermit);
|
||||
data += 16;
|
||||
}
|
||||
|
||||
while (len--) {
|
||||
crc = (crc >> 8) ^ pgm_read_word(&crc_table_kermit[(crc & 0xff) ^ *data++]);
|
||||
}
|
||||
|
||||
seed = crc;
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint16_t FastCRC16::kermit(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x1021 init=0x0000 refin=true refout=true xorout=0x0000 check=0x2189
|
||||
// sometimes byteswapped presentation of result
|
||||
seed = 0x0000;
|
||||
return kermit_upd(data, datalen);
|
||||
}
|
||||
|
||||
/** XMODEM
|
||||
* Alias ZMODEM, CRC-16/ACORN
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::xmodem_upd(const uint8_t *data, size_t len)
|
||||
{
|
||||
|
||||
uint16_t crc = seed;
|
||||
|
||||
while (((uintptr_t)data & 3) && len) {
|
||||
crc = (crc >> 8) ^ pgm_read_word(&crc_table_xmodem[(crc & 0xff) ^ *data++]);
|
||||
len--;
|
||||
}
|
||||
|
||||
while (len >= 16) {
|
||||
len -= 16;
|
||||
crc_n4(crc, ((uint32_t *)data)[0], crc_table_xmodem);
|
||||
crc_n4(crc, ((uint32_t *)data)[1], crc_table_xmodem);
|
||||
crc_n4(crc, ((uint32_t *)data)[2], crc_table_xmodem);
|
||||
crc_n4(crc, ((uint32_t *)data)[3], crc_table_xmodem);
|
||||
data += 16;
|
||||
}
|
||||
|
||||
while (len--) {
|
||||
crc = (crc >> 8) ^ pgm_read_word(&crc_table_xmodem[(crc & 0xff) ^ *data++]);
|
||||
}
|
||||
|
||||
seed = crc;
|
||||
crc = REV16(crc);
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint16_t FastCRC16::xmodem(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
//width=16 poly=0x1021 init=0x0000 refin=false refout=false xorout=0x0000 check=0x31c3
|
||||
seed = 0x0000;
|
||||
return xmodem_upd(data, datalen);
|
||||
}
|
||||
|
||||
/** X25
|
||||
* Alias CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-B
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
uint16_t FastCRC16::x25_upd(const uint8_t *data, size_t len)
|
||||
{
|
||||
|
||||
uint16_t crc = seed;
|
||||
|
||||
while (((uintptr_t)data & 3) && len) {
|
||||
crc = (crc >> 8) ^ pgm_read_word(&crc_table_x25[(crc & 0xff) ^ *data++]);
|
||||
len--;
|
||||
}
|
||||
|
||||
while (len >= 16) {
|
||||
len -= 16;
|
||||
crc_n4(crc, ((uint32_t *)data)[0], crc_table_x25);
|
||||
crc_n4(crc, ((uint32_t *)data)[1], crc_table_x25);
|
||||
crc_n4(crc, ((uint32_t *)data)[2], crc_table_x25);
|
||||
crc_n4(crc, ((uint32_t *)data)[3], crc_table_x25);
|
||||
data += 16;
|
||||
}
|
||||
|
||||
while (len--) {
|
||||
crc = (crc >> 8) ^ pgm_read_word(&crc_table_x25[(crc & 0xff) ^ *data++]);
|
||||
}
|
||||
|
||||
seed = crc;
|
||||
crc = ~crc;
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint16_t FastCRC16::x25(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x1021 init=0xffff refin=true refout=true xorout=0xffff check=0x906e
|
||||
seed = 0xffff;
|
||||
return x25_upd(data, datalen);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ================= 32-BIT CRC ===================
|
||||
/** Constructor
|
||||
*/
|
||||
FastCRC32::FastCRC32(){}
|
||||
|
||||
#define crc_n4d(crc, data, table) crc ^= data; \
|
||||
crc = pgm_read_dword(&table[(crc & 0xff) + 0x300]) ^ \
|
||||
pgm_read_dword(&table[((crc >> 8) & 0xff) + 0x200]) ^ \
|
||||
pgm_read_dword(&table[((crc >> 16) & 0xff) + 0x100]) ^ \
|
||||
pgm_read_dword(&table[(crc >> 24) & 0xff]);
|
||||
|
||||
#define crcsm_n4d(crc, data, table) crc ^= data; \
|
||||
crc = (crc >> 8) ^ pgm_read_dword(&table[crc & 0xff]); \
|
||||
crc = (crc >> 8) ^ pgm_read_dword(&table[crc & 0xff]); \
|
||||
crc = (crc >> 8) ^ pgm_read_dword(&table[crc & 0xff]); \
|
||||
crc = (crc >> 8) ^ pgm_read_dword(&table[crc & 0xff]);
|
||||
|
||||
/** CRC32
|
||||
* Alias CRC-32/ADCCP, PKZIP, Ethernet, 802.3
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
#if CRC_BIGTABLES
|
||||
#define CRC_TABLE_CRC32 crc_table_crc32_big
|
||||
#else
|
||||
#define CRC_TABLE_CRC32 crc_table_crc32
|
||||
#endif
|
||||
|
||||
uint32_t FastCRC32::crc32_upd(const uint8_t *data, size_t len)
|
||||
{
|
||||
|
||||
uint32_t crc = seed;
|
||||
|
||||
while (((uintptr_t)data & 3) && len) {
|
||||
crc = (crc >> 8) ^ pgm_read_dword(&CRC_TABLE_CRC32[(crc & 0xff) ^ *data++]);
|
||||
len--;
|
||||
}
|
||||
|
||||
while (len >= 16) {
|
||||
len -= 16;
|
||||
#if CRC_BIGTABLES
|
||||
crc_n4d(crc, ((uint32_t *)data)[0], CRC_TABLE_CRC32);
|
||||
crc_n4d(crc, ((uint32_t *)data)[1], CRC_TABLE_CRC32);
|
||||
crc_n4d(crc, ((uint32_t *)data)[2], CRC_TABLE_CRC32);
|
||||
crc_n4d(crc, ((uint32_t *)data)[3], CRC_TABLE_CRC32);
|
||||
#else
|
||||
crcsm_n4d(crc, ((uint32_t *)data)[0], CRC_TABLE_CRC32);
|
||||
crcsm_n4d(crc, ((uint32_t *)data)[1], CRC_TABLE_CRC32);
|
||||
crcsm_n4d(crc, ((uint32_t *)data)[2], CRC_TABLE_CRC32);
|
||||
crcsm_n4d(crc, ((uint32_t *)data)[3], CRC_TABLE_CRC32);
|
||||
#endif
|
||||
data += 16;
|
||||
}
|
||||
|
||||
while (len--) {
|
||||
crc = (crc >> 8) ^ pgm_read_dword(&CRC_TABLE_CRC32[(crc & 0xff) ^ *data++]);
|
||||
}
|
||||
|
||||
seed = crc;
|
||||
crc = ~crc;
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint32_t FastCRC32::crc32(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// poly=0x04c11db7 init=0xffffffff refin=true refout=true xorout=0xffffffff check=0xcbf43926
|
||||
seed = 0xffffffff;
|
||||
return crc32_upd(data, datalen);
|
||||
}
|
||||
|
||||
/** CKSUM
|
||||
* Alias CRC-32/POSIX
|
||||
* @param data Pointer to Data
|
||||
* @param datalen Length of Data
|
||||
* @return CRC value
|
||||
*/
|
||||
#if CRC_BIGTABLES
|
||||
#define CRC_TABLE_CKSUM crc_table_cksum_big
|
||||
#else
|
||||
#define CRC_TABLE_CKSUM crc_table_cksum
|
||||
#endif
|
||||
uint32_t FastCRC32::cksum_upd(const uint8_t *data, size_t len)
|
||||
{
|
||||
|
||||
uint32_t crc = seed;
|
||||
|
||||
while (((uintptr_t)data & 3) && len) {
|
||||
crc = (crc >> 8) ^ pgm_read_dword(&CRC_TABLE_CKSUM[(crc & 0xff) ^ *data++]);
|
||||
len--;
|
||||
}
|
||||
|
||||
while (len >= 16) {
|
||||
len -= 16;
|
||||
#if CRC_BIGTABLES
|
||||
crc_n4d(crc, ((uint32_t *)data)[0], CRC_TABLE_CKSUM);
|
||||
crc_n4d(crc, ((uint32_t *)data)[1], CRC_TABLE_CKSUM);
|
||||
crc_n4d(crc, ((uint32_t *)data)[2], CRC_TABLE_CKSUM);
|
||||
crc_n4d(crc, ((uint32_t *)data)[3], CRC_TABLE_CKSUM);
|
||||
#else
|
||||
crcsm_n4d(crc, ((uint32_t *)data)[0], CRC_TABLE_CKSUM);
|
||||
crcsm_n4d(crc, ((uint32_t *)data)[1], CRC_TABLE_CKSUM);
|
||||
crcsm_n4d(crc, ((uint32_t *)data)[2], CRC_TABLE_CKSUM);
|
||||
crcsm_n4d(crc, ((uint32_t *)data)[3], CRC_TABLE_CKSUM);
|
||||
#endif
|
||||
data += 16;
|
||||
}
|
||||
|
||||
while (len--) {
|
||||
crc = (crc >> 8) ^ pgm_read_dword(&CRC_TABLE_CKSUM[(crc & 0xff) ^ *data++]);
|
||||
}
|
||||
|
||||
seed = crc;
|
||||
crc = ~REV32(crc);
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint32_t FastCRC32::cksum(const uint8_t *data, const size_t datalen)
|
||||
{
|
||||
// width=32 poly=0x04c11db7 init=0x00000000 refin=false refout=false xorout=0xffffffff check=0x765e7680
|
||||
seed = 0x00;
|
||||
return cksum_upd(data, datalen);
|
||||
}
|
||||
|
||||
#endif // #if !defined(KINETISK)
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Frank
|
||||
|
||||
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.
|
||||
Vendored
+56
@@ -0,0 +1,56 @@
|
||||
FastCRC
|
||||
=======
|
||||
|
||||
Fast CRC Arduino library
|
||||
Up to 30 times faster than crc16.h (_avr_libc)
|
||||
|
||||
- uses the on-chip hardware for Teensy 3.0 / 3.1 / 3.2 / 3.5 / 3.6
|
||||
- uses fast table-algorithms for other chips
|
||||
|
||||
List of supported CRC calculations:
|
||||
-
|
||||
7 BIT:
|
||||
|
||||
CRC7
|
||||
(poly=0x09 init=0x00 refin=false refout=false xorout=0x00 check=0x75)
|
||||
MultiMediaCard interface
|
||||
|
||||
|
||||
8 BIT:
|
||||
|
||||
SMBUS
|
||||
(poly=0x07 init=0x00 refin=false refout=false xorout=0x00 check=0xf4)
|
||||
|
||||
MAXIM
|
||||
(poly=0x31 init=0x00 refin=true refout=true xorout=0x00 check=0xa1)
|
||||
|
||||
|
||||
16 BIT:
|
||||
|
||||
KERMIT (Alias CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-CCITT)
|
||||
(poly=0x1021 init=0x0000 refin=true refout=true xorout=0x0000 check=0x2189
|
||||
Attention: sometimes you'll find byteswapped presentation of result in other implementations)
|
||||
|
||||
CCITT-FALSE
|
||||
(poly=0x1021 init=0xffff refin=false refout=false xorout=0x0000 check=0x29b1)
|
||||
|
||||
MCRF4XX
|
||||
(poly=0x1021 init=0xffff refin=true refout=true xorout=0x0000 check=0x6f91)
|
||||
|
||||
MODBUS
|
||||
(poly=0x8005 init=0xffff refin=true refout=true xorout=0x0000 check=0x4b37)
|
||||
|
||||
XMODEM (Alias ZMODEM, CRC-16/ACORN)
|
||||
(poly=0x1021 init=0x0000 refin=false refout=false xorout=0x0000 check=0x31c3)
|
||||
|
||||
X25 (Alias CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-B)
|
||||
(poly=0x1021 init=0xffff refin=true refout=true xorout=0xffff check=0x906e)
|
||||
|
||||
|
||||
32 BIT:
|
||||
|
||||
CRC32, CRC-32/ADCCP, PKZIP, ETHERNET, 802.3
|
||||
(poly=0x04c11db7 init=0xffffffff refin=true refout=true xorout=0xffffffff check=0xcbf43926)
|
||||
|
||||
CKSUM, CRC-32/POSIX
|
||||
(poly=0x04c11db7 init=0x00000000 refin=false refout=false xorout=0xffffffff check=0x765e7680)
|
||||
Reference in New Issue
Block a user