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