MeshProtocolSimulator  1.0.0
HardwareCRC.h
Go to the documentation of this file.
1 
15 #ifndef HARDWARECRC_H_
16 #define HARDWARECRC_H_
17 
18 #ifndef SIMULATION
19 #include "wirish.h"
20 #else
21 #include "Simulation.h"
22 #endif
23 
24 
33 #define CRC_X0 (0x01UL)
34 #define CRC_X1 (0x01UL << 1U)
35 #define CRC_X2 (0x01UL << 2U)
36 #define CRC_X3 (0x01UL << 3U)
37 #define CRC_X4 (0x01UL << 4U)
38 #define CRC_X5 (0x01UL << 5U)
39 #define CRC_X6 (0x01UL << 6U)
40 #define CRC_X7 (0x01UL << 7U)
41 #define CRC_X8 (0x01UL << 8U)
42 #define CRC_X9 (0x01UL << 9U)
43 #define CRC_X10 (0x01UL << 10U)
44 #define CRC_X11 (0x01UL << 11U)
45 #define CRC_X12 (0x01UL << 12U)
46 #define CRC_X13 (0x01UL << 13U)
47 #define CRC_X14 (0x01UL << 14U)
48 #define CRC_X15 (0x01UL << 15U)
49 #define CRC_X16 (0x01UL << 16U)
50 #define CRC_X17 (0x01UL << 17U)
51 #define CRC_X18 (0x01UL << 18U)
52 #define CRC_X19 (0x01UL << 19U)
53 #define CRC_X20 (0x01UL << 20U)
54 #define CRC_X21 (0x01UL << 21U)
55 #define CRC_X22 (0x01UL << 22U)
56 #define CRC_X23 (0x01UL << 23U)
57 #define CRC_X24 (0x01UL << 24U)
58 #define CRC_X25 (0x01UL << 25U)
59 #define CRC_X26 (0x01UL << 26U)
60 #define CRC_X27 (0x01UL << 27U)
61 #define CRC_X28 (0x01UL << 28U)
62 #define CRC_X29 (0x01UL << 29U)
63 #define CRC_X30 (0x01UL << 30U)
64 #define CRC_X31 (0x01UL << 31U)
65 
73 //Mostly used polynomials
74 #define CRC32_POLY (0x04C11DB7UL) /* X^32 + X^26 + X^23 + X^22 + X^16 + X^12 + X^11 + X^10 +X^8 + X^7 + X^5 + X^4 + X^2+ X +1 */
75 #define CRC16_POLY (0x8005UL) /* X^16 + X^15 + X^2 + 1 - IBM CRC polynomial*/
76 #define CRC8_POLY (0x31UL) /* X^8 + X^5 + X^4 + 1 - I2C CRC polynomial*/
77 #define CRC7_POLY (0x09UL) /* X^7 + X^3 + 1 */
78 
83 #define DEFAULT_HCRC_INITVAL (0xFFFFFFFFUL) //Default initial value
84 
85 //Polynomial size CR register mask
86 #define HCRC_POLYSIZE_MASK (0x18) //B00011000
87 //Polynomial size values
88 #define HCRC_32BIT_POLYSIZE (0x00)
89 #define HCRC_16BIT_POLYSIZE (0x08)
90 #define HCRC_8BIT_POLYSIZE (0x10)
91 #define HCRC_7BIT_POLYSIZE (HCRC_POLYSIZE_MASK)
92 
93 //Output reverse CR register mask
94 #define HCRC_OUT_REVERSE_MASK (0x80) //B10000000
95 //Output reverse values
96 #define HCRC_OUT_REVERSE_ENABLED HCRC_OUT_REVERSE_MASK
97 #define HCRC_OUT_REVERSE_DISABLED (0x0)
98 
99 //Input reverse CR register mask
100 #define HCRC_IN_REVERSE_MASK (0x60) //B01100000
101 //Input reverse values
102 #define HCRC_IN_REVERSE_ENABLED_BYTE (0x20)
103 #define HCRC_IN_REVERSE_ENABLED_HALFWORD (0x40)
104 #define HCRC_IN_REVERSE_ENABLED_WORD HCRC_IN_REVERSE_MASK
105 #define HCRC_IN_REVERSE_DISABLED (0x0)
106 
107 
108 
133 public:
134 
139  bool begin() {
140  return true;
141  }
142 
149  bool setPolynomial7(uint32_t polynomial = CRC7_POLY) {
150  return true;
151  }
152 
159  bool setPolynomial8(uint32_t polynomial = CRC8_POLY) {
160  return true;
161  }
162 
169  bool setPolynomial16(uint32_t polynomial = CRC16_POLY) {
170  return true;
171  }
172 
179  bool setPolynomial32(uint32_t polynomial = CRC32_POLY) {
180  return true;
181  }
182 
187  inline uint32_t getPolynomial(){
188  return CRC32_POLY;
189  }
190 
196  return 32;
197  }
198 
203  inline uint32_t getInitValue(){
204  return 0xFFFFFFFFUL;
205  }
206 
212  inline uint32_t getLastCRC(uint32_t finalXOR = 0x00){
213  return 0;
214  }
215 
220  void reset(uint32_t initValue = DEFAULT_HCRC_INITVAL) {
221  }
222 
230  uint32_t append(const uint8_t data, uint32_t finalXOR = 0x00) {
231  return 0;
232  }
233 
242  uint32_t append(const uint8_t* data, uint32_t dataLength, uint32_t finalXOR = 0x00) {
243  return 0;
244  }
245 
254  uint32_t append(const int8_t* data, uint32_t dataLength, uint32_t finalXOR = 0x00){
255  return append((const uint8_t*)data, dataLength, finalXOR);
256  }
257 
265  uint32_t append(const uint16_t data, uint32_t finalXOR = 0x00) {
266  return 0;
267  }
268 
277  uint32_t append(const uint16_t* data, uint32_t dataLength, uint32_t finalXOR = 0x00) {
278  return 0;
279  }
280 
289  uint32_t append(const int16_t* data, uint32_t dataLength, uint32_t finalXOR = 0x00){
290  return append((const uint16_t*)data, dataLength, finalXOR);
291  }
292 
300  uint32_t append(const uint32_t data, uint32_t finalXOR = 0x00) {
301  return 0;
302  }
303 
312  uint32_t append(const uint32_t* data, uint32_t dataLength, uint32_t finalXOR = 0x00) {
313  return 0;
314  }
315 
324  uint32_t append (const int32_t* data, uint32_t dataLength, uint32_t finalXOR = 0x00){
325  return append((const uint32_t*)data, dataLength, finalXOR);
326  }
327 
337  uint32_t compute(const uint8_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00) {
338  return 0;
339  }
340 
350  uint32_t compute(const int8_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
351  return compute((const uint8_t*)data, dataLength, initValue, finalXOR);
352  }
353 
363  uint32_t compute(const uint16_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00) {
364  return 0;
365  }
366 
376  uint32_t compute(const int16_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
377  return compute((const uint16_t*)data, dataLength, initValue, finalXOR);
378  }
379 
389  uint32_t compute(const uint32_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00) {
390  return 0;
391  }
392 
402  uint32_t compute(const int32_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
403  return compute((const uint32_t*)data, dataLength, initValue, finalXOR);
404  }
405 
415  uint32_t computeAndWriteToEnd(uint8_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00) {
416  return 0;
417  }
418 
428  uint32_t computeAndWriteToEnd(int8_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
429  return computeAndWriteToEnd((uint8_t*)data, dataLength, initValue, finalXOR);
430  }
431 
441  uint32_t computeAndWriteToEnd(uint16_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00) {
442  return 0;
443  }
444 
454  uint32_t computeAndWriteToEnd(int16_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
455  return computeAndWriteToEnd((uint16_t*)data, dataLength, initValue, finalXOR);
456  }
457 
467  uint32_t computeAndWriteToEnd(uint32_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00) {
468  return 0;
469  }
470 
480  uint32_t computeAndWriteToEnd(int32_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
481  return computeAndWriteToEnd((uint32_t*)data, dataLength, initValue, finalXOR);
482  }
483 
493  bool check(const uint8_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00) {
494  return true;
495  }
496 
506  bool check(const int8_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
507  return check((const uint8_t*)data, dataLength, initValue, finalXOR);
508  }
509 
519  bool check(const uint16_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00) {
520  return true;
521  }
522 
532  bool check(const int16_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
533  return check((const uint16_t*)data, dataLength, initValue, finalXOR);
534  }
535 
545  bool check(const uint32_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00) {
546  return true;
547  }
548 
558  bool check(const int32_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
559  return check((const uint32_t*)data, dataLength, initValue, finalXOR);
560  }
561 
568  uint32_t getCheckValue(uint32_t initValue = DEFAULT_HCRC_INITVAL,uint32_t finalXOR = 0x00) {
569  return 0;
570  }
571 
576  bool setInputReverse(bool reverse) {
577  return true;
578  }
579 
585  bool setOutputReverse(bool reverse) {
586  return true;
587  }
588 
593  inline bool getInputReverse(){
594 #if CRC_IO_REV_SUPP == (0x1)
595  return (inputReverse & HCRC_IN_REVERSE_MASK) != HCRC_IN_REVERSE_DISABLED;
596 #else
597  return true; //Input is reversed by hardware
598 #endif
599  }
600 
605  inline bool getOutputReverse(){
606 #if CRC_IO_REV_SUPP == (0x1)
607  return (CRC->CR & HCRC_OUT_REVERSE_MASK) == HCRC_OUT_REVERSE_ENABLED;
608 #else
609  return true; //Output is reversed by hardware
610 #endif
611  }
612 
616  void end() {
617  }
618 
619  /*CRCSettings exportSettings();
620 
621  void importSettings(CRCSettings settings);*/
622 
627  static const bool isCustomPolynomialSupported = 0;
628 
632  static const bool isIOReverseSupported = 0;
633 
637  static const bool isCustomInitValueSupported = 0;
638 
639 protected:
640 
641  //Global values used to check, if setting has changed in getCheckValue() method.
642  uint8_t lastCR = 0xFF;
643 #if CRC_CUST_POLY == (0x1)
644  uint32_t lastPoly = 0;
645 #endif
646 #if CRC_CUST_INIT_VAL == (0x1)
647  uint32_t lastInit = 0;
648 #endif
649  uint32_t lastFXOR = 0;
650  uint32_t lastCRCCheckValue = 0;
651 
652 /*#if CRC_CUST_INIT_VAL == (0x1)
653  uint32_t InitValue = 0xFFFFFFFF;
654 #endif*/
655 
660  void setInputReverseSize(uint32_t reverse) {
661  }
662 
663  uint8_t inputReverse = HCRC_IN_REVERSE_DISABLED;
664 };
665 
666 extern HardwareCRC crc;
667 
668 #endif /* HARDWARECRC_H_ */
HardwareCRC::end
void end()
Disables hardware CRC calculation unit.
Definition: HardwareCRC.h:616
HardwareCRC::check
bool check(const int16_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Checks if the array (with CRC attached at the end) is valid. reset() method is called at the method b...
Definition: HardwareCRC.h:532
HardwareCRC::getPolynomial
uint32_t getPolynomial()
Gets polynomial, that was set.
Definition: HardwareCRC.h:187
HardwareCRC::append
uint32_t append(const uint16_t data, uint32_t finalXOR=0x00)
Appends another data to previous CRC calculation and calculates new CRC value.
Definition: HardwareCRC.h:265
HardwareCRC::compute
uint32_t compute(const int32_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets and computes CRC value of the whole array. reset() method is called at the method begin.
Definition: HardwareCRC.h:402
HardwareCRC::getCheckValue
uint32_t getCheckValue(uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Calculates CRC check value. When settings were not changed, last check value is returned....
Definition: HardwareCRC.h:568
HardwareCRC::computeAndWriteToEnd
uint32_t computeAndWriteToEnd(int32_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets, computes CRC value of the whole array and attaches calculated CRC value at the end of the arr...
Definition: HardwareCRC.h:480
HardwareCRC::setInputReverseSize
void setInputReverseSize(uint32_t reverse)
Sets input reverse size to CR register.
Definition: HardwareCRC.h:660
HardwareCRC::append
uint32_t append(const uint16_t *data, uint32_t dataLength, uint32_t finalXOR=0x00)
Appends another data to previous CRC calculation and calculates new CRC value.
Definition: HardwareCRC.h:277
HardwareCRC::compute
uint32_t compute(const uint32_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets and computes CRC value of the whole array. reset() method is called at the method begin.
Definition: HardwareCRC.h:389
HardwareCRC::isCustomPolynomialSupported
static const bool isCustomPolynomialSupported
True when custom polynomials are supported. Methods setPolynomial7(), setPolynomial8(),...
Definition: HardwareCRC.h:627
HardwareCRC
Calculate CRC simply and fast using this class. Calculation is done using hardware CRC calculation un...
Definition: HardwareCRC.h:132
HardwareCRC::computeAndWriteToEnd
uint32_t computeAndWriteToEnd(uint16_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets, computes CRC value of the whole array and attaches calculated CRC value at the end of the arr...
Definition: HardwareCRC.h:441
HardwareCRC::setOutputReverse
bool setOutputReverse(bool reverse)
Sets, if output CRC data bits will be reversed(reflected) or not.
Definition: HardwareCRC.h:585
HardwareCRC::reset
void reset(uint32_t initValue=DEFAULT_HCRC_INITVAL)
Resets calculation and sets initial value.
Definition: HardwareCRC.h:220
HardwareCRC::compute
uint32_t compute(const uint8_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets and computes CRC value of the whole array. reset() method is called at the method begin.
Definition: HardwareCRC.h:337
HardwareCRC::setPolynomial32
bool setPolynomial32(uint32_t polynomial=CRC32_POLY)
Sets CRC-32 polynomial.
Definition: HardwareCRC.h:179
HardwareCRC::getPolynomialLength
uint8_t getPolynomialLength()
Gets polynomial length in bits.
Definition: HardwareCRC.h:195
HardwareCRC::getInitValue
uint32_t getInitValue()
Gets initial value, that was set using reset(), compute() or computeAndWriteToEnd() method.
Definition: HardwareCRC.h:203
HardwareCRC::append
uint32_t append(const uint8_t data, uint32_t finalXOR=0x00)
Appends another data to previous CRC calculation and calculates new CRC value.
Definition: HardwareCRC.h:230
HardwareCRC::getLastCRC
uint32_t getLastCRC(uint32_t finalXOR=0x00)
Gets last calculated CRC result.
Definition: HardwareCRC.h:212
HardwareCRC::setPolynomial16
bool setPolynomial16(uint32_t polynomial=CRC16_POLY)
Sets CRC-16 polynomial.
Definition: HardwareCRC.h:169
HardwareCRC::isCustomInitValueSupported
static const bool isCustomInitValueSupported
True when custom initial value is supported, if not, 0xFFFFFFFFUL is set.
Definition: HardwareCRC.h:637
HardwareCRC::append
uint32_t append(const uint32_t data, uint32_t finalXOR=0x00)
Appends another data to previous CRC calculation and calculates new CRC value.
Definition: HardwareCRC.h:300
HardwareCRC::append
uint32_t append(const int32_t *data, uint32_t dataLength, uint32_t finalXOR=0x00)
Appends another data to previous CRC calculation and calculates new CRC value.
Definition: HardwareCRC.h:324
HardwareCRC::isIOReverseSupported
static const bool isIOReverseSupported
True when input / output reversion (reflection) is supported.
Definition: HardwareCRC.h:632
HardwareCRC::compute
uint32_t compute(const uint16_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets and computes CRC value of the whole array. reset() method is called at the method begin.
Definition: HardwareCRC.h:363
HardwareCRC::computeAndWriteToEnd
uint32_t computeAndWriteToEnd(int16_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets, computes CRC value of the whole array and attaches calculated CRC value at the end of the arr...
Definition: HardwareCRC.h:454
HardwareCRC::compute
uint32_t compute(const int16_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets and computes CRC value of the whole array. reset() method is called at the method begin.
Definition: HardwareCRC.h:376
HardwareCRC::computeAndWriteToEnd
uint32_t computeAndWriteToEnd(int8_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets, computes CRC value of the whole array and attaches calculated CRC value at the end of the arr...
Definition: HardwareCRC.h:428
HardwareCRC::append
uint32_t append(const uint32_t *data, uint32_t dataLength, uint32_t finalXOR=0x00)
Appends another data to previous CRC calculation and calculates new CRC value.
Definition: HardwareCRC.h:312
HardwareCRC::getInputReverse
bool getInputReverse()
Gets, if input data bits will be reversed(reflected) or not.
Definition: HardwareCRC.h:593
HardwareCRC::computeAndWriteToEnd
uint32_t computeAndWriteToEnd(uint32_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets, computes CRC value of the whole array and attaches calculated CRC value at the end of the arr...
Definition: HardwareCRC.h:467
HardwareCRC::setPolynomial8
bool setPolynomial8(uint32_t polynomial=CRC8_POLY)
Sets CRC-8 polynomial.
Definition: HardwareCRC.h:159
HardwareCRC::compute
uint32_t compute(const int8_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets and computes CRC value of the whole array. reset() method is called at the method begin.
Definition: HardwareCRC.h:350
HardwareCRC::setInputReverse
bool setInputReverse(bool reverse)
Sets, if input data bits will be reversed(reflected) or not.
Definition: HardwareCRC.h:576
HardwareCRC::check
bool check(const int32_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Checks if the array (with CRC attached at the end) is valid. reset() method is called at the method b...
Definition: HardwareCRC.h:558
HardwareCRC::computeAndWriteToEnd
uint32_t computeAndWriteToEnd(uint8_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Resets, computes CRC value of the whole array and attaches calculated CRC value at the end of the arr...
Definition: HardwareCRC.h:415
HardwareCRC::setPolynomial7
bool setPolynomial7(uint32_t polynomial=CRC7_POLY)
Sets CRC-7 polynomial.
Definition: HardwareCRC.h:149
HardwareCRC::check
bool check(const uint32_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Checks if the array (with CRC attached at the end) is valid. reset() method is called at the method b...
Definition: HardwareCRC.h:545
HardwareCRC::append
uint32_t append(const int8_t *data, uint32_t dataLength, uint32_t finalXOR=0x00)
Appends another data to previous CRC calculation and calculates new CRC value.
Definition: HardwareCRC.h:254
HardwareCRC::append
uint32_t append(const int16_t *data, uint32_t dataLength, uint32_t finalXOR=0x00)
Appends another data to previous CRC calculation and calculates new CRC value.
Definition: HardwareCRC.h:289
HardwareCRC::check
bool check(const uint8_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Checks if the array (with CRC attached at the end) is valid. reset() method is called at the method b...
Definition: HardwareCRC.h:493
HardwareCRC::append
uint32_t append(const uint8_t *data, uint32_t dataLength, uint32_t finalXOR=0x00)
Appends another data to previous CRC calculation and calculates new CRC value.
Definition: HardwareCRC.h:242
HardwareCRC::check
bool check(const int8_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Checks if the array (with CRC attached at the end) is valid. reset() method is called at the method b...
Definition: HardwareCRC.h:506
HardwareCRC::getOutputReverse
bool getOutputReverse()
Gets, if output CRC data bits will be reversed(reflected) or not.
Definition: HardwareCRC.h:605
HardwareCRC::begin
bool begin()
Enables CRC hardware calculation unit.
Definition: HardwareCRC.h:139
HardwareCRC::check
bool check(const uint16_t *data, uint32_t dataLength, uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Checks if the array (with CRC attached at the end) is valid. reset() method is called at the method b...
Definition: HardwareCRC.h:519