1 /***************************************************************************//**
2  * @file
3  * @brief Voltage Comparator (VCMP) peripheral API
4  * @author Energy Micro AS
5  * @version 3.0.0
6  *******************************************************************************
7  * @section License
8  * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
9  *******************************************************************************
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute it
13  * freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must not
16  *    claim that you wrote the original software.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  *    misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  *
21  * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
22  * obligation to support this Software. Energy Micro AS is providing the
23  * Software "AS IS", with no express or implied warranties of any kind,
24  * including, but not limited to, any implied warranties of merchantability
25  * or fitness for any particular purpose or warranties against infringement
26  * of any proprietary rights of a third party.
27  *
28  * Energy Micro AS will not be liable for any consequential, incidental, or
29  * special damages, or any other relief, or for any claim by any third party,
30  * arising from your use of this Software.
31  *
32  ******************************************************************************/
33 #ifndef __EM_VCMP_H
34 #define __EM_VCMP_H
35 #include "em_part.h"
36 
37 #include <stdint.h>
38 #include <stdbool.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /***************************************************************************//**
45  * @addtogroup EM_Library
46  * @{
47  ******************************************************************************/
48 
49 /***************************************************************************//**
50  * @addtogroup VCMP
51  * @{
52  ******************************************************************************/
53 
54 /*******************************************************************************
55  ********************************   ENUMS   ************************************
56  ******************************************************************************/
57 
58 /** Warm-up Time in High Frequency Peripheral Clock cycles */
59 typedef enum
60 {
61   /** 4 cycles */
62   vcmpWarmTime4Cycles   = _VCMP_CTRL_WARMTIME_4CYCLES,
63   /** 8 cycles */
64   vcmpWarmTime8Cycles   = _VCMP_CTRL_WARMTIME_8CYCLES,
65   /** 16 cycles */
66   vcmpWarmTime16Cycles  = _VCMP_CTRL_WARMTIME_16CYCLES,
67   /** 32 cycles */
68   vcmpWarmTime32Cycles  = _VCMP_CTRL_WARMTIME_32CYCLES,
69   /** 64 cycles */
70   vcmpWarmTime64Cycles  = _VCMP_CTRL_WARMTIME_64CYCLES,
71   /** 128 cycles */
72   vcmpWarmTime128Cycles = _VCMP_CTRL_WARMTIME_128CYCLES,
73   /** 256 cycles */
74   vcmpWarmTime256Cycles = _VCMP_CTRL_WARMTIME_256CYCLES,
75   /** 512 cycles */
76   vcmpWarmTime512Cycles = _VCMP_CTRL_WARMTIME_512CYCLES
77 } VCMP_WarmTime_TypeDef;
78 
79 /** Hyseresis configuration */
80 typedef enum
81 {
82   /** Normal operation, no hysteresis */
83   vcmpHystNone,
84   /** Digital output will not toggle until positive edge is at least
85    *  20mV above or below negative input voltage */
86   vcmpHyst20mV
87 } VCMP_Hysteresis_TypeDef;
88 
89 /*******************************************************************************
90  *******************************   STRUCTS   ***********************************
91  ******************************************************************************/
92 
93 /** VCMP Initialization structure */
94 typedef struct
95 {
96   /** If set to true, will reduce by half the bias current */
97   bool                    halfBias;
98   /** BIAS current configuration, depends on halfBias setting,
99    *  above, see reference manual */
100   int                     biasProg;
101   /** Enable interrupt for falling edge */
102   bool                    irqFalling;
103   /** Enable interrupt for rising edge */
104   bool                    irqRising;
105   /** Warm-up time in clock cycles */
106   VCMP_WarmTime_TypeDef   warmup;
107   /** Hysteresis configuration */
108   VCMP_Hysteresis_TypeDef hyst;
109   /** Output value when comparator is inactive, should be 0 or 1 */
110   int                     inactive;
111   /** Enable low power mode for VDD and bandgap reference */
112   bool                    lowPowerRef;
113   /** Trigger level, according to formula
114    *  VDD Trigger Level = 1.667V + 0.034V x triggerLevel */
115   int                     triggerLevel;
116   /** Enable VCMP after configuration */
117   bool                    enable;
118 } VCMP_Init_TypeDef;
119 
120 /** Default VCMP initialization structure */
121 #define VCMP_INIT_DEFAULT                                                  \
122   { true,                /** Half Bias enabled */                          \
123     0x7,                 /** Bias curernt 0.7 uA when half bias enabled */ \
124     false,               /** Falling edge sense not enabled */             \
125     false,               /** Rising edge sense not enabled */              \
126     vcmpWarmTime4Cycles, /** 4 clock cycles warm-up time */                \
127     vcmpHystNone,        /** No hysteresis */                              \
128     0,                   /** 0 in digital ouput when inactive */           \
129     true,                /** Do not use low power reference */             \
130     39,                  /** Trigger level just below 3V */                \
131     true,                /** Enable after init */                          \
132   }
133 
134 /*******************************************************************************
135  *****************************   PROTOTYPES   **********************************
136  ******************************************************************************/
137 void VCMP_Init(const VCMP_Init_TypeDef *vcmpInit);
138 void VCMP_LowPowerRefSet(bool enable);
139 void VCMP_TriggerSet(int level);
140 
141 __STATIC_INLINE void VCMP_Enable(void);
142 __STATIC_INLINE void VCMP_Disable(void);
143 __STATIC_INLINE uint32_t VCMP_VoltageToLevel(float v);
144 __STATIC_INLINE bool VCMP_VDDLower(void);
145 __STATIC_INLINE bool VCMP_VDDHigher(void);
146 __STATIC_INLINE bool VCMP_Ready(void);
147 __STATIC_INLINE void VCMP_IntClear(uint32_t flags);
148 __STATIC_INLINE void VCMP_IntSet(uint32_t flags);
149 __STATIC_INLINE void VCMP_IntDisable(uint32_t flags);
150 __STATIC_INLINE void VCMP_IntEnable(uint32_t flags);
151 __STATIC_INLINE uint32_t VCMP_IntGet(void);
152 __STATIC_INLINE uint32_t VCMP_IntGetEnabled(void);
153 
154 /***************************************************************************//**
155  * @brief
156  *   Enable Voltage Comparator
157  ******************************************************************************/
VCMP_Enable(void)158 __STATIC_INLINE void VCMP_Enable(void)
159 {
160   VCMP->CTRL |= VCMP_CTRL_EN;
161 }
162 
163 
164 /***************************************************************************//**
165  * @brief
166  *   Disable Voltage Comparator
167  ******************************************************************************/
VCMP_Disable(void)168 __STATIC_INLINE void VCMP_Disable(void)
169 {
170   VCMP->CTRL &= ~(VCMP_CTRL_EN);
171 }
172 
173 
174 /***************************************************************************//**
175  * @brief
176  *   Calculate voltage to trigger level
177  *
178  * @note
179  *   You need soft float support for this function to be working
180  *
181  * @param[in] v
182  *   Voltage Level for trigger
183  ******************************************************************************/
VCMP_VoltageToLevel(float v)184 __STATIC_INLINE uint32_t VCMP_VoltageToLevel(float v)
185 {
186   return (uint32_t)((v - (float)1.667) / (float)0.034);
187 }
188 
189 
190 /***************************************************************************//**
191  * @brief
192  *   Returns true, if Voltage Comparator indicated VDD < trigger level, else
193  *   false
194  ******************************************************************************/
VCMP_VDDLower(void)195 __STATIC_INLINE bool VCMP_VDDLower(void)
196 {
197   if (VCMP->STATUS & VCMP_STATUS_VCMPOUT)
198   {
199     return false;
200   }
201   else
202   {
203     return true;
204   }
205 }
206 
207 
208 /***************************************************************************//**
209  * @brief
210  *   Returns true, if Voltage Comparator indicated VDD > trigger level, else
211  *   false
212  ******************************************************************************/
VCMP_VDDHigher(void)213 __STATIC_INLINE bool VCMP_VDDHigher(void)
214 {
215   if (VCMP->STATUS & VCMP_STATUS_VCMPOUT)
216   {
217     return true;
218   }
219   else
220   {
221     return false;
222   }
223 }
224 
225 
226 /***************************************************************************//**
227  * @brief
228  *    VCMP output is ready
229  ******************************************************************************/
VCMP_Ready(void)230 __STATIC_INLINE bool VCMP_Ready(void)
231 {
232   if (VCMP->STATUS & VCMP_STATUS_VCMPACT)
233   {
234     return true;
235   }
236   else
237   {
238     return false;
239   }
240 }
241 
242 
243 /***************************************************************************//**
244  * @brief
245  *   Clear one or more pending VCMP interrupts.
246  *
247  * @param[in] flags
248  *   VCMP interrupt sources to clear. Use a set of interrupt flags OR-ed
249  *   together to clear multiple interrupt sources for the VCMP module
250  *   (VCMP_IFS_nnn).
251  ******************************************************************************/
VCMP_IntClear(uint32_t flags)252 __STATIC_INLINE void VCMP_IntClear(uint32_t flags)
253 {
254   VCMP->IFC = flags;
255 }
256 
257 
258 /***************************************************************************//**
259  * @brief
260  *   Set one or more pending VCMP interrupts from SW.
261  *
262  * @param[in] flags
263  *   VCMP interrupt sources to set to pending. Use a set of interrupt flags
264  *   OR-ed together to set multiple interrupt sources for the VCMP module
265  *   (VCMP_IFS_nnn).
266  ******************************************************************************/
VCMP_IntSet(uint32_t flags)267 __STATIC_INLINE void VCMP_IntSet(uint32_t flags)
268 {
269   VCMP->IFS = flags;
270 }
271 
272 
273 /***************************************************************************//**
274  * @brief
275  *   Disable one or more VCMP interrupts
276  *
277  * @param[in] flags
278  *   VCMP interrupt sources to enable. Use a set of interrupt flags OR-ed
279  *   together to set multiple interrupt sources for the VCMP module
280  *   (VCMP_IFS_nnn).
281  ******************************************************************************/
VCMP_IntDisable(uint32_t flags)282 __STATIC_INLINE void VCMP_IntDisable(uint32_t flags)
283 {
284   VCMP->IEN &= ~(flags);
285 }
286 
287 
288 /***************************************************************************//**
289  * @brief
290  *   Enable one or more VCMP interrupts
291  *
292  * @param[in] flags
293  *   VCMP interrupt sources to enable. Use a set of interrupt flags OR-ed
294  *   together to set multiple interrupt sources for the VCMP module
295  *   (VCMP_IFS_nnn).
296  ******************************************************************************/
VCMP_IntEnable(uint32_t flags)297 __STATIC_INLINE void VCMP_IntEnable(uint32_t flags)
298 {
299   VCMP->IEN |= flags;
300 }
301 
302 
303 /***************************************************************************//**
304  * @brief
305  *   Get pending VCMP interrupt flags
306  *
307  * @note
308  *   The event bits are not cleared by the use of this function
309  *
310  * @return
311  *   Pending VCMP interrupt sources. Returns a set of interrupt flags OR-ed
312  *   together for multiple interrupt sources in the VCMP module (VCMP_IFS_nnn).
313  ******************************************************************************/
VCMP_IntGet(void)314 __STATIC_INLINE uint32_t VCMP_IntGet(void)
315 {
316   return(VCMP->IF);
317 }
318 
319 
320 /***************************************************************************//**
321  * @brief
322  *   Get enabled and pending VCMP interrupt flags.
323  *
324  * @details
325  *   Useful for handling more interrupt sources in the same interrupt handler.
326  *
327  * @note
328  *   The event bits are not cleared by the use of this function.
329  *
330  * @return
331  *   Pending and enabled VCMP interrupt sources.
332  *   The return value is the bitwise AND combination of
333  *   - the OR combination of enabled interrupt sources in VCMP_IEN_nnn
334  *   register (VCMP_IEN_nnn) and
335  *   - the OR combination of valid interrupt flags of the VCMP module
336  *   (VCMP_IF_nnn).
337  ******************************************************************************/
VCMP_IntGetEnabled(void)338 __STATIC_INLINE uint32_t VCMP_IntGetEnabled(void)
339 {
340   uint32_t tmp = 0U;
341 
342   /* Store VCMP->IEN in temporary variable in order to define explicit order
343    * of volatile accesses. */
344   tmp = VCMP->IEN;
345 
346   /* Bitwise AND of pending and enabled interrupts */
347   return VCMP->IF & tmp;
348 }
349 
350 /** @} (end addtogroup VCMP) */
351 /** @} (end addtogroup EM_Library) */
352 
353 #ifdef __cplusplus
354 }
355 #endif
356 
357 #endif /* __EM_VCMP_H */
358