]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_EFM32_Giant_Gecko_Simplicity_Studio/SiLabs_Source/emlib/em_rtc.c
Replace Gecko Simplicity Studio project that had multiple build configurations with...
[freertos] / FreeRTOS / Demo / CORTEX_EFM32_Giant_Gecko_Simplicity_Studio / SiLabs_Source / emlib / em_rtc.c
1 /***************************************************************************//**\r
2  * @file em_rtc.c\r
3  * @brief Real Time Counter (RTC) 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 #include "em_rtc.h"\r
34 #if defined(RTC_COUNT) && (RTC_COUNT > 0)\r
35 \r
36 #include "em_assert.h"\r
37 #include "em_bus.h"\r
38 \r
39 /***************************************************************************//**\r
40  * @addtogroup EM_Library\r
41  * @{\r
42  ******************************************************************************/\r
43 \r
44 /***************************************************************************//**\r
45  * @addtogroup RTC\r
46  * @brief Real Time Counter (RTC) Peripheral API\r
47  * @{\r
48  ******************************************************************************/\r
49 \r
50 /*******************************************************************************\r
51  *******************************   DEFINES   ***********************************\r
52  ******************************************************************************/\r
53 \r
54 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */\r
55 \r
56 /** Validation of valid comparator register for assert statements. */\r
57 #define RTC_COMP_REG_VALID(reg)    (((reg) <= 1))\r
58 \r
59 /** @endcond */\r
60 \r
61 \r
62 /*******************************************************************************\r
63  **************************   LOCAL FUNCTIONS   ********************************\r
64  ******************************************************************************/\r
65 \r
66 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */\r
67 \r
68 #if defined(_EFM32_GECKO_FAMILY)\r
69 /***************************************************************************//**\r
70  * @brief\r
71  *   Wait for ongoing sync of register(s) to low frequency domain to complete.\r
72  *\r
73  * @note\r
74  *   This only applies to the Gecko Family, see the reference manual\r
75  *   chapter about Access to Low Energy Peripherals (Asynchronos Registers)\r
76  *   for details. For Tiny Gecko and Giant Gecko, the RTC supports immediate\r
77  *   updates of registers, and will automatically hold the bus until the\r
78  *   register has been updated.\r
79  *\r
80  * @param[in] mask\r
81  *   Bitmask corresponding to SYNCBUSY register defined bits, indicating\r
82  *   registers that must complete any ongoing synchronization.\r
83  ******************************************************************************/\r
84 __STATIC_INLINE void regSync(uint32_t mask)\r
85 {\r
86   /* Avoid deadlock if modifying the same register twice when freeze mode is */\r
87   /* activated. */\r
88   if (RTC->FREEZE & RTC_FREEZE_REGFREEZE)\r
89     return;\r
90 \r
91   /* Wait for any pending previous write operation to have been completed */\r
92   /* in low frequency domain. This is only required for the Gecko Family */\r
93   while (RTC->SYNCBUSY & mask)\r
94     ;\r
95 }\r
96 #endif\r
97 \r
98 /** @endcond */\r
99 \r
100 /*******************************************************************************\r
101  **************************   GLOBAL FUNCTIONS   *******************************\r
102  ******************************************************************************/\r
103 \r
104 /***************************************************************************//**\r
105  * @brief\r
106  *   Get RTC compare register value.\r
107  *\r
108  * @param[in] comp\r
109  *   Compare register to get, either 0 or 1\r
110  *\r
111  * @return\r
112  *   Compare register value, 0 if invalid register selected.\r
113  ******************************************************************************/\r
114 uint32_t RTC_CompareGet(unsigned int comp)\r
115 {\r
116   uint32_t ret;\r
117 \r
118   EFM_ASSERT(RTC_COMP_REG_VALID(comp));\r
119 \r
120   /* Initialize selected compare value */\r
121   switch (comp)\r
122   {\r
123     case 0:\r
124       ret = RTC->COMP0;\r
125       break;\r
126 \r
127     case 1:\r
128       ret = RTC->COMP1;\r
129       break;\r
130 \r
131     default:\r
132       /* Unknown compare register selected */\r
133       ret = 0;\r
134       break;\r
135   }\r
136 \r
137   return ret;\r
138 }\r
139 \r
140 \r
141 /***************************************************************************//**\r
142  * @brief\r
143  *   Set RTC compare register value.\r
144  *\r
145  * @note\r
146  *   The setting of a compare register requires synchronization into the\r
147  *   low frequency domain. If the same register is modified before a previous\r
148  *   update has completed, this function will stall until the previous\r
149  *   synchronization has completed. This only applies to the Gecko Family, see\r
150  *   comment in the regSync() internal function call.\r
151  *\r
152  * @param[in] comp\r
153  *   Compare register to set, either 0 or 1\r
154  *\r
155  * @param[in] value\r
156  *   Initialization value (<= 0x00ffffff)\r
157  ******************************************************************************/\r
158 void RTC_CompareSet(unsigned int comp, uint32_t value)\r
159 {\r
160   volatile uint32_t *compReg;\r
161 #if defined(_EFM32_GECKO_FAMILY)\r
162   uint32_t          syncbusy;\r
163 #endif\r
164 \r
165   EFM_ASSERT(RTC_COMP_REG_VALID(comp)\r
166              && ((value & ~(_RTC_COMP0_COMP0_MASK\r
167                             >> _RTC_COMP0_COMP0_SHIFT)) == 0));\r
168 \r
169   /* Initialize selected compare value */\r
170   switch (comp)\r
171   {\r
172     case 0:\r
173       compReg = &(RTC->COMP0);\r
174 #if defined(_EFM32_GECKO_FAMILY)\r
175       syncbusy = RTC_SYNCBUSY_COMP0;\r
176 #endif\r
177       break;\r
178 \r
179     case 1:\r
180       compReg = &(RTC->COMP1);\r
181 #if defined(_EFM32_GECKO_FAMILY)\r
182       syncbusy = RTC_SYNCBUSY_COMP1;\r
183 #endif\r
184       break;\r
185 \r
186     default:\r
187       /* Unknown compare register selected, abort */\r
188       return;\r
189   }\r
190 #if defined(_EFM32_GECKO_FAMILY)\r
191   /* LF register about to be modified require sync. busy check */\r
192   regSync(syncbusy);\r
193 #endif\r
194 \r
195   *compReg = value;\r
196 }\r
197 \r
198 \r
199 /***************************************************************************//**\r
200  * @brief\r
201  *   Enable/disable RTC.\r
202  *\r
203  * @note\r
204  *   The enabling/disabling of the RTC modifies the RTC CTRL register which\r
205  *   requires synchronization into the low frequency domain. If this register is\r
206  *   modified before a previous update to the same register has completed, this\r
207  *   function will stall until the previous synchronization has completed. This\r
208  *   only applies to the Gecko Family, see comment in the regSync() internal\r
209  *   function call.\r
210  *\r
211  * @param[in] enable\r
212  *   true to enable counting, false to disable.\r
213  ******************************************************************************/\r
214 void RTC_Enable(bool enable)\r
215 {\r
216 #if defined(_EFM32_GECKO_FAMILY)\r
217   /* LF register about to be modified require sync. busy check */\r
218   regSync(RTC_SYNCBUSY_CTRL);\r
219 #endif\r
220 \r
221   BUS_RegBitWrite(&(RTC->CTRL), _RTC_CTRL_EN_SHIFT, enable);\r
222 \r
223 #if defined(_EFM32_GECKO_FAMILY)\r
224   /* Wait for CTRL to be updated before returning, because calling code may\r
225      depend upon that the CTRL register is updated after this function has\r
226      returned. */\r
227   regSync(RTC_SYNCBUSY_CTRL);\r
228 #endif\r
229 }\r
230 \r
231 \r
232 /***************************************************************************//**\r
233  * @brief\r
234  *   RTC register synchronization freeze control.\r
235  *\r
236  * @details\r
237  *   Some RTC registers require synchronization into the low frequency (LF)\r
238  *   domain. The freeze feature allows for several such registers to be\r
239  *   modified before passing them to the LF domain simultaneously (which\r
240  *   takes place when the freeze mode is disabled).\r
241  *\r
242  * @note\r
243  *   When enabling freeze mode, this function will wait for all current\r
244  *   ongoing RTC synchronization to LF domain to complete (Normally\r
245  *   synchronization will not be in progress.) However for this reason, when\r
246  *   using freeze mode, modifications of registers requiring LF synchronization\r
247  *   should be done within one freeze enable/disable block to avoid unecessary\r
248  *   stalling. This only applies to the Gecko Family, see the reference manual\r
249  *   chapter about Access to Low Energy Peripherals (Asynchronos Registers)\r
250  *   for details.\r
251  *\r
252  * @param[in] enable\r
253  *   @li true - enable freeze, modified registers are not propagated to the\r
254  *       LF domain\r
255  *   @li false - disables freeze, modified registers are propagated to LF\r
256  *       domain\r
257  ******************************************************************************/\r
258 void RTC_FreezeEnable(bool enable)\r
259 {\r
260   if (enable)\r
261   {\r
262 #if defined(_EFM32_GECKO_FAMILY)\r
263     /* Wait for any ongoing LF synchronization to complete. This is just to */\r
264     /* protect against the rare case when a user                            */\r
265     /* - modifies a register requiring LF sync                              */\r
266     /* - then enables freeze before LF sync completed                       */\r
267     /* - then modifies the same register again                              */\r
268     /* since modifying a register while it is in sync progress should be    */\r
269     /* avoided.                                                             */\r
270     while (RTC->SYNCBUSY)\r
271       ;\r
272 #endif\r
273     RTC->FREEZE = RTC_FREEZE_REGFREEZE;\r
274   }\r
275   else\r
276   {\r
277     RTC->FREEZE = 0;\r
278   }\r
279 }\r
280 \r
281 \r
282 /***************************************************************************//**\r
283  * @brief\r
284  *   Initialize RTC.\r
285  *\r
286  * @details\r
287  *   Note that the compare values must be set separately with RTC_CompareSet().\r
288  *   That should probably be done prior to the use of this function if\r
289  *   configuring the RTC to start when initialization is completed.\r
290  *\r
291  * @note\r
292  *   The initialization of the RTC modifies the RTC CTRL register which requires\r
293  *   synchronization into the low frequency domain. If this register is\r
294  *   modified before a previous update to the same register has completed, this\r
295  *   function will stall until the previous synchronization has completed. This\r
296  *   only applies to the Gecko Family, see comment in the regSync() internal\r
297  *   function call.\r
298  *\r
299  * @param[in] init\r
300  *   Pointer to RTC initialization structure.\r
301  ******************************************************************************/\r
302 void RTC_Init(const RTC_Init_TypeDef *init)\r
303 {\r
304   uint32_t tmp;\r
305 \r
306   if (init->enable)\r
307   {\r
308     tmp = RTC_CTRL_EN;\r
309   }\r
310   else\r
311   {\r
312     tmp = 0;\r
313   }\r
314 \r
315   /* Configure DEBUGRUN flag, sets whether or not counter should be\r
316    * updated when debugger is active */\r
317   if (init->debugRun)\r
318   {\r
319     tmp |= RTC_CTRL_DEBUGRUN;\r
320   }\r
321 \r
322   /* Configure COMP0TOP, this will use the COMP0 compare value as an\r
323    * overflow value, instead of default 24-bit 0x00ffffff */\r
324   if (init->comp0Top)\r
325   {\r
326     tmp |= RTC_CTRL_COMP0TOP;\r
327   }\r
328 \r
329 #if defined(_EFM32_GECKO_FAMILY)\r
330   /* LF register about to be modified require sync. busy check */\r
331   regSync(RTC_SYNCBUSY_CTRL);\r
332 #endif\r
333 \r
334   RTC->CTRL = tmp;\r
335 }\r
336 \r
337 \r
338 \r
339 /***************************************************************************//**\r
340  * @brief\r
341  *   Restore RTC to reset state\r
342  ******************************************************************************/\r
343 void RTC_Reset(void)\r
344 {\r
345   /* Restore all essential RTC register to default config */\r
346   RTC->FREEZE = _RTC_FREEZE_RESETVALUE;\r
347   RTC->CTRL   = _RTC_CTRL_RESETVALUE;\r
348   RTC->COMP0  = _RTC_COMP0_RESETVALUE;\r
349   RTC->COMP1  = _RTC_COMP1_RESETVALUE;\r
350   RTC->IEN    = _RTC_IEN_RESETVALUE;\r
351   RTC->IFC    = _RTC_IFC_RESETVALUE;\r
352 \r
353 #if defined(_EFM32_GECKO_FAMILY)\r
354   /* Wait for CTRL, COMP0 and COMP1 to be updated before returning, because the\r
355      calling code may depend upon that the register values are updated after\r
356      this function has returned. */\r
357   regSync(RTC_SYNCBUSY_CTRL | RTC_SYNCBUSY_COMP0 | RTC_SYNCBUSY_COMP1);\r
358 #endif\r
359 }\r
360 \r
361 \r
362 \r
363 /***************************************************************************//**\r
364  * @brief\r
365  *   Restart RTC counter from zero\r
366  ******************************************************************************/\r
367 void RTC_CounterReset(void)\r
368 {\r
369   /* A disable/enable sequnce will start the counter at zero */\r
370   RTC_Enable(false);\r
371   RTC_Enable(true);\r
372 }\r
373 \r
374 \r
375 /** @} (end addtogroup RTC) */\r
376 /** @} (end addtogroup EM_Library) */\r
377 #endif /* defined(RTC_COUNT) && (RTC_COUNT > 0) */\r