1 /***************************************************************************//**
\r
3 * @brief Backup Real Time Counter (BURTC) Peripheral API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b>
\r
8 *******************************************************************************
\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
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
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
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
31 ******************************************************************************/
\r
34 #include "em_burtc.h"
\r
35 #if defined(BURTC_PRESENT)
\r
37 /***************************************************************************//**
\r
38 * @addtogroup EM_Library
\r
40 ******************************************************************************/
\r
42 /***************************************************************************//**
\r
44 * @brief Backup Real Time Counter (BURTC) Peripheral API
\r
46 ******************************************************************************/
\r
48 /*******************************************************************************
\r
49 ******************************* DEFINES ***********************************
\r
50 ******************************************************************************/
\r
52 /*******************************************************************************
\r
53 ************************** LOCAL FUNCTIONS ********************************
\r
54 ******************************************************************************/
\r
56 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
\r
57 /***************************************************************************//**
\r
58 * @brief Convert dividend to prescaler logarithmic value. Only works for even
\r
59 * numbers equal to 2^n
\r
60 * @param[in] div Unscaled dividend,
\r
61 * @return Base 2 logarithm of input, as used by fixed prescalers
\r
62 ******************************************************************************/
\r
63 __STATIC_INLINE uint32_t divToLog2(uint32_t div)
\r
67 /* Prescaler accepts an argument of 128 or less, valid values being 2^n */
\r
68 EFM_ASSERT((div > 0) && (div <= 32768));
\r
70 /* Count leading zeroes and "reverse" result, Cortex-M3 intrinsic */
\r
71 log2 = (31 - __CLZ(div));
\r
77 /***************************************************************************//**
\r
79 * Wait for ongoing sync of register(s) to low frequency domain to complete.
\r
82 * Bitmask corresponding to SYNCBUSY register defined bits, indicating
\r
83 * registers that must complete any ongoing synchronization.
\r
84 ******************************************************************************/
\r
85 __STATIC_INLINE void regSync(uint32_t mask)
\r
87 /* Avoid deadlock if modifying the same register twice when freeze mode is
\r
88 activated, or when no clock is selected for the BURTC. If no clock is
\r
89 selected, then the sync is done once the clock source is set. */
\r
90 if ((BURTC->FREEZE & BURTC_FREEZE_REGFREEZE)
\r
91 || ((BURTC->CTRL & _BURTC_CTRL_CLKSEL_MASK) != _BURTC_CTRL_CLKSEL_NONE))
\r
95 /* Wait for any pending previous write operation to have been completed */
\r
96 /* in low frequency domain. This is only required for the Gecko Family */
\r
97 while (BURTC->SYNCBUSY & mask)
\r
103 /*******************************************************************************
\r
104 ************************** GLOBAL FUNCTIONS *******************************
\r
105 ******************************************************************************/
\r
107 /***************************************************************************//**
\r
108 * @brief Initialize BURTC
\r
111 * Configures the BURTC peripheral.
\r
114 * Before initialization, BURTC module must first be enabled by clearing the
\r
115 * reset bit in the RMU, i.e.
\r
117 * RMU_ResetControl(rmuResetBU, rmuResetModeClear);
\r
119 * Compare channel 0 must be configured outside this function, before
\r
120 * initialization if enable is set to true. The counter will always be reset.
\r
122 * @param[in] burtcInit
\r
123 * Pointer to BURTC initialization structure
\r
124 ******************************************************************************/
\r
125 void BURTC_Init(const BURTC_Init_TypeDef *burtcInit)
\r
130 /* Check initializer structure integrity */
\r
131 EFM_ASSERT(burtcInit != (BURTC_Init_TypeDef *) 0);
\r
132 /* Clock divider must be between 1 and 128, really on the form 2^n */
\r
133 EFM_ASSERT((burtcInit->clkDiv >= 1) && (burtcInit->clkDiv <= 128));
\r
134 /* Ignored compare bits during low power operation must be less than 7 */
\r
135 /* Note! Giant Gecko revision C errata, do NOT use LPCOMP=7 */
\r
136 EFM_ASSERT(burtcInit->lowPowerComp <= 6);
\r
137 /* You cannot enable the BURTC if mode is set to disabled */
\r
138 EFM_ASSERT((burtcInit->enable == false) ||
\r
139 ((burtcInit->enable == true)
\r
140 && (burtcInit->mode != burtcModeDisable)));
\r
141 /* Low power mode is only available with LFRCO or LFXO as clock source */
\r
142 EFM_ASSERT((burtcInit->clkSel != burtcClkSelULFRCO)
\r
143 || ((burtcInit->clkSel == burtcClkSelULFRCO)
\r
144 && (burtcInit->lowPowerMode == burtcLPDisable)));
\r
146 /* Calculate prescaler value from clock divider input */
\r
147 /* Note! If clock select (clkSel) is ULFRCO, a clock divisor (clkDiv) of
\r
148 value 1 will select a 2kHz ULFRCO clock, while any other value will
\r
149 select a 1kHz ULFRCO clock source. */
\r
150 presc = divToLog2(burtcInit->clkDiv);
\r
152 /* Make sure all registers are updated simultaneously */
\r
153 if (burtcInit->enable)
\r
155 BURTC_FreezeEnable(true);
\r
158 /* Modification of LPMODE register requires sync with potential ongoing
\r
159 * register updates in LF domain. */
\r
160 regSync(BURTC_SYNCBUSY_LPMODE);
\r
162 /* Configure low power mode */
\r
163 BURTC->LPMODE = (uint32_t) (burtcInit->lowPowerMode);
\r
165 /* New configuration */
\r
166 ctrl = (BURTC_CTRL_RSTEN
\r
167 | (burtcInit->mode)
\r
168 | (burtcInit->debugRun << _BURTC_CTRL_DEBUGRUN_SHIFT)
\r
169 | (burtcInit->compare0Top << _BURTC_CTRL_COMP0TOP_SHIFT)
\r
170 | (burtcInit->lowPowerComp << _BURTC_CTRL_LPCOMP_SHIFT)
\r
171 | (presc << _BURTC_CTRL_PRESC_SHIFT)
\r
172 | (burtcInit->clkSel)
\r
173 | (burtcInit->timeStamp << _BURTC_CTRL_BUMODETSEN_SHIFT));
\r
175 /* Clear interrupts */
\r
176 BURTC_IntClear(0xFFFFFFFF);
\r
178 /* Set new configuration */
\r
179 BURTC->CTRL = ctrl;
\r
181 /* Enable BURTC and counter */
\r
182 if (burtcInit->enable)
\r
184 /* To enable BURTC counter, we need to disable reset */
\r
185 BURTC_Enable(true);
\r
188 BURTC_FreezeEnable(false);
\r
193 /***************************************************************************//**
\r
194 * @brief Set BURTC compare channel
\r
196 * @param[in] comp Compare channel index, must be 0 for Giant / Leopard Gecko
\r
198 * @param[in] value New compare value
\r
199 ******************************************************************************/
\r
200 void BURTC_CompareSet(unsigned int comp, uint32_t value)
\r
202 (void) comp; /* Unused parameter when EFM_ASSERT is undefined. */
\r
204 EFM_ASSERT(comp == 0);
\r
206 /* Modification of COMP0 register requires sync with potential ongoing
\r
207 * register updates in LF domain. */
\r
208 regSync(BURTC_SYNCBUSY_COMP0);
\r
210 /* Configure compare channel 0 */
\r
211 BURTC->COMP0 = value;
\r
215 /***************************************************************************//**
\r
216 * @brief Get BURTC compare value
\r
218 * @param[in] comp Compare channel index value, must be 0 for Giant/Leopard.
\r
220 * @return Currently configured value for this compare channel
\r
221 ******************************************************************************/
\r
222 uint32_t BURTC_CompareGet(unsigned int comp)
\r
224 (void) comp; /* Unused parameter when EFM_ASSERT is undefined. */
\r
226 EFM_ASSERT(comp == 0);
\r
228 return BURTC->COMP0;
\r
232 /***************************************************************************//**
\r
233 * @brief Reset counter
\r
234 ******************************************************************************/
\r
235 void BURTC_CounterReset(void)
\r
237 /* Set and clear reset bit */
\r
238 BUS_RegBitWrite(&BURTC->CTRL, _BURTC_CTRL_RSTEN_SHIFT, 1);
\r
239 BUS_RegBitWrite(&BURTC->CTRL, _BURTC_CTRL_RSTEN_SHIFT, 0);
\r
243 /***************************************************************************//**
\r
245 * Restore BURTC to reset state
\r
247 * Before accessing the BURTC, BURSTEN in RMU->CTRL must be cleared.
\r
248 * LOCK will not be reset to default value, as this will disable access
\r
249 * to core BURTC registers.
\r
250 ******************************************************************************/
\r
251 void BURTC_Reset(void)
\r
255 /* Read reset state, set reset and restore state */
\r
256 buResetState = BUS_RegBitRead(&RMU->CTRL, _RMU_CTRL_BURSTEN_SHIFT);
\r
257 BUS_RegBitWrite(&RMU->CTRL, _RMU_CTRL_BURSTEN_SHIFT, 1);
\r
258 BUS_RegBitWrite(&RMU->CTRL, _RMU_CTRL_BURSTEN_SHIFT, buResetState);
\r
262 /***************************************************************************//**
\r
264 * Get clock frequency of the BURTC.
\r
267 * The current frequency in Hz.
\r
268 ******************************************************************************/
\r
269 uint32_t BURTC_ClockFreqGet(void)
\r
273 uint32_t frequency;
\r
275 clkSel = BURTC->CTRL & _BURTC_CTRL_CLKSEL_MASK;
\r
276 clkDiv = (BURTC->CTRL & _BURTC_CTRL_PRESC_MASK) >> _BURTC_CTRL_PRESC_SHIFT;
\r
280 /** Ultra low frequency (1 kHz) clock */
\r
281 case BURTC_CTRL_CLKSEL_ULFRCO:
\r
282 if (_BURTC_CTRL_PRESC_DIV1 == clkDiv)
\r
284 frequency = 2000; /* 2KHz when clock divisor is 1. */
\r
288 frequency = SystemULFRCOClockGet(); /* 1KHz when divisor is different
\r
293 /** Low frequency RC oscillator */
\r
294 case BURTC_CTRL_CLKSEL_LFRCO:
\r
295 frequency = SystemLFRCOClockGet() / (1 << clkDiv); /* freq=32768/2^clkDiv */
\r
298 /** Low frequency crystal osciallator */
\r
299 case BURTC_CTRL_CLKSEL_LFXO:
\r
300 frequency = SystemLFXOClockGet() / (1 << clkDiv); /* freq=32768/2^clkDiv */
\r
304 /* No clock selected for BURTC. */
\r
311 /** @} (end addtogroup BURTC) */
\r
312 /** @} (end addtogroup EM_Library) */
\r
314 #endif /* BURTC_PRESENT */
\r