1
0

em_letimer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Low Energy Timer (LETIMER) 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_LETIMER_H
  34. #define __EM_LETIMER_H
  35. #include <stdbool.h>
  36. #include "em_part.h"
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /***************************************************************************//**
  41. * @addtogroup EM_Library
  42. * @{
  43. ******************************************************************************/
  44. /***************************************************************************//**
  45. * @addtogroup LETIMER
  46. * @{
  47. ******************************************************************************/
  48. /*******************************************************************************
  49. ******************************** ENUMS ************************************
  50. ******************************************************************************/
  51. /** Repeat mode. */
  52. typedef enum
  53. {
  54. /** Count until stopped by SW. */
  55. letimerRepeatFree = _LETIMER_CTRL_REPMODE_FREE,
  56. /** Count REP0 times. */
  57. letimerRepeatOneshot = _LETIMER_CTRL_REPMODE_ONESHOT,
  58. /**
  59. * Count REP0 times, if REP1 has been written to, it is loaded into
  60. * REP0 when REP0 is about to be decremented to 0.
  61. */
  62. letimerRepeatBuffered = _LETIMER_CTRL_REPMODE_BUFFERED,
  63. /**
  64. * Run as long as both REP0 and REP1 are not 0. Both REP0 and REP1
  65. * are decremented when counter underflows.
  66. */
  67. letimerRepeatDouble = _LETIMER_CTRL_REPMODE_DOUBLE
  68. } LETIMER_RepeatMode_TypeDef;
  69. /** Underflow action on output. */
  70. typedef enum
  71. {
  72. /** No output action. */
  73. letimerUFOANone = _LETIMER_CTRL_UFOA0_NONE,
  74. /** Toggle output when counter underflows. */
  75. letimerUFOAToggle = _LETIMER_CTRL_UFOA0_TOGGLE,
  76. /** Hold output one LETIMER clock cycle when counter underflows. */
  77. letimerUFOAPulse = _LETIMER_CTRL_UFOA0_PULSE,
  78. /** Set output idle when counter underflows, and active when matching COMP1. */
  79. letimerUFOAPwm = _LETIMER_CTRL_UFOA0_PWM
  80. } LETIMER_UFOA_TypeDef;
  81. /*******************************************************************************
  82. ******************************* STRUCTS ***********************************
  83. ******************************************************************************/
  84. /** LETIMER initialization structure. */
  85. typedef struct
  86. {
  87. bool enable; /**< Start counting when init completed. */
  88. bool debugRun; /**< Counter shall keep running during debug halt. */
  89. bool rtcComp0Enable; /**< Start counting on RTC COMP0 match. */
  90. bool rtcComp1Enable; /**< Start counting on RTC COMP1 match. */
  91. bool comp0Top; /**< Load COMP0 register into CNT when counter underflows. */
  92. bool bufTop; /**< Load COMP1 into COMP0 when REP0 reaches 0. */
  93. uint8_t out0Pol; /**< Idle value for output 0. */
  94. uint8_t out1Pol; /**< Idle value for output 1. */
  95. LETIMER_UFOA_TypeDef ufoa0; /**< Underflow output 0 action. */
  96. LETIMER_UFOA_TypeDef ufoa1; /**< Underflow output 1 action. */
  97. LETIMER_RepeatMode_TypeDef repMode; /**< Repeat mode. */
  98. } LETIMER_Init_TypeDef;
  99. /** Default config for LETIMER init structure. */
  100. #define LETIMER_INIT_DEFAULT \
  101. { true, /* Enable timer when init complete. */ \
  102. false, /* Stop counter during debug halt. */ \
  103. false, /* Do not start counting on RTC COMP0 match. */ \
  104. false, /* Do not start counting on RTC COMP1 match. */ \
  105. false, /* Do not load COMP0 into CNT on underflow. */ \
  106. false, /* Do not load COMP1 into COMP0 when REP0 reaches 0. */ \
  107. 0, /* Idle value 0 for output 0. */ \
  108. 0, /* Idle value 0 for output 1. */ \
  109. letimerUFOANone, /* No action on underflow on output 0. */ \
  110. letimerUFOANone, /* No action on underflow on output 1. */ \
  111. letimerRepeatFree /* Count until stopped by SW. */ \
  112. }
  113. /*******************************************************************************
  114. ***************************** PROTOTYPES **********************************
  115. ******************************************************************************/
  116. uint32_t LETIMER_CompareGet(LETIMER_TypeDef *letimer, unsigned int comp);
  117. void LETIMER_CompareSet(LETIMER_TypeDef *letimer,
  118. unsigned int comp,
  119. uint32_t value);
  120. /***************************************************************************//**
  121. * @brief
  122. * Get LETIMER counter value.
  123. *
  124. * @param[in] letimer
  125. * Pointer to LETIMER peripheral register block.
  126. *
  127. * @return
  128. * Current LETIMER counter value.
  129. ******************************************************************************/
  130. __STATIC_INLINE uint32_t LETIMER_CounterGet(LETIMER_TypeDef *letimer)
  131. {
  132. return(letimer->CNT);
  133. }
  134. void LETIMER_Enable(LETIMER_TypeDef *letimer, bool enable);
  135. void LETIMER_FreezeEnable(LETIMER_TypeDef *letimer, bool enable);
  136. void LETIMER_Init(LETIMER_TypeDef *letimer, const LETIMER_Init_TypeDef *init);
  137. /***************************************************************************//**
  138. * @brief
  139. * Clear one or more pending LETIMER interrupts.
  140. *
  141. * @param[in] letimer
  142. * Pointer to LETIMER peripheral register block.
  143. *
  144. * @param[in] flags
  145. * Pending LETIMER interrupt source to clear. Use a bitwise logic OR
  146. * combination of valid interrupt flags for the LETIMER module
  147. * (LETIMER_IF_nnn).
  148. ******************************************************************************/
  149. __STATIC_INLINE void LETIMER_IntClear(LETIMER_TypeDef *letimer, uint32_t flags)
  150. {
  151. letimer->IFC = flags;
  152. }
  153. /***************************************************************************//**
  154. * @brief
  155. * Disable one or more LETIMER interrupts.
  156. *
  157. * @param[in] letimer
  158. * Pointer to LETIMER peripheral register block.
  159. *
  160. * @param[in] flags
  161. * LETIMER interrupt sources to disable. Use a bitwise logic OR combination of
  162. * valid interrupt flags for the LETIMER module (LETIMER_IF_nnn).
  163. ******************************************************************************/
  164. __STATIC_INLINE void LETIMER_IntDisable(LETIMER_TypeDef *letimer, uint32_t flags)
  165. {
  166. letimer->IEN &= ~(flags);
  167. }
  168. /***************************************************************************//**
  169. * @brief
  170. * Enable one or more LETIMER interrupts.
  171. *
  172. * @note
  173. * Depending on the use, a pending interrupt may already be set prior to
  174. * enabling the interrupt. Consider using LETIMER_IntClear() prior to enabling
  175. * if such a pending interrupt should be ignored.
  176. *
  177. * @param[in] letimer
  178. * Pointer to LETIMER peripheral register block.
  179. *
  180. * @param[in] flags
  181. * LETIMER interrupt sources to enable. Use a bitwise logic OR combination of
  182. * valid interrupt flags for the LETIMER module (LETIMER_IF_nnn).
  183. ******************************************************************************/
  184. __STATIC_INLINE void LETIMER_IntEnable(LETIMER_TypeDef *letimer, uint32_t flags)
  185. {
  186. letimer->IEN |= flags;
  187. }
  188. /***************************************************************************//**
  189. * @brief
  190. * Get pending LETIMER interrupt flags.
  191. *
  192. * @note
  193. * The event bits are not cleared by the use of this function.
  194. *
  195. * @param[in] letimer
  196. * Pointer to LETIMER peripheral register block.
  197. *
  198. * @return
  199. * LETIMER interrupt sources pending. A bitwise logic OR combination of
  200. * valid interrupt flags for the LETIMER module (LETIMER_IF_nnn).
  201. ******************************************************************************/
  202. __STATIC_INLINE uint32_t LETIMER_IntGet(LETIMER_TypeDef *letimer)
  203. {
  204. return(letimer->IF);
  205. }
  206. /***************************************************************************//**
  207. * @brief
  208. * Set one or more pending LETIMER interrupts from SW.
  209. *
  210. * @param[in] letimer
  211. * Pointer to LETIMER peripheral register block.
  212. *
  213. * @param[in] flags
  214. * LETIMER interrupt sources to set to pending. Use a bitwise logic OR
  215. * combination of valid interrupt flags for the LETIMER module (LETIMER_IF_nnn).
  216. ******************************************************************************/
  217. __STATIC_INLINE void LETIMER_IntSet(LETIMER_TypeDef *letimer, uint32_t flags)
  218. {
  219. letimer->IFS = flags;
  220. }
  221. uint32_t LETIMER_RepeatGet(LETIMER_TypeDef *letimer, unsigned int rep);
  222. void LETIMER_RepeatSet(LETIMER_TypeDef *letimer,
  223. unsigned int rep,
  224. uint32_t value);
  225. void LETIMER_Reset(LETIMER_TypeDef *letimer);
  226. /** @} (end addtogroup LETIMER) */
  227. /** @} (end addtogroup EM_Library) */
  228. #ifdef __cplusplus
  229. }
  230. #endif
  231. #endif /* __EM_LETIMER_H */