fsl_crc.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #ifndef _FSL_CRC_H_
  31. #define _FSL_CRC_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup crc
  35. * @{
  36. */
  37. /*! @file */
  38. /*******************************************************************************
  39. * Definitions
  40. ******************************************************************************/
  41. /*! @name Driver version */
  42. /*@{*/
  43. /*! @brief CRC driver version. Version 2.0.1.
  44. *
  45. * Current version: 2.0.1
  46. *
  47. * Change log:
  48. * - Version 2.0.0
  49. * - initial version
  50. * - Version 2.0.1
  51. * - add explicit type cast when writing to WR_DATA
  52. */
  53. #define FSL_CRC_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
  54. /*@}*/
  55. #ifndef CRC_DRIVER_CUSTOM_DEFAULTS
  56. /*! @brief Default configuration structure filled by CRC_GetDefaultConfig(). Uses CRC-16/CCITT-FALSE as default. */
  57. #define CRC_DRIVER_USE_CRC16_CCITT_FALSE_AS_DEFAULT 1
  58. #endif
  59. /*! @brief CRC polynomials to use. */
  60. typedef enum _crc_polynomial
  61. {
  62. kCRC_Polynomial_CRC_CCITT = 0U, /*!< x^16+x^12+x^5+1 */
  63. kCRC_Polynomial_CRC_16 = 1U, /*!< x^16+x^15+x^2+1 */
  64. kCRC_Polynomial_CRC_32 = 2U /*!< x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1 */
  65. } crc_polynomial_t;
  66. /*!
  67. * @brief CRC protocol configuration.
  68. *
  69. * This structure holds the configuration for the CRC protocol.
  70. *
  71. */
  72. typedef struct _crc_config
  73. {
  74. crc_polynomial_t polynomial; /*!< CRC polynomial. */
  75. bool reverseIn; /*!< Reverse bits on input. */
  76. bool complementIn; /*!< Perform 1's complement on input. */
  77. bool reverseOut; /*!< Reverse bits on output. */
  78. bool complementOut; /*!< Perform 1's complement on output. */
  79. uint32_t seed; /*!< Starting checksum value. */
  80. } crc_config_t;
  81. /*******************************************************************************
  82. * API
  83. ******************************************************************************/
  84. #if defined(__cplusplus)
  85. extern "C" {
  86. #endif
  87. /*!
  88. * @brief Enables and configures the CRC peripheral module.
  89. *
  90. * This functions enables the CRC peripheral clock in the LPC SYSCON block.
  91. * It also configures the CRC engine and starts checksum computation by writing the seed.
  92. *
  93. * @param base CRC peripheral address.
  94. * @param config CRC module configuration structure.
  95. */
  96. void CRC_Init(CRC_Type *base, const crc_config_t *config);
  97. /*!
  98. * @brief Disables the CRC peripheral module.
  99. *
  100. * This functions disables the CRC peripheral clock in the LPC SYSCON block.
  101. *
  102. * @param base CRC peripheral address.
  103. */
  104. static inline void CRC_Deinit(CRC_Type *base)
  105. {
  106. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  107. /* disable clock to CRC */
  108. CLOCK_DisableClock(kCLOCK_Crc);
  109. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  110. }
  111. /*!
  112. * @brief resets CRC peripheral module.
  113. *
  114. * @param base CRC peripheral address.
  115. */
  116. void CRC_Reset(CRC_Type *base);
  117. /*!
  118. * @brief Loads default values to CRC protocol configuration structure.
  119. *
  120. * Loads default values to CRC protocol configuration structure. The default values are:
  121. * @code
  122. * config->polynomial = kCRC_Polynomial_CRC_CCITT;
  123. * config->reverseIn = false;
  124. * config->complementIn = false;
  125. * config->reverseOut = false;
  126. * config->complementOut = false;
  127. * config->seed = 0xFFFFU;
  128. * @endcode
  129. *
  130. * @param config CRC protocol configuration structure
  131. */
  132. void CRC_GetDefaultConfig(crc_config_t *config);
  133. /*!
  134. * @brief Loads actual values configured in CRC peripheral to CRC protocol configuration structure.
  135. *
  136. * The values, including seed, can be used to resume CRC calculation later.
  137. * @param base CRC peripheral address.
  138. * @param config CRC protocol configuration structure
  139. */
  140. void CRC_GetConfig(CRC_Type *base, crc_config_t *config);
  141. /*!
  142. * @brief Writes data to the CRC module.
  143. *
  144. * Writes input data buffer bytes to CRC data register.
  145. *
  146. * @param base CRC peripheral address.
  147. * @param data Input data stream, MSByte in data[0].
  148. * @param dataSize Size of the input data buffer in bytes.
  149. */
  150. void CRC_WriteData(CRC_Type *base, const uint8_t *data, size_t dataSize);
  151. /*!
  152. * @brief Reads 32-bit checksum from the CRC module.
  153. *
  154. * Reads CRC data register.
  155. *
  156. * @param base CRC peripheral address.
  157. * @return final 32-bit checksum, after configured bit reverse and complement operations.
  158. */
  159. static inline uint32_t CRC_Get32bitResult(CRC_Type *base)
  160. {
  161. return base->SUM;
  162. }
  163. /*!
  164. * @brief Reads 16-bit checksum from the CRC module.
  165. *
  166. * Reads CRC data register.
  167. *
  168. * @param base CRC peripheral address.
  169. * @return final 16-bit checksum, after configured bit reverse and complement operations.
  170. */
  171. static inline uint16_t CRC_Get16bitResult(CRC_Type *base)
  172. {
  173. return (uint16_t)base->SUM;
  174. }
  175. #if defined(__cplusplus)
  176. }
  177. #endif
  178. /*!
  179. *@}
  180. */
  181. #endif /* _FSL_CRC_H_ */