drv_hwtimer.c 7.0 KB

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