drv_hw_timer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_uint32_t wm_timer_get(rt_hwtimer_t *timer)
  55. {
  56. return 0;
  57. }
  58. static rt_err_t wm_timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
  59. {
  60. /* The frequency value is an immutable value. */
  61. if (cmd != HWTIMER_CTRL_FREQ_SET)
  62. {
  63. return -RT_ENOSYS;
  64. }
  65. if ( *(rt_uint32_t*)arg == 1000000)
  66. {
  67. return RT_EOK;
  68. }
  69. else
  70. {
  71. return -RT_ENOSYS;
  72. }
  73. }
  74. static const struct rt_hwtimer_info _info =
  75. {
  76. 1000000, /* the maximum count frequency can be set */
  77. 1000000, /* the minimum count frequency can be set */
  78. 0xFFFFFFFF, /* the maximum counter value */
  79. HWTIMER_CNTMODE_DW, /* Increment or Decreasing count mode */
  80. };
  81. static const struct rt_hwtimer_ops _ops =
  82. {
  83. wm_timer_init,
  84. wm_timer_start,
  85. wm_timer_stop,
  86. wm_timer_get,
  87. wm_timer_ctrl,
  88. };
  89. #ifdef USING_HW_TIMER1
  90. static rt_hwtimer_t _timer1;
  91. static struct wm_timer_Type wm_timer1;
  92. #endif
  93. #ifdef USING_HW_TIMER2
  94. static rt_hwtimer_t _timer2;
  95. static struct wm_timer_Type wm_timer2;
  96. #endif
  97. #ifdef USING_HW_TIMER3
  98. static rt_hwtimer_t _timer3;
  99. static struct wm_timer_Type wm_timer3;
  100. #endif
  101. #ifdef USING_HW_TIMER4
  102. static rt_hwtimer_t _timer4;
  103. static struct wm_timer_Type wm_timer4;
  104. #endif
  105. #ifdef USING_HW_TIMER5
  106. static rt_hwtimer_t _timer5;
  107. static struct wm_timer_Type wm_timer5;
  108. #endif
  109. int wm_hw_timer_init(void)
  110. {
  111. #ifdef USING_HW_TIMER1
  112. wm_timer1.id = TLS_TIMER_ID_1;
  113. wm_timer1.unit = TLS_TIMER_UNIT_US;
  114. _timer1.info = &_info;
  115. _timer1.ops = &_ops;
  116. rt_device_hwtimer_register(&_timer1, "timer1", &wm_timer1);
  117. #endif
  118. #ifdef USING_HW_TIMER2
  119. wm_timer2.id = TLS_TIMER_ID_2;
  120. wm_timer2.unit = TLS_TIMER_UNIT_US;
  121. _timer2.info = &_info;
  122. _timer2.ops = &_ops;
  123. rt_device_hwtimer_register(&_timer2, "timer2", &wm_timer2);
  124. #endif
  125. #ifdef USING_HW_TIMER3
  126. wm_timer3.id = TLS_TIMER_ID_3;
  127. wm_timer3.unit = TLS_TIMER_UNIT_US;
  128. _timer3.info = &_info;
  129. _timer3.ops = &_ops;
  130. rt_device_hwtimer_register(&_timer3, "timer3", &wm_timer3);
  131. #endif
  132. #ifdef USING_HW_TIMER4
  133. wm_timer4.id = TLS_TIMER_ID_4;
  134. wm_timer4.unit = TLS_TIMER_UNIT_US;
  135. _timer4.info = &_info;
  136. _timer4.ops = &_ops;
  137. rt_device_hwtimer_register(&_timer4, "timer4", &wm_timer4);
  138. #endif
  139. #ifdef USING_HW_TIMER5
  140. wm_timer5.id = TLS_TIMER_ID_5;
  141. wm_timer5.unit = TLS_TIMER_UNIT_US;
  142. _timer5.info = &_info;
  143. _timer5.ops = &_ops;
  144. rt_device_hwtimer_register(&_timer5, "timer5", &wm_timer5);
  145. #endif
  146. return 0;
  147. }
  148. INIT_BOARD_EXPORT(wm_hw_timer_init);
  149. void TIM1_IRQHandler(void)
  150. {
  151. timer_clear_irq(1);
  152. #ifdef USING_HW_TIMER1
  153. rt_device_hwtimer_isr(&_timer1);
  154. #endif
  155. }
  156. void TIM2_IRQHandler(void)
  157. {
  158. timer_clear_irq(2);
  159. #ifdef USING_HW_TIMER2
  160. rt_device_hwtimer_isr(&_timer2);
  161. #endif
  162. }
  163. void TIM3_IRQHandler(void)
  164. {
  165. timer_clear_irq(3);
  166. #ifdef USING_HW_TIMER3
  167. rt_device_hwtimer_isr(&_timer3);
  168. #endif
  169. }
  170. void TIM4_IRQHandler(void)
  171. {
  172. timer_clear_irq(4);
  173. #ifdef USING_HW_TIMER4
  174. rt_device_hwtimer_isr(&_timer4);
  175. #endif
  176. }
  177. void TIM5_IRQHandler(void)
  178. {
  179. timer_clear_irq(5);
  180. #ifdef USING_HW_TIMER5
  181. rt_device_hwtimer_isr(&_timer5);
  182. #endif
  183. }
  184. #endif /* BSP_USING_HWTIMER */