Arduino Core for STM32  1.0
HardwareCRC.h
Go to the documentation of this file.
1 
15 #ifndef HARDWARECRC_H_
16 #define HARDWARECRC_H_
17 
18 #include "wirish.h"
19 
20 
21 #if defined(STM32F030x6)
22 #define CRC_CUST_POLY (0x0)
23 #define CRC_IO_REV_SUPP (0x1)
24 #define CRC_CUST_INIT_VAL (0x1)
25 #define CRC_CALC_DELAY //Nothing
26 
27 #elif defined(STM32F070x6)
28 #define CRC_CUST_POLY (0x1)
29 #define CRC_IO_REV_SUPP (0x1)
30 #define CRC_CUST_INIT_VAL (0x1)
31 #define CRC_CALC_DELAY //Nothing
32 
33 #elif defined(STM32F070xB)
34 #define CRC_CUST_POLY (0x1)
35 #define CRC_IO_REV_SUPP (0x1)
36 #define CRC_CUST_INIT_VAL (0x1)
37 #define CRC_CALC_DELAY //Nothing
38 
39 #elif defined(STM32F031x6)
40 #define CRC_CUST_POLY (0x0)
41 #define CRC_IO_REV_SUPP (0x1)
42 #define CRC_CUST_INIT_VAL (0x1)
43 #define CRC_CALC_DELAY //Nothing
44 
45 #elif defined(STM32F103xB)
46 #define CRC_CUST_POLY (0x0)
47 #define CRC_IO_REV_SUPP (0x0)
48 #define CRC_CUST_INIT_VAL (0x0)
49 #define CRC_CALC_DELAY asm("nop"); //Wait 1 tick for every one calculated byte
50 
51 #elif defined(STM32L031xx)
52 #define CRC_CUST_POLY (0x1)
53 #define CRC_IO_REV_SUPP (0x1)
54 #define CRC_CUST_INIT_VAL (0x1)
55 #define CRC_CALC_DELAY //Nothing
56 
57 #elif defined(STM32L432xx)
58 #define CRC_CUST_POLY (0x1) //Custom polynomial supported
59 #define CRC_IO_REV_SUPP (0x1) //Input / output reverse (reflection)supported
60 #define CRC_CUST_INIT_VAL (0x1) //Custom init value supported
61 #define CRC_CALC_DELAY //Nothing
62 
63 #else
64 #error "This library does not supports selected MCU." //This is here only, because some methods may not works with that chip.
65 #endif
66 
67 
76 #define CRC_X0 (0x01UL)
77 #define CRC_X1 (0x01UL << 1U)
78 #define CRC_X2 (0x01UL << 2U)
79 #define CRC_X3 (0x01UL << 3U)
80 #define CRC_X4 (0x01UL << 4U)
81 #define CRC_X5 (0x01UL << 5U)
82 #define CRC_X6 (0x01UL << 6U)
83 #define CRC_X7 (0x01UL << 7U)
84 #define CRC_X8 (0x01UL << 8U)
85 #define CRC_X9 (0x01UL << 9U)
86 #define CRC_X10 (0x01UL << 10U)
87 #define CRC_X11 (0x01UL << 11U)
88 #define CRC_X12 (0x01UL << 12U)
89 #define CRC_X13 (0x01UL << 13U)
90 #define CRC_X14 (0x01UL << 14U)
91 #define CRC_X15 (0x01UL << 15U)
92 #define CRC_X16 (0x01UL << 16U)
93 #define CRC_X17 (0x01UL << 17U)
94 #define CRC_X18 (0x01UL << 18U)
95 #define CRC_X19 (0x01UL << 19U)
96 #define CRC_X20 (0x01UL << 20U)
97 #define CRC_X21 (0x01UL << 21U)
98 #define CRC_X22 (0x01UL << 22U)
99 #define CRC_X23 (0x01UL << 23U)
100 #define CRC_X24 (0x01UL << 24U)
101 #define CRC_X25 (0x01UL << 25U)
102 #define CRC_X26 (0x01UL << 26U)
103 #define CRC_X27 (0x01UL << 27U)
104 #define CRC_X28 (0x01UL << 28U)
105 #define CRC_X29 (0x01UL << 29U)
106 #define CRC_X30 (0x01UL << 30U)
107 #define CRC_X31 (0x01UL << 31U)
108 
116 //Mostly used polynomials
117 #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 */
118 #define CRC16_POLY (0x8005UL) /* X^16 + X^15 + X^2 + 1 - IBM CRC polynomial*/
119 #define CRC8_POLY (0x31UL) /* X^8 + X^5 + X^4 + 1 - I2C CRC polynomial*/
120 #define CRC7_POLY (0x09UL) /* X^7 + X^3 + 1 */
121 
126 #define DEFAULT_HCRC_INITVAL (0xFFFFFFFFUL) //Default initial value
127 
128 //Polynomial size CR register mask
129 #define HCRC_POLYSIZE_MASK (0x18) //B00011000
130 //Polynomial size values
131 #define HCRC_32BIT_POLYSIZE (0x00)
132 #define HCRC_16BIT_POLYSIZE (0x08)
133 #define HCRC_8BIT_POLYSIZE (0x10)
134 #define HCRC_7BIT_POLYSIZE (HCRC_POLYSIZE_MASK)
135 
136 //Output reverse CR register mask
137 #define HCRC_OUT_REVERSE_MASK (0x80) //B10000000
138 //Output reverse values
139 #define HCRC_OUT_REVERSE_ENABLED HCRC_OUT_REVERSE_MASK
140 #define HCRC_OUT_REVERSE_DISABLED (0x0)
141 
142 //Input reverse CR register mask
143 #define HCRC_IN_REVERSE_MASK (0x60) //B01100000
144 //Input reverse values
145 #define HCRC_IN_REVERSE_ENABLED_BYTE (0x20)
146 #define HCRC_IN_REVERSE_ENABLED_HALFWORD (0x40)
147 #define HCRC_IN_REVERSE_ENABLED_WORD HCRC_IN_REVERSE_MASK
148 #define HCRC_IN_REVERSE_DISABLED (0x0)
149 
150 
151 /*struct CRCSettings{
152  CRCSettings(uint32_t lastResult, uint32_t initValue, uint32_t polynomial, uint8_t settings)
153  {
154  LastResult = lastResult;
155  InitValue = initValue;
156  Polynomial = polynomial;
157  Settings = settings;
158  }
159 
160  uint32_t LastResult = 0;
161  uint32_t InitValue = 0;
162  uint32_t Polynomial = 0;
163  uint8_t Settings = 0;
164 };*/
165 
166 
191 public:
192 
197  bool begin();
198 
205  bool setPolynomial7(uint32_t polynomial = CRC7_POLY);
206 
213  bool setPolynomial8(uint32_t polynomial = CRC8_POLY);
214 
221  bool setPolynomial16(uint32_t polynomial = CRC16_POLY);
222 
229  bool setPolynomial32(uint32_t polynomial = CRC32_POLY);
230 
235  inline uint32_t getPolynomial(){
236 #if CRC_CUST_POLY == (0x1)
237  return CRC->POL;
238 #else
239  return CRC32_POLY;
240 #endif
241  }
242 
247  uint8_t getPolynomialLength();
248 
253  inline uint32_t getInitValue(){
254 #if CRC_CUST_INIT_VAL == (0x1)
255  return CRC->INIT;
256  //return InitValue;
257 #else
258  return 0xFFFFFFFFUL;
259 #endif
260  }
261 
267  inline uint32_t getLastCRC(uint32_t finalXOR = 0x00){
268  return CRC->DR ^ finalXOR;
269  }
270 
275  void reset(uint32_t initValue = DEFAULT_HCRC_INITVAL);
276 
284  uint32_t append(const uint8_t data, uint32_t finalXOR = 0x00);
285 
294  uint32_t append(const uint8_t* data, uint32_t dataLength, uint32_t finalXOR = 0x00);
295 
304  uint32_t append(const int8_t* data, uint32_t dataLength, uint32_t finalXOR = 0x00){
305  return append((const uint8_t*)data, dataLength, finalXOR);
306  }
307 
315  uint32_t append(const uint16_t data, uint32_t finalXOR = 0x00);
316 
325  uint32_t append(const uint16_t* data, uint32_t dataLength, uint32_t finalXOR = 0x00);
326 
335  uint32_t append(const int16_t* data, uint32_t dataLength, uint32_t finalXOR = 0x00){
336  return append((const uint16_t*)data, dataLength, finalXOR);
337  }
338 
346  uint32_t append(const uint32_t data, uint32_t finalXOR = 0x00);
347 
356  uint32_t append(const uint32_t* data, uint32_t dataLength, uint32_t finalXOR = 0x00);
357 
366  uint32_t append (const int32_t* data, uint32_t dataLength, uint32_t finalXOR = 0x00){
367  return append((const uint32_t*)data, dataLength, finalXOR);
368  }
369 
379  uint32_t compute(const uint8_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00);
380 
390  uint32_t compute(const int8_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
391  return compute((const uint8_t*)data, dataLength, initValue, finalXOR);
392  }
393 
403  uint32_t compute(const uint16_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00);
404 
414  uint32_t compute(const int16_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
415  return compute((const uint16_t*)data, dataLength, initValue, finalXOR);
416  }
417 
427  uint32_t compute(const uint32_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00);
428 
438  uint32_t compute(const int32_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
439  return compute((const uint32_t*)data, dataLength, initValue, finalXOR);
440  }
441 
451  uint32_t computeAndWriteToEnd(uint8_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00);
452 
462  uint32_t computeAndWriteToEnd(int8_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
463  return computeAndWriteToEnd((uint8_t*)data, dataLength, initValue, finalXOR);
464  }
465 
475  uint32_t computeAndWriteToEnd(uint16_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00);
476 
486  uint32_t computeAndWriteToEnd(int16_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
487  return computeAndWriteToEnd((uint16_t*)data, dataLength, initValue, finalXOR);
488  }
489 
499  uint32_t computeAndWriteToEnd(uint32_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00);
500 
510  uint32_t computeAndWriteToEnd(int32_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
511  return computeAndWriteToEnd((uint32_t*)data, dataLength, initValue, finalXOR);
512  }
513 
523  bool check(const uint8_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00);
524 
534  bool check(const int8_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
535  return check((const uint8_t*)data, dataLength, initValue, finalXOR);
536  }
537 
547  bool check(const uint16_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00);
548 
558  bool check(const int16_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
559  return check((const uint16_t*)data, dataLength, initValue, finalXOR);
560  }
561 
571  bool check(const uint32_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00);
572 
582  bool check(const int32_t* data, uint32_t dataLength, uint32_t initValue = DEFAULT_HCRC_INITVAL, uint32_t finalXOR = 0x00){
583  return check((const uint32_t*)data, dataLength, initValue, finalXOR);
584  }
585 
592  uint32_t getCheckValue(uint32_t initValue = DEFAULT_HCRC_INITVAL,uint32_t finalXOR = 0x00);
593 
598  bool setInputReverse(bool reverse);
599 
605  bool setOutputReverse(bool reverse);
606 
611  inline bool getInputReverse(){
612 #if CRC_IO_REV_SUPP == (0x1)
614 #else
615  return true; //Input is reversed by hardware
616 #endif
617  }
618 
623  inline bool getOutputReverse(){
624 #if CRC_IO_REV_SUPP == (0x1)
625  return (CRC->CR & HCRC_OUT_REVERSE_MASK) == HCRC_OUT_REVERSE_ENABLED;
626 #else
627  return true; //Output is reversed by hardware
628 #endif
629  }
630 
634  void end();
635 
636  /*CRCSettings exportSettings();
637 
638  void importSettings(CRCSettings settings);*/
639 
644  static const bool isCustomPolynomialSupported = CRC_CUST_POLY;
645 
649  static const bool isIOReverseSupported = CRC_IO_REV_SUPP;
650 
654  static const bool isCustomInitValueSupported = CRC_CUST_INIT_VAL;
655 
656 protected:
657 
658  //Global values used to check, if setting has changed in getCheckValue() method.
659  uint8_t lastCR = 0xFF;
660 #if CRC_CUST_POLY == (0x1)
661  uint32_t lastPoly = 0;
662 #endif
663 #if CRC_CUST_INIT_VAL == (0x1)
664  uint32_t lastInit = 0;
665 #endif
666  uint32_t lastFXOR = 0;
667  uint32_t lastCRCCheckValue = 0;
668 
669 /*#if CRC_CUST_INIT_VAL == (0x1)
670  uint32_t InitValue = 0xFFFFFFFF;
671 #endif*/
672 
677  void setInputReverseSize(uint32_t reverse);
678 
680 };
681 
682 extern HardwareCRC crc;
683 
684 #endif /* HARDWARECRC_H_ */
HardwareCRC::end
void end()
Disables hardware CRC calculation unit.
Definition: HardwareCRC.cpp:501
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.
Definition: HardwareCRC.h:558
HardwareCRC::getPolynomial
uint32_t getPolynomial()
Gets polynomial, that was set.
Definition: HardwareCRC.h:235
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.
Definition: HardwareCRC.h:438
HardwareCRC::getCheckValue
uint32_t getCheckValue(uint32_t initValue=DEFAULT_HCRC_INITVAL, uint32_t finalXOR=0x00)
Calculates CRC check value.
Definition: HardwareCRC.cpp:432
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:510
HardwareCRC::setInputReverseSize
void setInputReverseSize(uint32_t reverse)
Sets input reverse size to CR register.
Definition: HardwareCRC.cpp:532
HardwareCRC::isCustomPolynomialSupported
static const bool isCustomPolynomialSupported
True when custom polynomials are supported.
Definition: HardwareCRC.h:644
HardwareCRC
Calculate CRC simply and fast using this class. Calculation is done using hardware CRC calculation un...
Definition: HardwareCRC.h:190
HardwareCRC::setOutputReverse
bool setOutputReverse(bool reverse)
Sets, if output CRC data bits will be reversed(reflected) or not.
Definition: HardwareCRC.cpp:491
HardwareCRC::reset
void reset(uint32_t initValue=DEFAULT_HCRC_INITVAL)
Resets calculation and sets initial value.
Definition: HardwareCRC.cpp:92
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.
Definition: HardwareCRC.cpp:255
HardwareCRC::setPolynomial32
bool setPolynomial32(uint32_t polynomial=CRC32_POLY)
Sets CRC-32 polynomial.
Definition: HardwareCRC.cpp:63
HardwareCRC::lastCR
uint8_t lastCR
Definition: HardwareCRC.h:659
HCRC_IN_REVERSE_DISABLED
#define HCRC_IN_REVERSE_DISABLED
Definition: HardwareCRC.h:148
HardwareCRC::getPolynomialLength
uint8_t getPolynomialLength()
Gets polynomial length in bits.
Definition: HardwareCRC.cpp:74
HardwareCRC::getInitValue
uint32_t getInitValue()
Gets initial value, that was set using reset(), compute() or computeAndWriteToEnd() method.
Definition: HardwareCRC.h:253
CRC16_POLY
#define CRC16_POLY
Definition: HardwareCRC.h:118
CRC8_POLY
#define CRC8_POLY
Definition: HardwareCRC.h:119
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.cpp:100
HardwareCRC::getLastCRC
uint32_t getLastCRC(uint32_t finalXOR=0x00)
Gets last calculated CRC result.
Definition: HardwareCRC.h:267
HardwareCRC::setPolynomial16
bool setPolynomial16(uint32_t polynomial=CRC16_POLY)
Sets CRC-16 polynomial.
Definition: HardwareCRC.cpp:50
HardwareCRC::isCustomInitValueSupported
static const bool isCustomInitValueSupported
True when custom initial value is supported, if not, 0xFFFFFFFFUL is set.
Definition: HardwareCRC.h:654
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:366
HardwareCRC::isIOReverseSupported
static const bool isIOReverseSupported
True when input / output reversion (reflection) is supported.
Definition: HardwareCRC.h:649
HCRC_OUT_REVERSE_MASK
#define HCRC_OUT_REVERSE_MASK
Definition: HardwareCRC.h:137
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:486
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.
Definition: HardwareCRC.h:414
HardwareCRC::inputReverse
uint8_t inputReverse
Definition: HardwareCRC.h:679
HardwareCRC::lastFXOR
uint32_t lastFXOR
Definition: HardwareCRC.h:666
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:462
HardwareCRC::getInputReverse
bool getInputReverse()
Gets, if input data bits will be reversed(reflected) or not.
Definition: HardwareCRC.h:611
HardwareCRC::setPolynomial8
bool setPolynomial8(uint32_t polynomial=CRC8_POLY)
Sets CRC-8 polynomial.
Definition: HardwareCRC.cpp:37
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.
Definition: HardwareCRC.h:390
HardwareCRC::setInputReverse
bool setInputReverse(bool reverse)
Sets, if input data bits will be reversed(reflected) or not.
Definition: HardwareCRC.cpp:481
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.
Definition: HardwareCRC.h:582
CRC7_POLY
#define CRC7_POLY
Definition: HardwareCRC.h:120
HardwareCRC::lastCRCCheckValue
uint32_t lastCRCCheckValue
Definition: HardwareCRC.h:667
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.cpp:270
HardwareCRC::setPolynomial7
bool setPolynomial7(uint32_t polynomial=CRC7_POLY)
Sets CRC-7 polynomial.
Definition: HardwareCRC.cpp:24
wirish.h
This file contains basic (Arduino like) GPIO manipulation functions, delay, random,...
HCRC_IN_REVERSE_MASK
#define HCRC_IN_REVERSE_MASK
Definition: HardwareCRC.h:143
HCRC_OUT_REVERSE_ENABLED
#define HCRC_OUT_REVERSE_ENABLED
Definition: HardwareCRC.h:139
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:304
crc
HardwareCRC crc
Definition: HardwareCRC.cpp:539
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:335
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.
Definition: HardwareCRC.cpp:354
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.
Definition: HardwareCRC.h:534
HardwareCRC::getOutputReverse
bool getOutputReverse()
Gets, if output CRC data bits will be reversed(reflected) or not.
Definition: HardwareCRC.h:623
CRC32_POLY
#define CRC32_POLY
Definition: HardwareCRC.h:117
HardwareCRC::begin
bool begin()
Enables CRC hardware calculation unit.
Definition: HardwareCRC.cpp:11
DEFAULT_HCRC_INITVAL
#define DEFAULT_HCRC_INITVAL
Definition: HardwareCRC.h:126