1 /******************************************************************************
2 * @brief header file for ACMP utilities.
3 *
4 *******************************************************************************
5 *
6 * provide APIs for accessing ACMP
7 ******************************************************************************/
8 #ifndef _MY_ACMP_H_
9 #define _MY_ACMP_H_
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 /******************************************************************************
14 * Includes
15 ******************************************************************************/
16
17 /******************************************************************************
18 * Constants
19 ******************************************************************************/
20
21 /* DAC reference select */
22 enum
23 {
24 DAC_REF_BANDGAP = 0,
25 DAC_REF_VDDA
26 };
27
28 /******************************************************************************
29 * Macros
30 ******************************************************************************/
31
32 /******************************************************************************
33 * ACMP module number definition *
34 ******************************************************************************/
35 #define MAX_ACMP_NO 2
36
37 /******************************************************************************
38 * ACMP positive and negative pin select definition
39 *
40 *//*! @addtogroup acmp_pinsel_list
41 * @{
42 *******************************************************************************/
43 #define ACMP_INPUT_P_EXT0 (0<<4) /*!< positive pin select external pin 0 */
44 #define ACMP_INPUT_P_EXT1 (1<<4) /*!< positive pin select external pin 1 */
45 #define ACMP_INPUT_P_EXT2 (2<<4) /*!< positive pin select external pin 2 */
46 #define ACMP_INPUT_P_DAC (3<<4) /*!< positive pin select internal DAC */
47
48 #define ACMP_INPUT_N_EXT0 0 /*!< positive pin select external pin 0 */
49 #define ACMP_INPUT_N_EXT1 1 /*!< positive pin select external pin 1 */
50 #define ACMP_INPUT_N_EXT2 2 /*!< positive pin select external pin 2 */
51 #define ACMP_INPUT_N_DAC 3 /*!< positive pin select internal DAC */
52 /*! @} End of acmp_pinsel_list */
53
54 /******************************************************************************
55 * ACMP interrupt sensitivity edge definition
56 *
57 *//*! @addtogroup acmp_intedgesel
58 * @{
59 *******************************************************************************/
60 #define ACMP_SENSITIVITYMODE_FALLING 0 /*!< interrupt on falling edge */
61 #define ACMP_SENSITIVITYMODE_RISING 1 /*!< interrupt on rising edge */
62 #define ACMP_SENSITIVITYMODE_ANY 3 /*!< interrupt on falling or rising edge */
63 /*! @} End of acmp_intedgesel */
64
65
66 /******************************************************************************
67 * ACMP hysterisis selection definition
68 *
69 *//*! @addtogroup acmp_hyst
70 * @{
71 *******************************************************************************/
72 #define ACMP_HYST_20MV (0<<6) /*!< 20mv hyst */
73 #define ACMP_HYST_30MV (1<<6) /*!< 30mv hyst */
74 /*! @} End of acmp_hyst */
75
76
77 /******************************************************************************
78 * ACMP internal DAC reference selection definition
79 *
80 *//*! @addtogroup acmp_dacref
81 * @{
82 *******************************************************************************/
83 #define ACMP_DAC_REFERENCE_BANDGAP (0<<6) /*!< select bandgap as refference */
84 #define ACMP_DAC_REFERENCE_VDDA (1<<6) /*!< select VDDA as refference */
85 /*! @} End of acmp_dacref */
86
87
88 /******************************************************************************
89 * Types
90 ******************************************************************************/
91
92 /*! @brief ACMP_CALLBACK function declaration */
93 typedef void (*ACMP_CallbackPtr)(void);
94 /*! @} End of acmp_callback */
95
96 /******************************************************************************
97 * ACMP control status struct
98 *
99 *//*! @addtogroup acmp_ctrlstatusstruct
100 * @{
101 *******************************************************************************/
102 /*!
103 * @brief ACMP control and status fields type.
104 *
105 */
106
107 typedef union
108 {
109 uint8_t byte; /*!< byte field of union type */
110 struct
111 {
112 uint8_t bMod : 2; /*!< Sensitivity modes of the interrupt trigger */
113 uint8_t bOutEn : 1; /*!< Output can be placed onto an external pin */
114 uint8_t bOutState : 1; /*!< The current value of the analog comparator output */
115 uint8_t bIntEn : 1; /*!< ACMP interrupt enable */
116 uint8_t bIntFlag : 1; /*!< ACMP Interrupt Flag Bit */
117 uint8_t bHyst : 1; /*!< Selects ACMP hystersis */
118 uint8_t bEn : 1; /*!< Enables the ACMP module */
119 }bits; /*!< bitfield of union type */
120 }ACMP_CtrlStatusType, *ACMP_CtrlStatusPtr; /*!< ACMP Control/Status reg structure */
121 /*! @} End of acmp_ctrlstatusstruct */
122
123 /******************************************************************************
124 * ACMP pin select struct
125 *
126 *//*! @addtogroup acmp_pinselectstruct
127 * @{
128 *******************************************************************************/
129 /*!
130 * @brief ACMP external pins control struct.
131 *
132 */
133
134 typedef union
135 {
136 uint8_t byte; /*!< byte field of union type */
137 struct
138 {
139 uint8_t bNegPin : 2; /*!< Negative pin select */
140 uint8_t : 2;
141 uint8_t bPosPin : 2; /*!< Positive pin select */
142 uint8_t : 2;
143 }bits; /*!< bitfield of union type */
144 }ACMP_PinSelType, *ACMP_PinSelPtr; /*!< ACMP Pin select structure */
145 /*! @} End of acmp_pinselectstruct */
146
147 /******************************************************************************
148 * ACMP DAC control struct
149 *
150 *//*! @addtogroup acmp_dacctrlstruct
151 * @{
152 *******************************************************************************/
153 /*!
154 * @brief ACMP internal ADC control struct.
155 *
156 */
157 typedef union
158 {
159 uint8_t byte; /*!< byte field of union type */
160 struct
161 {
162 uint8_t bVal : 6; /*!< 6 bit DAC value */
163 uint8_t bRef : 1; /*!< 6 bit DAC reference select */
164 uint8_t bEn : 1; /*!< 6 bit DAC enable bit */
165 }bits; /*!< bitfield of union type */
166 }ACMP_DACType, *ACMP_DACPtr; /*!< ACMP DAC control structure */
167 /*! @} End of acmp_dacctrlstruct */
168
169 /******************************************************************************
170 * ACMP pin enable union
171 *
172 *//*! @addtogroup acmp_pinenunion
173 * @{
174 *******************************************************************************/
175 /*!
176 * @brief ACMP external input pin enable control struct.
177 *
178 */
179 typedef union
180 {
181 uint8_t byte; /*!< byte field of union type */
182 struct
183 {
184 uint8_t bEn : 3; /*!< ACMP external input pin enable */
185 uint8_t bRsvd : 5;
186 }bits; /*!< bitfield of union type */
187 }ACMP_PinEnType, *ACMP_PinEnPtr; /*!< ACMP Pin enable structure */
188 /*! @} End of acmp_pinenunion */
189
190 /******************************************************************************
191 * ACMP config struct
192 *
193 *//*! @addtogroup acmp_configstruct
194 * @{
195 *******************************************************************************/
196 /*!
197 * @brief ACMP module configuration struct.
198 *
199 */
200
201 typedef struct
202 {
203 ACMP_CtrlStatusType sCtrlStatus; /*!< ACMP control and status */
204 ACMP_PinSelType sPinSelect; /*!< ACMP pin select */
205 ACMP_DACType sDacSet; /*!< ACMP internal dac set */
206 ACMP_PinEnType sPinEnable; /*!< ACMP external pin control */
207 }ACMP_ConfigType, *ACMP_ConfigPtr;
208 /*! @} End of acmp_configstruct */
209
210 /******************************************************************************
211 * Global variables
212 ******************************************************************************/
213
214 /*!
215 * inline functions
216 */
217 /******************************************************************************
218 * ACMP api list.
219 *
220 *//*! @addtogroup acmp_api_list
221 * @{
222 *******************************************************************************/
223 /*****************************************************************************//*!
224 *
225 * @brief enable the acmp module.
226 *
227 * @param[in] pACMPx pointer to an ACMP module.
228 *
229 * @return none.
230 *
231 * @ Pass/ Fail criteria: none.
232 *
233 * @see ACMP_Disable.
234 *
235 *****************************************************************************/
ACMP_Enable(ACMP_Type * pACMPx)236 __STATIC_INLINE void ACMP_Enable(ACMP_Type *pACMPx)
237 {
238 pACMPx->CS |= ACMP_CS_ACE_MASK;
239 }
240
241 /*****************************************************************************//*!
242 *
243 * @brief disable the acmp module.
244 *
245 * @param[in] pACMPx pointer to an ACMP module.
246 *
247 * @return none.
248 *
249 * @ Pass/ Fail criteria: none.
250 *
251 * @see ACMP_Enable.
252 *
253 *****************************************************************************/
ACMP_Disable(ACMP_Type * pACMPx)254 __STATIC_INLINE void ACMP_Disable(ACMP_Type *pACMPx)
255 {
256 pACMPx->CS &= ~ACMP_CS_ACE_MASK;
257 }
258
259 /*****************************************************************************//*!
260 *
261 * @brief select sensitivity modes of the interrupt trigger.
262 *
263 * @param[in] pACMPx pointer to an ACMP module.
264 * @param[in] u8EdgeSelect falling or rising selction, 0~3.
265 *
266 * @return none.
267 *
268 * @ Pass/ Fail criteria: none.
269 *
270 *****************************************************************************/
ACMP_SelectIntMode(ACMP_Type * pACMPx,uint8_t u8EdgeSelect)271 __STATIC_INLINE void ACMP_SelectIntMode(ACMP_Type *pACMPx, uint8_t u8EdgeSelect)
272 {
273 pACMPx->CS &= ~ACMP_CS_ACMOD_MASK;
274 pACMPx->CS |= ACMP_CS_ACMOD(u8EdgeSelect & 0x3);
275 }
276
277 /*****************************************************************************//*!
278 *
279 * @brief enable the ACMP module analog comparator output.
280 *
281 * @param[in] pACMPx pointer to an ACMP module.
282 *
283 * @return none.
284 *
285 * @ Pass/ Fail criteria: none.
286 *
287 * @see ACMP_DisablePinOut.
288 *
289 *****************************************************************************/
ACMP_EnablePinOut(ACMP_Type * pACMPx)290 __STATIC_INLINE void ACMP_EnablePinOut(ACMP_Type *pACMPx)
291 {
292 pACMPx->CS |= ACMP_CS_ACOPE_MASK;
293 }
294
295 /*****************************************************************************//*!
296 *
297 * @brief disable the ACMP module analog comparator output.
298 *
299 * @param[in] pACMPx pointer to an ACMP module.
300 *
301 * @return none.
302 *
303 * @ Pass/ Fail criteria: none.
304 *
305 * @see ACMP_EnablePinOut.
306 *
307 *****************************************************************************/
ACMP_DisablePinOut(ACMP_Type * pACMPx)308 __STATIC_INLINE void ACMP_DisablePinOut(ACMP_Type *pACMPx)
309 {
310 pACMPx->CS &= ~ACMP_CS_ACOPE_MASK;
311 }
312
313 /*****************************************************************************//*!
314 *
315 * @brief select ACMP hystersis.
316 *
317 * @param[in] pACMPx pointer to an ACMP module.
318 * @param[in] u8HystSelect ACMP_HYST_20MV or ACMP_HYST_30MV.
319 *
320 * @return none.
321 *
322 * @ Pass/ Fail criteria: none.
323 *
324 *****************************************************************************/
ACMP_SelectHyst(ACMP_Type * pACMPx,uint8_t u8HystSelect)325 __STATIC_INLINE void ACMP_SelectHyst(ACMP_Type *pACMPx, uint8_t u8HystSelect)
326 {
327 pACMPx->CS &= ~ACMP_CS_HYST_MASK;
328 pACMPx->CS |= u8HystSelect;
329 }
330
331 /*****************************************************************************//*!
332 *
333 * @brief enable the acmp module interrupt.
334 *
335 * @param[in] pACMPx pointer to an ACMP module.
336 *
337 * @return none.
338 *
339 * @ Pass/ Fail criteria: none.
340 *
341 * @see ACMP_DisableInterrupt.
342 *
343 *****************************************************************************/
ACMP_EnableInterrupt(ACMP_Type * pACMPx)344 __STATIC_INLINE void ACMP_EnableInterrupt(ACMP_Type *pACMPx)
345 {
346 pACMPx->CS |= ACMP_CS_ACIE_MASK;
347 }
348
349 /*****************************************************************************//*!
350 *
351 * @brief disable the acmp module interrupt.
352 *
353 * @param[in] pACMPx pointer to an ACMP module.
354 *
355 * @return none.
356 *
357 * @ Pass/ Fail criteria: none.
358 *
359 * @see ACMP_EnableInterrupt.
360 *
361 *****************************************************************************/
ACMP_DisableInterrupt(ACMP_Type * pACMPx)362 __STATIC_INLINE void ACMP_DisableInterrupt(ACMP_Type *pACMPx)
363 {
364 pACMPx->CS &= ~ACMP_CS_ACIE_MASK;
365 }
366
367 /*****************************************************************************//*!
368 *
369 * @brief get the interrupt flag bit.
370 *
371 * @param[in] pACMPx pointer to an ACMP module.
372 *
373 * @return none.
374 *
375 * @ Pass/ Fail criteria: none.
376 *
377 * @see ACMP_ClrFlag.
378 *
379 *****************************************************************************/
ACMP_GetFlag(ACMP_Type * pACMPx)380 __STATIC_INLINE uint8_t ACMP_GetFlag(ACMP_Type *pACMPx)
381 {
382 return (pACMPx->CS & ACMP_CS_ACF_MASK);
383 }
384
385 /*****************************************************************************//*!
386 *
387 * @brief clear the interrupt flag bit.
388 *
389 * @param[in] pACMPx pointer to an ACMP module.
390 *
391 * @return none.
392 *
393 * @ Pass/ Fail criteria: none.
394 *
395 * @see ACMP_GetFlag.
396 *
397 *****************************************************************************/
ACMP_ClrFlag(ACMP_Type * pACMPx)398 __STATIC_INLINE void ACMP_ClrFlag(ACMP_Type *pACMPx)
399 {
400 pACMPx->CS &= ~ACMP_CS_ACF_MASK;
401 }
402
403 /*****************************************************************************//*!
404 *
405 * @brief ACMP Positive Input Select.
406 *
407 * @param[in] pACMPx pointer to an ACMP module.
408 * @param[in] u8PosPinSel positive input select, ACMP_INPUT_P_EXT0~2 or ACMP_INPUT_P_DAC.
409 *
410 * @return none.
411 *
412 * @ Pass/ Fail criteria: none.
413 *
414 * @see ACMP_NegativeInputSelect.
415 *
416 *****************************************************************************/
ACMP_PositiveInputSelect(ACMP_Type * pACMPx,uint8_t u8PosPinSel)417 __STATIC_INLINE void ACMP_PositiveInputSelect(ACMP_Type *pACMPx, uint8_t u8PosPinSel)
418 {
419 pACMPx->C0 &= ~ACMP_C0_ACPSEL_MASK;
420 pACMPx->C0 |= u8PosPinSel;
421 }
422
423 /*****************************************************************************//*!
424 *
425 * @brief ACMP Negative Input Select.
426 *
427 * @param[in] pACMPx pointer to an ACMP module.
428 * @param[in] u8NegPinSel negative input select, ACMP_INPUT_N_EXT0~2 or ACMP_INPUT_N_DAC.
429 *
430 * @return none.
431 *
432 * @ Pass/ Fail criteria: none.
433 *
434 * @see ACMP_PositiveInputSelect.
435 *
436 *****************************************************************************/
ACMP_NegativeInputSelect(ACMP_Type * pACMPx,uint8_t u8NegPinSel)437 __STATIC_INLINE void ACMP_NegativeInputSelect(ACMP_Type *pACMPx, uint8_t u8NegPinSel)
438 {
439 pACMPx->C0 &= ~ACMP_C0_ACNSEL_MASK;
440 pACMPx->C0 |= u8NegPinSel;
441 }
442
443 /*****************************************************************************//*!
444 *
445 * @brief Enable 6 bit DAC in ACMP module.
446 *
447 * @param[in] pACMPx pointer to an ACMP module.
448 *
449 * @return none.
450 *
451 * @ Pass/ Fail criteria: none.
452 *
453 * @see ACMP_DacDisable.
454 *
455 *****************************************************************************/
ACMP_DacEnable(ACMP_Type * pACMPx)456 __STATIC_INLINE void ACMP_DacEnable(ACMP_Type *pACMPx)
457 {
458 pACMPx->C1 |= ACMP_C1_DACEN_MASK;
459 }
460
461 /*****************************************************************************//*!
462 *
463 * @brief Disable 6 bit DAC in ACMP module.
464 *
465 * @param[in] pACMPx pointer to an ACMP module.
466 *
467 * @return none.
468 *
469 * @ Pass/ Fail criteria: none.
470 *
471 * @see ACMP_DacEnable.
472 *
473 *****************************************************************************/
ACMP_DacDisable(ACMP_Type * pACMPx)474 __STATIC_INLINE void ACMP_DacDisable(ACMP_Type *pACMPx)
475 {
476 pACMPx->C1 &= ~ACMP_C1_DACEN_MASK;
477 }
478
479 /*****************************************************************************//*!
480 *
481 * @brief ACMP 6 bit DAC Reference Select.
482 *
483 * @param[in] pACMPx pointer to an ACMP module.
484 * @param[in] u8RefSelect dac reference select:ACMP_DAC_REFERENCE_BANDGAP or ACMP_DAC_REFERENCE_VDDA.
485 *
486 * @return none.
487 *
488 * @ Pass/ Fail criteria: none.
489 *
490 *****************************************************************************/
ACMP_DacReferenceSelect(ACMP_Type * pACMPx,uint8_t u8RefSelect)491 __STATIC_INLINE void ACMP_DacReferenceSelect(ACMP_Type *pACMPx, uint8_t u8RefSelect)
492 {
493 pACMPx->C1 &= ~ACMP_C1_DACREF_MASK;
494 pACMPx->C1 |= u8RefSelect;
495 }
496
497 /*****************************************************************************//*!
498 *
499 * @brief ACMP 6 bit DAC Output Value Set.
500 *
501 * @param[in] pACMPx pointer to an ACMP module.
502 * @param[in] u8DacValue dac output set, Voutput= (Vin/64)x(DACVAL[5:0]+1).
503 *
504 * @return none.
505 *
506 * @ Pass/ Fail criteria: none.
507 *
508 *****************************************************************************/
ACMP_DacOutputSet(ACMP_Type * pACMPx,uint8_t u8DacValue)509 __STATIC_INLINE void ACMP_DacOutputSet(ACMP_Type *pACMPx, uint8_t u8DacValue)
510 {
511 ASSERT(!(u8DacValue & (~ACMP_C1_DACVAL_MASK)));
512 pACMPx->C1 &= ~ACMP_C1_DACVAL_MASK;
513 pACMPx->C1 |= ACMP_C1_DACVAL(u8DacValue);
514 }
515
516 /*****************************************************************************//*!
517 *
518 * @brief Enable ACMP input pin.
519 *
520 * @param[in] pACMPx pointer to an ACMP module.
521 * @param[in] u8InputPin ACMP external pin, 0~2.
522 *
523 * @return none.
524 *
525 * @ Pass/ Fail criteria: none.
526 *
527 *****************************************************************************/
ACMP_InputPinEnable(ACMP_Type * pACMPx,uint8_t u8InputPin)528 __STATIC_INLINE void ACMP_InputPinEnable(ACMP_Type *pACMPx, uint8_t u8InputPin)
529 {
530 ASSERT(!(u8InputPin & (~ACMP_C2_ACIPE_MASK)));
531 pACMPx->C2 |= ACMP_C2_ACIPE(u8InputPin);
532 }
533
534 /*****************************************************************************//*!
535 *
536 * @brief Disable ACMP input pin.
537 *
538 * @param[in] pACMPx pointer to an ACMP module.
539 * @param[in] u8InputPin ACMP external pin, 0~2.
540 *
541 * @return none.
542 *
543 * @ Pass/ Fail criteria: none.
544 *
545 *****************************************************************************/
ACMP_InputPinDisable(ACMP_Type * pACMPx,uint8_t u8InputPin)546 __STATIC_INLINE void ACMP_InputPinDisable(ACMP_Type *pACMPx, uint8_t u8InputPin)
547 {
548 ASSERT(!(u8InputPin & (~ACMP_C2_ACIPE_MASK)));
549 pACMPx->C2 &= ~ACMP_C2_ACIPE(u8InputPin);
550 }
551
552 /*! @} End of acmp_api_list */
553
554 /******************************************************************************
555 * Global functions
556 ******************************************************************************/
557 void ACMP_Init(ACMP_Type *pACMPx, ACMP_ConfigType *pConfig);
558 void ACMP_DeInit(ACMP_Type *pACMPx);
559 void ACMP_ConfigDAC(ACMP_Type *pACMPx, ACMP_DACType *pDACConfig);
560 void ACMP_SetCallback(ACMP_Type *pACMPx, ACMP_CallbackPtr pfnCallback);
561
562 #ifdef __cplusplus
563 }
564 #endif
565 #endif /* _MY_ACMP_H_ */
566
567
568
569