crc.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /** \defgroup uart_apis, uart configuration and communication related apis
  20. * @brief uart apis
  21. */
  22. /** @addtogroup uart_apis
  23. * @{
  24. */
  25. /* Notes about CRC APIs usage
  26. * The ESP32 ROM include some CRC tables and CRC APIs to speed up CRC calculation.
  27. * The CRC APIs include CRC8, CRC16, CRC32 algorithms for both little endian and big endian modes.
  28. * Here are the polynomials for the algorithms:
  29. * CRC-8 x8+x2+x1+1 0x07
  30. * CRC16-CCITT x16+x12+x5+1 0x1021
  31. * CRC32 x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x1+1 0x04c11db7
  32. *
  33. * These group of CRC APIs are designed to calculate the data in buffers either continuous or not.
  34. * To make it easy, we had added a `~` at the beginning and the end of the functions.
  35. * To calculate non-continuous buffers, we can write the code like this:
  36. * init = ~init;
  37. * crc = crc32_le(init, buf0, length0);
  38. * crc = crc32_le(crc, buf1, length1);
  39. * crc = ~crc;
  40. *
  41. * However, it is not easy to select which API to use and give the correct parameters.
  42. * A specific CRC algorithm will include this parameters: width, polynomials, init, refin, refout, xorout
  43. * refin and refout show the endian of the algorithm:
  44. * if both of them are true, please use the little endian API.
  45. * if both of them are false, please use the big endian API.
  46. * xorout is the value which you need to be xored to the raw result.
  47. * However, these group of APIs need one '~' before and after the APIs.
  48. *
  49. * Here are some examples for CRC16:
  50. * CRC-16/CCITT, poly = 0x1021, init = 0x0000, refin = true, refout = true, xorout = 0x0000
  51. * crc = ~crc16_le((uint16_t)~0x0000, buf, length);
  52. *
  53. * CRC-16/CCITT-FALSE, poly = 0x1021, init = 0xffff, refin = false, refout = false, xorout = 0x0000
  54. * crc = ~crc16_be((uint16_t)~0xffff, buf, length);
  55. *
  56. * CRC-16/X25, poly = 0x1021, init = 0xffff, refin = true, refout = true, xorout = 0xffff
  57. * crc = (~crc16_le((uint16_t)~(0xffff), buf, length))^0xffff;
  58. *
  59. * CRC-16/XMODEM, poly= 0x1021, init = 0x0000, refin = false, refout = false, xorout = 0x0000
  60. * crc = ~crc16_be((uint16_t)~0x0000, buf, length);
  61. *
  62. *
  63. */
  64. /**
  65. * @brief CRC32 value that is in little endian.
  66. *
  67. * @param uint32_t crc : init crc value, use 0 at the first use.
  68. *
  69. * @param uint8_t const *buf : buffer to start calculate crc.
  70. *
  71. * @param uint32_t len : buffer length in byte.
  72. *
  73. * @return None
  74. */
  75. uint32_t crc32_le(uint32_t crc, uint8_t const *buf, uint32_t len);
  76. /**
  77. * @brief CRC32 value that is in big endian.
  78. *
  79. * @param uint32_t crc : init crc value, use 0 at the first use.
  80. *
  81. * @param uint8_t const *buf : buffer to start calculate crc.
  82. *
  83. * @param uint32_t len : buffer length in byte.
  84. *
  85. * @return None
  86. */
  87. uint32_t crc32_be(uint32_t crc, uint8_t const *buf, uint32_t len);
  88. /**
  89. * @brief CRC16 value that is in little endian.
  90. *
  91. * @param uint16_t crc : init crc value, use 0 at the first use.
  92. *
  93. * @param uint8_t const *buf : buffer to start calculate crc.
  94. *
  95. * @param uint32_t len : buffer length in byte.
  96. *
  97. * @return None
  98. */
  99. uint16_t crc16_le(uint16_t crc, uint8_t const *buf, uint32_t len);
  100. /**
  101. * @brief CRC16 value that is in big endian.
  102. *
  103. * @param uint16_t crc : init crc value, use 0 at the first use.
  104. *
  105. * @param uint8_t const *buf : buffer to start calculate crc.
  106. *
  107. * @param uint32_t len : buffer length in byte.
  108. *
  109. * @return None
  110. */
  111. uint16_t crc16_be(uint16_t crc, uint8_t const *buf, uint32_t len);
  112. /**
  113. * @brief CRC8 value that is in little endian.
  114. *
  115. * @param uint8_t crc : init crc value, use 0 at the first use.
  116. *
  117. * @param uint8_t const *buf : buffer to start calculate crc.
  118. *
  119. * @param uint32_t len : buffer length in byte.
  120. *
  121. * @return None
  122. */
  123. uint8_t crc8_le(uint8_t crc, uint8_t const *buf, uint32_t len);
  124. /**
  125. * @brief CRC8 value that is in big endian.
  126. *
  127. * @param uint32_t crc : init crc value, use 0 at the first use.
  128. *
  129. * @param uint8_t const *buf : buffer to start calculate crc.
  130. *
  131. * @param uint32_t len : buffer length in byte.
  132. *
  133. * @return None
  134. */
  135. uint8_t crc8_be(uint8_t crc, uint8_t const *buf, uint32_t len);
  136. /**
  137. * @}
  138. */
  139. #ifdef __cplusplus
  140. }
  141. #endif
  142. #endif