fsl_crc.c 11 KB

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