timer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**************************************************************************//**
  2. * @file timer.h
  3. * @version V3.00
  4. * $Revision: 6 $
  5. * $Date: 14/01/28 10:49a $
  6. * @brief M051 series Timer driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #ifndef __TIMER_H__
  12. #define __TIMER_H__
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. /** @addtogroup M051_Device_Driver M051 Device Driver
  18. @{
  19. */
  20. /** @addtogroup M051_TIMER_Driver TIMER Driver
  21. @{
  22. */
  23. /** @addtogroup M051_TIMER_EXPORTED_CONSTANTS TIMER Exported Constants
  24. @{
  25. */
  26. #define TIMER_ONESHOT_MODE (0UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in one-shot mode */
  27. #define TIMER_PERIODIC_MODE (1UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in periodic mode */
  28. #define TIMER_TOGGLE_MODE (2UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in toggle-output mode */
  29. #define TIMER_CONTINUOUS_MODE (3UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in continuous counting mode */
  30. #define TIMER_CONTINUOUS_MODE (3UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in continuous counting mode */
  31. #define TIMER_CAPTURE_FROM_TXEX_PIN (0UL << TIMER_TCSR_CAP_SRC_Pos) /*!< Timer capture source is from TxEX pin */
  32. #define TIMER_CAPTURE_FROM_INTERNAL_ACMP (1UL << TIMER_TCSR_CAP_SRC_Pos) /*!< Timer capture source is from internal ACMP output signal */
  33. #define TIMER_TOUT_PIN_FROM_TX_PIN (0UL << TIMER_TCSR_TOUT_SEL_Pos) /*!< Timer toggle-output pin is from Tx pin */
  34. #define TIMER_TOUT_PIN_FROM_TXEX_PIN (1UL << TIMER_TCSR_TOUT_SEL_Pos) /*!< Timer toggle-output pin is from TxEX pin */
  35. #define TIMER_CAPTURE_FREE_COUNTING_MODE (0UL << TIMER_TEXCON_RSTCAPSEL_Pos) /*!< Timer capture event to get timer counter value */
  36. #define TIMER_CAPTURE_COUNTER_RESET_MODE (1UL << TIMER_TEXCON_RSTCAPSEL_Pos) /*!< Timer capture event to reset timer counter */
  37. #define TIMER_CAPTURE_FALLING_EDGE (0UL << TIMER_TEXCON_TEX_EDGE_Pos) /*!< Falling edge trigger timer capture */
  38. #define TIMER_CAPTURE_RISING_EDGE (1UL << TIMER_TEXCON_TEX_EDGE_Pos) /*!< Rising edge trigger timer capture */
  39. #define TIMER_CAPTURE_FALLING_AND_RISING_EDGE (2UL << TIMER_TEXCON_TEX_EDGE_Pos) /*!< Both falling and rising edge trigger timer capture */
  40. #define TIMER_COUNTER_FALLING_EDGE (0UL << TIMER_TEXCON_TX_PHASE_Pos) /*!< Counter increase on falling edge */
  41. #define TIMER_COUNTER_RISING_EDGE (1UL << TIMER_TEXCON_TX_PHASE_Pos) /*!< Counter increase on rising edge */
  42. /*@}*/ /* end of group M051_TIMER_EXPORTED_CONSTANTS */
  43. /** @addtogroup M051_TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions
  44. @{
  45. */
  46. /**
  47. * @brief Set Timer Compare Value
  48. *
  49. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  50. * @param[in] u32Value Timer compare value. Valid values are between 2 to 0xFFFFFF.
  51. *
  52. * @return None
  53. *
  54. * @details This macro is used to set new Timer compared value.
  55. */
  56. #define TIMER_SET_CMP_VALUE(timer, u32Value) ((timer)->TCMPR = (u32Value))
  57. /**
  58. * @brief Set Timer Prescale Value
  59. *
  60. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  61. * @param[in] u32Value Timer prescale value. Valid values are between 0 to 0xFF.
  62. *
  63. * @return None
  64. *
  65. * @details This macro is used to set new Timer prescale value.
  66. * @note Clock input is divided by (prescale + 1) before it is fed into timer.
  67. */
  68. #define TIMER_SET_PRESCALE_VALUE(timer, u32Value) ((timer)->TCSR = ((timer)->TCSR & ~TIMER_TCSR_PRESCALE_Msk) | (u32Value))
  69. /**
  70. * @brief Check specify Timer Status
  71. *
  72. * @return Timer counter is activate or inactivate
  73. * @retval 0 Timer 24-bit up counter is inactive
  74. * @retval 1 Timer 24-bit up counter is active
  75. *
  76. * @details This macro is used to check if specify Timer is inactive or active.
  77. */
  78. #define TIMER_IS_ACTIVE(timer) ((timer)->TCSR & TIMER_TCSR_CACT_Msk ? 1 : 0)
  79. /**
  80. * @brief Select Toggle-output Pin
  81. *
  82. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  83. * @param[in] u32ToutSel Toggle-output pin selection, valid values are:
  84. * - \ref TIMER_TOUT_PIN_FROM_TX_PIN
  85. * - \ref TIMER_TOUT_PIN_FROM_TXEX_PIN
  86. *
  87. * @return None
  88. *
  89. * @details This macro is used to select Timer toggle-output pin.
  90. */
  91. #define TIMER_SELECT_TOUT_PIN(timer, u32ToutSel) ((timer)->TCSR = ((timer)->TCSR & ~TIMER_TCSR_TOUT_SEL_Msk) | (u32ToutSel))
  92. /**
  93. * @details This function is used to start Timer counting.
  94. */
  95. static __INLINE void TIMER_Start(TIMER_T *timer)
  96. {
  97. timer->TCSR |= TIMER_TCSR_CEN_Msk;
  98. }
  99. /**
  100. * @details This function is used to stop Timer counting.
  101. */
  102. static __INLINE void TIMER_Stop(TIMER_T *timer)
  103. {
  104. timer->TCSR &= ~TIMER_TCSR_CEN_Msk;
  105. }
  106. /**
  107. * @details This function is used to enable the Timer wake-up function.
  108. * @note To wake the system from Power-down mode, timer clock source must be ether LXT or LIRC.
  109. */
  110. static __INLINE void TIMER_EnableWakeup(TIMER_T *timer)
  111. {
  112. timer->TCSR |= TIMER_TCSR_WAKE_EN_Msk;
  113. }
  114. /**
  115. * @details This function is used to disable the Timer wake-up function.
  116. */
  117. static __INLINE void TIMER_DisableWakeup(TIMER_T *timer)
  118. {
  119. timer->TCSR &= ~TIMER_TCSR_WAKE_EN_Msk;
  120. }
  121. /**
  122. * @details This function is used to enable the capture pin detection de-bounce function.
  123. */
  124. static __INLINE void TIMER_EnableCaptureDebounce(TIMER_T *timer)
  125. {
  126. timer->TEXCON |= TIMER_TEXCON_TEXDB_Msk;
  127. }
  128. /**
  129. * @details This function is used to disable the capture pin detection de-bounce function.
  130. */
  131. static __INLINE void TIMER_DisableCaptureDebounce(TIMER_T *timer)
  132. {
  133. timer->TEXCON &= ~TIMER_TEXCON_TEXDB_Msk;
  134. }
  135. /**
  136. * @details This function is used to enable the counter pin detection de-bounce function.
  137. */
  138. static __INLINE void TIMER_EnableEventCounterDebounce(TIMER_T *timer)
  139. {
  140. timer->TEXCON |= TIMER_TEXCON_TCDB_Msk;
  141. }
  142. /**
  143. * @details This function is used to disable the counter pin detection de-bounce function.
  144. */
  145. static __INLINE void TIMER_DisableEventCounterDebounce(TIMER_T *timer)
  146. {
  147. timer->TEXCON &= ~TIMER_TEXCON_TCDB_Msk;
  148. }
  149. /**
  150. * @details This function is used to enable the Timer time-out interrupt function.
  151. */
  152. static __INLINE void TIMER_EnableInt(TIMER_T *timer)
  153. {
  154. timer->TCSR |= TIMER_TCSR_IE_Msk;
  155. }
  156. /**
  157. * @details This function is used to disable the Timer time-out interrupt function.
  158. */
  159. static __INLINE void TIMER_DisableInt(TIMER_T *timer)
  160. {
  161. timer->TCSR &= ~TIMER_TCSR_IE_Msk;
  162. }
  163. /**
  164. * @details This function is used to enable the Timer capture trigger interrupt function.
  165. */
  166. static __INLINE void TIMER_EnableCaptureInt(TIMER_T *timer)
  167. {
  168. timer->TEXCON |= TIMER_TEXCON_TEXIEN_Msk;
  169. }
  170. /**
  171. * @details This function is used to disable the Timer capture trigger interrupt function.
  172. */
  173. static __INLINE void TIMER_DisableCaptureInt(TIMER_T *timer)
  174. {
  175. timer->TEXCON &= ~TIMER_TEXCON_TEXIEN_Msk;
  176. }
  177. /**
  178. * @brief Get Timer Time-out Interrupt Flag
  179. *
  180. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  181. *
  182. * @return Timer time-out interrupt occurred or not
  183. * @retval 0 Timer time-out interrupt did not occur
  184. * @retval 1 Timer time-out interrupt occurred
  185. *
  186. * @details This function indicates Timer time-out interrupt occurred or not.
  187. */
  188. static __INLINE uint32_t TIMER_GetIntFlag(TIMER_T *timer)
  189. {
  190. return (timer->TISR & TIMER_TISR_TIF_Msk ? 1 : 0);
  191. }
  192. /**
  193. * @details This function clears Timer time-out interrupt flag.
  194. */
  195. static __INLINE void TIMER_ClearIntFlag(TIMER_T *timer)
  196. {
  197. timer->TISR = TIMER_TISR_TIF_Msk;
  198. }
  199. /**
  200. * @brief Get Timer Capture Interrupt Flag
  201. *
  202. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  203. *
  204. * @return Timer capture interrupt occurred or not
  205. * @retval 0 Timer capture interrupt did not occur
  206. * @retval 1 Timer capture interrupt occurred
  207. *
  208. * @details This function indicates Timer capture interrupt occurred or not.
  209. */
  210. static __INLINE uint32_t TIMER_GetCaptureIntFlag(TIMER_T *timer)
  211. {
  212. return timer->TEXISR;
  213. }
  214. /**
  215. * @details This function clears Timer capture interrupt flag.
  216. */
  217. static __INLINE void TIMER_ClearCaptureIntFlag(TIMER_T *timer)
  218. {
  219. timer->TEXISR = TIMER_TEXISR_TEXIF_Msk;
  220. }
  221. /**
  222. * @brief Get Timer Wakeup Flag
  223. *
  224. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  225. *
  226. * @return Timer has waked up system or not
  227. * @retval 0 Timer did not wake up system
  228. * @retval 1 Timer Timer wake up system
  229. *
  230. * @details This function indicates Timer has waked up system or not.
  231. */
  232. static __INLINE uint32_t TIMER_GetWakeupFlag(TIMER_T *timer)
  233. {
  234. return (timer->TISR & TIMER_TISR_TWF_Msk ? 1 : 0);
  235. }
  236. /**
  237. * @details This function clears the Timer wake-up system flag.
  238. */
  239. static __INLINE void TIMER_ClearWakeupFlag(TIMER_T *timer)
  240. {
  241. timer->TISR = TIMER_TISR_TWF_Msk;
  242. }
  243. /**
  244. * @details This function reports the current timer capture data value.
  245. */
  246. static __INLINE uint32_t TIMER_GetCaptureData(TIMER_T *timer)
  247. {
  248. return timer->TCAP;
  249. }
  250. /**
  251. * @details This function reports the current timer counter value.
  252. */
  253. static __INLINE uint32_t TIMER_GetCounter(TIMER_T *timer)
  254. {
  255. return timer->TDR;
  256. }
  257. uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq);
  258. void TIMER_Close(TIMER_T *timer);
  259. void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec);
  260. void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge);
  261. void TIMER_DisableCapture(TIMER_T *timer);
  262. void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge);
  263. void TIMER_DisableEventCounter(TIMER_T *timer);
  264. uint32_t TIMER_GetModuleClock(TIMER_T *timer);
  265. /*@}*/ /* end of group M051_TIMER_EXPORTED_FUNCTIONS */
  266. /*@}*/ /* end of group M051_TIMER_Driver */
  267. /*@}*/ /* end of group M051_Device_Driver */
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif //__TIMER_H__
  272. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/