crc_8xx.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * @brief LPC8xx Cyclic Redundancy Check (CRC) Engine driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2012
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licenser disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #ifndef __CRC_8XX_H_
  32. #define __CRC_8XX_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup CRC_8XX CHIP: LPC8xx Cyclic Redundancy Check Engine driver
  37. * @ingroup CHIP_8XX_Drivers
  38. * @{
  39. */
  40. /**
  41. * @brief CRC register block structure
  42. */
  43. typedef struct { /*!< CRC Structure */
  44. __IO uint32_t MODE; /*!< CRC Mode Register */
  45. __IO uint32_t SEED; /*!< CRC SEED Register */
  46. union {
  47. __I uint32_t SUM; /*!< CRC Checksum Register. */
  48. __O uint32_t WRDATA32; /*!< CRC Data Register: write size 32-bit*/
  49. __O uint16_t WRDATA16; /*!< CRC Data Register: write size 16-bit*/
  50. __O uint8_t WRDATA8; /*!< CRC Data Register: write size 8-bit*/
  51. };
  52. } LPC_CRC_T;
  53. /*
  54. * @brief CRC MODE register description
  55. */
  56. #define CRC_MODE_POLY_BITMASK ((0x03)) /** CRC polynomial Bit mask */
  57. #define CRC_MODE_POLY_CCITT (0x00) /** Select CRC-CCITT polynomial */
  58. #define CRC_MODE_POLY_CRC16 (0x01) /** Select CRC-16 polynomial */
  59. #define CRC_MODE_POLY_CRC32 (0x02) /** Select CRC-32 polynomial */
  60. #define CRC_MODE_WRDATA_BITMASK (0x03 << 2) /** CRC WR_Data Config Bit mask */
  61. #define CRC_MODE_WRDATA_BIT_RVS (1 << 2) /** Select Bit order reverse for WR_DATA (per byte) */
  62. #define CRC_MODE_WRDATA_CMPL (1 << 3) /** Select One's complement for WR_DATA */
  63. #define CRC_MODE_SUM_BITMASK (0x03 << 4) /** CRC Sum Config Bit mask */
  64. #define CRC_MODE_SUM_BIT_RVS (1 << 4) /** Select Bit order reverse for CRC_SUM */
  65. #define CRC_MODE_SUM_CMPL (1 << 5) /** Select One's complement for CRC_SUM */
  66. #define MODE_CFG_CCITT (0x00) /** Pre-defined mode word for default CCITT setup */
  67. #define MODE_CFG_CRC16 (0x15) /** Pre-defined mode word for default CRC16 setup */
  68. #define MODE_CFG_CRC32 (0x36) /** Pre-defined mode word for default CRC32 setup */
  69. #define CRC_SEED_CCITT (0x0000FFFF)/** Initial seed value for CCITT mode */
  70. #define CRC_SEED_CRC16 (0x00000000)/** Initial seed value for CRC16 mode */
  71. #define CRC_SEED_CRC32 (0xFFFFFFFF)/** Initial seed value for CRC32 mode */
  72. /**
  73. * @brief CRC polynomial
  74. */
  75. typedef enum IP_CRC_001_POLY {
  76. CRC_POLY_CCITT = CRC_MODE_POLY_CCITT, /**< CRC-CCIT polynomial */
  77. CRC_POLY_CRC16 = CRC_MODE_POLY_CRC16, /**< CRC-16 polynomial */
  78. CRC_POLY_CRC32 = CRC_MODE_POLY_CRC32, /**< CRC-32 polynomial */
  79. CRC_POLY_LAST,
  80. } CRC_POLY_T;
  81. /**
  82. * @brief Initializes the CRC Engine
  83. * @return Nothing
  84. */
  85. void Chip_CRC_Init(void);
  86. /**
  87. * @brief Deinitializes the CRC Engine
  88. * @return Nothing
  89. */
  90. void Chip_CRC_Deinit(void);
  91. /**
  92. * @brief Set the polynomial used for the CRC calculation
  93. * @param poly : The enumerated polynomial to be used
  94. * @param flags : An Or'ed value of flags that setup the mode
  95. * @return Nothing
  96. * @note Flags for setting up the mode word include CRC_MODE_WRDATA_BIT_RVS,
  97. * CRC_MODE_WRDATA_CMPL, CRC_MODE_SUM_BIT_RVS, and CRC_MODE_SUM_CMPL.
  98. */
  99. STATIC INLINE void Chip_CRC_SetPoly(CRC_POLY_T poly, uint32_t flags)
  100. {
  101. LPC_CRC->MODE = (uint32_t) poly | flags;
  102. }
  103. /**
  104. * @brief Sets up the CRC engine for CRC16 mode
  105. * @return Nothing
  106. */
  107. STATIC INLINE void Chip_CRC_UseCRC16(void)
  108. {
  109. LPC_CRC->MODE = MODE_CFG_CRC16;
  110. LPC_CRC->SEED = CRC_SEED_CRC16;
  111. }
  112. /**
  113. * @brief Sets up the CRC engine for CRC32 mode
  114. * @return Nothing
  115. */
  116. STATIC INLINE void Chip_CRC_UseCRC32(void)
  117. {
  118. LPC_CRC->MODE = MODE_CFG_CRC32;
  119. LPC_CRC->SEED = CRC_SEED_CRC32;
  120. }
  121. /**
  122. * @brief Sets up the CRC engine for CCITT mode
  123. * @return Nothing
  124. */
  125. STATIC INLINE void Chip_CRC_UseCCITT(void)
  126. {
  127. LPC_CRC->MODE = MODE_CFG_CCITT;
  128. LPC_CRC->SEED = CRC_SEED_CCITT;
  129. }
  130. /**
  131. * @brief Engage the CRC engine with defaults based on the polynomial to be used
  132. * @param poly : The enumerated polynomial to be used
  133. * @return Nothing
  134. */
  135. void Chip_CRC_UseDefaultConfig(CRC_POLY_T poly);
  136. /**
  137. * @brief Set the CRC Mode bits
  138. * @param mode : Mode value
  139. * @return Nothing
  140. */
  141. STATIC INLINE void Chip_CRC_SetMode(uint32_t mode)
  142. {
  143. LPC_CRC->MODE = mode;
  144. }
  145. /**
  146. * @brief Get the CRC Mode bits
  147. * @return The current value of the CRC Mode bits
  148. */
  149. STATIC INLINE uint32_t Chip_CRC_GetMode(void)
  150. {
  151. return LPC_CRC->MODE;
  152. }
  153. /**
  154. * @brief Set the seed bits used by the CRC_SUM register
  155. * @param seed : Seed value
  156. * @return Nothing
  157. */
  158. STATIC INLINE void Chip_CRC_SetSeed(uint32_t seed)
  159. {
  160. LPC_CRC->SEED = seed;
  161. }
  162. /**
  163. * @brief Get the CRC seed value
  164. * @return Seed value
  165. */
  166. STATIC INLINE uint32_t Chip_CRC_GetSeed(void)
  167. {
  168. return LPC_CRC->SEED;
  169. }
  170. /**
  171. * @brief Convenience function for writing 8-bit data to the CRC engine
  172. * @param data : 8-bit data to write
  173. * @return Nothing
  174. */
  175. STATIC INLINE void Chip_CRC_Write8(uint8_t data)
  176. {
  177. LPC_CRC->WRDATA8 = data;
  178. }
  179. /**
  180. * @brief Convenience function for writing 16-bit data to the CRC engine
  181. * @param data : 16-bit data to write
  182. * @return Nothing
  183. */
  184. STATIC INLINE void Chip_CRC_Write16(uint16_t data)
  185. {
  186. LPC_CRC->WRDATA16 = data;
  187. }
  188. /**
  189. * @brief Convenience function for writing 32-bit data to the CRC engine
  190. * @param data : 32-bit data to write
  191. * @return Nothing
  192. */
  193. STATIC INLINE void Chip_CRC_Write32(uint32_t data)
  194. {
  195. LPC_CRC->WRDATA32 = data;
  196. }
  197. /**
  198. * @brief Gets the CRC Sum based on the Mode and Seed as previously configured
  199. * @return CRC Checksum value
  200. */
  201. STATIC INLINE uint32_t Chip_CRC_Sum(void)
  202. {
  203. return LPC_CRC->SUM;
  204. }
  205. /**
  206. * @brief Convenience function for computing a standard CCITT checksum from an 8-bit data block
  207. * @param data : Pointer to the block of 8-bit data
  208. * @param bytes : The number of bytes pointed to by data
  209. * @return Check sum value
  210. */
  211. uint32_t Chip_CRC_CRC8(const uint8_t *data, uint32_t bytes);
  212. /**
  213. * @brief Convenience function for computing a standard CRC16 checksum from 16-bit data block
  214. * @param data : Pointer to the block of 16-bit data
  215. * @param hwords : The number of 16 byte entries pointed to by data
  216. * @return Check sum value
  217. */
  218. uint32_t Chip_CRC_CRC16(const uint16_t *data, uint32_t hwords);
  219. /**
  220. * @brief Convenience function for computing a standard CRC32 checksum from 32-bit data block
  221. * @param data : Pointer to the block of 32-bit data
  222. * @param words : The number of 32-bit entries pointed to by data
  223. * @return Check sum value
  224. */
  225. uint32_t Chip_CRC_CRC32(const uint32_t *data, uint32_t words);
  226. /**
  227. * @}
  228. */
  229. #ifdef __cplusplus
  230. }
  231. #endif
  232. #endif /* __CRC_8XX_H_ */