]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_EFM32_Gecko_Starter_Kit_Simplicity_Studio/Source/SilLabs_Code/emlib/inc/em_vcmp.h
Add EFM32 Giant Gecko Starter Kit demo - still a work in progress as the low power...
[freertos] / FreeRTOS / Demo / CORTEX_EFM32_Gecko_Starter_Kit_Simplicity_Studio / Source / SilLabs_Code / emlib / inc / em_vcmp.h
1 /***************************************************************************//**\r
2  * @file em_vcmp.h\r
3  * @brief Voltage Comparator (VCMP) peripheral API\r
4  * @version 4.0.0\r
5  *******************************************************************************\r
6  * @section License\r
7  * <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b>\r
8  *******************************************************************************\r
9  *\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
13  *\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
19  *\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
26  *\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
30  *\r
31  ******************************************************************************/\r
32 \r
33 \r
34 #ifndef __SILICON_LABS_EM_VCMP_H_\r
35 #define __SILICON_LABS_EM_VCMP_H_\r
36 \r
37 #include "em_device.h"\r
38 #if defined(VCMP_COUNT) && (VCMP_COUNT > 0)\r
39 \r
40 #include <stdint.h>\r
41 #include <stdbool.h>\r
42 \r
43 #ifdef __cplusplus\r
44 extern "C" {\r
45 #endif\r
46 \r
47 /***************************************************************************//**\r
48  * @addtogroup EM_Library\r
49  * @{\r
50  ******************************************************************************/\r
51 \r
52 /***************************************************************************//**\r
53  * @addtogroup VCMP\r
54  * @{\r
55  ******************************************************************************/\r
56 \r
57 /*******************************************************************************\r
58  ********************************   ENUMS   ************************************\r
59  ******************************************************************************/\r
60 \r
61 /** Warm-up Time in High Frequency Peripheral Clock cycles */\r
62 typedef enum\r
63 {\r
64   /** 4 cycles */\r
65   vcmpWarmTime4Cycles   = _VCMP_CTRL_WARMTIME_4CYCLES,\r
66   /** 8 cycles */\r
67   vcmpWarmTime8Cycles   = _VCMP_CTRL_WARMTIME_8CYCLES,\r
68   /** 16 cycles */\r
69   vcmpWarmTime16Cycles  = _VCMP_CTRL_WARMTIME_16CYCLES,\r
70   /** 32 cycles */\r
71   vcmpWarmTime32Cycles  = _VCMP_CTRL_WARMTIME_32CYCLES,\r
72   /** 64 cycles */\r
73   vcmpWarmTime64Cycles  = _VCMP_CTRL_WARMTIME_64CYCLES,\r
74   /** 128 cycles */\r
75   vcmpWarmTime128Cycles = _VCMP_CTRL_WARMTIME_128CYCLES,\r
76   /** 256 cycles */\r
77   vcmpWarmTime256Cycles = _VCMP_CTRL_WARMTIME_256CYCLES,\r
78   /** 512 cycles */\r
79   vcmpWarmTime512Cycles = _VCMP_CTRL_WARMTIME_512CYCLES\r
80 } VCMP_WarmTime_TypeDef;\r
81 \r
82 /** Hyseresis configuration */\r
83 typedef enum\r
84 {\r
85   /** Normal operation, no hysteresis */\r
86   vcmpHystNone,\r
87   /** Digital output will not toggle until positive edge is at least\r
88    *  20mV above or below negative input voltage */\r
89   vcmpHyst20mV\r
90 } VCMP_Hysteresis_TypeDef;\r
91 \r
92 /*******************************************************************************\r
93  *******************************   STRUCTS   ***********************************\r
94  ******************************************************************************/\r
95 \r
96 /** VCMP Initialization structure */\r
97 typedef struct\r
98 {\r
99   /** If set to true, will reduce by half the bias current */\r
100   bool                    halfBias;\r
101   /** BIAS current configuration, depends on halfBias setting,\r
102    *  above, see reference manual */\r
103   int                     biasProg;\r
104   /** Enable interrupt for falling edge */\r
105   bool                    irqFalling;\r
106   /** Enable interrupt for rising edge */\r
107   bool                    irqRising;\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
113   int                     inactive;\r
114   /** Enable low power mode for VDD and bandgap reference */\r
115   bool                    lowPowerRef;\r
116   /** Trigger level, according to formula\r
117    *  VDD Trigger Level = 1.667V + 0.034V x triggerLevel */\r
118   int                     triggerLevel;\r
119   /** Enable VCMP after configuration */\r
120   bool                    enable;\r
121 } VCMP_Init_TypeDef;\r
122 \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
135   }\r
136 \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
143 \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
156 \r
157 /***************************************************************************//**\r
158  * @brief\r
159  *   Enable Voltage Comparator\r
160  ******************************************************************************/\r
161 __STATIC_INLINE void VCMP_Enable(void)\r
162 {\r
163   VCMP->CTRL |= VCMP_CTRL_EN;\r
164 }\r
165 \r
166 \r
167 /***************************************************************************//**\r
168  * @brief\r
169  *   Disable Voltage Comparator\r
170  ******************************************************************************/\r
171 __STATIC_INLINE void VCMP_Disable(void)\r
172 {\r
173   VCMP->CTRL &= ~(VCMP_CTRL_EN);\r
174 }\r
175 \r
176 \r
177 /***************************************************************************//**\r
178  * @brief\r
179  *   Calculate voltage to trigger level\r
180  *\r
181  * @note\r
182  *   You need soft float support for this function to be working\r
183  *\r
184  * @param[in] v\r
185  *   Voltage Level for trigger\r
186  ******************************************************************************/\r
187 __STATIC_INLINE uint32_t VCMP_VoltageToLevel(float v)\r
188 {\r
189   return (uint32_t)((v - (float)1.667) / (float)0.034);\r
190 }\r
191 \r
192 \r
193 /***************************************************************************//**\r
194  * @brief\r
195  *   Returns true, if Voltage Comparator indicated VDD < trigger level, else\r
196  *   false\r
197  ******************************************************************************/\r
198 __STATIC_INLINE bool VCMP_VDDLower(void)\r
199 {\r
200   if (VCMP->STATUS & VCMP_STATUS_VCMPOUT)\r
201   {\r
202     return false;\r
203   }\r
204   else\r
205   {\r
206     return true;\r
207   }\r
208 }\r
209 \r
210 \r
211 /***************************************************************************//**\r
212  * @brief\r
213  *   Returns true, if Voltage Comparator indicated VDD > trigger level, else\r
214  *   false\r
215  ******************************************************************************/\r
216 __STATIC_INLINE bool VCMP_VDDHigher(void)\r
217 {\r
218   if (VCMP->STATUS & VCMP_STATUS_VCMPOUT)\r
219   {\r
220     return true;\r
221   }\r
222   else\r
223   {\r
224     return false;\r
225   }\r
226 }\r
227 \r
228 \r
229 /***************************************************************************//**\r
230  * @brief\r
231  *    VCMP output is ready\r
232  ******************************************************************************/\r
233 __STATIC_INLINE bool VCMP_Ready(void)\r
234 {\r
235   if (VCMP->STATUS & VCMP_STATUS_VCMPACT)\r
236   {\r
237     return true;\r
238   }\r
239   else\r
240   {\r
241     return false;\r
242   }\r
243 }\r
244 \r
245 \r
246 /***************************************************************************//**\r
247  * @brief\r
248  *   Clear one or more pending VCMP interrupts.\r
249  *\r
250  * @param[in] flags\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
253  *   (VCMP_IFS_nnn).\r
254  ******************************************************************************/\r
255 __STATIC_INLINE void VCMP_IntClear(uint32_t flags)\r
256 {\r
257   VCMP->IFC = flags;\r
258 }\r
259 \r
260 \r
261 /***************************************************************************//**\r
262  * @brief\r
263  *   Set one or more pending VCMP interrupts from SW.\r
264  *\r
265  * @param[in] flags\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
268  *   (VCMP_IFS_nnn).\r
269  ******************************************************************************/\r
270 __STATIC_INLINE void VCMP_IntSet(uint32_t flags)\r
271 {\r
272   VCMP->IFS = flags;\r
273 }\r
274 \r
275 \r
276 /***************************************************************************//**\r
277  * @brief\r
278  *   Disable one or more VCMP interrupts\r
279  *\r
280  * @param[in] flags\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
283  *   (VCMP_IFS_nnn).\r
284  ******************************************************************************/\r
285 __STATIC_INLINE void VCMP_IntDisable(uint32_t flags)\r
286 {\r
287   VCMP->IEN &= ~(flags);\r
288 }\r
289 \r
290 \r
291 /***************************************************************************//**\r
292  * @brief\r
293  *   Enable one or more VCMP interrupts\r
294  *\r
295  * @param[in] flags\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
298  *   (VCMP_IFS_nnn).\r
299  ******************************************************************************/\r
300 __STATIC_INLINE void VCMP_IntEnable(uint32_t flags)\r
301 {\r
302   VCMP->IEN |= flags;\r
303 }\r
304 \r
305 \r
306 /***************************************************************************//**\r
307  * @brief\r
308  *   Get pending VCMP interrupt flags\r
309  *\r
310  * @note\r
311  *   The event bits are not cleared by the use of this function\r
312  *\r
313  * @return\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
318 {\r
319   return(VCMP->IF);\r
320 }\r
321 \r
322 \r
323 /***************************************************************************//**\r
324  * @brief\r
325  *   Get enabled and pending VCMP interrupt flags.\r
326  *\r
327  * @details\r
328  *   Useful for handling more interrupt sources in the same interrupt handler.\r
329  *\r
330  * @note\r
331  *   The event bits are not cleared by the use of this function.\r
332  *\r
333  * @return\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
339  *   (VCMP_IF_nnn).\r
340  ******************************************************************************/\r
341 __STATIC_INLINE uint32_t VCMP_IntGetEnabled(void)\r
342 {\r
343   uint32_t tmp = 0U;\r
344 \r
345   /* Store VCMP->IEN in temporary variable in order to define explicit order\r
346    * of volatile accesses. */\r
347   tmp = VCMP->IEN;\r
348 \r
349   /* Bitwise AND of pending and enabled interrupts */\r
350   return VCMP->IF & tmp;\r
351 }\r
352 \r
353 /** @} (end addtogroup VCMP) */\r
354 /** @} (end addtogroup EM_Library) */\r
355 \r
356 #ifdef __cplusplus\r
357 }\r
358 #endif\r
359 \r
360 #endif /* defined(VCMP_COUNT) && (VCMP_COUNT > 0) */\r
361 #endif /* __SILICON_LABS_EM_VCMP_H_ */\r