drv_tim.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. * 2018-12-10 zylx first version
  9. * 2020-06-16 thread-liu Porting for stm32mp1
  10. * 2020-08-25 linyongkang Fix the timer clock frequency doubling problem
  11. * 2020-10-14 Dozingfiretruck Porting for stm32wbxx
  12. * 2020-11-18 leizhixiong add STM32H7 series support
  13. */
  14. #include <rtdevice.h>
  15. #include "drv_config.h"
  16. //#define DRV_DEBUG
  17. #define LOG_TAG "drv.tim"
  18. #include <drv_log.h>
  19. /* APBx timer clocks frequency doubler state related to APB1CLKDivider value */
  20. void stm32_tim_pclkx_doubler_get(rt_uint32_t *pclk1_doubler, rt_uint32_t *pclk2_doubler)
  21. {
  22. rt_uint32_t flatency = 0;
  23. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  24. RT_ASSERT(pclk1_doubler != RT_NULL);
  25. RT_ASSERT(pclk1_doubler != RT_NULL);
  26. HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &flatency);
  27. *pclk1_doubler = 1;
  28. *pclk2_doubler = 1;
  29. #if defined(SOC_SERIES_STM32MP1)
  30. if (RCC_ClkInitStruct.APB1_Div != RCC_APB1_DIV1)
  31. {
  32. *pclk1_doubler = 2;
  33. }
  34. if (RCC_ClkInitStruct.APB2_Div != RCC_APB2_DIV1)
  35. {
  36. *pclk2_doubler = 2;
  37. }
  38. #else
  39. if (RCC_ClkInitStruct.APB1CLKDivider != RCC_HCLK_DIV1)
  40. {
  41. *pclk1_doubler = 2;
  42. }
  43. #if !(defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0))
  44. if (RCC_ClkInitStruct.APB2CLKDivider != RCC_HCLK_DIV1)
  45. {
  46. *pclk2_doubler = 2;
  47. }
  48. #endif /* !(defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0)) */
  49. #endif /* defined(SOC_SERIES_STM32MP1) */
  50. }
  51. void stm32_tim_enable_clock(TIM_HandleTypeDef* htim_base)
  52. {
  53. RT_ASSERT(htim_base != RT_NULL);
  54. if(RT_FALSE);
  55. #ifdef TIM1
  56. else if(htim_base->Instance==TIM1)
  57. {
  58. __HAL_RCC_TIM1_CLK_ENABLE();
  59. }
  60. #endif /* TIM1 */
  61. #ifdef TIM2
  62. else if(htim_base->Instance==TIM2)
  63. {
  64. __HAL_RCC_TIM2_CLK_ENABLE();
  65. }
  66. #endif /* TIM2 */
  67. #ifdef TIM3
  68. else if(htim_base->Instance==TIM3)
  69. {
  70. __HAL_RCC_TIM3_CLK_ENABLE();
  71. }
  72. #endif /* TIM3 */
  73. #ifdef TIM4
  74. else if(htim_base->Instance==TIM4)
  75. {
  76. __HAL_RCC_TIM4_CLK_ENABLE();
  77. }
  78. #endif /* TIM4 */
  79. #ifdef TIM5
  80. else if(htim_base->Instance==TIM5)
  81. {
  82. __HAL_RCC_TIM5_CLK_ENABLE();
  83. }
  84. #endif /* TIM5 */
  85. #ifdef TIM6
  86. else if(htim_base->Instance==TIM6)
  87. {
  88. __HAL_RCC_TIM6_CLK_ENABLE();
  89. }
  90. #endif /* TIM6 */
  91. #ifdef TIM7
  92. else if(htim_base->Instance==TIM7)
  93. {
  94. __HAL_RCC_TIM7_CLK_ENABLE();
  95. }
  96. #endif /* TIM7 */
  97. #ifdef TIM8
  98. else if(htim_base->Instance==TIM8)
  99. {
  100. __HAL_RCC_TIM8_CLK_ENABLE();
  101. }
  102. #endif /* TIM8 */
  103. #ifdef TIM9
  104. else if(htim_base->Instance==TIM9)
  105. {
  106. __HAL_RCC_TIM9_CLK_ENABLE();
  107. }
  108. #endif /* TIM9 */
  109. #ifdef TIM10
  110. else if(htim_base->Instance==TIM10)
  111. {
  112. __HAL_RCC_TIM10_CLK_ENABLE();
  113. }
  114. #endif /* TIM10 */
  115. #ifdef TIM11
  116. else if(htim_base->Instance==TIM11)
  117. {
  118. __HAL_RCC_TIM11_CLK_ENABLE();
  119. }
  120. #endif /* TIM11 */
  121. #ifdef TIM12
  122. else if(htim_base->Instance==TIM12)
  123. {
  124. __HAL_RCC_TIM12_CLK_ENABLE();
  125. }
  126. #endif /* TIM12 */
  127. #ifdef TIM13
  128. else if(htim_base->Instance==TIM13)
  129. {
  130. __HAL_RCC_TIM13_CLK_ENABLE();
  131. }
  132. #endif /* TIM13 */
  133. #ifdef TIM14
  134. else if(htim_base->Instance==TIM14)
  135. {
  136. __HAL_RCC_TIM14_CLK_ENABLE();
  137. }
  138. #endif /* TIM14 */
  139. #ifdef TIM15
  140. else if(htim_base->Instance==TIM15)
  141. {
  142. __HAL_RCC_TIM15_CLK_ENABLE();
  143. }
  144. #endif /* TIM15 */
  145. #ifdef TIM16
  146. else if(htim_base->Instance==TIM16)
  147. {
  148. __HAL_RCC_TIM16_CLK_ENABLE();
  149. }
  150. #endif /* TIM16 */
  151. #ifdef TIM17
  152. else if(htim_base->Instance==TIM17)
  153. {
  154. __HAL_RCC_TIM17_CLK_ENABLE();
  155. }
  156. #endif /* TIM17 */
  157. #ifdef TIM18
  158. else if(htim_base->Instance==TIM18)
  159. {
  160. __HAL_RCC_TIM18_CLK_ENABLE();
  161. }
  162. #endif /* TIM18 */
  163. #ifdef TIM19
  164. else if(htim_base->Instance==TIM19)
  165. {
  166. __HAL_RCC_TIM19_CLK_ENABLE();
  167. }
  168. #endif /* TIM19 */
  169. else
  170. {
  171. RT_ASSERT(RT_TRUE);
  172. }
  173. }
  174. #ifdef BSP_USING_TIM
  175. enum
  176. {
  177. #ifdef BSP_USING_TIM1
  178. TIM1_INDEX,
  179. #endif
  180. #ifdef BSP_USING_TIM2
  181. TIM2_INDEX,
  182. #endif
  183. #ifdef BSP_USING_TIM3
  184. TIM3_INDEX,
  185. #endif
  186. #ifdef BSP_USING_TIM4
  187. TIM4_INDEX,
  188. #endif
  189. #ifdef BSP_USING_TIM5
  190. TIM5_INDEX,
  191. #endif
  192. #ifdef BSP_USING_TIM6
  193. TIM6_INDEX,
  194. #endif
  195. #ifdef BSP_USING_TIM7
  196. TIM7_INDEX,
  197. #endif
  198. #ifdef BSP_USING_TIM8
  199. TIM8_INDEX,
  200. #endif
  201. #ifdef BSP_USING_TIM9
  202. TIM9_INDEX,
  203. #endif
  204. #ifdef BSP_USING_TIM10
  205. TIM10_INDEX,
  206. #endif
  207. #ifdef BSP_USING_TIM11
  208. TIM11_INDEX,
  209. #endif
  210. #ifdef BSP_USING_TIM12
  211. TIM12_INDEX,
  212. #endif
  213. #ifdef BSP_USING_TIM13
  214. TIM13_INDEX,
  215. #endif
  216. #ifdef BSP_USING_TIM14
  217. TIM14_INDEX,
  218. #endif
  219. #ifdef BSP_USING_TIM15
  220. TIM15_INDEX,
  221. #endif
  222. #ifdef BSP_USING_TIM16
  223. TIM16_INDEX,
  224. #endif
  225. #ifdef BSP_USING_TIM17
  226. TIM17_INDEX,
  227. #endif
  228. };
  229. struct stm32_hwtimer
  230. {
  231. rt_hwtimer_t time_device;
  232. TIM_HandleTypeDef tim_handle;
  233. IRQn_Type tim_irqn;
  234. char *name;
  235. };
  236. static struct stm32_hwtimer stm32_hwtimer_obj[] =
  237. {
  238. #ifdef BSP_USING_TIM1
  239. TIM1_CONFIG,
  240. #endif
  241. #ifdef BSP_USING_TIM2
  242. TIM2_CONFIG,
  243. #endif
  244. #ifdef BSP_USING_TIM3
  245. TIM3_CONFIG,
  246. #endif
  247. #ifdef BSP_USING_TIM4
  248. TIM4_CONFIG,
  249. #endif
  250. #ifdef BSP_USING_TIM5
  251. TIM5_CONFIG,
  252. #endif
  253. #ifdef BSP_USING_TIM6
  254. TIM6_CONFIG,
  255. #endif
  256. #ifdef BSP_USING_TIM7
  257. TIM7_CONFIG,
  258. #endif
  259. #ifdef BSP_USING_TIM8
  260. TIM8_CONFIG,
  261. #endif
  262. #ifdef BSP_USING_TIM9
  263. TIM9_CONFIG,
  264. #endif
  265. #ifdef BSP_USING_TIM10
  266. TIM10_CONFIG,
  267. #endif
  268. #ifdef BSP_USING_TIM11
  269. TIM11_CONFIG,
  270. #endif
  271. #ifdef BSP_USING_TIM12
  272. TIM12_CONFIG,
  273. #endif
  274. #ifdef BSP_USING_TIM13
  275. TIM13_CONFIG,
  276. #endif
  277. #ifdef BSP_USING_TIM14
  278. TIM14_CONFIG,
  279. #endif
  280. #ifdef BSP_USING_TIM15
  281. TIM15_CONFIG,
  282. #endif
  283. #ifdef BSP_USING_TIM16
  284. TIM16_CONFIG,
  285. #endif
  286. #ifdef BSP_USING_TIM17
  287. TIM17_CONFIG,
  288. #endif
  289. };
  290. static void timer_init(struct rt_hwtimer_device *timer, rt_uint32_t state)
  291. {
  292. uint32_t prescaler_value = 0;
  293. uint32_t pclk1_doubler, pclk2_doubler;
  294. TIM_HandleTypeDef *tim = RT_NULL;
  295. struct stm32_hwtimer *tim_device = RT_NULL;
  296. RT_ASSERT(timer != RT_NULL);
  297. if (state)
  298. {
  299. tim = (TIM_HandleTypeDef *)timer->parent.user_data;
  300. tim_device = (struct stm32_hwtimer *)timer;
  301. stm32_tim_pclkx_doubler_get(&pclk1_doubler, &pclk2_doubler);
  302. /* time init */
  303. /* Some series may only have APBPERIPH_BASE, don't have HAL_RCC_GetPCLK2Freq */
  304. #if defined(APBPERIPH_BASE)
  305. prescaler_value = (uint32_t)(HAL_RCC_GetPCLK1Freq() * pclk1_doubler / 10000) - 1;
  306. #elif defined(APB1PERIPH_BASE) || defined(APB2PERIPH_BASE)
  307. if ((rt_uint32_t)tim->Instance >= APB2PERIPH_BASE)
  308. {
  309. prescaler_value = (uint32_t)(HAL_RCC_GetPCLK2Freq() * pclk2_doubler / 10000) - 1;
  310. }
  311. else
  312. {
  313. prescaler_value = (uint32_t)(HAL_RCC_GetPCLK1Freq() * pclk1_doubler / 10000) - 1;
  314. }
  315. #endif
  316. tim->Init.Period = 10000 - 1;
  317. tim->Init.Prescaler = prescaler_value;
  318. tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  319. if (timer->info->cntmode == HWTIMER_CNTMODE_UP)
  320. {
  321. tim->Init.CounterMode = TIM_COUNTERMODE_UP;
  322. }
  323. else
  324. {
  325. tim->Init.CounterMode = TIM_COUNTERMODE_DOWN;
  326. }
  327. tim->Init.RepetitionCounter = 0;
  328. #if defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32MP1) || defined(SOC_SERIES_STM32WB)
  329. tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  330. #endif
  331. if (HAL_TIM_Base_Init(tim) != HAL_OK)
  332. {
  333. LOG_E("%s init failed", tim_device->name);
  334. return;
  335. }
  336. stm32_tim_enable_clock(tim);
  337. HAL_NVIC_SetPriority(tim_device->tim_irqn, 3, 0); /* set the TIMx priority */
  338. HAL_NVIC_EnableIRQ(tim_device->tim_irqn); /* enable the TIMx global Interrupt */
  339. __HAL_TIM_CLEAR_FLAG(tim, TIM_FLAG_UPDATE); /* clear update flag */
  340. __HAL_TIM_URS_ENABLE(tim); /* enable update request source */
  341. LOG_D("%s init success", tim_device->name);
  342. }
  343. }
  344. static rt_err_t timer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode)
  345. {
  346. rt_err_t result = RT_EOK;
  347. TIM_HandleTypeDef *tim = RT_NULL;
  348. RT_ASSERT(timer != RT_NULL);
  349. tim = (TIM_HandleTypeDef *)timer->parent.user_data;
  350. /* set tim cnt */
  351. __HAL_TIM_SET_COUNTER(tim, 0);
  352. /* set tim arr */
  353. __HAL_TIM_SET_AUTORELOAD(tim, t - 1);
  354. if (opmode == HWTIMER_MODE_ONESHOT)
  355. {
  356. /* set timer to single mode */
  357. tim->Instance->CR1 |= TIM_OPMODE_SINGLE;
  358. }
  359. else
  360. {
  361. tim->Instance->CR1 &= (~TIM_OPMODE_SINGLE);
  362. }
  363. /* start timer */
  364. if (HAL_TIM_Base_Start_IT(tim) != HAL_OK)
  365. {
  366. LOG_E("TIM start failed");
  367. result = -RT_ERROR;
  368. }
  369. return result;
  370. }
  371. static void timer_stop(rt_hwtimer_t *timer)
  372. {
  373. TIM_HandleTypeDef *tim = RT_NULL;
  374. RT_ASSERT(timer != RT_NULL);
  375. tim = (TIM_HandleTypeDef *)timer->parent.user_data;
  376. /* stop timer */
  377. HAL_TIM_Base_Stop_IT(tim);
  378. /* set tim cnt */
  379. __HAL_TIM_SET_COUNTER(tim, 0);
  380. }
  381. static rt_err_t timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
  382. {
  383. TIM_HandleTypeDef *tim = RT_NULL;
  384. rt_err_t result = -RT_ERROR;
  385. uint32_t pclk1_doubler, pclk2_doubler;
  386. RT_ASSERT(timer != RT_NULL);
  387. RT_ASSERT(arg != RT_NULL);
  388. tim = (TIM_HandleTypeDef *)timer->parent.user_data;
  389. switch (cmd)
  390. {
  391. case HWTIMER_CTRL_FREQ_SET:
  392. {
  393. rt_uint32_t freq;
  394. rt_uint16_t val;
  395. /* set timer frequence */
  396. freq = *((rt_uint32_t *)arg);
  397. stm32_tim_pclkx_doubler_get(&pclk1_doubler, &pclk2_doubler);
  398. #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  399. if (tim->Instance == TIM9 || tim->Instance == TIM10 || tim->Instance == TIM11)
  400. #elif defined(SOC_SERIES_STM32L4)
  401. if (tim->Instance == TIM15 || tim->Instance == TIM16 || tim->Instance == TIM17)
  402. #elif defined(SOC_SERIES_STM32WB)
  403. if (tim->Instance == TIM16 || tim->Instance == TIM17)
  404. #elif defined(SOC_SERIES_STM32MP1)
  405. if(tim->Instance == TIM14 || tim->Instance == TIM16 || tim->Instance == TIM17)
  406. #elif defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7)
  407. if (0)
  408. #else
  409. #error "This driver has not supported this series yet!"
  410. #endif
  411. {
  412. #if !defined(SOC_SERIES_STM32F0) && !defined(SOC_SERIES_STM32G0)
  413. val = HAL_RCC_GetPCLK2Freq() * pclk2_doubler / freq;
  414. #endif
  415. }
  416. else
  417. {
  418. val = HAL_RCC_GetPCLK1Freq() * pclk1_doubler / freq;
  419. }
  420. __HAL_TIM_SET_PRESCALER(tim, val - 1);
  421. /* Update frequency value */
  422. tim->Instance->EGR |= TIM_EVENTSOURCE_UPDATE;
  423. result = RT_EOK;
  424. }
  425. break;
  426. default:
  427. {
  428. result = -RT_EINVAL;
  429. }
  430. break;
  431. }
  432. return result;
  433. }
  434. static rt_uint32_t timer_counter_get(rt_hwtimer_t *timer)
  435. {
  436. TIM_HandleTypeDef *tim = RT_NULL;
  437. RT_ASSERT(timer != RT_NULL);
  438. tim = (TIM_HandleTypeDef *)timer->parent.user_data;
  439. return tim->Instance->CNT;
  440. }
  441. static const struct rt_hwtimer_info _info = TIM_DEV_INFO_CONFIG;
  442. static const struct rt_hwtimer_ops _ops =
  443. {
  444. .init = timer_init,
  445. .start = timer_start,
  446. .stop = timer_stop,
  447. .count_get = timer_counter_get,
  448. .control = timer_ctrl,
  449. };
  450. #ifdef BSP_USING_TIM2
  451. void TIM2_IRQHandler(void)
  452. {
  453. /* enter interrupt */
  454. rt_interrupt_enter();
  455. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM2_INDEX].tim_handle);
  456. /* leave interrupt */
  457. rt_interrupt_leave();
  458. }
  459. #endif
  460. #ifdef BSP_USING_TIM3
  461. void TIM3_IRQHandler(void)
  462. {
  463. /* enter interrupt */
  464. rt_interrupt_enter();
  465. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM3_INDEX].tim_handle);
  466. /* leave interrupt */
  467. rt_interrupt_leave();
  468. }
  469. #endif
  470. #ifdef BSP_USING_TIM4
  471. void TIM4_IRQHandler(void)
  472. {
  473. /* enter interrupt */
  474. rt_interrupt_enter();
  475. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM4_INDEX].tim_handle);
  476. /* leave interrupt */
  477. rt_interrupt_leave();
  478. }
  479. #endif
  480. #ifdef BSP_USING_TIM5
  481. void TIM5_IRQHandler(void)
  482. {
  483. /* enter interrupt */
  484. rt_interrupt_enter();
  485. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM5_INDEX].tim_handle);
  486. /* leave interrupt */
  487. rt_interrupt_leave();
  488. }
  489. #endif
  490. #ifdef BSP_USING_TIM7
  491. void TIM7_IRQHandler(void)
  492. {
  493. /* enter interrupt */
  494. rt_interrupt_enter();
  495. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM7_INDEX].tim_handle);
  496. /* leave interrupt */
  497. rt_interrupt_leave();
  498. }
  499. #endif
  500. #ifdef BSP_USING_TIM11
  501. void TIM1_TRG_COM_TIM11_IRQHandler(void)
  502. {
  503. /* enter interrupt */
  504. rt_interrupt_enter();
  505. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM11_INDEX].tim_handle);
  506. /* leave interrupt */
  507. rt_interrupt_leave();
  508. }
  509. #endif
  510. #ifdef BSP_USING_TIM13
  511. void TIM8_UP_TIM13_IRQHandler(void)
  512. {
  513. /* enter interrupt */
  514. rt_interrupt_enter();
  515. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM13_INDEX].tim_handle);
  516. /* leave interrupt */
  517. rt_interrupt_leave();
  518. }
  519. #endif
  520. #ifdef BSP_USING_TIM14
  521. #if defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7)
  522. void TIM8_TRG_COM_TIM14_IRQHandler(void)
  523. #elif defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32MP1)
  524. void TIM14_IRQHandler(void)
  525. #endif
  526. {
  527. /* enter interrupt */
  528. rt_interrupt_enter();
  529. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM14_INDEX].tim_handle);
  530. /* leave interrupt */
  531. rt_interrupt_leave();
  532. }
  533. #endif
  534. #ifdef BSP_USING_TIM15
  535. void TIM1_BRK_TIM15_IRQHandler(void)
  536. {
  537. /* enter interrupt */
  538. rt_interrupt_enter();
  539. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM15_INDEX].tim_handle);
  540. /* leave interrupt */
  541. rt_interrupt_leave();
  542. }
  543. #endif
  544. #ifdef BSP_USING_TIM16
  545. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WB)
  546. void TIM1_UP_TIM16_IRQHandler(void)
  547. #elif defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32MP1)
  548. void TIM16_IRQHandler(void)
  549. #endif
  550. {
  551. /* enter interrupt */
  552. rt_interrupt_enter();
  553. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM16_INDEX].tim_handle);
  554. /* leave interrupt */
  555. rt_interrupt_leave();
  556. }
  557. #endif
  558. #ifdef BSP_USING_TIM17
  559. #if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32WB)
  560. void TIM1_TRG_COM_TIM17_IRQHandler(void)
  561. #elif defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32MP1)
  562. void TIM17_IRQHandler(void)
  563. #endif
  564. {
  565. /* enter interrupt */
  566. rt_interrupt_enter();
  567. HAL_TIM_IRQHandler(&stm32_hwtimer_obj[TIM17_INDEX].tim_handle);
  568. /* leave interrupt */
  569. rt_interrupt_leave();
  570. }
  571. #endif
  572. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  573. {
  574. #ifdef BSP_USING_TIM2
  575. if (htim->Instance == TIM2)
  576. {
  577. rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM2_INDEX].time_device);
  578. }
  579. #endif
  580. #ifdef BSP_USING_TIM3
  581. if (htim->Instance == TIM3)
  582. {
  583. rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM3_INDEX].time_device);
  584. }
  585. #endif
  586. #ifdef BSP_USING_TIM4
  587. if (htim->Instance == TIM4)
  588. {
  589. rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM4_INDEX].time_device);
  590. }
  591. #endif
  592. #ifdef BSP_USING_TIM5
  593. if (htim->Instance == TIM5)
  594. {
  595. rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM5_INDEX].time_device);
  596. }
  597. #endif
  598. #ifdef BSP_USING_TIM7
  599. if (htim->Instance == TIM7)
  600. {
  601. rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM7_INDEX].time_device);
  602. }
  603. #endif
  604. #ifdef BSP_USING_TIM11
  605. if (htim->Instance == TIM11)
  606. {
  607. rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM11_INDEX].time_device);
  608. }
  609. #endif
  610. #ifdef BSP_USING_TIM13
  611. if (htim->Instance == TIM13)
  612. {
  613. rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM13_INDEX].time_device);
  614. }
  615. #endif
  616. #ifdef BSP_USING_TIM14
  617. if (htim->Instance == TIM14)
  618. {
  619. rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM14_INDEX].time_device);
  620. }
  621. #endif
  622. #ifdef BSP_USING_TIM15
  623. if (htim->Instance == TIM15)
  624. {
  625. rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM15_INDEX].time_device);
  626. }
  627. #endif
  628. #ifdef BSP_USING_TIM16
  629. if (htim->Instance == TIM16)
  630. {
  631. rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM16_INDEX].time_device);
  632. }
  633. #endif
  634. #ifdef BSP_USING_TIM17
  635. if (htim->Instance == TIM17)
  636. {
  637. rt_device_hwtimer_isr(&stm32_hwtimer_obj[TIM17_INDEX].time_device);
  638. }
  639. #endif
  640. }
  641. static int stm32_hwtimer_init(void)
  642. {
  643. int i = 0;
  644. int result = RT_EOK;
  645. for (i = 0; i < sizeof(stm32_hwtimer_obj) / sizeof(stm32_hwtimer_obj[0]); i++)
  646. {
  647. stm32_hwtimer_obj[i].time_device.info = &_info;
  648. stm32_hwtimer_obj[i].time_device.ops = &_ops;
  649. if (rt_device_hwtimer_register(&stm32_hwtimer_obj[i].time_device,
  650. stm32_hwtimer_obj[i].name, &stm32_hwtimer_obj[i].tim_handle) == RT_EOK)
  651. {
  652. LOG_D("%s register success", stm32_hwtimer_obj[i].name);
  653. }
  654. else
  655. {
  656. LOG_E("%s register failed", stm32_hwtimer_obj[i].name);
  657. result = -RT_ERROR;
  658. }
  659. }
  660. return result;
  661. }
  662. INIT_BOARD_EXPORT(stm32_hwtimer_init);
  663. #endif /* BSP_USING_TIM */