1 /*
2  * Copyright 2017-2020 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef _FSL_ACMP_H_
9 #define _FSL_ACMP_H_
10 
11 #include "fsl_common.h"
12 
13 /*!
14  * @addtogroup acmp
15  * @{
16  */
17 
18 /*******************************************************************************
19  * Definitions
20  ******************************************************************************/
21 
22 /*! @name Driver version */
23 /*@{*/
24 /*! @brief ACMP driver version 2.0.2. */
25 #define FSL_ACMP_DRIVER_VERSION (MAKE_VERSION(2U, 0U, 2U))
26 /*@}*/
27 
28 /*!
29  * @brief Analog Comparator Hysterisis Selection.
30  */
31 typedef enum _acmp_hysterisis_mode
32 {
33     kACMP_HysterisisLevel1 = 0U, /*!< ACMP hysterisis is 20mv. >*/
34     kACMP_HysterisisLevel2 = 1U, /*!< ACMP hysterisis is 30mv. >*/
35 } acmp_hysterisis_mode_t;
36 
37 /*!
38  * @brief DAC Voltage Reference source.
39  */
40 typedef enum _acmp_reference_voltage_source
41 {
42     kACMP_VrefSourceVin1 = 0U, /*!< The DAC selects Bandgap as the reference. */
43     kACMP_VrefSourceVin2 = 1U, /*!< The DAC selects VDDA as the reference. */
44 } acmp_reference_voltage_source_t;
45 
46 /*!
47  * @brief The sensitivity modes of the interrupt trigger.
48  */
49 typedef enum _acmp_interrupt_mode
50 {
51     kACMP_OutputFallingInterruptMode  = 0U, /*!< ACMP interrupt on output falling edge. >*/
52     kACMP_OutputRisingInterruptMode   = 1U, /*!< ACMP interrupt on output rising edge. >*/
53     kACMP_OutputBothEdgeInterruptMode = 3U, /*!< ACMP interrupt on output falling or rising edge. >*/
54 } acmp_interrupt_mode_t;
55 
56 /*!
57  * @brief The ACMP input channel selection.
58  */
59 typedef enum _acmp_input_channel_selection
60 {
61     kACMP_ExternalReference0 = 0U, /*!< External reference 0 is selected to as input channel. >*/
62     kACMP_ExternalReference1 = 1U, /*!< External reference 1 is selected to as input channel. >*/
63     kACMP_ExternalReference2 = 2U, /*!< External reference 2 is selected to as input channel. >*/
64     kACMP_InternalDACOutput  = 3U, /*!< Internal DAC putput is selected to as input channel. >*/
65 } acmp_input_channel_selection_t;
66 
67 /*!
68  * @brief The ACMP status flags.
69  */
70 enum _acmp_status_flags
71 {
72     kACMP_InterruptFlag = ACMP_CS_ACF_MASK, /*!< ACMP interrupt on output valid edge. >*/
73     kACMP_OutputFlag    = ACMP_CS_ACO_MASK, /*!< The current value of the analog comparator output. >*/
74 };
75 
76 /*!
77  * @brief Configuration for ACMP.
78  */
79 typedef struct _acmp_config
80 {
81     bool enablePinOut;                     /*!< The comparator output is available on the associated pin. */
82     acmp_hysterisis_mode_t hysteresisMode; /*!< Hysteresis mode. */
83 } acmp_config_t;
84 
85 /*!
86  * @brief Configuration for Internal DAC.
87  */
88 typedef struct _acmp_dac_config
89 {
90     uint8_t DACValue; /*!< Value for DAC Output Voltage. Available range is 0-63. */
91     acmp_reference_voltage_source_t referenceVoltageSource; /*!< Supply voltage reference source. */
92 } acmp_dac_config_t;
93 
94 #if defined(__cplusplus)
95 extern "C" {
96 #endif
97 
98 /*******************************************************************************
99  * API
100  ******************************************************************************/
101 
102 /*!
103  * @name Initialization and deinitialization
104  * @{
105  */
106 
107 /*!
108  * @brief Initialize the ACMP.
109  *
110  * The default configuration can be got by calling ACMP_GetDefaultConfig().
111  *
112  * @param base ACMP peripheral base address.
113  * @param config Pointer to ACMP configuration structure.
114  */
115 void ACMP_Init(ACMP_Type *base, const acmp_config_t *config);
116 
117 /*!
118  * @brief De-Initialize the ACMP.
119  *
120  * @param base ACMP peripheral basic address.
121  */
122 void ACMP_Deinit(ACMP_Type *base);
123 
124 /*!
125  * @brief Gets the default configuration for ACMP.
126  *
127  * This function initializes the user configuration structure to default value. The default value are:
128  * Example:
129  *   @code
130  *   config->enablePinOut = false;
131  *   config->hysteresisMode = kACMP_HysterisisLevel1;
132  *   @endcode
133  *
134  * @param config Pointer to ACMP configuration structure.
135  */
136 void ACMP_GetDefaultConfig(acmp_config_t *config);
137 
138 /*!
139  * @brief Enable/Disable the ACMP module.
140  *
141  * @param base ACMP peripheral base address.
142  * @param enable Switcher to enable/disable ACMP module.
143  */
ACMP_Enable(ACMP_Type * base,bool enable)144 static inline void ACMP_Enable(ACMP_Type *base, bool enable)
145 {
146     if (enable)
147     {
148         base->CS |= ACMP_CS_ACE_MASK;
149     }
150     else
151     {
152         base->CS &= ~(uint8_t)ACMP_CS_ACE_MASK;
153     }
154 }
155 
156 /*!
157  * @brief Enable the ACMP interrupt and determines the sensitivity modes of the interrupt trigger.
158  *
159  *
160  * @param base ACMP peripheral base address.
161  * @param mode Select one interrupt mode to generate interrupt.
162  */
163 void ACMP_EnableInterrupt(ACMP_Type *base, acmp_interrupt_mode_t mode);
164 
165 /*!
166  * @brief Disable the ACMP interrupt.
167  *
168  * @param base ACMP peripheral base address.
169  */
ACMP_DisableInterrupt(ACMP_Type * base)170 static inline void ACMP_DisableInterrupt(ACMP_Type *base)
171 {
172     base->CS &= ~(uint8_t)ACMP_CS_ACIE_MASK;
173 }
174 
175 /*!
176  * @brief Configure the ACMP positive and negative input channel.
177  *
178  * @param base ACMP peripheral base address.
179  * @param PositiveInput ACMP Positive Input Select. Refer to "acmp_input_channel_selection_t".
180  * @param negativeInout ACMP Negative Input Select. Refer to "acmp_input_channel_selection_t".
181  */
182 void ACMP_SetChannelConfig(ACMP_Type *base,
183                            acmp_input_channel_selection_t PositiveInput,
184                            acmp_input_channel_selection_t negativeInout);
185 
186 /*
187  * @brief Configure the internal DAC.
188  *
189  * @param base ACMP peripheral base address.
190  * @param config Pointer to DAC configuration structure. "NULL" is for disabling the feature.
191  */
192 void ACMP_SetDACConfig(ACMP_Type *base, const acmp_dac_config_t *config);
193 
194 /*!
195  * @brief Enable/Disable ACMP input pin.
196  *        The API controls if the corresponding ACMP external pin can be driven by an analog input
197  *
198  * @param base ACMP peripheral base address.
199  * @param mask The mask of the pin associated with channel ADx. Valid range is AD0:0x1U ~ AD3:0x4U.
200  *             For example: If enable AD0, AD1 and AD2 pins, mask should be set to 0x7U(0x1 | 0x2 | 0x4).
201  * @param enable Switcher to enable/disable ACMP module.
202  */
203 void ACMP_EnableInputPin(ACMP_Type *base, uint32_t mask, bool enable);
204 
205 /*!
206  * @brief Get ACMP status flags.
207  *
208  * @param base ACMP peripheral base address.
209  * @return Flags' mask if indicated flags are asserted. See "_acmp_status_flags".
210  */
ACMP_GetStatusFlags(ACMP_Type * base)211 static inline uint8_t ACMP_GetStatusFlags(ACMP_Type *base)
212 {
213     return (base->CS) & (ACMP_CS_ACF_MASK | ACMP_CS_ACO_MASK);
214 }
215 
216 /*!
217  * @brief Clear interrupts status flag.
218  *
219  * @param base ACMP peripheral base address.
220  */
ACMP_ClearInterruptFlags(ACMP_Type * base)221 static inline void ACMP_ClearInterruptFlags(ACMP_Type *base)
222 {
223     base->CS &= ~(uint8_t)ACMP_CS_ACF_MASK;
224 }
225 
226 /*@}*/
227 
228 #if defined(__cplusplus)
229 }
230 #endif
231 
232 /*@}*/
233 
234 #endif /* _FSL_ACMP_H_ */
235