1 /***************************************************************************//**
\r
3 * @brief Voltage Comparator (VCMP) peripheral API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2015 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
33 #ifndef __SILICON_LABS_EM_VCMP_H__
\r
34 #define __SILICON_LABS_EM_VCMP_H__
\r
36 #include "em_device.h"
\r
37 #if defined(VCMP_COUNT) && (VCMP_COUNT > 0)
\r
40 #include <stdbool.h>
\r
46 /***************************************************************************//**
\r
47 * @addtogroup EM_Library
\r
49 ******************************************************************************/
\r
51 /***************************************************************************//**
\r
54 ******************************************************************************/
\r
56 /*******************************************************************************
\r
57 ******************************** ENUMS ************************************
\r
58 ******************************************************************************/
\r
60 /** Warm-up Time in High Frequency Peripheral Clock cycles */
\r
64 vcmpWarmTime4Cycles = _VCMP_CTRL_WARMTIME_4CYCLES,
\r
66 vcmpWarmTime8Cycles = _VCMP_CTRL_WARMTIME_8CYCLES,
\r
68 vcmpWarmTime16Cycles = _VCMP_CTRL_WARMTIME_16CYCLES,
\r
70 vcmpWarmTime32Cycles = _VCMP_CTRL_WARMTIME_32CYCLES,
\r
72 vcmpWarmTime64Cycles = _VCMP_CTRL_WARMTIME_64CYCLES,
\r
74 vcmpWarmTime128Cycles = _VCMP_CTRL_WARMTIME_128CYCLES,
\r
76 vcmpWarmTime256Cycles = _VCMP_CTRL_WARMTIME_256CYCLES,
\r
78 vcmpWarmTime512Cycles = _VCMP_CTRL_WARMTIME_512CYCLES
\r
79 } VCMP_WarmTime_TypeDef;
\r
81 /** Hyseresis configuration */
\r
84 /** Normal operation, no hysteresis */
\r
86 /** Digital output will not toggle until positive edge is at least
\r
87 * 20mV above or below negative input voltage */
\r
89 } VCMP_Hysteresis_TypeDef;
\r
91 /*******************************************************************************
\r
92 ******************************* STRUCTS ***********************************
\r
93 ******************************************************************************/
\r
95 /** VCMP Initialization structure */
\r
98 /** If set to true, will reduce by half the bias current */
\r
100 /** BIAS current configuration, depends on halfBias setting,
\r
101 * above, see reference manual */
\r
103 /** Enable interrupt for falling edge */
\r
105 /** Enable interrupt for rising edge */
\r
107 /** Warm-up time in clock cycles */
\r
108 VCMP_WarmTime_TypeDef warmup;
\r
109 /** Hysteresis configuration */
\r
110 VCMP_Hysteresis_TypeDef hyst;
\r
111 /** Output value when comparator is inactive, should be 0 or 1 */
\r
113 /** Enable low power mode for VDD and bandgap reference */
\r
115 /** Trigger level, according to formula
\r
116 * VDD Trigger Level = 1.667V + 0.034V x triggerLevel */
\r
118 /** Enable VCMP after configuration */
\r
120 } VCMP_Init_TypeDef;
\r
122 /** Default VCMP initialization structure */
\r
123 #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
141 void VCMP_Init(const VCMP_Init_TypeDef *vcmpInit);
\r
142 void VCMP_LowPowerRefSet(bool enable);
\r
143 void VCMP_TriggerSet(int level);
\r
145 /***************************************************************************//**
\r
147 * Enable Voltage Comparator
\r
148 ******************************************************************************/
\r
149 __STATIC_INLINE void VCMP_Enable(void)
\r
151 VCMP->CTRL |= VCMP_CTRL_EN;
\r
155 /***************************************************************************//**
\r
157 * Disable Voltage Comparator
\r
158 ******************************************************************************/
\r
159 __STATIC_INLINE void VCMP_Disable(void)
\r
161 VCMP->CTRL &= ~VCMP_CTRL_EN;
\r
165 /***************************************************************************//**
\r
167 * Calculate voltage to trigger level
\r
170 * You need soft float support for this function to be working
\r
173 * Voltage Level for trigger
\r
174 ******************************************************************************/
\r
175 __STATIC_INLINE uint32_t VCMP_VoltageToLevel(float v)
\r
177 return (uint32_t)((v - (float)1.667) / (float)0.034);
\r
181 /***************************************************************************//**
\r
183 * Returns true, if Voltage Comparator indicated VDD < trigger level, else
\r
185 ******************************************************************************/
\r
186 __STATIC_INLINE bool VCMP_VDDLower(void)
\r
188 if (VCMP->STATUS & VCMP_STATUS_VCMPOUT)
\r
199 /***************************************************************************//**
\r
201 * Returns true, if Voltage Comparator indicated VDD > trigger level, else
\r
203 ******************************************************************************/
\r
204 __STATIC_INLINE bool VCMP_VDDHigher(void)
\r
206 if (VCMP->STATUS & VCMP_STATUS_VCMPOUT)
\r
217 /***************************************************************************//**
\r
219 * VCMP output is ready
\r
220 ******************************************************************************/
\r
221 __STATIC_INLINE bool VCMP_Ready(void)
\r
223 if (VCMP->STATUS & VCMP_STATUS_VCMPACT)
\r
234 /***************************************************************************//**
\r
236 * Clear one or more pending VCMP interrupts.
\r
239 * VCMP interrupt sources to clear. Use a set of interrupt flags OR-ed
\r
240 * together to clear multiple interrupt sources for the VCMP module
\r
242 ******************************************************************************/
\r
243 __STATIC_INLINE void VCMP_IntClear(uint32_t flags)
\r
249 /***************************************************************************//**
\r
251 * Set one or more pending VCMP interrupts from SW.
\r
254 * VCMP interrupt sources to set to pending. Use a set of interrupt flags
\r
255 * OR-ed together to set multiple interrupt sources for the VCMP module
\r
257 ******************************************************************************/
\r
258 __STATIC_INLINE void VCMP_IntSet(uint32_t flags)
\r
264 /***************************************************************************//**
\r
266 * Disable one or more VCMP interrupts
\r
269 * VCMP interrupt sources to enable. Use a set of interrupt flags OR-ed
\r
270 * together to set multiple interrupt sources for the VCMP module
\r
272 ******************************************************************************/
\r
273 __STATIC_INLINE void VCMP_IntDisable(uint32_t flags)
\r
275 VCMP->IEN &= ~flags;
\r
279 /***************************************************************************//**
\r
281 * Enable one or more VCMP interrupts
\r
284 * VCMP interrupt sources to enable. Use a set of interrupt flags OR-ed
\r
285 * together to set multiple interrupt sources for the VCMP module
\r
287 ******************************************************************************/
\r
288 __STATIC_INLINE void VCMP_IntEnable(uint32_t flags)
\r
290 VCMP->IEN |= flags;
\r
294 /***************************************************************************//**
\r
296 * Get pending VCMP interrupt flags
\r
299 * The event bits are not cleared by the use of this function
\r
302 * Pending VCMP interrupt sources. Returns a set of interrupt flags OR-ed
\r
303 * together for multiple interrupt sources in the VCMP module (VCMP_IFS_nnn).
\r
304 ******************************************************************************/
\r
305 __STATIC_INLINE uint32_t VCMP_IntGet(void)
\r
311 /***************************************************************************//**
\r
313 * Get enabled and pending VCMP interrupt flags.
\r
316 * Useful for handling more interrupt sources in the same interrupt handler.
\r
319 * The event bits are not cleared by the use of this function.
\r
322 * Pending and enabled VCMP interrupt sources.
\r
323 * The return value is the bitwise AND combination of
\r
324 * - the OR combination of enabled interrupt sources in VCMP_IEN_nnn
\r
325 * register (VCMP_IEN_nnn) and
\r
326 * - the OR combination of valid interrupt flags of the VCMP module
\r
328 ******************************************************************************/
\r
329 __STATIC_INLINE uint32_t VCMP_IntGetEnabled(void)
\r
333 /* Store VCMP->IEN in temporary variable in order to define explicit order
\r
334 * of volatile accesses. */
\r
337 /* Bitwise AND of pending and enabled interrupts */
\r
338 return VCMP->IF & tmp;
\r
341 /** @} (end addtogroup VCMP) */
\r
342 /** @} (end addtogroup EM_Library) */
\r
348 #endif /* defined(VCMP_COUNT) && (VCMP_COUNT > 0) */
\r
349 #endif /* __SILICON_LABS_EM_VCMP_H__ */
\r