drv_hwtimer.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-04-17 WangBing the first version.
  9. * 2019-04-22 tyustli add imxrt series support
  10. * 2019-07-15 Magicoe The first version for LPC55S6x
  11. *
  12. */
  13. #include <rtthread.h>
  14. #ifdef BSP_USING_HWTIMER
  15. #define LOG_TAG "drv.hwtimer"
  16. #include <drv_log.h>
  17. #include <rtdevice.h>
  18. #include "drv_hwtimer.h"
  19. #include "fsl_ctimer.h"
  20. static void NVIC_Configuration(void)
  21. {
  22. #ifdef BSP_USING_CTIMER0
  23. EnableIRQ(CTIMER0_IRQn);
  24. #endif
  25. #ifdef BSP_USING_CTIMER1
  26. EnableIRQ(CTIMER1_IRQn);
  27. #endif
  28. #ifdef BSP_USING_CTIMER2
  29. EnableIRQ(CTIMER2_IRQn);
  30. #endif
  31. #ifdef BSP_USING_CTIMER3
  32. EnableIRQ(CTIMER3_IRQn);
  33. #endif
  34. #ifdef BSP_USING_CTIMER4
  35. EnableIRQ(CTIMER4_IRQn);
  36. #endif
  37. }
  38. static rt_err_t lpc_ctimer_control(rt_hwtimer_t *timer, rt_uint32_t cmd, void *args)
  39. {
  40. rt_err_t err = RT_EOK;
  41. CTIMER_Type *hwtimer_dev;
  42. hwtimer_dev = (CTIMER_Type *)timer->parent.user_data;
  43. RT_ASSERT(timer != RT_NULL);
  44. switch (cmd)
  45. {
  46. case HWTIMER_CTRL_FREQ_SET:
  47. {
  48. uint32_t clk;
  49. uint32_t pre;
  50. if(hwtimer_dev == CTIMER0) clk = CLOCK_GetFreq(kCLOCK_CTimer0);
  51. if(hwtimer_dev == CTIMER1) clk = CLOCK_GetFreq(kCLOCK_CTimer1);
  52. if(hwtimer_dev == CTIMER2) clk = CLOCK_GetFreq(kCLOCK_CTimer2);
  53. if(hwtimer_dev == CTIMER3) clk = CLOCK_GetFreq(kCLOCK_CTimer3);
  54. if(hwtimer_dev == CTIMER4) clk = CLOCK_GetFreq(kCLOCK_CTimer4);
  55. pre = clk / *((uint32_t *)args) - 1;
  56. hwtimer_dev->PR = pre;
  57. }
  58. break;
  59. default:
  60. err = -RT_ENOSYS;
  61. break;
  62. }
  63. return err;
  64. }
  65. static rt_uint32_t lpc_ctimer_count_get(rt_hwtimer_t *timer)
  66. {
  67. rt_uint32_t CurrentTimer_Count;
  68. CTIMER_Type *hwtimer_dev;
  69. hwtimer_dev = (CTIMER_Type *)timer->parent.user_data;
  70. RT_ASSERT(timer != RT_NULL);
  71. CurrentTimer_Count = hwtimer_dev->TC;
  72. return CurrentTimer_Count;
  73. }
  74. static void lpc_ctimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
  75. {
  76. CTIMER_Type *hwtimer_dev;
  77. ctimer_config_t cfg;
  78. hwtimer_dev = (CTIMER_Type *)timer->parent.user_data;
  79. RT_ASSERT(timer != RT_NULL);
  80. /* Use Main clock for some of the Ctimers */
  81. if(hwtimer_dev == CTIMER0) CLOCK_AttachClk(kMAIN_CLK_to_CTIMER0);
  82. if(hwtimer_dev == CTIMER1) CLOCK_AttachClk(kMAIN_CLK_to_CTIMER1);
  83. if(hwtimer_dev == CTIMER2) CLOCK_AttachClk(kMAIN_CLK_to_CTIMER2);
  84. if(hwtimer_dev == CTIMER3) CLOCK_AttachClk(kMAIN_CLK_to_CTIMER3);
  85. if(hwtimer_dev == CTIMER4) CLOCK_AttachClk(kMAIN_CLK_to_CTIMER4);
  86. CTIMER_Deinit(hwtimer_dev);
  87. if (state == 1)
  88. {
  89. NVIC_Configuration();
  90. CTIMER_GetDefaultConfig(&cfg);
  91. CTIMER_Init(hwtimer_dev, &cfg);
  92. }
  93. }
  94. static rt_err_t lpc_ctimer_start(rt_hwtimer_t *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode)
  95. {
  96. CTIMER_Type *hwtimer_dev;
  97. hwtimer_dev = (CTIMER_Type *)timer->parent.user_data;
  98. /* Match Configuration for Channel 0 */
  99. ctimer_match_config_t matchCfg;
  100. RT_ASSERT(timer != RT_NULL);
  101. /* Configuration*/
  102. matchCfg.enableCounterReset = true;
  103. matchCfg.enableCounterStop = (mode == HWTIMER_MODE_ONESHOT) ? true : false;;
  104. matchCfg.matchValue = cnt;
  105. matchCfg.outControl = kCTIMER_Output_NoAction;
  106. matchCfg.outPinInitState = false;
  107. matchCfg.enableInterrupt = true;
  108. CTIMER_SetupMatch(hwtimer_dev, kCTIMER_Match_1, &matchCfg);
  109. NVIC_Configuration();
  110. CTIMER_StartTimer(hwtimer_dev);
  111. return RT_EOK;
  112. }
  113. static void lpc_ctimer_stop(rt_hwtimer_t *timer)
  114. {
  115. CTIMER_Type *hwtimer_dev;
  116. hwtimer_dev = (CTIMER_Type *)timer->parent.user_data;
  117. RT_ASSERT(timer != RT_NULL);
  118. CTIMER_StopTimer(hwtimer_dev);
  119. }
  120. static const struct rt_hwtimer_ops lpc_hwtimer_ops =
  121. {
  122. .init = lpc_ctimer_init,
  123. .start = lpc_ctimer_start,
  124. .stop = lpc_ctimer_stop,
  125. .count_get = lpc_ctimer_count_get,
  126. .control = lpc_ctimer_control,
  127. };
  128. static const struct rt_hwtimer_info lpc_hwtimer_info =
  129. {
  130. 25000000, /* the maximum count frequency can be set */
  131. 6103, /* the minimum count frequency can be set */
  132. 0xFFFFFFFF,
  133. HWTIMER_CNTMODE_UP,
  134. };
  135. #ifdef BSP_USING_CTIMER0
  136. static rt_hwtimer_t CTimer0;
  137. #endif /* BSP_USING_HWTIMER0 */
  138. #ifdef BSP_USING_CTIMER1
  139. static rt_hwtimer_t CTimer1;
  140. #endif /* BSP_USING_HWTIMER1 */
  141. #ifdef BSP_USING_CTIMER2
  142. static rt_hwtimer_t CTimer2;
  143. #endif /* BSP_USING_HWTIMER2 */
  144. #ifdef BSP_USING_CTIMER3
  145. static rt_hwtimer_t CTimer3;
  146. #endif /* BSP_USING_HWTIMER3 */
  147. #ifdef BSP_USING_CTIMER4
  148. static rt_hwtimer_t CTimer4;
  149. #endif /* BSP_USING_HWTIMER4 */
  150. int rt_hw_hwtimer_init(void)
  151. {
  152. int ret = RT_EOK;
  153. #ifdef BSP_USING_CTIMER0
  154. CTimer0.info = &lpc_hwtimer_info;
  155. CTimer0.ops = &lpc_hwtimer_ops;
  156. ret = rt_device_hwtimer_register(&CTimer0, "ctimer0", CTIMER0);
  157. if (ret != RT_EOK)
  158. {
  159. LOG_E("CTIMER0 register failed\n");
  160. }
  161. #endif
  162. #ifdef BSP_USING_CTIMER1
  163. CTimer1.info = &lpc_hwtimer_info;
  164. CTimer1.ops = &lpc_hwtimer_ops;
  165. ret = rt_device_hwtimer_register(&CTimer1, "ctimer1", CTIMER1);
  166. if (ret != RT_EOK)
  167. {
  168. LOG_E("CTIMER1 register failed\n");
  169. }
  170. #endif
  171. #ifdef BSP_USING_CTIMER2
  172. CTimer2.info = &lpc_hwtimer_info;
  173. CTimer2.ops = &lpc_hwtimer_ops;
  174. ret = rt_device_hwtimer_register(&CTimer2, "ctimer2", CTIMER2);
  175. if (ret != RT_EOK)
  176. {
  177. LOG_E("CTIMER2 register failed\n");
  178. }
  179. #endif
  180. #ifdef BSP_USING_CTIMER3
  181. CTimer3.info = &lpc_hwtimer_info;
  182. CTimer3.ops = &lpc_hwtimer_ops;
  183. ret = rt_device_hwtimer_register(&CTimer3, "ctimer3", CTIMER3);
  184. if (ret != RT_EOK)
  185. {
  186. LOG_E("CTIMER3 register failed\n");
  187. }
  188. #endif
  189. #ifdef BSP_USING_CTIMER4
  190. CTimer4.info = &lpc_hwtimer_info;
  191. CTimer4.ops = &lpc_hwtimer_ops;
  192. ret = rt_device_hwtimer_register(&CTimer4, "ctimer4", CTIMER4);
  193. if (ret != RT_EOK)
  194. {
  195. LOG_E("CTIMER4 register failed\n");
  196. }
  197. #endif
  198. return ret;
  199. }
  200. INIT_DEVICE_EXPORT(rt_hw_hwtimer_init);
  201. #ifdef BSP_USING_CTIMER0
  202. void CTIMER0_IRQHandler(void)
  203. {
  204. uint32_t int_stat;
  205. /* Get Interrupt status flags */
  206. int_stat = CTIMER_GetStatusFlags(CTIMER0);
  207. /* Clear the status flags that were set */
  208. CTIMER_ClearStatusFlags(CTIMER0, int_stat);
  209. rt_device_hwtimer_isr(&CTimer0);
  210. }
  211. #endif /* BSP_USING_HWTIMER0 */
  212. #ifdef BSP_USING_CTIMER1
  213. void CTIMER1_IRQHandler(void)
  214. {
  215. uint32_t int_stat;
  216. /* Get Interrupt status flags */
  217. int_stat = CTIMER_GetStatusFlags(CTIMER1);
  218. /* Clear the status flags that were set */
  219. CTIMER_ClearStatusFlags(CTIMER1, int_stat);
  220. rt_device_hwtimer_isr(&CTimer1);
  221. }
  222. #endif /* BSP_USING_HWTIMER1 */
  223. #ifdef BSP_USING_CTIMER2
  224. void CTIMER2_IRQHandler(void)
  225. {
  226. uint32_t int_stat;
  227. /* Get Interrupt status flags */
  228. int_stat = CTIMER_GetStatusFlags(CTIMER2);
  229. /* Clear the status flags that were set */
  230. CTIMER_ClearStatusFlags(CTIMER2, int_stat);
  231. rt_device_hwtimer_isr(&CTimer2);
  232. }
  233. #endif /* BSP_USING_HWTIMER2 */
  234. #ifdef BSP_USING_CTIMER3
  235. void CTIMER3_IRQHandler(void)
  236. {
  237. uint32_t int_stat;
  238. /* Get Interrupt status flags */
  239. int_stat = CTIMER_GetStatusFlags(CTIMER3);
  240. /* Clear the status flags that were set */
  241. CTIMER_ClearStatusFlags(CTIMER3, int_stat);
  242. rt_device_hwtimer_isr(&CTimer3);
  243. }
  244. #endif /* BSP_USING_HWTIMER3 */
  245. #ifdef BSP_USING_CTIMER4
  246. void CTIMER4_IRQHandler(void)
  247. {
  248. uint32_t int_stat;
  249. /* Get Interrupt status flags */
  250. int_stat = CTIMER_GetStatusFlags(CTIMER4);
  251. /* Clear the status flags that were set */
  252. CTIMER_ClearStatusFlags(CTIMER4, int_stat);
  253. rt_device_hwtimer_isr(&CTimer4);
  254. }
  255. #endif /* BSP_USING_HWTIMER4 */
  256. #endif /* BSP_USING_HWTIMER */