RobTillaart / CRC

CRC library for Arduino
MIT License
79 stars 17 forks source link
arduino-library crc

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

CRC

Arduino library with CRC8, CRC12, CRC16, CRC32 and CRC64 functions.

Description

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 single packet but also optional missing packets, or even injected packets.

Another trick one can do with the class CRCx is to change the polynome or the reverse flag runtime 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.

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.

Related

Deeper tech info

and many other websites.

Interface CRC classes

#include "CRC8.h"

Base

The interfaces are very similar for CRC8, CRC12, CRC16, CRC32 and CRC64 class. The difference is the data type for polynome, start- and end-mask, and the returned CRC. The start mask is named initial and end-mask is named XOR-out.

Base

Use #include "CRC8.h"

The interfaces for CRC12, CRC16, CRC32 and CRC64 are similar to CRC8.

Parameters

These functions allows to set individual parameters of the CRC at runtime. The parameters do not have defaults so the user must set them explicitly.

Example snippet

A minimal usage needs:

#include "CRC32.h"
...

CRC32 crc;
  ...
  while (Serial.available())
  {
    int c = Serial.read();
    crc.add(c);
  }
  Serial.println(crc.calc());

Interface static functions

Use #include "CRC.h"

Most functions have a default polynome, same start and end masks, and default there is no reversing. 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.

The static functions calcCRC..() in this library also support yield.

The static CRC functions use fast reverse functions that can be also be used outside CRC context. Their usage is straightforward.

reverse12bits is based upon reverse16bits, with a final shift. Other reverses can be created in similar way.

CrcParameters.h

Since version 1.0.0 the file CrcParameters.h is added to hold symbolic names for certain parameters (polynomes, etc..). 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.

(CrcParameters.h replaces and extends CRC_polynomes.h)

Future

Must

Should

Could

Exotic CRC's ?

Won't

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,