-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CRC classes CRC8, CRC16, CRC32, CRC64 (#3)
* version 0.1.1
- Loading branch information
1 parent
2dac155
commit 783d437
Showing
21 changed files
with
1,040 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// FILE: CRC16.cpp | ||
// AUTHOR: Rob Tillaart | ||
// PURPOSE: Arduino class for CRC16 | ||
// URL: https://github.com/RobTillaart/CRC | ||
|
||
|
||
#include "CRC16.h" | ||
|
||
|
||
CRC16::CRC16() | ||
{ | ||
reset(); | ||
} | ||
|
||
|
||
void CRC16::reset() | ||
{ | ||
_polynome = CRC16_DEFAULT_POLYNOME; | ||
_startMask = 0; | ||
_endMask = 0; | ||
_crc = 0; | ||
_reverseIn = false; | ||
_reverseOut = false; | ||
_started = false; | ||
_count = 0; | ||
} | ||
|
||
|
||
void CRC16::restart() | ||
{ | ||
_started = true; | ||
_crc = _startMask; | ||
_count = 0; | ||
} | ||
|
||
|
||
void CRC16::add(uint8_t value) | ||
{ | ||
_count++; | ||
_update(value); | ||
} | ||
|
||
|
||
void CRC16::add(uint8_t * array, uint32_t length) | ||
{ | ||
_count += length; | ||
while (length--) | ||
{ | ||
yield(); | ||
uint8_t data = *array++; | ||
_update(data); | ||
} | ||
} | ||
|
||
|
||
uint16_t CRC16::getCRC() | ||
{ | ||
uint16_t rv = _crc; | ||
if (_reverseOut) rv = _reverse(rv); | ||
rv ^= _endMask; | ||
return rv; | ||
} | ||
|
||
|
||
void CRC16::_update(uint8_t value) | ||
{ | ||
if (!_started) restart(); | ||
if (_reverseIn) value = _reverse(value); | ||
_crc ^= ((uint16_t)value) << 8;; | ||
for (uint8_t i = 8; i; i--) | ||
{ | ||
if (_crc & (1UL << 15)) | ||
{ | ||
_crc <<= 1; | ||
_crc ^= _polynome; | ||
} | ||
else | ||
{ | ||
_crc <<= 1; | ||
} | ||
} | ||
} | ||
|
||
|
||
uint16_t CRC16::_reverse(uint16_t in) | ||
{ | ||
uint16_t x = in; | ||
x = (((x & 0XAAAA) >> 1) | ((x & 0X5555) << 1)); | ||
x = (((x & 0xCCCC) >> 2) | ((x & 0X3333) << 2)); | ||
x = (((x & 0xF0F0) >> 4) | ((x & 0X0F0F) << 4)); | ||
x = (( x >> 8) | (x << 8)); | ||
return x; | ||
} | ||
|
||
// -- END OF FILE -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
// | ||
// FILE: CRC16.h | ||
// AUTHOR: Rob Tillaart | ||
// PURPOSE: Arduino class for CRC16 | ||
// URL: https://github.com/RobTillaart/CRC | ||
|
||
|
||
#include "Arduino.h" | ||
|
||
#define CRC16_DEFAULT_POLYNOME 0x1021 | ||
|
||
|
||
class CRC16 | ||
{ | ||
public: | ||
CRC16(); | ||
|
||
// set parameters to default | ||
void reset(); // set all to constructor defaults | ||
void restart(); // reset crc with same parameters. | ||
|
||
// set parameters | ||
void setPolynome(uint16_t polynome) { _polynome = polynome; }; | ||
void setStartXOR(uint16_t start) { _startMask = start; }; | ||
void setEndXOR(uint16_t end) { _endMask = end; }; | ||
void setReverseIn(bool reverseIn) { _reverseIn = reverseIn; }; | ||
void setReverseOut(bool reverseOut) { _reverseOut = reverseOut; }; | ||
|
||
void add(uint8_t value); | ||
void add(uint8_t * array, uint32_t length); | ||
|
||
uint16_t getCRC(); // returns CRC | ||
uint32_t count() { return _count; }; | ||
|
||
private: | ||
uint16_t _reverse(uint16_t value); | ||
void _update(uint8_t value); | ||
|
||
uint16_t _polynome; | ||
uint16_t _startMask; | ||
uint16_t _endMask; | ||
uint16_t _crc; | ||
bool _reverseIn; | ||
bool _reverseOut; | ||
bool _started; | ||
uint32_t _count; | ||
}; | ||
|
||
// -- END OF FILE -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// FILE: CRC32.cpp | ||
// AUTHOR: Rob Tillaart | ||
// PURPOSE: Arduino class for CRC32; | ||
// URL: https://github.com/RobTillaart/CRC | ||
|
||
|
||
#include "CRC32.h" | ||
|
||
|
||
CRC32::CRC32() | ||
{ | ||
reset(); | ||
} | ||
|
||
|
||
void CRC32::reset() | ||
{ | ||
_polynome = CRC32_DEFAULT_POLYNOME; | ||
_startMask = 0; | ||
_endMask = 0; | ||
_crc = 0; | ||
_reverseIn = false; | ||
_reverseOut = false; | ||
_started = false; | ||
_count = 0; | ||
} | ||
|
||
|
||
void CRC32::restart() | ||
{ | ||
_started = true; | ||
_crc = _startMask; | ||
_count = 0; | ||
} | ||
|
||
|
||
void CRC32::add(uint8_t value) | ||
{ | ||
_count++; | ||
_update(value); | ||
} | ||
|
||
|
||
void CRC32::add(uint8_t * array, uint32_t length) | ||
{ | ||
_count += length; | ||
while (length--) | ||
{ | ||
yield(); | ||
_update(*array++); | ||
} | ||
} | ||
|
||
|
||
uint32_t CRC32::getCRC() | ||
{ | ||
uint32_t rv = _crc; | ||
if (_reverseOut) rv = _reverse(rv); | ||
rv ^=_endMask; | ||
return rv; | ||
} | ||
|
||
|
||
void CRC32::_update(uint8_t value) | ||
{ | ||
if (!_started) restart(); | ||
if (_reverseIn) value = _reverse(value); | ||
_crc ^= ((uint32_t)value) << 24;; | ||
for (uint8_t i = 8; i; i--) | ||
{ | ||
if (_crc & (1UL << 31)) | ||
{ | ||
_crc <<= 1; | ||
_crc ^= _polynome; | ||
} | ||
else | ||
{ | ||
_crc <<= 1; | ||
} | ||
} | ||
} | ||
|
||
|
||
uint32_t CRC32::_reverse(uint32_t in) | ||
{ | ||
uint32_t x = in; | ||
x = (((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1)); | ||
x = (((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2)); | ||
x = (((x & 0xF0F0F0F0) >> 4) | ((x & 0x0F0F0F0F) << 4)); | ||
x = (((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8)); | ||
x = (x >> 16) | (x << 16); | ||
return x; | ||
} | ||
|
||
// -- END OF FILE -- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
// | ||
// FILE: CRC32.h | ||
// AUTHOR: Rob Tillaart | ||
// PURPOSE: Arduino class for CRC32 | ||
// URL: https://github.com/RobTillaart/CRC | ||
|
||
|
||
#include "Arduino.h" | ||
|
||
#define CRC32_DEFAULT_POLYNOME 0x04C11DB7 | ||
|
||
|
||
class CRC32 | ||
{ | ||
public: | ||
CRC32(); | ||
|
||
// set parameters to default | ||
void reset(); // set all to constructor defaults | ||
void restart(); // reset crc with same parameters. | ||
|
||
// set parameters | ||
void setPolynome(uint32_t polynome) { _polynome = polynome; }; | ||
void setStartXOR(uint32_t start) { _startMask = start; }; | ||
void setEndXOR(uint32_t end) { _endMask = end; }; | ||
void setReverseIn(bool reverseIn) { _reverseIn = reverseIn; }; | ||
void setReverseOut(bool reverseOut) { _reverseOut = reverseOut; }; | ||
|
||
void add(uint8_t value); | ||
void add(uint8_t * array, uint32_t length); | ||
|
||
uint32_t getCRC(); // returns CRC | ||
uint32_t count() { return _count; }; | ||
|
||
private: | ||
uint32_t _reverse(uint32_t value); | ||
void _update(uint8_t value); | ||
|
||
uint32_t _polynome; | ||
uint32_t _startMask; | ||
uint32_t _endMask; | ||
uint32_t _crc; | ||
bool _reverseIn; | ||
bool _reverseOut; | ||
bool _started; | ||
uint32_t _count; | ||
}; | ||
|
||
// -- END OF FILE -- |
Oops, something went wrong.