drv_hwtimer.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. /* can not use when UART2 Handler is enabled */
  39. void BS16T1_UART2_Handler(void)
  40. {
  41. /* if BS16T1 it */
  42. if (timer_get_it_status(hwtimer1.hwtimer_periph, TIMER_IT_UPDATE) &&
  43. timer_get_flag_status(hwtimer1.hwtimer_periph, TIMER_FLAG_UPDATE))
  44. {
  45. timer_clear_flag_status(hwtimer1.hwtimer_periph, TIMER_FLAG_UPDATE);
  46. rt_device_hwtimer_isr(&hwtimer1.parent);
  47. if (HWTIMER_MODE_ONESHOT == hwtimer1.parent.mode)
  48. {
  49. timer_base_stop(hwtimer1.hwtimer_periph);
  50. }
  51. }
  52. }
  53. #endif
  54. #ifdef BSP_USING_HWTIMER2
  55. static struct es32f0_hwtimer_dev hwtimer2;
  56. /* can not use when UART3 Handler is enabled */
  57. void BS16T2_UART3_Handler(void)
  58. {
  59. /* if BS16T2 it */
  60. if (timer_get_it_status(hwtimer2.hwtimer_periph, TIMER_IT_UPDATE) &&
  61. timer_get_flag_status(hwtimer2.hwtimer_periph, TIMER_FLAG_UPDATE))
  62. {
  63. timer_clear_flag_status(hwtimer2.hwtimer_periph, TIMER_FLAG_UPDATE);
  64. rt_device_hwtimer_isr(&hwtimer2.parent);
  65. if (HWTIMER_MODE_ONESHOT == hwtimer2.parent.mode)
  66. {
  67. timer_base_stop(hwtimer2.hwtimer_periph);
  68. }
  69. }
  70. }
  71. #endif
  72. #ifdef BSP_USING_HWTIMER3
  73. static struct es32f0_hwtimer_dev hwtimer3;
  74. /* can not use when DAC0 Handler is enabled */
  75. void BS16T3_DAC0_Handler(void)
  76. {
  77. /* if BS16T3 it */
  78. if (timer_get_it_status(hwtimer3.hwtimer_periph, TIMER_IT_UPDATE) &&
  79. timer_get_flag_status(hwtimer3.hwtimer_periph, TIMER_FLAG_UPDATE))
  80. {
  81. timer_clear_flag_status(hwtimer3.hwtimer_periph, TIMER_FLAG_UPDATE);
  82. rt_device_hwtimer_isr(&hwtimer3.parent);
  83. if (HWTIMER_MODE_ONESHOT == hwtimer3.parent.mode)
  84. {
  85. timer_base_stop(hwtimer3.hwtimer_periph);
  86. }
  87. }
  88. }
  89. #endif
  90. static struct rt_hwtimer_info es32f0_hwtimer_info =
  91. {
  92. 48000000, /* maximum count frequency */
  93. 1, /* minimum count frequency */
  94. 65535, /* counter maximum value */
  95. HWTIMER_CNTMODE_UP
  96. };
  97. static void es32f0_hwtimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
  98. {
  99. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  100. RT_ASSERT(hwtimer != RT_NULL);
  101. if (1 == state)
  102. {
  103. timer_base_init(hwtimer->hwtimer_periph);
  104. timer_interrupt_config(hwtimer->hwtimer_periph, TIMER_IT_UPDATE, ENABLE);
  105. NVIC_EnableIRQ(hwtimer->IRQn);
  106. }
  107. hwtimer->parent.freq = cmu_get_pclk1_clock();
  108. es32f0_hwtimer_info.maxfreq = cmu_get_pclk1_clock();
  109. es32f0_hwtimer_info.minfreq = cmu_get_pclk1_clock();
  110. }
  111. static rt_err_t es32f0_hwtimer_start(rt_hwtimer_t *timer,
  112. rt_uint32_t cnt,
  113. rt_hwtimer_mode_t mode)
  114. {
  115. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  116. RT_ASSERT(hwtimer != RT_NULL);
  117. WRITE_REG(hwtimer->hwtimer_periph->perh->AR, cnt);
  118. timer_base_start(hwtimer->hwtimer_periph);
  119. return RT_EOK;
  120. }
  121. static void es32f0_hwtimer_stop(rt_hwtimer_t *timer)
  122. {
  123. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  124. RT_ASSERT(hwtimer != RT_NULL);
  125. timer_base_stop(hwtimer->hwtimer_periph);
  126. }
  127. static rt_uint32_t es32f0_hwtimer_count_get(rt_hwtimer_t *timer)
  128. {
  129. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  130. uint32_t hwtimer_count = 0;
  131. RT_ASSERT(hwtimer != RT_NULL);
  132. hwtimer_count = READ_REG(hwtimer->hwtimer_periph->perh->COUNT);
  133. return hwtimer_count;
  134. }
  135. static rt_err_t es32f0_hwtimer_control(rt_hwtimer_t *timer,
  136. rt_uint32_t cmd,
  137. void *args)
  138. {
  139. rt_err_t ret = RT_EOK;
  140. rt_uint32_t freq = 0;
  141. struct es32f0_hwtimer_dev *hwtimer = (struct es32f0_hwtimer_dev *)timer->parent.user_data;
  142. RT_ASSERT(hwtimer != RT_NULL);
  143. switch (cmd)
  144. {
  145. case HWTIMER_CTRL_FREQ_SET:
  146. freq = *(rt_uint32_t *)args;
  147. if (freq != cmu_get_pclk1_clock())
  148. {
  149. ret = -RT_ERROR;
  150. }
  151. break;
  152. case HWTIMER_CTRL_STOP:
  153. timer_base_stop(hwtimer->hwtimer_periph);
  154. break;
  155. default:
  156. ret = RT_EINVAL;
  157. break;
  158. }
  159. return ret;
  160. }
  161. static struct rt_hwtimer_ops es32f0_hwtimer_ops =
  162. {
  163. es32f0_hwtimer_init,
  164. es32f0_hwtimer_start,
  165. es32f0_hwtimer_stop,
  166. es32f0_hwtimer_count_get,
  167. es32f0_hwtimer_control
  168. };
  169. int rt_hw_hwtimer_init(void)
  170. {
  171. rt_err_t ret = RT_EOK;
  172. #ifdef BSP_USING_HWTIMER0
  173. static timer_handle_t _hwtimer_periph0;
  174. _hwtimer_periph0.perh = BS16T0;
  175. hwtimer0.IRQn = BS16T0_IRQn;
  176. hwtimer0.hwtimer_periph = &_hwtimer_periph0;
  177. hwtimer0.parent.info = &es32f0_hwtimer_info;
  178. hwtimer0.parent.ops = &es32f0_hwtimer_ops;
  179. ret = rt_device_hwtimer_register(&hwtimer0.parent, "timer0", &hwtimer0);
  180. #endif
  181. #ifdef BSP_USING_HWTIMER1
  182. static timer_handle_t _hwtimer_periph1;
  183. _hwtimer_periph1.perh = BS16T1;
  184. hwtimer1.IRQn = BS16T1_UART2_IRQn;
  185. hwtimer1.hwtimer_periph = &_hwtimer_periph1;
  186. hwtimer1.parent.info = &es32f0_hwtimer_info;
  187. hwtimer1.parent.ops = &es32f0_hwtimer_ops;
  188. ret = rt_device_hwtimer_register(&hwtimer1.parent, "timer1", &hwtimer1);
  189. #endif
  190. #ifdef BSP_USING_HWTIMER2
  191. static timer_handle_t _hwtimer_periph2;
  192. _hwtimer_periph2.perh = BS16T2;
  193. hwtimer2.IRQn = BS16T2_UART3_IRQn;
  194. hwtimer2.hwtimer_periph = &_hwtimer_periph2;
  195. hwtimer2.parent.info = &es32f0_hwtimer_info;
  196. hwtimer2.parent.ops = &es32f0_hwtimer_ops;
  197. ret = rt_device_hwtimer_register(&hwtimer2.parent, "timer2", &hwtimer2);
  198. #endif
  199. #ifdef BSP_USING_HWTIMER3
  200. static timer_handle_t _hwtimer_periph3;
  201. _hwtimer_periph3.perh = BS16T3;
  202. hwtimer3.IRQn = BS16T3_DAC0_IRQn;
  203. hwtimer3.hwtimer_periph = &_hwtimer_periph3;
  204. hwtimer3.parent.info = &es32f0_hwtimer_info;
  205. hwtimer3.parent.ops = &es32f0_hwtimer_ops;
  206. ret = rt_device_hwtimer_register(&hwtimer3.parent, "timer3", &hwtimer3);
  207. #endif
  208. return ret;
  209. }
  210. INIT_BOARD_EXPORT(rt_hw_hwtimer_init);
  211. #endif