drv_hwtimer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-3-19 wangyq the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <drv_hwtimer.h>
  14. #include <board.h>
  15. #include <ald_cmu.h>
  16. #include <ald_timer.h>
  17. #ifdef RT_USING_HWTIMER
  18. struct es32f0_hwtimer_dev
  19. {
  20. rt_hwtimer_t parent;
  21. timer_handle_t *hwtimer_periph;
  22. IRQn_Type IRQn;
  23. };
  24. #ifdef BSP_USING_HWTIMER0
  25. static struct es32f0_hwtimer_dev hwtimer0;
  26. void BS16T0_Handler(void)
  27. {
  28. timer_clear_flag_status(hwtimer0.hwtimer_periph, TIMER_FLAG_UPDATE);
  29. rt_device_hwtimer_isr(&hwtimer0.parent);
  30. if (HWTIMER_MODE_ONESHOT == hwtimer0.parent.mode)
  31. {
  32. timer_base_stop(hwtimer0.hwtimer_periph);
  33. }
  34. }
  35. #endif
  36. #ifdef BSP_USING_HWTIMER1
  37. static struct es32f0_hwtimer_dev hwtimer1;
  38. void BS16T1_UART2_Handler(void)
  39. {
  40. if (timer_get_it_status(hwtimer1.hwtimer_periph, TIMER_IT_UPDATE) &&
  41. timer_get_flag_status(hwtimer1.hwtimer_periph, TIMER_FLAG_UPDATE))
  42. {
  43. timer_clear_flag_status(hwtimer1.hwtimer_periph, TIMER_FLAG_UPDATE);
  44. rt_device_hwtimer_isr(&hwtimer1.parent);
  45. if (HWTIMER_MODE_ONESHOT == hwtimer1.parent.mode)
  46. {
  47. timer_base_stop(hwtimer1.hwtimer_periph);
  48. }
  49. }
  50. }
  51. #endif
  52. #ifdef BSP_USING_HWTIMER2
  53. static struct es32f0_hwtimer_dev hwtimer2;
  54. void BS16T2_UART3_Handler(void)
  55. {
  56. if (timer_get_it_status(hwtimer2.hwtimer_periph, TIMER_IT_UPDATE) &&
  57. timer_get_flag_status(hwtimer2.hwtimer_periph, TIMER_FLAG_UPDATE))
  58. {
  59. timer_clear_flag_status(hwtimer2.hwtimer_periph, TIMER_FLAG_UPDATE);
  60. rt_device_hwtimer_isr(&hwtimer2.parent);
  61. if (HWTIMER_MODE_ONESHOT == hwtimer2.parent.mode)
  62. {
  63. timer_base_stop(hwtimer2.hwtimer_periph);
  64. }
  65. }
  66. }
  67. #endif
  68. #ifdef BSP_USING_HWTIMER3
  69. static struct es32f0_hwtimer_dev hwtimer3;
  70. /* can not use when DAC0 Handler is enabled */
  71. void BS16T3_DAC0_Handler(void)
  72. {
  73. /* if BS16T3 it */
  74. if (timer_get_it_status(hwtimer3.hwtimer_periph, TIMER_IT_UPDATE) &&
  75. timer_get_flag_status(hwtimer3.hwtimer_periph, TIMER_FLAG_UPDATE))
  76. {
  77. timer_clear_flag_status(hwtimer3.hwtimer_periph, TIMER_FLAG_UPDATE);
  78. rt_device_hwtimer_isr(&hwtimer3.parent);
  79. if (HWTIMER_MODE_ONESHOT == hwtimer3.parent.mode)
  80. {
  81. timer_base_stop(hwtimer3.hwtimer_periph);
  82. }
  83. }
  84. }
  85. #endif
  86. static struct rt_hwtimer_info es32f0_hwtimer_info =
  87. {
  88. 48000000, /* maximum count frequency */
  89. 1, /* minimum count frequency */
  90. 65535, /* counter maximum value */
  91. HWTIMER_CNTMODE_UP
  92. };
  93. static void es32f0_hwtimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
  94. {
  95. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  96. RT_ASSERT(hwtimer != RT_NULL);
  97. if (1 == state)
  98. {
  99. timer_base_init(hwtimer->hwtimer_periph);
  100. timer_interrupt_config(hwtimer->hwtimer_periph, TIMER_IT_UPDATE, ENABLE);
  101. NVIC_EnableIRQ(hwtimer->IRQn);
  102. }
  103. hwtimer->parent.freq = cmu_get_pclk1_clock();
  104. es32f0_hwtimer_info.maxfreq = cmu_get_pclk1_clock();
  105. es32f0_hwtimer_info.minfreq = cmu_get_pclk1_clock();
  106. }
  107. static rt_err_t es32f0_hwtimer_start(rt_hwtimer_t *timer,
  108. rt_uint32_t cnt,
  109. rt_hwtimer_mode_t mode)
  110. {
  111. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  112. RT_ASSERT(hwtimer != RT_NULL);
  113. WRITE_REG(hwtimer->hwtimer_periph->perh->AR, cnt);
  114. timer_base_start(hwtimer->hwtimer_periph);
  115. return RT_EOK;
  116. }
  117. static void es32f0_hwtimer_stop(rt_hwtimer_t *timer)
  118. {
  119. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  120. RT_ASSERT(hwtimer != RT_NULL);
  121. timer_base_stop(hwtimer->hwtimer_periph);
  122. }
  123. static rt_uint32_t es32f0_hwtimer_count_get(rt_hwtimer_t *timer)
  124. {
  125. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  126. uint32_t hwtimer_count = 0;
  127. RT_ASSERT(hwtimer != RT_NULL);
  128. hwtimer_count = READ_REG(hwtimer->hwtimer_periph->perh->COUNT);
  129. return hwtimer_count;
  130. }
  131. static rt_err_t es32f0_hwtimer_control(rt_hwtimer_t *timer,
  132. rt_uint32_t cmd,
  133. void *args)
  134. {
  135. rt_err_t ret = RT_EOK;
  136. rt_uint32_t freq = 0;
  137. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  138. RT_ASSERT(hwtimer != RT_NULL);
  139. switch (cmd)
  140. {
  141. case HWTIMER_CTRL_FREQ_SET:
  142. freq = *(rt_uint32_t *)args;
  143. if (freq != cmu_get_pclk1_clock())
  144. {
  145. ret = -RT_ERROR;
  146. }
  147. break;
  148. case HWTIMER_CTRL_STOP:
  149. timer_base_stop(hwtimer->hwtimer_periph);
  150. break;
  151. default:
  152. ret = RT_EINVAL;
  153. break;
  154. }
  155. return ret;
  156. }
  157. static struct rt_hwtimer_ops es32f0_hwtimer_ops =
  158. {
  159. es32f0_hwtimer_init,
  160. es32f0_hwtimer_start,
  161. es32f0_hwtimer_stop,
  162. es32f0_hwtimer_count_get,
  163. es32f0_hwtimer_control
  164. };
  165. int rt_hw_hwtimer_init(void)
  166. {
  167. rt_err_t ret = RT_EOK;
  168. #ifdef BSP_USING_HWTIMER0
  169. static timer_handle_t _hwtimer_periph0;
  170. _hwtimer_periph0.perh = BS16T0;
  171. hwtimer0.IRQn = BS16T0_IRQn;
  172. hwtimer0.hwtimer_periph = &_hwtimer_periph0;
  173. hwtimer0.parent.info = &es32f0_hwtimer_info;
  174. hwtimer0.parent.ops = &es32f0_hwtimer_ops;
  175. ret = rt_device_hwtimer_register(&hwtimer0.parent, "timer0", &hwtimer0);
  176. #endif
  177. #ifdef BSP_USING_HWTIMER1
  178. static timer_handle_t _hwtimer_periph1;
  179. _hwtimer_periph1.perh = BS16T1;
  180. hwtimer1.IRQn = BS16T1_UART2_IRQn;
  181. hwtimer1.hwtimer_periph = &_hwtimer_periph1;
  182. hwtimer1.parent.info = &es32f0_hwtimer_info;
  183. hwtimer1.parent.ops = &es32f0_hwtimer_ops;
  184. ret = rt_device_hwtimer_register(&hwtimer1.parent, "timer1", &hwtimer1);
  185. #endif
  186. #ifdef BSP_USING_HWTIMER2
  187. static timer_handle_t _hwtimer_periph2;
  188. _hwtimer_periph2.perh = BS16T2;
  189. hwtimer2.IRQn = BS16T2_UART3_IRQn;
  190. hwtimer2.hwtimer_periph = &_hwtimer_periph2;
  191. hwtimer2.parent.info = &es32f0_hwtimer_info;
  192. hwtimer2.parent.ops = &es32f0_hwtimer_ops;
  193. ret = rt_device_hwtimer_register(&hwtimer2.parent, "timer2", &hwtimer2);
  194. #endif
  195. #ifdef BSP_USING_HWTIMER3
  196. static timer_handle_t _hwtimer_periph3;
  197. _hwtimer_periph3.perh = BS16T3;
  198. hwtimer3.IRQn = BS16T3_DAC0_IRQn;
  199. hwtimer3.hwtimer_periph = &_hwtimer_periph3;
  200. hwtimer3.parent.info = &es32f0_hwtimer_info;
  201. hwtimer3.parent.ops = &es32f0_hwtimer_ops;
  202. ret = rt_device_hwtimer_register(&hwtimer3.parent, "timer3", &hwtimer3);
  203. #endif
  204. return ret;
  205. }
  206. INIT_BOARD_EXPORT(rt_hw_hwtimer_init);
  207. #endif