drv_tim.c 22 KB

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