drv_hwtimer.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. * Copyright (c) 2006-2023, 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. * 2022-12-26 luobeihai add apm32F0 serie MCU support
  11. */
  12. #include <board.h>
  13. #define DBG_TAG "drv.hwtimer"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #ifdef RT_USING_HWTIMER
  17. static const struct rt_hwtimer_info apm32_timer_info =
  18. {
  19. .maxfreq = 1000000,
  20. .minfreq = 2000,
  21. .maxcnt = 0xFFFF,
  22. .cntmode = HWTIMER_CNTMODE_UP,
  23. };
  24. /* apm32 config class */
  25. struct apm32_timer
  26. {
  27. char *name;
  28. TMR_T *tmr;
  29. IRQn_Type irqn;
  30. rt_hwtimer_t device;
  31. };
  32. enum
  33. {
  34. #ifdef BSP_USING_TMR1
  35. TMR1_INDEX,
  36. #endif
  37. #ifdef BSP_USING_TMR2
  38. TMR2_INDEX,
  39. #endif
  40. #ifdef BSP_USING_TMR3
  41. TMR3_INDEX,
  42. #endif
  43. #ifdef BSP_USING_TMR4
  44. TMR4_INDEX,
  45. #endif
  46. #ifdef BSP_USING_TMR5
  47. TMR5_INDEX,
  48. #endif
  49. #ifdef BSP_USING_TMR6
  50. TMR6_INDEX,
  51. #endif
  52. #ifdef BSP_USING_TMR7
  53. TMR7_INDEX,
  54. #endif
  55. #ifdef BSP_USING_TMR8
  56. TMR8_INDEX,
  57. #endif
  58. #ifdef BSP_USING_TMR9
  59. TMR9_INDEX,
  60. #endif
  61. #ifdef BSP_USING_TMR10
  62. TMR10_INDEX,
  63. #endif
  64. #ifdef BSP_USING_TMR11
  65. TMR11_INDEX,
  66. #endif
  67. #ifdef BSP_USING_TMR12
  68. TMR12_INDEX,
  69. #endif
  70. #ifdef BSP_USING_TMR13
  71. TMR13_INDEX,
  72. #endif
  73. #ifdef BSP_USING_TMR14
  74. TMR14_INDEX,
  75. #endif
  76. #ifdef BSP_USING_TMR15
  77. TMR15_INDEX,
  78. #endif
  79. #ifdef BSP_USING_TMR16
  80. TMR16_INDEX,
  81. #endif
  82. #ifdef BSP_USING_TMR17
  83. TMR17_INDEX,
  84. #endif
  85. };
  86. static struct apm32_timer tmr_config[] =
  87. {
  88. #ifdef BSP_USING_TMR1
  89. {
  90. "timer1",
  91. TMR1,
  92. #if defined(SOC_SERIES_APM32F1)
  93. TMR1_UP_IRQn,
  94. #elif defined(SOC_SERIES_APM32F4)
  95. TMR1_UP_TMR10_IRQn,
  96. #elif defined(SOC_SERIES_APM32F0)
  97. TMR1_BRK_UP_TRG_COM_IRQn
  98. #endif
  99. },
  100. #endif
  101. #ifdef BSP_USING_TMR2
  102. {
  103. "timer2",
  104. TMR2,
  105. TMR2_IRQn,
  106. },
  107. #endif
  108. #ifdef BSP_USING_TMR3
  109. {
  110. "timer3",
  111. TMR3,
  112. TMR3_IRQn,
  113. },
  114. #endif
  115. #ifdef BSP_USING_TMR4
  116. {
  117. "timer4",
  118. TMR4,
  119. TMR4_IRQn,
  120. },
  121. #endif
  122. #ifdef BSP_USING_TMR5
  123. {
  124. "timer5",
  125. TMR5,
  126. TMR5_IRQn,
  127. },
  128. #endif
  129. #ifdef BSP_USING_TMR6
  130. {
  131. "timer6",
  132. TMR6,
  133. #if defined(SOC_SERIES_APM32F1) || defined(APM32F030) || defined(APM32F070)
  134. TMR6_IRQn,
  135. #elif defined(SOC_SERIES_APM32F4)
  136. TMR6_DAC_IRQn
  137. #elif defined(SOC_SERIES_APM32F0) && !defined(APM32F030) && !defined(APM32F070)
  138. TMR6_DAC_IRQn
  139. #endif
  140. },
  141. #endif
  142. #ifdef BSP_USING_TMR7
  143. {
  144. "timer7",
  145. TMR7,
  146. TMR7_IRQn,
  147. },
  148. #endif
  149. #ifdef BSP_USING_TMR8
  150. {
  151. "timer8",
  152. TMR8,
  153. #if defined(SOC_SERIES_APM32F1)
  154. TMR8_UP_IRQn,
  155. #elif defined(SOC_SERIES_APM32F4)
  156. TMR8_UP_TMR13_IRQn,
  157. #endif
  158. },
  159. #endif
  160. #ifdef BSP_USING_TMR9
  161. {
  162. "timer9",
  163. TMR9,
  164. TMR1_BRK_TMR9_IRQn,
  165. },
  166. #endif
  167. #ifdef BSP_USING_TMR10
  168. {
  169. "timer10",
  170. TMR10,
  171. TMR1_UP_TMR10_IRQn,
  172. },
  173. #endif
  174. #ifdef BSP_USING_TMR11
  175. {
  176. "timer11",
  177. TMR11,
  178. TMR1_TRG_COM_TMR11_IRQn,
  179. },
  180. #endif
  181. #ifdef BSP_USING_TMR12
  182. {
  183. "timer12",
  184. TMR12,
  185. TMR8_BRK_TMR12_IRQn,
  186. },
  187. #endif
  188. #ifdef BSP_USING_TMR13
  189. {
  190. "timer13",
  191. TMR13,
  192. TMR8_UP_TMR13_IRQn,
  193. },
  194. #endif
  195. #ifdef BSP_USING_TMR14
  196. {
  197. "timer14",
  198. TMR14,
  199. #if defined(SOC_SERIES_APM32F0)
  200. TMR14_IRQn,
  201. #elif defined(SOC_SERIES_APM32F4)
  202. TMR8_TRG_COM_TMR14_IRQn,
  203. #endif
  204. },
  205. #endif
  206. #ifdef BSP_USING_TMR15
  207. {
  208. "timer15",
  209. TMR15,
  210. TMR15_IRQn,
  211. },
  212. #endif
  213. #ifdef BSP_USING_TMR16
  214. {
  215. "timer16",
  216. TMR16,
  217. TMR16_IRQn,
  218. },
  219. #endif
  220. #ifdef BSP_USING_TMR17
  221. {
  222. "timer17",
  223. TMR17,
  224. TMR17_IRQn,
  225. },
  226. #endif
  227. };
  228. static rt_uint32_t apm32_hwtimer_clock_get(TMR_T *tmr)
  229. {
  230. #if defined(SOC_SERIES_APM32F0)
  231. uint32_t pclk1;
  232. pclk1 = RCM_ReadPCLKFreq();
  233. return (rt_uint32_t)(pclk1 * ((RCM->CFG1_B.APB1PSC != 0) ? 2 : 1));
  234. #else
  235. uint32_t pclk1, pclk2;
  236. RCM_ReadPCLKFreq(&pclk1, &pclk2);
  237. if (tmr == TMR1 || tmr == TMR8 || tmr == TMR9 || tmr == TMR10 || tmr == TMR11)
  238. {
  239. return (rt_uint32_t)(pclk2 * ((RCM->CFG_B.APB2PSC != 0) ? 2 : 1));
  240. }
  241. else
  242. {
  243. return (rt_uint32_t)(pclk1 * ((RCM->CFG_B.APB1PSC != 0) ? 2 : 1));
  244. }
  245. #endif
  246. }
  247. static void apm32_hwtimer_enable_clock(void)
  248. {
  249. #ifdef BSP_USING_TMR1
  250. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR1);
  251. #endif
  252. #ifdef BSP_USING_TMR2
  253. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR2);
  254. #endif
  255. #ifdef BSP_USING_TMR3
  256. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR3);
  257. #endif
  258. #ifdef BSP_USING_TMR4
  259. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR4);
  260. #endif
  261. #ifdef BSP_USING_TMR5
  262. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR5);
  263. #endif
  264. #ifdef BSP_USING_TMR6
  265. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR6);
  266. #endif
  267. #ifdef BSP_USING_TMR7
  268. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR7);
  269. #endif
  270. #ifdef BSP_USING_TMR8
  271. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR8);
  272. #endif
  273. #ifdef BSP_USING_TMR9
  274. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR9);
  275. #endif
  276. #ifdef BSP_USING_TMR10
  277. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR10);
  278. #endif
  279. #ifdef BSP_USING_TMR11
  280. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR11);
  281. #endif
  282. #ifdef BSP_USING_TMR12
  283. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR12);
  284. #endif
  285. #ifdef BSP_USING_TMR13
  286. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR13);
  287. #endif
  288. #ifdef BSP_USING_TMR14
  289. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR14);
  290. #endif
  291. #ifdef BSP_USING_TMR15
  292. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR15);
  293. #endif
  294. #ifdef BSP_USING_TMR16
  295. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR16);
  296. #endif
  297. #ifdef BSP_USING_TMR17
  298. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR17);
  299. #endif
  300. }
  301. static void apm32_hwtimer_init(struct rt_hwtimer_device *timer, rt_uint32_t state)
  302. {
  303. #if defined(SOC_SERIES_APM32F0)
  304. TMR_TimeBase_T base_config;
  305. #else
  306. TMR_BaseConfig_T base_config;
  307. #endif
  308. uint32_t prescaler = 0;
  309. struct apm32_timer *timer_config;
  310. RT_ASSERT(timer != RT_NULL);
  311. if (state)
  312. {
  313. timer_config = (struct apm32_timer *)timer->parent.user_data;
  314. apm32_hwtimer_enable_clock();
  315. prescaler = (uint32_t)(apm32_hwtimer_clock_get(timer_config->tmr) / 10000) - 1;
  316. base_config.period = 10000 - 1;
  317. #if defined(SOC_SERIES_APM32F0)
  318. base_config.div = prescaler;
  319. base_config.clockDivision = TMR_CKD_DIV1;
  320. if (timer->info->cntmode == HWTIMER_CNTMODE_UP)
  321. {
  322. base_config.counterMode = TMR_COUNTER_MODE_UP;
  323. }
  324. else
  325. {
  326. base_config.counterMode = TMR_COUNTER_MODE_DOWN;
  327. }
  328. #else
  329. base_config.division = prescaler;
  330. base_config.clockDivision = TMR_CLOCK_DIV_1;
  331. if (timer->info->cntmode == HWTIMER_CNTMODE_UP)
  332. {
  333. base_config.countMode = TMR_COUNTER_MODE_UP;
  334. }
  335. else
  336. {
  337. base_config.countMode = TMR_COUNTER_MODE_DOWN;
  338. }
  339. #endif
  340. base_config.repetitionCounter = 0;
  341. TMR_ConfigTimeBase(timer_config->tmr, &base_config);
  342. #if defined(SOC_SERIES_APM32F0)
  343. /* set the TIMx priority */
  344. NVIC_EnableIRQRequest(timer_config->irqn, 3);
  345. /* enable update request source */
  346. TMR_ConfigUPdateRequest(timer_config->tmr, TMR_UPDATE_SOURCE_REGULAR);
  347. #else
  348. /* set the TIMx priority */
  349. NVIC_EnableIRQRequest(timer_config->irqn, 3, 0);
  350. /* enable update request source */
  351. TMR_ConfigUpdateRequest(timer_config->tmr, TMR_UPDATE_SOURCE_REGULAR);
  352. #endif
  353. /* clear update flag */
  354. TMR_ClearStatusFlag(timer_config->tmr, TMR_FLAG_UPDATE);
  355. LOG_D("%s init success", timer_config->name);
  356. }
  357. }
  358. static rt_err_t apm32_hwtimer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode)
  359. {
  360. rt_err_t result = RT_EOK;
  361. struct apm32_timer *timer_config = RT_NULL;
  362. RT_ASSERT(timer != RT_NULL);
  363. timer_config = (struct apm32_timer *)timer->parent.user_data;
  364. /* set timer_config counter */
  365. timer_config->tmr->CNT = 0;
  366. /* set timer_config autoReload */
  367. timer_config->tmr->AUTORLD = t - 1;
  368. if (opmode == HWTIMER_MODE_ONESHOT)
  369. {
  370. /* set timer to single mode */
  371. timer_config->tmr->CTRL1_B.SPMEN = 1;
  372. }
  373. else
  374. {
  375. timer_config->tmr->CTRL1_B.SPMEN = 0;
  376. }
  377. TMR_EnableInterrupt(timer_config->tmr, TMR_INT_UPDATE);
  378. #if defined(SOC_SERIES_APM32F0)
  379. if (timer_config->tmr == TMR1 || timer_config->tmr == TMR2 || timer_config->tmr == TMR3 || \
  380. timer_config->tmr == TMR15)
  381. #else
  382. if (timer_config->tmr == TMR1 || timer_config->tmr == TMR2 || timer_config->tmr == TMR3 || \
  383. timer_config->tmr == TMR4 || timer_config->tmr == TMR5 || timer_config->tmr == TMR8 || \
  384. timer_config->tmr == TMR9 || timer_config->tmr == TMR12)
  385. #endif
  386. {
  387. if (timer_config->tmr->SMCTRL_B.SMFSEL != 0x06)
  388. {
  389. TMR_Enable(timer_config->tmr);
  390. result = RT_EOK;
  391. }
  392. }
  393. else
  394. {
  395. TMR_Enable(timer_config->tmr);
  396. result = RT_EOK;
  397. }
  398. return result;
  399. }
  400. static void apm32_hwtimer_stop(rt_hwtimer_t *timer)
  401. {
  402. struct apm32_timer *timer_config = RT_NULL;
  403. RT_ASSERT(timer != RT_NULL);
  404. timer_config = (struct apm32_timer *)timer->parent.user_data;
  405. TMR_DisableInterrupt(timer_config->tmr, TMR_INT_UPDATE);
  406. TMR_Enable(timer_config->tmr);
  407. timer_config->tmr->CNT = 0;
  408. }
  409. static rt_err_t apm32_hwtimer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
  410. {
  411. struct apm32_timer *timer_config = RT_NULL;
  412. rt_err_t result = RT_EOK;
  413. rt_uint32_t freq;
  414. rt_uint16_t val;
  415. RT_ASSERT(timer != RT_NULL);
  416. RT_ASSERT(arg != RT_NULL);
  417. timer_config = (struct apm32_timer *)timer->parent.user_data;
  418. switch (cmd)
  419. {
  420. case HWTIMER_CTRL_FREQ_SET:
  421. /* set timer frequence */
  422. freq = *((rt_uint32_t *)arg);
  423. val = apm32_hwtimer_clock_get(timer_config->tmr) / freq;
  424. /* Configures the timer prescaler */
  425. timer_config->tmr->PSC_B.PSC = val - 1;
  426. timer_config->tmr->CEG_B.UEG = 1;
  427. break;
  428. default:
  429. LOG_E("invalid cmd: 0x%x\n", cmd);
  430. result = -RT_ENOSYS;
  431. break;
  432. }
  433. return result;
  434. }
  435. static rt_uint32_t apm32_hwtimer_counter_get(rt_hwtimer_t *timer)
  436. {
  437. struct apm32_timer *timer_config = RT_NULL;
  438. RT_ASSERT(timer != RT_NULL);
  439. timer_config = (struct apm32_timer *)timer->parent.user_data;
  440. return timer_config->tmr->CNT;
  441. }
  442. static const struct rt_hwtimer_ops apm32_hwtimer_ops =
  443. {
  444. .init = apm32_hwtimer_init,
  445. .start = apm32_hwtimer_start,
  446. .stop = apm32_hwtimer_stop,
  447. .count_get = apm32_hwtimer_counter_get,
  448. .control = apm32_hwtimer_ctrl,
  449. };
  450. #if defined(SOC_SERIES_APM32F0)
  451. #ifdef BSP_USING_TMR1
  452. void TMR1_BRK_UP_TRG_COM_IRQHandler(void)
  453. {
  454. rt_interrupt_enter();
  455. rt_device_hwtimer_isr(&tmr_config[TMR1_INDEX].device);
  456. TMR_ClearIntFlag(TMR1, TMR_INT_UPDATE);
  457. rt_interrupt_leave();
  458. }
  459. #endif
  460. #elif defined(SOC_SERIES_APM32F1)
  461. #ifdef BSP_USING_TMR1
  462. void TMR1_UP_IRQHandler(void)
  463. {
  464. rt_interrupt_enter();
  465. rt_device_hwtimer_isr(&tmr_config[TMR1_INDEX].device);
  466. TMR_ClearIntFlag(TMR1, TMR_INT_UPDATE);
  467. rt_interrupt_leave();
  468. }
  469. #endif
  470. #elif defined(SOC_SERIES_APM32F4)
  471. #if (defined(BSP_USING_TMR1) || defined(BSP_USING_TMR10))
  472. void TMR1_UP_TMR10_IRQHandler(void)
  473. {
  474. rt_interrupt_enter();
  475. if (TMR_ReadIntFlag(TMR1, TMR_INT_UPDATE))
  476. {
  477. rt_device_hwtimer_isr(&tmr_config[TMR1_INDEX].device);
  478. TMR_ClearIntFlag(TMR1, TMR_INT_UPDATE);
  479. }
  480. if (TMR_ReadIntFlag(TMR10, TMR_INT_UPDATE))
  481. {
  482. rt_device_hwtimer_isr(&tmr_config[TMR10_INDEX].device);
  483. TMR_ClearIntFlag(TMR10, TMR_INT_UPDATE);
  484. }
  485. rt_interrupt_leave();
  486. }
  487. #endif
  488. #endif
  489. #ifdef BSP_USING_TMR2
  490. void TMR2_IRQHandler(void)
  491. {
  492. rt_interrupt_enter();
  493. rt_device_hwtimer_isr(&tmr_config[TMR2_INDEX].device);
  494. TMR_ClearIntFlag(TMR2, TMR_INT_UPDATE);
  495. rt_interrupt_leave();
  496. }
  497. #endif
  498. #ifdef BSP_USING_TMR3
  499. void TMR3_IRQHandler(void)
  500. {
  501. rt_interrupt_enter();
  502. rt_device_hwtimer_isr(&tmr_config[TMR3_INDEX].device);
  503. TMR_ClearIntFlag(TMR3, TMR_INT_UPDATE);
  504. rt_interrupt_leave();
  505. }
  506. #endif
  507. #ifdef BSP_USING_TMR4
  508. void TMR4_IRQHandler(void)
  509. {
  510. rt_interrupt_enter();
  511. rt_device_hwtimer_isr(&tmr_config[TMR4_INDEX].device);
  512. TMR_ClearIntFlag(TMR4, TMR_INT_UPDATE);
  513. rt_interrupt_leave();
  514. }
  515. #endif
  516. #ifdef BSP_USING_TMR5
  517. void TMR5_IRQHandler(void)
  518. {
  519. rt_interrupt_enter();
  520. rt_device_hwtimer_isr(&tmr_config[TMR5_INDEX].device);
  521. TMR_ClearIntFlag(TMR5, TMR_INT_UPDATE);
  522. rt_interrupt_leave();
  523. }
  524. #endif
  525. #ifdef BSP_USING_TMR6
  526. #if defined(SOC_SERIES_APM32F1) || defined(APM32F030) || defined(APM32F070)
  527. void TMR6_IRQHandler(void)
  528. #elif defined(SOC_SERIES_APM32F4)
  529. void TMR6_DAC_IRQHandler(void)
  530. #elif defined(SOC_SERIES_APM32F0) && !defined(APM32F030) && !defined(APM32F070)
  531. void TMR6_DAC_IRQHandler(void)
  532. #endif
  533. {
  534. rt_interrupt_enter();
  535. rt_device_hwtimer_isr(&tmr_config[TMR6_INDEX].device);
  536. TMR_ClearIntFlag(TMR6, TMR_INT_UPDATE);
  537. rt_interrupt_leave();
  538. }
  539. #endif
  540. #ifdef BSP_USING_TMR7
  541. void TMR7_IRQHandler(void)
  542. {
  543. rt_interrupt_enter();
  544. rt_device_hwtimer_isr(&tmr_config[TMR7_INDEX].device);
  545. TMR_ClearIntFlag(TMR7, TMR_INT_UPDATE);
  546. rt_interrupt_leave();
  547. }
  548. #endif
  549. #if defined(SOC_SERIES_APM32F1)
  550. #ifdef BSP_USING_TMR8
  551. void TMR8_UP_IRQHandler(void)
  552. {
  553. rt_interrupt_enter();
  554. rt_device_hwtimer_isr(&tmr_config[TMR8_INDEX].device);
  555. TMR_ClearIntFlag(TMR8, TMR_INT_UPDATE);
  556. rt_interrupt_leave();
  557. }
  558. #endif
  559. #elif defined(SOC_SERIES_APM32F4)
  560. #if (defined(BSP_USING_TMR8) || defined(BSP_USING_TMR13))
  561. void TMR8_UP_TMR13_IRQHandler(void)
  562. {
  563. rt_interrupt_enter();
  564. if (TMR_ReadIntFlag(TMR8, TMR_INT_UPDATE))
  565. {
  566. rt_device_hwtimer_isr(&tmr_config[TMR8_INDEX].device);
  567. TMR_ClearIntFlag(TMR8, TMR_INT_UPDATE);
  568. }
  569. if (TMR_ReadIntFlag(TMR13, TMR_INT_UPDATE))
  570. {
  571. rt_device_hwtimer_isr(&tmr_config[TMR13_INDEX].device);
  572. TMR_ClearIntFlag(TMR13, TMR_INT_UPDATE);
  573. }
  574. rt_interrupt_leave();
  575. }
  576. #endif
  577. #endif
  578. #ifdef BSP_USING_TMR9
  579. void TMR1_BRK_TMR9_IRQHandler(void)
  580. {
  581. rt_interrupt_enter();
  582. rt_device_hwtimer_isr(&tmr_config[TMR9_INDEX].device);
  583. TMR_ClearIntFlag(TMR9, TMR_INT_UPDATE);
  584. rt_interrupt_leave();
  585. }
  586. #endif
  587. #ifdef BSP_USING_TMR11
  588. void TMR1_TRG_COM_TMR11_IRQHandler(void)
  589. {
  590. rt_interrupt_enter();
  591. rt_device_hwtimer_isr(&tmr_config[TMR11_INDEX].device);
  592. TMR_ClearIntFlag(TMR11, TMR_INT_UPDATE);
  593. rt_interrupt_leave();
  594. }
  595. #endif
  596. #ifdef BSP_USING_TMR12
  597. void TMR8_BRK_TMR12_IRQHandler(void)
  598. {
  599. rt_interrupt_enter();
  600. rt_device_hwtimer_isr(&tmr_config[TMR12_INDEX].device);
  601. TMR_ClearIntFlag(TMR12, TMR_INT_UPDATE);
  602. rt_interrupt_leave();
  603. }
  604. #endif
  605. #ifdef BSP_USING_TMR14
  606. #if defined(SOC_SERIES_APM32F0)
  607. void TMR14_IRQHandler(void)
  608. #elif defined(SOC_SERIES_APM32F4)
  609. void TMR8_TRG_COM_TMR14_IRQHandler(void)
  610. #endif
  611. {
  612. rt_interrupt_enter();
  613. rt_device_hwtimer_isr(&tmr_config[TMR14_INDEX].device);
  614. TMR_ClearIntFlag(TMR14, TMR_INT_UPDATE);
  615. rt_interrupt_leave();
  616. }
  617. #endif
  618. #ifdef BSP_USING_TMR15
  619. void TMR15_IRQHandler(void)
  620. {
  621. rt_interrupt_enter();
  622. rt_device_hwtimer_isr(&tmr_config[TMR15_INDEX].device);
  623. TMR_ClearIntFlag(TMR15, TMR_INT_UPDATE);
  624. rt_interrupt_leave();
  625. }
  626. #endif
  627. #ifdef BSP_USING_TMR16
  628. void TMR16_IRQHandler(void)
  629. {
  630. rt_interrupt_enter();
  631. rt_device_hwtimer_isr(&tmr_config[TMR16_INDEX].device);
  632. TMR_ClearIntFlag(TMR16, TMR_INT_UPDATE);
  633. rt_interrupt_leave();
  634. }
  635. #endif
  636. #ifdef BSP_USING_TMR17
  637. void TMR17_IRQHandler(void)
  638. {
  639. rt_interrupt_enter();
  640. rt_device_hwtimer_isr(&tmr_config[TMR17_INDEX].device);
  641. TMR_ClearIntFlag(TMR17, TMR_INT_UPDATE);
  642. rt_interrupt_leave();
  643. }
  644. #endif
  645. static int rt_hw_hwtimer_init(void)
  646. {
  647. int i = 0;
  648. int result = RT_EOK;
  649. for (i = 0; i < sizeof(tmr_config) / sizeof(tmr_config[0]); i++)
  650. {
  651. tmr_config[i].device.info = &apm32_timer_info;
  652. tmr_config[i].device.ops = &apm32_hwtimer_ops;
  653. if (rt_device_hwtimer_register(&tmr_config[i].device, tmr_config[i].name, &tmr_config[i]) == RT_EOK)
  654. {
  655. LOG_D("%s register success", tmr_config[i].name);
  656. }
  657. else
  658. {
  659. LOG_E("%s register failed", tmr_config[i].name);
  660. result = -RT_ERROR;
  661. }
  662. }
  663. return result;
  664. }
  665. INIT_BOARD_EXPORT(rt_hw_hwtimer_init);
  666. #endif /* RT_USING_HWTIMER */