timer.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**************************************************************************//**
  2. * @file timer.c
  3. * @version V3.00
  4. * $Revision: 6 $
  5. * $Date: 14/01/28 4:19p $
  6. * @brief M051 series Timer driver source file
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include "M051Series.h"
  12. /** @addtogroup M051_Device_Driver M051 Device Driver
  13. @{
  14. */
  15. /** @addtogroup M051_TIMER_Driver TIMER Driver
  16. @{
  17. */
  18. /** @addtogroup M051_TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions
  19. @{
  20. */
  21. /**
  22. * @brief Open Timer in specified mode and frequency
  23. *
  24. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  25. * @param[in] u32Mode Operation mode. Possible options are
  26. * - \ref TIMER_ONESHOT_MODE
  27. * - \ref TIMER_PERIODIC_MODE
  28. * - \ref TIMER_TOGGLE_MODE
  29. * - \ref TIMER_CONTINUOUS_MODE
  30. * @param[in] u32Freq Target working frequency
  31. *
  32. * @return Real Timer working frequency
  33. *
  34. * @details This API is used to configure timer to operate in specified mode and frequency.
  35. * If timer cannot work in target frequency, a closest frequency will be chose and returned.
  36. * @note After calling this API, Timer is \b NOT running yet. But could start timer running be calling
  37. * \ref TIMER_Start macro or program registers directly.
  38. */
  39. uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq)
  40. {
  41. uint32_t u32Clk = TIMER_GetModuleClock(timer);
  42. uint32_t u32Cmpr = 0, u32Prescale = 0;
  43. // Fastest possible timer working freq is (u32Clk / 2). While cmpr = 2, pre-scale = 0.
  44. if(u32Freq > (u32Clk / 2))
  45. {
  46. u32Cmpr = 2;
  47. }
  48. else
  49. {
  50. if(u32Clk >= 0x4000000)
  51. {
  52. u32Prescale = 7; // real prescaler value is 8
  53. u32Clk >>= 3;
  54. }
  55. else if(u32Clk >= 0x2000000)
  56. {
  57. u32Prescale = 3; // real prescaler value is 4
  58. u32Clk >>= 2;
  59. }
  60. else if(u32Clk >= 0x1000000)
  61. {
  62. u32Prescale = 1; // real prescaler value is 2
  63. u32Clk >>= 1;
  64. }
  65. u32Cmpr = u32Clk / u32Freq;
  66. }
  67. timer->TCSR = u32Mode | u32Prescale;
  68. timer->TCMPR = u32Cmpr;
  69. return(u32Clk / (u32Cmpr * (u32Prescale + 1)));
  70. }
  71. /**
  72. * @brief Stop Timer Counting
  73. *
  74. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  75. *
  76. * @return None
  77. *
  78. * @details This API stops Timer counting and disable the Timer interrupt function.
  79. */
  80. void TIMER_Close(TIMER_T *timer)
  81. {
  82. timer->TCSR = 0;
  83. timer->TEXCON = 0;
  84. }
  85. /**
  86. * @brief Open Timer in specified mode and frequency
  87. *
  88. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  89. * @param[in] u32Usec Delay period in micro seconds. Valid values are between 100~1000000 (100 micro second ~ 1 second).
  90. *
  91. * @return None
  92. *
  93. * @details This API is used to create a delay loop for u32usec micro seconds.
  94. * @note This API overwrites the register setting of the timer used to count the delay time.
  95. * @note This API use polling mode. So there is no need to enable interrupt for the timer module used to generate delay.
  96. */
  97. void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec)
  98. {
  99. uint32_t u32Clk = TIMER_GetModuleClock(timer);
  100. uint32_t u32Prescale = 0, delay = SystemCoreClock / u32Clk;
  101. uint32_t u32Cmpr, u32NsecPerTick;
  102. // Clear current timer configuration/
  103. timer->TCSR = 0;
  104. timer->TEXCON = 0;
  105. if (u32Clk <= 1000000) // min delay is 1000 us if timer clock source is <= 1 MHz
  106. {
  107. if (u32Usec < 1000)
  108. u32Usec = 1000;
  109. if (u32Usec > 1000000)
  110. u32Usec = 1000000;
  111. }
  112. else
  113. {
  114. if (u32Usec < 100)
  115. u32Usec = 100;
  116. if (u32Usec > 1000000)
  117. u32Usec = 1000000;
  118. }
  119. if (u32Clk <= 1000000)
  120. {
  121. u32Prescale = 0;
  122. u32NsecPerTick = 1000000000 / u32Clk;
  123. u32Cmpr = (u32Usec * 1000) / u32NsecPerTick;
  124. }
  125. else
  126. {
  127. if (u32Clk > 64000000)
  128. {
  129. u32Prescale = 7; // real prescaler value is 8
  130. u32Clk >>= 3;
  131. }
  132. else if (u32Clk > 32000000)
  133. {
  134. u32Prescale = 3; // real prescaler value is 4
  135. u32Clk >>= 2;
  136. }
  137. else if (u32Clk > 16000000)
  138. {
  139. u32Prescale = 1; // real prescaler value is 2
  140. u32Clk >>= 1;
  141. }
  142. if (u32Usec < 250)
  143. {
  144. u32Cmpr = (u32Usec * u32Clk) / 1000000;
  145. }
  146. else
  147. {
  148. u32NsecPerTick = 1000000000 / u32Clk;
  149. u32Cmpr = (u32Usec * 1000) / u32NsecPerTick;
  150. }
  151. }
  152. timer->TCMPR = u32Cmpr;
  153. timer->TCSR = TIMER_TCSR_CEN_Msk | (u32Prescale - 1); // one shot mode
  154. // When system clock is faster than timer clock, it is possible timer active bit cannot set in time while we check it.
  155. // And the while loop below return immediately, so put a tiny delay here allowing timer start counting and raise active flag.
  156. for(; delay > 0; delay--)
  157. {
  158. __NOP();
  159. }
  160. while(timer->TCSR & TIMER_TCSR_CACT_Msk);
  161. }
  162. /**
  163. * @brief Enable Timer Capture Function
  164. *
  165. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  166. * @param[in] u32CapMode Timer capture mode. Could be
  167. * - \ref TIMER_CAPTURE_FREE_COUNTING_MODE
  168. * - \ref TIMER_CAPTURE_COUNTER_RESET_MODE
  169. * @param[in] u32Edge Timer capture edge. Possible values are
  170. * - \ref TIMER_CAPTURE_FALLING_EDGE
  171. * - \ref TIMER_CAPTURE_RISING_EDGE
  172. * - \ref TIMER_CAPTURE_FALLING_AND_RISING_EDGE
  173. *
  174. * @return None
  175. *
  176. * @details This API is used to enable timer capture function with specified mode and capture edge.
  177. * @note Timer frequency should be configured separately by using \ref TIMER_Open API, or program registers directly.
  178. */
  179. void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge)
  180. {
  181. timer->TEXCON = (timer->TEXCON & ~(TIMER_TEXCON_RSTCAPSEL_Msk |
  182. TIMER_TEXCON_TEX_EDGE_Msk)) |
  183. u32CapMode | u32Edge | TIMER_TEXCON_TEXEN_Msk;
  184. }
  185. /**
  186. * @brief Disable Timer Capture Function
  187. *
  188. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  189. *
  190. * @return None
  191. *
  192. * @details This API is used to disable the Timer capture function.
  193. */
  194. void TIMER_DisableCapture(TIMER_T *timer)
  195. {
  196. timer->TEXCON &= ~TIMER_TEXCON_TEXEN_Msk;
  197. }
  198. /**
  199. * @brief Enable Timer Counter Function
  200. *
  201. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  202. * @param[in] u32Edge Detection edge of counter pin. Could be ether
  203. * - \ref TIMER_COUNTER_FALLING_EDGE, or
  204. * - \ref TIMER_COUNTER_RISING_EDGE
  205. *
  206. * @return None
  207. *
  208. * @details This function is used to enable the Timer counter function with specify detection edge.
  209. * @note Timer compare value should be configured separately by using \ref TIMER_SET_CMP_VALUE macro or program registers directly.
  210. * @note While using event counter function, \ref TIMER_TOGGLE_MODE cannot set as timer operation mode.
  211. */
  212. void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge)
  213. {
  214. timer->TEXCON = (timer->TEXCON & ~TIMER_TEXCON_TX_PHASE_Msk) | u32Edge;
  215. timer->TCSR |= TIMER_TCSR_CTB_Msk;
  216. }
  217. /**
  218. * @brief Disable Timer Counter Function
  219. *
  220. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  221. *
  222. * @return None
  223. *
  224. * @details This API is used to disable the Timer event counter function.
  225. */
  226. void TIMER_DisableEventCounter(TIMER_T *timer)
  227. {
  228. timer->TCSR &= ~TIMER_TCSR_CTB_Msk;
  229. }
  230. /**
  231. * @brief Get Timer Clock Frequency
  232. *
  233. * @param[in] timer The base address of Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
  234. *
  235. * @return Timer clock frequency
  236. *
  237. * @details This API is used to get the clock frequency of Timer.
  238. * @note This API cannot return correct clock rate if timer source is external clock input.
  239. */
  240. uint32_t TIMER_GetModuleClock(TIMER_T *timer)
  241. {
  242. uint32_t u32Src;
  243. const uint32_t au32Clk[] = {__HXT, 0, 0, 0, 0, __LIRC, 0, __HIRC};
  244. if(timer == TIMER0)
  245. u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR0_S_Msk) >> CLK_CLKSEL1_TMR0_S_Pos;
  246. else if(timer == TIMER1)
  247. u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR1_S_Msk) >> CLK_CLKSEL1_TMR1_S_Pos;
  248. else if(timer == TIMER2)
  249. u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR2_S_Msk) >> CLK_CLKSEL1_TMR2_S_Pos;
  250. else // Timer 3
  251. u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR3_S_Msk) >> CLK_CLKSEL1_TMR3_S_Pos;
  252. if(u32Src == 2)
  253. {
  254. return(SystemCoreClock);
  255. }
  256. return(au32Clk[u32Src]);
  257. }
  258. /*@}*/ /* end of group M051_TIMER_EXPORTED_FUNCTIONS */
  259. /*@}*/ /* end of group M051_TIMER_Driver */
  260. /*@}*/ /* end of group M051_Device_Driver */
  261. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/