1 /***************************************************************************//**
\r
3 * @brief Backup Real Time Counter (BURTC) peripheral API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2014 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 #ifndef __SILICON_LABS_EM_BURTC_H_
\r
35 #define __SILICON_LABS_EM_BURTC_H_
\r
37 #include <stdbool.h>
\r
38 #include "em_device.h"
\r
40 #if defined(BURTC_PRESENT)
\r
42 #include "em_assert.h"
\r
43 #include "em_bitband.h"
\r
49 /***************************************************************************//**
\r
50 * @addtogroup EM_Library
\r
52 ******************************************************************************/
\r
54 /***************************************************************************//**
\r
57 ******************************************************************************/
\r
59 /*******************************************************************************
\r
60 ******************************* DEFINES ***********************************
\r
61 ******************************************************************************/
\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
73 /*******************************************************************************
\r
74 ******************************** ENUMS ************************************
\r
75 ******************************************************************************/
\r
77 /** BURTC clock selection */
\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
89 /** BURTC mode of operation */
\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
102 /** BURTC low power mode */
\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
113 /*******************************************************************************
\r
114 ******************************* STRUCTS ***********************************
\r
115 ******************************************************************************/
\r
117 /** BURTC initialization structure. */
\r
120 bool enable; /**< Enable BURTC after initialization (starts counter) */
\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
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
130 bool compare0Top; /**< Set if Compare Value 0 is also top value (counter restart) */
\r
132 BURTC_LP_TypeDef lowPowerMode; /**< Low power operation mode, requires LFXO or LFRCO */
\r
133 } BURTC_Init_TypeDef;
\r
135 /** Default configuration for BURTC init structure */
\r
136 #define BURTC_INIT_DEFAULT \
\r
140 burtcClkSelULFRCO, \
\r
148 /*******************************************************************************
\r
149 ***************************** PROTOTYPES **********************************
\r
150 ******************************************************************************/
\r
152 /***************************************************************************//**
\r
154 * Clear one or more pending BURTC interrupts.
\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
160 ******************************************************************************/
\r
161 __STATIC_INLINE void BURTC_IntClear(uint32_t flags)
\r
163 BURTC->IFC = flags;
\r
167 /***************************************************************************//**
\r
169 * Disable one or more BURTC interrupts.
\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
175 ******************************************************************************/
\r
176 __STATIC_INLINE void BURTC_IntDisable(uint32_t flags)
\r
178 BURTC->IEN &= ~(flags);
\r
182 /***************************************************************************//**
\r
184 * Enable one or more BURTC interrupts.
\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
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
195 ******************************************************************************/
\r
196 __STATIC_INLINE void BURTC_IntEnable(uint32_t flags)
\r
198 BURTC->IEN |= flags;
\r
202 /***************************************************************************//**
\r
204 * Get pending BURTC interrupt flags.
\r
207 * The event bits are not cleared by the use of this function.
\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
219 /***************************************************************************//**
\r
221 * Get enabled and pending BURTC interrupt flags.
\r
224 * The event bits are not cleared by the use of this function.
\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
235 /* Get enabled interrupts */
\r
238 /* Return set intterupts */
\r
239 return BURTC->IF & tmp;
\r
243 /***************************************************************************//**
\r
245 * Set one or more pending BURTC interrupts from SW.
\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
251 ******************************************************************************/
\r
252 __STATIC_INLINE void BURTC_IntSet(uint32_t flags)
\r
254 BURTC->IFS = flags;
\r
258 /***************************************************************************//**
\r
260 * Status of BURTC RAM, timestamp and LP Mode
\r
262 * @return A mask logially OR-ed status bits
\r
263 ******************************************************************************/
\r
264 __STATIC_INLINE uint32_t BURTC_Status(void)
\r
266 return BURTC->STATUS;
\r
270 /***************************************************************************//**
\r
272 * Clear and reset BURTC status register
\r
273 ******************************************************************************/
\r
274 __STATIC_INLINE void BURTC_StatusClear(void)
\r
276 BURTC->CMD = BURTC_CMD_CLRSTATUS;
\r
280 /***************************************************************************//**
\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
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
293 BITBAND_Peripheral(&BURTC->CTRL, _BURTC_CTRL_RSTEN_SHIFT, 0);
\r
297 BITBAND_Peripheral(&BURTC->CTRL, _BURTC_CTRL_RSTEN_SHIFT, 1);
\r
302 /***************************************************************************//**
\r
303 * @brief Get BURTC counter
\r
306 * BURTC counter value
\r
307 ******************************************************************************/
\r
308 __STATIC_INLINE uint32_t BURTC_CounterGet(void)
\r
314 /***************************************************************************//**
\r
315 * @brief Get BURTC timestamp for entering BU
\r
318 * BURTC Time Stamp value
\r
319 ******************************************************************************/
\r
320 __STATIC_INLINE uint32_t BURTC_TimestampGet(void)
\r
322 return BURTC->TIMESTAMP;
\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
332 BITBAND_Peripheral(&BURTC->FREEZE, _BURTC_FREEZE_REGFREEZE_SHIFT, enable);
\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
341 * When power rentention is disabled, it cannot be enabled again (until
\r
343 ******************************************************************************/
\r
344 __STATIC_INLINE void BURTC_Powerdown(bool enable)
\r
346 BITBAND_Peripheral(&BURTC->POWERDOWN, _BURTC_POWERDOWN_RAM_SHIFT, enable);
\r
350 /***************************************************************************//**
\r
352 * Set a value in one of the retention registers
\r
357 * Value to put into register
\r
358 ******************************************************************************/
\r
359 __STATIC_INLINE void BURTC_RetRegSet(uint32_t num, uint32_t data)
\r
361 EFM_ASSERT(num <= 127);
\r
363 BURTC->RET[num].REG = data;
\r
367 /***************************************************************************//**
\r
369 * Read a value from one of the retention registers
\r
372 * Retention Register to read
\r
373 ******************************************************************************/
\r
374 __STATIC_INLINE uint32_t BURTC_RetRegGet(uint32_t num)
\r
376 EFM_ASSERT(num <= 127);
\r
378 return BURTC->RET[num].REG;
\r
382 /***************************************************************************//**
\r
384 * Lock BURTC registers, will protect from writing new config settings
\r
385 ******************************************************************************/
\r
386 __STATIC_INLINE void BURTC_Lock(void)
\r
388 BURTC->LOCK = BURTC_LOCK_LOCKKEY_LOCK;
\r
392 /***************************************************************************//**
\r
394 * Unlock BURTC registers, enable write access to change configuration
\r
395 ******************************************************************************/
\r
396 __STATIC_INLINE void BURTC_Unlock(void)
\r
398 BURTC->LOCK = BURTC_LOCK_LOCKKEY_UNLOCK;
\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
410 /** @} (end addtogroup BURTC) */
\r
411 /** @} (end addtogroup EM_Library) */
\r
417 #endif /* BURTC_PRESENT */
\r
418 #endif /* __SILICON_LABS_EM_BURTC_H_ */
\r