drv_hwtimer.c 6.8 KB

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