Skip to content

Commit

Permalink
Develop (#10)
Browse files Browse the repository at this point in the history
* update library.json, license, minor edits
  • Loading branch information
RobTillaart authored Dec 14, 2021
1 parent 15c42f1 commit 029918d
Show file tree
Hide file tree
Showing 23 changed files with 110 additions and 49 deletions.
6 changes: 4 additions & 2 deletions CRC.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -11,7 +11,7 @@
#include "Arduino.h"


#define CRC_LIB_VERSION (F("0.1.3"))
#define CRC_LIB_VERSION (F("0.1.4"))


////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -64,6 +64,7 @@ uint64_t reverse64(uint64_t in)
return x;
}


///////////////////////////////////////////////////////////////////////////////////

// CRC POLYNOME = x8 + x5 + x4 + 1 = 1001 1000 = 0x8C
Expand Down Expand Up @@ -186,3 +187,4 @@ uint64_t crc64(uint8_t *array, uint8_t length, uint64_t polynome, uint64_t start


// -- END OF FILE --

5 changes: 4 additions & 1 deletion CRC16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ uint16_t CRC16::_reverse(uint16_t in)
return x;
}


uint8_t CRC16::_reverse8(uint8_t in)
{
uint8_t x = in;
Expand All @@ -102,4 +103,6 @@ uint8_t CRC16::_reverse8(uint8_t in)
return x;
}

// -- END OF FILE --

// -- END OF FILE --

4 changes: 3 additions & 1 deletion CRC16.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ class CRC16
uint32_t _count;
};

// -- END OF FILE --

// -- END OF FILE --

5 changes: 4 additions & 1 deletion CRC32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ uint32_t CRC32::_reverse(uint32_t in)
return x;
}


uint8_t CRC32::_reverse8(uint8_t in)
{
uint8_t x = in;
Expand All @@ -102,4 +103,6 @@ uint8_t CRC32::_reverse8(uint8_t in)
return x;
}

// -- END OF FILE --

// -- END OF FILE --

4 changes: 3 additions & 1 deletion CRC32.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ class CRC32
uint32_t _count;
};

// -- END OF FILE --

// -- END OF FILE --

5 changes: 4 additions & 1 deletion CRC64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ uint64_t CRC64::_reverse(uint64_t in)
return x;
}


uint8_t CRC64::_reverse8(uint8_t in)
{
uint8_t x = in;
Expand All @@ -103,4 +104,6 @@ uint8_t CRC64::_reverse8(uint8_t in)
return x;
}

// -- END OF FILE --

// -- END OF FILE --

4 changes: 3 additions & 1 deletion CRC64.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ class CRC64
uint64_t _count;
};

// -- END OF FILE --

// -- END OF FILE --

4 changes: 3 additions & 1 deletion CRC8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,6 @@ uint8_t CRC8::_reverse(uint8_t in)
return x;
}

// -- END OF FILE --

// -- END OF FILE --

4 changes: 3 additions & 1 deletion CRC8.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ class CRC8
uint32_t _count;
};

// -- END OF FILE --

// -- END OF FILE --

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
60 changes: 38 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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 changes: 5 additions & 0 deletions examples/CRC16_test/CRC16_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
// DATE: 2021-01-20
// (c) : MIT


#include "CRC16.h"
#include "CRC.h"

char str[24] = "123456789";

CRC16 crc;


void setup()
{
Serial.begin(115200);
Expand All @@ -27,6 +29,7 @@ void loop()
{
}


void test()
{
Serial.println(crc16((uint8_t *) str, 9, 0x1021, 0, 0, false, false), HEX);
Expand Down Expand Up @@ -59,4 +62,6 @@ void test()
Serial.println(crc.count());
}


// -- END OF FILE --

5 changes: 5 additions & 0 deletions examples/CRC32_test/CRC32_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
// DATE: 2021-01-20
// (c) : MIT


#include "CRC32.h"

char str[24] = "123456789";

CRC32 crc;


void setup()
{
Serial.begin(115200);
Expand All @@ -26,6 +28,7 @@ void loop()
{
}


void test()
{
crc.setPolynome(0x04C11DB7);
Expand Down Expand Up @@ -54,4 +57,6 @@ void test()
Serial.println(crc.count());
}


// -- END OF FILE --

5 changes: 5 additions & 0 deletions examples/CRC64_test/CRC64_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
// DATE: 2021-01-20
// (c) : MIT


#include "CRC64.h"
#include "printHelpers.h" // https://github.com/RobTillaart/printHelpers

char str[24] = "123456789";

CRC64 crc;


void setup()
{
Serial.begin(115200);
Expand All @@ -27,6 +29,7 @@ void loop()
{
}


void test()
{
crc.setPolynome(0x07);
Expand Down Expand Up @@ -55,4 +58,6 @@ void test()
Serial.println(print64(crc.count()));
}


// -- END OF FILE --

5 changes: 4 additions & 1 deletion examples/CRC8_test/CRC8_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@


#include "CRC8.h"

#include "CRC.h"


char str[24] = "123456789";

CRC8 crc;


void setup()
{
Serial.begin(115200);
Expand All @@ -30,6 +30,7 @@ void loop()
{
}


void test()
{
Serial.println(crc8((uint8_t *)str, 9, 0x07), HEX);
Expand Down Expand Up @@ -60,4 +61,6 @@ void test()
Serial.println(crc.count());
}


// -- END OF FILE --

Loading

0 comments on commit 029918d

Please sign in to comment.