fsl_crc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "fsl_crc.h"
  31. /*******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. /*! @internal @brief Has data register with name CRC. */
  35. #if defined(FSL_FEATURE_CRC_HAS_CRC_REG) && FSL_FEATURE_CRC_HAS_CRC_REG
  36. #define DATA CRC
  37. #define DATALL CRCLL
  38. #endif
  39. #if defined(CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT) && CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT
  40. /* @brief Default user configuration structure for CRC-16-CCITT */
  41. #define CRC_DRIVER_DEFAULT_POLYNOMIAL 0x1021U
  42. /*< CRC-16-CCIT polynomial x**16 + x**12 + x**5 + x**0 */
  43. #define CRC_DRIVER_DEFAULT_SEED 0xFFFFU
  44. /*< Default initial checksum */
  45. #define CRC_DRIVER_DEFAULT_REFLECT_IN false
  46. /*< Default is no transpose */
  47. #define CRC_DRIVER_DEFAULT_REFLECT_OUT false
  48. /*< Default is transpose bytes */
  49. #define CRC_DRIVER_DEFAULT_COMPLEMENT_CHECKSUM false
  50. /*< Default is without complement of CRC data register read data */
  51. #define CRC_DRIVER_DEFAULT_CRC_BITS kCrcBits16
  52. /*< Default is 16-bit CRC protocol */
  53. #define CRC_DRIVER_DEFAULT_CRC_RESULT kCrcFinalChecksum
  54. /*< Default is resutl type is final checksum */
  55. #endif /* CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT */
  56. /*! @brief CRC type of transpose of read write data */
  57. typedef enum _crc_transpose_type
  58. {
  59. kCrcTransposeNone = 0U, /*! No transpose */
  60. kCrcTransposeBits = 1U, /*! Tranpose bits in bytes */
  61. kCrcTransposeBitsAndBytes = 2U, /*! Transpose bytes and bits in bytes */
  62. kCrcTransposeBytes = 3U, /*! Transpose bytes */
  63. } crc_transpose_type_t;
  64. /*!
  65. * @brief CRC module configuration.
  66. *
  67. * This structure holds the configuration for the CRC module.
  68. */
  69. typedef struct _crc_module_config
  70. {
  71. uint32_t polynomial; /*!< CRC Polynomial, MSBit first.@n
  72. Example polynomial: 0x1021 = 1_0000_0010_0001 = x^12+x^5+1 */
  73. uint32_t seed; /*!< Starting checksum value */
  74. crc_transpose_type_t readTranspose; /*!< Type of transpose when reading CRC result. */
  75. crc_transpose_type_t writeTranspose; /*!< Type of transpose when writing CRC input data. */
  76. bool complementChecksum; /*!< True if the result shall be complement of the actual checksum. */
  77. crc_bits_t crcBits; /*!< Selects 16- or 32- bit CRC protocol. */
  78. } crc_module_config_t;
  79. /*******************************************************************************
  80. * Code
  81. ******************************************************************************/
  82. /*!
  83. * @brief Returns transpose type for CRC protocol reflect in parameter.
  84. *
  85. * This functions helps to set writeTranspose member of crc_config_t structure. Reflect in is CRC protocol parameter.
  86. *
  87. * @param enable True or false for the selected CRC protocol Reflect In (refin) parameter.
  88. */
  89. static inline crc_transpose_type_t CRC_GetTransposeTypeFromReflectIn(bool enable)
  90. {
  91. return ((enable) ? kCrcTransposeBitsAndBytes : kCrcTransposeBytes);
  92. }
  93. /*!
  94. * @brief Returns transpose type for CRC protocol reflect out parameter.
  95. *
  96. * This functions helps to set readTranspose member of crc_config_t structure. Reflect out is CRC protocol parameter.
  97. *
  98. * @param enable True or false for the selected CRC protocol Reflect Out (refout) parameter.
  99. */
  100. static inline crc_transpose_type_t CRC_GetTransposeTypeFromReflectOut(bool enable)
  101. {
  102. return ((enable) ? kCrcTransposeBitsAndBytes : kCrcTransposeNone);
  103. }
  104. /*!
  105. * @brief Starts checksum computation.
  106. *
  107. * Configures the CRC module for the specified CRC protocol. @n
  108. * Starts the checksum computation by writing the seed value
  109. *
  110. * @param base CRC peripheral address.
  111. * @param config Pointer to protocol configuration structure.
  112. */
  113. static void CRC_ConfigureAndStart(CRC_Type *base, const crc_module_config_t *config)
  114. {
  115. uint32_t crcControl;
  116. /* pre-compute value for CRC control registger based on user configuraton without WAS field */
  117. crcControl = 0 | CRC_CTRL_TOT(config->writeTranspose) | CRC_CTRL_TOTR(config->readTranspose) |
  118. CRC_CTRL_FXOR(config->complementChecksum) | CRC_CTRL_TCRC(config->crcBits);
  119. /* make sure the control register is clear - WAS is deasserted, and protocol is set */
  120. base->CTRL = crcControl;
  121. /* write polynomial register */
  122. base->GPOLY = config->polynomial;
  123. /* write pre-computed control register value along with WAS to start checksum computation */
  124. base->CTRL = crcControl | CRC_CTRL_WAS(true);
  125. /* write seed (initial checksum) */
  126. base->DATA = config->seed;
  127. /* deassert WAS by writing pre-computed CRC control register value */
  128. base->CTRL = crcControl;
  129. }
  130. /*!
  131. * @brief Starts final checksum computation.
  132. *
  133. * Configures the CRC module for the specified CRC protocol. @n
  134. * Starts final checksum computation by writing the seed value.
  135. * @note CRC_Get16bitResult() or CRC_Get32bitResult() return final checksum
  136. * (output reflection and xor functions are applied).
  137. *
  138. * @param base CRC peripheral address.
  139. * @param protocolConfig Pointer to protocol configuration structure.
  140. */
  141. static void CRC_SetProtocolConfig(CRC_Type *base, const crc_config_t *protocolConfig)
  142. {
  143. crc_module_config_t moduleConfig;
  144. /* convert protocol to CRC peripheral module configuration, prepare for final checksum */
  145. moduleConfig.polynomial = protocolConfig->polynomial;
  146. moduleConfig.seed = protocolConfig->seed;
  147. moduleConfig.readTranspose = CRC_GetTransposeTypeFromReflectOut(protocolConfig->reflectOut);
  148. moduleConfig.writeTranspose = CRC_GetTransposeTypeFromReflectIn(protocolConfig->reflectIn);
  149. moduleConfig.complementChecksum = protocolConfig->complementChecksum;
  150. moduleConfig.crcBits = protocolConfig->crcBits;
  151. CRC_ConfigureAndStart(base, &moduleConfig);
  152. }
  153. /*!
  154. * @brief Starts intermediate checksum computation.
  155. *
  156. * Configures the CRC module for the specified CRC protocol. @n
  157. * Starts intermediate checksum computation by writing the seed value.
  158. * @note CRC_Get16bitResult() or CRC_Get32bitResult() return intermediate checksum (raw data register value).
  159. *
  160. * @param base CRC peripheral address.
  161. * @param protocolConfig Pointer to protocol configuration structure.
  162. */
  163. static void CRC_SetRawProtocolConfig(CRC_Type *base, const crc_config_t *protocolConfig)
  164. {
  165. crc_module_config_t moduleConfig;
  166. /* convert protocol to CRC peripheral module configuration, prepare for intermediate checksum */
  167. moduleConfig.polynomial = protocolConfig->polynomial;
  168. moduleConfig.seed = protocolConfig->seed;
  169. moduleConfig.readTranspose =
  170. kCrcTransposeNone; /* intermediate checksum does no transpose of data register read value */
  171. moduleConfig.writeTranspose = CRC_GetTransposeTypeFromReflectIn(protocolConfig->reflectIn);
  172. moduleConfig.complementChecksum = false; /* intermediate checksum does no xor of data register read value */
  173. moduleConfig.crcBits = protocolConfig->crcBits;
  174. CRC_ConfigureAndStart(base, &moduleConfig);
  175. }
  176. void CRC_Init(CRC_Type *base, const crc_config_t *config)
  177. {
  178. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  179. /* ungate clock */
  180. CLOCK_EnableClock(kCLOCK_Crc0);
  181. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  182. /* configure CRC module and write the seed */
  183. if (config->crcResult == kCrcFinalChecksum)
  184. {
  185. CRC_SetProtocolConfig(base, config);
  186. }
  187. else
  188. {
  189. CRC_SetRawProtocolConfig(base, config);
  190. }
  191. }
  192. void CRC_GetDefaultConfig(crc_config_t *config)
  193. {
  194. static const crc_config_t crc16ccit = {
  195. CRC_DRIVER_DEFAULT_POLYNOMIAL, CRC_DRIVER_DEFAULT_SEED,
  196. CRC_DRIVER_DEFAULT_REFLECT_IN, CRC_DRIVER_DEFAULT_REFLECT_OUT,
  197. CRC_DRIVER_DEFAULT_COMPLEMENT_CHECKSUM, CRC_DRIVER_DEFAULT_CRC_BITS,
  198. CRC_DRIVER_DEFAULT_CRC_RESULT,
  199. };
  200. *config = crc16ccit;
  201. }
  202. void CRC_WriteData(CRC_Type *base, const uint8_t *data, size_t dataSize)
  203. {
  204. const uint32_t *data32;
  205. /* 8-bit reads and writes till source address is aligned 4 bytes */
  206. while ((dataSize) && ((uint32_t)data & 3U))
  207. {
  208. base->ACCESS8BIT.DATALL = *data;
  209. data++;
  210. dataSize--;
  211. }
  212. /* use 32-bit reads and writes as long as possible */
  213. data32 = (const uint32_t *)data;
  214. while (dataSize >= sizeof(uint32_t))
  215. {
  216. base->DATA = *data32;
  217. data32++;
  218. dataSize -= sizeof(uint32_t);
  219. }
  220. data = (const uint8_t *)data32;
  221. /* 8-bit reads and writes till end of data buffer */
  222. while (dataSize)
  223. {
  224. base->ACCESS8BIT.DATALL = *data;
  225. data++;
  226. dataSize--;
  227. }
  228. }
  229. uint32_t CRC_Get32bitResult(CRC_Type *base)
  230. {
  231. return base->DATA;
  232. }
  233. uint16_t CRC_Get16bitResult(CRC_Type *base)
  234. {
  235. uint32_t retval;
  236. uint32_t totr; /* type of transpose read bitfield */
  237. retval = base->DATA;
  238. totr = (base->CTRL & CRC_CTRL_TOTR_MASK) >> CRC_CTRL_TOTR_SHIFT;
  239. /* check transpose type to get 16-bit out of 32-bit register */
  240. if (totr >= 2U)
  241. {
  242. /* transpose of bytes for read is set, the result CRC is in CRC_DATA[HU:HL] */
  243. retval &= 0xFFFF0000U;
  244. retval = retval >> 16U;
  245. }
  246. else
  247. {
  248. /* no transpose of bytes for read, the result CRC is in CRC_DATA[LU:LL] */
  249. retval &= 0x0000FFFFU;
  250. }
  251. return (uint16_t)retval;
  252. }