mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.3.3 CRC
This commit is contained in:
parent
c2af029108
commit
f9d02b6c7b
@ -6,7 +6,7 @@ jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- uses: arduino/arduino-lint-action@v1
|
||||
with:
|
||||
library-manager: update
|
||||
|
@ -8,7 +8,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: 2.6
|
||||
|
@ -10,7 +10,7 @@ jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- name: json-syntax-check
|
||||
uses: limitusus/json-syntax-check@v1
|
||||
with:
|
||||
|
@ -1,13 +1,23 @@
|
||||
# Change Log Complex
|
||||
|
||||
# Release Notes
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
|
||||
## 0.3.2 - 2022-10-30
|
||||
## [0.3.3] - 2023-01-17
|
||||
- update GitHub actions
|
||||
- update license
|
||||
- renamed releaseNotes.md to CHANGELOG.md (in line with other libs)
|
||||
- update readme.md
|
||||
|
||||
|
||||
## [0.3.2] - 2022-10-30
|
||||
- add RP2040 to build-CI
|
||||
- fix version numbers
|
||||
|
||||
|
||||
## 0.3.1 - 2022-05-20
|
||||
## [0.3.1] - 2022-05-20
|
||||
- added constructors with all parameters.
|
||||
CRC16(uint16_t polynome, uint16_t XORstart, uint16_t XORend, bool reverseIn, bool reverseOut);
|
||||
All five parameters are mandatory, no defaults in this first release.
|
||||
@ -15,19 +25,19 @@
|
||||
- remove #ifndef Header guards as these are not needed.
|
||||
- update documentation
|
||||
|
||||
## 0.3.0 - 2022-04-15
|
||||
## [0.3.0] - 2022-04-15
|
||||
- split CRC.h in CRC.h and CRC.cpp to fix #21 (again)
|
||||
- remove #ifndef Header guards as these are not needed.
|
||||
|
||||
----
|
||||
|
||||
## 0.2.3 - 2022-04-13
|
||||
## [0.2.3] - 2022-04-13
|
||||
- replace #pragma once with #ifndef Header guards #21
|
||||
|
||||
## 0.2.2
|
||||
## [0.2.2]
|
||||
- fix #19 enable/disable yield call
|
||||
|
||||
## 0.2.1
|
||||
## [0.2.1]
|
||||
- fix #17 yield() count in **add(array, length)**
|
||||
(kudo's to dlsloan)
|
||||
- added defaults for CRC64 static function.
|
||||
@ -36,7 +46,7 @@
|
||||
- added conditional yield().
|
||||
- added CRC_polynomes.h
|
||||
|
||||
## 0.2.0
|
||||
## [0.2.0]
|
||||
- added getters for parameters
|
||||
- made yield conditional in **add(array, length)**
|
||||
- improved examples
|
||||
@ -44,11 +54,11 @@
|
||||
|
||||
----
|
||||
|
||||
## 0.1.6
|
||||
## [0.1.6]
|
||||
- add CRC12 function
|
||||
- add CRC12 class
|
||||
|
||||
## 0.1.5
|
||||
## [0.1.5]
|
||||
- TODO (just as older versions)
|
||||
|
||||
That's all folks!
|
@ -1,15 +1,18 @@
|
||||
//
|
||||
// FILE: CRC.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.3.2
|
||||
// VERSION: 0.3.3
|
||||
// PURPOSE: Arduino library for CRC8, CRC12, CRC16, CRC16-CCITT, CRC32, CRC64
|
||||
// URL: https://github.com/RobTillaart/CRC
|
||||
//
|
||||
|
||||
|
||||
#include "CRC.h"
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// fast reverse from bitHelper library
|
||||
//
|
||||
uint8_t reverse8(uint8_t in)
|
||||
{
|
||||
uint8_t x = in;
|
||||
@ -63,8 +66,11 @@ uint64_t reverse64(uint64_t in)
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// static functions for CRC
|
||||
//
|
||||
|
||||
// CRC POLYNOME = x8 + x5 + x4 + 1 = 1001 1000 = 0x8C
|
||||
// CRC POLYNOME = x8 + x5 + x4 + 1 = 1001 1000 = 0x8C
|
||||
uint8_t crc8(const uint8_t *array, uint16_t length, const uint8_t polynome,
|
||||
const uint8_t startmask, const uint8_t endmask,
|
||||
const bool reverseIn, const bool reverseOut)
|
||||
@ -95,7 +101,7 @@ uint8_t crc8(const uint8_t *array, uint16_t length, const uint8_t polynome,
|
||||
}
|
||||
|
||||
|
||||
// CRC POLYNOME = x12 + x3 + x2 + 1 = 0000 1000 0000 1101 = 0x80D
|
||||
// CRC POLYNOME = x12 + x3 + x2 + 1 = 0000 1000 0000 1101 = 0x80D
|
||||
uint16_t crc12(const uint8_t *array, uint16_t length, const uint16_t polynome,
|
||||
const uint16_t startmask, const uint16_t endmask,
|
||||
const bool reverseIn, const bool reverseOut)
|
||||
@ -128,7 +134,7 @@ uint16_t crc12(const uint8_t *array, uint16_t length, const uint16_t polynome,
|
||||
}
|
||||
|
||||
|
||||
// CRC POLYNOME = x15 + 1 = 1000 0000 0000 0001 = 0x8001
|
||||
// CRC POLYNOME = x15 + 1 = 1000 0000 0000 0001 = 0x8001
|
||||
uint16_t crc16(const uint8_t *array, uint16_t length, const uint16_t polynome,
|
||||
const uint16_t startmask, const uint16_t endmask,
|
||||
const bool reverseIn, const bool reverseOut)
|
||||
@ -159,14 +165,14 @@ uint16_t crc16(const uint8_t *array, uint16_t length, const uint16_t polynome,
|
||||
}
|
||||
|
||||
|
||||
// CRC-CCITT POLYNOME = x13 + X5 + 1 = 0001 0000 0010 0001 = 0x1021
|
||||
// CRC-CCITT POLYNOME = x13 + X5 + 1 = 0001 0000 0010 0001 = 0x1021
|
||||
uint16_t crc16_CCITT(uint8_t *array, uint16_t length)
|
||||
{
|
||||
return crc16(array, length, 0x1021, 0xFFFF);
|
||||
}
|
||||
|
||||
|
||||
// CRC-32 POLYNOME = x32 + ..... + 1
|
||||
// CRC-32 POLYNOME = x32 + ..... + 1
|
||||
uint32_t crc32(const uint8_t *array, uint16_t length, const uint32_t polynome,
|
||||
const uint32_t startmask, const uint32_t endmask,
|
||||
const bool reverseIn, const bool reverseOut)
|
||||
@ -197,8 +203,8 @@ uint32_t crc32(const uint8_t *array, uint16_t length, const uint32_t polynome,
|
||||
}
|
||||
|
||||
|
||||
// CRC-CCITT POLYNOME = x64 + ..... + 1
|
||||
// CRC_ECMA64 = 0x42F0E1EBA9EA3693
|
||||
// CRC-CCITT POLYNOME = x64 + ..... + 1
|
||||
// CRC_ECMA64 = 0x42F0E1EBA9EA3693
|
||||
uint64_t crc64(const uint8_t *array, uint16_t length, const uint64_t polynome,
|
||||
const uint64_t startmask, const uint64_t endmask,
|
||||
const bool reverseIn, const bool reverseOut)
|
||||
@ -229,5 +235,5 @@ uint64_t crc64(const uint8_t *array, uint16_t length, const uint64_t polynome,
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE
|
||||
// -- END OF FILE
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
//
|
||||
// FILE: CRC.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.3.2
|
||||
// VERSION: 0.3.3
|
||||
// PURPOSE: Arduino library for CRC8, CRC12, CRC16, CRC16-CCITT, CRC32, CRC64
|
||||
// URL: https://github.com/RobTillaart/CRC
|
||||
//
|
||||
@ -12,12 +12,12 @@
|
||||
|
||||
#include "CRC_polynomes.h"
|
||||
|
||||
#define CRC_LIB_VERSION (F("0.3.2"))
|
||||
#define CRC_LIB_VERSION (F("0.3.3"))
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// fast reverse from bitHelper library
|
||||
// fast reverse from bitHelper library
|
||||
//
|
||||
uint8_t reverse8(uint8_t in);
|
||||
|
||||
@ -29,27 +29,32 @@ uint32_t reverse32(uint32_t in);
|
||||
|
||||
uint64_t reverse64(uint64_t in);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// CRC POLYNOME = x8 + x5 + x4 + 1 = 1001 1000 = 0x8C
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// static functions for CRC
|
||||
//
|
||||
|
||||
// CRC POLYNOME = x8 + x5 + x4 + 1 = 1001 1000 = 0x8C
|
||||
uint8_t crc8(const uint8_t *array, uint16_t length, const uint8_t polynome = 0xD5, const uint8_t startmask = 0x00, const uint8_t endmask = 0x00, const bool reverseIn = false, const bool reverseOut = false);
|
||||
|
||||
// CRC POLYNOME = x12 + x3 + x2 + 1 = 0000 1000 0000 1101 = 0x80D
|
||||
// CRC POLYNOME = x12 + x3 + x2 + 1 = 0000 1000 0000 1101 = 0x80D
|
||||
uint16_t crc12(const uint8_t *array, uint16_t length, const uint16_t polynome = 0x80D, const uint16_t startmask = 0x0000, const uint16_t endmask = 0x0000, const bool reverseIn = false, const bool reverseOut = false);
|
||||
|
||||
// CRC POLYNOME = x15 + 1 = 1000 0000 0000 0001 = 0x8001
|
||||
// CRC POLYNOME = x15 + 1 = 1000 0000 0000 0001 = 0x8001
|
||||
uint16_t crc16(const uint8_t *array, uint16_t length, const uint16_t polynome = 0x8001, const uint16_t startmask = 0x0000, const uint16_t endmask = 0x0000, const bool reverseIn = false, const bool reverseOut = false);
|
||||
|
||||
// CRC-CCITT POLYNOME = x13 + X5 + 1 = 0001 0000 0010 0001 = 0x1021
|
||||
// CRC-CCITT POLYNOME = x13 + X5 + 1 = 0001 0000 0010 0001 = 0x1021
|
||||
uint16_t crc16_CCITT(uint8_t *array, uint16_t length);
|
||||
|
||||
// CRC-32 POLYNOME = x32 + ..... + 1
|
||||
// CRC-32 POLYNOME = x32 + ..... + 1
|
||||
uint32_t crc32(const uint8_t *array, uint16_t length, const uint32_t polynome = 0x04C11DB7, const uint32_t startmask = 0, const uint32_t endmask = 0, const bool reverseIn = false, const bool reverseOut = false);
|
||||
|
||||
// CRC-CCITT POLYNOME = x64 + ..... + 1
|
||||
// CRC_ECMA64 = 0x42F0E1EBA9EA3693
|
||||
// CRC-CCITT POLYNOME = x64 + ..... + 1
|
||||
// CRC_ECMA64 = 0x42F0E1EBA9EA3693
|
||||
uint64_t crc64(const uint8_t *array, uint16_t length, const uint64_t polynome = 0x42F0E1EBA9EA3693, const uint64_t startmask = 0, const uint64_t endmask = 0, const bool reverseIn = false, const bool reverseOut = false);
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021-2022 Rob Tillaart
|
||||
Copyright (c) 2021-2023 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
|
||||
|
@ -13,39 +13,47 @@ 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
|
||||
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,
|
||||
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
|
||||
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
|
||||
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
|
||||
is UPPER case. So **CRC8** is a class and **crc8()** is the function.
|
||||
is UPPER case. So **CRC8** is a class and **crc8()** is the function.
|
||||
|
||||
Deeper tech info - https://en.wikipedia.org/wiki/Cyclic_redundancy_check
|
||||
and many other websites.
|
||||
|
||||
#### Related
|
||||
|
||||
- https://github.com/RobTillaart/Adler
|
||||
- https://github.com/RobTillaart/CRC
|
||||
- https://github.com/RobTillaart/Fletcher
|
||||
- https://github.com/RobTillaart/LUHN
|
||||
|
||||
|
||||
|
||||
## Interface CRC classes
|
||||
|
||||
The interfaces are very similar for CRC8, CRC12, CRC16, CRC32 and CRC64 class.
|
||||
The only difference is the data type for polynome, start- and end-mask,
|
||||
The only difference is the data type for polynome, start- and end-mask,
|
||||
and the returned CRC.
|
||||
|
||||
|
||||
#### base
|
||||
#### Base
|
||||
|
||||
Use **\#include "CRC8.h"**
|
||||
|
||||
@ -55,14 +63,14 @@ Use **\#include "CRC8.h"**
|
||||
- **void restart()** reset internal CRC and count only;
|
||||
reuse values for other e.g polynome, XOR masks and reverse flags.
|
||||
- **void add(value)** add a single value to CRC calculation.
|
||||
- **void add(array, uint16_t length)** add an array of values to the CRC.
|
||||
- **void add(array, uint16_t length)** add an array of values to the CRC.
|
||||
In case of a warning/error for the array type, use casting to (uint8_t \*).
|
||||
- **uint8_t getCRC()** returns CRC calculated so far. This allows to check the CRC of
|
||||
- **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.
|
||||
|
||||
|
||||
#### parameters
|
||||
#### Parameters
|
||||
|
||||
The parameters do not have defaults so the user must set them explicitly.
|
||||
|
||||
@ -78,29 +86,29 @@ The parameters do not have defaults so the user must set them explicitly.
|
||||
- **bool getReverseOut()** return parameter set above or default.
|
||||
|
||||
|
||||
#### power users only
|
||||
#### Power users only
|
||||
|
||||
As CRC calculations of large blocks can take serious time (in milliseconds),
|
||||
the classes call **yield()** after every 256 **add()** calls to keep RTOS
|
||||
environments happy.
|
||||
environments happy.
|
||||
|
||||
The following two calls allows one to enable and disable these calls to
|
||||
The following two calls allows one to enable and disable these calls to
|
||||
**yield()** to get optimal performance. The risk is missing context switching
|
||||
to handle interrupts etc. So use at own risk.
|
||||
|
||||
- **void enableYield()** enables the calls to **yield()**.
|
||||
- **void disableYield()** disables the calls to **yield()**.
|
||||
|
||||
_Note: the static functions in this library also call **yield()** but this
|
||||
_Note: the static functions in this library also call **yield()** but this
|
||||
cannot be disabled (for now)._
|
||||
|
||||
_Note: a parameter could be a future option to set the number of adds before
|
||||
_Note: a parameter could be a future option to set the number of adds before
|
||||
**yield()** is called. **setYield(0)** would be disable it._
|
||||
|
||||
|
||||
### Example snippet
|
||||
|
||||
A minimal usage only needs:
|
||||
A minimal usage only needs:
|
||||
- the constructor, the add() function and the getCRC() function.
|
||||
|
||||
```cpp
|
||||
@ -123,9 +131,9 @@ CRC32 crc;
|
||||
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.
|
||||
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 crc12(array, length, polynome = 0x080D, start = 0, end = 0, reverseIn = false, reverseOut = false)** idem with default polynome.
|
||||
@ -134,11 +142,11 @@ For flexibility both parameters are kept available.
|
||||
- **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 = 0x42F0E1EBA9EA3693, start = 0, end = 0, reverseIn = false, reverseOut = false)** - experimental version, no reference found except on Wikipedia.
|
||||
|
||||
Note these functions are limited to one call per block of data.
|
||||
Note these functions are limited to one call per block of data.
|
||||
These functions will call **yield()** every 256 bytes to keep RTOS happy.
|
||||
For more flexibility use the specific classes.
|
||||
|
||||
The static CRC functions use fast reverse functions that can be also be
|
||||
The static CRC functions use fast reverse functions that can be also be
|
||||
used outside CRC context. Their usage is straightforward.
|
||||
|
||||
- **uint8_t reverse8(uint8_t in)** idem.
|
||||
@ -163,7 +171,7 @@ If standard polynomes are missing, please open an issue and report, with referen
|
||||
See examples.
|
||||
|
||||
|
||||
## Links
|
||||
## Links
|
||||
|
||||
- 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.)
|
||||
@ -173,14 +181,24 @@ See examples.
|
||||
|
||||
## Future
|
||||
|
||||
- extend examples.
|
||||
#### Must
|
||||
|
||||
|
||||
#### Should
|
||||
|
||||
- add examples.
|
||||
- example showing multiple packages of data linked by their CRC.
|
||||
sort of "blockchain"
|
||||
|
||||
|
||||
#### Could
|
||||
|
||||
- table versions for performance? (performance - memory discussion).
|
||||
- stream version - 4 classes class?
|
||||
- **setCRC(value)** to be able to pick up where one left ?
|
||||
- can be done with **setStartXOR()**
|
||||
- needs **getRawCRC()** without reverse and end mask
|
||||
- Think about default parameters for constructor **CRC8(polynome, XORstart, XORend, reverseIn, reverseOut)**
|
||||
- Think about default parameters for constructor **CRC8(polynome, XORstart, XORend, reverseIn, reverseOut)**
|
||||
- same as reset so constructors merge? Note the CRC-functions do have defaults too.
|
||||
|
||||
|
||||
@ -188,9 +206,9 @@ See examples.
|
||||
|
||||
- **CRC1()** // parity :)
|
||||
- **CRC4(array, length, polynome, start, end, reverseIn, reverseOut)** nibbles?
|
||||
- default polynome 0x03
|
||||
- default polynome 0x03 ITU
|
||||
- One CRC() with #bits as parameter?
|
||||
- up to 64 bit for all missing ones.?
|
||||
- up to 64 bit for all missing ones?
|
||||
- performance penalty
|
||||
- One CRC() template class?
|
||||
|
||||
@ -200,4 +218,4 @@ See examples.
|
||||
- add a dump(Stream = Serial) to see all the settings at once.
|
||||
user can access parameters, so no need.
|
||||
|
||||
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/CRC"
|
||||
},
|
||||
"version": "0.3.2",
|
||||
"version": "0.3.3",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=CRC
|
||||
version=0.3.2
|
||||
version=0.3.3
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library for CRC for Arduino
|
||||
|
Loading…
Reference in New Issue
Block a user