1 /***************************************************************************//**
\r
3 * @brief Voltage Comparator (VCMP) peripheral API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b>
\r
8 *******************************************************************************
\r
10 * Permission is granted to anyone to use this software for any purpose,
\r
11 * including commercial applications, and to alter it and redistribute it
\r
12 * freely, subject to the following restrictions:
\r
14 * 1. The origin of this software must not be misrepresented; you must not
\r
15 * claim that you wrote the original software.
\r
16 * 2. Altered source versions must be plainly marked as such, and must not be
\r
17 * misrepresented as being the original software.
\r
18 * 3. This notice may not be removed or altered from any source distribution.
\r
20 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
\r
21 * obligation to support this Software. Silicon Labs is providing the
\r
22 * Software "AS IS", with no express or implied warranties of any kind,
\r
23 * including, but not limited to, any implied warranties of merchantability
\r
24 * or fitness for any particular purpose or warranties against infringement
\r
25 * of any proprietary rights of a third party.
\r
27 * Silicon Labs will not be liable for any consequential, incidental, or
\r
28 * special damages, or any other relief, or for any claim by any third party,
\r
29 * arising from your use of this Software.
\r
31 ******************************************************************************/
\r
34 #ifndef __SILICON_LABS_EM_VCMP_H_
\r
35 #define __SILICON_LABS_EM_VCMP_H_
\r
37 #include "em_device.h"
\r
38 #if defined(VCMP_COUNT) && (VCMP_COUNT > 0)
\r
41 #include <stdbool.h>
\r
47 /***************************************************************************//**
\r
48 * @addtogroup EM_Library
\r
50 ******************************************************************************/
\r
52 /***************************************************************************//**
\r
55 ******************************************************************************/
\r
57 /*******************************************************************************
\r
58 ******************************** ENUMS ************************************
\r
59 ******************************************************************************/
\r
61 /** Warm-up Time in High Frequency Peripheral Clock cycles */
\r
65 vcmpWarmTime4Cycles = _VCMP_CTRL_WARMTIME_4CYCLES,
\r
67 vcmpWarmTime8Cycles = _VCMP_CTRL_WARMTIME_8CYCLES,
\r
69 vcmpWarmTime16Cycles = _VCMP_CTRL_WARMTIME_16CYCLES,
\r
71 vcmpWarmTime32Cycles = _VCMP_CTRL_WARMTIME_32CYCLES,
\r
73 vcmpWarmTime64Cycles = _VCMP_CTRL_WARMTIME_64CYCLES,
\r
75 vcmpWarmTime128Cycles = _VCMP_CTRL_WARMTIME_128CYCLES,
\r
77 vcmpWarmTime256Cycles = _VCMP_CTRL_WARMTIME_256CYCLES,
\r
79 vcmpWarmTime512Cycles = _VCMP_CTRL_WARMTIME_512CYCLES
\r
80 } VCMP_WarmTime_TypeDef;
\r
82 /** Hyseresis configuration */
\r
85 /** Normal operation, no hysteresis */
\r
87 /** Digital output will not toggle until positive edge is at least
\r
88 * 20mV above or below negative input voltage */
\r
90 } VCMP_Hysteresis_TypeDef;
\r
92 /*******************************************************************************
\r
93 ******************************* STRUCTS ***********************************
\r
94 ******************************************************************************/
\r
96 /** VCMP Initialization structure */
\r
99 /** If set to true, will reduce by half the bias current */
\r
101 /** BIAS current configuration, depends on halfBias setting,
\r
102 * above, see reference manual */
\r
104 /** Enable interrupt for falling edge */
\r
106 /** Enable interrupt for rising edge */
\r
108 /** Warm-up time in clock cycles */
\r
109 VCMP_WarmTime_TypeDef warmup;
\r
110 /** Hysteresis configuration */
\r
111 VCMP_Hysteresis_TypeDef hyst;
\r
112 /** Output value when comparator is inactive, should be 0 or 1 */
\r
114 /** Enable low power mode for VDD and bandgap reference */
\r
116 /** Trigger level, according to formula
\r
117 * VDD Trigger Level = 1.667V + 0.034V x triggerLevel */
\r
119 /** Enable VCMP after configuration */
\r
121 } VCMP_Init_TypeDef;
\r
123 /** Default VCMP initialization structure */
\r
124 #define VCMP_INIT_DEFAULT \
\r
125 { true, /** Half Bias enabled */ \
\r
126 0x7, /** Bias curernt 0.7 uA when half bias enabled */ \
\r
127 false, /** Falling edge sense not enabled */ \
\r
128 false, /** Rising edge sense not enabled */ \
\r
129 vcmpWarmTime4Cycles, /** 4 clock cycles warm-up time */ \
\r
130 vcmpHystNone, /** No hysteresis */ \
\r
131 0, /** 0 in digital ouput when inactive */ \
\r
132 true, /** Do not use low power reference */ \
\r
133 39, /** Trigger level just below 3V */ \
\r
134 true, /** Enable after init */ \
\r
137 /*******************************************************************************
\r
138 ***************************** PROTOTYPES **********************************
\r
139 ******************************************************************************/
\r
140 void VCMP_Init(const VCMP_Init_TypeDef *vcmpInit);
\r
141 void VCMP_LowPowerRefSet(bool enable);
\r
142 void VCMP_TriggerSet(int level);
\r
144 __STATIC_INLINE void VCMP_Enable(void);
\r
145 __STATIC_INLINE void VCMP_Disable(void);
\r
146 __STATIC_INLINE uint32_t VCMP_VoltageToLevel(float v);
\r
147 __STATIC_INLINE bool VCMP_VDDLower(void);
\r
148 __STATIC_INLINE bool VCMP_VDDHigher(void);
\r
149 __STATIC_INLINE bool VCMP_Ready(void);
\r
150 __STATIC_INLINE void VCMP_IntClear(uint32_t flags);
\r
151 __STATIC_INLINE void VCMP_IntSet(uint32_t flags);
\r
152 __STATIC_INLINE void VCMP_IntDisable(uint32_t flags);
\r
153 __STATIC_INLINE void VCMP_IntEnable(uint32_t flags);
\r
154 __STATIC_INLINE uint32_t VCMP_IntGet(void);
\r
155 __STATIC_INLINE uint32_t VCMP_IntGetEnabled(void);
\r
157 /***************************************************************************//**
\r
159 * Enable Voltage Comparator
\r
160 ******************************************************************************/
\r
161 __STATIC_INLINE void VCMP_Enable(void)
\r
163 VCMP->CTRL |= VCMP_CTRL_EN;
\r
167 /***************************************************************************//**
\r
169 * Disable Voltage Comparator
\r
170 ******************************************************************************/
\r
171 __STATIC_INLINE void VCMP_Disable(void)
\r
173 VCMP->CTRL &= ~(VCMP_CTRL_EN);
\r
177 /***************************************************************************//**
\r
179 * Calculate voltage to trigger level
\r
182 * You need soft float support for this function to be working
\r
185 * Voltage Level for trigger
\r
186 ******************************************************************************/
\r
187 __STATIC_INLINE uint32_t VCMP_VoltageToLevel(float v)
\r
189 return (uint32_t)((v - (float)1.667) / (float)0.034);
\r
193 /***************************************************************************//**
\r
195 * Returns true, if Voltage Comparator indicated VDD < trigger level, else
\r
197 ******************************************************************************/
\r
198 __STATIC_INLINE bool VCMP_VDDLower(void)
\r
200 if (VCMP->STATUS & VCMP_STATUS_VCMPOUT)
\r
211 /***************************************************************************//**
\r
213 * Returns true, if Voltage Comparator indicated VDD > trigger level, else
\r
215 ******************************************************************************/
\r
216 __STATIC_INLINE bool VCMP_VDDHigher(void)
\r
218 if (VCMP->STATUS & VCMP_STATUS_VCMPOUT)
\r
229 /***************************************************************************//**
\r
231 * VCMP output is ready
\r
232 ******************************************************************************/
\r
233 __STATIC_INLINE bool VCMP_Ready(void)
\r
235 if (VCMP->STATUS & VCMP_STATUS_VCMPACT)
\r
246 /***************************************************************************//**
\r
248 * Clear one or more pending VCMP interrupts.
\r
251 * VCMP interrupt sources to clear. Use a set of interrupt flags OR-ed
\r
252 * together to clear multiple interrupt sources for the VCMP module
\r
254 ******************************************************************************/
\r
255 __STATIC_INLINE void VCMP_IntClear(uint32_t flags)
\r
261 /***************************************************************************//**
\r
263 * Set one or more pending VCMP interrupts from SW.
\r
266 * VCMP interrupt sources to set to pending. Use a set of interrupt flags
\r
267 * OR-ed together to set multiple interrupt sources for the VCMP module
\r
269 ******************************************************************************/
\r
270 __STATIC_INLINE void VCMP_IntSet(uint32_t flags)
\r
276 /***************************************************************************//**
\r
278 * Disable one or more VCMP interrupts
\r
281 * VCMP interrupt sources to enable. Use a set of interrupt flags OR-ed
\r
282 * together to set multiple interrupt sources for the VCMP module
\r
284 ******************************************************************************/
\r
285 __STATIC_INLINE void VCMP_IntDisable(uint32_t flags)
\r
287 VCMP->IEN &= ~(flags);
\r
291 /***************************************************************************//**
\r
293 * Enable one or more VCMP interrupts
\r
296 * VCMP interrupt sources to enable. Use a set of interrupt flags OR-ed
\r
297 * together to set multiple interrupt sources for the VCMP module
\r
299 ******************************************************************************/
\r
300 __STATIC_INLINE void VCMP_IntEnable(uint32_t flags)
\r
302 VCMP->IEN |= flags;
\r
306 /***************************************************************************//**
\r
308 * Get pending VCMP interrupt flags
\r
311 * The event bits are not cleared by the use of this function
\r
314 * Pending VCMP interrupt sources. Returns a set of interrupt flags OR-ed
\r
315 * together for multiple interrupt sources in the VCMP module (VCMP_IFS_nnn).
\r
316 ******************************************************************************/
\r
317 __STATIC_INLINE uint32_t VCMP_IntGet(void)
\r
323 /***************************************************************************//**
\r
325 * Get enabled and pending VCMP interrupt flags.
\r
328 * Useful for handling more interrupt sources in the same interrupt handler.
\r
331 * The event bits are not cleared by the use of this function.
\r
334 * Pending and enabled VCMP interrupt sources.
\r
335 * The return value is the bitwise AND combination of
\r
336 * - the OR combination of enabled interrupt sources in VCMP_IEN_nnn
\r
337 * register (VCMP_IEN_nnn) and
\r
338 * - the OR combination of valid interrupt flags of the VCMP module
\r
340 ******************************************************************************/
\r
341 __STATIC_INLINE uint32_t VCMP_IntGetEnabled(void)
\r
345 /* Store VCMP->IEN in temporary variable in order to define explicit order
\r
346 * of volatile accesses. */
\r
349 /* Bitwise AND of pending and enabled interrupts */
\r
350 return VCMP->IF & tmp;
\r
353 /** @} (end addtogroup VCMP) */
\r
354 /** @} (end addtogroup EM_Library) */
\r
360 #endif /* defined(VCMP_COUNT) && (VCMP_COUNT > 0) */
\r
361 #endif /* __SILICON_LABS_EM_VCMP_H_ */
\r