nu_timer.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /**************************************************************************//**
  2. * @file nu_timer.h
  3. * @version V3.00
  4. * @brief Timer Controller(Timer) driver header file
  5. *
  6. * @copyright SPDX-License-Identifier: Apache-2.0
  7. * @copyright Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #ifndef __NU_TIMER_H__
  10. #define __NU_TIMER_H__
  11. #ifdef __cplusplus
  12. extern "C"
  13. {
  14. #endif
  15. /** @addtogroup Standard_Driver Standard Driver
  16. @{
  17. */
  18. /** @addtogroup TIMER_Driver TIMER Driver
  19. @{
  20. */
  21. /** @addtogroup TIMER_EXPORTED_CONSTANTS TIMER Exported Constants
  22. @{
  23. */
  24. /*---------------------------------------------------------------------------------------------------------*/
  25. /* TIMER Operation Mode, External Counter and Capture Mode Constant Definitions */
  26. /*---------------------------------------------------------------------------------------------------------*/
  27. #define TIMER_ONESHOT_MODE (0UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in one-shot mode \hideinitializer */
  28. #define TIMER_PERIODIC_MODE (1UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in periodic mode \hideinitializer */
  29. #define TIMER_TOGGLE_MODE (2UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in toggle-output mode \hideinitializer */
  30. #define TIMER_CONTINUOUS_MODE (3UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in continuous counting mode \hideinitializer */
  31. #define TIMER_TOUT_PIN_FROM_TMX (0UL << TIMER_CTL_TGLPINSEL_Pos) /*!< Timer toggle-output pin is from TMx pin \hideinitializer */
  32. #define TIMER_TOUT_PIN_FROM_TMX_EXT (1UL << TIMER_CTL_TGLPINSEL_Pos) /*!< Timer toggle-output pin is from TMx_EXT pin \hideinitializer */
  33. #define TIMER_COUNTER_EVENT_FALLING (0UL << TIMER_EXTCTL_CNTPHASE_Pos) /*!< Counter increase on falling edge detection \hideinitializer */
  34. #define TIMER_COUNTER_EVENT_RISING (1UL << TIMER_EXTCTL_CNTPHASE_Pos) /*!< Counter increase on rising edge detection \hideinitializer */
  35. #define TIMER_CAPTURE_FREE_COUNTING_MODE (0UL << TIMER_EXTCTL_CAPFUNCS_Pos) /*!< Timer capture event to get timer counter value \hideinitializer */
  36. #define TIMER_CAPTURE_COUNTER_RESET_MODE (1UL << TIMER_EXTCTL_CAPFUNCS_Pos) /*!< Timer capture event to reset timer counter \hideinitializer */
  37. #define TIMER_CAPTURE_EVENT_FALLING (0UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Falling edge detection to trigger capture event \hideinitializer */
  38. #define TIMER_CAPTURE_EVENT_RISING (1UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Rising edge detection to trigger capture event \hideinitializer */
  39. #define TIMER_CAPTURE_EVENT_FALLING_RISING (2UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Both falling and rising edge detection to trigger capture event, and first event at falling edge \hideinitializer */
  40. #define TIMER_CAPTURE_EVENT_RISING_FALLING (3UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Both rising and falling edge detection to trigger capture event, and first event at rising edge \hideinitializer */
  41. #define TIMER_CAPTURE_EVENT_GET_LOW_PERIOD (6UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< First capture event is at falling edge, follows are at at rising edge \hideinitializer */
  42. #define TIMER_CAPTURE_EVENT_GET_HIGH_PERIOD (7UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< First capture event is at rising edge, follows are at at falling edge \hideinitializer */
  43. #define TIMER_CAPTURE_SOURCE_FROM_PIN (0UL << TIMER_CTL_CAPSRC_Pos) /*!< The capture source is from TMx_EXT pin \hideinitializer */
  44. #define TIMER_CAPTURE_SOURCE_FROM_INTERNAL (1UL << TIMER_CTL_CAPSRC_Pos) /*!< The capture source is from internal ACMPx signal or clock source \hideinitializer */
  45. #define TIMER_CAPTURE_SOURCE_DIV_1 (0UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 1 \hideinitializer */
  46. #define TIMER_CAPTURE_SOURCE_DIV_2 (1UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 2 \hideinitializer */
  47. #define TIMER_CAPTURE_SOURCE_DIV_4 (2UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 4 \hideinitializer */
  48. #define TIMER_CAPTURE_SOURCE_DIV_8 (3UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 8 \hideinitializer */
  49. #define TIMER_CAPTURE_SOURCE_DIV_16 (4UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 16 \hideinitializer */
  50. #define TIMER_CAPTURE_SOURCE_DIV_32 (5UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 32 \hideinitializer */
  51. #define TIMER_CAPTURE_SOURCE_DIV_64 (6UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 64 \hideinitializer */
  52. #define TIMER_CAPTURE_SOURCE_DIV_128 (7UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 128 \hideinitializer */
  53. #define TIMER_CAPTURE_SOURCE_DIV_256 (8UL << TIMER_EXTCTL_CAPDIVSCL_Pos) /*!< Input capture source divide 256 \hideinitializer */
  54. #define TIMER_INTER_CAPTURE_SOURCE_ACMP0 (0UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from internal ACMP0 output signal \hideinitializer */
  55. #define TIMER_INTER_CAPTURE_SOURCE_ACMP1 (1UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from internal ACMP1 output signal \hideinitializer */
  56. #define TIMER_INTER_CAPTURE_SOURCE_HXT (2UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from HXT \hideinitializer */
  57. #define TIMER_INTER_CAPTURE_SOURCE_LXT (3UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from LXT \hideinitializer */
  58. #define TIMER_INTER_CAPTURE_SOURCE_HIRC (4UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from HIRC \hideinitializer */
  59. #define TIMER_INTER_CAPTURE_SOURCE_LIRC (5UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from LIRC \hideinitializer */
  60. #define TIMER_INTER_CAPTURE_SOURCE_MIRC (6UL << TIMER_EXTCTL_INTERCAPSEL_Pos) /*!< Capture source from MIRC. Only available on TIMER4 and TIMER5 \hideinitializer */
  61. #define TIMER_TRGSRC_TIMEOUT_EVENT (0UL << TIMER_TRGCTL_TRGSSEL_Pos) /*!< Select internal trigger source from timer time-out event \hideinitializer */
  62. #define TIMER_TRGSRC_CAPTURE_EVENT (1UL << TIMER_TRGCTL_TRGSSEL_Pos) /*!< Select internal trigger source from timer capture event \hideinitializer */
  63. #define TIMER_TRG_TO_PWM (TIMER_TRGCTL_TRGPWM_Msk) /*!< Each timer event as EPWM and BPWM counter clock source. NOT supported on TIMER4 and TIMER5 \hideinitializer */
  64. #define TIMER_TRG_TO_EADC (TIMER_TRGCTL_TRGEADC_Msk) /*!< Each timer event to start ADC conversion \hideinitializer */
  65. #define TIMER_TRG_TO_DAC (TIMER_TRGCTL_TRGDAC_Msk) /*!< Each timer event to start DAC conversion. NOT supported on TIMER4 and TIMER5 \hideinitializer */
  66. #define TIMER_TRG_TO_PDMA (TIMER_TRGCTL_TRGPDMA_Msk) /*!< Each timer event to trigger PDMA transfer \hideinitializer */
  67. /**@}*/ /* end of group TIMER_EXPORTED_CONSTANTS */
  68. /** @addtogroup TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions
  69. @{
  70. */
  71. /**
  72. * @brief Set Timer Compared Value
  73. *
  74. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  75. * @param[in] u32Value Timer compare value. Valid values are between 2 to 0xFFFFFF.
  76. *
  77. * @return None
  78. *
  79. * @details This macro is used to set timer compared value to adjust timer time-out interval.
  80. * @note 1. Never write 0x0 or 0x1 in this field, or the core will run into unknown state. \n
  81. * 2. If update timer compared value in continuous counting mode, timer counter value will keep counting continuously. \n
  82. * But if timer is operating at other modes, the timer up counter will restart counting and start from 0.
  83. * \hideinitializer
  84. */
  85. #define TIMER_SET_CMP_VALUE(timer, u32Value) ((timer)->CMP = (u32Value))
  86. /**
  87. * @brief Set Timer Prescale Value
  88. *
  89. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  90. * @param[in] u32Value Timer prescale value. Valid values are between 0 to 0xFF.
  91. *
  92. * @return None
  93. *
  94. * @details This macro is used to set timer prescale value and timer source clock will be divided by (prescale + 1) \n
  95. * before it is fed into timer.
  96. * \hideinitializer
  97. */
  98. #define TIMER_SET_PRESCALE_VALUE(timer, u32Value) ((timer)->CTL = ((timer)->CTL & ~TIMER_CTL_PSC_Msk) | (u32Value))
  99. /**
  100. * @brief Check specify Timer Status
  101. *
  102. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  103. *
  104. * @retval 0 Timer 24-bit up counter is inactive
  105. * @retval 1 Timer 24-bit up counter is active
  106. *
  107. * @details This macro is used to check if specify Timer counter is inactive or active.
  108. * \hideinitializer
  109. */
  110. #define TIMER_IS_ACTIVE(timer) ((((timer)->CTL & TIMER_CTL_ACTSTS_Msk) == TIMER_CTL_ACTSTS_Msk)? 1 : 0)
  111. /**
  112. * @brief Select Toggle-output Pin
  113. *
  114. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  115. * @param[in] u32ToutSel Toggle-output pin selection, valid values are:
  116. * - \ref TIMER_TOUT_PIN_FROM_TMX
  117. * - \ref TIMER_TOUT_PIN_FROM_TMX_EXT
  118. *
  119. * @return None
  120. *
  121. * @details This macro is used to select timer toggle-output pin is output on TMx or TMx_EXT pin.
  122. * \hideinitializer
  123. */
  124. #define TIMER_SELECT_TOUT_PIN(timer, u32ToutSel) ((timer)->CTL = ((timer)->CTL & ~TIMER_CTL_TGLPINSEL_Msk) | (u32ToutSel))
  125. /**
  126. * @brief Set Timer Operating Mode
  127. *
  128. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  129. * @param[in] u32OpMode Operation mode. Possible options are
  130. * - \ref TIMER_ONESHOT_MODE
  131. * - \ref TIMER_PERIODIC_MODE
  132. * - \ref TIMER_TOGGLE_MODE
  133. * - \ref TIMER_CONTINUOUS_MODE
  134. *
  135. * @return None
  136. * \hideinitializer
  137. */
  138. #define TIMER_SET_OPMODE(timer, u32OpMode) ((timer)->CTL = ((timer)->CTL & ~TIMER_CTL_OPMODE_Msk) | (u32OpMode))
  139. /*---------------------------------------------------------------------------------------------------------*/
  140. /* static inline functions */
  141. /*---------------------------------------------------------------------------------------------------------*/
  142. /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
  143. __STATIC_INLINE void TIMER_Start(TIMER_T *timer);
  144. __STATIC_INLINE void TIMER_Stop(TIMER_T *timer);
  145. __STATIC_INLINE void TIMER_EnableWakeup(TIMER_T *timer);
  146. __STATIC_INLINE void TIMER_DisableWakeup(TIMER_T *timer);
  147. __STATIC_INLINE void TIMER_StartCapture(TIMER_T *timer);
  148. __STATIC_INLINE void TIMER_StopCapture(TIMER_T *timer);
  149. __STATIC_INLINE void TIMER_EnableCaptureDebounce(TIMER_T *timer);
  150. __STATIC_INLINE void TIMER_DisableCaptureDebounce(TIMER_T *timer);
  151. __STATIC_INLINE void TIMER_EnableEventCounterDebounce(TIMER_T *timer);
  152. __STATIC_INLINE void TIMER_DisableEventCounterDebounce(TIMER_T *timer);
  153. __STATIC_INLINE void TIMER_EnableInt(TIMER_T *timer);
  154. __STATIC_INLINE void TIMER_DisableInt(TIMER_T *timer);
  155. __STATIC_INLINE void TIMER_EnableCaptureInt(TIMER_T *timer);
  156. __STATIC_INLINE void TIMER_DisableCaptureInt(TIMER_T *timer);
  157. __STATIC_INLINE uint32_t TIMER_GetIntFlag(TIMER_T *timer);
  158. __STATIC_INLINE void TIMER_ClearIntFlag(TIMER_T *timer);
  159. __STATIC_INLINE uint32_t TIMER_GetCaptureIntFlag(TIMER_T *timer);
  160. __STATIC_INLINE void TIMER_ClearCaptureIntFlag(TIMER_T *timer);
  161. __STATIC_INLINE uint32_t TIMER_GetWakeupFlag(TIMER_T *timer);
  162. __STATIC_INLINE void TIMER_ClearWakeupFlag(TIMER_T *timer);
  163. __STATIC_INLINE uint32_t TIMER_GetCaptureData(TIMER_T *timer);
  164. __STATIC_INLINE uint32_t TIMER_GetCounter(TIMER_T *timer);
  165. __STATIC_INLINE void TIMER_ResetCounter(TIMER_T *timer);
  166. /**
  167. * @brief Start Timer Counting
  168. *
  169. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  170. *
  171. * @return None
  172. *
  173. * @details This function is used to start Timer counting.
  174. */
  175. __STATIC_INLINE void TIMER_Start(TIMER_T *timer)
  176. {
  177. timer->CTL |= TIMER_CTL_CNTEN_Msk;
  178. }
  179. /**
  180. * @brief Stop Timer Counting
  181. *
  182. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  183. *
  184. * @return None
  185. *
  186. * @details This function is used to stop/suspend Timer counting.
  187. */
  188. __STATIC_INLINE void TIMER_Stop(TIMER_T *timer)
  189. {
  190. timer->CTL &= ~TIMER_CTL_CNTEN_Msk;
  191. }
  192. /**
  193. * @brief Enable Timer Interrupt Wake-up Function
  194. *
  195. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  196. *
  197. * @return None
  198. *
  199. * @details This function is used to enable the timer interrupt wake-up function and interrupt source could be time-out interrupt, \n
  200. * counter event interrupt or capture trigger interrupt.
  201. * @note To wake the system from Power-down mode, timer clock source must be ether LXT or LIRC.
  202. */
  203. __STATIC_INLINE void TIMER_EnableWakeup(TIMER_T *timer)
  204. {
  205. timer->CTL |= TIMER_CTL_WKEN_Msk;
  206. }
  207. /**
  208. * @brief Disable Timer Wake-up Function
  209. *
  210. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  211. *
  212. * @return None
  213. *
  214. * @details This function is used to disable the timer interrupt wake-up function.
  215. */
  216. __STATIC_INLINE void TIMER_DisableWakeup(TIMER_T *timer)
  217. {
  218. timer->CTL &= ~TIMER_CTL_WKEN_Msk;
  219. }
  220. /**
  221. * @brief Start Timer Capture Function
  222. *
  223. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  224. *
  225. * @return None
  226. *
  227. * @details This function is used to start Timer capture function.
  228. */
  229. __STATIC_INLINE void TIMER_StartCapture(TIMER_T *timer)
  230. {
  231. timer->EXTCTL |= TIMER_EXTCTL_CAPEN_Msk;
  232. }
  233. /**
  234. * @brief Stop Timer Capture Function
  235. *
  236. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  237. *
  238. * @return None
  239. *
  240. * @details This function is used to stop Timer capture function.
  241. */
  242. __STATIC_INLINE void TIMER_StopCapture(TIMER_T *timer)
  243. {
  244. timer->EXTCTL &= ~TIMER_EXTCTL_CAPEN_Msk;
  245. }
  246. /**
  247. * @brief Enable Capture Pin De-bounce
  248. *
  249. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  250. *
  251. * @return None
  252. *
  253. * @details This function is used to enable the detect de-bounce function of capture pin.
  254. */
  255. __STATIC_INLINE void TIMER_EnableCaptureDebounce(TIMER_T *timer)
  256. {
  257. timer->EXTCTL |= TIMER_EXTCTL_CAPDBEN_Msk;
  258. }
  259. /**
  260. * @brief Disable Capture Pin De-bounce
  261. *
  262. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  263. *
  264. * @return None
  265. *
  266. * @details This function is used to disable the detect de-bounce function of capture pin.
  267. */
  268. __STATIC_INLINE void TIMER_DisableCaptureDebounce(TIMER_T *timer)
  269. {
  270. timer->EXTCTL &= ~TIMER_EXTCTL_CAPDBEN_Msk;
  271. }
  272. /**
  273. * @brief Enable Counter Pin De-bounce
  274. *
  275. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  276. *
  277. * @return None
  278. *
  279. * @details This function is used to enable the detect de-bounce function of counter pin.
  280. */
  281. __STATIC_INLINE void TIMER_EnableEventCounterDebounce(TIMER_T *timer)
  282. {
  283. timer->EXTCTL |= TIMER_EXTCTL_CNTDBEN_Msk;
  284. }
  285. /**
  286. * @brief Disable Counter Pin De-bounce
  287. *
  288. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  289. *
  290. * @return None
  291. *
  292. * @details This function is used to disable the detect de-bounce function of counter pin.
  293. */
  294. __STATIC_INLINE void TIMER_DisableEventCounterDebounce(TIMER_T *timer)
  295. {
  296. timer->EXTCTL &= ~TIMER_EXTCTL_CNTDBEN_Msk;
  297. }
  298. /**
  299. * @brief Enable Timer Time-out Interrupt
  300. *
  301. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  302. *
  303. * @return None
  304. *
  305. * @details This function is used to enable the timer time-out interrupt function.
  306. */
  307. __STATIC_INLINE void TIMER_EnableInt(TIMER_T *timer)
  308. {
  309. timer->CTL |= TIMER_CTL_INTEN_Msk;
  310. }
  311. /**
  312. * @brief Disable Timer Time-out Interrupt
  313. *
  314. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  315. *
  316. * @return None
  317. *
  318. * @details This function is used to disable the timer time-out interrupt function.
  319. */
  320. __STATIC_INLINE void TIMER_DisableInt(TIMER_T *timer)
  321. {
  322. timer->CTL &= ~TIMER_CTL_INTEN_Msk;
  323. }
  324. /**
  325. * @brief Enable Capture Trigger Interrupt
  326. *
  327. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  328. *
  329. * @return None
  330. *
  331. * @details This function is used to enable the timer capture trigger interrupt function.
  332. */
  333. __STATIC_INLINE void TIMER_EnableCaptureInt(TIMER_T *timer)
  334. {
  335. timer->EXTCTL |= TIMER_EXTCTL_CAPIEN_Msk;
  336. }
  337. /**
  338. * @brief Disable Capture Trigger Interrupt
  339. *
  340. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  341. *
  342. * @return None
  343. *
  344. * @details This function is used to disable the timer capture trigger interrupt function.
  345. */
  346. __STATIC_INLINE void TIMER_DisableCaptureInt(TIMER_T *timer)
  347. {
  348. timer->EXTCTL &= ~TIMER_EXTCTL_CAPIEN_Msk;
  349. }
  350. /**
  351. * @brief Get Timer Time-out Interrupt Flag
  352. *
  353. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  354. *
  355. * @retval 0 Timer time-out interrupt did not occur
  356. * @retval 1 Timer time-out interrupt occurred
  357. *
  358. * @details This function indicates timer time-out interrupt occurred or not.
  359. */
  360. __STATIC_INLINE uint32_t TIMER_GetIntFlag(TIMER_T *timer)
  361. {
  362. return (((timer->INTSTS & TIMER_INTSTS_TIF_Msk) == TIMER_INTSTS_TIF_Msk) ? 1UL : 0UL);
  363. }
  364. /**
  365. * @brief Clear Timer Time-out Interrupt Flag
  366. *
  367. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  368. *
  369. * @return None
  370. *
  371. * @details This function clears timer time-out interrupt flag to 0.
  372. */
  373. __STATIC_INLINE void TIMER_ClearIntFlag(TIMER_T *timer)
  374. {
  375. timer->INTSTS = TIMER_INTSTS_TIF_Msk;
  376. }
  377. /**
  378. * @brief Get Timer Capture Interrupt Flag
  379. *
  380. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  381. *
  382. * @retval 0 Timer capture interrupt did not occur
  383. * @retval 1 Timer capture interrupt occurred
  384. *
  385. * @details This function indicates timer capture trigger interrupt occurred or not.
  386. */
  387. __STATIC_INLINE uint32_t TIMER_GetCaptureIntFlag(TIMER_T *timer)
  388. {
  389. return timer->EINTSTS;
  390. }
  391. /**
  392. * @brief Clear Timer Capture Interrupt Flag
  393. *
  394. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  395. *
  396. * @return None
  397. *
  398. * @details This function clears timer capture trigger interrupt flag to 0.
  399. */
  400. __STATIC_INLINE void TIMER_ClearCaptureIntFlag(TIMER_T *timer)
  401. {
  402. timer->EINTSTS = TIMER_EINTSTS_CAPIF_Msk;
  403. }
  404. /**
  405. * @brief Get Timer Wake-up Flag
  406. *
  407. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  408. *
  409. * @retval 0 Timer does not cause CPU wake-up
  410. * @retval 1 Timer interrupt event cause CPU wake-up
  411. *
  412. * @details This function indicates timer interrupt event has waked up system or not.
  413. */
  414. __STATIC_INLINE uint32_t TIMER_GetWakeupFlag(TIMER_T *timer)
  415. {
  416. return (((timer->INTSTS & TIMER_INTSTS_TWKF_Msk) == TIMER_INTSTS_TWKF_Msk) ? 1UL : 0UL);
  417. }
  418. /**
  419. * @brief Clear Timer Wake-up Flag
  420. *
  421. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  422. *
  423. * @return None
  424. *
  425. * @details This function clears the timer wake-up system flag to 0.
  426. */
  427. __STATIC_INLINE void TIMER_ClearWakeupFlag(TIMER_T *timer)
  428. {
  429. timer->INTSTS = TIMER_INTSTS_TWKF_Msk;
  430. }
  431. /**
  432. * @brief Get Capture value
  433. *
  434. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  435. *
  436. * @return 24-bit Capture Value
  437. *
  438. * @details This function reports the current 24-bit timer capture value.
  439. */
  440. __STATIC_INLINE uint32_t TIMER_GetCaptureData(TIMER_T *timer)
  441. {
  442. return timer->CAP;
  443. }
  444. /**
  445. * @brief Get Counter value
  446. *
  447. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  448. *
  449. * @return 24-bit Counter Value
  450. *
  451. * @details This function reports the current 24-bit timer counter value.
  452. */
  453. __STATIC_INLINE uint32_t TIMER_GetCounter(TIMER_T *timer)
  454. {
  455. return timer->CNT;
  456. }
  457. /**
  458. * @brief Reset Counter
  459. *
  460. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0 ~ TIMER5.
  461. *
  462. * @return None
  463. *
  464. * @details This function is used to reset current counter value and internal prescale counter value.
  465. */
  466. __STATIC_INLINE void TIMER_ResetCounter(TIMER_T *timer)
  467. {
  468. timer->CNT = 0UL;
  469. while((timer->CNT & TIMER_CNT_RSTACT_Msk) == TIMER_CNT_RSTACT_Msk) {}
  470. }
  471. uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq);
  472. void TIMER_Close(TIMER_T *timer);
  473. void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec);
  474. void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge);
  475. void TIMER_DisableCapture(TIMER_T *timer);
  476. void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge);
  477. void TIMER_DisableEventCounter(TIMER_T *timer);
  478. uint32_t TIMER_GetModuleClock(TIMER_T *timer);
  479. void TIMER_EnableFreqCounter(TIMER_T *timer, uint32_t u32DropCount, uint32_t u32Timeout, uint32_t u32EnableInt);
  480. void TIMER_DisableFreqCounter(TIMER_T *timer);
  481. void TIMER_SetTriggerSource(TIMER_T *timer, uint32_t u32Src);
  482. void TIMER_SetTriggerTarget(TIMER_T *timer, uint32_t u32Mask);
  483. /**@}*/ /* end of group TIMER_EXPORTED_FUNCTIONS */
  484. /**@}*/ /* end of group TIMER_Driver */
  485. /**@}*/ /* end of group Standard_Driver */
  486. #ifdef __cplusplus
  487. }
  488. #endif
  489. #endif /* __NU_TIMER_H__ */