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