1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2019 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #ifndef FSL_CMP_H_
10 #define FSL_CMP_H_
11
12 #include "fsl_common.h"
13
14 /*!
15 * @addtogroup cmp
16 * @{
17 */
18
19 /*******************************************************************************
20 * Definitions
21 ******************************************************************************/
22
23 /*! @name Driver version */
24 /*! @{ */
25 /*! @brief CMP driver version 2.0.3. */
26 #define FSL_CMP_DRIVER_VERSION (MAKE_VERSION(2, 0, 3))
27 /*! @} */
28
29 /*!
30 * @brief Interrupt enable/disable mask.
31 */
32 enum _cmp_interrupt_enable
33 {
34 kCMP_OutputRisingInterruptEnable = CMP_SCR_IER_MASK, /*!< Comparator interrupt enable rising. */
35 kCMP_OutputFallingInterruptEnable = CMP_SCR_IEF_MASK, /*!< Comparator interrupt enable falling. */
36 };
37
38 /*!
39 * @brief Status flags' mask.
40 */
41 enum _cmp_status_flags
42 {
43 kCMP_OutputRisingEventFlag = CMP_SCR_CFR_MASK, /*!< Rising-edge on the comparison output has occurred. */
44 kCMP_OutputFallingEventFlag = CMP_SCR_CFF_MASK, /*!< Falling-edge on the comparison output has occurred. */
45 kCMP_OutputAssertEventFlag = CMP_SCR_COUT_MASK, /*!< Return the current value of the analog comparator output. */
46 };
47
48 /*!
49 * @brief CMP Hysteresis mode.
50 */
51 typedef enum _cmp_hysteresis_mode
52 {
53 kCMP_HysteresisLevel0 = 0U, /*!< Hysteresis level 0. */
54 kCMP_HysteresisLevel1 = 1U, /*!< Hysteresis level 1. */
55 kCMP_HysteresisLevel2 = 2U, /*!< Hysteresis level 2. */
56 kCMP_HysteresisLevel3 = 3U, /*!< Hysteresis level 3. */
57 } cmp_hysteresis_mode_t;
58
59 /*!
60 * @brief CMP Voltage Reference source.
61 */
62 typedef enum _cmp_reference_voltage_source
63 {
64 kCMP_VrefSourceVin1 = 0U, /*!< Vin1 is selected as a resistor ladder network supply reference Vin. */
65 kCMP_VrefSourceVin2 = 1U, /*!< Vin2 is selected as a resistor ladder network supply reference Vin. */
66 } cmp_reference_voltage_source_t;
67
68 /*!
69 * @brief Configures the comparator.
70 */
71 typedef struct _cmp_config
72 {
73 bool enableCmp; /*!< Enable the CMP module. */
74 cmp_hysteresis_mode_t hysteresisMode; /*!< CMP Hysteresis mode. */
75 bool enableHighSpeed; /*!< Enable High-speed (HS) comparison mode. */
76 bool enableInvertOutput; /*!< Enable the inverted comparator output. */
77 bool useUnfilteredOutput; /*!< Set the compare output(COUT) to equal COUTA(true) or COUT(false). */
78 bool enablePinOut; /*!< The comparator output is available on the associated pin. */
79 #if defined(FSL_FEATURE_CMP_HAS_TRIGGER_MODE) && FSL_FEATURE_CMP_HAS_TRIGGER_MODE
80 bool enableTriggerMode; /*!< Enable the trigger mode. */
81 #endif /* FSL_FEATURE_CMP_HAS_TRIGGER_MODE */
82 } cmp_config_t;
83
84 /*!
85 * @brief Configures the filter.
86 */
87 typedef struct _cmp_filter_config
88 {
89 #if defined(FSL_FEATURE_CMP_HAS_EXTERNAL_SAMPLE_SUPPORT) && FSL_FEATURE_CMP_HAS_EXTERNAL_SAMPLE_SUPPORT
90 bool enableSample; /*!< Using the external SAMPLE as a sampling clock input or using a divided bus clock. */
91 #endif /* FSL_FEATURE_CMP_HAS_EXTERNAL_SAMPLE_SUPPORT */
92 uint8_t filterCount; /*!< Filter Sample Count. Available range is 1-7; 0 disables the filter.*/
93 uint8_t filterPeriod; /*!< Filter Sample Period. The divider to the bus clock. Available range is 0-255. */
94 } cmp_filter_config_t;
95
96 /*!
97 * @brief Configures the internal DAC.
98 */
99 typedef struct _cmp_dac_config
100 {
101 cmp_reference_voltage_source_t referenceVoltageSource; /*!< Supply voltage reference source. */
102 uint8_t DACValue; /*!< Value for the DAC Output Voltage. Available range is 0-63.*/
103 } cmp_dac_config_t;
104
105 #if defined(__cplusplus)
106 extern "C" {
107 #endif
108
109 /*******************************************************************************
110 * API
111 ******************************************************************************/
112
113 /*!
114 * @name Initialization
115 * @{
116 */
117
118 /*!
119 * @brief Initializes the CMP.
120 *
121 * This function initializes the CMP module. The operations included are as follows.
122 * - Enabling the clock for CMP module.
123 * - Configuring the comparator.
124 * - Enabling the CMP module.
125 * Note that for some devices, multiple CMP instances share the same clock gate. In this case, to enable the clock for
126 * any instance enables all CMPs. See the appropriate MCU reference manual for the clock assignment of the CMP.
127 *
128 * @param base CMP peripheral base address.
129 * @param config Pointer to the configuration structure.
130 */
131 void CMP_Init(CMP_Type *base, const cmp_config_t *config);
132
133 /*!
134 * @brief De-initializes the CMP module.
135 *
136 * This function de-initializes the CMP module. The operations included are as follows.
137 * - Disabling the CMP module.
138 * - Disabling the clock for CMP module.
139 *
140 * This function disables the clock for the CMP.
141 * Note that for some devices, multiple CMP instances share the same clock gate. In this case, before disabling the
142 * clock for the CMP, ensure that all the CMP instances are not used.
143 *
144 * @param base CMP peripheral base address.
145 */
146 void CMP_Deinit(CMP_Type *base);
147
148 /*!
149 * @brief Enables/disables the CMP module.
150 *
151 * @param base CMP peripheral base address.
152 * @param enable Enables or disables the module.
153 */
CMP_Enable(CMP_Type * base,bool enable)154 static inline void CMP_Enable(CMP_Type *base, bool enable)
155 {
156 if (enable)
157 {
158 base->CR1 |= CMP_CR1_EN_MASK;
159 }
160 else
161 {
162 base->CR1 &= ~(uint8_t)CMP_CR1_EN_MASK;
163 }
164 }
165
166 /*!
167 * @brief Initializes the CMP user configuration structure.
168 *
169 * This function initializes the user configuration structure to these default values.
170 * @code
171 * config->enableCmp = true;
172 * config->hysteresisMode = kCMP_HysteresisLevel0;
173 * config->enableHighSpeed = false;
174 * config->enableInvertOutput = false;
175 * config->useUnfilteredOutput = false;
176 * config->enablePinOut = false;
177 * config->enableTriggerMode = false;
178 * @endcode
179 * @param config Pointer to the configuration structure.
180 */
181 void CMP_GetDefaultConfig(cmp_config_t *config);
182
183 /*!
184 * @brief Sets the input channels for the comparator.
185 *
186 * This function sets the input channels for the comparator.
187 * Note that two input channels cannot be set the same way in the application. When the user selects the same input
188 * from the analog mux to the positive and negative port, the comparator is disabled automatically.
189 *
190 * @param base CMP peripheral base address.
191 * @param positiveChannel Positive side input channel number. Available range is 0-7.
192 * @param negativeChannel Negative side input channel number. Available range is 0-7.
193 */
194 void CMP_SetInputChannels(CMP_Type *base, uint8_t positiveChannel, uint8_t negativeChannel);
195
196 /*! @} */
197
198 /*!
199 * @name Advanced Features
200 * @{
201 */
202
203 #if defined(FSL_FEATURE_CMP_HAS_DMA) && FSL_FEATURE_CMP_HAS_DMA
204 /*!
205 * @brief Enables/disables the DMA request for rising/falling events.
206 *
207 * This function enables/disables the DMA request for rising/falling events. Either event triggers the generation of
208 * the DMA request from CMP if the DMA feature is enabled. Both events are ignored for generating the DMA request from
209 * the CMP
210 * if the DMA is disabled.
211 *
212 * @param base CMP peripheral base address.
213 * @param enable Enables or disables the feature.
214 */
215 void CMP_EnableDMA(CMP_Type *base, bool enable);
216 #endif /* FSL_FEATURE_CMP_HAS_DMA */
217
218 #if defined(FSL_FEATURE_CMP_HAS_WINDOW_MODE) && FSL_FEATURE_CMP_HAS_WINDOW_MODE
219 /*!
220 * @brief Enables/disables the window mode.
221 *
222 * @param base CMP peripheral base address.
223 * @param enable Enables or disables the feature.
224 */
CMP_EnableWindowMode(CMP_Type * base,bool enable)225 static inline void CMP_EnableWindowMode(CMP_Type *base, bool enable)
226 {
227 if (enable)
228 {
229 base->CR1 |= CMP_CR1_WE_MASK;
230 }
231 else
232 {
233 base->CR1 &= (uint8_t)(~CMP_CR1_WE_MASK);
234 }
235 }
236 #endif /* FSL_FEATURE_CMP_HAS_WINDOW_MODE */
237
238 #if defined(FSL_FEATURE_CMP_HAS_PASS_THROUGH_MODE) && FSL_FEATURE_CMP_HAS_PASS_THROUGH_MODE
239 /*!
240 * @brief Enables/disables the pass through mode.
241 *
242 * @param base CMP peripheral base address.
243 * @param enable Enables or disables the feature.
244 */
CMP_EnablePassThroughMode(CMP_Type * base,bool enable)245 static inline void CMP_EnablePassThroughMode(CMP_Type *base, bool enable)
246 {
247 if (enable)
248 {
249 base->MUXCR |= CMP_MUXCR_PSTM_MASK;
250 }
251 else
252 {
253 base->MUXCR &= (uint8_t)(~CMP_MUXCR_PSTM_MASK);
254 }
255 }
256 #endif /* FSL_FEATURE_CMP_HAS_PASS_THROUGH_MODE */
257
258 /*!
259 * @brief Configures the filter.
260 *
261 * @param base CMP peripheral base address.
262 * @param config Pointer to the configuration structure.
263 */
264 void CMP_SetFilterConfig(CMP_Type *base, const cmp_filter_config_t *config);
265
266 /*!
267 * @brief Configures the internal DAC.
268 *
269 * @param base CMP peripheral base address.
270 * @param config Pointer to the configuration structure. "NULL" disables the feature.
271 */
272 void CMP_SetDACConfig(CMP_Type *base, const cmp_dac_config_t *config);
273
274 /*!
275 * @brief Enables the interrupts.
276 *
277 * @param base CMP peripheral base address.
278 * @param mask Mask value for interrupts. See "_cmp_interrupt_enable".
279 */
280 void CMP_EnableInterrupts(CMP_Type *base, uint32_t mask);
281
282 /*!
283 * @brief Disables the interrupts.
284 *
285 * @param base CMP peripheral base address.
286 * @param mask Mask value for interrupts. See "_cmp_interrupt_enable".
287 */
288 void CMP_DisableInterrupts(CMP_Type *base, uint32_t mask);
289
290 /*! @} */
291
292 /*!
293 * @name Results
294 * @{
295 */
296
297 /*!
298 * @brief Gets the status flags.
299 *
300 * @param base CMP peripheral base address.
301 *
302 * @return Mask value for the asserted flags. See "_cmp_status_flags".
303 */
304 uint32_t CMP_GetStatusFlags(CMP_Type *base);
305
306 /*!
307 * @brief Clears the status flags.
308 *
309 * @param base CMP peripheral base address.
310 * @param mask Mask value for the flags. See "_cmp_status_flags".
311 */
312 void CMP_ClearStatusFlags(CMP_Type *base, uint32_t mask);
313
314 /*! @} */
315 #if defined(__cplusplus)
316 }
317 #endif
318 /*!
319 * @}
320 */
321 #endif /* FSL_CMP_H_ */
322