timer.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /**************************************************************************//**
  2. * @file timer.h
  3. * @version V3.00
  4. * $Revision: 10 $
  5. * $Date: 15/08/11 10:26a $
  6. * @brief M451 series Timer driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2013~2015 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 Standard_Driver Standard Driver
  18. @{
  19. */
  20. /** @addtogroup TIMER_Driver TIMER Driver
  21. @{
  22. */
  23. /** @addtogroup TIMER_EXPORTED_CONSTANTS TIMER Exported Constants
  24. @{
  25. */
  26. /*---------------------------------------------------------------------------------------------------------*/
  27. /* TIMER Operation Mode, External Counter and Capture Mode Constant Definitions */
  28. /*---------------------------------------------------------------------------------------------------------*/
  29. #define TIMER_ONESHOT_MODE (0UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in one-shot mode */
  30. #define TIMER_PERIODIC_MODE (1UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in periodic mode */
  31. #define TIMER_TOGGLE_MODE (2UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in toggle-output mode */
  32. #define TIMER_CONTINUOUS_MODE (3UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in continuous counting mode */
  33. #define TIMER_TOUT_PIN_FROM_TX (0UL << TIMER_CTL_TGLPINSEL_Pos) /*!< Timer toggle-output pin is from Tx pin */
  34. #define TIMER_TOUT_PIN_FROM_TX_EXT (1UL << TIMER_CTL_TGLPINSEL_Pos) /*!< Timer toggle-output pin is from Tx_EXT pin */
  35. #define TIMER_CAPTURE_FREE_COUNTING_MODE (0UL << TIMER_EXTCTL_CAPFUNCS_Pos) /*!< Timer capture event to get timer counter value */
  36. #define TIMER_CAPTURE_COUNTER_RESET_MODE (1UL << TIMER_EXTCTL_CAPFUNCS_Pos) /*!< Timer capture event to reset timer counter */
  37. #define TIMER_CAPTURE_FALLING_EDGE (0UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Falling edge detection to trigger timer capture */
  38. #define TIMER_CAPTURE_RISING_EDGE (1UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Rising edge detection to trigger timer capture */
  39. #define TIMER_CAPTURE_FALLING_AND_RISING_EDGE (2UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Both falling and rising edge detection to trigger timer capture */
  40. #define TIMER_COUNTER_FALLING_EDGE (0UL << TIMER_EXTCTL_CNTPHASE_Pos) /*!< Counter increase on falling edge detection */
  41. #define TIMER_COUNTER_RISING_EDGE (1UL << TIMER_EXTCTL_CNTPHASE_Pos) /*!< Counter increase on rising edge detection */
  42. /*@}*/ /* end of group TIMER_EXPORTED_CONSTANTS */
  43. /** @addtogroup TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions
  44. @{
  45. */
  46. /**
  47. * @brief Set Timer Compared Value
  48. *
  49. * @param[in] timer The pointer of the specified 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 timer compared value to adjust timer time-out interval.
  55. * @note 1. Never write 0x0 or 0x1 in this field, or the core will run into unknown state. \n
  56. * 2. If update timer compared value in continuous counting mode, timer counter value will keep counting continuously. \n
  57. * But if timer is operating at other modes, the timer up counter will restart counting and start from 0.
  58. */
  59. #define TIMER_SET_CMP_VALUE(timer, u32Value) ((timer)->CMP = (u32Value))
  60. /**
  61. * @brief Set Timer Prescale Value
  62. *
  63. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  64. * @param[in] u32Value Timer prescale value. Valid values are between 0 to 0xFF.
  65. *
  66. * @return None
  67. *
  68. * @details This macro is used to set timer prescale value and timer source clock will be divided by (prescale + 1) \n
  69. * before it is fed into timer.
  70. */
  71. #define TIMER_SET_PRESCALE_VALUE(timer, u32Value) ((timer)->CTL = ((timer)->CTL & ~TIMER_CTL_PSC_Msk) | (u32Value))
  72. /**
  73. * @brief Check specify Timer Status
  74. *
  75. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  76. *
  77. * @retval 0 Timer 24-bit up counter is inactive
  78. * @retval 1 Timer 24-bit up counter is active
  79. *
  80. * @details This macro is used to check if specify Timer counter is inactive or active.
  81. */
  82. #define TIMER_IS_ACTIVE(timer) (((timer)->CTL & TIMER_CTL_ACTSTS_Msk)? 1 : 0)
  83. /**
  84. * @brief Select Toggle-output Pin
  85. *
  86. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  87. * @param[in] u32ToutSel Toggle-output pin selection, valid values are:
  88. * - \ref TIMER_TOUT_PIN_FROM_TX
  89. * - \ref TIMER_TOUT_PIN_FROM_TX_EXT
  90. *
  91. * @return None
  92. *
  93. * @details This macro is used to select timer toggle-output pin is output on Tx or Tx_EXT pin.
  94. */
  95. #define TIMER_SELECT_TOUT_PIN(timer, u32ToutSel) ((timer)->CTL = ((timer)->CTL & ~TIMER_CTL_TGLPINSEL_Msk) | (u32ToutSel))
  96. /**
  97. * @brief Start Timer Counting
  98. *
  99. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  100. *
  101. * @return None
  102. *
  103. * @details This function is used to start Timer counting.
  104. */
  105. static __INLINE void TIMER_Start(TIMER_T *timer)
  106. {
  107. timer->CTL |= TIMER_CTL_CNTEN_Msk;
  108. }
  109. /**
  110. * @brief Stop Timer Counting
  111. *
  112. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  113. *
  114. * @return None
  115. *
  116. * @details This function is used to stop/suspend Timer counting.
  117. */
  118. static __INLINE void TIMER_Stop(TIMER_T *timer)
  119. {
  120. timer->CTL &= ~TIMER_CTL_CNTEN_Msk;
  121. }
  122. /**
  123. * @brief Enable Timer Interrupt Wake-up Function
  124. *
  125. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  126. *
  127. * @return None
  128. *
  129. * @details This function is used to enable the timer interrupt wake-up function and interrupt source could be time-out interrupt, \n
  130. * counter event interrupt or capture trigger interrupt.
  131. * @note To wake the system from Power-down mode, timer clock source must be ether LXT or LIRC.
  132. */
  133. static __INLINE void TIMER_EnableWakeup(TIMER_T *timer)
  134. {
  135. timer->CTL |= TIMER_CTL_WKEN_Msk;
  136. }
  137. /**
  138. * @brief Disable Timer Wake-up Function
  139. *
  140. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  141. *
  142. * @return None
  143. *
  144. * @details This function is used to disable the timer interrupt wake-up function.
  145. */
  146. static __INLINE void TIMER_DisableWakeup(TIMER_T *timer)
  147. {
  148. timer->CTL &= ~TIMER_CTL_WKEN_Msk;
  149. }
  150. /**
  151. * @brief Enable Capture Pin De-bounce
  152. *
  153. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  154. *
  155. * @return None
  156. *
  157. * @details This function is used to enable the detect de-bounce function of capture pin.
  158. */
  159. static __INLINE void TIMER_EnableCaptureDebounce(TIMER_T *timer)
  160. {
  161. timer->EXTCTL |= TIMER_EXTCTL_CAPDBEN_Msk;
  162. }
  163. /**
  164. * @brief Disable Capture Pin De-bounce
  165. *
  166. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  167. *
  168. * @return None
  169. *
  170. * @details This function is used to disable the detect de-bounce function of capture pin.
  171. */
  172. static __INLINE void TIMER_DisableCaptureDebounce(TIMER_T *timer)
  173. {
  174. timer->EXTCTL &= ~TIMER_EXTCTL_CAPDBEN_Msk;
  175. }
  176. /**
  177. * @brief Enable Counter Pin De-bounce
  178. *
  179. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  180. *
  181. * @return None
  182. *
  183. * @details This function is used to enable the detect de-bounce function of counter pin.
  184. */
  185. static __INLINE void TIMER_EnableEventCounterDebounce(TIMER_T *timer)
  186. {
  187. timer->EXTCTL |= TIMER_EXTCTL_CNTDBEN_Msk;
  188. }
  189. /**
  190. * @brief Disable Counter Pin De-bounce
  191. *
  192. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  193. *
  194. * @return None
  195. *
  196. * @details This function is used to disable the detect de-bounce function of counter pin.
  197. */
  198. static __INLINE void TIMER_DisableEventCounterDebounce(TIMER_T *timer)
  199. {
  200. timer->EXTCTL &= ~TIMER_EXTCTL_CNTDBEN_Msk;
  201. }
  202. /**
  203. * @brief Enable Timer Time-out Interrupt
  204. *
  205. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  206. *
  207. * @return None
  208. *
  209. * @details This function is used to enable the timer time-out interrupt function.
  210. */
  211. static __INLINE void TIMER_EnableInt(TIMER_T *timer)
  212. {
  213. timer->CTL |= TIMER_CTL_INTEN_Msk;
  214. }
  215. /**
  216. * @brief Disable Timer Time-out Interrupt
  217. *
  218. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  219. *
  220. * @return None
  221. *
  222. * @details This function is used to disable the timer time-out interrupt function.
  223. */
  224. static __INLINE void TIMER_DisableInt(TIMER_T *timer)
  225. {
  226. timer->CTL &= ~TIMER_CTL_INTEN_Msk;
  227. }
  228. /**
  229. * @brief Enable Capture Trigger Interrupt
  230. *
  231. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  232. *
  233. * @return None
  234. *
  235. * @details This function is used to enable the timer capture trigger interrupt function.
  236. */
  237. static __INLINE void TIMER_EnableCaptureInt(TIMER_T *timer)
  238. {
  239. timer->EXTCTL |= TIMER_EXTCTL_CAPIEN_Msk;
  240. }
  241. /**
  242. * @brief Disable Capture Trigger Interrupt
  243. *
  244. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  245. *
  246. * @return None
  247. *
  248. * @details This function is used to disable the timer capture trigger interrupt function.
  249. */
  250. static __INLINE void TIMER_DisableCaptureInt(TIMER_T *timer)
  251. {
  252. timer->EXTCTL &= ~TIMER_EXTCTL_CAPIEN_Msk;
  253. }
  254. /**
  255. * @brief Get Timer Time-out Interrupt Flag
  256. *
  257. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  258. *
  259. * @retval 0 Timer time-out interrupt did not occur
  260. * @retval 1 Timer time-out interrupt occurred
  261. *
  262. * @details This function indicates timer time-out interrupt occurred or not.
  263. */
  264. static __INLINE uint32_t TIMER_GetIntFlag(TIMER_T *timer)
  265. {
  266. return ((timer->INTSTS & TIMER_INTSTS_TIF_Msk) ? 1 : 0);
  267. }
  268. /**
  269. * @brief Clear Timer Time-out Interrupt Flag
  270. *
  271. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  272. *
  273. * @return None
  274. *
  275. * @details This function clears timer time-out interrupt flag to 0.
  276. */
  277. static __INLINE void TIMER_ClearIntFlag(TIMER_T *timer)
  278. {
  279. timer->INTSTS = (timer->INTSTS & ~TIMER_INTSTS_TWKF_Msk) | TIMER_INTSTS_TIF_Msk;
  280. }
  281. /**
  282. * @brief Get Timer Capture Interrupt Flag
  283. *
  284. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  285. *
  286. * @retval 0 Timer capture interrupt did not occur
  287. * @retval 1 Timer capture interrupt occurred
  288. *
  289. * @details This function indicates timer capture trigger interrupt occurred or not.
  290. */
  291. static __INLINE uint32_t TIMER_GetCaptureIntFlag(TIMER_T *timer)
  292. {
  293. return timer->EINTSTS;
  294. }
  295. /**
  296. * @brief Clear Timer Capture Interrupt Flag
  297. *
  298. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  299. *
  300. * @return None
  301. *
  302. * @details This function clears timer capture trigger interrupt flag to 0.
  303. */
  304. static __INLINE void TIMER_ClearCaptureIntFlag(TIMER_T *timer)
  305. {
  306. timer->EINTSTS = TIMER_EINTSTS_CAPIF_Msk;
  307. }
  308. /**
  309. * @brief Get Timer Wake-up Flag
  310. *
  311. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  312. *
  313. * @retval 0 Timer does not cause CPU wake-up
  314. * @retval 1 Timer interrupt event cause CPU wake-up
  315. *
  316. * @details This function indicates timer interrupt event has waked up system or not.
  317. */
  318. static __INLINE uint32_t TIMER_GetWakeupFlag(TIMER_T *timer)
  319. {
  320. return (timer->INTSTS & TIMER_INTSTS_TWKF_Msk ? 1 : 0);
  321. }
  322. /**
  323. * @brief Clear Timer Wake-up Flag
  324. *
  325. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  326. *
  327. * @return None
  328. *
  329. * @details This function clears the timer wake-up system flag to 0.
  330. */
  331. static __INLINE void TIMER_ClearWakeupFlag(TIMER_T *timer)
  332. {
  333. timer->INTSTS = (timer->INTSTS & ~TIMER_INTSTS_TIF_Msk) | TIMER_INTSTS_TWKF_Msk;
  334. }
  335. /**
  336. * @brief Get Capture value
  337. *
  338. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  339. *
  340. * @return 24-bit Capture Value
  341. *
  342. * @details This function reports the current 24-bit timer capture value.
  343. */
  344. static __INLINE uint32_t TIMER_GetCaptureData(TIMER_T *timer)
  345. {
  346. return timer->CAP;
  347. }
  348. /**
  349. * @brief Get Counter value
  350. *
  351. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  352. *
  353. * @return 24-bit Counter Value
  354. *
  355. * @details This function reports the current 24-bit timer counter value.
  356. */
  357. static __INLINE uint32_t TIMER_GetCounter(TIMER_T *timer)
  358. {
  359. return timer->CNT;
  360. }
  361. uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq);
  362. void TIMER_Close(TIMER_T *timer);
  363. void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec);
  364. void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge);
  365. void TIMER_DisableCapture(TIMER_T *timer);
  366. void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge);
  367. void TIMER_DisableEventCounter(TIMER_T *timer);
  368. uint32_t TIMER_GetModuleClock(TIMER_T *timer);
  369. /*@}*/ /* end of group TIMER_EXPORTED_FUNCTIONS */
  370. /*@}*/ /* end of group TIMER_Driver */
  371. /*@}*/ /* end of group Standard_Driver */
  372. #ifdef __cplusplus
  373. }
  374. #endif
  375. #endif //__TIMER_H__
  376. /*** (C) COPYRIGHT 2013~2015 Nuvoton Technology Corp. ***/