fsl_crc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. /*! @internal @brief Has data register with name CRC. */
  39. #if defined(FSL_FEATURE_CRC_HAS_CRC_REG) && FSL_FEATURE_CRC_HAS_CRC_REG
  40. #define DATA CRC
  41. #define DATALL CRCLL
  42. #endif
  43. #if defined(CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT) && CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT
  44. /* @brief Default user configuration structure for CRC-16-CCITT */
  45. #define CRC_DRIVER_DEFAULT_POLYNOMIAL 0x1021U
  46. /*< CRC-16-CCIT polynomial x**16 + x**12 + x**5 + x**0 */
  47. #define CRC_DRIVER_DEFAULT_SEED 0xFFFFU
  48. /*< Default initial checksum */
  49. #define CRC_DRIVER_DEFAULT_REFLECT_IN false
  50. /*< Default is no transpose */
  51. #define CRC_DRIVER_DEFAULT_REFLECT_OUT false
  52. /*< Default is transpose bytes */
  53. #define CRC_DRIVER_DEFAULT_COMPLEMENT_CHECKSUM false
  54. /*< Default is without complement of CRC data register read data */
  55. #define CRC_DRIVER_DEFAULT_CRC_BITS kCrcBits16
  56. /*< Default is 16-bit CRC protocol */
  57. #define CRC_DRIVER_DEFAULT_CRC_RESULT kCrcFinalChecksum
  58. /*< Default is resutl type is final checksum */
  59. #endif /* CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT */
  60. /*! @brief CRC type of transpose of read write data */
  61. typedef enum _crc_transpose_type
  62. {
  63. kCrcTransposeNone = 0U, /*! No transpose */
  64. kCrcTransposeBits = 1U, /*! Tranpose bits in bytes */
  65. kCrcTransposeBitsAndBytes = 2U, /*! Transpose bytes and bits in bytes */
  66. kCrcTransposeBytes = 3U, /*! Transpose bytes */
  67. } crc_transpose_type_t;
  68. /*!
  69. * @brief CRC module configuration.
  70. *
  71. * This structure holds the configuration for the CRC module.
  72. */
  73. typedef struct _crc_module_config
  74. {
  75. uint32_t polynomial; /*!< CRC Polynomial, MSBit first.@n
  76. Example polynomial: 0x1021 = 1_0000_0010_0001 = x^12+x^5+1 */
  77. uint32_t seed; /*!< Starting checksum value */
  78. crc_transpose_type_t readTranspose; /*!< Type of transpose when reading CRC result. */
  79. crc_transpose_type_t writeTranspose; /*!< Type of transpose when writing CRC input data. */
  80. bool complementChecksum; /*!< True if the result shall be complement of the actual checksum. */
  81. crc_bits_t crcBits; /*!< Selects 16- or 32- bit CRC protocol. */
  82. } crc_module_config_t;
  83. /*******************************************************************************
  84. * Code
  85. ******************************************************************************/
  86. /*!
  87. * @brief Returns transpose type for CRC protocol reflect in parameter.
  88. *
  89. * This functions helps to set writeTranspose member of crc_config_t structure. Reflect in is CRC protocol parameter.
  90. *
  91. * @param enable True or false for the selected CRC protocol Reflect In (refin) parameter.
  92. */
  93. static inline crc_transpose_type_t CRC_GetTransposeTypeFromReflectIn(bool enable)
  94. {
  95. return ((enable) ? kCrcTransposeBitsAndBytes : kCrcTransposeBytes);
  96. }
  97. /*!
  98. * @brief Returns transpose type for CRC protocol reflect out parameter.
  99. *
  100. * This functions helps to set readTranspose member of crc_config_t structure. Reflect out is CRC protocol parameter.
  101. *
  102. * @param enable True or false for the selected CRC protocol Reflect Out (refout) parameter.
  103. */
  104. static inline crc_transpose_type_t CRC_GetTransposeTypeFromReflectOut(bool enable)
  105. {
  106. return ((enable) ? kCrcTransposeBitsAndBytes : kCrcTransposeNone);
  107. }
  108. /*!
  109. * @brief Starts checksum computation.
  110. *
  111. * Configures the CRC module for the specified CRC protocol. @n
  112. * Starts the checksum computation by writing the seed value
  113. *
  114. * @param base CRC peripheral address.
  115. * @param config Pointer to protocol configuration structure.
  116. */
  117. static void CRC_ConfigureAndStart(CRC_Type *base, const crc_module_config_t *config)
  118. {
  119. uint32_t crcControl;
  120. /* pre-compute value for CRC control registger based on user configuraton without WAS field */
  121. crcControl = 0 | CRC_CTRL_TOT(config->writeTranspose) | CRC_CTRL_TOTR(config->readTranspose) |
  122. CRC_CTRL_FXOR(config->complementChecksum) | CRC_CTRL_TCRC(config->crcBits);
  123. /* make sure the control register is clear - WAS is deasserted, and protocol is set */
  124. base->CTRL = crcControl;
  125. /* write polynomial register */
  126. base->GPOLY = config->polynomial;
  127. /* write pre-computed control register value along with WAS to start checksum computation */
  128. base->CTRL = crcControl | CRC_CTRL_WAS(true);
  129. /* write seed (initial checksum) */
  130. base->DATA = config->seed;
  131. /* deassert WAS by writing pre-computed CRC control register value */
  132. base->CTRL = crcControl;
  133. }
  134. /*!
  135. * @brief Starts final checksum computation.
  136. *
  137. * Configures the CRC module for the specified CRC protocol. @n
  138. * Starts final checksum computation by writing the seed value.
  139. * @note CRC_Get16bitResult() or CRC_Get32bitResult() return final checksum
  140. * (output reflection and xor functions are applied).
  141. *
  142. * @param base CRC peripheral address.
  143. * @param protocolConfig Pointer to protocol configuration structure.
  144. */
  145. static void CRC_SetProtocolConfig(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 final checksum */
  149. moduleConfig.polynomial = protocolConfig->polynomial;
  150. moduleConfig.seed = protocolConfig->seed;
  151. moduleConfig.readTranspose = CRC_GetTransposeTypeFromReflectOut(protocolConfig->reflectOut);
  152. moduleConfig.writeTranspose = CRC_GetTransposeTypeFromReflectIn(protocolConfig->reflectIn);
  153. moduleConfig.complementChecksum = protocolConfig->complementChecksum;
  154. moduleConfig.crcBits = protocolConfig->crcBits;
  155. CRC_ConfigureAndStart(base, &moduleConfig);
  156. }
  157. /*!
  158. * @brief Starts intermediate checksum computation.
  159. *
  160. * Configures the CRC module for the specified CRC protocol. @n
  161. * Starts intermediate checksum computation by writing the seed value.
  162. * @note CRC_Get16bitResult() or CRC_Get32bitResult() return intermediate checksum (raw data register value).
  163. *
  164. * @param base CRC peripheral address.
  165. * @param protocolConfig Pointer to protocol configuration structure.
  166. */
  167. static void CRC_SetRawProtocolConfig(CRC_Type *base, const crc_config_t *protocolConfig)
  168. {
  169. crc_module_config_t moduleConfig;
  170. /* convert protocol to CRC peripheral module configuration, prepare for intermediate checksum */
  171. moduleConfig.polynomial = protocolConfig->polynomial;
  172. moduleConfig.seed = protocolConfig->seed;
  173. moduleConfig.readTranspose =
  174. kCrcTransposeNone; /* intermediate checksum does no transpose of data register read value */
  175. moduleConfig.writeTranspose = CRC_GetTransposeTypeFromReflectIn(protocolConfig->reflectIn);
  176. moduleConfig.complementChecksum = false; /* intermediate checksum does no xor of data register read value */
  177. moduleConfig.crcBits = protocolConfig->crcBits;
  178. CRC_ConfigureAndStart(base, &moduleConfig);
  179. }
  180. void CRC_Init(CRC_Type *base, const crc_config_t *config)
  181. {
  182. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  183. /* ungate clock */
  184. CLOCK_EnableClock(kCLOCK_Crc0);
  185. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  186. /* configure CRC module and write the seed */
  187. if (config->crcResult == kCrcFinalChecksum)
  188. {
  189. CRC_SetProtocolConfig(base, config);
  190. }
  191. else
  192. {
  193. CRC_SetRawProtocolConfig(base, config);
  194. }
  195. }
  196. void CRC_GetDefaultConfig(crc_config_t *config)
  197. {
  198. static const crc_config_t crc16ccit = {
  199. CRC_DRIVER_DEFAULT_POLYNOMIAL, CRC_DRIVER_DEFAULT_SEED,
  200. CRC_DRIVER_DEFAULT_REFLECT_IN, CRC_DRIVER_DEFAULT_REFLECT_OUT,
  201. CRC_DRIVER_DEFAULT_COMPLEMENT_CHECKSUM, CRC_DRIVER_DEFAULT_CRC_BITS,
  202. CRC_DRIVER_DEFAULT_CRC_RESULT,
  203. };
  204. *config = crc16ccit;
  205. }
  206. void CRC_WriteData(CRC_Type *base, const uint8_t *data, size_t dataSize)
  207. {
  208. const uint32_t *data32;
  209. /* 8-bit reads and writes till source address is aligned 4 bytes */
  210. while ((dataSize) && ((uint32_t)data & 3U))
  211. {
  212. base->ACCESS8BIT.DATALL = *data;
  213. data++;
  214. dataSize--;
  215. }
  216. /* use 32-bit reads and writes as long as possible */
  217. data32 = (const uint32_t *)data;
  218. while (dataSize >= sizeof(uint32_t))
  219. {
  220. base->DATA = *data32;
  221. data32++;
  222. dataSize -= sizeof(uint32_t);
  223. }
  224. data = (const uint8_t *)data32;
  225. /* 8-bit reads and writes till end of data buffer */
  226. while (dataSize)
  227. {
  228. base->ACCESS8BIT.DATALL = *data;
  229. data++;
  230. dataSize--;
  231. }
  232. }
  233. uint32_t CRC_Get32bitResult(CRC_Type *base)
  234. {
  235. return base->DATA;
  236. }
  237. uint16_t CRC_Get16bitResult(CRC_Type *base)
  238. {
  239. uint32_t retval;
  240. uint32_t totr; /* type of transpose read bitfield */
  241. retval = base->DATA;
  242. totr = (base->CTRL & CRC_CTRL_TOTR_MASK) >> CRC_CTRL_TOTR_SHIFT;
  243. /* check transpose type to get 16-bit out of 32-bit register */
  244. if (totr >= 2U)
  245. {
  246. /* transpose of bytes for read is set, the result CRC is in CRC_DATA[HU:HL] */
  247. retval &= 0xFFFF0000U;
  248. retval = retval >> 16U;
  249. }
  250. else
  251. {
  252. /* no transpose of bytes for read, the result CRC is in CRC_DATA[LU:LL] */
  253. retval &= 0x0000FFFFU;
  254. }
  255. return (uint16_t)retval;
  256. }