drv_hwtimer.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2006-2018, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-12-10 Zohar_Lee first version
  9. * 2020-07-10 lik format file
  10. */
  11. #include "drv_hwtimer.h"
  12. #ifdef RT_USING_HWTIMER
  13. #ifdef BSP_USING_TIM
  14. enum
  15. {
  16. #ifdef BSP_USING_TIM0
  17. TIM0_INDEX,
  18. #endif
  19. #ifdef BSP_USING_TIM1
  20. TIM1_INDEX,
  21. #endif
  22. #ifdef BSP_USING_TIM2
  23. TIM2_INDEX,
  24. #endif
  25. #ifdef BSP_USING_TIM3
  26. TIM3_INDEX,
  27. #endif
  28. #ifdef BSP_USING_TIM4
  29. TIM4_INDEX,
  30. #endif
  31. #ifdef BSP_USING_TIM5
  32. TIM5_INDEX,
  33. #endif
  34. };
  35. static struct swm_hwtimer_cfg hwtimer_cfg[] =
  36. {
  37. #ifdef BSP_USING_TIM0
  38. TIM0_CFG,
  39. #endif
  40. #ifdef BSP_USING_TIM1
  41. TIM1_CFG,
  42. #endif
  43. #ifdef BSP_USING_TIM2
  44. TIM2_CFG,
  45. #endif
  46. #ifdef BSP_USING_TIM3
  47. TIM3_CFG,
  48. #endif
  49. #ifdef BSP_USING_TIM4
  50. TIM4_CFG,
  51. #endif
  52. #ifdef BSP_USING_TIM5
  53. TIM5_CFG,
  54. #endif
  55. };
  56. static struct swm_hwtimer hwtimer_drv[sizeof(hwtimer_cfg) / sizeof(hwtimer_cfg[0])] = {0};
  57. static void swm_timer_init(struct rt_hwtimer_device *timer_device, rt_uint32_t state)
  58. {
  59. struct swm_hwtimer_cfg *cfg = RT_NULL;
  60. RT_ASSERT(timer_device != RT_NULL);
  61. if (state)
  62. {
  63. cfg = timer_device->parent.user_data;
  64. TIMR_Init(cfg->TIMRx, TIMR_MODE_TIMER, SystemCoreClock, 1);
  65. timer_device->freq = SystemCoreClock;
  66. }
  67. }
  68. static rt_err_t swm_timer_start(rt_hwtimer_t *timer_device, rt_uint32_t cnt, rt_hwtimer_mode_t opmode)
  69. {
  70. rt_err_t result = RT_EOK;
  71. struct swm_hwtimer_cfg *cfg = RT_NULL;
  72. RT_ASSERT(timer_device != RT_NULL);
  73. cfg = timer_device->parent.user_data;
  74. if (opmode == HWTIMER_MODE_ONESHOT)
  75. {
  76. /* set timer to single mode */
  77. timer_device->mode = HWTIMER_MODE_ONESHOT;
  78. }
  79. else
  80. {
  81. timer_device->mode = HWTIMER_MODE_PERIOD;
  82. }
  83. TIMR_SetPeriod(cfg->TIMRx, cnt);
  84. TIMR_Stop(cfg->TIMRx);
  85. TIMR_Start(cfg->TIMRx);
  86. return result;
  87. }
  88. static void swm_timer_stop(rt_hwtimer_t *timer_device)
  89. {
  90. struct swm_hwtimer_cfg *cfg = RT_NULL;
  91. RT_ASSERT(timer_device != RT_NULL);
  92. cfg = timer_device->parent.user_data;
  93. /* stop timer */
  94. TIMR_Stop(cfg->TIMRx);
  95. }
  96. static rt_uint32_t swm_timer_count_get(rt_hwtimer_t *timer_device)
  97. {
  98. struct swm_hwtimer_cfg *cfg = RT_NULL;
  99. RT_ASSERT(timer_device != RT_NULL);
  100. cfg = timer_device->parent.user_data;
  101. return TIMR_GetCurValue(cfg->TIMRx);
  102. }
  103. static rt_err_t swm_timer_ctrl(rt_hwtimer_t *timer_device, rt_uint32_t cmd, void *args)
  104. {
  105. struct swm_hwtimer_cfg *cfg = RT_NULL;
  106. rt_err_t result = RT_EOK;
  107. RT_ASSERT(timer_device != RT_NULL);
  108. RT_ASSERT(args != RT_NULL);
  109. cfg = timer_device->parent.user_data;
  110. switch (cmd)
  111. {
  112. case HWTIMER_CTRL_FREQ_SET:
  113. {
  114. rt_uint32_t freq;
  115. freq = *(rt_uint32_t *)args;
  116. TIMR_Init(cfg->TIMRx, TIMR_MODE_TIMER, SystemCoreClock / freq, 1);
  117. }
  118. break;
  119. default:
  120. {
  121. result = -RT_ENOSYS;
  122. }
  123. break;
  124. }
  125. return result;
  126. }
  127. static const struct rt_hwtimer_info _info = TIM_DEV_INFO_CONFIG;
  128. static struct rt_hwtimer_ops swm_hwtimer_ops =
  129. {
  130. .init = swm_timer_init,
  131. .start = swm_timer_start,
  132. .stop = swm_timer_stop,
  133. .count_get = swm_timer_count_get,
  134. .control = swm_timer_ctrl};
  135. void rt_hw_hwtimer_isr(rt_hwtimer_t *timer_device)
  136. {
  137. struct swm_hwtimer_cfg *cfg = RT_NULL;
  138. RT_ASSERT(timer_device != RT_NULL);
  139. cfg = timer_device->parent.user_data;
  140. TIMR_INTClr(cfg->TIMRx);
  141. rt_device_hwtimer_isr(timer_device);
  142. }
  143. #ifdef BSP_USING_TIM0
  144. void TIMR0_Handler(void)
  145. {
  146. /* enter interrupt */
  147. rt_interrupt_enter();
  148. rt_hw_hwtimer_isr(&(hwtimer_drv[TIM0_INDEX].time_device));
  149. /* leave interrupt */
  150. rt_interrupt_leave();
  151. }
  152. #endif //BSP_USING_TIM0
  153. #ifdef BSP_USING_TIM1
  154. void TIMR1_Handler(void)
  155. {
  156. /* enter interrupt */
  157. rt_interrupt_enter();
  158. rt_hw_hwtimer_isr(&(hwtimer_drv[TIM1_INDEX].time_device));
  159. /* leave interrupt */
  160. rt_interrupt_leave();
  161. }
  162. #endif //BSP_USING_TIM1
  163. #ifdef BSP_USING_TIM2
  164. void TIMR2_Handler(void)
  165. {
  166. /* enter interrupt */
  167. rt_interrupt_enter();
  168. rt_hw_hwtimer_isr(&(hwtimer_drv[TIM2_INDEX].time_device));
  169. /* leave interrupt */
  170. rt_interrupt_leave();
  171. }
  172. #endif //BSP_USING_TIM2
  173. #ifdef BSP_USING_TIM3
  174. void TIMR3_Handler(void)
  175. {
  176. /* enter interrupt */
  177. rt_interrupt_enter();
  178. rt_hw_hwtimer_isr(&(hwtimer_drv[TIM3_INDEX].time_device));
  179. /* leave interrupt */
  180. rt_interrupt_leave();
  181. }
  182. #endif //BSP_USING_TIM3
  183. #ifdef BSP_USING_TIM4
  184. void TIMR4_Handler(void)
  185. {
  186. /* enter interrupt */
  187. rt_interrupt_enter();
  188. rt_hw_hwtimer_isr(&(hwtimer_drv[TIM4_INDEX].time_device));
  189. /* leave interrupt */
  190. rt_interrupt_leave();
  191. }
  192. #endif //BSP_USING_TIM4
  193. #ifdef BSP_USING_TIM5
  194. void TIMR5_Handler(void)
  195. {
  196. /* enter interrupt */
  197. rt_interrupt_enter();
  198. rt_hw_hwtimer_isr(&(hwtimer_drv[TIM5_INDEX].time_device));
  199. /* leave interrupt */
  200. rt_interrupt_leave();
  201. }
  202. #endif //BSP_USING_TIM5
  203. static int rt_hw_hwtimer_init(void)
  204. {
  205. int i = 0;
  206. int result = RT_EOK;
  207. for (i = 0; i < sizeof(hwtimer_cfg) / sizeof(hwtimer_cfg[0]); i++)
  208. {
  209. hwtimer_drv[i].cfg = &hwtimer_cfg[i];
  210. hwtimer_drv[i].time_device.info = &_info;
  211. hwtimer_drv[i].time_device.ops = &swm_hwtimer_ops;
  212. if (rt_device_hwtimer_register(&hwtimer_drv[i].time_device, hwtimer_drv[i].cfg->name, hwtimer_drv[i].cfg) == RT_EOK)
  213. {
  214. ;
  215. }
  216. else
  217. {
  218. result = -RT_ERROR;
  219. }
  220. }
  221. return result;
  222. }
  223. INIT_BOARD_EXPORT(rt_hw_hwtimer_init);
  224. #endif /* BSP_USING_TIM */
  225. #endif /* RT_USING_HWTIMER */