2 * @brief CMP Registers and control functions
5 * Copyright(C) NXP Semiconductors, 2012
9 * Software that is described herein is for illustrative purposes only
10 * which provides customers with programming information regarding the
11 * LPC products. This software is supplied "AS IS" without any warranties of
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
13 * all warranties, express or implied, including all implied warranties of
14 * merchantability, fitness for a particular purpose and non-infringement of
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
16 * or liability for the use of the software, conveys no license or rights under any
17 * patent, copyright, mask work right, or any other intellectual property rights in
18 * or to any products. NXP Semiconductors reserves the right to make changes
19 * in the software without notification. NXP Semiconductors also makes no
20 * representation or warranty that such application will be suitable for the
21 * specified use without further testing or modification.
24 * Permission to use, copy, modify, and distribute this software and its
25 * documentation is hereby granted, under NXP Semiconductors' and its
26 * licensor's relevant copyrights in the software, without fee, provided that it
27 * is used in conjunction with NXP Semiconductors microcontrollers. This
28 * copyright, permission, and disclaimer notice must appear in all copies of
35 #include "sys_config.h"
42 /** @defgroup IP_CMP_001 IP: Comparator register block and driver
47 /** The number of embeded comparators supported */
51 * @brief Comparator (CMP) register block structure
53 typedef struct { /*!< Comparator structure */
54 __IO uint32_t CMP_CTRL; /*!< Comparator block control register */
55 __IO uint32_t CMP_CTRLx[CMP_NUM]; /*!< Specific comparator control register */
59 * @brief Comparator control definition
61 typedef enum IP_CMP_ENCTRL {
62 CMP_ENCTRL_DISABLE, /*!< Disable */
63 CMP_ENCTRL_DIS_IN_DS_PWD, /*!< Disable in deep sleep mode and power down mode*/
64 CMP_ENCTRL_DIS_IN_PWD, /*!< Disable in power down mode*/
65 CMP_ENCTRL_ENABLE, /*!< Enable/Power-up*/
68 /*!< Comparator control register Bitmask */
69 #define CMP_CTRL_BITMASK (0xF30F)
70 /*!< The comparator current source control bitmask*/
71 #define CMP_CTRL_PD_IREF_BITMASK (0x03)
72 /*!< Control the comparator current source (n is IP_CMP_ENCTRL_T value)*/
73 #define CMP_CTRL_PD_IREF(n) ((((uint32_t) (n)) & 0x03))
74 /*!< The comparator bandgap reference control bitmask*/
75 #define CMP_CTRL_PD_VBG_BITMASK (((uint32_t) 0x03) << 2)
76 /*!< Control the comparator bandgap reference (n is IP_CMP_ENCTRL_T value)*/
77 #define CMP_CTRL_PD_VBG(n) ((((uint32_t) (n)) & 0x03) << 2)
78 /*!< The CMP_ROSC ouput control bitmask */
79 #define CMP_CTRL_ROSC_BITMASK ((uint32_t) 0x300)
80 /*!< The CMP_ROSC output is set by CMP1, reset by CMP0 */
81 #define CMP_CTRL_ROSCCTL_CMP1 (0x00)
82 /*!< The CMP_ROSC output is set by CMP0, reset by CMP1 */
83 #define CMP_CTRL_ROSCCTL_CMP0 (((uint32_t) 0x01) << 8)
84 /*!< The CMP_ROSC output is reset by the internal chip reset */
85 #define CMP_CTRL_EXT_RESET_INTERNAL (0x00)
86 /*!< The CMP_ROSC output is reset by the CMP_RESET input */
87 #define CMP_CTRL_EXT_RESET_CMPRESET (((uint32_t) 0x01) << 9)
88 /*!< Timer Capture input control bitmask */
89 #define CMP_CTRL_TIMERCAPTURE_BITMASK ((uint32_t) 0xF000)
90 /*!< Selects the comparator 0 level output as the input for Timer0 capture input 2 */
91 #define CMP_CTRL_T0CAP2_0LEVEL (0x00)
92 /*!< Selects the comparator 1 level output as the input for Timer0 capture input 2 */
93 #define CMP_CTRL_T0CAP2_1LEVEL (((uint32_t) 0x01) << 12)
94 /*!< Selects the comparator 0 edge output as the input for Timer0 capture input 3 */
95 #define CMP_CTRL_T0CAP3_0EDGE (0x00)
96 /*!< Selects the comparator 1 edge output as the input for Timer0 capture input 3 */
97 #define CMP_CTRL_T0CAP3_1EDGE (((uint32_t) 0x01) << 13)
98 /*!< Selects the comparator 1 edge output as the input for Timer1 capture input 2 */
99 #define CMP_CTRL_T1CAP2_1EDGE (0x00)
100 /*!< Selects the comparator 0 level output as the input for Timer1 capture input 2 */
101 #define CMP_CTRL_T1CAP2_0LEVEL (((uint32_t) 0x01) << 14)
102 /*!< Selects the comparator 1 level output as the input for Timer1 capture input 3 */
103 #define CMP_CTRL_T1CAP3_1LEVEL (0x00)
104 /*!< Selects the comparator 0 edge output as the input for Timer1 capture input 3 */
105 #define CMP_CTRL_T1CAP3_0EDGE (((uint32_t) 0x01) << 15)
107 /*!< The comparator x control register Bitmask */
108 #define CMP_CTRLx_BITMASK ((uint32_t) 0x1F7FF77F)
109 /*!< The comparator x enable bitmask*/
110 #define CMP_CTRLx_EN_BITMASK (0x03)
111 /*!< Control the comparator x (n is IP_CMP_ENCTRL_T value)*/
112 #define CMP_CTRLx_EN(n) ((((uint32_t) (n)) & 0x03))
113 /*!< Enable the output of compartor x */
114 #define CMP_CTRLx_OE (((uint32_t) 0x01) << 2)
115 /*!< The status of compartor x, reflects the comparator x output*/
116 #define CMP_CTRLx_STAT (((uint32_t) 0x01) << 3)
117 /*!< Comparator VM input control bitmask */
118 #define CMP_CTRLx_VM_BITMASK (((uint32_t) 0x07) << 4)
119 /*!< Select the VM input*/
120 #define CMP_CTRLx_VM(n) ((((uint32_t) (n)) & 0x07) << 4)
121 /*!< Comparator VP input control bitmask */
122 #define CMP_CTRLx_VP_BITMASK (((uint32_t) 0x07) << 8)
123 /*!< Select the VP input */
124 #define CMP_CTRLx_VP(n) ((((uint32_t) (n)) & 0x07) << 8)
125 /*!< Synchronize the Comparator x output with the internal bus clock for outpur to other peripherals */
126 #define CMP_CTRLx_SYNC (((uint32_t) 0x01) << 12)
127 /*!< Comparator Hysteresis control bitmask */
128 #define CMP_CTRLx_HYS_BITMASK (((uint32_t) 0x03) << 13)
129 /*!< Determine the difference required between the comparator inputs before the comparator output switch*/
130 #define CMP_CTRLx_HYS(n) ((((uint32_t) (n)) & 0x03) << 13)
131 /*!< Interrupt control bitmask */
132 #define CMP_CTRLx_INTCTRL_BITMASK ((uint32_t) 0x78000)
133 /*!< Comparator output is used as-is for generating interrupts. */
134 #define CMP_CTRLx_INTPOL_NORMAL (0x00)
135 /*!< Comparator output is used inverted for generating interrupts. */
136 #define CMP_CTRLx_INTPOL_INV (((uint32_t) 0x01) << 15)
137 /*!< Comparator x interrupt is edge triggered. */
138 #define CMP_CTRLx_INTTYPE_EDGE (0x00)
139 /*!< Comparator x interrupt is level triggered.*/
140 #define CMP_CTRLx_INTTYPE_LEVEL (((uint32_t) 0x01) << 16)
141 /*!< Comparator x interrupt edge control bitmask */
142 #define CMP_CTRLx_INTEDGE_BITMASK (((uint32_t) 0x03) << 17)
143 /*!< Select edge on which triggered interrupt is active*/
144 #define CMP_CTRLx_INTEDGE(n) ((((uint32_t) (n)) & 0x03) << 17)
145 /*!< Comparator interrupt flag */
146 #define CMP_CTRLx_INTFLAG (((uint32_t) 0x01) << 19)
147 /*!< The VLAD Enable bitmask*/
148 #define CMP_CTRLx_VLADEN_BITMASK (((uint32_t) 0x03) << 20)
149 /*!< Control the comparator x (n is IP_CMP_ENCTRL_T value)*/
150 #define CMP_CTRLx_VLADEN(n) ((((uint32_t) (n)) & 0x03) << 20)
151 /*!< Select VREF_CMP pin as voltage reference for comparator voltage ladder */
152 #define CMP_CTRLx_VLADREF_VREFCMP (0x00)
153 /*!< Select VDDA pin as voltage reference for comparator voltage ladder */
154 #define CMP_CTRLx_VLADREF_VDDA (((uint32_t) 0x01) << 22)
155 /*!< Volatge ladder value bitmask */
156 #define CMP_CTRLx_VSEL_BITMASK ((uint32_t) ((0x1F) << 24))
157 /*!< Voltage ladder value for Comparator */
158 #define CMP_CTRLx_VSEL(n) ((((uint32_t) (n)) & 0x1F) << 24)
161 * @brief Comparator VM/VP input definitions
163 typedef enum IP_CMP_INPUT {
164 CMP_INPUT_VREF_DIV, /*!< Vref divider.*/
165 CMP_INPUT_CMPx_IN0, /*!< Use the input 0 of the comparator*/
166 CMP_INPUT_CMPx_IN1, /*!< Use the input 1 of the comparator*/
167 CMP_INPUT_CMPx_IN2, /*!< Use the input 2 of the comparator*/
168 CMP_INPUT_CMPx_IN3, /*!< Use the input 3 of the comparator*/
169 CMP_INPUT_CMP_OTHER_IN0, /*!< Use the input 0 of the other comparator.*/
170 CMP_INPUT_INTERNAL_09VBG, /*!< internal 0.9 V band gap reference.*/
174 * @brief Comparator hysteresis selection definitions
176 typedef enum IP_CMP_HYS {
177 CMP_HYS_NONE = CMP_CTRLx_HYS(0), /*!<No hysteresis (the output will switch as the voltages cross) */
178 CMP_HYS_5MV = CMP_CTRLx_HYS(1), /*!< 5mV hysteresis */
179 CMP_HYS_10MV = CMP_CTRLx_HYS(2), /*!< 10mV hysteresis */
180 CMP_HYS_15MV = CMP_CTRLx_HYS(3), /*!< 15mV hysteresis */
184 * @brief Comparator interrupt edge selection definitions
186 typedef enum IP_CMP_INTEDGE {
187 CMP_INTEDGE_FALLING = CMP_CTRLx_INTEDGE(0), /*!< Interrupt is active on falling edge */
188 CMP_INTEDGE_RISING = CMP_CTRLx_INTEDGE(1), /*!< Interrupt is active on rising edge */
189 CMP_INTEDGE_BOTH = CMP_CTRLx_INTEDGE(2), /*!< Interrupt is active on falling and rising edges */
193 * @brief Initializes the CMP
194 * @param pCMP : Pointer to Comparator Register block
197 STATIC INLINE void IP_CMP_Init(IP_CMP_001_T *pCMP) {}
200 * @brief De-initializes the CMP
201 * @param pCMP : Pointer to Comparator Register block
204 STATIC INLINE void IP_CMP_DeInit(IP_CMP_001_T *pCMP) {}
207 * @brief Enables comparator current source
208 * @param pCMP : Pointer to Comparator Register block
209 * @param en : Enable mode
212 STATIC INLINE void IP_CMP_EnableCurrentSrc(IP_CMP_001_T *pCMP, IP_CMP_ENCTRL_T en)
214 pCMP->CMP_CTRL = (pCMP->CMP_CTRL & (~CMP_CTRL_PD_IREF_BITMASK)) | CMP_CTRL_PD_IREF(en);
218 * @brief Enables comparator bandgap reference
219 * @param pCMP : Pointer to Comparator Register block
220 * @param en : Enable mode
223 STATIC INLINE void IP_CMP_EnableBandGap(IP_CMP_001_T *pCMP, IP_CMP_ENCTRL_T en)
225 pCMP->CMP_CTRL = (pCMP->CMP_CTRL & (~CMP_CTRL_PD_VBG_BITMASK)) | CMP_CTRL_PD_VBG(en);
229 * @brief Control CMP_ROSC
230 * @param pCMP : Pointer to Comparator Register block
231 * @param flag : Or-ed bit value of CMP_CTRL_ROSCCTL_* and CMP_CTRL_EXT_RESET_*
234 STATIC INLINE void IP_CMP_ControlROSC(IP_CMP_001_T *pCMP, uint32_t flag)
236 pCMP->CMP_CTRL = (pCMP->CMP_CTRL & (~CMP_CTRL_ROSC_BITMASK)) | flag;
240 * @brief Control CMP_ROSC
241 * @param pCMP : Pointer to Comparator Register block
242 * @param flag : Or-ed bit value of CMP_CTRL_T*CAP*
245 STATIC INLINE void IP_CMP_SetTimerCapInput(IP_CMP_001_T *pCMP, uint32_t flag)
247 pCMP->CMP_CTRL = (pCMP->CMP_CTRL & (~CMP_CTRL_TIMERCAPTURE_BITMASK)) | flag;
251 * @brief Sets up voltage ladder
252 * @param pCMP : Pointer to Comparator Register block
253 * @param id : Comparator ID
254 * @param ladSel : Voltage ladder value (0~31).
255 * @param flag :0(CMP_VREF used)/CMP_CTRLx_VLADREF_VDDA (VDDA used)
257 * @note VREF divider 0 = ladSel*VRef0/31
259 STATIC INLINE void IP_CMP_SetupVoltLadder(IP_CMP_001_T *pCMP, uint8_t id,
260 uint16_t ladSel, uint32_t flag)
262 pCMP->CMP_CTRLx[id] = (pCMP->CMP_CTRLx[id] & (~(CMP_CTRLx_VSEL_BITMASK | CMP_CTRLx_VLADREF_VDDA))) | CMP_CTRLx_VSEL(
267 * @brief Enables voltage ladder
268 * @param pCMP : Pointer to Comparator Register block
269 * @param id : Comparator ID
270 * @param en : Enable mode
273 STATIC INLINE void IP_CMP_EnableVoltLadder(IP_CMP_001_T *pCMP, uint8_t id, IP_CMP_ENCTRL_T en)
275 pCMP->CMP_CTRLx[id] = (pCMP->CMP_CTRLx[id] & (~CMP_CTRLx_VLADEN_BITMASK)) | CMP_CTRLx_VLADEN(en);
279 * @brief Selects positive voltage input
280 * @param pCMP : Pointer to Comparator Register block
281 * @param id : Comparator ID
282 * @param input : Selected input
285 STATIC INLINE void IP_CMP_SetPosVoltRef(IP_CMP_001_T *pCMP, uint8_t id, IP_CMP_INPUT_T input)
287 pCMP->CMP_CTRLx[id] = (pCMP->CMP_CTRLx[id] & (~CMP_CTRLx_VP_BITMASK)) | CMP_CTRLx_VP(input);
291 * @brief Selects negative voltage input
292 * @param pCMP : Pointer to Comparator Register block
293 * @param id : Comparator ID
294 * @param input : Selected input
297 STATIC INLINE void IP_CMP_SetNegVoltRef(IP_CMP_001_T *pCMP, uint8_t id, IP_CMP_INPUT_T input)
299 pCMP->CMP_CTRLx[id] = (pCMP->CMP_CTRLx[id] & (~CMP_CTRLx_VM_BITMASK)) | CMP_CTRLx_VM(input);
303 * @brief Selects hysteresis level
304 * @param pCMP : Pointer to Comparator Register block
305 * @param id : Comparator ID
306 * @param hys : Selected Hysteresis level
309 STATIC INLINE void IP_CMP_SetHysteresis(IP_CMP_001_T *pCMP, uint8_t id, IP_CMP_HYS_T hys)
311 pCMP->CMP_CTRLx[id] = (pCMP->CMP_CTRLx[id] & (~CMP_CTRLx_HYS_BITMASK)) | hys;
315 * @brief Enables specified comparator
316 * @param pCMP : Pointer to Comparator Register block
317 * @param id : Comparator ID
318 * @param en : Enable mode
321 STATIC INLINE void IP_CMP_Enable(IP_CMP_001_T *pCMP, uint8_t id, IP_CMP_ENCTRL_T en)
323 pCMP->CMP_CTRLx[id] = (pCMP->CMP_CTRLx[id] & (~CMP_CTRLx_EN_BITMASK)) | CMP_CTRLx_EN(en);
327 * @brief Returns the current comparator status
328 * @param pCMP : Pointer to Comparator Register block
329 * @param id : Comparator Id (0/1)
332 STATIC INLINE FlagStatus IP_CMP_GetCmpStatus(IP_CMP_001_T *pCMP, uint8_t id)
334 return (pCMP->CMP_CTRLx[id] & CMP_CTRLx_STAT) ? SET : RESET;
338 * @brief Enable comparator output
339 * @param pCMP : Pointer to Comparator Register block
340 * @param id : Comparator ID
343 STATIC INLINE void IP_CMP_EnableOuput(IP_CMP_001_T *pCMP, uint8_t id)
345 pCMP->CMP_CTRLx[id] |= CMP_CTRLx_OE;
349 * @brief Disable comparator output
350 * @param pCMP : Pointer to Comparator Register block
351 * @param id : Comparator ID
354 STATIC INLINE void IP_CMP_DisableOutput(IP_CMP_001_T *pCMP, uint8_t id)
356 pCMP->CMP_CTRLx[id] &= ~CMP_CTRLx_OE;
360 * @brief Synchronizes Comparator output to bus clock
361 * @param pCMP : Pointer to Comparator Register block
362 * @param id : Comparator ID
365 STATIC INLINE void IP_CMP_EnableSyncCmpOut(IP_CMP_001_T *pCMP, uint8_t id)
367 pCMP->CMP_CTRLx[id] |= CMP_CTRLx_SYNC;
371 * @brief Sets comparator output to be used directly (no sync)
372 * @param pCMP : Pointer to Comparator Register block
373 * @param id : Comparator ID
376 STATIC INLINE void IP_CMP_DisableSyncCmpOut(IP_CMP_001_T *pCMP, uint8_t id)
378 pCMP->CMP_CTRLx[id] &= ~CMP_CTRLx_SYNC;
382 * @brief Sets up comparator interrupt
383 * @param pCMP : Pointer to Comparator Register block
384 * @param id : Comparator ID
385 * @param intFlag : Or-ed value of CMP_CTRLx_INTTYPE_*, CMP_CTRLx_INTPOL_*
386 * @param edgeSel : the edge on which interrupt occurs.
389 STATIC INLINE void IP_CMP_ConfigInt(IP_CMP_001_T *pCMP, uint8_t id,
391 IP_CMP_INTEDGE_T edgeSel)
393 pCMP->CMP_CTRLx[id] = (pCMP->CMP_CTRLx[id] & (~CMP_CTRLx_INTCTRL_BITMASK)) | intFlag | edgeSel;
397 * @brief Get the CMP interrupt status
398 * @param pCMP : Pointer to Comparator Register block
399 * @param id : Comparator ID
402 STATIC INLINE FlagStatus IP_CMP_GetIntStatus(IP_CMP_001_T *pCMP, uint8_t id)
404 return (pCMP->CMP_CTRLx[id] & CMP_CTRLx_INTFLAG) ? SET : RESET;
408 * @brief Clears the CMP interrupt status
409 * @param pCMP : Pointer to Comparator Register block
410 * @param id : Comparator ID
413 STATIC INLINE void IP_CMP_CMP_ClearIntStatus(IP_CMP_001_T *pCMP, uint8_t id)
415 pCMP->CMP_CTRLx[id] |= CMP_CTRLx_INTFLAG;
426 #endif /* __CMP_001_H_ */