GY-63_MS5611/libraries/CRC/README.md

223 lines
8.0 KiB
Markdown
Raw Normal View History

2021-01-29 06:31:58 -05:00
[![Arduino CI](https://github.com/RobTillaart/CRC/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
2021-10-27 07:42:30 -04:00
[![Arduino-lint](https://github.com/RobTillaart/CRC/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/CRC/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/CRC/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/CRC/actions/workflows/jsoncheck.yml)
2023-10-19 06:13:06 -04:00
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/CRC.svg)](https://github.com/RobTillaart/CRC/issues)
2021-01-29 06:31:58 -05:00
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/CRC/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/CRC.svg?maxAge=3600)](https://github.com/RobTillaart/CRC/releases)
2023-10-19 06:13:06 -04:00
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/CRC.svg)](https://registry.platformio.org/libraries/robtillaart/CRC)
2021-01-29 06:31:58 -05:00
# CRC
2022-01-26 04:27:58 -05:00
Arduino library with CRC8, CRC12, CRC16, CRC32 and CRC64 functions.
2021-01-29 06:31:58 -05:00
## Description
2023-01-17 10:18:12 -05:00
Goal of this library is to have a flexible and portable set of generic
2021-12-14 14:22:40 -05:00
CRC functions and classes.
2021-01-29 06:31:58 -05:00
2023-01-17 10:18:12 -05:00
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 single packet but also optional missing packets,
2022-05-20 03:04:29 -04:00
or even injected packets.
2021-01-29 06:31:58 -05:00
2023-01-17 10:18:12 -05:00
Another trick one can do with the class CRCx is to change the polynome or
2022-05-20 03:04:29 -04:00
the reverse flag runtime during the process. This makes it harder to imitate.
2021-04-07 07:31:22 -04:00
Furthermore the class allows to add values in single steps and continue too.
2023-01-17 10:18:12 -05:00
Finally the class version gives more readable code (IMHO) as the parameters
2021-12-14 14:22:40 -05:00
are explicitly set.
2021-01-29 06:31:58 -05:00
2023-08-01 11:27:01 -04:00
**Note** the classes have similar names as the static functions
The class name is always (full) UPPER case.
So **CRC8** is a class and **calcCRC8()** is the function.
2021-01-29 06:31:58 -05:00
2023-01-17 10:18:12 -05:00
#### Related
- https://github.com/RobTillaart/Adler
- https://github.com/RobTillaart/CRC
- https://github.com/RobTillaart/Fletcher
- https://github.com/RobTillaart/LUHN
2023-08-01 11:27:01 -04:00
- https://github.com/RobTillaart/PrintHelpers (for CRC64)
Deeper tech info
2023-01-17 10:18:12 -05:00
2023-08-01 11:27:01 -04:00
- https://en.wikipedia.org/wiki/Cyclic_redundancy_check - generic background.
- http://zorc.breitbandkatze.de/crc.html - online CRC calculator (any base up to 64 is supported.)
- https://crccalc.com/ - online CRC calculator to verify.
- https://www.lddgo.net/en/encrypt/crc - online CRC calculator
- http://www.sunshine2k.de/coding/javascript/crc/crc_js.html - online CRC calculator
2023-01-17 10:18:12 -05:00
2023-08-01 11:27:01 -04:00
and many other websites.
2021-01-29 06:31:58 -05:00
2023-10-19 06:13:06 -04:00
2021-01-29 06:31:58 -05:00
## Interface CRC classes
2023-10-19 06:13:06 -04:00
```cpp
#include "CRC8.h"
```
#### Base
2022-05-20 03:04:29 -04:00
The interfaces are very similar for CRC8, CRC12, CRC16, CRC32 and CRC64 class.
2023-08-01 11:27:01 -04:00
The difference is the data type for polynome, start- and end-mask,
2021-12-14 14:22:40 -05:00
and the returned CRC.
2023-08-01 11:27:01 -04:00
The start mask is named ```initial``` and end-mask is named ```XOR-out```.
2021-01-29 06:31:58 -05:00
2022-02-07 08:10:53 -05:00
2023-01-17 10:18:12 -05:00
#### Base
2022-01-28 08:56:11 -05:00
2021-01-29 06:31:58 -05:00
Use **\#include "CRC8.h"**
2023-08-01 11:27:01 -04:00
The interfaces for CRC12, CRC16, CRC32 and CRC64 are similar to CRC8.
2023-07-13 05:08:10 -04:00
- **CRC8(polynome, initial, xorOut, reverseIn, reverseOut)** Constructor to set all parameters at once.
2023-08-01 11:27:01 -04:00
- **void reset()** set all internals to defaults of the constructor.
2022-02-07 08:10:53 -05:00
- **void restart()** reset internal CRC and count only;
2023-08-01 11:27:01 -04:00
reuses values for the other flags e.g polynome, XOR masks and reverse flags.
2023-07-13 05:08:10 -04:00
- **uint8_t calc()** 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.
2023-08-01 11:27:01 -04:00
- **crc_size_t count()** returns number of values added since (re)start.
The default == 0.
- **void add(value)** add a single value to the CRC calculation.
- **void add(array, length)** add an array of length values to the CRC calculation.
In case of a warning/error for the array type, use casting e.g. (uint8_t \*).
- **void add(array, length, yieldPeriod)** as CRC calculations of large blocks
can take serious time (in milliseconds), the classes call **yield()** after every
**yieldPeriod** calls to keep RTOS environments happy.
The call allows to add values with **yield()** to get optimal performance.
The risk is missing context switching to handle interrupts etc. So use at own risk.
2022-02-07 08:10:53 -05:00
2023-01-17 10:18:12 -05:00
#### Parameters
2022-01-28 08:56:11 -05:00
2023-08-01 11:27:01 -04:00
These functions allows to set individual parameters of the CRC at runtime.
2022-05-20 03:04:29 -04:00
The parameters do not have defaults so the user must set them explicitly.
2022-01-28 08:56:11 -05:00
2023-08-01 11:27:01 -04:00
- **void setPolynome(polynome)** set polynome.
Note: **reset()** and **restart()** sets a default polynome.
- **void setInitial(initial)** set the start-mask.
- **void setXorOut(xorOut)** set the end-mask.
2023-07-13 05:08:10 -04:00
- **void setReverseIn(reverseIn)** reverse the bit pattern of input data (MSB vs LSB).
- **void setReverseOut(reverseOut)** reverse the bit pattern of CRC (MSB vs LSB).
- **uint8_t getPolynome()** return parameter set above or default.
- **uint8_t getInitial()** return parameter set above or default.
- **uint8_t getXorOut()** return parameter set above or default.
2022-01-28 08:56:11 -05:00
- **bool getReverseIn()** return parameter set above or default.
- **bool getReverseOut()** return parameter set above or default.
2023-08-01 11:27:01 -04:00
2021-01-29 06:31:58 -05:00
### Example snippet
2023-08-01 11:27:01 -04:00
A minimal usage needs:
2023-07-13 05:08:10 -04:00
- the constructor, the add() function and the calc() function.
2021-01-29 06:31:58 -05:00
```cpp
2021-04-07 07:31:22 -04:00
#include "CRC32.h"
...
2021-01-29 06:31:58 -05:00
CRC32 crc;
...
while (Serial.available())
{
int c = Serial.read();
crc.add(c);
}
2023-07-13 05:08:10 -04:00
Serial.println(crc.calc());
2021-01-29 06:31:58 -05:00
```
## Interface static functions
Use **\#include "CRC.h"**
Most functions have a default polynome, same start and end masks, and default there is no reversing.
2023-01-17 10:18:12 -05:00
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.
2021-01-29 06:31:58 -05:00
2023-07-13 05:08:10 -04:00
- **uint8_t calcCRC8(array, length)** idem with default polynome.
- **uint16_t calcCRC12(array, length)** idem with default polynome.
- **uint16_t calcCRC16(array, length)** idem with default polynome.
- **uint32_t calcCRC32(array, length)** idem with default polynome.
- **uint64_t calcCRC64(array, length)** - experimental version, no reference found except on Wikipedia.
2021-01-29 06:31:58 -05:00
2023-07-13 05:08:10 -04:00
The static functions **calcCRC..()** in this library also support yield.
2021-01-29 06:31:58 -05:00
2023-01-17 10:18:12 -05:00
The static CRC functions use fast reverse functions that can be also be
2022-02-08 04:10:54 -05:00
used outside CRC context. Their usage is straightforward.
2022-01-28 08:56:11 -05:00
2023-07-13 05:08:10 -04:00
- **uint8_t reverse8bits(uint8_t in)** idem.
- **uint16_t reverse16bits(uint16_t in)** idem.
- **uint16_t reverse12bits(uint16_t in)** idem.
- **uint32_t reverse32bits(uint32_t in)** idem.
- **uint64_t reverse64bits(uint64_t in)** idem.
2022-01-28 08:56:11 -05:00
2023-07-13 05:08:10 -04:00
reverse12bits is based upon reverse16bits, with a final shift.
2022-01-28 08:56:11 -05:00
Other reverses can be created in similar way.
2021-01-29 06:31:58 -05:00
2023-07-13 05:08:10 -04:00
## CrcParameters.h
2022-02-08 04:10:54 -05:00
2023-08-01 11:27:01 -04:00
Since version 1.0.0 the file **CrcParameters.h** is added to hold symbolic names for certain parameters (polynomes, etc..).
2022-02-08 04:10:54 -05:00
These can be used in your code too to minimize the number of "magic HEX codes".
If standard polynomes are missing, please open an issue and report, with reference.
2023-08-01 11:27:01 -04:00
(CrcParameters.h replaces and extends CRC_polynomes.h)
2022-02-08 04:10:54 -05:00
2021-12-14 14:22:40 -05:00
2021-01-29 06:31:58 -05:00
## Future
2023-01-17 10:18:12 -05:00
#### Must
2023-08-01 11:27:01 -04:00
- extended performance measurements 1.x.x version
- both class and functions.
2023-01-17 10:18:12 -05:00
#### Should
- add examples.
2022-01-28 08:56:11 -05:00
- example showing multiple packages of data linked by their CRC.
2023-01-17 10:18:12 -05:00
sort of "blockchain"
#### Could
2022-05-20 03:04:29 -04:00
- table versions for performance? (performance - memory discussion).
2021-01-29 06:31:58 -05:00
- stream version - 4 classes class?
2022-02-07 08:10:53 -05:00
- **setCRC(value)** to be able to pick up where one left ?
2023-08-01 11:27:01 -04:00
- can be done with **setInitial()**
2022-05-20 03:04:29 -04:00
- needs **getRawCRC()** without reverse and end mask
2021-01-29 06:31:58 -05:00
#### Exotic CRC's ?
- **CRC1()** // parity :)
2023-07-13 05:08:10 -04:00
- **CRC4()** nibbles?
2023-01-17 10:18:12 -05:00
- default polynome 0x03 ITU
2022-02-07 08:10:53 -05:00
- One CRC() with #bits as parameter?
2023-01-17 10:18:12 -05:00
- up to 64 bit for all missing ones?
2022-02-07 08:10:53 -05:00
- performance penalty
2022-05-20 03:04:29 -04:00
- One CRC() template class?
2021-01-29 06:31:58 -05:00
2022-02-07 08:10:53 -05:00
#### Won't
2021-01-29 06:31:58 -05:00
2022-02-07 08:10:53 -05:00
- add a dump(Stream = Serial) to see all the settings at once.
user can access parameters, so no need.
2021-01-29 06:31:58 -05:00
2023-01-17 10:18:12 -05:00
2023-10-19 06:13:06 -04:00
## Support
If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.
Thank you,