PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 line
7.2KB

  1. /* FastCRC library code is placed under the MIT license
  2. * Copyright (c) 2014-2019 Frank Bösing
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. // Teensy 3.0, Teensy 3.1:
  25. // See K20P64M72SF1RM.pdf (Kinetis), Pages 638 - 641 for documentation of CRC Device
  26. // See KINETIS_4N30D.pdf for Errata (Errata ID 2776)
  27. //
  28. // So, ALL HW-calculations are done as 32 bit.
  29. //
  30. //
  31. //
  32. // Thanks to:
  33. // - Catalogue of parametrised CRC algorithms, CRC RevEng
  34. // http://reveng.sourceforge.net/crc-catalogue/
  35. //
  36. // - Danjel McGougan (CRC-Table-Generator)
  37. //
  38. // Set this to 0 for smaller 32BIT-CRC-Tables:
  39. #define CRC_BIGTABLES 1
  40. #if !defined(FastCRC_h)
  41. #define FastCRC_h
  42. #include "inttypes.h"
  43. // ================= DEFINES ===================
  44. #if defined(KINETISK)
  45. #define CRC_SW 0
  46. #define CRC_FLAG_NOREFLECT (((1<<31) | (1<<30)) | ((0<<29) | (0<<28))) //refin=false refout=false
  47. #define CRC_FLAG_REFLECT (((1<<31) | (0<<30)) | ((1<<29) | (0<<28))) //Reflect in- and outgoing bytes (refin=true refout=true)
  48. #define CRC_FLAG_XOR (1<<26) //Perform XOR on result
  49. #define CRC_FLAG_NOREFLECT_8 (0) //For 8-Bit CRC
  50. #define CRC_FLAG_REFLECT_SWAP (((1<<31) | (0<<30)) | ((0<<29) | (1<<28))) //For 16-Bit CRC (byteswap)
  51. #else
  52. #define CRC_SW 1
  53. #endif
  54. // ================= 7-BIT CRC ===================
  55. class FastCRC7
  56. {
  57. public:
  58. FastCRC7();
  59. uint8_t crc7(const uint8_t *data, const uint16_t datalen); // (MultiMediaCard interface)
  60. uint8_t crc7_upd(const uint8_t *data, const uint16_t datalen); // Call for subsequent calculations with previous seed.
  61. #if !CRC_SW
  62. uint8_t generic(const uint8_t polyom, const uint8_t seed, const uint32_t flags, const uint8_t *data, const uint16_t datalen); //Not available in non-hw-variant (not T3.x)
  63. #endif
  64. private:
  65. #if CRC_SW
  66. uint8_t seed;
  67. #else
  68. uint8_t update(const uint8_t *data, const uint16_t datalen);
  69. #endif
  70. };
  71. // ================= 8-BIT CRC ===================
  72. class FastCRC8
  73. {
  74. public:
  75. FastCRC8();
  76. uint8_t smbus(const uint8_t *data, const uint16_t datalen); // Alias CRC-8
  77. uint8_t maxim(const uint8_t *data, const uint16_t datalen); // Equivalent to _crc_ibutton_update() in crc16.h from avr_libc
  78. uint8_t smbus_upd(const uint8_t *data, uint16_t datalen); // Call for subsequent calculations with previous seed.
  79. uint8_t maxim_upd(const uint8_t *data, uint16_t datalen); // Call for subsequent calculations with previous seed.
  80. #if !CRC_SW
  81. uint8_t generic(const uint8_t polyom, const uint8_t seed, const uint32_t flags, const uint8_t *data, const uint16_t datalen); //Not available in non-hw-variant (not T3.x)
  82. #endif
  83. private:
  84. #if CRC_SW
  85. uint8_t seed;
  86. #else
  87. uint8_t update(const uint8_t *data, const uint16_t datalen);
  88. #endif
  89. };
  90. // ================= 14-BIT CRC ===================
  91. class FastCRC14
  92. {
  93. public:
  94. #if !CRC_SW //NO Software-implemenation so far
  95. FastCRC14();
  96. uint16_t darc(const uint8_t *data, const uint16_t datalen);
  97. uint16_t gsm(const uint8_t *data, const uint16_t datalen);
  98. uint16_t eloran(const uint8_t *data, const uint16_t datalen);
  99. uint16_t ft4(const uint8_t *data, const uint16_t datalen);
  100. uint16_t darc_upd(const uint8_t *data, uint16_t len);
  101. uint16_t gsm_upd(const uint8_t *data, uint16_t len);
  102. uint16_t eloran_upd(const uint8_t *data, uint16_t len);
  103. uint16_t ft4_upd(const uint8_t *data, uint16_t len);
  104. #endif
  105. #if !CRC_SW
  106. uint16_t generic(const uint16_t polyom, const uint16_t seed, const uint32_t flags, const uint8_t *data, const uint16_t datalen); //Not available in non-hw-variant (not T3.x)
  107. #endif
  108. private:
  109. #if CRC_SW
  110. uint16_t seed;
  111. #else
  112. uint16_t update(const uint8_t *data, const uint16_t datalen);
  113. #endif
  114. };
  115. // ================= 16-BIT CRC ===================
  116. class FastCRC16
  117. {
  118. public:
  119. FastCRC16();
  120. uint16_t ccitt(const uint8_t *data, const uint16_t datalen); // Alias "false CCITT"
  121. uint16_t mcrf4xx(const uint8_t *data,const uint16_t datalen); // Equivalent to _crc_ccitt_update() in crc16.h from avr_libc
  122. uint16_t kermit(const uint8_t *data, const uint16_t datalen); // Alias CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-CCITT
  123. uint16_t modbus(const uint8_t *data, const uint16_t datalen); // Equivalent to _crc_16_update() in crc16.h from avr_libc
  124. uint16_t xmodem(const uint8_t *data, const uint16_t datalen); // Alias ZMODEM, CRC-16/ACORN
  125. uint16_t x25(const uint8_t *data, const uint16_t datalen); // Alias CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-B
  126. uint16_t ccitt_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
  127. uint16_t mcrf4xx_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
  128. uint16_t kermit_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
  129. uint16_t modbus_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
  130. uint16_t xmodem_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
  131. uint16_t x25_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
  132. #if !CRC_SW
  133. uint16_t generic(const uint16_t polyom, const uint16_t seed, const uint32_t flags, const uint8_t *data, const uint16_t datalen); //Not available in non-hw-variant (not T3.x)
  134. #endif
  135. private:
  136. #if CRC_SW
  137. uint16_t seed;
  138. #else
  139. uint16_t update(const uint8_t *data, const uint16_t datalen);
  140. #endif
  141. };
  142. // ================= 32-BIT CRC ===================
  143. class FastCRC32
  144. {
  145. public:
  146. FastCRC32();
  147. uint32_t crc32(const uint8_t *data, const uint16_t datalen); // Alias CRC-32/ADCCP, PKZIP, Ethernet, 802.3
  148. uint32_t cksum(const uint8_t *data, const uint16_t datalen); // Alias CRC-32/POSIX
  149. uint32_t crc32_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
  150. uint32_t cksum_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed
  151. #if !CRC_SW
  152. uint32_t generic(const uint32_t polyom, const uint32_t seed, const uint32_t flags, const uint8_t *data, const uint16_t datalen); //Not available in non-hw-variant (not T3.x)
  153. #endif
  154. private:
  155. #if CRC_SW
  156. uint32_t seed;
  157. #else
  158. uint32_t update(const uint8_t *data, const uint16_t datalen);
  159. #endif
  160. };
  161. #endif