drv_hwtimer.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-11-26 hywing the first version.
  9. *
  10. */
  11. #include <rtthread.h>
  12. #ifdef BSP_USING_HWTIMER
  13. #define LOG_TAG "drv.hwtimer"
  14. #include <drv_log.h>
  15. #include <rtdevice.h>
  16. #include "fsl_ctimer.h"
  17. enum
  18. {
  19. #ifdef BSP_USING_CTIMER0
  20. TIM0_INDEX,
  21. #endif
  22. #ifdef BSP_USING_CTIMER1
  23. TIM1_INDEX,
  24. #endif
  25. #ifdef BSP_USING_CTIMER2
  26. TIM2_INDEX,
  27. #endif
  28. };
  29. #ifdef BSP_USING_CTIMER0
  30. #define TIM0_CONFIG \
  31. { \
  32. .tim_handle = CTIMER0, \
  33. .tim_irqn = CTIMER0_IRQn, \
  34. .name = "timer0", \
  35. }
  36. #endif /* TIM0_CONFIG */
  37. #ifdef BSP_USING_CTIMER1
  38. #define TIM1_CONFIG \
  39. { \
  40. .tim_handle = CTIMER1, \
  41. .tim_irqn = CTIMER1_IRQn, \
  42. .name = "timer1", \
  43. }
  44. #endif /* TIM1_CONFIG */
  45. #ifdef BSP_USING_CTIMER2
  46. #define TIM2_CONFIG \
  47. { \
  48. .tim_handle = CTIMER2, \
  49. .tim_irqn = CTIMER2_IRQn, \
  50. .name = "timer2", \
  51. }
  52. #endif /* TIM2_CONFIG */
  53. struct mcxa_hwtimer
  54. {
  55. rt_hwtimer_t time_device;
  56. CTIMER_Type* tim_handle;
  57. enum IRQn tim_irqn;
  58. char* name;
  59. };
  60. static struct mcxa_hwtimer mcxa_hwtimer_obj[] =
  61. {
  62. #ifdef BSP_USING_CTIMER0
  63. TIM0_CONFIG,
  64. #endif
  65. #ifdef BSP_USING_CTIMER1
  66. TIM1_CONFIG,
  67. #endif
  68. #ifdef BSP_USING_CTIMER2
  69. TIM2_CONFIG,
  70. #endif
  71. };
  72. static void NVIC_Configuration(void)
  73. {
  74. #ifdef BSP_USING_CTIMER0
  75. EnableIRQ(CTIMER0_IRQn);
  76. #endif
  77. #ifdef BSP_USING_CTIMER1
  78. EnableIRQ(CTIMER1_IRQn);
  79. #endif
  80. #ifdef BSP_USING_CTIMER2
  81. EnableIRQ(CTIMER2_IRQn);
  82. #endif
  83. }
  84. static rt_err_t mcxa_ctimer_control(rt_hwtimer_t *timer, rt_uint32_t cmd, void *args)
  85. {
  86. rt_err_t err = RT_EOK;
  87. CTIMER_Type *hwtimer_dev;
  88. hwtimer_dev = (CTIMER_Type *)timer->parent.user_data;
  89. RT_ASSERT(timer != RT_NULL);
  90. switch (cmd)
  91. {
  92. case HWTIMER_CTRL_FREQ_SET:
  93. {
  94. uint32_t clk;
  95. uint32_t pre;
  96. if(hwtimer_dev == CTIMER0) clk = CLOCK_GetCTimerClkFreq(0U);
  97. if(hwtimer_dev == CTIMER1) clk = CLOCK_GetCTimerClkFreq(1U);
  98. if(hwtimer_dev == CTIMER2) clk = CLOCK_GetCTimerClkFreq(2U);
  99. pre = clk / *((uint32_t *)args) - 1;
  100. hwtimer_dev->PR = pre;
  101. }
  102. break;
  103. default:
  104. err = -RT_ENOSYS;
  105. break;
  106. }
  107. return err;
  108. }
  109. static rt_uint32_t mcxa_ctimer_count_get(rt_hwtimer_t *timer)
  110. {
  111. rt_uint32_t CurrentTimer_Count;
  112. CTIMER_Type *hwtimer_dev;
  113. hwtimer_dev = (CTIMER_Type *)timer->parent.user_data;
  114. RT_ASSERT(timer != RT_NULL);
  115. CurrentTimer_Count = hwtimer_dev->TC;
  116. return CurrentTimer_Count;
  117. }
  118. static void mcxa_ctimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
  119. {
  120. CTIMER_Type *hwtimer_dev;
  121. ctimer_config_t cfg;
  122. hwtimer_dev = (CTIMER_Type *)timer->parent.user_data;
  123. RT_ASSERT(timer != RT_NULL);
  124. /* Use Main clock for some of the Ctimers */
  125. if(hwtimer_dev == CTIMER0) CLOCK_AttachClk(kFRO_HF_to_CTIMER0);
  126. if(hwtimer_dev == CTIMER1) CLOCK_AttachClk(kFRO_HF_to_CTIMER1);
  127. if(hwtimer_dev == CTIMER2) CLOCK_AttachClk(kFRO_HF_to_CTIMER2);
  128. CTIMER_Init(hwtimer_dev, &cfg);
  129. if (state == 1)
  130. {
  131. NVIC_Configuration();
  132. CTIMER_GetDefaultConfig(&cfg);
  133. CTIMER_Init(hwtimer_dev, &cfg);
  134. }
  135. }
  136. static rt_err_t mcxa_ctimer_start(rt_hwtimer_t *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode)
  137. {
  138. CTIMER_Type *hwtimer_dev;
  139. hwtimer_dev = (CTIMER_Type *)timer->parent.user_data;
  140. /* Match Configuration for Channel 0 */
  141. ctimer_match_config_t matchCfg;
  142. RT_ASSERT(timer != RT_NULL);
  143. /* Configuration*/
  144. matchCfg.enableCounterReset = true;
  145. matchCfg.enableCounterStop = (mode == HWTIMER_MODE_ONESHOT) ? true : false;;
  146. matchCfg.matchValue = cnt;
  147. matchCfg.outControl = kCTIMER_Output_NoAction;
  148. matchCfg.outPinInitState = false;
  149. matchCfg.enableInterrupt = true;
  150. CTIMER_SetupMatch(hwtimer_dev, kCTIMER_Match_1, &matchCfg);
  151. NVIC_Configuration();
  152. CTIMER_StartTimer(hwtimer_dev);
  153. return RT_EOK;
  154. }
  155. static void mcxa_ctimer_stop(rt_hwtimer_t *timer)
  156. {
  157. CTIMER_Type *hwtimer_dev;
  158. hwtimer_dev = (CTIMER_Type *)timer->parent.user_data;
  159. RT_ASSERT(timer != RT_NULL);
  160. CTIMER_StopTimer(hwtimer_dev);
  161. }
  162. static const struct rt_hwtimer_ops mcxa_hwtimer_ops =
  163. {
  164. .init = mcxa_ctimer_init,
  165. .start = mcxa_ctimer_start,
  166. .stop = mcxa_ctimer_stop,
  167. .count_get = mcxa_ctimer_count_get,
  168. .control = mcxa_ctimer_control,
  169. };
  170. static const struct rt_hwtimer_info mcxa_hwtimer_info =
  171. {
  172. 96000000, /* the maximum count frequency can be set */
  173. 6103, /* the minimum count frequency can be set */
  174. 0xFFFFFFFF,
  175. HWTIMER_CNTMODE_UP,
  176. };
  177. int rt_hw_hwtimer_init(void)
  178. {
  179. int i = 0;
  180. int result = RT_EOK;
  181. for (i = 0; i < sizeof(mcxa_hwtimer_obj) / sizeof(mcxa_hwtimer_obj[0]); i++)
  182. {
  183. mcxa_hwtimer_obj[i].time_device.info = &mcxa_hwtimer_info;
  184. mcxa_hwtimer_obj[i].time_device.ops = &mcxa_hwtimer_ops;
  185. if (rt_device_hwtimer_register(&mcxa_hwtimer_obj[i].time_device,
  186. mcxa_hwtimer_obj[i].name, mcxa_hwtimer_obj[i].tim_handle) == RT_EOK)
  187. {
  188. LOG_D("%s register success", mcxa_hwtimer_obj[i].name);
  189. }
  190. else
  191. {
  192. LOG_E("%s register failed", mcxa_hwtimer_obj[i].name);
  193. result = -RT_ERROR;
  194. }
  195. }
  196. return result;
  197. }
  198. INIT_DEVICE_EXPORT(rt_hw_hwtimer_init);
  199. #ifdef BSP_USING_CTIMER0
  200. void CTIMER0_IRQHandler(void)
  201. {
  202. rt_interrupt_enter();
  203. uint32_t int_stat;
  204. /* Get Interrupt status flags */
  205. int_stat = CTIMER_GetStatusFlags(CTIMER0);
  206. /* Clear the status flags that were set */
  207. CTIMER_ClearStatusFlags(CTIMER0, int_stat);
  208. rt_device_hwtimer_isr(&mcxa_hwtimer_obj[TIM0_INDEX].time_device);
  209. rt_interrupt_leave();
  210. }
  211. #endif /* BSP_USING_HWTIMER0 */
  212. #ifdef BSP_USING_CTIMER1
  213. void CTIMER1_IRQHandler(void)
  214. {
  215. rt_interrupt_enter();
  216. uint32_t int_stat;
  217. /* Get Interrupt status flags */
  218. int_stat = CTIMER_GetStatusFlags(CTIMER1);
  219. /* Clear the status flags that were set */
  220. CTIMER_ClearStatusFlags(CTIMER1, int_stat);
  221. rt_device_hwtimer_isr(&mcxa_hwtimer_obj[TIM1_INDEX].time_device);
  222. rt_interrupt_leave();
  223. }
  224. #endif /* BSP_USING_HWTIMER1 */
  225. #ifdef BSP_USING_CTIMER2
  226. void CTIMER2_IRQHandler(void)
  227. {
  228. rt_interrupt_enter();
  229. uint32_t int_stat;
  230. /* Get Interrupt status flags */
  231. int_stat = CTIMER_GetStatusFlags(CTIMER2);
  232. /* Clear the status flags that were set */
  233. CTIMER_ClearStatusFlags(CTIMER2, int_stat);
  234. rt_device_hwtimer_isr(&mcxa_hwtimer_obj[TIM2_INDEX].time_device);
  235. rt_interrupt_leave();
  236. }
  237. #endif /* BSP_USING_HWTIMER2 */
  238. #endif /* BSP_USING_HWTIMER */