drv_hwtimer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-03-04 stevetong459 first version
  9. * 2022-07-15 Aligagago add apm32F4 serie MCU support
  10. */
  11. #include <board.h>
  12. #define LOG_TAG "drv.hwtimer"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. #ifdef RT_USING_HWTIMER
  16. static const struct rt_hwtimer_info _info =
  17. {
  18. .maxfreq = 1000000,
  19. .minfreq = 2000,
  20. .maxcnt = 0xFFFF,
  21. .cntmode = HWTIMER_CNTMODE_UP,
  22. };
  23. /* apm32 config class */
  24. struct apm32_timer
  25. {
  26. char *name;
  27. TMR_T *tmr;
  28. IRQn_Type irqn;
  29. rt_hwtimer_t device;
  30. };
  31. enum
  32. {
  33. #ifdef BSP_USING_TMR1
  34. TMR1_INDEX,
  35. #endif
  36. #ifdef BSP_USING_TMR2
  37. TMR2_INDEX,
  38. #endif
  39. #ifdef BSP_USING_TMR3
  40. TMR3_INDEX,
  41. #endif
  42. #ifdef BSP_USING_TMR4
  43. TMR4_INDEX,
  44. #endif
  45. #ifdef BSP_USING_TMR5
  46. TMR5_INDEX,
  47. #endif
  48. #ifdef BSP_USING_TMR6
  49. TMR6_INDEX,
  50. #endif
  51. #ifdef BSP_USING_TMR7
  52. TMR7_INDEX,
  53. #endif
  54. #ifdef BSP_USING_TMR8
  55. TMR8_INDEX,
  56. #endif
  57. #ifdef BSP_USING_TMR9
  58. TMR9_INDEX,
  59. #endif
  60. #ifdef BSP_USING_TMR10
  61. TMR10_INDEX,
  62. #endif
  63. #ifdef BSP_USING_TMR11
  64. TMR11_INDEX,
  65. #endif
  66. #ifdef BSP_USING_TMR12
  67. TMR12_INDEX,
  68. #endif
  69. #ifdef BSP_USING_TMR13
  70. TMR13_INDEX,
  71. #endif
  72. #ifdef BSP_USING_TMR14
  73. TMR14_INDEX,
  74. #endif
  75. };
  76. static struct apm32_timer tmr_config[] =
  77. {
  78. #ifdef BSP_USING_TMR1
  79. {
  80. "timer1",
  81. TMR1,
  82. #ifdef APM32F10X_HD
  83. TMR1_UP_IRQn,
  84. #elif APM32F40X
  85. TMR1_UP_TMR10_IRQn,
  86. #endif
  87. },
  88. #endif
  89. #ifdef BSP_USING_TMR2
  90. {
  91. "timer2",
  92. TMR2,
  93. TMR2_IRQn,
  94. },
  95. #endif
  96. #ifdef BSP_USING_TMR3
  97. {
  98. "timer3",
  99. TMR3,
  100. TMR3_IRQn,
  101. },
  102. #endif
  103. #ifdef BSP_USING_TMR4
  104. {
  105. "timer4",
  106. TMR4,
  107. TMR4_IRQn,
  108. },
  109. #endif
  110. #ifdef BSP_USING_TMR5
  111. {
  112. "timer5",
  113. TMR5,
  114. TMR5_IRQn,
  115. },
  116. #endif
  117. #ifdef BSP_USING_TMR6
  118. {
  119. "timer6",
  120. TMR6,
  121. #ifdef APM32F10X_HD
  122. TMR6_IRQn,
  123. #elif APM32F40X
  124. TMR6_DAC_IRQn
  125. #endif
  126. },
  127. #endif
  128. #ifdef BSP_USING_TMR7
  129. {
  130. "timer7",
  131. TMR7,
  132. TMR7_IRQn,
  133. },
  134. #endif
  135. #ifdef BSP_USING_TMR8
  136. {
  137. "timer8",
  138. TMR8,
  139. #ifdef APM32F10X_HD
  140. TMR8_UP_IRQn,
  141. #elif APM32F40X
  142. TMR8_UP_TMR13_IRQn,
  143. #endif
  144. },
  145. #endif
  146. #ifdef BSP_USING_TMR9
  147. {
  148. "timer9",
  149. TMR9,
  150. TMR1_BRK_TMR9_IRQn,
  151. },
  152. #endif
  153. #ifdef BSP_USING_TMR10
  154. {
  155. "timer10",
  156. TMR10,
  157. TMR1_UP_TMR10_IRQn,
  158. },
  159. #endif
  160. #ifdef BSP_USING_TMR11
  161. {
  162. "timer11",
  163. TMR11,
  164. TMR1_TRG_COM_TMR11_IRQn,
  165. },
  166. #endif
  167. #ifdef BSP_USING_TMR12
  168. {
  169. "timer12",
  170. TMR12,
  171. TMR8_BRK_TMR12_IRQn,
  172. },
  173. #endif
  174. #ifdef BSP_USING_TMR13
  175. {
  176. "timer13",
  177. TMR13,
  178. TMR8_UP_TMR13_IRQn,
  179. },
  180. #endif
  181. #ifdef BSP_USING_TMR14
  182. {
  183. "timer14",
  184. TMR14,
  185. TMR8_TRG_COM_TMR14_IRQn,
  186. },
  187. #endif
  188. };
  189. static rt_uint32_t _hwtimer_clock_get(TMR_T *tmr)
  190. {
  191. uint32_t pclk1, pclk2;
  192. RCM_ReadPCLKFreq(&pclk1, &pclk2);
  193. if (tmr == TMR1 || tmr == TMR8 || tmr == TMR9 || tmr == TMR10 || tmr == TMR11)
  194. {
  195. return (rt_uint32_t)(pclk2 * ((RCM->CFG_B.APB2PSC != RCM_APB_DIV_1) ? 2 : 1));
  196. }
  197. else
  198. {
  199. return (rt_uint32_t)(pclk1 * ((RCM->CFG_B.APB1PSC != RCM_APB_DIV_1) ? 2 : 1));
  200. }
  201. }
  202. static void _hwtimer_init(struct rt_hwtimer_device *timer, rt_uint32_t state)
  203. {
  204. TMR_BaseConfig_T base_config;
  205. uint32_t prescaler = 0;
  206. struct apm32_timer *timer_config;
  207. RT_ASSERT(timer != RT_NULL);
  208. if (state)
  209. {
  210. timer_config = (struct apm32_timer *)timer->parent.user_data;
  211. if (timer_config->tmr == TMR2)
  212. {
  213. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR2);
  214. }
  215. else if (timer_config->tmr == TMR3)
  216. {
  217. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR3);
  218. }
  219. else if (timer_config->tmr == TMR4)
  220. {
  221. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR4);
  222. }
  223. else if (timer_config->tmr == TMR5)
  224. {
  225. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR5);
  226. }
  227. else if (timer_config->tmr == TMR6)
  228. {
  229. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR6);
  230. }
  231. else if (timer_config->tmr == TMR7)
  232. {
  233. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR7);
  234. }
  235. #ifdef APM32F10X_HD
  236. else if (timer_config->tmr == TMR1)
  237. {
  238. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR1);
  239. }
  240. else if (timer_config->tmr == TMR8)
  241. {
  242. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR8);
  243. }
  244. #endif
  245. #ifdef APM32F40X
  246. else if (timer_config->tmr == TMR1)
  247. {
  248. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR1);
  249. }
  250. else if (timer_config->tmr == TMR8)
  251. {
  252. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR8);
  253. }
  254. else if (timer_config->tmr == TMR9)
  255. {
  256. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR9);
  257. }
  258. else if (timer_config->tmr == TMR10)
  259. {
  260. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR10);
  261. }
  262. else if (timer_config->tmr == TMR11)
  263. {
  264. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR11);
  265. }
  266. else if (timer_config->tmr == TMR12)
  267. {
  268. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR12);
  269. }
  270. else if (timer_config->tmr == TMR13)
  271. {
  272. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR13);
  273. }
  274. else if (timer_config->tmr == TMR14)
  275. {
  276. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR14);
  277. }
  278. #endif
  279. prescaler = (uint32_t)(_hwtimer_clock_get(timer_config->tmr) / 10000) - 1;
  280. base_config.period = 10000 - 1;
  281. base_config.division = prescaler;
  282. base_config.clockDivision = TMR_CLOCK_DIV_1;
  283. if (timer->info->cntmode == HWTIMER_CNTMODE_UP)
  284. {
  285. base_config.countMode = TMR_COUNTER_MODE_UP;
  286. }
  287. else
  288. {
  289. base_config.countMode = TMR_COUNTER_MODE_DOWN;
  290. }
  291. base_config.repetitionCounter = 0;
  292. TMR_ConfigTimeBase(timer_config->tmr, &base_config);
  293. /* set the TIMx priority */
  294. NVIC_EnableIRQRequest(timer_config->irqn, 3, 0);
  295. /* clear update flag */
  296. TMR_ClearStatusFlag(timer_config->tmr, TMR_FLAG_UPDATE);
  297. /* enable update request source */
  298. TMR_ConfigUpdateRequest(timer_config->tmr, TMR_UPDATE_SOURCE_REGULAR);
  299. LOG_D("%s init success", timer_config->name);
  300. }
  301. }
  302. static rt_err_t _hwtimer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode)
  303. {
  304. rt_err_t result = RT_EOK;
  305. struct apm32_timer *timer_config = RT_NULL;
  306. RT_ASSERT(timer != RT_NULL);
  307. timer_config = (struct apm32_timer *)timer->parent.user_data;
  308. /* set timer_config counter */
  309. TMR_ConfigCounter(timer_config->tmr, 0);
  310. /* set timer_config autoReload */
  311. TMR_ConfigAutoreload(timer_config->tmr, t - 1);
  312. if (opmode == HWTIMER_MODE_ONESHOT)
  313. {
  314. /* set timer to single mode */
  315. TMR_ConfigSinglePulseMode(timer_config->tmr, TMR_SPM_SINGLE);
  316. }
  317. else
  318. {
  319. TMR_ConfigSinglePulseMode(timer_config->tmr, TMR_SPM_REPETITIVE);
  320. }
  321. TMR_EnableInterrupt(timer_config->tmr, TMR_INT_UPDATE);
  322. if (timer_config->tmr == TMR1 || timer_config->tmr == TMR8 || timer_config->tmr == TMR2 || \
  323. timer_config->tmr == TMR3 || timer_config->tmr == TMR4 || timer_config->tmr == TMR5 || \
  324. timer_config->tmr == TMR9 || timer_config->tmr == TMR12)
  325. {
  326. if (timer_config->tmr->SMCTRL_B.SMFSEL != TMR_SLAVE_MODE_TRIGGER)
  327. {
  328. TMR_Enable(timer_config->tmr);
  329. result = -RT_EOK;
  330. }
  331. }
  332. else
  333. {
  334. TMR_Enable(timer_config->tmr);
  335. result = -RT_EOK;
  336. }
  337. return result;
  338. }
  339. static void _hwtimer_stop(rt_hwtimer_t *timer)
  340. {
  341. struct apm32_timer *timer_config = RT_NULL;
  342. RT_ASSERT(timer != RT_NULL);
  343. timer_config = (struct apm32_timer *)timer->parent.user_data;
  344. TMR_DisableInterrupt(timer_config->tmr, TMR_INT_UPDATE);
  345. TMR_Enable(timer_config->tmr);
  346. TMR_ConfigCounter(timer_config->tmr, 0);
  347. }
  348. static rt_err_t _hwtimer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
  349. {
  350. struct apm32_timer *timer_config = RT_NULL;
  351. rt_err_t result = RT_EOK;
  352. rt_uint32_t freq;
  353. rt_uint16_t val;
  354. RT_ASSERT(timer != RT_NULL);
  355. RT_ASSERT(arg != RT_NULL);
  356. timer_config = (struct apm32_timer *)timer->parent.user_data;
  357. switch (cmd)
  358. {
  359. case HWTIMER_CTRL_FREQ_SET:
  360. /* set timer frequence */
  361. freq = *((rt_uint32_t *)arg);
  362. val = _hwtimer_clock_get(timer_config->tmr) / freq;
  363. TMR_ConfigPrescaler(timer_config->tmr, val - 1, TMR_PSC_RELOAD_IMMEDIATE);
  364. break;
  365. default:
  366. result = -RT_ENOSYS;
  367. break;
  368. }
  369. return result;
  370. }
  371. static rt_uint32_t _hwtimer_counter_get(rt_hwtimer_t *timer)
  372. {
  373. struct apm32_timer *timer_config = RT_NULL;
  374. RT_ASSERT(timer != RT_NULL);
  375. timer_config = (struct apm32_timer *)timer->parent.user_data;
  376. return timer_config->tmr->CNT;
  377. }
  378. static const struct rt_hwtimer_ops _hwtimer_ops =
  379. {
  380. .init = _hwtimer_init,
  381. .start = _hwtimer_start,
  382. .stop = _hwtimer_stop,
  383. .count_get = _hwtimer_counter_get,
  384. .control = _hwtimer_ctrl,
  385. };
  386. #ifdef APM32F10X_HD
  387. #ifdef BSP_USING_TMR1
  388. void TMR1_UP_IRQHandler(void)
  389. {
  390. rt_interrupt_enter();
  391. rt_device_hwtimer_isr(&tmr_config[TMR1_INDEX].device);
  392. TMR_ClearIntFlag(TMR1, TMR_INT_UPDATE);
  393. rt_interrupt_leave();
  394. }
  395. #endif
  396. #elif APM32F40X
  397. #if (defined(BSP_USING_TMR1) || defined(BSP_USING_TMR10))
  398. void TMR1_UP_TMR10_IRQHandler(void)
  399. {
  400. rt_interrupt_enter();
  401. if (TMR_ReadIntFlag(TMR1, TMR_INT_UPDATE))
  402. {
  403. rt_device_hwtimer_isr(&tmr_config[TMR1_INDEX].device);
  404. TMR_ClearIntFlag(TMR1, TMR_INT_UPDATE);
  405. }
  406. if (TMR_ReadIntFlag(TMR10, TMR_INT_UPDATE))
  407. {
  408. rt_device_hwtimer_isr(&tmr_config[TMR10_INDEX].device);
  409. TMR_ClearIntFlag(TMR10, TMR_INT_UPDATE);
  410. }
  411. rt_interrupt_leave();
  412. }
  413. #endif
  414. #endif
  415. #ifdef BSP_USING_TMR2
  416. void TMR2_IRQHandler(void)
  417. {
  418. rt_interrupt_enter();
  419. rt_device_hwtimer_isr(&tmr_config[TMR2_INDEX].device);
  420. TMR_ClearIntFlag(TMR2, TMR_INT_UPDATE);
  421. rt_interrupt_leave();
  422. }
  423. #endif
  424. #ifdef BSP_USING_TMR3
  425. void TMR3_IRQHandler(void)
  426. {
  427. rt_interrupt_enter();
  428. rt_device_hwtimer_isr(&tmr_config[TMR3_INDEX].device);
  429. TMR_ClearIntFlag(TMR3, TMR_INT_UPDATE);
  430. rt_interrupt_leave();
  431. }
  432. #endif
  433. #ifdef BSP_USING_TMR4
  434. void TMR4_IRQHandler(void)
  435. {
  436. rt_interrupt_enter();
  437. rt_device_hwtimer_isr(&tmr_config[TMR4_INDEX].device);
  438. TMR_ClearIntFlag(TMR4, TMR_INT_UPDATE);
  439. rt_interrupt_leave();
  440. }
  441. #endif
  442. #ifdef BSP_USING_TMR5
  443. void TMR5_IRQHandler(void)
  444. {
  445. rt_interrupt_enter();
  446. rt_device_hwtimer_isr(&tmr_config[TMR5_INDEX].device);
  447. TMR_ClearIntFlag(TMR5, TMR_INT_UPDATE);
  448. rt_interrupt_leave();
  449. }
  450. #endif
  451. #ifdef BSP_USING_TMR6
  452. #ifdef APM32F10X_HD
  453. void TMR6_IRQHandler(void)
  454. #elif APM32F40X
  455. void TMR6_DAC_IRQHandler(void)
  456. #endif
  457. {
  458. rt_interrupt_enter();
  459. rt_device_hwtimer_isr(&tmr_config[TMR6_INDEX].device);
  460. TMR_ClearIntFlag(TMR6, TMR_INT_UPDATE);
  461. rt_interrupt_leave();
  462. }
  463. #endif
  464. #ifdef BSP_USING_TMR7
  465. void TMR7_IRQHandler(void)
  466. {
  467. rt_interrupt_enter();
  468. rt_device_hwtimer_isr(&tmr_config[TMR7_INDEX].device);
  469. TMR_ClearIntFlag(TMR7, TMR_INT_UPDATE);
  470. rt_interrupt_leave();
  471. }
  472. #endif
  473. #ifdef APM32F10X_HD
  474. #ifdef BSP_USING_TMR8
  475. void TMR8_UP_IRQHandler(void)
  476. {
  477. rt_interrupt_enter();
  478. rt_device_hwtimer_isr(&tmr_config[TMR8_INDEX].device);
  479. TMR_ClearIntFlag(TMR8, TMR_INT_UPDATE);
  480. rt_interrupt_leave();
  481. }
  482. #endif
  483. #elif APM32F40X
  484. #if (defined(BSP_USING_TMR8) || defined(BSP_USING_TMR13))
  485. void TMR8_UP_TMR13_IRQHandler(void)
  486. {
  487. rt_interrupt_enter();
  488. if (TMR_ReadIntFlag(TMR8, TMR_INT_UPDATE))
  489. {
  490. rt_device_hwtimer_isr(&tmr_config[TMR8_INDEX].device);
  491. TMR_ClearIntFlag(TMR8, TMR_INT_UPDATE);
  492. }
  493. if (TMR_ReadIntFlag(TMR13, TMR_INT_UPDATE))
  494. {
  495. rt_device_hwtimer_isr(&tmr_config[TMR13_INDEX].device);
  496. TMR_ClearIntFlag(TMR13, TMR_INT_UPDATE);
  497. }
  498. rt_interrupt_leave();
  499. }
  500. #endif
  501. #endif
  502. #ifdef BSP_USING_TMR9
  503. void TMR1_BRK_TMR9_IRQHandler(void)
  504. {
  505. rt_interrupt_enter();
  506. rt_device_hwtimer_isr(&tmr_config[TMR9_INDEX].device);
  507. TMR_ClearIntFlag(TMR9, TMR_INT_UPDATE);
  508. rt_interrupt_leave();
  509. }
  510. #endif
  511. #ifdef BSP_USING_TMR11
  512. void TMR1_TRG_COM_TMR11_IRQHandler(void)
  513. {
  514. rt_interrupt_enter();
  515. rt_device_hwtimer_isr(&tmr_config[TMR11_INDEX].device);
  516. TMR_ClearIntFlag(TMR11, TMR_INT_UPDATE);
  517. rt_interrupt_leave();
  518. }
  519. #endif
  520. #ifdef BSP_USING_TMR12
  521. void TMR8_BRK_TMR12_IRQHandler(void)
  522. {
  523. rt_interrupt_enter();
  524. rt_device_hwtimer_isr(&tmr_config[TMR12_INDEX].device);
  525. TMR_ClearIntFlag(TMR12, TMR_INT_UPDATE);
  526. rt_interrupt_leave();
  527. }
  528. #endif
  529. #ifdef BSP_USING_TMR14
  530. void TMR8_TRG_COM_TMR14_IRQHandler(void)
  531. {
  532. rt_interrupt_enter();
  533. rt_device_hwtimer_isr(&tmr_config[TMR14_INDEX].device);
  534. TMR_ClearIntFlag(TMR14, TMR_INT_UPDATE);
  535. rt_interrupt_leave();
  536. }
  537. #endif
  538. static int rt_hw_hwtimer_init(void)
  539. {
  540. int i = 0;
  541. int result = RT_EOK;
  542. for (i = 0; i < sizeof(tmr_config) / sizeof(tmr_config[0]); i++)
  543. {
  544. tmr_config[i].device.info = &_info;
  545. tmr_config[i].device.ops = &_hwtimer_ops;
  546. if (rt_device_hwtimer_register(&tmr_config[i].device, tmr_config[i].name, &tmr_config[i]) == RT_EOK)
  547. {
  548. LOG_D("%s register success", tmr_config[i].name);
  549. }
  550. else
  551. {
  552. LOG_E("%s register failed", tmr_config[i].name);
  553. result = -RT_ERROR;
  554. }
  555. }
  556. return result;
  557. }
  558. INIT_BOARD_EXPORT(rt_hw_hwtimer_init);
  559. #endif /* RT_USING_HWTIMER */