1 /*
2  * Copyright 2021 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef _FSL_HSCMP_H_
9 #define _FSL_HSCMP_H_
10 
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup hscmp
15  * @{
16  */
17 
18 /*******************************************************************************
19  * Definitions
20  ******************************************************************************/
21 
22 /*! @name Driver version */
23 /*@{*/
24 /*! @brief HSCMP driver version 2.0.3. */
25 #define FSL_HSCMP_DRIVER_VERSION (MAKE_VERSION(2, 0, 3))
26 /*@}*/
27 
28 /*!
29  * @brief HSCMP status falgs mask.
30  */
31 enum _hscmp_status_flags
32 {
33     kHSCMP_OutputRisingEventFlag  = HSCMP_CSR_CFR_MASK,  /*!< Rising-edge on the comparison output has occurred. */
34     kHSCMP_OutputFallingEventFlag = HSCMP_CSR_CFF_MASK,  /*!< Falling-edge on the comparison output has occurred. */
35     kHSCMP_OutputAssertEventFlag  = HSCMP_CSR_COUT_MASK, /*!< Return the current value of the analog comparator output.
36                                                               The flag does not support W1C. */
37 };
38 
39 /*!
40  * @brief HSCMP interrupt enable/disable mask.
41  */
42 enum _hscmp_interrupt_enable
43 {
44     kHSCMP_OutputRisingInterruptEnable  = HSCMP_IER_CFR_IE_MASK, /*!< Comparator interrupt enable rising. */
45     kHSCMP_OutputFallingInterruptEnable = HSCMP_IER_CFF_IE_MASK, /*!< Comparator interrupt enable falling. */
46 };
47 /*!
48  * @brief HSCMP hysteresis mode. See chip data sheet to get the actual hystersis
49  *        value with each level
50  */
51 typedef enum _hscmp_hysteresis_mode
52 {
53     kHSCMP_HysteresisLevel0 = 0U, /*!< The hard block output has level 0 hysteresis internally. */
54     kHSCMP_HysteresisLevel1 = 1U, /*!< The hard block output has level 1 hysteresis internally. */
55     kHSCMP_HysteresisLevel2 = 2U, /*!< The hard block output has level 2 hysteresis internally. */
56     kHSCMP_HysteresisLevel3 = 3U, /*!< The hard block output has level 3 hysteresis internally. */
57 } hscmp_hysteresis_mode_t;
58 
59 /*!
60  * @brief HSCMP nano mode.
61  */
62 typedef enum _hscmp_power_mode
63 {
64     kHSCMP_LowSpeedPowerMode  = 0U, /*!< Low speed comparison mode is selected. */
65     kHSCMP_HighSpeedPowerMode = 1U, /*!< High speed comparison mode is selected. */
66     kHSCMP_NanoPowerMode      = 2U, /*!< Nano power comparator is enabled. */
67 } hscmp_power_mode_t;
68 
69 /*!
70  * @brief Internal DAC reference voltage source.
71  */
72 typedef enum _hscmp_dac_reference_voltage_source
73 {
74     kHSCMP_VrefSourceVin1 = 0U, /*!< vrefh_int is selected as resistor ladder network supply reference Vin. */
75     kHSCMP_VrefSourceVin2 = 1U, /*!< vrefh_ext is selected as resistor ladder network supply reference Vin. */
76 } hscmp_dac_reference_voltage_source_t;
77 
78 /*!
79  * @brief Configure the filter.
80  */
81 typedef struct _hscmp_filter_config
82 {
83     bool enableSample;          /*!< Decide whether to use the external SAMPLE as a sampling clock input. */
84     uint8_t filterSampleCount;  /*!< Filter Sample Count. Available range is 1-7; 0 disables the filter. */
85     uint8_t filterSamplePeriod; /*!< Filter Sample Period. The divider to the bus clock. Available range is 0-255. The
86                                 sampling clock must be at least 4 times slower than the system clock to the comparator.
87                                 So if enableSample is "false", filterSamplePeriod should be set greater than 4.*/
88 } hscmp_filter_config_t;
89 
90 /*!
91  * @brief configure the internal DAC.
92  */
93 typedef struct _hscmp_dac_config
94 {
95     bool enableLowPowerMode;                                     /*!< Decide whether to enable DAC low power mode. */
96     hscmp_dac_reference_voltage_source_t referenceVoltageSource; /*!< Internal DAC supply voltage reference source. */
97     uint8_t DACValue; /*!< Value for the DAC Output Voltage. Available range is 0-63.*/
98 } hscmp_dac_config_t;
99 
100 /*!
101  * @brief Configures the comparator.
102  */
103 typedef struct _hscmp_config
104 {
105     bool enableStopMode;      /*!< Decide whether to enable the comparator when in STOP modes. */
106     bool enableOutputPin;     /*!< Decide whether to enable the comparator is available in selected pin. */
107     bool useUnfilteredOutput; /*!< Decide whether to use unfiltered output. */
108     bool enableInvertOutput;  /*!< Decide whether to inverts the comparator output. */
109     hscmp_hysteresis_mode_t hysteresisMode; /*!< HSCMP hysteresis mode. */
110     hscmp_power_mode_t powerMode;           /*!< HSCMP power mode. */
111 } hscmp_config_t;
112 /*******************************************************************************
113  * API
114  ******************************************************************************/
115 
116 /*!
117  * @name Initialization
118  * @{
119  */
120 
121 /*!
122  * @brief Initialize the HSCMP
123  *
124  * This function initializes the HSCMP module. The operations included are:
125  * - Enabling the clock for HSCMP module.
126  * - Configuring the comparator.
127  * - Enabling the HSCMP module.
128  * Note: For some devices, multiple HSCMP instance share the same clock gate. In this case, to enable the clock for
129  * any instance enables all the HSCMPs. Check the chip reference manual for the clock assignment of the HSCMP.
130  *
131  * @param base HSCMP peripheral base address.
132  * @param config Pointer to "hscmp_config_t" structure.
133  */
134 void HSCMP_Init(HSCMP_Type *base, const hscmp_config_t *config);
135 
136 /*!
137  * @brief De-initializes the HSCMP module.
138  *
139  * This function de-initializes the HSCMP module. The operations included are:
140  * - Disabling the HSCMP module.
141  * - Disabling the clock for HSCMP module.
142  *
143  * This function disables the clock for the HSCMP.
144  * Note: For some devices, multiple HSCMP instance shares the same clock gate. In this case, before disabling the
145  * clock for the HSCMP, ensure that all the HSCMP instances are not used.
146  *
147  * @param base HSCMP peripheral base address.
148  */
149 void HSCMP_Deinit(HSCMP_Type *base);
150 
151 /*!
152  * @brief Gets an available pre-defined settings for the comparator's configuration.
153  *
154  * This function initializes the comparator configuration structure to these default values:
155  * @code
156  *   config->enableStopMode      = false;
157  *   config->enableOutputPin     = false;
158  *   config->useUnfilteredOutput = false;
159  *   config->enableInvertOutput  = false;
160  *   config->hysteresisMode      = kHSCMP_HysteresisLevel0;
161  *   config->powerMode           = kHSCMP_LowSpeedPowerMode;
162  * @endcode
163  * @param config Pointer to "hscmp_config_t" structure.
164  */
165 void HSCMP_GetDefaultConfig(hscmp_config_t *config);
166 
167 /*!
168  * @brief Enable/Disable HSCMP module.
169  *
170  * @param base HSCMP peripheral base address.
171  * @param enable "true" means enable the module, and "false" means disable the module.
172  */
HSCMP_Enable(HSCMP_Type * base,bool enable)173 static inline void HSCMP_Enable(HSCMP_Type *base, bool enable)
174 {
175     if (enable)
176     {
177         base->CCR0 |= HSCMP_CCR0_CMP_EN_MASK;
178     }
179     else
180     {
181         base->CCR0 &= ~HSCMP_CCR0_CMP_EN_MASK;
182     }
183 }
184 
185 /*!
186  * @brief Select the input channels for HSCMP. This function determines which input
187  *        is selected for the negative and positive mux.
188  *
189  * @param base HSCMP peripheral base address.
190  * @param positiveChannel Positive side input channel number. Available range is 0-7.
191  * @param negativeChannel Negative side input channel number. Available range is 0-7.
192  */
193 void HSCMP_SetInputChannels(HSCMP_Type *base, uint32_t positiveChannel, uint32_t negativeChannel);
194 
195 /*!
196  * @brief Enables/disables the DMA request for rising/falling events.
197  *        Normally, the HSCMP generates a CPU interrupt if there is a rising/falling event. When
198  *        DMA support is enabled and the rising/falling interrupt is enabled , the rising/falling
199  *        event forces a DMA transfer request rather than a CPU interrupt instead.
200  *
201  * @param base HSCMP peripheral base address.
202  * @param enable "true" means enable DMA support, and "false" means disable DMA support.
203  */
HSCMP_EnableDMA(HSCMP_Type * base,bool enable)204 static inline void HSCMP_EnableDMA(HSCMP_Type *base, bool enable)
205 {
206     if (enable)
207     {
208         base->CCR1 |= HSCMP_CCR1_DMA_EN_MASK;
209     }
210     else
211     {
212         base->CCR1 &= ~HSCMP_CCR1_DMA_EN_MASK;
213     }
214 }
215 
216 /*!
217  * @brief Enable/Disable window mode.When any windowed mode is active, COUTA is clocked by
218  *        the bus clock whenever WINDOW = 1. The last latched value is held when WINDOW = 0.
219  *        The optionally inverted comparator output COUT_RAW is sampled on every bus clock
220  *        when WINDOW=1 to generate COUTA.
221  *
222  * @param base HSCMP peripheral base address.
223  * @param enable "true" means enable window mode, and "false" means disable window mode.
224  */
HSCMP_EnableWindowMode(HSCMP_Type * base,bool enable)225 static inline void HSCMP_EnableWindowMode(HSCMP_Type *base, bool enable)
226 {
227     if (enable)
228     {
229         base->CCR1 |= HSCMP_CCR1_WINDOW_EN_MASK;
230     }
231     else
232     {
233         base->CCR1 &= ~HSCMP_CCR1_WINDOW_EN_MASK;
234     }
235 }
236 
237 /*!
238  * @brief Configures the filter.
239  *
240  * @param base HSCMP peripheral base address.
241  * @param config Pointer to "hscmp_filter_config_t" structure.
242  */
243 void HSCMP_SetFilterConfig(HSCMP_Type *base, const hscmp_filter_config_t *config);
244 
245 /*!
246  * @brief Configure the internal DAC module.
247  *
248  * @param base HSCMP peripheral base address.
249  * @param config Pointer to "hscmp_dac_config_t" structure. If config is "NULL", disable internal DAC.
250  */
251 void HSCMP_SetDACConfig(HSCMP_Type *base, const hscmp_dac_config_t *config);
252 
253 /*!
254  * @brief Enable the interrupts.
255  *
256  * @param base HSCMP peripheral base address.
257  * @param mask Mask value for interrupts. See "_hscmp_interrupt_enable".
258  */
HSCMP_EnableInterrupts(HSCMP_Type * base,uint32_t mask)259 static inline void HSCMP_EnableInterrupts(HSCMP_Type *base, uint32_t mask)
260 {
261     base->IER |= mask;
262 }
263 
264 /*!
265  * @brief Disable the interrupts.
266  *
267  * @param base HSCMP peripheral base address.
268  * @param mask Mask value for interrupts. See "_hscmp_interrupt_enable".
269  */
HSCMP_DisableInterrupts(HSCMP_Type * base,uint32_t mask)270 static inline void HSCMP_DisableInterrupts(HSCMP_Type *base, uint32_t mask)
271 {
272     base->IER &= ~mask;
273 }
274 
275 /*!
276  * @brief Get the HSCMP status flags.
277  *
278  * @param base HSCMP peripheral base address.
279  *
280  * @return Mask value for the asserted flags. See "_hscmp_status_flags".
281  */
HSCMP_GetStatusFlags(HSCMP_Type * base)282 static inline uint32_t HSCMP_GetStatusFlags(HSCMP_Type *base)
283 {
284     return base->CSR;
285 }
286 
287 /*!
288  * @brief Clear the HSCMP status flags
289  *
290  * @param base HSCMP peripheral base address.
291  * @param mask Mask value for the flags. See "_hscmp_status_flags".
292  */
HSCMP_ClearStatusFlags(HSCMP_Type * base,uint32_t mask)293 static inline void HSCMP_ClearStatusFlags(HSCMP_Type *base, uint32_t mask)
294 {
295     base->CSR = mask;
296 }
297 
298 /*@}*/
299 
300 /*@}*/
301 
302 #endif /* _FSL_HSCMP_H_ */
303