fsl_crc.h 6.4 KB

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