nu_timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**************************************************************************//**
  2. * @file timer.c
  3. * @brief M480 Timer Controller(Timer) driver source file
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #include "NuMicro.h"
  9. /** @addtogroup Standard_Driver Standard Driver
  10. @{
  11. */
  12. /** @addtogroup TIMER_Driver TIMER Driver
  13. @{
  14. */
  15. /** @addtogroup TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions
  16. @{
  17. */
  18. /**
  19. * @brief Open Timer with Operate Mode and Frequency
  20. *
  21. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  22. * @param[in] u32Mode Operation mode. Possible options are
  23. * - \ref TIMER_ONESHOT_MODE
  24. * - \ref TIMER_PERIODIC_MODE
  25. * - \ref TIMER_TOGGLE_MODE
  26. * - \ref TIMER_CONTINUOUS_MODE
  27. * @param[in] u32Freq Target working frequency
  28. *
  29. * @return Real timer working frequency
  30. *
  31. * @details This API is used to configure timer to operate in specified mode and frequency.
  32. * If timer cannot work in target frequency, a closest frequency will be chose and returned.
  33. * @note After calling this API, Timer is \b NOT running yet. But could start timer running be calling
  34. * \ref TIMER_Start macro or program registers directly.
  35. */
  36. uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)
  37. {
  38. uint32_t u32Clk = TIMER_GetModuleClock(timer);
  39. uint32_t u32Cmpr = 0UL, u32Prescale = 0UL;
  40. /* Fastest possible timer working freq is (u32Clk / 2). While cmpr = 2, prescaler = 0. */
  41. if(u32Freq > (u32Clk / 2UL))
  42. {
  43. u32Cmpr = 2UL;
  44. }
  45. else
  46. {
  47. u32Cmpr = u32Clk / u32Freq;
  48. u32Prescale = (u32Cmpr >> 24); /* for 24 bits CMPDAT */
  49. if (u32Prescale > 0UL)
  50. u32Cmpr = u32Cmpr / (u32Prescale + 1UL);
  51. }
  52. timer->CTL = u32Mode | u32Prescale;
  53. timer->CMP = u32Cmpr;
  54. return(u32Clk / (u32Cmpr * (u32Prescale + 1UL)));
  55. }
  56. /**
  57. * @brief Stop Timer Counting
  58. *
  59. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  60. *
  61. * @return None
  62. *
  63. * @details This API stops timer counting and disable all timer interrupt function.
  64. */
  65. void TIMER_Close(TIMER_T *timer)
  66. {
  67. timer->CTL = 0UL;
  68. timer->EXTCTL = 0UL;
  69. }
  70. /**
  71. * @brief Create a specify Delay Time
  72. *
  73. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  74. * @param[in] u32Usec Delay period in micro seconds. Valid values are between 100~1000000 (100 micro second ~ 1 second).
  75. *
  76. * @return None
  77. *
  78. * @details This API is used to create a delay loop for u32usec micro seconds by using timer one-shot mode.
  79. * @note This API overwrites the register setting of the timer used to count the delay time.
  80. * @note This API use polling mode. So there is no need to enable interrupt for the timer module used to generate delay.
  81. */
  82. void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec)
  83. {
  84. uint32_t u32Clk = TIMER_GetModuleClock(timer);
  85. uint32_t u32Prescale = 0UL, delay = (SystemCoreClock / u32Clk) + 1UL;
  86. uint32_t u32Cmpr, u32NsecPerTick;
  87. /* Clear current timer configuration */
  88. timer->CTL = 0UL;
  89. timer->EXTCTL = 0UL;
  90. if(u32Clk <= 1000000UL) /* min delay is 1000 us if timer clock source is <= 1 MHz */
  91. {
  92. if(u32Usec < 1000UL)
  93. {
  94. u32Usec = 1000UL;
  95. }
  96. if(u32Usec > 1000000UL)
  97. {
  98. u32Usec = 1000000UL;
  99. }
  100. }
  101. else
  102. {
  103. if(u32Usec < 100UL)
  104. {
  105. u32Usec = 100UL;
  106. }
  107. if(u32Usec > 1000000UL)
  108. {
  109. u32Usec = 1000000UL;
  110. }
  111. }
  112. if(u32Clk <= 1000000UL)
  113. {
  114. u32Prescale = 0UL;
  115. u32NsecPerTick = 1000000000UL / u32Clk;
  116. u32Cmpr = (u32Usec * 1000UL) / u32NsecPerTick;
  117. }
  118. else
  119. {
  120. u32Cmpr = u32Usec * (u32Clk / 1000000UL);
  121. u32Prescale = (u32Cmpr >> 24); /* for 24 bits CMPDAT */
  122. if (u32Prescale > 0UL)
  123. u32Cmpr = u32Cmpr / (u32Prescale + 1UL);
  124. }
  125. timer->CMP = u32Cmpr;
  126. timer->CTL = TIMER_CTL_CNTEN_Msk | TIMER_ONESHOT_MODE | u32Prescale;
  127. /* When system clock is faster than timer clock, it is possible timer active bit cannot set in time while we check it.
  128. And the while loop below return immediately, so put a tiny delay here allowing timer start counting and raise active flag. */
  129. for(; delay > 0UL; delay--)
  130. {
  131. __NOP();
  132. }
  133. while(timer->CTL & TIMER_CTL_ACTSTS_Msk)
  134. {
  135. ;
  136. }
  137. }
  138. /**
  139. * @brief Enable Timer Capture Function
  140. *
  141. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  142. * @param[in] u32CapMode Timer capture mode. Could be
  143. * - \ref TIMER_CAPTURE_FREE_COUNTING_MODE
  144. * - \ref TIMER_CAPTURE_COUNTER_RESET_MODE
  145. * @param[in] u32Edge Timer capture trigger edge. Possible values are
  146. * - \ref TIMER_CAPTURE_EVENT_FALLING
  147. * - \ref TIMER_CAPTURE_EVENT_RISING
  148. * - \ref TIMER_CAPTURE_EVENT_FALLING_RISING
  149. * - \ref TIMER_CAPTURE_EVENT_RISING_FALLING
  150. *
  151. * @return None
  152. *
  153. * @details This API is used to enable timer capture function with specify capture trigger edge \n
  154. * to get current counter value or reset counter value to 0.
  155. * @note Timer frequency should be configured separately by using \ref TIMER_Open API, or program registers directly.
  156. */
  157. void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge)
  158. {
  159. timer->EXTCTL = (timer->EXTCTL & ~(TIMER_EXTCTL_CAPFUNCS_Msk | TIMER_EXTCTL_CAPEDGE_Msk)) |
  160. u32CapMode | u32Edge | TIMER_EXTCTL_CAPEN_Msk;
  161. }
  162. /**
  163. * @brief Disable Timer Capture Function
  164. *
  165. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  166. *
  167. * @return None
  168. *
  169. * @details This API is used to disable the timer capture function.
  170. */
  171. void TIMER_DisableCapture(TIMER_T *timer)
  172. {
  173. timer->EXTCTL &= ~TIMER_EXTCTL_CAPEN_Msk;
  174. }
  175. /**
  176. * @brief Enable Timer Counter Function
  177. *
  178. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  179. * @param[in] u32Edge Detection edge of counter pin. Could be ether
  180. * - \ref TIMER_COUNTER_EVENT_FALLING, or
  181. * - \ref TIMER_COUNTER_EVENT_RISING
  182. *
  183. * @return None
  184. *
  185. * @details This function is used to enable the timer counter function with specify detection edge.
  186. * @note Timer compare value should be configured separately by using \ref TIMER_SET_CMP_VALUE macro or program registers directly.
  187. * @note While using event counter function, \ref TIMER_TOGGLE_MODE cannot set as timer operation mode.
  188. */
  189. void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge)
  190. {
  191. timer->EXTCTL = (timer->EXTCTL & ~TIMER_EXTCTL_CNTPHASE_Msk) | u32Edge;
  192. timer->CTL |= TIMER_CTL_EXTCNTEN_Msk;
  193. }
  194. /**
  195. * @brief Disable Timer Counter Function
  196. *
  197. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  198. *
  199. * @return None
  200. *
  201. * @details This API is used to disable the timer event counter function.
  202. */
  203. void TIMER_DisableEventCounter(TIMER_T *timer)
  204. {
  205. timer->CTL &= ~TIMER_CTL_EXTCNTEN_Msk;
  206. }
  207. /**
  208. * @brief Get Timer Clock Frequency
  209. *
  210. * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  211. *
  212. * @return Timer clock frequency
  213. *
  214. * @details This API is used to get the timer clock frequency.
  215. * @note This API cannot return correct clock rate if timer source is from external clock input.
  216. */
  217. uint32_t TIMER_GetModuleClock(TIMER_T *timer)
  218. {
  219. uint32_t u32Src, u32Clk;
  220. const uint32_t au32Clk[] = {__HXT, __LXT, 0UL, 0UL, 0UL, __LIRC, 0UL, __HIRC};
  221. if(timer == TIMER0)
  222. {
  223. u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR0SEL_Msk) >> CLK_CLKSEL1_TMR0SEL_Pos;
  224. }
  225. else if(timer == TIMER1)
  226. {
  227. u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR1SEL_Msk) >> CLK_CLKSEL1_TMR1SEL_Pos;
  228. }
  229. else if(timer == TIMER2)
  230. {
  231. u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR2SEL_Msk) >> CLK_CLKSEL1_TMR2SEL_Pos;
  232. }
  233. else /* Timer 3 */
  234. {
  235. u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR3SEL_Msk) >> CLK_CLKSEL1_TMR3SEL_Pos;
  236. }
  237. if(u32Src == 2UL)
  238. {
  239. if((timer == TIMER0) || (timer == TIMER1))
  240. {
  241. u32Clk = CLK_GetPCLK0Freq();
  242. }
  243. else
  244. {
  245. u32Clk = CLK_GetPCLK1Freq();
  246. }
  247. }
  248. else
  249. {
  250. u32Clk = au32Clk[u32Src];
  251. }
  252. return u32Clk;
  253. }
  254. /**
  255. * @brief This function is used to enable the Timer frequency counter function
  256. * @param[in] timer The base address of Timer module. Can be \ref TIMER0 or \ref TIMER2
  257. * @param[in] u32DropCount This parameter has no effect in M480 series BSP
  258. * @param[in] u32Timeout This parameter has no effect in M480 series BSP
  259. * @param[in] u32EnableInt Enable interrupt assertion after capture complete or not. Valid values are TRUE and FALSE
  260. * @return None
  261. * @details This function is used to calculate input event frequency. After enable
  262. * this function, a pair of timers, TIMER0 and TIMER1, or TIMER2 and TIMER3
  263. * will be configured for this function. The mode used to calculate input
  264. * event frequency is mentioned as "Inter Timer Trigger Mode" in Technical
  265. * Reference Manual
  266. */
  267. void TIMER_EnableFreqCounter(TIMER_T *timer,
  268. uint32_t u32DropCount,
  269. uint32_t u32Timeout,
  270. uint32_t u32EnableInt)
  271. {
  272. TIMER_T *t; /* store the timer base to configure compare value */
  273. t = (timer == TIMER0) ? TIMER1 : TIMER3;
  274. t->CMP = 0xFFFFFFUL;
  275. t->EXTCTL = u32EnableInt ? TIMER_EXTCTL_CAPIEN_Msk : 0UL;
  276. timer->CTL = TIMER_CTL_INTRGEN_Msk | TIMER_CTL_CNTEN_Msk;
  277. return;
  278. }
  279. /**
  280. * @brief This function is used to disable the Timer frequency counter function.
  281. * @param[in] timer The base address of Timer module
  282. * @return None
  283. */
  284. void TIMER_DisableFreqCounter(TIMER_T *timer)
  285. {
  286. timer->CTL &= ~TIMER_CTL_INTRGEN_Msk;
  287. }
  288. /**
  289. * @brief This function is used to select the interrupt source used to trigger other modules.
  290. * @param[in] timer The base address of Timer module
  291. * @param[in] u32Src Selects the interrupt source to trigger other modules. Could be:
  292. * - \ref TIMER_TRGSRC_TIMEOUT_EVENT
  293. * - \ref TIMER_TRGSRC_CAPTURE_EVENT
  294. * @return None
  295. */
  296. void TIMER_SetTriggerSource(TIMER_T *timer, uint32_t u32Src)
  297. {
  298. timer->TRGCTL = (timer->TRGCTL & ~TIMER_TRGCTL_TRGSSEL_Msk) | u32Src;
  299. }
  300. /**
  301. * @brief This function is used to set modules trigger by timer interrupt
  302. * @param[in] timer The base address of Timer module
  303. * @param[in] u32Mask The mask of modules (EPWM, EADC, DAC and PDMA) trigger by timer. Is the combination of
  304. * - \ref TIMER_TRG_TO_EPWM,
  305. * - \ref TIMER_TRG_TO_EADC,
  306. * - \ref TIMER_TRG_TO_DAC, and
  307. * - \ref TIMER_TRG_TO_PDMA
  308. * @return None
  309. */
  310. void TIMER_SetTriggerTarget(TIMER_T *timer, uint32_t u32Mask)
  311. {
  312. timer->TRGCTL = (timer->TRGCTL & ~(TIMER_TRGCTL_TRGEPWM_Msk | TIMER_TRGCTL_TRGDAC_Msk | TIMER_TRGCTL_TRGEADC_Msk | TIMER_TRGCTL_TRGPDMA_Msk)) | u32Mask;
  313. }
  314. /*@}*/ /* end of group TIMER_EXPORTED_FUNCTIONS */
  315. /*@}*/ /* end of group TIMER_Driver */
  316. /*@}*/ /* end of group Standard_Driver */