nu_timer.h 16 KB

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