1 /**************************************************************************//**
2  * @file     acmp.h
3  * @version  V1.0
4  * @brief    M2L31 series ACMP driver header file
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  * @copyright (C) 2023 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 /** @addtogroup Standard_Driver Standard Driver
18   @{
19 */
20 
21 /** @addtogroup ACMP_Driver ACMP Driver
22   @{
23 */
24 
25 /** @addtogroup ACMP_EXPORTED_CONSTANTS ACMP Exported Constants
26   @{
27 */
28 
29 /*---------------------------------------------------------------------------------------------------------*/
30 /* ACMP_CTL constant definitions                                                                           */
31 /*---------------------------------------------------------------------------------------------------------*/
32 #define ACMP_CTL_FILTSEL_OFF         (0UL << ACMP_CTL_FILTSEL_Pos) /*!< ACMP_CTL setting for filter function disabled. \hideinitializer */
33 #define ACMP_CTL_FILTSEL_1PCLK       (1UL << ACMP_CTL_FILTSEL_Pos) /*!< ACMP_CTL setting for 1 PCLK filter count. \hideinitializer */
34 #define ACMP_CTL_FILTSEL_2PCLK       (2UL << ACMP_CTL_FILTSEL_Pos) /*!< ACMP_CTL setting for 2 PCLK filter count. \hideinitializer */
35 #define ACMP_CTL_FILTSEL_4PCLK       (3UL << ACMP_CTL_FILTSEL_Pos) /*!< ACMP_CTL setting for 4 PCLK filter count. \hideinitializer */
36 #define ACMP_CTL_FILTSEL_8PCLK       (4UL << ACMP_CTL_FILTSEL_Pos) /*!< ACMP_CTL setting for 8 PCLK filter count. \hideinitializer */
37 #define ACMP_CTL_FILTSEL_16PCLK      (5UL << ACMP_CTL_FILTSEL_Pos) /*!< ACMP_CTL setting for 16 PCLK filter count. \hideinitializer */
38 #define ACMP_CTL_FILTSEL_32PCLK      (6UL << ACMP_CTL_FILTSEL_Pos) /*!< ACMP_CTL setting for 32 PCLK filter count. \hideinitializer */
39 #define ACMP_CTL_FILTSEL_64PCLK      (7UL << ACMP_CTL_FILTSEL_Pos) /*!< ACMP_CTL setting for 64 PCLK filter count. \hideinitializer */
40 
41 #define ACMP_CTL_INTPOL_RF           (0UL << ACMP_CTL_INTPOL_Pos)  /*!< ACMP_CTL setting for selecting rising edge and falling edge as interrupt condition. \hideinitializer */
42 #define ACMP_CTL_INTPOL_R            (1UL << ACMP_CTL_INTPOL_Pos)  /*!< ACMP_CTL setting for selecting rising edge as interrupt condition. \hideinitializer */
43 #define ACMP_CTL_INTPOL_F            (2UL << ACMP_CTL_INTPOL_Pos)  /*!< ACMP_CTL setting for selecting falling edge as interrupt condition. \hideinitializer */
44 
45 #define ACMP_CTL_POSSEL_P0           (0UL << ACMP_CTL_POSSEL_Pos)  /*!< ACMP_CTL setting for selecting ACMPx_P0 pin as the source of ACMP V+. \hideinitializer */
46 #define ACMP_CTL_POSSEL_P1           (1UL << ACMP_CTL_POSSEL_Pos)  /*!< ACMP_CTL setting for selecting ACMPx_P1 pin as the source of ACMP V+. \hideinitializer */
47 #define ACMP_CTL_POSSEL_P2           (2UL << ACMP_CTL_POSSEL_Pos)  /*!< ACMP_CTL setting for selecting ACMPx_P2 pin as the source of ACMP V+. \hideinitializer */
48 #define ACMP_CTL_POSSEL_P3           (3UL << ACMP_CTL_POSSEL_Pos)  /*!< ACMP_CTL setting for selecting ACMPx_P3 pin as the source of ACMP V+. \hideinitializer */
49 #define ACMP_CTL_POSSEL_OPA0         (4UL << ACMP_CTL_POSSEL_Pos)  /*!< ACMP_CTL setting for selecting OPA0 output as the source of ACMP V+. \hideinitializer */
50 #define ACMP_CTL_POSSEL_OPA1         (5UL << ACMP_CTL_POSSEL_Pos)  /*!< ACMP_CTL setting for selecting OPA0 output as the source of ACMP V+. \hideinitializer */
51 #define ACMP_CTL_POSSEL_OPA2         (6UL << ACMP_CTL_POSSEL_Pos)  /*!< ACMP_CTL setting for selecting OPA0 output as the source of ACMP V+. \hideinitializer */
52 
53 #define ACMP_CTL_NEGSEL_PIN          (0UL << ACMP_CTL_NEGSEL_Pos)  /*!< ACMP_CTL setting for selecting the voltage of ACMP negative input pin as the source of ACMP V-. \hideinitializer */
54 #define ACMP_CTL_NEGSEL_CRV          (1UL << ACMP_CTL_NEGSEL_Pos)  /*!< ACMP_CTL setting for selecting internal comparator reference voltage as the source of ACMP V-. \hideinitializer */
55 #define ACMP_CTL_NEGSEL_VBG          (2UL << ACMP_CTL_NEGSEL_Pos)  /*!< ACMP_CTL setting for selecting internal Band-gap voltage as the source of ACMP V-. \hideinitializer */
56 #define ACMP_CTL_NEGSEL_DAC0         (3UL << ACMP_CTL_NEGSEL_Pos)  /*!< ACMP_CTL setting for selecting DAC0 output voltage as the source of ACMP V-. \hideinitializer */
57 #define ACMP_CTL_NEGSEL_DAC1         (4UL << ACMP_CTL_NEGSEL_Pos)  /*!< ACMP_CTL setting for selecting DAC1 output voltage as the source of ACMP V-. \hideinitializer */
58 
59 #define ACMP_CTL_HYSTERESIS_40MV     (4UL << ACMP_CTL_HYSSEL_Pos)  /*!< ACMP_CTL setting for enabling the hysteresis function at 40mV. \hideinitializer */
60 #define ACMP_CTL_HYSTERESIS_20MV     (2UL << ACMP_CTL_HYSSEL_Pos)  /*!< ACMP_CTL setting for enabling the hysteresis function at 20mV. \hideinitializer */
61 #define ACMP_CTL_HYSTERESIS_DISABLE  (0UL << ACMP_CTL_HYSSEL_Pos)  /*!< ACMP_CTL setting for disabling the hysteresis function. \hideinitializer */
62 
63 /*---------------------------------------------------------------------------------------------------------*/
64 /* ACMP_VREF constant definitions                                                                          */
65 /*---------------------------------------------------------------------------------------------------------*/
66 #define ACMP_VREF_CRV0SSEL_VDDA      (0UL << ACMP_VREF_CRV0SSEL_Pos)  /*!< ACMP_VREF setting for selecting analog supply voltage VDDA as the CRV0 source voltage \hideinitializer */
67 #define ACMP_VREF_CRV0SSEL_INTVREF   (1UL << ACMP_VREF_CRV0SSEL_Pos)  /*!< ACMP_VREF setting for selecting internal reference voltage as the CRV0 source voltage \hideinitializer */
68 #define ACMP_VREF_CRV1SSEL_VDDA      (0UL << ACMP_VREF_CRV1SSEL_Pos)  /*!< ACMP_VREF setting for selecting analog supply voltage VDDA as the CRV1 source voltage \hideinitializer */
69 #define ACMP_VREF_CRV1SSEL_INTVREF   (1UL << ACMP_VREF_CRV1SSEL_Pos)  /*!< ACMP_VREF setting for selecting internal reference voltage as the CRV1 source voltage \hideinitializer */
70 #define ACMP_VREF_CRV2SSEL_VDDA      (0UL << ACMP_VREF_CRV2SSEL_Pos)  /*!< ACMP_VREF setting for selecting analog supply voltage VDDA as the CRV2 source voltage \hideinitializer */
71 #define ACMP_VREF_CRV2SSEL_INTVREF   (1UL << ACMP_VREF_CRV2SSEL_Pos)  /*!< ACMP_VREF setting for selecting internal reference voltage as the CRV2 source voltage \hideinitializer */
72 
73 /*--------------------------------------------------------------------------------------------------*/
74 /* Define Error Code                                                                                */
75 /*--------------------------------------------------------------------------------------------------*/
76 #define ACMP_TIMEOUT_ERR            (-1)    /*!< ACMP operation abort due to timeout error \hideinitializer */
77 
78 /*@}*/ /* end of group ACMP_EXPORTED_CONSTANTS */
79 
80 extern int32_t g_ACMP_i32ErrCode;
81 
82 /** @addtogroup ACMP_EXPORTED_FUNCTIONS ACMP Exported Functions
83   @{
84 */
85 
86 /*---------------------------------------------------------------------------------------------------------*/
87 /*  Define Macros and functions                                                                            */
88 /*---------------------------------------------------------------------------------------------------------*/
89 
90 
91 /**
92   * @brief This macro is used to enable output inverse function
93   * @param[in] acmp The pointer of the specified ACMP module
94   * @param[in] u32ChNum The ACMP number
95   * @return None
96   * @details This macro will set ACMPOINV bit of ACMP_CTL register to enable output inverse function.
97   * \hideinitializer
98   */
99 #define ACMP_ENABLE_OUTPUT_INVERSE(acmp, u32ChNum) \
100     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] |= ACMP_CTL_ACMPOINV_Msk;}\
101     else if (acmp == ACMP2) {(acmp)->CTL[0] |= ACMP_CTL_ACMPOINV_Msk;}
102 
103 /**
104   * @brief This macro is used to disable output inverse function
105   * @param[in] acmp The pointer of the specified ACMP module
106   * @param[in] u32ChNum The ACMP number
107   * @return None
108   * @details This macro will clear ACMPOINV bit of ACMP_CTL register to disable output inverse function.
109   * \hideinitializer
110   */
111 #define ACMP_DISABLE_OUTPUT_INVERSE(acmp, u32ChNum) \
112     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] &= ~ACMP_CTL_ACMPOINV_Msk);}\
113     else if (acmp == ACMP2) {(acmp)->CTL[0] &= ~ACMP_CTL_ACMPOINV_Msk);}
114 
115 /**
116   * @brief This macro is used to select ACMP negative input source
117   * @param[in] acmp The pointer of the specified ACMP module
118   * @param[in] u32ChNum The ACMP number
119   * @param[in] u32Src is comparator negative input selection. Including:
120   *                  - \ref ACMP_CTL_NEGSEL_PIN
121   *                  - \ref ACMP_CTL_NEGSEL_CRV
122   *                  - \ref ACMP_CTL_NEGSEL_VBG
123   *                  - \ref ACMP_CTL_NEGSEL_DAC0
124   *                  - \ref ACMP_CTL_NEGSEL_DAC1
125   * @return None
126   * @details This macro will set NEGSEL to determine the source of negative input.
127   * \hideinitializer
128   */
129 #define ACMP_SET_NEG_SRC(acmp, u32ChNum, u32Src) \
130     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] = ((acmp)->CTL[u32ChNum] & ~ACMP_CTL_NEGSEL_Msk) | (u32Src);}\
131     else if (acmp == ACMP2) {(acmp)->CTL[0] = ((acmp)->CTL[0] & ~ACMP_CTL_NEGSEL_Msk) | (u32Src);}
132 
133 /**
134   * @brief This macro is used to enable hysteresis function and set hysteresis to 20mV
135   * @param[in] acmp The pointer of the specified ACMP module
136   * @param[in] u32ChNum The ACMP number
137   * @return None
138   * \hideinitializer
139   */
140 #define ACMP_ENABLE_HYSTERESIS(acmp, u32ChNum) \
141     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] = ((acmp)->CTL[u32ChNum] & ~ACMP_CTL_HYSSEL_Msk) | (ACMP_CTL_HYSTERESIS_20MV);}\
142     else if (acmp == ACMP2) {(acmp)->CTL[0] = ((acmp)->CTL[0] & ~ACMP_CTL_HYSSEL_Msk) | (ACMP_CTL_HYSTERESIS_20MV);}
143 
144 /**
145   * @brief This macro is used to disable hysteresis function
146   * @param[in] acmp The pointer of the specified ACMP module
147   * @param[in] u32ChNum The ACMP number
148   * @return None
149   * @details This macro will clear HYSEL bits of ACMP_CTL register to disable hysteresis function.
150   * \hideinitializer
151   */
152 #define ACMP_DISABLE_HYSTERESIS(acmp, u32ChNum) \
153     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] = ((acmp)->CTL[u32ChNum] & ~ACMP_CTL_HYSSEL_Msk) | (ACMP_CTL_HYSTERESIS_DISABLE);}\
154     else if (acmp == ACMP2) {(acmp)->CTL[0] = ((acmp)->CTL[0] & ~ACMP_CTL_HYSSEL_Msk) | (ACMP_CTL_HYSTERESIS_DISABLE);}
155 
156 /**
157   * @brief This macro is used to select hysteresis level
158   * @param[in] acmp The pointer of the specified ACMP module
159   * @param[in] u32ChNum The ACMP number
160   * @param[in] u32HysSel The hysteresis function option. Including:
161   *                  - \ref ACMP_CTL_HYSTERESIS_40MV
162   *                  - \ref ACMP_CTL_HYSTERESIS_20MV
163   *                  - \ref ACMP_CTL_HYSTERESIS_DISABLE
164   * \hideinitializer
165   * @return None
166   */
167 #define ACMP_CONFIG_HYSTERESIS(acmp, u32ChNum, u32HysSel) \
168     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] = ((acmp)->CTL[u32ChNum] & ~ACMP_CTL_HYSSEL_Msk) | (u32HysSel);}\
169     else if (acmp == ACMP2) {(acmp)->CTL[0] = ((acmp)->CTL[0] & ~ACMP_CTL_HYSSEL_Msk) | (u32HysSel);}
170 
171 /**
172   * @brief This macro is used to enable interrupt
173   * @param[in] acmp The pointer of the specified ACMP module
174   * @param[in] u32ChNum The ACMP number
175   * @return None
176   * @details This macro will set ACMPIE bit of ACMP_CTL register to enable interrupt function.
177   *          If wake-up function is enabled, the wake-up interrupt will be enabled as well.
178   * \hideinitializer
179   */
180 #define ACMP_ENABLE_INT(acmp, u32ChNum) \
181     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] |= ACMP_CTL_ACMPIE_Msk;}\
182     else if (acmp == ACMP2) {(acmp)->CTL[0] |= ACMP_CTL_ACMPIE_Msk;}
183 
184 /**
185   * @brief This macro is used to disable interrupt
186   * @param[in] acmp The pointer of the specified ACMP module
187   * @param[in] u32ChNum The ACMP number
188   * @return None
189   * @details This macro will clear ACMPIE bit of ACMP_CTL register to disable interrupt function.
190   * \hideinitializer
191   */
192 #define ACMP_DISABLE_INT(acmp, u32ChNum) \
193     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] &= ~ACMP_CTL_ACMPIE_Msk;}\
194     else if (acmp == ACMP2) {(acmp)->CTL[0] &= ~ACMP_CTL_ACMPIE_Msk;}
195 
196 /**
197   * @brief This macro is used to enable ACMP
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 set ACMPEN bit of ACMP_CTL register to enable analog comparator.
202   * \hideinitializer
203   */
204 #define ACMP_ENABLE(acmp, u32ChNum) \
205     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] |= ACMP_CTL_ACMPEN_Msk;}\
206     else if (acmp == ACMP2) {(acmp)->CTL[0] |= ACMP_CTL_ACMPEN_Msk;}
207 
208 /**
209   * @brief This macro is used to disable ACMP
210   * @param[in] acmp The pointer of the specified ACMP module
211   * @param[in] u32ChNum The ACMP number
212   * @return None
213   * @details This macro will clear ACMPEN bit of ACMP_CTL register to disable analog comparator.
214   * \hideinitializer
215   */
216 #define ACMP_DISABLE(acmp, u32ChNum) \
217     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] &= ~ACMP_CTL_ACMPEN_Msk;}\
218     else if (acmp == ACMP2) {(acmp)->CTL[0] &= ~ACMP_CTL_ACMPEN_Msk;}
219 
220 /**
221   * @brief This macro is used to get ACMP output value
222   * @param[in] acmp The pointer of the specified ACMP module
223   * @param[in] u32ChNum The ACMP number
224   * @return  ACMP output value
225   * @details This macro will return the ACMP output value.
226   * \hideinitializer
227   */
228 #define ACMP_GET_OUTPUT(acmp, u32ChNum) \
229     ( (acmp == ACMP01) ? \
230       (((acmp)->STATUS & (ACMP_STATUS_ACMPO0_Msk<<(u32ChNum)))?1:0) : \
231       (((acmp)->STATUS & (ACMP_STATUS_ACMPO0_Msk<<(0)))?1:0) )
232 
233 /**
234   * @brief This macro is used to get ACMP interrupt flag
235   * @param[in] acmp The pointer of the specified ACMP module
236   * @param[in] u32ChNum The ACMP number
237   * @return   ACMP interrupt occurred (1) or not (0)
238   * @details This macro will return the ACMP interrupt flag.
239   * \hideinitializer
240   */
241 #define ACMP_GET_INT_FLAG(acmp, u32ChNum) \
242     ( (acmp == ACMP01) ? \
243       (((acmp)->STATUS & (ACMP_STATUS_ACMPIF0_Msk<<(u32ChNum)))?1:0) : \
244       (((acmp)->STATUS & (ACMP_STATUS_ACMPIF0_Msk<<(0)))?1:0) )
245 
246 /**
247   * @brief This macro is used to clear ACMP interrupt flag
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 write 1 to ACMPIFn bit of ACMP_STATUS register to clear interrupt flag.
252   * \hideinitializer
253   */
254 #define ACMP_CLR_INT_FLAG(acmp, u32ChNum) \
255     if (acmp == ACMP01) {(acmp)->STATUS = (ACMP_STATUS_ACMPIF0_Msk<<(u32ChNum));}\
256     else if (acmp == ACMP2) {(acmp)->STATUS = (ACMP_STATUS_ACMPIF0_Msk<<(0));}
257 
258 /**
259   * @brief This macro is used to clear ACMP wake-up interrupt flag
260   * @param[in] acmp The pointer of the specified ACMP module
261   * @param[in] u32ChNum The ACMP number
262   * @return   None
263   * @details This macro will write 1 to WKIFn bit of ACMP_STATUS register to clear interrupt flag.
264   * \hideinitializer
265   */
266 #define ACMP_CLR_WAKEUP_INT_FLAG(acmp, u32ChNum) \
267     if (acmp == ACMP01) {(acmp)->STATUS = (ACMP_STATUS_WKIF0_Msk<<(u32ChNum));}\
268     else if (acmp == ACMP2) {(acmp)->STATUS = (ACMP_STATUS_WKIF0_Msk<<(0));}
269 
270 /**
271   * @brief This macro is used to enable ACMP wake-up function
272   * @param[in] acmp The pointer of the specified ACMP module
273   * @param[in] u32ChNum The ACMP number
274   * @return None
275   * @details This macro will set WKEN to enable ACMP wake-up function.
276   * \hideinitializer
277   */
278 #define ACMP_ENABLE_WAKEUP(acmp, u32ChNum) \
279     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] |= ACMP_CTL_WKEN_Msk;}\
280     else if (acmp == ACMP2) {(acmp)->CTL[0] |= ACMP_CTL_WKEN_Msk;}
281 
282 /**
283   * @brief This macro is used to disable ACMP wake-up function
284   * @param[in] acmp The pointer of the specified ACMP module
285   * @param[in] u32ChNum The ACMP number
286   * @return None
287   * @details This macro will clear WKEN to disable ACMP wake-up function.
288   * \hideinitializer
289   */
290 #define ACMP_DISABLE_WAKEUP(acmp, u32ChNum) \
291     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] &= ~ACMP_CTL_WKEN_Msk;}\
292     else if (acmp == ACMP2) {(acmp)->CTL[0] &= ~ACMP_CTL_WKEN_Msk;}
293 
294 /**
295   * @brief This macro is used to select ACMP positive input pin
296   * @param[in] acmp The pointer of the specified ACMP module
297   * @param[in] u32ChNum The ACMP number
298   * @param[in] u32Pin Comparator positive pin selection. Including:
299   *                  - \ref ACMP_CTL_POSSEL_P0
300   *                  - \ref ACMP_CTL_POSSEL_P1
301   *                  - \ref ACMP_CTL_POSSEL_P2
302   *                  - \ref ACMP_CTL_POSSEL_P3
303   *                  - \ref ACMP_CTL_POSSEL_OPA0
304   *                  - \ref ACMP_CTL_POSSEL_OPA1
305   *                  - \ref ACMP_CTL_POSSEL_OPA2
306   * @return None
307   * @details This macro will set POSSEL to determine the comparator positive input pin.
308   * \hideinitializer
309   */
310 #define ACMP_SELECT_P(acmp, u32ChNum, u32Pin) \
311     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] = ((acmp)->CTL[u32ChNum] & ~ACMP_CTL_POSSEL_Msk) | (u32Pin);}\
312     else if (acmp == ACMP2) {(acmp)->CTL[0] = ((acmp)->CTL[0] & ~ACMP_CTL_POSSEL_Msk) | (u32Pin);}
313 
314 /**
315   * @brief This macro is used to enable ACMP filter function
316   * @param[in] acmp The pointer of the specified ACMP module
317   * @param[in] u32ChNum The ACMP number
318   * @return None
319   * @details This macro will set OUTSEL to enable output filter function.
320   * \hideinitializer
321   */
322 #define ACMP_ENABLE_FILTER(acmp, u32ChNum) \
323     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] |= ACMP_CTL_OUTSEL_Msk;}\
324     else if (acmp == ACMP2) {(acmp)->CTL[0] |= ACMP_CTL_OUTSEL_Msk;}
325 
326 /**
327   * @brief This macro is used to disable ACMP filter function
328   * @param[in] acmp The pointer of the specified ACMP module
329   * @param[in] u32ChNum The ACMP number
330   * @return None
331   * @details This macro will clear OUTSEL to disable output filter function.
332   * \hideinitializer
333   */
334 #define ACMP_DISABLE_FILTER(acmp, u32ChNum) \
335     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] &= ~ACMP_CTL_OUTSEL_Msk;}\
336     else if (acmp == ACMP2) {(acmp)->CTL[0] &= ~ACMP_CTL_OUTSEL_Msk;}
337 
338 /**
339   * @brief This macro is used to set ACMP filter function
340   * @param[in] acmp The pointer of the specified ACMP module
341   * @param[in] u32ChNum The ACMP number
342   * @param[in] u32Cnt is comparator filter count setting.
343   *                  - \ref ACMP_CTL_FILTSEL_OFF
344   *                  - \ref ACMP_CTL_FILTSEL_1PCLK
345   *                  - \ref ACMP_CTL_FILTSEL_2PCLK
346   *                  - \ref ACMP_CTL_FILTSEL_4PCLK
347   *                  - \ref ACMP_CTL_FILTSEL_8PCLK
348   *                  - \ref ACMP_CTL_FILTSEL_16PCLK
349   *                  - \ref ACMP_CTL_FILTSEL_32PCLK
350   *                  - \ref ACMP_CTL_FILTSEL_64PCLK
351   * @return None
352   * @details When ACMP output filter function is enabled, the output sampling count is determined by FILTSEL.
353   * \hideinitializer
354   */
355 #define ACMP_SET_FILTER(acmp, u32ChNum, u32Cnt) \
356     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] = ((acmp)->CTL[u32ChNum] & ~ACMP_CTL_FILTSEL_Msk) | (u32Cnt);}\
357     else if (acmp == ACMP2) {(acmp)->CTL[0] = ((acmp)->CTL[0] & ~ACMP_CTL_FILTSEL_Msk) | (u32Cnt);}
358 
359 /**
360   * @brief This macro is used to select comparator reference voltage CRV0
361   * @param[in] acmp The pointer of the specified ACMP module
362   * @param[in] u32Level  The comparator reference voltage setting.
363   *             The formula is:
364   *                       comparator reference voltage CRV0 = CRV0 source voltage * u32Level / 63
365   *             The range of u32Level is 0 ~ 63.
366   * @return   None
367   * @details  When CRV0 is selected as ACMP negative input source, the CRV0 level is determined by CRV0SEL.
368   * \hideinitializer
369   */
370 #define ACMP_CRV0_SEL(acmp, u32Level) ((acmp)->VREF = ((acmp)->VREF & ~ACMP_VREF_CRV0SEL_Msk) | ((u32Level)<<ACMP_VREF_CRV0SEL_Pos))
371 
372 /**
373   * @brief This macro is used to select the source of CRV0
374   * @param[in] acmp The pointer of the specified ACMP module
375   * @param[in] u32Src is the source of CRV0. Including:
376   *                  - \ref ACMP_VREF_CRV0SSEL_VDDA
377   *                  - \ref ACMP_VREF_CRV0SSEL_INTVREF
378   * @return None
379   * @details The source of CRV0 can be VDDA or internal reference voltage. The internal reference voltage level is determined by SYS_VREFCTL register.
380   * \hideinitializer
381   */
382 #define ACMP_SELECT_CRV0_SRC(acmp, u32Src) ((acmp)->VREF = ((acmp)->VREF & ~ACMP_VREF_CRV0SSEL_Msk) | (u32Src))
383 
384 /**
385   * @brief This macro is used to select comparator reference voltage CRV1
386   * @param[in] acmp The pointer of the specified ACMP module
387   * @param[in] u32Level  The comparator reference voltage setting.
388   *             The formula is:
389   *                       comparator reference voltage CRV1 = CRV1 source voltage * u32Level / 63
390   *             The range of u32Level is 0 ~ 63.
391   * @return   None
392   * @details  When CRV1 is selected as ACMP negative input source, the CRV1 level is determined by CRV1SEL.
393   * \hideinitializer
394   */
395 #define ACMP_CRV1_SEL(acmp, u32Level) ((acmp)->VREF = ((acmp)->VREF & ~ACMP_VREF_CRV1SEL_Msk) | ((u32Level)<<ACMP_VREF_CRV1SEL_Pos))
396 
397 /**
398   * @brief This macro is used to select the source of CRV1
399   * @param[in] acmp The pointer of the specified ACMP module
400   * @param[in] u32Src is the source of CRV1. Including:
401   *                  - \ref ACMP_VREF_CRV1SSEL_VDDA
402   *                  - \ref ACMP_VREF_CRV1SSEL_INTVREF
403   * @return None
404   * @details The source of CRV1 can be VDDA or internal reference voltage. The internal reference voltage level is determined by SYS_VREFCTL register.
405   * \hideinitializer
406   */
407 #define ACMP_SELECT_CRV1_SRC(acmp, u32Src) ((acmp)->VREF = ((acmp)->VREF & ~ACMP_VREF_CRV1SSEL_Msk) | (u32Src))
408 
409 /**
410   * @brief This macro is used to select comparator reference voltage CRV2
411   * @param[in] acmp The pointer of the specified ACMP module
412   * @param[in] u32Level  The comparator reference voltage setting.
413   *             The formula is:
414   *                       comparator reference voltage CRV2 = CRV2 source voltage * u32Level / 63
415   *             The range of u32Level is 0 ~ 63.
416   * @return   None
417   * @details  When CRV2 is selected as ACMP negative input source, the CRV2 level is determined by CRV2SEL.
418   * \hideinitializer
419   */
420 #define ACMP_CRV2_SEL(acmp, u32Level) ((acmp)->VREF = ((acmp)->VREF & ~ACMP_VREF_CRV2SEL_Msk) | ((u32Level)<<ACMP_VREF_CRV2SEL_Pos))
421 
422 /**
423   * @brief This macro is used to select the source of CRV2
424   * @param[in] acmp The pointer of the specified ACMP module
425   * @param[in] u32Src is the source of CRV2. Including:
426   *                  - \ref ACMP_VREF_CRV2SSEL_VDDA
427   *                  - \ref ACMP_VREF_CRV2SSEL_INTVREF
428   * @return None
429   * @details The source of CRV2 can be VDDA or internal reference voltage. The internal reference voltage level is determined by SYS_VREFCTL register.
430   * \hideinitializer
431   */
432 #define ACMP_SELECT_CRV2_SRC(acmp, u32Src) ((acmp)->VREF = ((acmp)->VREF & ~ACMP_VREF_CRV2SSEL_Msk) | (u32Src))
433 
434 /**
435   * @brief This macro is used to select ACMP interrupt condition
436   * @param[in] acmp The pointer of the specified ACMP module
437   * @param[in] u32ChNum The ACMP number
438   * @param[in] u32Cond Comparator interrupt condition selection. Including:
439   *                  - \ref ACMP_CTL_INTPOL_RF
440   *                  - \ref ACMP_CTL_INTPOL_R
441   *                  - \ref ACMP_CTL_INTPOL_F
442   * @return None
443   * @details The ACMP output interrupt condition can be rising edge, falling edge or any edge.
444   * \hideinitializer
445   */
446 #define ACMP_SELECT_INT_COND(acmp, u32ChNum, u32Cond) \
447     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] = ((acmp)->CTL[u32ChNum] & ~ACMP_CTL_INTPOL_Msk) | (u32Cond);}\
448     else if (acmp == ACMP2) {(acmp)->CTL[0] = ((acmp)->CTL[0] & ~ACMP_CTL_INTPOL_Msk) | (u32Cond);}
449 
450 /**
451   * @brief This macro is used to enable ACMP window latch mode
452   * @param[in] acmp The pointer of the specified ACMP module
453   * @param[in] u32ChNum The ACMP number
454   * @return None
455   * @details This macro will set WLATEN to enable ACMP window latch mode.
456   *          When ACMP_WLAT pin is at high level, ACMPO passes through window latch
457   *          block; when ACMP_WLAT pin is at low level, the output of window latch block,
458   *          WLATOUT, is frozen.
459   * \hideinitializer
460   */
461 #define ACMP_ENABLE_WINDOW_LATCH(acmp, u32ChNum) \
462     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] |= ACMP_CTL_WLATEN_Msk;}\
463     else if (acmp == ACMP2) {(acmp)->CTL[0] |= ACMP_CTL_WLATEN_Msk;}
464 
465 /**
466   * @brief This macro is used to disable ACMP window latch mode
467   * @param[in] acmp The pointer of the specified ACMP module
468   * @param[in] u32ChNum The ACMP number
469   * @return None
470   * @details This macro will clear WLATEN to disable ACMP window latch mode.
471   * \hideinitializer
472   */
473 #define ACMP_DISABLE_WINDOW_LATCH(acmp, u32ChNum) \
474     if (acmp == ACMP01) {(acmp)->CTL[u32ChNum] &= ~ACMP_CTL_WLATEN_Msk;}\
475     else if (acmp == ACMP2) {(acmp)->CTL[0] &= ~ACMP_CTL_WLATEN_Msk;}
476 
477 /**
478   * @brief This macro is used to enable ACMP window compare mode
479   * @param[in] acmp The pointer of the specified ACMP module
480   * @param[in] u32ChNum The ACMP number
481   * @return None
482   * @details This macro will set WCMPSEL to enable ACMP window compare mode.
483   *          When window compare mode is enabled, user can connect the specific analog voltage
484   *          source to either the positive inputs of both comparators or the negative inputs of
485   *          both comparators. The upper bound and lower bound of the designated range are
486   *          determined by the voltages applied to the other inputs of both comparators. If the
487   *          output of a comparator is low and the other comparator outputs high, which means two
488   *          comparators implies the upper and lower bound. User can directly monitor a specific
489   *          analog voltage source via ACMPWO.
490   * \hideinitializer
491   */
492 #define ACMP_ENABLE_WINDOW_COMPARE(acmp, u32ChNum) ((acmp)->CTL[u32ChNum] |= ACMP_CTL_WCMPSEL_Msk)
493 
494 /**
495   * @brief This macro is used to disable ACMP window compare mode
496   * @param[in] acmp The pointer of the specified ACMP module
497   * @param[in] u32ChNum The ACMP number
498   * @return None
499   * @details This macro will clear WCMPSEL to disable ACMP window compare mode.
500   * \hideinitializer
501   */
502 #define ACMP_DISABLE_WINDOW_COMPARE(acmp, u32ChNum) ((acmp)->CTL[u32ChNum] &= ~ACMP_CTL_WCMPSEL_Msk)
503 
504 /* Function prototype declaration */
505 void ACMP_Open(ACMP_T *acmp, uint32_t u32ChNum, uint32_t u32NegSrc, uint32_t u32HysSel);
506 void ACMP_Close(ACMP_T *acmp, uint32_t u32ChNum);
507 
508 /*@}*/ /* end of group ACMP_EXPORTED_FUNCTIONS */
509 
510 /*@}*/ /* end of group ACMP_Driver */
511 
512 /*@}*/ /* end of group Standard_Driver */
513 
514 #ifdef __cplusplus
515 }
516 #endif
517 
518 #endif /* __ACMP_H__ */
519 
520 /*** (C) COPYRIGHT 2023 Nuvoton Technology Corp. ***/
521