esp_crc.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2015-2019 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. #include "sdkconfig.h"
  20. #if defined(CONFIG_IDF_TARGET_ESP32)
  21. #include "esp32/rom/crc.h"
  22. #endif
  23. #if defined(CONFIG_IDF_TARGET_ESP32S2)
  24. #include "esp32s2/rom/crc.h"
  25. #endif
  26. /******************* Polynomials Used in the CRC APIs ****************************
  27. * CRC-8 x8+x2+x1+1 0x07
  28. * CRC16-CCITT x16+x12+x5+1 0x1021
  29. * CRC32 x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x1+1 0x04c11db7
  30. ********************************************************************************/
  31. /**
  32. * @brief CRC32 value in little endian.
  33. *
  34. * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
  35. * @param buf: Data buffer that used to calculate the CRC value
  36. * @param len: Length of the data buffer
  37. * @return CRC32 value
  38. */
  39. static inline uint32_t esp_crc32_le(uint32_t crc, uint8_t const *buf, uint32_t len)
  40. {
  41. return crc32_le(crc, buf, len);
  42. }
  43. #if defined(CONFIG_IDF_TARGET_ESP32)
  44. /**
  45. * @brief CRC32 value in big endian.
  46. *
  47. * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
  48. * @param buf: Data buffer that used to calculate the CRC value
  49. * @param len: Length of the data buffer
  50. * @return CRC32 value
  51. */
  52. static inline uint32_t esp_crc32_be(uint32_t crc, uint8_t const *buf, uint32_t len)
  53. {
  54. return crc32_be(crc, buf, len);
  55. }
  56. #endif
  57. /**
  58. * @brief CRC16 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 CRC16 value
  64. */
  65. static inline uint16_t esp_crc16_le(uint16_t crc, uint8_t const *buf, uint32_t len)
  66. {
  67. return crc16_le(crc, buf, len);
  68. }
  69. #if defined(CONFIG_IDF_TARGET_ESP32)
  70. /**
  71. * @brief CRC16 value in big endian.
  72. *
  73. * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
  74. * @param buf: Data buffer that used to calculate the CRC value
  75. * @param len: Length of the data buffer
  76. * @return CRC16 value
  77. */
  78. static inline uint16_t esp_crc16_be(uint16_t crc, uint8_t const *buf, uint32_t len)
  79. {
  80. return crc16_be(crc, buf, len);
  81. }
  82. #endif
  83. /**
  84. * @brief CRC8 value in little endian.
  85. *
  86. * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
  87. * @param buf: Data buffer that used to calculate the CRC value
  88. * @param len: Length of the data buffer
  89. * @return CRC8 value
  90. */
  91. static inline uint8_t esp_crc8_le(uint8_t crc, uint8_t const *buf, uint32_t len)
  92. {
  93. return crc8_le(crc, buf, len);
  94. }
  95. #if defined(CONFIG_IDF_TARGET_ESP32)
  96. /**
  97. * @brief CRC8 value in big endian.
  98. *
  99. * @param crc: Initial CRC value (result of last calculation or 0 for the first time)
  100. * @param buf: Data buffer that used to calculate the CRC value
  101. * @param len: Length of the data buffer
  102. * @return CRC8 value
  103. */
  104. static inline uint8_t esp_crc8_be(uint8_t crc, uint8_t const *buf, uint32_t len)
  105. {
  106. return crc8_be(crc, buf, len);
  107. }
  108. #endif
  109. #ifdef __cplusplus
  110. }
  111. #endif