1 /**************************************************************************//**
2  * @file     acmp.h
3  * @version  V3.00
4  * @brief    ACMP Driver Header File
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
8  ******************************************************************************/
9 #ifndef __ACMP_H__
10 #define __ACMP_H__
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
17 
18 /** @addtogroup Standard_Driver Standard Driver
19   @{
20 */
21 
22 /** @addtogroup ACMP_Driver ACMP Driver
23   @{
24 */
25 
26 
27 /** @addtogroup ACMP_EXPORTED_CONSTANTS ACMP Exported Constants
28   @{
29 */
30 
31 
32 
33 /*---------------------------------------------------------------------------------------------------------*/
34 /* ACMP_CTL constant definitions                                                                           */
35 /*---------------------------------------------------------------------------------------------------------*/
36 #define ACMP_CTL_FILTSEL_OFF         (0UL << 13) /*!< ACMP_CTL setting for filter function disabled. */
37 #define ACMP_CTL_FILTSEL_1PCLK       (1UL << 13) /*!< ACMP_CTL setting for 1 PCLK filter count. */
38 #define ACMP_CTL_FILTSEL_2PCLK       (2UL << 13) /*!< ACMP_CTL setting for 2 PCLK filter count. */
39 #define ACMP_CTL_FILTSEL_4PCLK       (3UL << 13) /*!< ACMP_CTL setting for 4 PCLK filter count. */
40 #define ACMP_CTL_FILTSEL_8PCLK       (4UL << 13) /*!< ACMP_CTL setting for 8 PCLK filter count. */
41 #define ACMP_CTL_FILTSEL_16PCLK      (5UL << 13) /*!< ACMP_CTL setting for 16 PCLK filter count. */
42 #define ACMP_CTL_FILTSEL_32PCLK      (6UL << 13) /*!< ACMP_CTL setting for 32 PCLK filter count. */
43 #define ACMP_CTL_FILTSEL_64PCLK      (7UL << 13) /*!< ACMP_CTL setting for 64 PCLK filter count. */
44 #define ACMP_CTL_INTPOL_RF           (0UL << 8)  /*!< ACMP_CTL setting for selecting rising edge and falling edge as interrupt condition. */
45 #define ACMP_CTL_INTPOL_R            (1UL << 8)  /*!< ACMP_CTL setting for selecting rising edge as interrupt condition. */
46 #define ACMP_CTL_INTPOL_F            (2UL << 8)  /*!< ACMP_CTL setting for selecting falling edge as interrupt condition. */
47 #define ACMP_CTL_POSSEL_P0           (0UL << 6)  /*!< ACMP_CTL setting for selecting ACMPx_P0 pin as the source of ACMP V+. */
48 #define ACMP_CTL_POSSEL_P1           (1UL << 6)  /*!< ACMP_CTL setting for selecting ACMPx_P1 pin as the source of ACMP V+. */
49 #define ACMP_CTL_POSSEL_P2           (2UL << 6)  /*!< ACMP_CTL setting for selecting ACMPx_P2 pin as the source of ACMP V+. */
50 #define ACMP_CTL_POSSEL_P3           (3UL << 6)  /*!< ACMP_CTL setting for selecting ACMPx_P3 pin as the source of ACMP V+. */
51 #define ACMP_CTL_NEGSEL_PIN          (0UL << 4)  /*!< ACMP_CTL setting for selecting the voltage of ACMP negative input pin as the source of ACMP V-. */
52 #define ACMP_CTL_NEGSEL_CRV          (1UL << 4)  /*!< ACMP_CTL setting for selecting internal comparator reference voltage as the source of ACMP V-. */
53 #define ACMP_CTL_NEGSEL_VBG          (2UL << 4)  /*!< ACMP_CTL setting for selecting internal Band-gap voltage as the source of ACMP V-. */
54 #define ACMP_CTL_NEGSEL_DAC          (3UL << 4)  /*!< ACMP_CTL setting for selecting DAC output voltage as the source of ACMP V-. */
55 #define ACMP_CTL_HYSTERESIS_30MV     (3UL << 24) /*!< ACMP_CTL setting for enabling the hysteresis function at 30mV. */
56 #define ACMP_CTL_HYSTERESIS_20MV     (2UL << 24) /*!< ACMP_CTL setting for enabling the hysteresis function at 20mV. */
57 #define ACMP_CTL_HYSTERESIS_10MV     (1UL << 24) /*!< ACMP_CTL setting for enabling the hysteresis function at 10mV. */
58 #define ACMP_CTL_HYSTERESIS_DISABLE  (0UL << 2)  /*!< ACMP_CTL setting for disabling the hysteresis function. */
59 
60 /*---------------------------------------------------------------------------------------------------------*/
61 /* ACMP_VREF constant definitions                                                                          */
62 /*---------------------------------------------------------------------------------------------------------*/
63 #define ACMP_VREF_CRVSSEL_VDDA       (0UL << 6)  /*!< ACMP_VREF setting for selecting analog supply voltage VDDA as the CRV source voltage */
64 #define ACMP_VREF_CRVSSEL_INTVREF    (1UL << 6)  /*!< ACMP_VREF setting for selecting internal reference voltage as the CRV source voltage */
65 
66 
67 /**@}*/ /* end of group ACMP_EXPORTED_CONSTANTS */
68 
69 
70 /** @addtogroup ACMP_EXPORTED_FUNCTIONS ACMP Exported Functions
71   @{
72 */
73 
74 /*---------------------------------------------------------------------------------------------------------*/
75 /*  Define Macros and functions                                                                            */
76 /*---------------------------------------------------------------------------------------------------------*/
77 
78 
79 /**
80   * @brief This macro is used to enable output inverse function
81   * @param[in] acmp The pointer of the specified ACMP module
82   * @param[in] u32ChNum The ACMP number
83   * @return None
84   * @details This macro will set ACMPOINV bit of ACMP_CTL register to enable output inverse function.
85   */
86 #define ACMP_ENABLE_OUTPUT_INVERSE(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] |= ACMP_CTL_ACMPOINV_Msk)
87 
88 /**
89   * @brief This macro is used to disable output inverse function
90   * @param[in] acmp The pointer of the specified ACMP module
91   * @param[in] u32ChNum The ACMP number
92   * @return None
93   * @details This macro will clear ACMPOINV bit of ACMP_CTL register to disable output inverse function.
94   */
95 #define ACMP_DISABLE_OUTPUT_INVERSE(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] &= ~ACMP_CTL_ACMPOINV_Msk)
96 
97 /**
98   * @brief This macro is used to select ACMP negative input source
99   * @param[in] acmp The pointer of the specified ACMP module
100   * @param[in] u32ChNum The ACMP number
101   * @param[in] u32Src is comparator negative input selection. Including:
102   *                  - \ref ACMP_CTL_NEGSEL_PIN
103   *                  - \ref ACMP_CTL_NEGSEL_CRV
104   *                  - \ref ACMP_CTL_NEGSEL_VBG
105   *                  - \ref ACMP_CTL_NEGSEL_DAC
106   * @return None
107   * @details This macro will set NEGSEL (ACMP_CTL[5:4]) to determine the source of negative input.
108   */
109 #define ACMP_SET_NEG_SRC(acmp, u32ChNum, u32Src) ((acmp)->CTL[(u32ChNum)] = ((acmp)->CTL[(u32ChNum)] & ~ACMP_CTL_NEGSEL_Msk) | (u32Src))
110 
111 /**
112   * @brief This macro is used to enable hysteresis function
113   * @param[in] acmp The pointer of the specified ACMP module
114   * @param[in] u32ChNum The ACMP number
115   * @return None
116   */
117 #define ACMP_ENABLE_HYSTERESIS(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] |= ACMP_CTL_HYSTERESIS_30MV)
118 
119 /**
120   * @brief This macro is used to disable hysteresis function
121   * @param[in] acmp The pointer of the specified ACMP module
122   * @param[in] u32ChNum The ACMP number
123   * @return None
124   * @details This macro will set HYSSEL of ACMP_CTL register to disable hysteresis function.
125   */
126 #define ACMP_DISABLE_HYSTERESIS(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] &= ~ACMP_CTL_HYSSEL_Msk)
127 
128 /**
129   * @brief This macro is used to select hysteresis level
130   * @param[in] acmp The pointer of the specified ACMP module
131   * @param[in] u32ChNum The ACMP number
132   * @param[in] u32HysSel The hysteresis function option. Including:
133   *                  - \ref ACMP_CTL_HYSTERESIS_30MV
134   *                  - \ref ACMP_CTL_HYSTERESIS_20MV
135   *                  - \ref ACMP_CTL_HYSTERESIS_10MV
136   *                  - \ref ACMP_CTL_HYSTERESIS_DISABLE
137   * @return None
138   */
139 #define ACMP_CONFIG_HYSTERESIS(acmp, u32ChNum, u32HysSel) ((acmp)->CTL[(u32ChNum)] = ((acmp)->CTL[(u32ChNum)] & ~ACMP_CTL_HYSSEL_Msk) | (u32HysSel))
140 
141 /**
142   * @brief This macro is used to enable interrupt
143   * @param[in] acmp The pointer of the specified ACMP module
144   * @param[in] u32ChNum The ACMP number
145   * @return None
146   * @details This macro will set ACMPIE bit of ACMP_CTL register to enable interrupt function.
147   *          If wake-up function is enabled, the wake-up interrupt will be enabled as well.
148   */
149 #define ACMP_ENABLE_INT(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] |= ACMP_CTL_ACMPIE_Msk)
150 
151 /**
152   * @brief This macro is used to disable interrupt
153   * @param[in] acmp The pointer of the specified ACMP module
154   * @param[in] u32ChNum The ACMP number
155   * @return None
156   * @details This macro will clear ACMPIE bit of ACMP_CTL register to disable interrupt function.
157   */
158 #define ACMP_DISABLE_INT(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] &= ~ACMP_CTL_ACMPIE_Msk)
159 
160 /**
161   * @brief This macro is used to enable ACMP
162   * @param[in] acmp The pointer of the specified ACMP module
163   * @param[in] u32ChNum The ACMP number
164   * @return None
165   * @details This macro will set ACMPEN bit of ACMP_CTL register to enable analog comparator.
166   */
167 #define ACMP_ENABLE(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] |= ACMP_CTL_ACMPEN_Msk)
168 
169 /**
170   * @brief This macro is used to disable ACMP
171   * @param[in] acmp The pointer of the specified ACMP module
172   * @param[in] u32ChNum The ACMP number
173   * @return None
174   * @details This macro will clear ACMPEN bit of ACMP_CTL register to disable analog comparator.
175   */
176 #define ACMP_DISABLE(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] &= ~ACMP_CTL_ACMPEN_Msk)
177 
178 /**
179   * @brief This macro is used to get ACMP output value
180   * @param[in] acmp The pointer of the specified ACMP module
181   * @param[in] u32ChNum The ACMP number
182   * @return  ACMP output value
183   * @details This macro will return the ACMP output value.
184   */
185 #define ACMP_GET_OUTPUT(acmp, u32ChNum) (((acmp)->STATUS & (ACMP_STATUS_ACMPO0_Msk<<((u32ChNum))))?1:0)
186 
187 /**
188   * @brief This macro is used to get ACMP interrupt flag
189   * @param[in] acmp The pointer of the specified ACMP module
190   * @param[in] u32ChNum The ACMP number
191   * @return   ACMP interrupt occurred (1) or not (0)
192   * @details This macro will return the ACMP interrupt flag.
193   */
194 #define ACMP_GET_INT_FLAG(acmp, u32ChNum) (((acmp)->STATUS & (ACMP_STATUS_ACMPIF0_Msk<<((u32ChNum))))?1:0)
195 
196 /**
197   * @brief This macro is used to clear ACMP interrupt flag
198   * @param[in] acmp The pointer of the specified ACMP module
199   * @param[in] u32ChNum The ACMP number
200   * @return   None
201   * @details This macro will write 1 to ACMPIFn bit of ACMP_STATUS register to clear interrupt flag.
202   */
203 #define ACMP_CLR_INT_FLAG(acmp, u32ChNum) ((acmp)->STATUS = (ACMP_STATUS_ACMPIF0_Msk<<((u32ChNum))))
204 
205 /**
206   * @brief This macro is used to clear ACMP wake-up interrupt flag
207   * @param[in] acmp The pointer of the specified ACMP module
208   * @param[in] u32ChNum The ACMP number
209   * @return   None
210   * @details This macro will write 1 to WKIFn bit of ACMP_STATUS register to clear interrupt flag.
211   */
212 #define ACMP_CLR_WAKEUP_INT_FLAG(acmp, u32ChNum) ((acmp)->STATUS = (ACMP_STATUS_WKIF0_Msk<<((u32ChNum))))
213 
214 /**
215   * @brief This macro is used to enable ACMP wake-up function
216   * @param[in] acmp The pointer of the specified ACMP module
217   * @param[in] u32ChNum The ACMP number
218   * @return None
219   * @details This macro will set WKEN (ACMP_CTL[16]) to enable ACMP wake-up function.
220   */
221 #define ACMP_ENABLE_WAKEUP(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] |= ACMP_CTL_WKEN_Msk)
222 
223 /**
224   * @brief This macro is used to disable ACMP wake-up function
225   * @param[in] acmp The pointer of the specified ACMP module
226   * @param[in] u32ChNum The ACMP number
227   * @return None
228   * @details This macro will clear WKEN (ACMP_CTL[16]) to disable ACMP wake-up function.
229   */
230 #define ACMP_DISABLE_WAKEUP(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] &= ~ACMP_CTL_WKEN_Msk)
231 
232 /**
233   * @brief This macro is used to select ACMP positive input pin
234   * @param[in] acmp The pointer of the specified ACMP module
235   * @param[in] u32ChNum The ACMP number
236   * @param[in] u32Pin Comparator positive pin selection. Including:
237   *                  - \ref ACMP_CTL_POSSEL_P0
238   *                  - \ref ACMP_CTL_POSSEL_P1
239   *                  - \ref ACMP_CTL_POSSEL_P2
240   *                  - \ref ACMP_CTL_POSSEL_P3
241   * @return None
242   * @details This macro will set POSSEL (ACMP_CTL[7:6]) to determine the comparator positive input pin.
243   */
244 #define ACMP_SELECT_P(acmp, u32ChNum, u32Pin) ((acmp)->CTL[(u32ChNum)] = ((acmp)->CTL[(u32ChNum)] & ~ACMP_CTL_POSSEL_Msk) | (u32Pin))
245 
246 /**
247   * @brief This macro is used to enable ACMP filter function
248   * @param[in] acmp The pointer of the specified ACMP module
249   * @param[in] u32ChNum The ACMP number
250   * @return None
251   * @details This macro will set OUTSEL (ACMP_CTL[12]) to enable output filter function.
252   */
253 #define ACMP_ENABLE_FILTER(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] |= ACMP_CTL_OUTSEL_Msk)
254 
255 /**
256   * @brief This macro is used to disable ACMP filter function
257   * @param[in] acmp The pointer of the specified ACMP module
258   * @param[in] u32ChNum The ACMP number
259   * @return None
260   * @details This macro will clear OUTSEL (ACMP_CTL[12]) to disable output filter function.
261   */
262 #define ACMP_DISABLE_FILTER(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] &= ~ACMP_CTL_OUTSEL_Msk)
263 
264 /**
265   * @brief This macro is used to set ACMP filter function
266   * @param[in] acmp The pointer of the specified ACMP module
267   * @param[in] u32ChNum The ACMP number
268   * @param[in] u32Cnt is comparator filter count setting.
269   *                  - \ref ACMP_CTL_FILTSEL_OFF
270   *                  - \ref ACMP_CTL_FILTSEL_1PCLK
271   *                  - \ref ACMP_CTL_FILTSEL_2PCLK
272   *                  - \ref ACMP_CTL_FILTSEL_4PCLK
273   *                  - \ref ACMP_CTL_FILTSEL_8PCLK
274   *                  - \ref ACMP_CTL_FILTSEL_16PCLK
275   *                  - \ref ACMP_CTL_FILTSEL_32PCLK
276   *                  - \ref ACMP_CTL_FILTSEL_64PCLK
277   * @return None
278   * @details When ACMP output filter function is enabled, the output sampling count is determined by FILTSEL (ACMP_CTL[15:13]).
279   */
280 #define ACMP_SET_FILTER(acmp, u32ChNum, u32Cnt) ((acmp)->CTL[(u32ChNum)] = ((acmp)->CTL[(u32ChNum)] & ~ACMP_CTL_FILTSEL_Msk) | (u32Cnt))
281 
282 /**
283   * @brief This macro is used to select comparator reference voltage
284   * @param[in] acmp The pointer of the specified ACMP module
285   * @param[in] u32Level  The comparator reference voltage setting.
286   *             The formula is:
287   *                       comparator reference voltage = CRV source voltage x (1/6 + u32Level/24)
288   *             The range of u32Level is 0 ~ 15.
289   * @return   None
290   * @details  When CRV is selected as ACMP negative input source, the CRV level is determined by CRVCTL (ACMP_VREF[3:0]).
291   */
292 #define ACMP_CRV_SEL(acmp, u32Level) ((acmp)->VREF = ((acmp)->VREF & ~ACMP_VREF_CRVCTL_Msk) | ((u32Level)<<ACMP_VREF_CRVCTL_Pos))
293 
294 /**
295   * @brief This macro is used to select the source of CRV
296   * @param[in] acmp The pointer of the specified ACMP module
297   * @param[in] u32Src is the source of CRV. Including:
298   *                  - \ref ACMP_VREF_CRVSSEL_VDDA
299   *                  - \ref ACMP_VREF_CRVSSEL_INTVREF
300   * @return None
301   * @details The source of CRV can be VDDA or internal reference voltage. The internal reference voltage level is determined by SYS_VREFCTL register.
302   */
303 #define ACMP_SELECT_CRV_SRC(acmp, u32Src) ((acmp)->VREF = ((acmp)->VREF & ~ACMP_VREF_CRVSSEL_Msk) | (u32Src))
304 
305 /**
306   * @brief This macro is used to select ACMP interrupt condition
307   * @param[in] acmp The pointer of the specified ACMP module
308   * @param[in] u32ChNum The ACMP number
309   * @param[in] u32Cond Comparator interrupt condition selection. Including:
310   *                  - \ref ACMP_CTL_INTPOL_RF
311   *                  - \ref ACMP_CTL_INTPOL_R
312   *                  - \ref ACMP_CTL_INTPOL_F
313   * @return None
314   * @details The ACMP output interrupt condition can be rising edge, falling edge or any edge.
315   */
316 #define ACMP_SELECT_INT_COND(acmp, u32ChNum, u32Cond) ((acmp)->CTL[(u32ChNum)] = ((acmp)->CTL[(u32ChNum)] & ~ACMP_CTL_INTPOL_Msk) | (u32Cond))
317 
318 /**
319   * @brief This macro is used to enable ACMP window latch mode
320   * @param[in] acmp The pointer of the specified ACMP module
321   * @param[in] u32ChNum The ACMP number
322   * @return None
323   * @details This macro will set WLATEN (ACMP_CTL[17]) to enable ACMP window latch mode.
324   *          When ACMP0/1_WLAT pin is at high level, ACMPO0/1 passes through window latch
325   *          block; when ACMP0/1_WLAT pin is at low level, the output of window latch block,
326   *          WLATOUT, is frozen.
327   */
328 #define ACMP_ENABLE_WINDOW_LATCH(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] |= ACMP_CTL_WLATEN_Msk)
329 
330 /**
331   * @brief This macro is used to disable ACMP window latch mode
332   * @param[in] acmp The pointer of the specified ACMP module
333   * @param[in] u32ChNum The ACMP number
334   * @return None
335   * @details This macro will clear WLATEN (ACMP_CTL[17]) to disable ACMP window latch mode.
336   */
337 #define ACMP_DISABLE_WINDOW_LATCH(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] &= ~ACMP_CTL_WLATEN_Msk)
338 
339 /**
340   * @brief This macro is used to enable ACMP window compare mode
341   * @param[in] acmp The pointer of the specified ACMP module
342   * @param[in] u32ChNum The ACMP number
343   * @return None
344   * @details This macro will set WCMPSEL (ACMP_CTL[18]) to enable ACMP window compare mode.
345   *          When window compare mode is enabled, user can connect the specific analog voltage
346   *          source to either the positive inputs of both comparators or the negative inputs of
347   *          both comparators. The upper bound and lower bound of the designated range are
348   *          determined by the voltages applied to the other inputs of both comparators. If the
349   *          output of a comparator is low and the other comparator outputs high, which means two
350   *          comparators implies the upper and lower bound. User can directly monitor a specific
351   *          analog voltage source via ACMPWO (ACMP_STATUS[16]).
352   */
353 #define ACMP_ENABLE_WINDOW_COMPARE(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] |= ACMP_CTL_WCMPSEL_Msk)
354 
355 /**
356   * @brief This macro is used to disable ACMP window compare mode
357   * @param[in] acmp The pointer of the specified ACMP module
358   * @param[in] u32ChNum The ACMP number
359   * @return None
360   * @details This macro will clear WCMPSEL (ACMP_CTL[18]) to disable ACMP window compare mode.
361   */
362 #define ACMP_DISABLE_WINDOW_COMPARE(acmp, u32ChNum) ((acmp)->CTL[(u32ChNum)] &= ~ACMP_CTL_WCMPSEL_Msk)
363 
364 
365 
366 
367 /* Function prototype declaration */
368 void ACMP_Open(ACMP_T *acmp, uint32_t u32ChNum, uint32_t u32NegSrc, uint32_t u32HysSel);
369 void ACMP_Close(ACMP_T *acmp, uint32_t u32ChNum);
370 
371 
372 /**@}*/ /* end of group ACMP_EXPORTED_FUNCTIONS */
373 /**@}*/ /* end of group ACMP_Driver */
374 /**@}*/ /* end of group Standard_Driver */
375 
376 #ifdef __cplusplus
377 }
378 #endif
379 
380 #endif /* __ACMP_H__ */
381