]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_EFM32_Gecko_Starter_Kit_Simplicity_Studio/Source/SilLabs_Code/emlib/inc/em_burtc.h
Add Pearl Gecko demo.
[freertos] / FreeRTOS / Demo / CORTEX_EFM32_Gecko_Starter_Kit_Simplicity_Studio / Source / SilLabs_Code / emlib / inc / em_burtc.h
1 /***************************************************************************//**\r
2  * @file em_burtc.h\r
3  * @brief Backup Real Time Counter (BURTC) 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_BURTC_H__\r
34 #define __SILICON_LABS_EM_BURTC_H__\r
35 \r
36 #include "em_device.h"\r
37 #if defined(BURTC_PRESENT)\r
38 \r
39 #include <stdbool.h>\r
40 #include "em_assert.h"\r
41 #include "em_bus.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 BURTC\r
54  * @{\r
55  ******************************************************************************/\r
56 \r
57 /*******************************************************************************\r
58  *******************************   DEFINES   ***********************************\r
59  ******************************************************************************/\r
60 \r
61 /** BURTC clock divisors. These values are valid for the BURTC prescaler. */\r
62 #define burtcClkDiv_1      1     /**< Divide clock by 1. */\r
63 #define burtcClkDiv_2      2     /**< Divide clock by 2. */\r
64 #define burtcClkDiv_4      4     /**< Divide clock by 4. */\r
65 #define burtcClkDiv_8      8     /**< Divide clock by 8. */\r
66 #define burtcClkDiv_16     16    /**< Divide clock by 16. */\r
67 #define burtcClkDiv_32     32    /**< Divide clock by 32. */\r
68 #define burtcClkDiv_64     64    /**< Divide clock by 64. */\r
69 #define burtcClkDiv_128    128   /**< Divide clock by 128. */\r
70 \r
71 /*******************************************************************************\r
72  ********************************   ENUMS   ************************************\r
73  ******************************************************************************/\r
74 \r
75 /** BURTC clock selection */\r
76 typedef enum\r
77 {\r
78   /** Ultra low frequency (1 kHz) clock */\r
79   burtcClkSelULFRCO = BURTC_CTRL_CLKSEL_ULFRCO,\r
80   /** Low frequency RC oscillator */\r
81   burtcClkSelLFRCO  = BURTC_CTRL_CLKSEL_LFRCO,\r
82   /** Low frequency crystal osciallator */\r
83   burtcClkSelLFXO   = BURTC_CTRL_CLKSEL_LFXO\r
84 } BURTC_ClkSel_TypeDef;\r
85 \r
86 \r
87 /** BURTC mode of operation */\r
88 typedef enum\r
89 {\r
90   /** Disable BURTC */\r
91   burtcModeDisable = BURTC_CTRL_MODE_DISABLE,\r
92   /** Enable and start BURTC counter in EM0 to EM2 */\r
93   burtcModeEM2     = BURTC_CTRL_MODE_EM2EN,\r
94   /** Enable and start BURTC counter in EM0 to EM3 */\r
95   burtcModeEM3     = BURTC_CTRL_MODE_EM3EN,\r
96   /** Enable and start BURTC counter in EM0 to EM4 */\r
97   burtcModeEM4     = BURTC_CTRL_MODE_EM4EN,\r
98 } BURTC_Mode_TypeDef;\r
99 \r
100 /** BURTC low power mode */\r
101 typedef enum\r
102 {\r
103   /** Low Power Mode is disabled */\r
104   burtcLPDisable = BURTC_LPMODE_LPMODE_DISABLE,\r
105   /** Low Power Mode is always enabled */\r
106   burtcLPEnable  = BURTC_LPMODE_LPMODE_ENABLE,\r
107   /** Low Power Mode when system enters backup mode */\r
108   burtcLPBU      = BURTC_LPMODE_LPMODE_BUEN\r
109 } BURTC_LP_TypeDef;\r
110 \r
111 /*******************************************************************************\r
112  *******************************   STRUCTS   ***********************************\r
113  ******************************************************************************/\r
114 \r
115 /** BURTC initialization structure. */\r
116 typedef struct\r
117 {\r
118   bool                 enable;       /**< Enable BURTC after initialization (starts counter) */\r
119 \r
120   BURTC_Mode_TypeDef   mode;         /**< Configure energy mode operation */\r
121   bool                 debugRun;     /**< If true, counter will keep running under debug halt */\r
122   BURTC_ClkSel_TypeDef clkSel;       /**< Select clock source */\r
123   uint32_t             clkDiv;       /**< Clock divider; for ULFRCO 1Khz or 2kHz operation */\r
124 \r
125   uint32_t             lowPowerComp; /**< Number of least significantt clock bits to ignore in low power mode */\r
126   bool                 timeStamp;    /**< Enable time stamp on entering backup power domain */\r
127 \r
128   bool                 compare0Top;  /**< Set if Compare Value 0 is also top value (counter restart) */\r
129 \r
130   BURTC_LP_TypeDef     lowPowerMode; /**< Low power operation mode, requires LFXO or LFRCO */\r
131 } BURTC_Init_TypeDef;\r
132 \r
133 /** Default configuration for BURTC init structure */\r
134 #define BURTC_INIT_DEFAULT  \\r
135 {                           \\r
136   true,                     \\r
137   burtcModeEM2,             \\r
138   false,                    \\r
139   burtcClkSelULFRCO,        \\r
140   burtcClkDiv_1,            \\r
141   0,                        \\r
142   true,                     \\r
143   false,                    \\r
144   burtcLPDisable,           \\r
145 }\r
146 \r
147 /*******************************************************************************\r
148  *****************************   PROTOTYPES   **********************************\r
149  ******************************************************************************/\r
150 \r
151 /***************************************************************************//**\r
152  * @brief\r
153  *   Clear one or more pending BURTC interrupts.\r
154  *\r
155  * @param[in] flags\r
156  *   BURTC interrupt sources to clear. Use a set of interrupt flags OR-ed\r
157  *   together to clear multiple interrupt sources for the BURTC module\r
158  *   (BURTC_IFS_nnn).\r
159  ******************************************************************************/\r
160 __STATIC_INLINE void BURTC_IntClear(uint32_t flags)\r
161 {\r
162   BURTC->IFC = flags;\r
163 }\r
164 \r
165 \r
166 /***************************************************************************//**\r
167  * @brief\r
168  *   Disable one or more BURTC interrupts.\r
169  *\r
170  * @param[in] flags\r
171  *   BURTC interrupt sources to disable. Use a set of interrupt flags OR-ed\r
172  *   together to disable multiple interrupt sources for the BURTC module\r
173  *   (BURTC_IFS_nnn).\r
174  ******************************************************************************/\r
175 __STATIC_INLINE void BURTC_IntDisable(uint32_t flags)\r
176 {\r
177   BURTC->IEN &= ~(flags);\r
178 }\r
179 \r
180 \r
181 /***************************************************************************//**\r
182  * @brief\r
183  *   Enable one or more BURTC interrupts.\r
184  *\r
185  * @note\r
186  *   Depending on the use, a pending interrupt may already be set prior to\r
187  *   enabling the interrupt. Consider using BURTC_IntClear() prior to enabling\r
188  *   if such a pending interrupt should be ignored.\r
189  *\r
190  * @param[in] flags\r
191  *   BURTC interrupt sources to enable. Use a set of interrupt flags OR-ed\r
192  *   together to set multiple interrupt sources for the BURTC module\r
193  *   (BURTC_IFS_nnn).\r
194  ******************************************************************************/\r
195 __STATIC_INLINE void BURTC_IntEnable(uint32_t flags)\r
196 {\r
197   BURTC->IEN |= flags;\r
198 }\r
199 \r
200 \r
201 /***************************************************************************//**\r
202  * @brief\r
203  *   Get pending BURTC interrupt flags.\r
204  *\r
205  * @note\r
206  *   The event bits are not cleared by the use of this function.\r
207  *\r
208  * @return\r
209  *   Pending BURTC interrupt sources. Returns a set of interrupt flags OR-ed\r
210  *   together for multiple interrupt sources in the BURTC module (BURTC_IFS_nnn).\r
211  ******************************************************************************/\r
212 __STATIC_INLINE uint32_t BURTC_IntGet(void)\r
213 {\r
214   return(BURTC->IF);\r
215 }\r
216 \r
217 \r
218 /***************************************************************************//**\r
219  * @brief\r
220  *   Get enabled and pending BURTC interrupt flags.\r
221  *\r
222  * @note\r
223  *   The event bits are not cleared by the use of this function.\r
224  *\r
225  * @return\r
226  *   Pending BURTC interrupt sources that is also enabled. Returns a set of\r
227  *   interrupt flags OR-ed together for multiple interrupt sources in the\r
228  *   BURTC module (BURTC_IFS_nnn).\r
229  ******************************************************************************/\r
230 __STATIC_INLINE uint32_t BURTC_IntGetEnabled(void)\r
231 {\r
232   uint32_t tmp;\r
233 \r
234   /* Get enabled interrupts */\r
235   tmp = BURTC->IEN;\r
236 \r
237   /* Return set intterupts */\r
238   return BURTC->IF & tmp;\r
239 }\r
240 \r
241 \r
242 /***************************************************************************//**\r
243  * @brief\r
244  *   Set one or more pending BURTC interrupts from SW.\r
245  *\r
246  * @param[in] flags\r
247  *   BURTC interrupt sources to set to pending. Use a set of interrupt flags\r
248  *   OR-ed together to set multiple interrupt sources for the BURTC module\r
249  *   (BURTC_IFS_nnn).\r
250  ******************************************************************************/\r
251 __STATIC_INLINE void BURTC_IntSet(uint32_t flags)\r
252 {\r
253   BURTC->IFS = flags;\r
254 }\r
255 \r
256 \r
257 /***************************************************************************//**\r
258  * @brief\r
259  *   Status of BURTC RAM, timestamp and LP Mode\r
260  *\r
261  * @return A mask logially OR-ed status bits\r
262  ******************************************************************************/\r
263 __STATIC_INLINE uint32_t BURTC_Status(void)\r
264 {\r
265   return BURTC->STATUS;\r
266 }\r
267 \r
268 \r
269 /***************************************************************************//**\r
270  * @brief\r
271  *   Clear and reset BURTC status register\r
272  ******************************************************************************/\r
273 __STATIC_INLINE void BURTC_StatusClear(void)\r
274 {\r
275   BURTC->CMD = BURTC_CMD_CLRSTATUS;\r
276 }\r
277 \r
278 \r
279 /***************************************************************************//**\r
280  * @brief\r
281  *   Enable or Disable BURTC peripheral reset and start counter\r
282  * @param[in] enable\r
283  *   If true; asserts reset to BURTC, halts counter, if false; deassert reset\r
284  ******************************************************************************/\r
285 __STATIC_INLINE void BURTC_Enable(bool enable)\r
286 {\r
287   /* Note! If mode is disabled, BURTC counter will not start */\r
288   EFM_ASSERT(((enable == true)\r
289               && ((BURTC->CTRL & _BURTC_CTRL_MODE_MASK)\r
290                   != BURTC_CTRL_MODE_DISABLE))\r
291              || (enable == false));\r
292   if (enable)\r
293   {\r
294     BUS_RegBitWrite(&BURTC->CTRL, _BURTC_CTRL_RSTEN_SHIFT, 0);\r
295   }\r
296   else\r
297   {\r
298     BUS_RegBitWrite(&BURTC->CTRL, _BURTC_CTRL_RSTEN_SHIFT, 1);\r
299   }\r
300 }\r
301 \r
302 \r
303 /***************************************************************************//**\r
304  * @brief Get BURTC counter\r
305  *\r
306  * @return\r
307  *   BURTC counter value\r
308  ******************************************************************************/\r
309 __STATIC_INLINE uint32_t BURTC_CounterGet(void)\r
310 {\r
311   return BURTC->CNT;\r
312 }\r
313 \r
314 \r
315 /***************************************************************************//**\r
316  * @brief Get BURTC timestamp for entering BU\r
317  *\r
318  * @return\r
319  *   BURTC Time Stamp value\r
320  ******************************************************************************/\r
321 __STATIC_INLINE uint32_t BURTC_TimestampGet(void)\r
322 {\r
323   return BURTC->TIMESTAMP;\r
324 }\r
325 \r
326 \r
327 /***************************************************************************//**\r
328  * @brief Freeze register updates until enabled\r
329  * @param[in] enable If true, registers are not updated until enabled again.\r
330  ******************************************************************************/\r
331 __STATIC_INLINE void BURTC_FreezeEnable(bool enable)\r
332 {\r
333   BUS_RegBitWrite(&BURTC->FREEZE, _BURTC_FREEZE_REGFREEZE_SHIFT, enable);\r
334 }\r
335 \r
336 \r
337 /***************************************************************************//**\r
338  * @brief Shut down power to rentention register bank.\r
339  * @param[in] enable\r
340  *     If true, shuts off power to retention registers.\r
341  * @note\r
342  *    When power rentention is disabled, it cannot be enabled again (until\r
343  *    reset).\r
344  ******************************************************************************/\r
345 __STATIC_INLINE void BURTC_Powerdown(bool enable)\r
346 {\r
347   BUS_RegBitWrite(&BURTC->POWERDOWN, _BURTC_POWERDOWN_RAM_SHIFT, enable);\r
348 }\r
349 \r
350 \r
351 /***************************************************************************//**\r
352  * @brief\r
353  *   Set a value in one of the retention registers\r
354  *\r
355  * @param[in] num\r
356  *   Register to set\r
357  * @param[in] data\r
358  *   Value to put into register\r
359  ******************************************************************************/\r
360 __STATIC_INLINE void BURTC_RetRegSet(uint32_t num, uint32_t data)\r
361 {\r
362   EFM_ASSERT(num <= 127);\r
363 \r
364   BURTC->RET[num].REG = data;\r
365 }\r
366 \r
367 \r
368 /***************************************************************************//**\r
369  * @brief\r
370  *   Read a value from one of the retention registers\r
371  *\r
372  * @param[in] num\r
373  *   Retention Register to read\r
374  ******************************************************************************/\r
375 __STATIC_INLINE uint32_t BURTC_RetRegGet(uint32_t num)\r
376 {\r
377   EFM_ASSERT(num <= 127);\r
378 \r
379   return BURTC->RET[num].REG;\r
380 }\r
381 \r
382 \r
383 /***************************************************************************//**\r
384  * @brief\r
385  *   Lock BURTC registers, will protect from writing new config settings\r
386  ******************************************************************************/\r
387 __STATIC_INLINE void BURTC_Lock(void)\r
388 {\r
389   BURTC->LOCK = BURTC_LOCK_LOCKKEY_LOCK;\r
390 }\r
391 \r
392 \r
393 /***************************************************************************//**\r
394  * @brief\r
395  *   Unlock BURTC registers, enable write access to change configuration\r
396  ******************************************************************************/\r
397 __STATIC_INLINE void BURTC_Unlock(void)\r
398 {\r
399   BURTC->LOCK = BURTC_LOCK_LOCKKEY_UNLOCK;\r
400 }\r
401 \r
402 \r
403 void BURTC_Reset(void);\r
404 void BURTC_Init(const BURTC_Init_TypeDef *burtcInit);\r
405 void BURTC_CounterReset(void);\r
406 void BURTC_CompareSet(unsigned int comp, uint32_t value);\r
407 uint32_t BURTC_CompareGet(unsigned int comp);\r
408 uint32_t BURTC_ClockFreqGet(void);\r
409 \r
410 \r
411 /** @} (end addtogroup BURTC) */\r
412 /** @} (end addtogroup EM_Library) */\r
413 \r
414 #ifdef __cplusplus\r
415 }\r
416 #endif\r
417 \r
418 #endif /* BURTC_PRESENT */\r
419 #endif /* __SILICON_LABS_EM_BURTC_H__ */\r