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