drv_hwtimer.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-12 hqfang first version
  9. */
  10. #include "drv_hwtimer.h"
  11. #ifdef BSP_USING_HWTIMER
  12. #if !defined(BSP_USING_HWTIMER0) && !defined(BSP_USING_HWTIMER1) && !defined(BSP_USING_HWTIMER2) \
  13. && !defined(BSP_USING_HWTIMER3) && !defined(BSP_USING_HWTIMER4)
  14. #error "Please define at least one BSP_USING_HWTIMERx"
  15. /* this driver can be disabled at menuconfig -> Hardware Drivers Config -> On-chip Peripheral Drivers -> Enable HWTIMER */
  16. #endif
  17. static struct gd32_hwtimer_config hwtimer_config[] =
  18. {
  19. #ifdef BSP_USING_HWTIMER0
  20. {
  21. "timer0",
  22. TIMER0,
  23. TIMER0_UP_IRQn,
  24. },
  25. #endif
  26. #ifdef BSP_USING_HWTIMER1
  27. {
  28. "timer1",
  29. TIMER1,
  30. TIMER1_IRQn,
  31. },
  32. #endif
  33. #ifdef BSP_USING_HWTIMER2
  34. {
  35. "timer2",
  36. TIMER2,
  37. TIMER2_IRQn,
  38. },
  39. #endif
  40. #ifdef BSP_USING_HWTIMER3
  41. {
  42. "timer3",
  43. TIMER3,
  44. TIMER3_IRQn,
  45. },
  46. #endif
  47. #ifdef BSP_USING_HWTIMER4
  48. {
  49. "timer4",
  50. TIMER4,
  51. TIMER4_IRQn,
  52. },
  53. #endif
  54. #ifdef BSP_USING_HWTIMER5
  55. {
  56. "timer5",
  57. TIMER5,
  58. TIMER5_IRQn,
  59. },
  60. #endif
  61. #ifdef BSP_USING_HWTIMER6
  62. {
  63. "timer6",
  64. TIMER6,
  65. TIMER6_IRQn,
  66. },
  67. #endif
  68. };
  69. static struct gd32_hwtimer hwtimer_obj[sizeof(hwtimer_config) / sizeof(hwtimer_config[0])] = {0};
  70. static rt_err_t gd32_hwtimer_control(rt_hwtimer_t *timer, rt_uint32_t cmd, void *args)
  71. {
  72. rt_err_t err = RT_EOK;
  73. struct gd32_hwtimer_config *config;
  74. RT_ASSERT(timer != RT_NULL);
  75. config = (struct gd32_hwtimer_config *)timer->parent.user_data;
  76. switch (cmd)
  77. {
  78. case HWTIMER_CTRL_FREQ_SET:
  79. {
  80. uint32_t clk;
  81. uint8_t clkpre;
  82. uint32_t pre;
  83. if (config->timer_periph != TIMER0)
  84. {
  85. clk = rcu_clock_freq_get(CK_APB1);
  86. clkpre = GET_BITS(RCU_CFG0, 8, 10);
  87. }
  88. else
  89. {
  90. clk = rcu_clock_freq_get(CK_APB2);
  91. clkpre = GET_BITS(RCU_CFG0, 11, 13);
  92. }
  93. if (clkpre >= 4)
  94. {
  95. clk = clk * 2;
  96. }
  97. pre = (clk / * ((uint32_t *)args)) - 1;
  98. TIMER_PSC(config->timer_periph) = (uint32_t)pre;
  99. }
  100. break;
  101. case HWTIMER_CTRL_STOP:
  102. timer_disable(config->timer_periph);
  103. break;
  104. default:
  105. err = -RT_ENOSYS;
  106. break;
  107. }
  108. return err;
  109. }
  110. static rt_uint32_t gd32_hwtimer_count_get(rt_hwtimer_t *timer)
  111. {
  112. rt_uint32_t CurrentTimer_Count;
  113. struct gd32_hwtimer_config *config;
  114. RT_ASSERT(timer != RT_NULL);
  115. config = (struct gd32_hwtimer_config *)timer->parent.user_data;
  116. CurrentTimer_Count = timer_counter_read(config->timer_periph);
  117. return CurrentTimer_Count;
  118. }
  119. static void gd32_hwtimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
  120. {
  121. struct gd32_hwtimer_config *config;
  122. timer_parameter_struct initpara;
  123. RT_ASSERT(timer != RT_NULL);
  124. config = (struct gd32_hwtimer_config *)timer->parent.user_data;
  125. if (state == 1)
  126. {
  127. timer_deinit(config->timer_periph);
  128. timer_struct_para_init(&initpara);
  129. timer_init(config->timer_periph, &initpara);
  130. }
  131. else
  132. {
  133. timer_disable(config->timer_periph);
  134. timer_interrupt_enable(config->timer_periph, TIMER_INT_FLAG_UP);
  135. ECLIC_DisableIRQ(config->irqn);
  136. }
  137. }
  138. static rt_err_t gd32_hwtimer_start(rt_hwtimer_t *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode)
  139. {
  140. struct gd32_hwtimer_config *config;
  141. RT_ASSERT(timer != RT_NULL);
  142. config = (struct gd32_hwtimer_config *)timer->parent.user_data;
  143. if (mode == HWTIMER_MODE_ONESHOT)
  144. {
  145. timer_single_pulse_mode_config(config->timer_periph, TIMER_SP_MODE_SINGLE);
  146. }
  147. else
  148. {
  149. timer_single_pulse_mode_config(config->timer_periph, TIMER_SP_MODE_REPETITIVE);
  150. }
  151. timer_counter_value_config(config->timer_periph, 0);
  152. timer_autoreload_value_config(config->timer_periph, cnt);
  153. timer_interrupt_enable(config->timer_periph, TIMER_INT_FLAG_UP);
  154. timer_enable(config->timer_periph);
  155. ECLIC_EnableIRQ(config->irqn);
  156. return RT_EOK;
  157. }
  158. static void gd32_hwtimer_stop(rt_hwtimer_t *timer)
  159. {
  160. struct gd32_hwtimer_config *config;
  161. RT_ASSERT(timer != RT_NULL);
  162. config = (struct gd32_hwtimer_config *)timer->parent.user_data;
  163. timer_disable(config->timer_periph);
  164. ECLIC_DisableIRQ(config->irqn);
  165. }
  166. static const struct rt_hwtimer_ops gd32_hwtimer_ops =
  167. {
  168. .init = gd32_hwtimer_init,
  169. .start = gd32_hwtimer_start,
  170. .stop = gd32_hwtimer_stop,
  171. .count_get = gd32_hwtimer_count_get,
  172. .control = gd32_hwtimer_control,
  173. };
  174. static const struct rt_hwtimer_info gd32_hwtimer_info =
  175. {
  176. 54000000, /* the maximum count frequency can be set */
  177. 1000, /* the minimum count frequency can be set */
  178. 0xFFFF,
  179. HWTIMER_CNTMODE_UP,
  180. };
  181. #ifdef BSP_USING_HWTIMER0
  182. void TIMER0_UP_IRQHandler(void)
  183. {
  184. timer_interrupt_flag_clear(hwtimer_obj[0].config->timer_periph, TIMER_INT_FLAG_UP);
  185. rt_device_hwtimer_isr(&hwtimer_obj[0].time_device);
  186. }
  187. #endif
  188. #ifdef BSP_USING_HWTIMER1
  189. void TIMER1_IRQHandler(void)
  190. {
  191. timer_interrupt_flag_clear(hwtimer_obj[1].config->timer_periph, TIMER_INT_FLAG_UP);
  192. rt_device_hwtimer_isr(&hwtimer_obj[1].time_device);
  193. }
  194. #endif
  195. #ifdef BSP_USING_HWTIMER2
  196. void TIMER2_IRQHandler(void)
  197. {
  198. timer_interrupt_flag_clear(hwtimer_obj[2].config->timer_periph, TIMER_INT_FLAG_UP);
  199. rt_device_hwtimer_isr(&hwtimer_obj[2].time_device);
  200. }
  201. #endif
  202. #ifdef BSP_USING_HWTIMER3
  203. void TIMER3_IRQHandler(void)
  204. {
  205. timer_interrupt_flag_clear(hwtimer_obj[3].config->timer_periph, TIMER_INT_FLAG_UP);
  206. rt_device_hwtimer_isr(&hwtimer_obj[3].time_device);
  207. }
  208. #endif
  209. #ifdef BSP_USING_HWTIMER4
  210. void TIMER4_IRQHandler(void)
  211. {
  212. timer_interrupt_flag_clear(hwtimer_obj[4].config->timer_periph, TIMER_INT_FLAG_UP);
  213. rt_device_hwtimer_isr(&hwtimer_obj[4].time_device);
  214. }
  215. #endif
  216. #ifdef BSP_USING_HWTIMER5
  217. void TIMER5_IRQHandler(void)
  218. {
  219. timer_interrupt_flag_clear(hwtimer_obj[5].config->timer_periph, TIMER_INT_FLAG_UP);
  220. rt_device_hwtimer_isr(&hwtimer_obj[5].time_device);
  221. }
  222. #endif
  223. #ifdef BSP_USING_HWTIMER6
  224. void TIMER6_IRQHandler(void)
  225. {
  226. timer_interrupt_flag_clear(hwtimer_obj[6].config->timer_periph, TIMER_INT_FLAG_UP);
  227. rt_device_hwtimer_isr(&hwtimer_obj[6].time_device);
  228. }
  229. #endif
  230. static int rt_hwtimer_init(void)
  231. {
  232. int i = 0;
  233. int result = RT_EOK;
  234. #ifdef BSP_USING_HWTIMER0
  235. rcu_periph_clock_enable(RCU_TIMER0);
  236. #endif
  237. #ifdef BSP_USING_HWTIMER1
  238. rcu_periph_clock_enable(RCU_TIMER1);
  239. #endif
  240. #ifdef BSP_USING_HWTIMER2
  241. rcu_periph_clock_enable(RCU_TIMER2);
  242. #endif
  243. #ifdef BSP_USING_HWTIMER3
  244. rcu_periph_clock_enable(RCU_TIMER3);
  245. #endif
  246. #ifdef BSP_USING_HWTIMER4
  247. rcu_periph_clock_enable(RCU_TIMER4);
  248. #endif
  249. #ifdef BSP_USING_HWTIMER5
  250. rcu_periph_clock_enable(RCU_TIMER5);
  251. #endif
  252. #ifdef BSP_USING_HWTIMER6
  253. rcu_periph_clock_enable(RCU_TIMER6);
  254. #endif
  255. for (i = 0; i < sizeof(hwtimer_obj) / sizeof(hwtimer_obj[0]); i++)
  256. {
  257. hwtimer_obj[i].time_device.info = &gd32_hwtimer_info;
  258. hwtimer_obj[i].time_device.ops = &gd32_hwtimer_ops;
  259. hwtimer_obj[i].config = &hwtimer_config[i];
  260. rt_device_hwtimer_register(&hwtimer_obj[i].time_device, \
  261. hwtimer_obj[i].config->name, hwtimer_obj[i].config);
  262. }
  263. return result;
  264. }
  265. INIT_DEVICE_EXPORT(rt_hwtimer_init);
  266. #endif /* RT_USING_HWTIMER */