fsl_crc.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief CRC driver version. Version 2.0.1.
  43. *
  44. * Current version: 2.0.1
  45. *
  46. * Change log:
  47. * - Version 2.0.1
  48. * - move DATA and DATALL macro definition from header file to source file
  49. */
  50. #define FSL_CRC_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
  51. /*@}*/
  52. #ifndef CRC_DRIVER_CUSTOM_DEFAULTS
  53. /*! @brief Default configuration structure filled by CRC_GetDefaultConfig(). Use CRC16-CCIT-FALSE as defeault. */
  54. #define CRC_DRIVER_USE_CRC16_CCIT_FALSE_AS_DEFAULT 1
  55. #endif
  56. /*! @brief CRC bit width */
  57. typedef enum _crc_bits
  58. {
  59. kCrcBits16 = 0U, /*!< Generate 16-bit CRC code */
  60. kCrcBits32 = 1U /*!< Generate 32-bit CRC code */
  61. } crc_bits_t;
  62. /*! @brief CRC result type */
  63. typedef enum _crc_result
  64. {
  65. kCrcFinalChecksum = 0U, /*!< CRC data register read value is the final checksum.
  66. Reflect out and final xor protocol features are applied. */
  67. kCrcIntermediateChecksum = 1U /*!< CRC data register read value is intermediate checksum (raw value).
  68. Reflect out and final xor protocol feature are not applied.
  69. Intermediate checksum can be used as a seed for CRC_Init()
  70. to continue adding data to this checksum. */
  71. } crc_result_t;
  72. /*!
  73. * @brief CRC protocol configuration.
  74. *
  75. * This structure holds the configuration for the CRC protocol.
  76. *
  77. */
  78. typedef struct _crc_config
  79. {
  80. uint32_t polynomial; /*!< CRC Polynomial, MSBit first.
  81. Example polynomial: 0x1021 = 1_0000_0010_0001 = x^12+x^5+1 */
  82. uint32_t seed; /*!< Starting checksum value */
  83. bool reflectIn; /*!< Reflect bits on input. */
  84. bool reflectOut; /*!< Reflect bits on output. */
  85. bool complementChecksum; /*!< True if the result shall be complement of the actual checksum. */
  86. crc_bits_t crcBits; /*!< Selects 16- or 32- bit CRC protocol. */
  87. crc_result_t crcResult; /*!< Selects final or intermediate checksum return from CRC_Get16bitResult() or
  88. CRC_Get32bitResult() */
  89. } crc_config_t;
  90. /*******************************************************************************
  91. * API
  92. ******************************************************************************/
  93. #if defined(__cplusplus)
  94. extern "C" {
  95. #endif
  96. /*!
  97. * @brief Enables and configures the CRC peripheral module.
  98. *
  99. * This function enables the clock gate in the SIM module for the CRC peripheral.
  100. * It also configures the CRC module and starts a checksum computation by writing the seed.
  101. *
  102. * @param base CRC peripheral address.
  103. * @param config CRC module configuration structure.
  104. */
  105. void CRC_Init(CRC_Type *base, const crc_config_t *config);
  106. /*!
  107. * @brief Disables the CRC peripheral module.
  108. *
  109. * This function disables the clock gate in the SIM module for the CRC peripheral.
  110. *
  111. * @param base CRC peripheral address.
  112. */
  113. static inline void CRC_Deinit(CRC_Type *base)
  114. {
  115. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  116. /* gate clock */
  117. CLOCK_DisableClock(kCLOCK_Crc0);
  118. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  119. }
  120. /*!
  121. * @brief Loads default values to the CRC protocol configuration structure.
  122. *
  123. * Loads default values to the CRC protocol configuration structure. The default values are as follows.
  124. * @code
  125. * config->polynomial = 0x1021;
  126. * config->seed = 0xFFFF;
  127. * config->reflectIn = false;
  128. * config->reflectOut = false;
  129. * config->complementChecksum = false;
  130. * config->crcBits = kCrcBits16;
  131. * config->crcResult = kCrcFinalChecksum;
  132. * @endcode
  133. *
  134. * @param config CRC protocol configuration structure.
  135. */
  136. void CRC_GetDefaultConfig(crc_config_t *config);
  137. /*!
  138. * @brief Writes data to the CRC module.
  139. *
  140. * Writes input data buffer bytes to the CRC data register.
  141. * The configured type of transpose is applied.
  142. *
  143. * @param base CRC peripheral address.
  144. * @param data Input data stream, MSByte in data[0].
  145. * @param dataSize Size in bytes of the input data buffer.
  146. */
  147. void CRC_WriteData(CRC_Type *base, const uint8_t *data, size_t dataSize);
  148. /*!
  149. * @brief Reads the 32-bit checksum from the CRC module.
  150. *
  151. * Reads the CRC data register (either an intermediate or the final checksum).
  152. * The configured type of transpose and complement is applied.
  153. *
  154. * @param base CRC peripheral address.
  155. * @return An intermediate or the final 32-bit checksum, after configured transpose and complement operations.
  156. */
  157. uint32_t CRC_Get32bitResult(CRC_Type *base);
  158. /*!
  159. * @brief Reads a 16-bit checksum from the CRC module.
  160. *
  161. * Reads the CRC data register (either an intermediate or the final checksum).
  162. * The configured type of transpose and complement is applied.
  163. *
  164. * @param base CRC peripheral address.
  165. * @return An intermediate or the final 16-bit checksum, after configured transpose and complement operations.
  166. */
  167. uint16_t CRC_Get16bitResult(CRC_Type *base);
  168. #if defined(__cplusplus)
  169. }
  170. #endif
  171. /*!
  172. *@}
  173. */
  174. #endif /* _FSL_CRC_H_ */