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