You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

323 rivejä
12 KiB

  1. /*
  2. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_crc.h"
  9. /*******************************************************************************
  10. * Definitions
  11. ******************************************************************************/
  12. /* Component ID definition, used by tools. */
  13. #ifndef FSL_COMPONENT_ID
  14. #define FSL_COMPONENT_ID "platform.drivers.crc"
  15. #endif
  16. /*! @internal @brief Has data register with name CRC. */
  17. #if defined(FSL_FEATURE_CRC_HAS_CRC_REG) && FSL_FEATURE_CRC_HAS_CRC_REG
  18. #define DATA CRC
  19. #define DATALL CRCLL
  20. #endif
  21. #if defined(CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT) && CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT
  22. /* @brief Default user configuration structure for CRC-16-CCITT */
  23. #define CRC_DRIVER_DEFAULT_POLYNOMIAL 0x1021U
  24. /*< CRC-16-CCIT polynomial x**16 + x**12 + x**5 + x**0 */
  25. #define CRC_DRIVER_DEFAULT_SEED 0xFFFFU
  26. /*< Default initial checksum */
  27. #define CRC_DRIVER_DEFAULT_REFLECT_IN false
  28. /*< Default is no transpose */
  29. #define CRC_DRIVER_DEFAULT_REFLECT_OUT false
  30. /*< Default is transpose bytes */
  31. #define CRC_DRIVER_DEFAULT_COMPLEMENT_CHECKSUM false
  32. /*< Default is without complement of CRC data register read data */
  33. #define CRC_DRIVER_DEFAULT_CRC_BITS kCrcBits16
  34. /*< Default is 16-bit CRC protocol */
  35. #define CRC_DRIVER_DEFAULT_CRC_RESULT kCrcFinalChecksum
  36. /*< Default is resutl type is final checksum */
  37. #endif /* CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT */
  38. /*! @brief CRC type of transpose of read write data */
  39. typedef enum _crc_transpose_type
  40. {
  41. kCrcTransposeNone = 0U, /*! No transpose */
  42. kCrcTransposeBits = 1U, /*! Tranpose bits in bytes */
  43. kCrcTransposeBitsAndBytes = 2U, /*! Transpose bytes and bits in bytes */
  44. kCrcTransposeBytes = 3U, /*! Transpose bytes */
  45. } crc_transpose_type_t;
  46. /*!
  47. * @brief CRC module configuration.
  48. *
  49. * This structure holds the configuration for the CRC module.
  50. */
  51. typedef struct _crc_module_config
  52. {
  53. uint32_t polynomial; /*!< CRC Polynomial, MSBit first.@n
  54. Example polynomial: 0x1021 = 1_0000_0010_0001 = x^12+x^5+1 */
  55. uint32_t seed; /*!< Starting checksum value */
  56. crc_transpose_type_t readTranspose; /*!< Type of transpose when reading CRC result. */
  57. crc_transpose_type_t writeTranspose; /*!< Type of transpose when writing CRC input data. */
  58. bool complementChecksum; /*!< True if the result shall be complement of the actual checksum. */
  59. crc_bits_t crcBits; /*!< Selects 16- or 32- bit CRC protocol. */
  60. } crc_module_config_t;
  61. /*******************************************************************************
  62. * Code
  63. ******************************************************************************/
  64. /*!
  65. * @brief Returns transpose type for CRC protocol reflect in parameter.
  66. *
  67. * This functions helps to set writeTranspose member of crc_config_t structure. Reflect in is CRC protocol parameter.
  68. *
  69. * @param enable True or false for the selected CRC protocol Reflect In (refin) parameter.
  70. */
  71. static inline crc_transpose_type_t CRC_GetTransposeTypeFromReflectIn(bool enable)
  72. {
  73. return ((enable) ? kCrcTransposeBitsAndBytes : kCrcTransposeBytes);
  74. }
  75. /*!
  76. * @brief Returns transpose type for CRC protocol reflect out parameter.
  77. *
  78. * This functions helps to set readTranspose member of crc_config_t structure. Reflect out is CRC protocol parameter.
  79. *
  80. * @param enable True or false for the selected CRC protocol Reflect Out (refout) parameter.
  81. */
  82. static inline crc_transpose_type_t CRC_GetTransposeTypeFromReflectOut(bool enable)
  83. {
  84. return ((enable) ? kCrcTransposeBitsAndBytes : kCrcTransposeNone);
  85. }
  86. /*!
  87. * @brief Starts checksum computation.
  88. *
  89. * Configures the CRC module for the specified CRC protocol. @n
  90. * Starts the checksum computation by writing the seed value
  91. *
  92. * @param base CRC peripheral address.
  93. * @param config Pointer to protocol configuration structure.
  94. */
  95. static void CRC_ConfigureAndStart(CRC_Type *base, const crc_module_config_t *config)
  96. {
  97. uint32_t crcControl;
  98. /* pre-compute value for CRC control registger based on user configuraton without WAS field */
  99. crcControl = 0U | CRC_CTRL_TOT(config->writeTranspose) | CRC_CTRL_TOTR(config->readTranspose) |
  100. CRC_CTRL_FXOR(config->complementChecksum) | CRC_CTRL_TCRC(config->crcBits);
  101. /* make sure the control register is clear - WAS is deasserted, and protocol is set */
  102. base->CTRL = crcControl;
  103. /* write polynomial register */
  104. base->GPOLY = config->polynomial;
  105. /* write pre-computed control register value along with WAS to start checksum computation */
  106. base->CTRL = crcControl | CRC_CTRL_WAS(true);
  107. /* write seed (initial checksum) */
  108. base->DATA = config->seed;
  109. /* deassert WAS by writing pre-computed CRC control register value */
  110. base->CTRL = crcControl;
  111. }
  112. /*!
  113. * @brief Starts final checksum computation.
  114. *
  115. * Configures the CRC module for the specified CRC protocol. @n
  116. * Starts final checksum computation by writing the seed value.
  117. * @note CRC_Get16bitResult() or CRC_Get32bitResult() return final checksum
  118. * (output reflection and xor functions are applied).
  119. *
  120. * @param base CRC peripheral address.
  121. * @param protocolConfig Pointer to protocol configuration structure.
  122. */
  123. static void CRC_SetProtocolConfig(CRC_Type *base, const crc_config_t *protocolConfig)
  124. {
  125. crc_module_config_t moduleConfig;
  126. /* convert protocol to CRC peripheral module configuration, prepare for final checksum */
  127. moduleConfig.polynomial = protocolConfig->polynomial;
  128. moduleConfig.seed = protocolConfig->seed;
  129. moduleConfig.readTranspose = CRC_GetTransposeTypeFromReflectOut(protocolConfig->reflectOut);
  130. moduleConfig.writeTranspose = CRC_GetTransposeTypeFromReflectIn(protocolConfig->reflectIn);
  131. moduleConfig.complementChecksum = protocolConfig->complementChecksum;
  132. moduleConfig.crcBits = protocolConfig->crcBits;
  133. CRC_ConfigureAndStart(base, &moduleConfig);
  134. }
  135. /*!
  136. * @brief Starts intermediate checksum computation.
  137. *
  138. * Configures the CRC module for the specified CRC protocol. @n
  139. * Starts intermediate checksum computation by writing the seed value.
  140. * @note CRC_Get16bitResult() or CRC_Get32bitResult() return intermediate checksum (raw data register value).
  141. *
  142. * @param base CRC peripheral address.
  143. * @param protocolConfig Pointer to protocol configuration structure.
  144. */
  145. static void CRC_SetRawProtocolConfig(CRC_Type *base, const crc_config_t *protocolConfig)
  146. {
  147. crc_module_config_t moduleConfig;
  148. /* convert protocol to CRC peripheral module configuration, prepare for intermediate checksum */
  149. moduleConfig.polynomial = protocolConfig->polynomial;
  150. moduleConfig.seed = protocolConfig->seed;
  151. moduleConfig.readTranspose =
  152. kCrcTransposeNone; /* intermediate checksum does no transpose of data register read value */
  153. moduleConfig.writeTranspose = CRC_GetTransposeTypeFromReflectIn(protocolConfig->reflectIn);
  154. moduleConfig.complementChecksum = false; /* intermediate checksum does no xor of data register read value */
  155. moduleConfig.crcBits = protocolConfig->crcBits;
  156. CRC_ConfigureAndStart(base, &moduleConfig);
  157. }
  158. /*!
  159. * brief Enables and configures the CRC peripheral module.
  160. *
  161. * This function enables the clock gate in the SIM module for the CRC peripheral.
  162. * It also configures the CRC module and starts a checksum computation by writing the seed.
  163. *
  164. * param base CRC peripheral address.
  165. * param config CRC module configuration structure.
  166. */
  167. void CRC_Init(CRC_Type *base, const crc_config_t *config)
  168. {
  169. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  170. /* ungate clock */
  171. CLOCK_EnableClock(kCLOCK_Crc0);
  172. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  173. /* configure CRC module and write the seed */
  174. if (config->crcResult == kCrcFinalChecksum)
  175. {
  176. CRC_SetProtocolConfig(base, config);
  177. }
  178. else
  179. {
  180. CRC_SetRawProtocolConfig(base, config);
  181. }
  182. }
  183. /*!
  184. * brief Loads default values to the CRC protocol configuration structure.
  185. *
  186. * Loads default values to the CRC protocol configuration structure. The default values are as follows.
  187. * code
  188. * config->polynomial = 0x1021;
  189. * config->seed = 0xFFFF;
  190. * config->reflectIn = false;
  191. * config->reflectOut = false;
  192. * config->complementChecksum = false;
  193. * config->crcBits = kCrcBits16;
  194. * config->crcResult = kCrcFinalChecksum;
  195. * endcode
  196. *
  197. * param config CRC protocol configuration structure.
  198. */
  199. void CRC_GetDefaultConfig(crc_config_t *config)
  200. {
  201. /* Initializes the configure structure to zero. */
  202. (void)memset(config, 0, sizeof(*config));
  203. static const crc_config_t crc16ccit = {
  204. CRC_DRIVER_DEFAULT_POLYNOMIAL, CRC_DRIVER_DEFAULT_SEED,
  205. CRC_DRIVER_DEFAULT_REFLECT_IN, CRC_DRIVER_DEFAULT_REFLECT_OUT,
  206. CRC_DRIVER_DEFAULT_COMPLEMENT_CHECKSUM, CRC_DRIVER_DEFAULT_CRC_BITS,
  207. CRC_DRIVER_DEFAULT_CRC_RESULT,
  208. };
  209. *config = crc16ccit;
  210. }
  211. /*!
  212. * brief Writes data to the CRC module.
  213. *
  214. * Writes input data buffer bytes to the CRC data register.
  215. * The configured type of transpose is applied.
  216. *
  217. * param base CRC peripheral address.
  218. * param data Input data stream, MSByte in data[0].
  219. * param dataSize Size in bytes of the input data buffer.
  220. */
  221. void CRC_WriteData(CRC_Type *base, const uint8_t *data, size_t dataSize)
  222. {
  223. const uint32_t *data32;
  224. /* 8-bit reads and writes till source address is aligned 4 bytes */
  225. while ((dataSize) && ((uint32_t)data & 3U))
  226. {
  227. base->ACCESS8BIT.DATALL = *data;
  228. data++;
  229. dataSize--;
  230. }
  231. /* use 32-bit reads and writes as long as possible */
  232. data32 = (const uint32_t *)data;
  233. while (dataSize >= sizeof(uint32_t))
  234. {
  235. base->DATA = *data32;
  236. data32++;
  237. dataSize -= sizeof(uint32_t);
  238. }
  239. data = (const uint8_t *)data32;
  240. /* 8-bit reads and writes till end of data buffer */
  241. while (dataSize != 0U)
  242. {
  243. base->ACCESS8BIT.DATALL = *data;
  244. data++;
  245. dataSize--;
  246. }
  247. }
  248. /*!
  249. * brief Reads the 32-bit checksum from the CRC module.
  250. *
  251. * Reads the CRC data register (either an intermediate or the final checksum).
  252. * The configured type of transpose and complement is applied.
  253. *
  254. * param base CRC peripheral address.
  255. * return An intermediate or the final 32-bit checksum, after configured transpose and complement operations.
  256. */
  257. uint32_t CRC_Get32bitResult(CRC_Type *base)
  258. {
  259. return base->DATA;
  260. }
  261. /*!
  262. * brief Reads a 16-bit checksum from the CRC module.
  263. *
  264. * Reads the CRC data register (either an intermediate or the final checksum).
  265. * The configured type of transpose and complement is applied.
  266. *
  267. * param base CRC peripheral address.
  268. * return An intermediate or the final 16-bit checksum, after configured transpose and complement operations.
  269. */
  270. uint16_t CRC_Get16bitResult(CRC_Type *base)
  271. {
  272. uint32_t retval;
  273. uint32_t totr; /* type of transpose read bitfield */
  274. retval = base->DATA;
  275. totr = (base->CTRL & CRC_CTRL_TOTR_MASK) >> CRC_CTRL_TOTR_SHIFT;
  276. /* check transpose type to get 16-bit out of 32-bit register */
  277. if (totr >= 2U)
  278. {
  279. /* transpose of bytes for read is set, the result CRC is in CRC_DATA[HU:HL] */
  280. retval &= 0xFFFF0000U;
  281. retval = retval >> 16U;
  282. }
  283. else
  284. {
  285. /* no transpose of bytes for read, the result CRC is in CRC_DATA[LU:LL] */
  286. retval &= 0x0000FFFFU;
  287. }
  288. return (uint16_t)retval;
  289. }