drv_hwtimer.c 18 KB

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