drv_hwtimer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. * 2021-01-28 iysheng first version
  9. */
  10. #include <board.h>
  11. #include <drivers/drv_comm.h>
  12. #include <drivers/drv_hwtimer.h>
  13. #ifdef BSP_USING_HWTIMER
  14. enum timer_index_E {
  15. #ifdef BSP_USING_HWTIMER0
  16. TIM0_INDEX,
  17. #endif
  18. #ifdef BSP_USING_HWTIMER1
  19. TIM1_INDEX,
  20. #endif
  21. #ifdef BSP_USING_HWTIMER2
  22. TIM2_INDEX,
  23. #endif
  24. #ifdef BSP_USING_HWTIMER3
  25. TIM3_INDEX,
  26. #endif
  27. #ifdef BSP_USING_HWTIMER4
  28. TIM4_INDEX,
  29. #endif
  30. #ifdef BSP_USING_HWTIMER5
  31. TIM5_INDEX,
  32. #endif
  33. #ifdef BSP_USING_HWTIMER6
  34. TIM6_INDEX,
  35. #endif
  36. #ifdef BSP_USING_HWTIMER7
  37. TIM7_INDEX,
  38. #endif
  39. #ifdef BSP_USING_HWTIMER8
  40. TIM8_INDEX,
  41. #endif
  42. #ifdef BSP_USING_HWTIMER9
  43. TIM9_INDEX,
  44. #endif
  45. #ifdef BSP_USING_HWTIMER10
  46. TIM10_INDEX,
  47. #endif
  48. #ifdef BSP_USING_HWTIMER11
  49. TIM11_INDEX,
  50. #endif
  51. #ifdef BSP_USING_HWTIMER12
  52. TIM12_INDEX,
  53. #endif
  54. #ifdef BSP_USING_HWTIMER13
  55. TIM13_INDEX,
  56. #endif
  57. };
  58. /*
  59. * static void __set_timerx_freq
  60. * Set freq with timerx
  61. *
  62. * @param timerx the pointer of TIMER_TypeDef
  63. * @param freq of the timer clock
  64. * @retval None
  65. */
  66. static void __set_timerx_freq(TIMER_TypeDef *timerx, uint32_t freq)
  67. {
  68. RCC_ClocksPara RCC_Clocks = {0};
  69. uint16_t prescaler;
  70. uint32_t temp;
  71. RCC_GetClocksFreq(&RCC_Clocks);
  72. if (timerx == TIMER0 || timerx == TIMER7 || timerx == TIMER8 \
  73. || timerx == TIMER9 || timerx == TIMER10)
  74. {
  75. temp = RCC->GCFGR & RCC_GCFGR_APB2PS;
  76. temp >>= 11;
  77. /* whether should frequency doubling */
  78. temp = (temp < 4) ? 0 : 1;
  79. prescaler = (RCC_Clocks.APB2_Frequency << temp) / freq - 1;
  80. }
  81. else
  82. {
  83. temp = RCC->GCFGR & RCC_GCFGR_APB1PS;
  84. temp >>= 8;
  85. /* whether should frequency doubling */
  86. temp = (temp < 4) ? 0 : 1;
  87. prescaler = (RCC_Clocks.APB1_Frequency << temp) / freq - 1;
  88. }
  89. TIMER_PrescalerConfig(timerx, prescaler, TIMER_PSC_RELOAD_NOW);
  90. }
  91. static void gd32_hwtimer_init(struct rt_hwtimer_device *timer, rt_uint32_t state)
  92. {
  93. TIMER_TypeDef * timer_base = timer->parent.user_data;
  94. TIMER_BaseInitPara TIMER_Init;
  95. RT_ASSERT(timer_base);
  96. if (state)
  97. {
  98. TIMER_InternalClockConfig(timer_base);
  99. TIMER_BaseStructInit(&TIMER_Init);
  100. TIMER_Init.TIMER_Period = timer->info->maxcnt;
  101. TIMER_BaseInit(timer_base, &TIMER_Init);
  102. __set_timerx_freq(timer_base, timer->info->maxfreq);
  103. }
  104. }
  105. static rt_err_t gd32_hwtimer_start(struct rt_hwtimer_device *timer, \
  106. rt_uint32_t cnt, rt_hwtimer_mode_t mode)
  107. {
  108. TIMER_TypeDef * timer_base = timer->parent.user_data;
  109. if (mode == HWTIMER_MODE_ONESHOT)
  110. {
  111. TIMER_SinglePulseMode(timer_base, TIMER_SP_MODE_SINGLE);
  112. }
  113. else if (mode == HWTIMER_MODE_PERIOD)
  114. {
  115. TIMER_SinglePulseMode(timer_base, TIMER_SP_MODE_REPETITIVE);
  116. }
  117. TIMER_SetCounter(timer_base, 0);
  118. TIMER_SetAutoreload(timer_base, cnt - 1);
  119. TIMER_Enable(timer_base, ENABLE);
  120. return 0;
  121. }
  122. static void gd32_hwtimer_stop(struct rt_hwtimer_device *timer)
  123. {
  124. TIMER_TypeDef * timer_base = timer->parent.user_data;
  125. TIMER_Enable(timer_base, DISABLE);
  126. }
  127. static rt_uint32_t gd32_hwtimer_count_get(struct rt_hwtimer_device *timer)
  128. {
  129. TIMER_TypeDef * timer_base = timer->parent.user_data;
  130. rt_uint32_t count;
  131. count = TIMER_GetCounter(timer_base);
  132. return count;
  133. }
  134. static rt_err_t gd32_hwtimer_control(struct rt_hwtimer_device *timer, rt_uint32_t cmd, \
  135. void *args)
  136. {
  137. int ret = RT_EOK;
  138. rt_int32_t freq;
  139. rt_hwtimer_mode_t mode;
  140. switch (cmd)
  141. {
  142. case HWTIMER_CTRL_FREQ_SET:
  143. freq = *(rt_uint32_t *)args;
  144. __set_timerx_freq(timer->parent.user_data, freq);
  145. break;
  146. default:
  147. rt_kprintf("invalid cmd:%x\n", cmd);
  148. ret = -EINVAL;
  149. break;
  150. }
  151. return ret;
  152. }
  153. static const struct rt_hwtimer_ops g_gd32_hwtimer_ops = {
  154. gd32_hwtimer_init,
  155. gd32_hwtimer_start,
  156. gd32_hwtimer_stop,
  157. gd32_hwtimer_count_get,
  158. gd32_hwtimer_control,
  159. };
  160. static gd32_hwtimer_device g_gd32_hwtimer[] = {
  161. #ifdef BSP_USING_HWTIMER0
  162. {
  163. "timer0",
  164. {
  165. TIMER0,
  166. TIMER0_UP_IRQn,
  167. RCU_TIMER0,
  168. },
  169. {0},
  170. {
  171. 1000000,
  172. 1000,
  173. 0xffff,
  174. 0, /* count up mode */
  175. }
  176. },
  177. #endif
  178. #ifdef BSP_USING_HWTIMER1
  179. {
  180. "timer1",
  181. {
  182. TIMER1,
  183. TIMER1_IRQn,
  184. RCU_TIMER1,
  185. },
  186. {0},
  187. {
  188. 1000000,
  189. 1000,
  190. 0xffff,
  191. 0, /* count up mode */
  192. }
  193. },
  194. #endif
  195. #ifdef BSP_USING_HWTIMER2
  196. {
  197. "timer2",
  198. {
  199. TIMER2,
  200. TIMER2_IRQn,
  201. RCU_TIMER2,
  202. },
  203. {0},
  204. {
  205. 1000000,
  206. 1000,
  207. 0xffff,
  208. 0, /* count up mode */
  209. }
  210. },
  211. #endif
  212. #ifdef BSP_USING_HWTIMER3
  213. {
  214. "timer3",
  215. {
  216. TIMER3,
  217. TIMER3_IRQn,
  218. RCU_TIMER3,
  219. },
  220. {0},
  221. {
  222. 1000000,
  223. 1000,
  224. 0xffff,
  225. 0, /* count up mode */
  226. }
  227. },
  228. #endif
  229. #ifdef BSP_USING_HWTIMER4
  230. {
  231. "timer4",
  232. {
  233. TIMER4,
  234. TIMER4_IRQn,
  235. RCU_TIMER4,
  236. },
  237. {0},
  238. {
  239. 1000000,
  240. 1000,
  241. 0xffff,
  242. 0, /* count up mode */
  243. }
  244. },
  245. #endif
  246. #ifdef BSP_USING_HWTIMER5
  247. {
  248. "timer5",
  249. {
  250. TIMER5,
  251. TIMER5_IRQn,
  252. RCU_TIMER5,
  253. },
  254. {0},
  255. {
  256. 1000000,
  257. 1000,
  258. 0xffff,
  259. 0, /* count up mode */
  260. }
  261. },
  262. #endif
  263. #ifdef BSP_USING_HWTIMER6
  264. {
  265. "timer6",
  266. {
  267. TIMER6,
  268. TIMER6_IRQn,
  269. RCU_TIMER6,
  270. },
  271. {0},
  272. {
  273. 1000000,
  274. 1000,
  275. 0xffff,
  276. 0, /* count up mode */
  277. }
  278. },
  279. #endif
  280. #ifdef BSP_USING_HWTIMER7
  281. {
  282. "timer7",
  283. {
  284. TIMER7,
  285. TIMER7_UP_IRQn,
  286. RCU_TIMER7,
  287. },
  288. {0},
  289. {
  290. 1000000,
  291. 1000,
  292. 0xffff,
  293. 0, /* count up mode */
  294. }
  295. },
  296. #endif
  297. #ifdef BSP_USING_HWTIMER8
  298. {
  299. "timer8",
  300. {
  301. TIMER8,
  302. TIMER8_IRQn,
  303. RCU_TIMER8,
  304. },
  305. {0},
  306. {
  307. 1000000,
  308. 1000,
  309. 0xffff,
  310. 0, /* count up mode */
  311. }
  312. },
  313. #endif
  314. #ifdef BSP_USING_HWTIMER9
  315. {
  316. "timer9",
  317. {
  318. TIMER9,
  319. TIMER9_IRQn,
  320. RCU_TIMER9,
  321. },
  322. {0},
  323. {
  324. 1000000,
  325. 1000,
  326. 0xffff,
  327. 0, /* count up mode */
  328. }
  329. },
  330. #endif
  331. #ifdef BSP_USING_HWTIMER10
  332. {
  333. "timer10",
  334. {
  335. TIMER10,
  336. TIMER10_IRQn,
  337. RCU_TIMER10,
  338. },
  339. {0},
  340. {
  341. 1000000,
  342. 1000,
  343. 0xffff,
  344. 0, /* count up mode */
  345. }
  346. },
  347. #endif
  348. #ifdef BSP_USING_HWTIMER11
  349. {
  350. "timer11",
  351. {
  352. TIMER11,
  353. TIMER11_IRQn,
  354. RCU_TIMER11,
  355. },
  356. {0},
  357. {
  358. 1000000,
  359. 1000,
  360. 0xffff,
  361. 0, /* count up mode */
  362. }
  363. },
  364. #endif
  365. #ifdef BSP_USING_HWTIMER12
  366. {
  367. "timer12",
  368. {
  369. TIMER12,
  370. TIMER12_IRQn,
  371. RCU_TIMER12,
  372. },
  373. {0},
  374. {
  375. 1000000,
  376. 1000,
  377. 0xffff,
  378. 0, /* count up mode */
  379. }
  380. },
  381. #endif
  382. #ifdef BSP_USING_HWTIMER13
  383. {
  384. "timer13",
  385. {
  386. TIMER13,
  387. TIMER13_IRQn,
  388. RCU_TIMER13,
  389. },
  390. {0},
  391. {
  392. 1000000,
  393. 1000,
  394. 0xffff,
  395. 0, /* count up mode */
  396. }
  397. },
  398. #endif
  399. };
  400. #ifdef BSP_USING_HWTIMER0
  401. void TIMER0_UP_IRQHandler(void)
  402. {
  403. rt_interrupt_enter();
  404. rt_device_hwtimer_isr(&g_gd32_hwtimer[TIM0_INDEX].hwtimer_dev);
  405. TIMER_ClearIntBitState(g_gd32_hwtimer[TIM0_INDEX].hwtimer_dev.parent.user_data, \
  406. TIMER_INT_UPDATE);
  407. rt_interrupt_leave();
  408. }
  409. #endif
  410. #ifdef BSP_USING_HWTIMER1
  411. void TIMER1_IRQHandler(void)
  412. {
  413. rt_interrupt_enter();
  414. rt_device_hwtimer_isr(&g_gd32_hwtimer[TIM1_INDEX].hwtimer_dev);
  415. TIMER_ClearIntBitState(g_gd32_hwtimer[TIM1_INDEX].hwtimer_dev.parent.user_data, \
  416. TIMER_INT_UPDATE);
  417. rt_interrupt_leave();
  418. }
  419. #endif
  420. #ifdef BSP_USING_HWTIMER2
  421. void TIMER2_IRQHandler(void)
  422. {
  423. rt_interrupt_enter();
  424. rt_device_hwtimer_isr(&g_gd32_hwtimer[TIM2_INDEX].hwtimer_dev);
  425. TIMER_ClearIntBitState(g_gd32_hwtimer[TIM2_INDEX].hwtimer_dev.parent.user_data, \
  426. TIMER_INT_UPDATE);
  427. rt_interrupt_leave();
  428. }
  429. #endif
  430. #ifdef BSP_USING_HWTIMER3
  431. void TIMER3_IRQHandler(void)
  432. {
  433. rt_interrupt_enter();
  434. rt_device_hwtimer_isr(&g_gd32_hwtimer[TIM3_INDEX].hwtimer_dev);
  435. TIMER_ClearIntBitState(g_gd32_hwtimer[TIM3_INDEX].hwtimer_dev.parent.user_data, \
  436. TIMER_INT_UPDATE);
  437. rt_interrupt_leave();
  438. }
  439. #endif
  440. #ifdef BSP_USING_HWTIMER4
  441. void TIMER4_IRQHandler(void)
  442. {
  443. rt_interrupt_enter();
  444. rt_device_hwtimer_isr(&g_gd32_hwtimer[TIM4_INDEX].hwtimer_dev);
  445. TIMER_ClearIntBitState(g_gd32_hwtimer[TIM4_INDEX].hwtimer_dev.parent.user_data, \
  446. TIMER_INT_UPDATE);
  447. rt_interrupt_leave();
  448. }
  449. #endif
  450. #ifdef BSP_USING_HWTIMER5
  451. void TIMER5_IRQHandler(void)
  452. {
  453. rt_interrupt_enter();
  454. rt_device_hwtimer_isr(&g_gd32_hwtimer[TIM5_INDEX].hwtimer_dev);
  455. TIMER_ClearIntBitState(g_gd32_hwtimer[TIM5_INDEX].hwtimer_dev.parent.user_data, \
  456. TIMER_INT_UPDATE);
  457. rt_interrupt_leave();
  458. }
  459. #endif
  460. #ifdef BSP_USING_HWTIMER6
  461. void TIMER6_IRQHandler(void)
  462. {
  463. rt_interrupt_enter();
  464. rt_device_hwtimer_isr(&g_gd32_hwtimer[TIM6_INDEX].hwtimer_dev);
  465. TIMER_ClearIntBitState(g_gd32_hwtimer[TIM6_INDEX].hwtimer_dev.parent.user_data, \
  466. TIMER_INT_UPDATE);
  467. rt_interrupt_leave();
  468. }
  469. #endif
  470. #ifdef BSP_USING_HWTIMER7
  471. void TIMER7_UP_IRQHandler(void)
  472. {
  473. rt_interrupt_enter();
  474. rt_device_hwtimer_isr(&g_gd32_hwtimer[TIM7_INDEX].hwtimer_dev);
  475. TIMER_ClearIntBitState(g_gd32_hwtimer[TIM7_INDEX].hwtimer_dev.parent.user_data, \
  476. TIMER_INT_UPDATE);
  477. rt_interrupt_leave();
  478. }
  479. #endif
  480. static int rt_hwtimer_init(void)
  481. {
  482. int ret = 0, i = 0;
  483. for (; i < ARRAY_SIZE(g_gd32_hwtimer); i++)
  484. {
  485. g_gd32_hwtimer[i].hwtimer_dev.ops = &g_gd32_hwtimer_ops;
  486. g_gd32_hwtimer[i].hwtimer_dev.info = &g_gd32_hwtimer[i].hwtimer_info;
  487. rcu_periph_clock_enable(g_gd32_hwtimer[i].hw_data.rcu);
  488. NVIC_SetPriority(g_gd32_hwtimer[i].hw_data.irqn, 0);
  489. NVIC_EnableIRQ(g_gd32_hwtimer[i].hw_data.irqn);
  490. TIMER_INTConfig(g_gd32_hwtimer[i].hw_data.reg_base, TIMER_INT_UPDATE, ENABLE);
  491. ret = rt_device_hwtimer_register(&g_gd32_hwtimer[i].hwtimer_dev, \
  492. g_gd32_hwtimer[i].dev_name, g_gd32_hwtimer[i].hw_data.reg_base);
  493. if (RT_EOK != ret)
  494. {
  495. rt_kprintf("failed register %s, err=%d\n", g_gd32_hwtimer[i].dev_name, \
  496. ret);
  497. break;
  498. }
  499. }
  500. return ret;
  501. }
  502. INIT_BOARD_EXPORT(rt_hwtimer_init);
  503. #endif