drv_tim.c 17 KB

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