drv_hw_timer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2019 Winner Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-19 fanwenl 1st version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "wm_type_def.h"
  13. #include "wm_timer.h"
  14. #include "drv_hw_timer.h"
  15. #ifdef BSP_USING_HWTIMER
  16. struct wm_timer_Type
  17. {
  18. enum tls_timer_unit unit;
  19. enum tls_timer_id id;
  20. };
  21. static void wm_timer_init(rt_hwtimer_t *timer, rt_uint32_t state)
  22. {
  23. struct tls_timer_cfg timer_cfg;
  24. struct wm_timer_Type *wm_timer = (struct wm_timer_Type *)timer->parent.user_data;
  25. timer_cfg.unit = wm_timer->unit;
  26. timer_cfg.timeout = 0xFFFFFFFF;
  27. timer_cfg.is_repeat = 0;
  28. timer_cfg.callback = NULL;
  29. timer_cfg.arg = NULL;
  30. if (state == 1)
  31. {
  32. tls_timer_create(&timer_cfg, wm_timer->id);
  33. }
  34. else if (state == 0)
  35. {
  36. tls_timer_destroy(wm_timer->id);
  37. }
  38. }
  39. static rt_err_t wm_timer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode)
  40. {
  41. struct wm_timer_Type *wm_timer = (struct wm_timer_Type *)timer->parent.user_data;
  42. uint8_t m;
  43. tls_timer_change(wm_timer->id, t);
  44. m = (opmode == HWTIMER_MODE_ONESHOT) ? 0 : 1;
  45. tls_timer_set_mode(wm_timer->id, m);
  46. tls_timer_start(wm_timer->id);
  47. return RT_EOK;
  48. }
  49. static void wm_timer_stop(rt_hwtimer_t *timer)
  50. {
  51. struct wm_timer_Type *wm_timer = (struct wm_timer_Type *)timer->parent.user_data;
  52. tls_timer_stop(wm_timer->id);
  53. }
  54. static rt_err_t wm_timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
  55. {
  56. /* The frequency value is an immutable value. */
  57. if (cmd != HWTIMER_CTRL_FREQ_SET)
  58. {
  59. return -RT_ENOSYS;
  60. }
  61. if ( *(rt_uint32_t*)arg == 1000000)
  62. {
  63. return RT_EOK;
  64. }
  65. else
  66. {
  67. return -RT_ENOSYS;
  68. }
  69. }
  70. static const struct rt_hwtimer_info _info =
  71. {
  72. 1000000, /* the maximum count frequency can be set */
  73. 1000000, /* the minimum count frequency can be set */
  74. 0xFFFFFFFF, /* the maximum counter value */
  75. HWTIMER_CNTMODE_DW, /* Increment or Decreasing count mode */
  76. };
  77. static const struct rt_hwtimer_ops _ops =
  78. {
  79. wm_timer_init,
  80. wm_timer_start,
  81. wm_timer_stop,
  82. RT_NULL,
  83. wm_timer_ctrl,
  84. };
  85. #ifdef USING_HW_TIMER1
  86. static rt_hwtimer_t _timer1;
  87. static struct wm_timer_Type wm_timer1;
  88. #endif
  89. #ifdef USING_HW_TIMER2
  90. static rt_hwtimer_t _timer2;
  91. static struct wm_timer_Type wm_timer2;
  92. #endif
  93. #ifdef USING_HW_TIMER3
  94. static rt_hwtimer_t _timer3;
  95. static struct wm_timer_Type wm_timer3;
  96. #endif
  97. #ifdef USING_HW_TIMER4
  98. static rt_hwtimer_t _timer4;
  99. static struct wm_timer_Type wm_timer4;
  100. #endif
  101. #ifdef USING_HW_TIMER5
  102. static rt_hwtimer_t _timer5;
  103. static struct wm_timer_Type wm_timer5;
  104. #endif
  105. int wm_hw_timer_init(void)
  106. {
  107. #ifdef USING_HW_TIMER1
  108. wm_timer1.id = TLS_TIMER_ID_1;
  109. wm_timer1.unit = TLS_TIMER_UNIT_US;
  110. _timer1.info = &_info;
  111. _timer1.ops = &_ops;
  112. rt_device_hwtimer_register(&_timer1, "timer1", &wm_timer1);
  113. #endif
  114. #ifdef USING_HW_TIMER2
  115. wm_timer2.id = TLS_TIMER_ID_2;
  116. wm_timer2.unit = TLS_TIMER_UNIT_US;
  117. _timer2.info = &_info;
  118. _timer2.ops = &_ops;
  119. rt_device_hwtimer_register(&_timer2, "timer2", &wm_timer2);
  120. #endif
  121. #ifdef USING_HW_TIMER3
  122. wm_timer3.id = TLS_TIMER_ID_3;
  123. wm_timer3.unit = TLS_TIMER_UNIT_US;
  124. _timer3.info = &_info;
  125. _timer3.ops = &_ops;
  126. rt_device_hwtimer_register(&_timer3, "timer3", &wm_timer3);
  127. #endif
  128. #ifdef USING_HW_TIMER4
  129. wm_timer4.id = TLS_TIMER_ID_4;
  130. wm_timer4.unit = TLS_TIMER_UNIT_US;
  131. _timer4.info = &_info;
  132. _timer4.ops = &_ops;
  133. rt_device_hwtimer_register(&_timer4, "timer4", &wm_timer4);
  134. #endif
  135. #ifdef USING_HW_TIMER5
  136. wm_timer5.id = TLS_TIMER_ID_5;
  137. wm_timer5.unit = TLS_TIMER_UNIT_US;
  138. _timer5.info = &_info;
  139. _timer5.ops = &_ops;
  140. rt_device_hwtimer_register(&_timer5, "timer5", &wm_timer5);
  141. #endif
  142. return 0;
  143. }
  144. INIT_BOARD_EXPORT(wm_hw_timer_init);
  145. void TIM1_IRQHandler(void)
  146. {
  147. timer_clear_irq(1);
  148. #ifdef USING_HW_TIMER1
  149. rt_device_hwtimer_isr(&_timer1);
  150. #endif
  151. }
  152. void TIM2_IRQHandler(void)
  153. {
  154. timer_clear_irq(2);
  155. #ifdef USING_HW_TIMER2
  156. rt_device_hwtimer_isr(&_timer2);
  157. #endif
  158. }
  159. void TIM3_IRQHandler(void)
  160. {
  161. timer_clear_irq(3);
  162. #ifdef USING_HW_TIMER3
  163. rt_device_hwtimer_isr(&_timer3);
  164. #endif
  165. }
  166. void TIM4_IRQHandler(void)
  167. {
  168. timer_clear_irq(4);
  169. #ifdef USING_HW_TIMER4
  170. rt_device_hwtimer_isr(&_timer4);
  171. #endif
  172. }
  173. void TIM5_IRQHandler(void)
  174. {
  175. timer_clear_irq(5);
  176. #ifdef USING_HW_TIMER5
  177. rt_device_hwtimer_isr(&_timer5);
  178. #endif
  179. }
  180. #endif /* BSP_USING_HWTIMER */