drv_hwtimer.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-03-11 shelton first version
  9. */
  10. #include "board.h"
  11. #include "drv_hwtimer.h"
  12. //#define DRV_DEBUG
  13. #define LOG_TAG "drv.hwtimer"
  14. #include <drv_log.h>
  15. #ifdef BSP_USING_HWTIMER
  16. enum
  17. {
  18. #ifdef BSP_USING_HWTMR1
  19. TMR1_INDEX,
  20. #endif
  21. #ifdef BSP_USING_HWTMR2
  22. TMR2_INDEX,
  23. #endif
  24. #ifdef BSP_USING_HWTMR3
  25. TMR3_INDEX,
  26. #endif
  27. #ifdef BSP_USING_HWTMR4
  28. TMR4_INDEX,
  29. #endif
  30. #ifdef BSP_USING_HWTMR5
  31. TMR5_INDEX,
  32. #endif
  33. #ifdef BSP_USING_HWTMR6
  34. TMR6_INDEX,
  35. #endif
  36. #ifdef BSP_USING_HWTMR7
  37. TMR7_INDEX,
  38. #endif
  39. #ifdef BSP_USING_HW_TMR8
  40. TMR8_INDEX,
  41. #endif
  42. #ifdef BSP_USING_HWTMR9
  43. TMR9_INDEX,
  44. #endif
  45. #ifdef BSP_USING_HWTMR10
  46. TMR10_INDEX,
  47. #endif
  48. #ifdef BSP_USING_HWTMR11
  49. TMR11_INDEX,
  50. #endif
  51. #ifdef BSP_USING_HWTMR12
  52. TMR12_INDEX,
  53. #endif
  54. #ifdef BSP_USING_HWTMR13
  55. TMR13_INDEX,
  56. #endif
  57. #ifdef BSP_USING_HWTMR14
  58. TMR14_INDEX,
  59. #endif
  60. #ifdef BSP_USING_HWTMR15
  61. TMR15_INDEX,
  62. #endif
  63. };
  64. struct at32_hwtimer
  65. {
  66. rt_hwtimer_t tmr_device;
  67. tmr_type* tmr_x;
  68. IRQn_Type tmr_irqn;
  69. char *name;
  70. };
  71. static struct at32_hwtimer at32_hwtimer_obj[] =
  72. {
  73. #ifdef BSP_USING_HWTMR1
  74. TMR1_CONFIG,
  75. #endif
  76. #ifdef BSP_USING_HWTMR2
  77. TMR2_CONFIG,
  78. #endif
  79. #ifdef BSP_USING_HWTMR3
  80. TMR3_CONFIG,
  81. #endif
  82. #ifdef BSP_USING_HWTMR4
  83. TMR4_CONFIG,
  84. #endif
  85. #ifdef BSP_USING_HWTMR5
  86. TMR5_CONFIG,
  87. #endif
  88. #ifdef BSP_USING_HWTMR6
  89. TMR6_CONFIG,
  90. #endif
  91. #ifdef BSP_USING_HWTMR7
  92. TMR7_CONFIG,
  93. #endif
  94. #ifdef BSP_USING_HWTMR8
  95. TMR8_CONFIG,
  96. #endif
  97. #ifdef BSP_USING_HWTMR9
  98. TMR9_CONFIG,
  99. #endif
  100. #ifdef BSP_USING_HWTMR10
  101. TMR10_CONFIG,
  102. #endif
  103. #ifdef BSP_USING_HWTMR11
  104. TMR11_CONFIG,
  105. #endif
  106. #ifdef BSP_USING_HWTMR12
  107. TMR12_CONFIG,
  108. #endif
  109. #ifdef BSP_USING_HWTMR13
  110. TMR13_CONFIG,
  111. #endif
  112. #ifdef BSP_USING_HWTMR14
  113. TMR14_CONFIG,
  114. #endif
  115. #ifdef BSP_USING_HWTMR15
  116. TMR15_CONFIG,
  117. #endif
  118. };
  119. static void tmr_pclk_get(rt_uint32_t *pclk1_doubler, rt_uint32_t *pclk2_doubler)
  120. {
  121. crm_clocks_freq_type clocks_struct;
  122. *pclk1_doubler = 1;
  123. *pclk2_doubler = 1;
  124. crm_clocks_freq_get(&clocks_struct);
  125. if(clocks_struct.ahb_freq != clocks_struct.apb1_freq)
  126. {
  127. *pclk1_doubler = 2;
  128. }
  129. if(clocks_struct.ahb_freq != clocks_struct.apb2_freq)
  130. {
  131. *pclk2_doubler = 2;
  132. }
  133. }
  134. static void at32_timer_init(struct rt_hwtimer_device *timer, rt_uint32_t state)
  135. {
  136. crm_clocks_freq_type clocks_struct;
  137. rt_uint32_t pclk1_doubler = 0, pclk2_doubler = 0;
  138. rt_uint32_t prescaler_value = 0, tmr_clock = 0;
  139. tmr_type *tmr_x = RT_NULL;
  140. struct at32_hwtimer *tmr_device = RT_NULL;
  141. RT_ASSERT(timer != RT_NULL);
  142. if (state)
  143. {
  144. tmr_x = (tmr_type *)timer->parent.user_data;
  145. tmr_device = (struct at32_hwtimer *)timer;
  146. /* timer clock enable */
  147. at32_msp_hwtmr_init(tmr_x);
  148. /* get timer clock */
  149. tmr_pclk_get(&pclk1_doubler, &pclk2_doubler);
  150. crm_clocks_freq_get(&clocks_struct);
  151. if((tmr_x == TMR1) || (tmr_x == TMR8) || (tmr_x == TMR9) || (tmr_x == TMR10) || (tmr_x == TMR11))
  152. {
  153. tmr_clock = clocks_struct.apb2_freq * pclk2_doubler;
  154. }
  155. else
  156. {
  157. tmr_clock = clocks_struct.apb1_freq * pclk1_doubler;
  158. }
  159. /* set timer clock is 1mhz */
  160. prescaler_value = (uint32_t)(tmr_clock / 10000) - 1;
  161. tmr_base_init(tmr_x, 10000 - 1, prescaler_value);
  162. tmr_clock_source_div_set(tmr_x, TMR_CLOCK_DIV1);
  163. tmr_repetition_counter_set(tmr_x, 0);
  164. if (timer->info->cntmode == HWTIMER_CNTMODE_UP)
  165. {
  166. tmr_cnt_dir_set(tmr_x, TMR_COUNT_UP);
  167. }
  168. else
  169. {
  170. tmr_cnt_dir_set(tmr_x, TMR_COUNT_DOWN);
  171. }
  172. /* enable the timer global interrupt and clear flag */
  173. nvic_irq_enable(tmr_device->tmr_irqn, 2, 0);
  174. tmr_interrupt_enable(tmr_x, TMR_OVF_INT, TRUE);
  175. tmr_flag_clear(tmr_x, TMR_OVF_INT);
  176. LOG_D("%s init success", tmr_device->name);
  177. }
  178. }
  179. static rt_err_t at32_timer_start(rt_hwtimer_t *timer, rt_uint32_t pr, rt_hwtimer_mode_t opmode)
  180. {
  181. rt_err_t result = RT_EOK;
  182. tmr_type *tmr_x = RT_NULL;
  183. RT_ASSERT(timer != RT_NULL);
  184. tmr_x = (tmr_type *)timer->parent.user_data;
  185. /* set tmr_x count */
  186. tmr_counter_value_set(tmr_x, 0);
  187. /* set tmr_x period register */
  188. tmr_period_value_set(tmr_x, pr - 1);
  189. if (opmode == HWTIMER_MODE_ONESHOT)
  190. {
  191. /* set timer to one cycle mode */
  192. tmr_one_cycle_mode_enable(tmr_x, TRUE);
  193. }
  194. else
  195. {
  196. /* set timer to period mode */
  197. tmr_one_cycle_mode_enable(tmr_x, FALSE);
  198. }
  199. /* start timer */
  200. tmr_counter_enable(tmr_x, TRUE);
  201. return result;
  202. }
  203. static void at32_timer_stop(rt_hwtimer_t *timer)
  204. {
  205. tmr_type *tmr_x = RT_NULL;
  206. RT_ASSERT(timer != RT_NULL);
  207. tmr_x = (tmr_type *)timer->parent.user_data;
  208. /* stop timer */
  209. tmr_counter_enable(tmr_x, FALSE);
  210. /* set tmr_x count */
  211. tmr_counter_value_set(tmr_x, 0);
  212. }
  213. static rt_uint32_t at32_timer_counter_get(rt_hwtimer_t *timer)
  214. {
  215. tmr_type *tmr_x = RT_NULL;
  216. RT_ASSERT(timer != RT_NULL);
  217. tmr_x = (tmr_type *)timer->parent.user_data;
  218. return tmr_counter_value_get(tmr_x);
  219. }
  220. static rt_err_t at32_timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
  221. {
  222. crm_clocks_freq_type clocks_struct;
  223. tmr_type *tmr_x = RT_NULL;
  224. rt_err_t result = RT_EOK;
  225. rt_uint32_t pclk1_doubler = 0, pclk2_doubler = 0, tmr_clock = 0;
  226. RT_ASSERT(timer != RT_NULL);
  227. RT_ASSERT(arg != RT_NULL);
  228. tmr_x = (tmr_type *)timer->parent.user_data;
  229. switch(cmd)
  230. {
  231. case HWTIMER_CTRL_FREQ_SET:
  232. {
  233. rt_uint32_t freq;
  234. rt_uint16_t val;
  235. /* get timer frequence */
  236. freq = *((rt_uint32_t *)arg);
  237. /* get timer clock */
  238. tmr_pclk_get(&pclk1_doubler, &pclk2_doubler);
  239. crm_clocks_freq_get(&clocks_struct);
  240. if((tmr_x == TMR1) || (tmr_x == TMR8) || (tmr_x == TMR9) || (tmr_x == TMR10) || (tmr_x == TMR11))
  241. {
  242. tmr_clock = clocks_struct.apb2_freq * pclk2_doubler;
  243. }
  244. else
  245. {
  246. tmr_clock = clocks_struct.apb1_freq * pclk1_doubler;
  247. }
  248. /* set div value */
  249. val = tmr_clock / freq;
  250. tmr_div_value_set(tmr_x, val - 1);
  251. tmr_event_sw_trigger(tmr_x, TMR_OVERFLOW_SWTRIG);
  252. }
  253. break;
  254. default:
  255. {
  256. result = -RT_ENOSYS;
  257. }
  258. break;
  259. }
  260. return result;
  261. }
  262. static const struct rt_hwtimer_info _info = TMR_DEV_INFO_CONFIG;
  263. static const struct rt_hwtimer_ops _ops =
  264. {
  265. .init = at32_timer_init,
  266. .start = at32_timer_start,
  267. .stop = at32_timer_stop,
  268. .count_get = at32_timer_counter_get,
  269. .control = at32_timer_ctrl,
  270. };
  271. #ifdef BSP_USING_HWTMR2
  272. void TMR2_GLOBAL_IRQHandler(void)
  273. {
  274. /* enter interrupt */
  275. rt_interrupt_enter();
  276. if(tmr_flag_get(TMR2, TMR_OVF_FLAG) == SET)
  277. {
  278. rt_device_hwtimer_isr(&at32_hwtimer_obj[TMR2_INDEX].tmr_device);
  279. tmr_flag_clear(TMR2, TMR_OVF_FLAG);
  280. }
  281. /* leave interrupt */
  282. rt_interrupt_leave();
  283. }
  284. #endif
  285. #ifdef BSP_USING_HWTMR3
  286. void TMR3_GLOBAL_IRQHandler(void)
  287. {
  288. /* enter interrupt */
  289. rt_interrupt_enter();
  290. if(tmr_flag_get(TMR3, TMR_OVF_FLAG) == SET)
  291. {
  292. rt_device_hwtimer_isr(&at32_hwtimer_obj[TMR3_INDEX].tmr_device);
  293. tmr_flag_clear(TMR3, TMR_OVF_FLAG);
  294. }
  295. /* leave interrupt */
  296. rt_interrupt_leave();
  297. }
  298. #endif
  299. #ifdef BSP_USING_HWTMR4
  300. void TMR4_GLOBAL_IRQHandler(void)
  301. {
  302. /* enter interrupt */
  303. rt_interrupt_enter();
  304. if(tmr_flag_get(TMR4, TMR_OVF_FLAG) == SET)
  305. {
  306. rt_device_hwtimer_isr(&at32_hwtimer_obj[TMR4_INDEX].tmr_device);
  307. tmr_flag_clear(TMR4, TMR_OVF_FLAG);
  308. }
  309. /* leave interrupt */
  310. rt_interrupt_leave();
  311. }
  312. #endif
  313. #ifdef BSP_USING_HWTMR5
  314. void TMR5_GLOBAL_IRQHandler(void)
  315. {
  316. /* enter interrupt */
  317. rt_interrupt_enter();
  318. if(tmr_flag_get(TMR5, TMR_OVF_FLAG) == SET)
  319. {
  320. rt_device_hwtimer_isr(&at32_hwtimer_obj[TMR5_INDEX].tmr_device);
  321. tmr_flag_clear(TMR5, TMR_OVF_FLAG);
  322. }
  323. /* leave interrupt */
  324. rt_interrupt_leave();
  325. }
  326. #endif
  327. static int rt_hw_hwtimer_init(void)
  328. {
  329. int i = 0;
  330. int result = RT_EOK;
  331. for (i = 0; i < sizeof(at32_hwtimer_obj) / sizeof(at32_hwtimer_obj[0]); i++)
  332. {
  333. at32_hwtimer_obj[i].tmr_device.info = &_info;
  334. at32_hwtimer_obj[i].tmr_device.ops = &_ops;
  335. if (rt_device_hwtimer_register(&at32_hwtimer_obj[i].tmr_device, at32_hwtimer_obj[i].name, at32_hwtimer_obj[i].tmr_x) == RT_EOK)
  336. {
  337. LOG_D("%s register success", at32_hwtimer_obj[i].name);
  338. }
  339. else
  340. {
  341. LOG_E("%s register failed", at32_hwtimer_obj[i].name);
  342. result = -RT_ERROR;
  343. }
  344. }
  345. return result;
  346. }
  347. INIT_BOARD_EXPORT(rt_hw_hwtimer_init);
  348. #endif /* BSP_USING_HWTIMER */