esp_rom_crc.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2010-2020 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include <stdint.h>
  19. /** Notes about CRC API
  20. * The ESP32 ROM include some CRC tables and CRC APIs to speed up CRC calculation.
  21. * The CRC APIs include CRC8, CRC16, CRC32 algorithms for both little endian and big endian modes.
  22. * Here are the polynomials for the algorithms:
  23. * CRC-8 x8+x2+x1+1 0x07
  24. * CRC16-CCITT x16+x12+x5+1 0x1021
  25. * CRC32 x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x1+1 0x04c11db7
  26. *
  27. * These group of CRC APIs are designed to calculate the data in buffers either continuous or not.
  28. * To make it easy, we had added a `~` at the beginning and the end of the functions.
  29. * To calculate non-continuous buffers, we can write the code like this:
  30. * init = ~init;
  31. * crc = crc32_le(init, buf0, length0);
  32. * crc = crc32_le(crc, buf1, length1);
  33. * crc = ~crc;
  34. *
  35. * However, it is not easy to select which API to use and give the correct parameters.
  36. * A specific CRC algorithm will include this parameters: width, polynomials, init, refin, refout, xorout
  37. * refin and refout show the endian of the algorithm:
  38. * if both of them are true, please use the little endian API.
  39. * if both of them are false, please use the big endian API.
  40. * xorout is the value which you need to be xored to the raw result.
  41. * However, these group of APIs need one '~' before and after the APIs.
  42. *
  43. * Here are some examples for CRC16:
  44. * CRC-16/CCITT, poly = 0x1021, init = 0x0000, refin = true, refout = true, xorout = 0x0000
  45. * crc = ~crc16_le((uint16_t)~0x0000, buf, length);
  46. *
  47. * CRC-16/CCITT-FALSE, poly = 0x1021, init = 0xffff, refin = false, refout = false, xorout = 0x0000
  48. * crc = ~crc16_be((uint16_t)~0xffff, buf, length);
  49. *
  50. * CRC-16/X25, poly = 0x1021, init = 0xffff, refin = true, refout = true, xorout = 0xffff
  51. * crc = (~crc16_le((uint16_t)~(0xffff), buf, length))^0xffff;
  52. *
  53. * CRC-16/XMODEM, poly= 0x1021, init = 0x0000, refin = false, refout = false, xorout = 0x0000
  54. * crc = ~crc16_be((uint16_t)~0x0000, buf, length);
  55. *
  56. */
  57. /**
  58. * @brief CRC32 value in little endian.
  59. *
  60. * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
  61. * @param buf: Data buffer that used to calculate the CRC value
  62. * @param len: Length of the data buffer
  63. * @return CRC32 value
  64. */
  65. uint32_t esp_rom_crc32_le(uint32_t crc, uint8_t const *buf, uint32_t len);
  66. /**
  67. * @brief CRC32 value in big endian.
  68. *
  69. * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
  70. * @param buf: Data buffer that used to calculate the CRC value
  71. * @param len: Length of the data buffer
  72. * @return CRC32 value
  73. */
  74. uint32_t esp_rom_crc32_be(uint32_t crc, uint8_t const *buf, uint32_t len);
  75. /**
  76. * @brief CRC16 value in little endian.
  77. *
  78. * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
  79. * @param buf: Data buffer that used to calculate the CRC value
  80. * @param len: Length of the data buffer
  81. * @return CRC16 value
  82. */
  83. uint16_t esp_rom_crc16_le(uint16_t crc, uint8_t const *buf, uint32_t len);
  84. /**
  85. * @brief CRC16 value in big endian.
  86. *
  87. * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
  88. * @param buf: Data buffer that used to calculate the CRC value
  89. * @param len: Length of the data buffer
  90. * @return CRC16 value
  91. */
  92. uint16_t esp_rom_crc16_be(uint16_t crc, uint8_t const *buf, uint32_t len);
  93. /**
  94. * @brief CRC8 value in little endian.
  95. *
  96. * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
  97. * @param buf: Data buffer that used to calculate the CRC value
  98. * @param len: Length of the data buffer
  99. * @return CRC8 value
  100. */
  101. uint8_t esp_rom_crc8_le(uint8_t crc, uint8_t const *buf, uint32_t len);
  102. /**
  103. * @brief CRC8 value in big endian.
  104. *
  105. * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
  106. * @param buf: Data buffer that used to calculate the CRC value
  107. * @param len: Length of the data buffer
  108. * @return CRC8 value
  109. */
  110. uint8_t esp_rom_crc8_be(uint8_t crc, uint8_t const *buf, uint32_t len);
  111. #ifdef __cplusplus
  112. }
  113. #endif