drv_hwtimer.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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-05-16 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(
  152. #if defined (TMR1)
  153. (tmr_x == TMR1)
  154. #endif
  155. #if defined (TMR8)
  156. || (tmr_x == TMR8)
  157. #endif
  158. #if defined (TMR9)
  159. || (tmr_x == TMR9)
  160. #endif
  161. #if defined (TMR10)
  162. || (tmr_x == TMR10)
  163. #endif
  164. #if defined (TMR11)
  165. || (tmr_x == TMR11)
  166. #endif
  167. )
  168. {
  169. tmr_clock = clocks_struct.apb2_freq * pclk2_doubler;
  170. }
  171. else
  172. {
  173. tmr_clock = clocks_struct.apb1_freq * pclk1_doubler;
  174. }
  175. /* set timer clock is 1mhz */
  176. prescaler_value = (uint32_t)(tmr_clock / 10000) - 1;
  177. tmr_base_init(tmr_x, 10000 - 1, prescaler_value);
  178. tmr_clock_source_div_set(tmr_x, TMR_CLOCK_DIV1);
  179. tmr_repetition_counter_set(tmr_x, 0);
  180. if (timer->info->cntmode == HWTIMER_CNTMODE_UP)
  181. {
  182. tmr_cnt_dir_set(tmr_x, TMR_COUNT_UP);
  183. }
  184. else
  185. {
  186. tmr_cnt_dir_set(tmr_x, TMR_COUNT_DOWN);
  187. }
  188. /* enable the timer global interrupt and clear flag */
  189. nvic_irq_enable(tmr_device->tmr_irqn, 2, 0);
  190. tmr_interrupt_enable(tmr_x, TMR_OVF_INT, TRUE);
  191. tmr_flag_clear(tmr_x, TMR_OVF_INT);
  192. LOG_D("%s init success", tmr_device->name);
  193. }
  194. }
  195. static rt_err_t at32_timer_start(rt_hwtimer_t *timer, rt_uint32_t pr, rt_hwtimer_mode_t opmode)
  196. {
  197. rt_err_t result = RT_EOK;
  198. tmr_type *tmr_x = RT_NULL;
  199. RT_ASSERT(timer != RT_NULL);
  200. tmr_x = (tmr_type *)timer->parent.user_data;
  201. /* set tmr_x count */
  202. tmr_counter_value_set(tmr_x, 0);
  203. /* set tmr_x period register */
  204. tmr_period_value_set(tmr_x, pr - 1);
  205. if (opmode == HWTIMER_MODE_ONESHOT)
  206. {
  207. /* set timer to one cycle mode */
  208. tmr_one_cycle_mode_enable(tmr_x, TRUE);
  209. }
  210. else
  211. {
  212. /* set timer to period mode */
  213. tmr_one_cycle_mode_enable(tmr_x, FALSE);
  214. }
  215. /* start timer */
  216. tmr_counter_enable(tmr_x, TRUE);
  217. return result;
  218. }
  219. static void at32_timer_stop(rt_hwtimer_t *timer)
  220. {
  221. tmr_type *tmr_x = RT_NULL;
  222. RT_ASSERT(timer != RT_NULL);
  223. tmr_x = (tmr_type *)timer->parent.user_data;
  224. /* stop timer */
  225. tmr_counter_enable(tmr_x, FALSE);
  226. /* set tmr_x count */
  227. tmr_counter_value_set(tmr_x, 0);
  228. }
  229. static rt_uint32_t at32_timer_counter_get(rt_hwtimer_t *timer)
  230. {
  231. tmr_type *tmr_x = RT_NULL;
  232. RT_ASSERT(timer != RT_NULL);
  233. tmr_x = (tmr_type *)timer->parent.user_data;
  234. return tmr_counter_value_get(tmr_x);
  235. }
  236. static rt_err_t at32_timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
  237. {
  238. crm_clocks_freq_type clocks_struct;
  239. tmr_type *tmr_x = RT_NULL;
  240. rt_err_t result = RT_EOK;
  241. rt_uint32_t pclk1_doubler = 0, pclk2_doubler = 0, tmr_clock = 0;
  242. RT_ASSERT(timer != RT_NULL);
  243. RT_ASSERT(arg != RT_NULL);
  244. tmr_x = (tmr_type *)timer->parent.user_data;
  245. switch(cmd)
  246. {
  247. case HWTIMER_CTRL_FREQ_SET:
  248. {
  249. rt_uint32_t freq;
  250. rt_uint16_t val;
  251. /* get timer frequence */
  252. freq = *((rt_uint32_t *)arg);
  253. /* get timer clock */
  254. tmr_pclk_get(&pclk1_doubler, &pclk2_doubler);
  255. crm_clocks_freq_get(&clocks_struct);
  256. if(
  257. #if defined (TMR1)
  258. (tmr_x == TMR1)
  259. #endif
  260. #if defined (TMR8)
  261. || (tmr_x == TMR8)
  262. #endif
  263. #if defined (TMR9)
  264. || (tmr_x == TMR9)
  265. #endif
  266. #if defined (TMR10)
  267. || (tmr_x == TMR10)
  268. #endif
  269. #if defined (TMR11)
  270. || (tmr_x == TMR11)
  271. #endif
  272. )
  273. {
  274. tmr_clock = clocks_struct.apb2_freq * pclk2_doubler;
  275. }
  276. else
  277. {
  278. tmr_clock = clocks_struct.apb1_freq * pclk1_doubler;
  279. }
  280. /* set div value */
  281. val = tmr_clock / freq;
  282. tmr_div_value_set(tmr_x, val - 1);
  283. tmr_event_sw_trigger(tmr_x, TMR_OVERFLOW_SWTRIG);
  284. }
  285. break;
  286. default:
  287. {
  288. result = -RT_ENOSYS;
  289. }
  290. break;
  291. }
  292. return result;
  293. }
  294. static const struct rt_hwtimer_info _info = TMR_DEV_INFO_CONFIG;
  295. static const struct rt_hwtimer_ops _ops =
  296. {
  297. .init = at32_timer_init,
  298. .start = at32_timer_start,
  299. .stop = at32_timer_stop,
  300. .count_get = at32_timer_counter_get,
  301. .control = at32_timer_ctrl,
  302. };
  303. #ifdef BSP_USING_HWTMR2
  304. void TMR2_GLOBAL_IRQHandler(void)
  305. {
  306. /* enter interrupt */
  307. rt_interrupt_enter();
  308. if(tmr_flag_get(TMR2, TMR_OVF_FLAG) == SET)
  309. {
  310. rt_device_hwtimer_isr(&at32_hwtimer_obj[TMR2_INDEX].tmr_device);
  311. tmr_flag_clear(TMR2, TMR_OVF_FLAG);
  312. }
  313. /* leave interrupt */
  314. rt_interrupt_leave();
  315. }
  316. #endif
  317. #ifdef BSP_USING_HWTMR3
  318. void TMR3_GLOBAL_IRQHandler(void)
  319. {
  320. /* enter interrupt */
  321. rt_interrupt_enter();
  322. if(tmr_flag_get(TMR3, TMR_OVF_FLAG) == SET)
  323. {
  324. rt_device_hwtimer_isr(&at32_hwtimer_obj[TMR3_INDEX].tmr_device);
  325. tmr_flag_clear(TMR3, TMR_OVF_FLAG);
  326. }
  327. /* leave interrupt */
  328. rt_interrupt_leave();
  329. }
  330. #endif
  331. #ifdef BSP_USING_HWTMR4
  332. void TMR4_GLOBAL_IRQHandler(void)
  333. {
  334. /* enter interrupt */
  335. rt_interrupt_enter();
  336. if(tmr_flag_get(TMR4, TMR_OVF_FLAG) == SET)
  337. {
  338. rt_device_hwtimer_isr(&at32_hwtimer_obj[TMR4_INDEX].tmr_device);
  339. tmr_flag_clear(TMR4, TMR_OVF_FLAG);
  340. }
  341. /* leave interrupt */
  342. rt_interrupt_leave();
  343. }
  344. #endif
  345. #ifdef BSP_USING_HWTMR5
  346. void TMR5_GLOBAL_IRQHandler(void)
  347. {
  348. /* enter interrupt */
  349. rt_interrupt_enter();
  350. if(tmr_flag_get(TMR5, TMR_OVF_FLAG) == SET)
  351. {
  352. rt_device_hwtimer_isr(&at32_hwtimer_obj[TMR5_INDEX].tmr_device);
  353. tmr_flag_clear(TMR5, TMR_OVF_FLAG);
  354. }
  355. /* leave interrupt */
  356. rt_interrupt_leave();
  357. }
  358. #endif
  359. static int rt_hw_hwtimer_init(void)
  360. {
  361. int i = 0;
  362. int result = RT_EOK;
  363. for (i = 0; i < sizeof(at32_hwtimer_obj) / sizeof(at32_hwtimer_obj[0]); i++)
  364. {
  365. at32_hwtimer_obj[i].tmr_device.info = &_info;
  366. at32_hwtimer_obj[i].tmr_device.ops = &_ops;
  367. if (rt_device_hwtimer_register(&at32_hwtimer_obj[i].tmr_device, at32_hwtimer_obj[i].name, at32_hwtimer_obj[i].tmr_x) == RT_EOK)
  368. {
  369. LOG_D("%s register success", at32_hwtimer_obj[i].name);
  370. }
  371. else
  372. {
  373. LOG_E("%s register failed", at32_hwtimer_obj[i].name);
  374. result = -RT_ERROR;
  375. }
  376. }
  377. return result;
  378. }
  379. INIT_BOARD_EXPORT(rt_hw_hwtimer_init);
  380. #endif /* BSP_USING_HWTIMER */