crc.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef ROM_CRC_H
  14. #define ROM_CRC_H
  15. #include <stdint.h>
  16. #define ESP_ROM_HAS_CRC8LE 1
  17. #define ESP_ROM_HAS_CRC16LE 1
  18. #define ESP_ROM_HAS_CRC32LE 1
  19. #define ESP_ROM_HAS_CRC8BE 1
  20. #define ESP_ROM_HAS_CRC16BE 1
  21. #define ESP_ROM_HAS_CRC32BE 1
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /** \defgroup uart_apis, uart configuration and communication related apis
  26. * @brief uart apis
  27. */
  28. /** @addtogroup uart_apis
  29. * @{
  30. */
  31. /* Notes about CRC APIs usage
  32. * The ESP32 ROM include some CRC tables and CRC APIs to speed up CRC calculation.
  33. * The CRC APIs include CRC8, CRC16, CRC32 algorithms for both little endian and big endian modes.
  34. * Here are the polynomials for the algorithms:
  35. * CRC-8 x8+x2+x1+1 0x07
  36. * CRC16-CCITT x16+x12+x5+1 0x1021
  37. * CRC32 x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x1+1 0x04c11db7
  38. *
  39. * These group of CRC APIs are designed to calculate the data in buffers either continuous or not.
  40. * To make it easy, we had added a `~` at the beginning and the end of the functions.
  41. * To calculate non-continuous buffers, we can write the code like this:
  42. * init = ~init;
  43. * crc = crc32_le(init, buf0, length0);
  44. * crc = crc32_le(crc, buf1, length1);
  45. * crc = ~crc;
  46. *
  47. * However, it is not easy to select which API to use and give the correct parameters.
  48. * A specific CRC algorithm will include this parameters: width, polynomials, init, refin, refout, xorout
  49. * refin and refout show the endian of the algorithm:
  50. * if both of them are true, please use the little endian API.
  51. * if both of them are false, please use the big endian API.
  52. * xorout is the value which you need to be xored to the raw result.
  53. * However, these group of APIs need one '~' before and after the APIs.
  54. *
  55. * Here are some examples for CRC16:
  56. * CRC-16/CCITT, poly = 0x1021, init = 0x0000, refin = true, refout = true, xorout = 0x0000
  57. * crc = ~crc16_le((uint16_t)~0x0000, buf, length);
  58. *
  59. * CRC-16/CCITT-FALSE, poly = 0x1021, init = 0xffff, refin = false, refout = false, xorout = 0x0000
  60. * crc = ~crc16_be((uint16_t)~0xffff, buf, length);
  61. *
  62. * CRC-16/X25, poly = 0x1021, init = 0xffff, refin = true, refout = true, xorout = 0xffff
  63. * crc = (~crc16_le((uint16_t)~(0xffff), buf, length))^0xffff;
  64. *
  65. * CRC-16/XMODEM, poly= 0x1021, init = 0x0000, refin = false, refout = false, xorout = 0x0000
  66. * crc = ~crc16_be((uint16_t)~0x0000, buf, length);
  67. *
  68. *
  69. */
  70. /**
  71. * @brief CRC32 value that is in little endian.
  72. *
  73. * @param uint32_t crc : init crc value, use 0 at the first use.
  74. *
  75. * @param uint8_t const *buf : buffer to start calculate crc.
  76. *
  77. * @param uint32_t len : buffer length in byte.
  78. *
  79. * @return None
  80. */
  81. uint32_t crc32_le(uint32_t crc, uint8_t const *buf, uint32_t len);
  82. /**
  83. * @brief CRC32 value that is in big endian.
  84. *
  85. * @param uint32_t crc : init crc value, use 0 at the first use.
  86. *
  87. * @param uint8_t const *buf : buffer to start calculate crc.
  88. *
  89. * @param uint32_t len : buffer length in byte.
  90. *
  91. * @return None
  92. */
  93. uint32_t crc32_be(uint32_t crc, uint8_t const *buf, uint32_t len);
  94. /**
  95. * @brief CRC16 value that is in little endian.
  96. *
  97. * @param uint16_t crc : init crc value, use 0 at the first use.
  98. *
  99. * @param uint8_t const *buf : buffer to start calculate crc.
  100. *
  101. * @param uint32_t len : buffer length in byte.
  102. *
  103. * @return None
  104. */
  105. uint16_t crc16_le(uint16_t crc, uint8_t const *buf, uint32_t len);
  106. /**
  107. * @brief CRC16 value that is in big endian.
  108. *
  109. * @param uint16_t crc : init crc value, use 0 at the first use.
  110. *
  111. * @param uint8_t const *buf : buffer to start calculate crc.
  112. *
  113. * @param uint32_t len : buffer length in byte.
  114. *
  115. * @return None
  116. */
  117. uint16_t crc16_be(uint16_t crc, uint8_t const *buf, uint32_t len);
  118. /**
  119. * @brief CRC8 value that is in little endian.
  120. *
  121. * @param uint8_t crc : init crc value, use 0 at the first use.
  122. *
  123. * @param uint8_t const *buf : buffer to start calculate crc.
  124. *
  125. * @param uint32_t len : buffer length in byte.
  126. *
  127. * @return None
  128. */
  129. uint8_t crc8_le(uint8_t crc, uint8_t const *buf, uint32_t len);
  130. /**
  131. * @brief CRC8 value that is in big endian.
  132. *
  133. * @param uint32_t crc : init crc value, use 0 at the first use.
  134. *
  135. * @param uint8_t const *buf : buffer to start calculate crc.
  136. *
  137. * @param uint32_t len : buffer length in byte.
  138. *
  139. * @return None
  140. */
  141. uint8_t crc8_be(uint8_t crc, uint8_t const *buf, uint32_t len);
  142. /**
  143. * @}
  144. */
  145. #ifdef __cplusplus
  146. }
  147. #endif
  148. #endif