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
33 #ifndef __SILICON_LABS_EM_BURTC_H__
\r
34 #define __SILICON_LABS_EM_BURTC_H__
\r
36 #include "em_device.h"
\r
37 #if defined(BURTC_PRESENT)
\r
39 #include <stdbool.h>
\r
40 #include "em_assert.h"
\r
47 /***************************************************************************//**
\r
48 * @addtogroup EM_Library
\r
50 ******************************************************************************/
\r
52 /***************************************************************************//**
\r
55 ******************************************************************************/
\r
57 /*******************************************************************************
\r
58 ******************************* DEFINES ***********************************
\r
59 ******************************************************************************/
\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
71 /*******************************************************************************
\r
72 ******************************** ENUMS ************************************
\r
73 ******************************************************************************/
\r
75 /** BURTC clock selection */
\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
87 /** BURTC mode of operation */
\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
100 /** BURTC low power mode */
\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
111 /*******************************************************************************
\r
112 ******************************* STRUCTS ***********************************
\r
113 ******************************************************************************/
\r
115 /** BURTC initialization structure. */
\r
118 bool enable; /**< Enable BURTC after initialization (starts counter) */
\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
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
128 bool compare0Top; /**< Set if Compare Value 0 is also top value (counter restart) */
\r
130 BURTC_LP_TypeDef lowPowerMode; /**< Low power operation mode, requires LFXO or LFRCO */
\r
131 } BURTC_Init_TypeDef;
\r
133 /** Default configuration for BURTC init structure */
\r
134 #define BURTC_INIT_DEFAULT \
\r
139 burtcClkSelULFRCO, \
\r
147 /*******************************************************************************
\r
148 ***************************** PROTOTYPES **********************************
\r
149 ******************************************************************************/
\r
151 /***************************************************************************//**
\r
153 * Clear one or more pending BURTC interrupts.
\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
159 ******************************************************************************/
\r
160 __STATIC_INLINE void BURTC_IntClear(uint32_t flags)
\r
162 BURTC->IFC = flags;
\r
166 /***************************************************************************//**
\r
168 * Disable one or more BURTC interrupts.
\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
174 ******************************************************************************/
\r
175 __STATIC_INLINE void BURTC_IntDisable(uint32_t flags)
\r
177 BURTC->IEN &= ~(flags);
\r
181 /***************************************************************************//**
\r
183 * Enable one or more BURTC interrupts.
\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
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
194 ******************************************************************************/
\r
195 __STATIC_INLINE void BURTC_IntEnable(uint32_t flags)
\r
197 BURTC->IEN |= flags;
\r
201 /***************************************************************************//**
\r
203 * Get pending BURTC interrupt flags.
\r
206 * The event bits are not cleared by the use of this function.
\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
218 /***************************************************************************//**
\r
220 * Get enabled and pending BURTC interrupt flags.
\r
223 * The event bits are not cleared by the use of this function.
\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
234 /* Get enabled interrupts */
\r
237 /* Return set intterupts */
\r
238 return BURTC->IF & tmp;
\r
242 /***************************************************************************//**
\r
244 * Set one or more pending BURTC interrupts from SW.
\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
250 ******************************************************************************/
\r
251 __STATIC_INLINE void BURTC_IntSet(uint32_t flags)
\r
253 BURTC->IFS = flags;
\r
257 /***************************************************************************//**
\r
259 * Status of BURTC RAM, timestamp and LP Mode
\r
261 * @return A mask logially OR-ed status bits
\r
262 ******************************************************************************/
\r
263 __STATIC_INLINE uint32_t BURTC_Status(void)
\r
265 return BURTC->STATUS;
\r
269 /***************************************************************************//**
\r
271 * Clear and reset BURTC status register
\r
272 ******************************************************************************/
\r
273 __STATIC_INLINE void BURTC_StatusClear(void)
\r
275 BURTC->CMD = BURTC_CMD_CLRSTATUS;
\r
279 /***************************************************************************//**
\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
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
294 BUS_RegBitWrite(&BURTC->CTRL, _BURTC_CTRL_RSTEN_SHIFT, 0);
\r
298 BUS_RegBitWrite(&BURTC->CTRL, _BURTC_CTRL_RSTEN_SHIFT, 1);
\r
303 /***************************************************************************//**
\r
304 * @brief Get BURTC counter
\r
307 * BURTC counter value
\r
308 ******************************************************************************/
\r
309 __STATIC_INLINE uint32_t BURTC_CounterGet(void)
\r
315 /***************************************************************************//**
\r
316 * @brief Get BURTC timestamp for entering BU
\r
319 * BURTC Time Stamp value
\r
320 ******************************************************************************/
\r
321 __STATIC_INLINE uint32_t BURTC_TimestampGet(void)
\r
323 return BURTC->TIMESTAMP;
\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
333 BUS_RegBitWrite(&BURTC->FREEZE, _BURTC_FREEZE_REGFREEZE_SHIFT, enable);
\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
342 * When power rentention is disabled, it cannot be enabled again (until
\r
344 ******************************************************************************/
\r
345 __STATIC_INLINE void BURTC_Powerdown(bool enable)
\r
347 BUS_RegBitWrite(&BURTC->POWERDOWN, _BURTC_POWERDOWN_RAM_SHIFT, enable);
\r
351 /***************************************************************************//**
\r
353 * Set a value in one of the retention registers
\r
358 * Value to put into register
\r
359 ******************************************************************************/
\r
360 __STATIC_INLINE void BURTC_RetRegSet(uint32_t num, uint32_t data)
\r
362 EFM_ASSERT(num <= 127);
\r
364 BURTC->RET[num].REG = data;
\r
368 /***************************************************************************//**
\r
370 * Read a value from one of the retention registers
\r
373 * Retention Register to read
\r
374 ******************************************************************************/
\r
375 __STATIC_INLINE uint32_t BURTC_RetRegGet(uint32_t num)
\r
377 EFM_ASSERT(num <= 127);
\r
379 return BURTC->RET[num].REG;
\r
383 /***************************************************************************//**
\r
385 * Lock BURTC registers, will protect from writing new config settings
\r
386 ******************************************************************************/
\r
387 __STATIC_INLINE void BURTC_Lock(void)
\r
389 BURTC->LOCK = BURTC_LOCK_LOCKKEY_LOCK;
\r
393 /***************************************************************************//**
\r
395 * Unlock BURTC registers, enable write access to change configuration
\r
396 ******************************************************************************/
\r
397 __STATIC_INLINE void BURTC_Unlock(void)
\r
399 BURTC->LOCK = BURTC_LOCK_LOCKKEY_UNLOCK;
\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
411 /** @} (end addtogroup BURTC) */
\r
412 /** @} (end addtogroup EM_Library) */
\r
418 #endif /* BURTC_PRESENT */
\r
419 #endif /* __SILICON_LABS_EM_BURTC_H__ */
\r