hpm_crc_drv.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2021-2022 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_CRC_DRV_H
  8. #define HPM_CRC_DRV_H
  9. /**
  10. * @brief CRC APIs
  11. * @defgroup crc_interface CRC driver APIs
  12. * @ingroup crc_interfaces
  13. * @{
  14. */
  15. #include "hpm_common.h"
  16. #include "hpm_crc_regs.h"
  17. /**
  18. * @brief CRC preset definitions
  19. */
  20. typedef enum crc_preset_enum {
  21. crc_preset_none = 0,
  22. crc_preset_crc32, /*!< Poly: 0x04C11DB7, Init: 0xFFFFFFFF, Refin: True, Refout: True, Xorout: 0xFFFFFFFF */
  23. crc_preset_crc32_autosar, /*!< Poly: 0xF4ACFB13, Init: 0xFFFFFFFF, Refin: True, Refout: True, Xorout: 0xFFFFFFFF */
  24. crc_preset_crc16_ccitt, /*!< Poly: 0x1021, Init: 0x0000, Refin: True, Refout: True, Xorout: 0x0000 */
  25. crc_preset_crc16_xmodem, /*!< Poly: 0x1021, Init: 0x0000, Refin: False, Refout: False, Xorout: 0x0000 */
  26. crc_preset_crc16_modbus, /*!< Poly: 0x8005, Init: 0xFFFF, Refin: True, Refout: True, Xorout: 0x0000 */
  27. crc_preset_crc16_dnp, /*!< Poly: 0x3D65, Init: 0x0000, Refin: True, Refout: True, Xorout: 0xFFFF */
  28. crc_preset_crc16_x25, /*!< Poly: 0x1021, Init: 0xFFFF, Refin: True, Refout: True, Xorout: 0xFFFF */
  29. crc_preset_crc16_usb, /*!< Poly: 0x8005, Init: 0xFFFF, Refin: True, Refout: True, Xorout: 0xFFFF */
  30. crc_preset_crc16_maxim, /*!< Poly: 0x8005, Init: 0x0000, Refin: True, Refout: True, Xorout: 0xFFFF */
  31. crc_preset_crc16_ibm, /*!< Poly: 0x8005, Init: 0x0000, Refin: True, Refout: True, Xorout: 0x0000 */
  32. crc_preset_crc8_maxim, /*!< Poly: 0x31, Init: 0x00, Refin: True, Refout: True, Xorout: 0x00 */
  33. crc_preset_crc8_rohc, /*!< Poly: 0x07, Init: 0xFF, Refin: True, Refout: True, Xorout: 0x00 */
  34. crc_preset_crc8_itu, /*!< Poly: 0x07, Init: 0x00, Refin: False, Refout: False, Xorout: 0x55 */
  35. crc_preset_crc8, /*!< Poly: 0x07, Init: 0x00, Refin: False, Refout: False, Xorout: 0x00 */
  36. crc_preset_crc5_usb, /*!< Poly: 0x05, Init: 0x1F, Refin: True, Refout: True, Xorout: 0x1F */
  37. } crc_preset_t;
  38. /**
  39. * @brief CRC Refin definitions.
  40. */
  41. typedef enum crc_refin_enum {
  42. crc_refin_false = 0, /*!< Do not manipulate input data stream. */
  43. crc_refin_true = 1, /*!< Reflect each byte in the input stream bitwise. */
  44. } crc_refin_t;
  45. /**
  46. * @brief CRC Refout definitions.
  47. */
  48. typedef enum crc_refout_enum {
  49. crc_refout_false = 0, /*!< Do not manipulate output data stream. */
  50. crc_refout_true = 1, /*!< Reflect each byte in the output stream bitwise. */
  51. } crc_refout_t;
  52. /**
  53. * @brief crc input data stream byte order definitions.
  54. */
  55. typedef enum crc_in_byte_order_enum {
  56. crc_in_byte_order_lsb = 0, /*!< Byte order of the CRC DATA LS Byte first. */
  57. crc_in_byte_order_msb = 1, /*!< Byte order of the CRC DATA MS Byte first. */
  58. } crc_in_byte_order_t;
  59. #define CRC_POLY_WIDTH_4 (4U)
  60. #define CRC_POLY_WIDTH_5 (5U)
  61. #define CRC_POLY_WIDTH_6 (6U)
  62. #define CRC_POLY_WIDTH_7 (7U)
  63. #define CRC_POLY_WIDTH_8 (8U)
  64. #define CRC_POLY_WIDTH_16 (16U)
  65. #define CRC_POLY_WIDTH_24 (24U)
  66. #define CRC_POLY_WIDTH_32 (32U)
  67. /**
  68. * @brief Channel config
  69. */
  70. typedef struct crc_channel_config {
  71. crc_preset_t preset; /*!< Preset CRC. See "crc_preset_t". */
  72. uint32_t init; /*!< Initial value for CRC. */
  73. uint32_t poly; /*!< Poly for CRC. */
  74. uint32_t poly_width; /*!< CRC poly width. See "CRC_POLY_WIDTH_x". */
  75. crc_in_byte_order_t in_byte_order; /*!< CRC intput byte order. See "crc_in_byte_order_t". */
  76. crc_refout_t refout; /*!< CRC reflect output. See "crc_refout_t". */
  77. crc_refin_t refin; /*!< CRC reflect iutput. See "crc_refin_t". */
  78. uint32_t xorout; /*!< XOR mask for CRC result (for no mask, should be 0). */
  79. } crc_channel_config_t;
  80. #define CRC_REG_WRITE8(addr, data)\
  81. {\
  82. uint32_t addr32 = (uint32_t)(addr);\
  83. (*(volatile uint8_t *)(addr32) = (data));\
  84. }
  85. #define CRC_REG_WRITE16(addr, data)\
  86. {\
  87. uint32_t addr32 = (uint32_t)(addr);\
  88. (*(volatile uint16_t *)(addr32) = (data));\
  89. }
  90. #define CRC_REG_WRITE32(addr, data)\
  91. {\
  92. uint32_t addr32 = (uint32_t)(addr);\
  93. (*(volatile uint32_t *)(addr32) = (data));\
  94. }
  95. #ifdef __cplusplus
  96. extern "C" {
  97. #endif
  98. /**
  99. * @brief Reset CRC channel
  100. *
  101. * @param[in] ptr CRC base address
  102. * @param[in] ch_index Index of the channel to be reset
  103. *
  104. */
  105. static inline void crc_reset(CRC_Type *ptr, uint32_t ch_index)
  106. {
  107. ptr->CHN[ch_index].CLR |= CRC_CHN_CLR_CLR_MASK;
  108. }
  109. /**
  110. * @brief Get default channel config
  111. *
  112. * @param[in] ptr CRC base address
  113. * @param[in] cfg Channel config
  114. */
  115. void crc_get_default_channel_config(crc_channel_config_t *cfg);
  116. /**
  117. * @brief Setup CRC channel
  118. *
  119. * @param[in] ptr CRC base address
  120. * @param[in] ch_index Target channel index to be configured
  121. * @param[in] cfg Channel config
  122. *
  123. * @return status_success if everything is okay
  124. */
  125. hpm_stat_t crc_setup_channel_config(CRC_Type *ptr, uint32_t ch_index,
  126. crc_channel_config_t *cfg);
  127. /**
  128. * @brief Calculate one byte data crc
  129. *
  130. * @param[in] ptr CRC base address
  131. * @param[in] ch_index CRC channel index
  132. * @param[in] data Data that to be calculate
  133. */
  134. static inline void crc_calc_byte(CRC_Type *ptr, uint32_t ch_index, uint8_t data)
  135. {
  136. CRC_REG_WRITE8(&ptr->CHN[ch_index].DATA, data);
  137. }
  138. /**
  139. * @brief Calculate length bytes data block crc
  140. *
  141. * @param[in] ptr CRC base address
  142. * @param[in] ch_index CRC channel index
  143. * @param[in] pbuffer Data to be calculate buffer
  144. * @param[in] length Number of pbuffer, unit is byte
  145. */
  146. void crc_calc_block_bytes(CRC_Type *ptr, uint32_t ch_index, uint8_t *pbuffer, uint32_t length);
  147. /**
  148. * @brief Calculate half-word data crc
  149. *
  150. * @param[in] ptr CRC base address
  151. * @param[in] ch_index CRC channel index
  152. * @param[in] data Data that to be calculate
  153. */
  154. static inline void crc_calc_half_word(CRC_Type *ptr, uint32_t ch_index, uint16_t data)
  155. {
  156. CRC_REG_WRITE16(&ptr->CHN[ch_index].DATA, data);
  157. }
  158. /**
  159. * @brief Calculate length half-words data block crc
  160. *
  161. * @param[in] ptr CRC base address
  162. * @param[in] ch_index CRC channel index
  163. * @param[in] pbuffer Data to be calculate buffer
  164. * @param[in] length Number of pbuffer, unit is half word(2 bytes)
  165. */
  166. void crc_calc_block_half_words(CRC_Type *ptr, uint32_t ch_index, uint16_t *pbuffer, uint32_t length);
  167. /**
  168. * @brief Calculate word data crc
  169. *
  170. * @param[in] ptr CRC base address
  171. * @param[in] ch_index CRC channel index
  172. * @param[in] data Data that to be calculate
  173. */
  174. static inline void crc_calc_word(CRC_Type *ptr, uint32_t ch_index, uint32_t data)
  175. {
  176. CRC_REG_WRITE32(&ptr->CHN[ch_index].DATA, data);
  177. }
  178. /**
  179. * @brief Calculate length words data block crc
  180. *
  181. * @param[in] ptr CRC base address
  182. * @param[in] ch_index CRC channel index
  183. * @param[in] pbuffer Data to be calculate buffer
  184. * @param[in] length Number of pbuffer, unit is word(4 bytes)
  185. */
  186. void crc_calc_block_words(CRC_Type *ptr, uint32_t ch_index, uint32_t *pbuffer, uint32_t length);
  187. /**
  188. * @brief Fast calculate length bytes large data block crc
  189. *
  190. * @param[in] ptr CRC base address
  191. * @param[in] ch_index CRC channel index
  192. * @param[in] pbuffer Data to be calculate buffer
  193. * @param[in] length Number of pbuffer, unit is byte
  194. */
  195. void crc_calc_large_block_fast(CRC_Type *ptr, uint32_t ch_index, uint8_t *pbuffer, uint32_t length);
  196. /**
  197. * @brief Get CRC result
  198. *
  199. * @param[in] ptr CRC base address
  200. * @param[in] ch_index Index of the channel to be get
  201. * @return CRC result
  202. */
  203. static inline uint32_t crc_get_result(CRC_Type *ptr, uint32_t ch_index)
  204. {
  205. return ptr->CHN[ch_index].RESULT;
  206. }
  207. #ifdef __cplusplus
  208. }
  209. #endif
  210. /**
  211. * @}
  212. */
  213. #endif /* HPM_CRC_DRV_H */