timer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**************************************************************************//**
  2. * @file timer.h
  3. * @version V1.00
  4. * $Revision: 7 $
  5. * $Date: 15/11/12 9:55a $
  6. * @brief NUC472/NUC442 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 NUC472_442_Device_Driver NUC472/NUC442 Device Driver
  18. @{
  19. */
  20. /** @addtogroup NUC472_442_TIMER_Driver TIMER Driver
  21. @{
  22. */
  23. /** @addtogroup NUC472_442_TIMER_EXPORTED_CONSTANTS TIMER Exported Constants
  24. @{
  25. */
  26. #define TIMER_ONESHOT_MODE (0UL) /*!< Timer working in one shot mode \hideinitializer */
  27. #define TIMER_PERIODIC_MODE (1UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in periodic mode \hideinitializer */
  28. #define TIMER_TOGGLE_MODE (2UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in toggle mode \hideinitializer */
  29. #define TIMER_CONTINUOUS_MODE (3UL << TIMER_CTL_OPMODE_Pos) /*!< Timer working in continuous mode \hideinitializer */
  30. #define TIMER_CAPTURE_FREE_COUNTING_MODE (0UL) /*!< Free counting mode \hideinitializer */
  31. #define TIMER_CAPTURE_COUNTER_RESET_MODE (TIMER_EXTCTL_CAPFUNCS_Msk) /*!< Counter reset mode \hideinitializer */
  32. #define TIMER_CAPTURE_FALLING_EDGE (0UL) /*!< Falling edge trigger timer capture \hideinitializer */
  33. #define TIMER_CAPTURE_RISING_EDGE (1UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Rising edge trigger timer capture \hideinitializer */
  34. #define TIMER_CAPTURE_FALLING_AND_RISING_EDGE (2UL << TIMER_EXTCTL_CAPEDGE_Pos) /*!< Both falling edge and rising edge trigger timer capture \hideinitializer */
  35. #define TIMER_COUNTER_RISING_EDGE (TIMER_EXTCTL_CNTPHASE_Msk) /*!< Counter increase on rising edge \hideinitializer */
  36. #define TIMER_COUNTER_FALLING_EDGE (0UL) /*!< Counter increase on falling edge \hideinitializer */
  37. #define TIMER_TOGGLE_OUT1 (TIMER_CTL_TOGDIS2_Msk) /*!< Select PB.4, PB.1, PC.6, PC.1 as timer toggle output pin \hideinitializer */
  38. #define TIMER_TOGGLE_OUT2 (TIMER_CTL_TOGDIS1_Msk) /*!< Select PD.1, PE.8, PE.1, PD.11 as timer toggle output pin \hideinitializer */
  39. /*@}*/ /* end of group NUC472_442_TIMER_EXPORTED_CONSTANTS */
  40. /** @addtogroup NUC472_442_TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions
  41. @{
  42. */
  43. /**
  44. * @brief This macro is used to set new Timer compared value
  45. * @param[in] timer The base address of Timer module
  46. * @param[in] u32Value Timer compare value. Valid values are between 2 to 0xFFFFFF
  47. * @return None
  48. * \hideinitializer
  49. */
  50. #define TIMER_SET_CMP_VALUE(timer, u32Value) ((timer)->CMP = (u32Value))
  51. /**
  52. * @brief This macro is used to set new Timer prescale value
  53. * @param[in] timer The base address of Timer module
  54. * @param[in] u32Value Timer prescale value. Valid values are between 0 to 0xFF
  55. * @return None
  56. * @note Clock input is divided by (prescale + 1) before it is fed into timer
  57. * \hideinitializer
  58. */
  59. #define TIMER_SET_PRESCALE_VALUE(timer, u32Value) ((timer)->CTL = ((timer)->CTL & ~TIMER_CTL_PSC_Msk) | (u32Value))
  60. /**
  61. * @brief This macro is used to check if specify Timer is inactive or active
  62. * @return timer is activate or inactivate
  63. * @retval 0 Timer 24-bit up counter is inactive
  64. * @retval 1 Timer 24-bit up counter is active
  65. * \hideinitializer
  66. */
  67. #define TIMER_IS_ACTIVE(timer) ((timer)->CTL & TIMER_CTL_ACTSTS_Msk ? 1 : 0)
  68. /**
  69. * @brief This macro is used to select Timer toggle output pin
  70. * @param[in] timer The base address of Timer module
  71. * @param[in] u32ToutSel Toggle output pin selection, valid values are
  72. * - \ref TIMER_TOGGLE_OUT1
  73. * - \ref TIMER_TOGGLE_OUT2
  74. * @return None
  75. * \hideinitializer
  76. */
  77. #define TIMER_SELECT_TOUT_PIN(timer, u32ToutSel) ((timer)->CTL = ((timer)->CTL & ~(TIMER_CTL_TOGDIS2_Msk | TIMER_CTL_TOGDIS1_Msk)) | (u32ToutSel))
  78. /**
  79. * @brief This function is used to start Timer counting
  80. * @param[in] timer The base address of Timer module
  81. * @return None
  82. */
  83. __STATIC_INLINE void TIMER_Start(TIMER_T *timer)
  84. {
  85. timer->CTL |= TIMER_CTL_CNTEN_Msk;
  86. }
  87. /**
  88. * @brief This function is used to stop Timer counting
  89. * @param[in] timer The base address of Timer module
  90. * @return None
  91. */
  92. __STATIC_INLINE void TIMER_Stop(TIMER_T *timer)
  93. {
  94. timer->CTL &= ~TIMER_CTL_CNTEN_Msk;
  95. }
  96. /**
  97. * @brief This function is used to enable the Timer wake-up function
  98. * @param[in] timer The base address of Timer module
  99. * @return None
  100. * @note To wake the system from power down mode, timer clock source must be ether LXT or LIRC
  101. */
  102. __STATIC_INLINE void TIMER_EnableWakeup(TIMER_T *timer)
  103. {
  104. timer->CTL |= TIMER_CTL_WKEN_Msk;
  105. }
  106. /**
  107. * @brief This function is used to disable the Timer wake-up function
  108. * @param[in] timer The base address of Timer module
  109. * @return None
  110. */
  111. __STATIC_INLINE void TIMER_DisableWakeup(TIMER_T *timer)
  112. {
  113. timer->CTL &= ~TIMER_CTL_WKEN_Msk;
  114. }
  115. /**
  116. * @brief This function is used to enable the capture pin detection de-bounce function.
  117. * @param[in] timer The base address of Timer module
  118. * @return None
  119. */
  120. __STATIC_INLINE void TIMER_EnableCaptureDebounce(TIMER_T *timer)
  121. {
  122. timer->EXTCTL |= TIMER_EXTCTL_CAPDBEN_Msk;
  123. }
  124. /**
  125. * @brief This function is used to disable the capture pin detection de-bounce function.
  126. * @param[in] timer The base address of Timer module
  127. * @return None
  128. */
  129. __STATIC_INLINE void TIMER_DisableCaptureDebounce(TIMER_T *timer)
  130. {
  131. timer->EXTCTL &= ~TIMER_EXTCTL_CAPDBEN_Msk;
  132. }
  133. /**
  134. * @brief This function is used to enable the counter pin detection de-bounce function.
  135. * @param[in] timer The base address of Timer module
  136. * @return None
  137. */
  138. __STATIC_INLINE void TIMER_EnableEventCounterDebounce(TIMER_T *timer)
  139. {
  140. timer->EXTCTL |= TIMER_EXTCTL_ECNTDBEN_Msk;
  141. }
  142. /**
  143. * @brief This function is used to disable the counter pin detection de-bounce function.
  144. * @param[in] timer The base address of Timer module
  145. * @return None
  146. */
  147. __STATIC_INLINE void TIMER_DisableEventCounterDebounce(TIMER_T *timer)
  148. {
  149. timer->EXTCTL &= ~TIMER_EXTCTL_ECNTDBEN_Msk;
  150. }
  151. /**
  152. * @brief This function is used to enable the Timer time-out interrupt function.
  153. * @param[in] timer The base address of Timer module
  154. * @return None
  155. */
  156. __STATIC_INLINE void TIMER_EnableInt(TIMER_T *timer)
  157. {
  158. timer->CTL |= TIMER_CTL_INTEN_Msk;
  159. }
  160. /**
  161. * @brief This function is used to disable the Timer time-out interrupt function.
  162. * @param[in] timer The base address of Timer module
  163. * @return None
  164. */
  165. __STATIC_INLINE void TIMER_DisableInt(TIMER_T *timer)
  166. {
  167. timer->CTL &= ~TIMER_CTL_INTEN_Msk;
  168. }
  169. /**
  170. * @brief This function is used to enable the Timer capture trigger interrupt function.
  171. * @param[in] timer The base address of Timer module
  172. * @return None
  173. */
  174. __STATIC_INLINE void TIMER_EnableCaptureInt(TIMER_T *timer)
  175. {
  176. timer->EXTCTL |= TIMER_EXTCTL_CAPIEN_Msk;
  177. }
  178. /**
  179. * @brief This function is used to disable the Timer capture trigger interrupt function.
  180. * @param[in] timer The base address of Timer module
  181. * @return None
  182. */
  183. __STATIC_INLINE void TIMER_DisableCaptureInt(TIMER_T *timer)
  184. {
  185. timer->EXTCTL &= ~TIMER_EXTCTL_CAPIEN_Msk;
  186. }
  187. /**
  188. * @brief This function indicates Timer time-out interrupt occurred or not.
  189. * @param[in] timer The base address of Timer module
  190. * @return Timer time-out interrupt occurred or not
  191. * @retval 0 Timer time-out interrupt did not occur
  192. * @retval 1 Timer time-out interrupt occurred
  193. */
  194. __STATIC_INLINE uint32_t TIMER_GetIntFlag(TIMER_T *timer)
  195. {
  196. return(timer->INTSTS & TIMER_INTSTS_TIF_Msk ? 1 : 0);
  197. }
  198. /**
  199. * @brief This function clears the Timer time-out interrupt flag.
  200. * @param[in] timer The base address of Timer module
  201. * @return None
  202. */
  203. __STATIC_INLINE void TIMER_ClearIntFlag(TIMER_T *timer)
  204. {
  205. timer->INTSTS = TIMER_INTSTS_TIF_Msk;
  206. }
  207. /**
  208. * @brief This function indicates Timer capture interrupt occurred or not.
  209. * @param[in] timer The base address of Timer module
  210. * @return Timer capture interrupt occurred or not
  211. * @retval 0 Timer capture interrupt did not occur
  212. * @retval 1 Timer capture interrupt occurred
  213. */
  214. __STATIC_INLINE uint32_t TIMER_GetCaptureIntFlag(TIMER_T *timer)
  215. {
  216. return timer->EINTSTS;
  217. }
  218. /**
  219. * @brief This function clears the Timer capture interrupt flag.
  220. * @param[in] timer The base address of Timer module
  221. * @return None
  222. */
  223. __STATIC_INLINE void TIMER_ClearCaptureIntFlag(TIMER_T *timer)
  224. {
  225. timer->EINTSTS = TIMER_EINTSTS_CAPIF_Msk;
  226. }
  227. /**
  228. * @brief This function indicates Timer has waked up system or not.
  229. * @param[in] timer The base address of Timer module
  230. * @return Timer has waked up system or not
  231. * @retval 0 Timer did not wake up system
  232. * @retval 1 Timer wake up system
  233. */
  234. __STATIC_INLINE uint32_t TIMER_GetWakeupFlag(TIMER_T *timer)
  235. {
  236. return (timer->INTSTS & TIMER_INTSTS_TWKF_Msk ? 1 : 0);
  237. }
  238. /**
  239. * @brief This function clears the Timer wakeup interrupt flag.
  240. * @param[in] timer The base address of Timer module
  241. * @return None
  242. */
  243. __STATIC_INLINE void TIMER_ClearWakeupFlag(TIMER_T *timer)
  244. {
  245. timer->INTSTS = TIMER_INTSTS_TWKF_Msk;
  246. }
  247. /**
  248. * @brief This function gets the Timer capture data.
  249. * @param[in] timer The base address of Timer module
  250. * @return Timer capture data value
  251. */
  252. __STATIC_INLINE uint32_t TIMER_GetCaptureData(TIMER_T *timer)
  253. {
  254. return timer->CAP;
  255. }
  256. /**
  257. * @brief This function reports the current timer counter value.
  258. * @param[in] timer The base address of Timer module
  259. * @return Timer counter value
  260. */
  261. __STATIC_INLINE uint32_t TIMER_GetCounter(TIMER_T *timer)
  262. {
  263. return timer->CNT;
  264. }
  265. uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq);
  266. void TIMER_Close(TIMER_T *timer);
  267. void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec);
  268. void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge);
  269. void TIMER_DisableCapture(TIMER_T *timer);
  270. void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge);
  271. void TIMER_DisableEventCounter(TIMER_T *timer);
  272. uint32_t TIMER_GetModuleClock(TIMER_T *timer);
  273. /*@}*/ /* end of group NUC472_442_TIMER_EXPORTED_FUNCTIONS */
  274. /*@}*/ /* end of group NUC472_442_TIMER_Driver */
  275. /*@}*/ /* end of group NUC472_442_Device_Driver */
  276. #ifdef __cplusplus
  277. }
  278. #endif
  279. #endif //__TIMER_H__
  280. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/