mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.4 CRC
This commit is contained in:
parent
84dbb68989
commit
87ad71de4d
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: CRC.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.3
|
||||
// VERSION: 0.1.4
|
||||
// PURPOSE: Arduino library fir CRC8, CRC16, CRC16-CCITT, CRC32
|
||||
// URL: https://github.com/RobTillaart/CRC
|
||||
//
|
||||
@ -11,7 +11,7 @@
|
||||
#include "Arduino.h"
|
||||
|
||||
|
||||
#define CRC_LIB_VERSION (F("0.1.3"))
|
||||
#define CRC_LIB_VERSION (F("0.1.4"))
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
@ -64,6 +64,7 @@ uint64_t reverse64(uint64_t in)
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// CRC POLYNOME = x8 + x5 + x4 + 1 = 1001 1000 = 0x8C
|
||||
@ -186,3 +187,4 @@ uint64_t crc64(uint8_t *array, uint8_t length, uint64_t polynome, uint64_t start
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -93,6 +93,7 @@ uint16_t CRC16::_reverse(uint16_t in)
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
uint8_t CRC16::_reverse8(uint8_t in)
|
||||
{
|
||||
uint8_t x = in;
|
||||
@ -102,4 +103,6 @@ uint8_t CRC16::_reverse8(uint8_t in)
|
||||
return x;
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -48,4 +48,6 @@ private:
|
||||
uint32_t _count;
|
||||
};
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -93,6 +93,7 @@ uint32_t CRC32::_reverse(uint32_t in)
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
uint8_t CRC32::_reverse8(uint8_t in)
|
||||
{
|
||||
uint8_t x = in;
|
||||
@ -102,4 +103,6 @@ uint8_t CRC32::_reverse8(uint8_t in)
|
||||
return x;
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -48,4 +48,6 @@ private:
|
||||
uint32_t _count;
|
||||
};
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -94,6 +94,7 @@ uint64_t CRC64::_reverse(uint64_t in)
|
||||
return x;
|
||||
}
|
||||
|
||||
|
||||
uint8_t CRC64::_reverse8(uint8_t in)
|
||||
{
|
||||
uint8_t x = in;
|
||||
@ -103,4 +104,6 @@ uint8_t CRC64::_reverse8(uint8_t in)
|
||||
return x;
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -48,4 +48,6 @@ private:
|
||||
uint64_t _count;
|
||||
};
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -91,4 +91,6 @@ uint8_t CRC8::_reverse(uint8_t in)
|
||||
return x;
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -47,4 +47,6 @@ private:
|
||||
uint32_t _count;
|
||||
};
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021-2021 Rob Tillaart
|
||||
Copyright (c) 2021-2022 Rob Tillaart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
@ -8,21 +8,26 @@
|
||||
|
||||
# CRC
|
||||
|
||||
Arduino library with CRC8, CRC16, CRC32 and CRC64 functions
|
||||
Arduino library with CRC8, CRC16, CRC32 and CRC64 functions.
|
||||
|
||||
|
||||
## Description
|
||||
|
||||
Goal of this library is to have a flexible and portable set of generic CRC functions and classes.
|
||||
Goal of this library is to have a flexible and portable set of generic
|
||||
CRC functions and classes.
|
||||
|
||||
The CRCx classes have a number of added values. Most important is that they allow one to verify intermediate CRC values. This is useful if one sends a "train of packets" which include a CRC so far. This detects both errors in one packet but also missing packets, or injected packages.
|
||||
The CRCx classes have a number of added values. Most important is that
|
||||
they allow one to verify intermediate CRC values. This is useful if one
|
||||
sends a "train of packets" which include a CRC so far. This detects both
|
||||
errors in one packet but also missing packets, or injected packages.
|
||||
|
||||
Another trick one can do is change the polynome or the reverse flag during the process.
|
||||
This makes it harder to imitate.
|
||||
Another trick one can do is change the polynome or the reverse flag during
|
||||
the process. This makes it harder to imitate.
|
||||
|
||||
Furthermore the class allows to add values in single steps and continue too.
|
||||
|
||||
Finally the class version gives more readable code (imho) as the parameters are explicitly set.
|
||||
Finally the class version gives more readable code (imho) as the parameters
|
||||
are explicitly set.
|
||||
|
||||
|
||||
**Note** the classes have same names as the static functions, except the class
|
||||
@ -35,27 +40,31 @@ and many other websites.
|
||||
## Interface CRC classes
|
||||
|
||||
These interfaces are very similar for CRC8, CRC16, CRC32 and CRC64 class.
|
||||
The only difference is the data type for polynome, start- and end-mask, and the returned CRC.
|
||||
The only difference is the data type for polynome, start- and end-mask,
|
||||
and the returned CRC.
|
||||
|
||||
Use **\#include "CRC8.h"**
|
||||
|
||||
- **CRC8()** Constructor
|
||||
- **void reset()** set all internals to constructor defaults.
|
||||
- **void restart()** reset internal CRC and count only; reuse values for other e.g polynome, XOR masks and reverse flags.
|
||||
- **void restart()** reset internal CRC and count only; reuse values for other
|
||||
e.g polynome, XOR masks and reverse flags.
|
||||
- **void setPolynome(polynome)** set polynome, note reset sets a default polynome.
|
||||
- **void setStartXOR(start)** set start-mask, default 0.
|
||||
- **void setEndXOR(end)** set end-mask, default 0.
|
||||
- **void setReverseIn(bool reverseIn)** reverse the bit pattern of input data (MSB vs LSB).
|
||||
- **void setReverseOut(bool reverseOut)** reverse the bit pattern of CRC (MSB vs LSB).
|
||||
- **void add(value)** add a single value to CRC calculation.
|
||||
- **void add(array, uint32_t length)** add an array of values to the CRC. In case of a warning/error use casting to (uint8_t \*).
|
||||
- **uint8_t getCRC()** returns CRC calculated so far. This allows to check the CRC of a really large stream at intermediate moments, e.g. to link multiple packets.
|
||||
- **uint32_t count()** returns number of values added sofar. Default 0.
|
||||
- **void add(array, uint32_t length)** add an array of values to the CRC.
|
||||
In case of a warning/error use casting to (uint8_t \*).
|
||||
- **uint8_t getCRC()** returns CRC calculated so far. This allows to check the CRC of
|
||||
a really large stream at intermediate moments, e.g. to link multiple packets.
|
||||
- **uint32_t count()** returns number of values added so far. Default 0.
|
||||
|
||||
|
||||
### Example snippet
|
||||
|
||||
A minimal usage only needs
|
||||
A minimal usage only needs:
|
||||
- the constructor, the add() function and the getCRC() function.
|
||||
|
||||
```cpp
|
||||
@ -82,15 +91,26 @@ However these parameters allow one to tweak the CRC in all aspects known.
|
||||
In all the examples encountered the reverse flags were set both to false or both to true.
|
||||
For flexibility both parameters are kept available.
|
||||
|
||||
- **uint8_t crc8(array, length, polynome = 0xD5, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome
|
||||
- **uint16_t crc16(array, length, polynome = 0xA001, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome
|
||||
- **uint8_t crc8(array, length, polynome = 0xD5, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome.
|
||||
- **uint16_t crc16(array, length, polynome = 0xA001, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome.
|
||||
- **uint16_t crc16-CCITT(array, length)** fixed polynome **0x1021**, non zero start / end masks.
|
||||
- **uint32_t crc32(array, length, polynome = 0x04C11DB7, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome
|
||||
- **uint64_t crc64(array, length, polynome, start, end, reverseIn, reverseOut)** - experimental version, no reference found except on Wikipedia
|
||||
- **uint32_t crc32(array, length, polynome = 0x04C11DB7, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome.
|
||||
- **uint64_t crc64(array, length, polynome, start, end, reverseIn, reverseOut)** - experimental version, no reference found except on Wikipedia.
|
||||
|
||||
Note these functions are limited to one call per block of data. For more flexibility use the classes.
|
||||
|
||||
|
||||
## Operational
|
||||
|
||||
See examples.
|
||||
|
||||
|
||||
## Links
|
||||
|
||||
- https://en.wikipedia.org/wiki/Cyclic_redundancy_check - generic background.
|
||||
- https://crccalc.com/ - online CRC calculator to verify.
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
- extend examples.
|
||||
@ -105,16 +125,12 @@ Note these functions are limited to one call per block of data. For more flexibi
|
||||
#### Exotic CRC's ?
|
||||
|
||||
- **CRC1()** // parity :)
|
||||
- **CRC4(array, length, polynome, start, end, reverseIn, reverseOut)**
|
||||
- **CRC4(array, length, polynome, start, end, reverseIn, reverseOut)** nibbles?
|
||||
- default polynome 0x03
|
||||
|
||||
|
||||
#### Magic \#defines for "common" polynomes? ?
|
||||
#### Magic \#defines for "common" polynomes? verify ?
|
||||
|
||||
- \#define CRC_ISO64 0x000000000000001B
|
||||
- \#define CRC_ECMA64 0x42F0E1EBA9EA3693
|
||||
|
||||
|
||||
## Operational
|
||||
|
||||
See examples.
|
||||
|
@ -5,6 +5,7 @@
|
||||
// DATE: 2021-01-20
|
||||
// (c) : MIT
|
||||
|
||||
|
||||
#include "CRC16.h"
|
||||
#include "CRC.h"
|
||||
|
||||
@ -12,6 +13,7 @@ char str[24] = "123456789";
|
||||
|
||||
CRC16 crc;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -27,6 +29,7 @@ void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void test()
|
||||
{
|
||||
Serial.println(crc16((uint8_t *) str, 9, 0x1021, 0, 0, false, false), HEX);
|
||||
@ -59,4 +62,6 @@ void test()
|
||||
Serial.println(crc.count());
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -5,12 +5,14 @@
|
||||
// DATE: 2021-01-20
|
||||
// (c) : MIT
|
||||
|
||||
|
||||
#include "CRC32.h"
|
||||
|
||||
char str[24] = "123456789";
|
||||
|
||||
CRC32 crc;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -26,6 +28,7 @@ void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void test()
|
||||
{
|
||||
crc.setPolynome(0x04C11DB7);
|
||||
@ -54,4 +57,6 @@ void test()
|
||||
Serial.println(crc.count());
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
// DATE: 2021-01-20
|
||||
// (c) : MIT
|
||||
|
||||
|
||||
#include "CRC64.h"
|
||||
#include "printHelpers.h" // https://github.com/RobTillaart/printHelpers
|
||||
|
||||
@ -12,6 +13,7 @@ char str[24] = "123456789";
|
||||
|
||||
CRC64 crc;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -27,6 +29,7 @@ void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void test()
|
||||
{
|
||||
crc.setPolynome(0x07);
|
||||
@ -55,4 +58,6 @@ void test()
|
||||
Serial.println(print64(crc.count()));
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -7,7 +7,6 @@
|
||||
|
||||
|
||||
#include "CRC8.h"
|
||||
|
||||
#include "CRC.h"
|
||||
|
||||
|
||||
@ -15,6 +14,7 @@ char str[24] = "123456789";
|
||||
|
||||
CRC8 crc;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -30,6 +30,7 @@ void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void test()
|
||||
{
|
||||
Serial.println(crc8((uint8_t *)str, 9, 0x07), HEX);
|
||||
@ -60,4 +61,6 @@ void test()
|
||||
Serial.println(crc.count());
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -6,13 +6,14 @@
|
||||
// (c) : MIT
|
||||
//
|
||||
|
||||
|
||||
#include "CRC.h"
|
||||
|
||||
char str[122] = "123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
|
||||
|
||||
|
||||
uint32_t start, stop;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -104,13 +105,14 @@ void setup()
|
||||
Serial.println("=============================");
|
||||
delay(100);
|
||||
|
||||
|
||||
Serial.println("\n\nDone...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -6,11 +6,13 @@
|
||||
// (c) : MIT
|
||||
//
|
||||
|
||||
|
||||
#include "CRC.h"
|
||||
#include "printHelpers.h" // for the 64 bit...
|
||||
|
||||
char str[24] = "123456789";
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -85,9 +87,11 @@ void setup()
|
||||
Serial.println("\n\nDone...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -6,11 +6,13 @@
|
||||
// (c) : MIT
|
||||
//
|
||||
|
||||
|
||||
#include "CRC.h"
|
||||
#include "printHelpers.h" // for the 64 bit...
|
||||
|
||||
char str[24] = "123456789";
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -35,9 +37,11 @@ void setup()
|
||||
Serial.println("\n\nDone...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -15,11 +15,13 @@ crc64 KEYWORD2
|
||||
|
||||
reset KEYWORD2
|
||||
restart KEYWORD2
|
||||
|
||||
setPolynome KEYWORD2
|
||||
setStartXOR KEYWORD2
|
||||
setEndXOR KEYWORD2
|
||||
setReverseIn KEYWORD2
|
||||
setReverseOut KEYWORD2
|
||||
|
||||
add KEYWORD2
|
||||
getCRC KEYWORD2
|
||||
count KEYWORD2
|
||||
|
@ -15,8 +15,9 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/CRC"
|
||||
},
|
||||
"version": "0.1.3",
|
||||
"version": "0.1.4",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*"
|
||||
"platforms": "*",
|
||||
"headers": "CRC.h"
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=CRC
|
||||
version=0.1.3
|
||||
version=0.1.4
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library for CRC for Arduino
|
||||
|
@ -43,6 +43,7 @@ uint8_t * data = (uint8_t *) str;
|
||||
|
||||
unittest_setup()
|
||||
{
|
||||
fprintf(stderr, "CRC_LIB_VERSION: %s\n", (char *) CRC_LIB_VERSION);
|
||||
}
|
||||
|
||||
unittest_teardown()
|
||||
@ -52,8 +53,6 @@ unittest_teardown()
|
||||
|
||||
unittest(test_crc8)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", CRC_LIB_VERSION);
|
||||
|
||||
assertEqual(0xF4, crc8(data, 9, 0x07));
|
||||
assertEqual(0xDA, crc8(data, 9, 0x9B, 0xFF));
|
||||
assertEqual(0x15, crc8(data, 9, 0x39, 0x00, 0x00, true, true));
|
||||
@ -69,8 +68,6 @@ unittest(test_crc8)
|
||||
|
||||
unittest(test_crc16)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", CRC_LIB_VERSION);
|
||||
|
||||
assertEqual(0x29B1, crc16(data, 9, 0x1021, 0xFFFF, 0x0000, false, false ));
|
||||
assertEqual(0xBB3D, crc16(data, 9, 0x8005, 0x0000, 0x0000, true, true ));
|
||||
assertEqual(0xE5CC, crc16(data, 9, 0x1021, 0x1D0F, 0x0000, false, false ));
|
||||
@ -99,8 +96,6 @@ unittest(test_crc16)
|
||||
|
||||
unittest(test_crc32)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", CRC_LIB_VERSION);
|
||||
|
||||
assertEqual(0xCBF43926, crc32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true));
|
||||
assertEqual(0xFC891918, crc32(data, 9, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false));
|
||||
assertEqual(0xE3069283, crc32(data, 9, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true));
|
||||
@ -115,8 +110,6 @@ unittest(test_crc32)
|
||||
|
||||
unittest(test_crc64)
|
||||
{
|
||||
fprintf(stderr, "VERSION: %s\n", CRC_LIB_VERSION);
|
||||
|
||||
fprintf(stderr, "no reference yet\n");
|
||||
assertEqual(1, 1);
|
||||
}
|
||||
|
@ -45,6 +45,7 @@ unittest_setup()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
unittest_teardown()
|
||||
{
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user