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