em_burtc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Backup Real Time Counter (BURTC) peripheral API
  4. * @author Energy Micro AS
  5. * @version 3.0.0
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software.
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. * 3. This notice may not be removed or altered from any source distribution.
  20. *
  21. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  22. * obligation to support this Software. Energy Micro AS is providing the
  23. * Software "AS IS", with no express or implied warranties of any kind,
  24. * including, but not limited to, any implied warranties of merchantability
  25. * or fitness for any particular purpose or warranties against infringement
  26. * of any proprietary rights of a third party.
  27. *
  28. * Energy Micro AS will not be liable for any consequential, incidental, or
  29. * special damages, or any other relief, or for any claim by any third party,
  30. * arising from your use of this Software.
  31. *
  32. ******************************************************************************/
  33. #ifndef __EM_BURTC_H
  34. #define __EM_BURTC_H
  35. #include <stdbool.h>
  36. #include "em_part.h"
  37. #if defined(BURTC_PRESENT)
  38. #include "em_assert.h"
  39. #include "em_bitband.h"
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /***************************************************************************//**
  44. * @addtogroup EM_Library
  45. * @{
  46. ******************************************************************************/
  47. /***************************************************************************//**
  48. * @addtogroup BURTC
  49. * @{
  50. ******************************************************************************/
  51. /*******************************************************************************
  52. ******************************* DEFINES ***********************************
  53. ******************************************************************************/
  54. #define burtcClkDiv_1 1
  55. #define burtcClkDiv_2 2
  56. #define burtcClkDiv_4 4
  57. #define burtcClkDiv_8 8
  58. #define burtcClkDiv_16 16
  59. #define burtcClkDiv_32 32
  60. #define burtcClkDiv_64 64
  61. #define burtcClkDiv_128 128
  62. /*******************************************************************************
  63. ******************************** ENUMS ************************************
  64. ******************************************************************************/
  65. /** BURTC clock selection */
  66. typedef enum
  67. {
  68. /** Ultra low frequency (1 kHz) clock */
  69. burtcClkSelULFRCO = BURTC_CTRL_CLKSEL_ULFRCO,
  70. /** Low frequency RC oscillator */
  71. burtcClkSelLFRCO = BURTC_CTRL_CLKSEL_LFRCO,
  72. /** Low frequency crystal osciallator */
  73. burtcClkSelLFXO = BURTC_CTRL_CLKSEL_LFXO
  74. } BURTC_ClkSel_TypeDef;
  75. /** BURTC mode of operation */
  76. typedef enum
  77. {
  78. /** Disable BURTC */
  79. burtcModeDisable = BURTC_CTRL_MODE_DISABLE,
  80. /** Enable and start BURTC counter in EM0 to EM2 */
  81. burtcModeEM2 = BURTC_CTRL_MODE_EM2EN,
  82. /** Enable and start BURTC counter in EM0 to EM3 */
  83. burtcModeEM3 = BURTC_CTRL_MODE_EM3EN,
  84. /** Enable and start BURTC counter in EM0 to EM4 */
  85. burtcModeEM4 = BURTC_CTRL_MODE_EM4EN,
  86. } BURTC_Mode_TypeDef;
  87. /** BURTC low power mode */
  88. typedef enum
  89. {
  90. /** Low Power Mode is disabled */
  91. burtcLPDisable = BURTC_LPMODE_LPMODE_DISABLE,
  92. /** Low Power Mode is always enabled */
  93. burtcLPEnable = BURTC_LPMODE_LPMODE_ENABLE,
  94. /** Low Power Mode when system enters backup mode */
  95. burtcLPBU = BURTC_LPMODE_LPMODE_BUEN
  96. } BURTC_LP_TypeDef;
  97. /*******************************************************************************
  98. ******************************* STRUCTS ***********************************
  99. ******************************************************************************/
  100. /** BURTC initialization structure. */
  101. typedef struct
  102. {
  103. bool enable; /** Enable BURTC after initialization (starts counter) */
  104. BURTC_Mode_TypeDef mode; /**< Configure energy mode operation */
  105. bool debugRun; /**< If true, counter will keep running under debug halt */
  106. BURTC_ClkSel_TypeDef clkSel; /**< Select clock source */
  107. uint32_t clkDiv; /**< Clock divider; for ULFRCO 1Khz or 2kHz operation */
  108. uint32_t lowPowerComp; /**< Number of least significantt clock bits to ignore in low power mode */
  109. bool timeStamp; /**< Enable time stamp on entering backup power domain */
  110. bool compare0Top; /**< Set if Compare Value 0 is also top value (counter restart) */
  111. BURTC_LP_TypeDef lowPowerMode; /**< Low power operation mode, requires LFXO or LFRCO */
  112. } BURTC_Init_TypeDef;
  113. /** Default configuration for BURTC init structure */
  114. #define BURTC_INIT_DEFAULT \
  115. { true, \
  116. burtcModeEM2, \
  117. false, \
  118. burtcClkSelULFRCO, \
  119. burtcClkDiv_1, \
  120. 0, \
  121. true, \
  122. false, \
  123. burtcLPDisable, \
  124. }
  125. /*******************************************************************************
  126. ***************************** PROTOTYPES **********************************
  127. ******************************************************************************/
  128. /***************************************************************************//**
  129. * @brief
  130. * Clear one or more pending BURTC interrupts.
  131. *
  132. * @param[in] flags
  133. * BURTC interrupt sources to clear. Use a set of interrupt flags OR-ed
  134. * together to clear multiple interrupt sources for the BURTC module
  135. * (BURTC_IFS_nnn).
  136. ******************************************************************************/
  137. __STATIC_INLINE void BURTC_IntClear(uint32_t flags)
  138. {
  139. BURTC->IFC = flags;
  140. }
  141. /***************************************************************************//**
  142. * @brief
  143. * Disable one or more BURTC interrupts.
  144. *
  145. * @param[in] flags
  146. * BURTC interrupt sources to disable. Use a set of interrupt flags OR-ed
  147. * together to disable multiple interrupt sources for the BURTC module
  148. * (BURTC_IFS_nnn).
  149. ******************************************************************************/
  150. __STATIC_INLINE void BURTC_IntDisable(uint32_t flags)
  151. {
  152. BURTC->IEN &= ~(flags);
  153. }
  154. /***************************************************************************//**
  155. * @brief
  156. * Enable one or more BURTC interrupts.
  157. *
  158. * @note
  159. * Depending on the use, a pending interrupt may already be set prior to
  160. * enabling the interrupt. Consider using BURTC_IntClear() prior to enabling
  161. * if such a pending interrupt should be ignored.
  162. *
  163. * @param[in] flags
  164. * BURTC interrupt sources to enable. Use a set of interrupt flags OR-ed
  165. * together to set multiple interrupt sources for the BURTC module
  166. * (BURTC_IFS_nnn).
  167. ******************************************************************************/
  168. __STATIC_INLINE void BURTC_IntEnable(uint32_t flags)
  169. {
  170. BURTC->IEN |= flags;
  171. }
  172. /***************************************************************************//**
  173. * @brief
  174. * Get pending BURTC interrupt flags.
  175. *
  176. * @note
  177. * The event bits are not cleared by the use of this function.
  178. *
  179. * @return
  180. * Pending BURTC interrupt sources. Returns a set of interrupt flags OR-ed
  181. * together for multiple interrupt sources in the BURTC module (BURTC_IFS_nnn).
  182. ******************************************************************************/
  183. __STATIC_INLINE uint32_t BURTC_IntGet(void)
  184. {
  185. return(BURTC->IF);
  186. }
  187. /***************************************************************************//**
  188. * @brief
  189. * Get enabled and pending BURTC interrupt flags.
  190. *
  191. * @note
  192. * The event bits are not cleared by the use of this function.
  193. *
  194. * @return
  195. * Pending BURTC interrupt sources that is also enabled. Returns a set of
  196. * interrupt flags OR-ed together for multiple interrupt sources in the
  197. * BURTC module (BURTC_IFS_nnn).
  198. ******************************************************************************/
  199. __STATIC_INLINE uint32_t BURTC_IntGetEnabled(void)
  200. {
  201. uint32_t tmp;
  202. /* Get enabled interrupts */
  203. tmp = BURTC->IEN;
  204. /* Return set intterupts */
  205. return BURTC->IF & tmp;
  206. }
  207. /***************************************************************************//**
  208. * @brief
  209. * Set one or more pending BURTC interrupts from SW.
  210. *
  211. * @param[in] flags
  212. * BURTC interrupt sources to set to pending. Use a set of interrupt flags
  213. * OR-ed together to set multiple interrupt sources for the BURTC module
  214. * (BURTC_IFS_nnn).
  215. ******************************************************************************/
  216. __STATIC_INLINE void BURTC_IntSet(uint32_t flags)
  217. {
  218. BURTC->IFS = flags;
  219. }
  220. /***************************************************************************//**
  221. * @brief
  222. * Status of BURTC RAM, timestamp and LP Mode
  223. *
  224. * @return A mask logially OR-ed status bits
  225. ******************************************************************************/
  226. __STATIC_INLINE uint32_t BURTC_Status(void)
  227. {
  228. return BURTC->STATUS;
  229. }
  230. /***************************************************************************//**
  231. * @brief
  232. * Clear and reset BURTC status register
  233. ******************************************************************************/
  234. __STATIC_INLINE void BURTC_StatusClear(void)
  235. {
  236. BURTC->CMD = BURTC_CMD_CLRSTATUS;
  237. }
  238. /***************************************************************************//**
  239. * @brief
  240. * Enable or Disable BURTC peripheral reset and start counter
  241. * @param[in] enable
  242. * If true; asserts reset to BURTC, halts counter, if false; deassert reset
  243. ******************************************************************************/
  244. __STATIC_INLINE void BURTC_Enable(bool enable)
  245. {
  246. /* Note! If mode is disabled, BURTC counter will not start */
  247. EFM_ASSERT(((enable == true) && ((BURTC->CTRL & _BURTC_CTRL_MODE_MASK) != BURTC_CTRL_MODE_DISABLE))
  248. || (enable == false));
  249. if( enable )
  250. {
  251. BITBAND_Peripheral(&BURTC->CTRL, _BURTC_CTRL_RSTEN_SHIFT, 0);
  252. }
  253. else
  254. {
  255. BITBAND_Peripheral(&BURTC->CTRL, _BURTC_CTRL_RSTEN_SHIFT, 1);
  256. }
  257. }
  258. /***************************************************************************//**
  259. * @brief Get BURTC counter
  260. *
  261. * @return
  262. * BURTC counter value
  263. ******************************************************************************/
  264. __STATIC_INLINE uint32_t BURTC_CounterGet(void)
  265. {
  266. return BURTC->CNT;
  267. }
  268. /***************************************************************************//**
  269. * @brief Get BURTC timestamp for entering BU
  270. *
  271. * @return
  272. * BURTC Time Stamp value
  273. ******************************************************************************/
  274. __STATIC_INLINE uint32_t BURTC_TimestampGet(void)
  275. {
  276. return BURTC->TIMESTAMP;
  277. }
  278. /***************************************************************************//**
  279. * @brief Freeze register updates until enabled
  280. * @param[in] enable If true, registers are not updated until enabled again.
  281. ******************************************************************************/
  282. __STATIC_INLINE void BURTC_FreezeEnable(bool enable)
  283. {
  284. BITBAND_Peripheral(&BURTC->FREEZE, _BURTC_FREEZE_REGFREEZE_SHIFT, enable);
  285. }
  286. /***************************************************************************//**
  287. * @brief Shut down power to rentention register bank.
  288. * @param[in] enable
  289. * If true, shuts off power to retention registers.
  290. * @note
  291. * When power rentention is disabled, it cannot be enabled again (until
  292. * reset).
  293. ******************************************************************************/
  294. __STATIC_INLINE void BURTC_Powerdown(bool enable)
  295. {
  296. BITBAND_Peripheral(&BURTC->POWERDOWN, _BURTC_POWERDOWN_RAM_SHIFT, enable);
  297. }
  298. /***************************************************************************//**
  299. * @brief
  300. * Set a value in one of the retention registers
  301. *
  302. * @param[in] num
  303. * Register to set
  304. * @param[in] data
  305. * Value to put into register
  306. ******************************************************************************/
  307. __STATIC_INLINE void BURTC_RetRegSet(uint32_t num, uint32_t data)
  308. {
  309. EFM_ASSERT(num <= 127);
  310. BURTC->RET[num].REG = data;
  311. }
  312. /***************************************************************************//**
  313. * @brief
  314. * Read a value from one of the retention registers
  315. *
  316. * @param[in] num
  317. * Retention Register to read
  318. ******************************************************************************/
  319. __STATIC_INLINE uint32_t BURTC_RetRegGet(uint32_t num)
  320. {
  321. EFM_ASSERT(num <= 127);
  322. return BURTC->RET[num].REG;
  323. }
  324. /***************************************************************************//**
  325. * @brief
  326. * Lock BURTC registers, will protect from writing new config settings
  327. ******************************************************************************/
  328. __STATIC_INLINE void BURTC_Lock(void)
  329. {
  330. BURTC->LOCK = BURTC_LOCK_LOCKKEY_LOCK;
  331. }
  332. /***************************************************************************//**
  333. * @brief
  334. * Unlock BURTC registers, enable write access to change configuration
  335. ******************************************************************************/
  336. __STATIC_INLINE void BURTC_Unlock(void)
  337. {
  338. BURTC->LOCK = BURTC_LOCK_LOCKKEY_UNLOCK;
  339. }
  340. void BURTC_Reset(void);
  341. void BURTC_Init(const BURTC_Init_TypeDef *burtcInit);
  342. void BURTC_CounterReset(void);
  343. void BURTC_CompareSet(unsigned int comp, uint32_t value);
  344. uint32_t BURTC_CompareGet(unsigned int comp);
  345. /** @} (end addtogroup BURTC) */
  346. /** @} (end addtogroup EM_Library) */
  347. #ifdef __cplusplus
  348. }
  349. #endif
  350. #endif /* BURTC_PRESENT */
  351. #endif /* __EM_BURTC_H */