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