timer.c 12 KB

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