drv_hwtimer_ch32f20x.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-01-21 charlown first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <board.h>
  13. #ifdef BSP_USING_HWTIMER
  14. #define LOG_TAG "drv.hwtimer"
  15. #include <drv_log.h>
  16. struct hwtimer_device
  17. {
  18. struct rt_hwtimer_device parent;
  19. TIM_TypeDef *periph;
  20. IRQn_Type irqn;
  21. char *name;
  22. };
  23. #ifdef BSP_USING_TIM1_HWTIMER
  24. struct hwtimer_device hwtimer_device1 =
  25. {
  26. .periph = TIM1,
  27. .irqn = TIM1_UP_IRQn,
  28. .name = "timer1"};
  29. #endif
  30. #ifdef BSP_USING_TIM2_HWTIMER
  31. struct hwtimer_device hwtimer_device2 =
  32. {
  33. .periph = TIM2,
  34. .irqn = TIM2_IRQn,
  35. .name = "timer2"};
  36. #endif
  37. #ifdef BSP_USING_TIM3_HWTIMER
  38. struct hwtimer_device hwtimer_device3 =
  39. {
  40. .periph = TIM3,
  41. .irqn = TIM3_IRQn,
  42. .name = "timer3"};
  43. #endif
  44. #ifdef BSP_USING_TIM4_HWTIMER
  45. struct hwtimer_device hwtimer_device4 =
  46. {
  47. .periph = TIM4,
  48. .irqn = TIM4_IRQn,
  49. .name = "timer4"};
  50. #endif
  51. #ifdef BSP_USING_TIM5_HWTIMER
  52. struct hwtimer_device hwtimer_device5 =
  53. {
  54. .periph = TIM5,
  55. .irqn = TIM5_IRQn,
  56. .name = "timer5"};
  57. #endif
  58. #ifdef BSP_USING_TIM6_HWTIMER
  59. struct hwtimer_device hwtimer_device6 =
  60. {
  61. .periph = TIM6,
  62. .irqn = TIM6_IRQn,
  63. .name = "timer6"};
  64. #endif
  65. #ifdef BSP_USING_TIM7_HWTIMER
  66. struct hwtimer_device hwtimer_device7 =
  67. {
  68. .periph = TIM7,
  69. .irqn = TIM7_IRQn,
  70. .name = "timer7"};
  71. #endif
  72. #ifdef BSP_USING_TIM8_HWTIMER
  73. struct hwtimer_device hwtimer_device8 =
  74. {
  75. .periph = TIM8,
  76. .irqn = TIM8_UP_IRQn,
  77. .name = "timer8"};
  78. #endif
  79. #ifdef BSP_USING_TIM9_HWTIMER
  80. struct hwtimer_device hwtimer_device9 =
  81. {
  82. .periph = TIM9,
  83. .irqn = TIM9_UP_IRQn,
  84. .name = "timer9"};
  85. #endif
  86. #ifdef BSP_USING_TIM10_HWTIMER
  87. struct hwtimer_device hwtimer_device10 =
  88. {
  89. .periph = TIM10,
  90. .irqn = TIM10_UP_IRQn,
  91. .name = "timer10"};
  92. #endif
  93. static void ch32f2_hwtimer_init(struct rt_hwtimer_device *device, rt_uint32_t state)
  94. {
  95. struct hwtimer_device *hwtimer_dev;
  96. struct rt_hwtimer_info *hwtimer_info;
  97. rt_uint32_t clk = 0;
  98. rt_uint16_t prescaler_value = 0;
  99. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitType;
  100. NVIC_InitTypeDef NVIC_InitStructure;
  101. RT_ASSERT(device != RT_NULL);
  102. hwtimer_dev = (struct hwtimer_device *)device;
  103. if (state)
  104. {
  105. ch32f2_tim_clock_init(hwtimer_dev->periph);
  106. hwtimer_info = ch32f2_hwtimer_info_config_get(hwtimer_dev->periph);
  107. clk = ch32f2_tim_clock_get(hwtimer_dev->periph);
  108. prescaler_value = (rt_uint16_t)(clk / hwtimer_info->minfreq) - 1;
  109. /*
  110. * (1 / freq) = (cnt + 1) * (1 / (clk/(prescaler_value + 1) ) )
  111. */
  112. TIM_TimeBaseInitType.TIM_Period = hwtimer_info->maxcnt - 1;
  113. TIM_TimeBaseInitType.TIM_Prescaler = prescaler_value;
  114. TIM_TimeBaseInitType.TIM_ClockDivision = TIM_CKD_DIV1;
  115. TIM_TimeBaseInitType.TIM_RepetitionCounter = 0;
  116. if (hwtimer_info == RT_NULL)
  117. {
  118. TIM_TimeBaseInitType.TIM_CounterMode = TIM_CounterMode_Up;
  119. }
  120. else
  121. {
  122. if (hwtimer_info->cntmode == HWTIMER_CNTMODE_UP)
  123. {
  124. TIM_TimeBaseInitType.TIM_CounterMode = TIM_CounterMode_Up;
  125. }
  126. else
  127. {
  128. TIM_TimeBaseInitType.TIM_CounterMode = TIM_CounterMode_Down;
  129. }
  130. }
  131. TIM_TimeBaseInit(hwtimer_dev->periph, &TIM_TimeBaseInitType);
  132. NVIC_InitStructure.NVIC_IRQChannel = hwtimer_dev->irqn;
  133. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  134. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  135. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  136. NVIC_Init(&NVIC_InitStructure);
  137. TIM_ITConfig(hwtimer_dev->periph, TIM_IT_Update, ENABLE);
  138. TIM_ClearITPendingBit(hwtimer_dev->periph, TIM_IT_Update);
  139. LOG_D("%s init success", hwtimer_dev->name);
  140. }
  141. }
  142. static rt_err_t ch32f2_hwtimer_start(struct rt_hwtimer_device *device, rt_uint32_t cnt, rt_hwtimer_mode_t mode)
  143. {
  144. struct hwtimer_device *hwtimer_dev;
  145. RT_ASSERT(device != RT_NULL);
  146. hwtimer_dev = (struct hwtimer_device *)device;
  147. /*
  148. * (1 / freq) = (cnt + 1) * (1 / (clk/(prescaler_value + 1) ) )
  149. */
  150. TIM_SetCounter(hwtimer_dev->periph, 0);
  151. TIM_SetAutoreload(hwtimer_dev->periph, cnt - 1);
  152. if (mode == HWTIMER_MODE_ONESHOT)
  153. {
  154. TIM_SelectOnePulseMode(hwtimer_dev->periph, TIM_OPMode_Single);
  155. }
  156. else
  157. {
  158. TIM_SelectOnePulseMode(hwtimer_dev->periph, TIM_OPMode_Repetitive);
  159. }
  160. TIM_Cmd(hwtimer_dev->periph, ENABLE);
  161. LOG_D("%s start, cnt = %d", hwtimer_dev->name, cnt);
  162. return RT_EOK;
  163. }
  164. static void ch32f2_hwtimer_stop(struct rt_hwtimer_device *device)
  165. {
  166. struct hwtimer_device *hwtimer_dev;
  167. RT_ASSERT(device != RT_NULL);
  168. hwtimer_dev = (struct hwtimer_device *)device;
  169. TIM_Cmd(hwtimer_dev->periph, DISABLE);
  170. TIM_SetCounter(hwtimer_dev->periph, 0);
  171. }
  172. static rt_uint32_t ch32f2_hwtimer_counter_get(struct rt_hwtimer_device *device)
  173. {
  174. struct hwtimer_device *hwtimer_dev;
  175. RT_ASSERT(device != RT_NULL);
  176. hwtimer_dev = (struct hwtimer_device *)device;
  177. return hwtimer_dev->periph->CNT;
  178. }
  179. static rt_err_t ch32f2_hwtimer_control(struct rt_hwtimer_device *device, rt_uint32_t cmd, void *arg)
  180. {
  181. struct hwtimer_device *hwtimer_dev;
  182. rt_err_t result = RT_EOK;
  183. RT_ASSERT(device != RT_NULL);
  184. hwtimer_dev = (struct hwtimer_device *)device;
  185. switch (cmd)
  186. {
  187. case HWTIMER_CTRL_FREQ_SET:
  188. {
  189. rt_uint32_t freq = 0;
  190. rt_uint32_t clk = 0;
  191. rt_uint16_t prescaler_value = 0;
  192. /*
  193. * (1 / freq) = (cnt + 1) * (1 / (clk/(prescaler_value + 1) ) )
  194. */
  195. if (arg != RT_NULL)
  196. {
  197. freq = *((rt_uint32_t *)arg);
  198. clk = ch32f2_tim_clock_get(hwtimer_dev->periph);
  199. prescaler_value = (rt_uint16_t)(clk / freq) - 1;
  200. TIM_PrescalerConfig(hwtimer_dev->periph, prescaler_value, TIM_PSCReloadMode_Immediate);
  201. }
  202. else
  203. {
  204. result = RT_EINVAL;
  205. }
  206. }
  207. break;
  208. default:
  209. result = RT_ENOSYS;
  210. break;
  211. }
  212. return result;
  213. }
  214. static const struct rt_hwtimer_ops hwtimer_ops =
  215. {
  216. .init = ch32f2_hwtimer_init,
  217. .start = ch32f2_hwtimer_start,
  218. .stop = ch32f2_hwtimer_stop,
  219. .count_get = ch32f2_hwtimer_counter_get,
  220. .control = ch32f2_hwtimer_control,
  221. };
  222. static int rt_hw_hwtimer_init(void)
  223. {
  224. rt_err_t ret;
  225. struct rt_hwtimer_info *hwtimer_info;
  226. #ifdef BSP_USING_TIM1_HWTIMER
  227. hwtimer_info = ch32f2_hwtimer_info_config_get(hwtimer_device1.periph);
  228. hwtimer_device1.parent.info = hwtimer_info;
  229. hwtimer_device1.parent.ops = &hwtimer_ops;
  230. ret = rt_device_hwtimer_register(&hwtimer_device1.parent, hwtimer_device1.name, RT_NULL);
  231. if (ret == RT_EOK)
  232. {
  233. LOG_D("hwtimer: %s register success.", hwtimer_device1.name);
  234. }
  235. else
  236. {
  237. LOG_D("hwtimer: %s register failed.", hwtimer_device1.name);
  238. }
  239. #endif
  240. #ifdef BSP_USING_TIM2_HWTIMER
  241. hwtimer_info = ch32f2_hwtimer_info_config_get(hwtimer_device2.periph);
  242. hwtimer_device2.parent.info = hwtimer_info;
  243. hwtimer_device2.parent.ops = &hwtimer_ops;
  244. ret = rt_device_hwtimer_register(&hwtimer_device2.parent, hwtimer_device2.name, RT_NULL);
  245. if (ret == RT_EOK)
  246. {
  247. LOG_D("hwtimer: %s register success.", hwtimer_device2.name);
  248. }
  249. else
  250. {
  251. LOG_D("hwtimer: %s register failed.", hwtimer_device2.name);
  252. }
  253. #endif
  254. #ifdef BSP_USING_TIM3_HWTIMER
  255. hwtimer_info = ch32f2_hwtimer_info_config_get(hwtimer_device3.periph);
  256. hwtimer_device3.parent.info = hwtimer_info;
  257. hwtimer_device3.parent.ops = &hwtimer_ops;
  258. ret = rt_device_hwtimer_register(&hwtimer_device3.parent, hwtimer_device3.name, RT_NULL);
  259. if (ret == RT_EOK)
  260. {
  261. LOG_D("hwtimer: %s register success.", hwtimer_device3.name);
  262. }
  263. else
  264. {
  265. LOG_D("hwtimer: %s register failed.", hwtimer_device3.name);
  266. }
  267. #endif
  268. #ifdef BSP_USING_TIM4_HWTIMER
  269. hwtimer_info = ch32f2_hwtimer_info_config_get(hwtimer_device4.periph);
  270. hwtimer_device4.parent.info = hwtimer_info;
  271. hwtimer_device4.parent.ops = &hwtimer_ops;
  272. ret = rt_device_hwtimer_register(&hwtimer_device4.parent, hwtimer_device4.name, RT_NULL);
  273. if (ret == RT_EOK)
  274. {
  275. LOG_D("hwtimer: %s register success.", hwtimer_device4.name);
  276. }
  277. else
  278. {
  279. LOG_D("hwtimer: %s register failed.", hwtimer_device4.name);
  280. }
  281. #endif
  282. #ifdef BSP_USING_TIM5_HWTIMER
  283. hwtimer_info = ch32f2_hwtimer_info_config_get(hwtimer_device5.periph);
  284. hwtimer_device5.parent.info = hwtimer_info;
  285. hwtimer_device5.parent.ops = &hwtimer_ops;
  286. ret = rt_device_hwtimer_register(&hwtimer_device5.parent, hwtimer_device5.name, RT_NULL);
  287. if (ret == RT_EOK)
  288. {
  289. LOG_D("hwtimer: %s register success.", hwtimer_device5.name);
  290. }
  291. else
  292. {
  293. LOG_D("hwtimer: %s register failed.", hwtimer_device5.name);
  294. }
  295. #endif
  296. #ifdef BSP_USING_TIM6_HWTIMER
  297. hwtimer_info = ch32f2_hwtimer_info_config_get(hwtimer_device6.periph);
  298. hwtimer_device6.parent.info = hwtimer_info;
  299. hwtimer_device6.parent.ops = &hwtimer_ops;
  300. ret = rt_device_hwtimer_register(&hwtimer_device6.parent, hwtimer_device6.name, RT_NULL);
  301. if (ret == RT_EOK)
  302. {
  303. LOG_D("hwtimer: %s register success.", hwtimer_device6.name);
  304. }
  305. else
  306. {
  307. LOG_D("hwtimer: %s register failed.", hwtimer_device6.name);
  308. }
  309. #endif
  310. #ifdef BSP_USING_TIM7_HWTIMER
  311. hwtimer_info = ch32f2_hwtimer_info_config_get(hwtimer_device7.periph);
  312. hwtimer_device7.parent.info = hwtimer_info;
  313. hwtimer_device7.parent.ops = &hwtimer_ops;
  314. ret = rt_device_hwtimer_register(&hwtimer_device7.parent, hwtimer_device7.name, RT_NULL);
  315. if (ret == RT_EOK)
  316. {
  317. LOG_D("hwtimer: %s register success.", hwtimer_device7.name);
  318. }
  319. else
  320. {
  321. LOG_D("hwtimer: %s register failed.", hwtimer_device7.name);
  322. }
  323. #endif
  324. #ifdef BSP_USING_TIM8_HWTIMER
  325. hwtimer_info = ch32f2_hwtimer_info_config_get(hwtimer_device8.periph);
  326. hwtimer_device8.parent.info = hwtimer_info;
  327. hwtimer_device8.parent.ops = &hwtimer_ops;
  328. ret = rt_device_hwtimer_register(&hwtimer_device8.parent, hwtimer_device8.name, RT_NULL);
  329. if (ret == RT_EOK)
  330. {
  331. LOG_D("hwtimer: %s register success.", hwtimer_device8.name);
  332. }
  333. else
  334. {
  335. LOG_D("hwtimer: %s register failed.", hwtimer_device8.name);
  336. }
  337. #endif
  338. #ifdef BSP_USING_TIM9_HWTIMER
  339. hwtimer_info = ch32f2_hwtimer_info_config_get(hwtimer_device9.periph);
  340. hwtimer_device9.parent.info = hwtimer_info;
  341. hwtimer_device9.parent.ops = &hwtimer_ops;
  342. ret = rt_device_hwtimer_register(&hwtimer_device9.parent, hwtimer_device9.name, RT_NULL);
  343. if (ret == RT_EOK)
  344. {
  345. LOG_D("hwtimer: %s register success.", hwtimer_device9.name);
  346. }
  347. else
  348. {
  349. LOG_D("hwtimer: %s register failed.", hwtimer_device9.name);
  350. }
  351. #endif
  352. #ifdef BSP_USING_TIM10_HWTIMER
  353. hwtimer_info = ch32f2_hwtimer_info_config_get(hwtimer_device10.periph);
  354. hwtimer_device10.parent.info = hwtimer_info;
  355. hwtimer_device10.parent.ops = &hwtimer_ops;
  356. ret = rt_device_hwtimer_register(&hwtimer_device10.parent, hwtimer_device10.name, RT_NULL);
  357. if (ret == RT_EOK)
  358. {
  359. LOG_D("hwtimer: %s register success.", hwtimer_device10.name);
  360. }
  361. else
  362. {
  363. LOG_D("hwtimer: %s register failed.", hwtimer_device10.name);
  364. }
  365. #endif
  366. return RT_EOK;
  367. }
  368. INIT_DEVICE_EXPORT(rt_hw_hwtimer_init);
  369. #ifdef BSP_USING_TIM1_HWTIMER
  370. void TIM1_UP_IRQHandler(void)
  371. {
  372. /* enter interrupt */
  373. rt_interrupt_enter();
  374. if (TIM_GetITStatus(hwtimer_device1.periph, TIM_IT_Update) == SET)
  375. {
  376. TIM_ClearITPendingBit(hwtimer_device1.periph, TIM_IT_Update);
  377. rt_device_hwtimer_isr(&hwtimer_device1.parent);
  378. }
  379. /* leave interrupt */
  380. rt_interrupt_leave();
  381. }
  382. #endif
  383. #ifdef BSP_USING_TIM2_HWTIMER
  384. void TIM2_IRQHandler(void)
  385. {
  386. /* enter interrupt */
  387. rt_interrupt_enter();
  388. if (TIM_GetITStatus(hwtimer_device2.periph, TIM_IT_Update) == SET)
  389. {
  390. TIM_ClearITPendingBit(hwtimer_device2.periph, TIM_IT_Update);
  391. rt_device_hwtimer_isr(&hwtimer_device2.parent);
  392. }
  393. /* leave interrupt */
  394. rt_interrupt_leave();
  395. }
  396. #endif
  397. #ifdef BSP_USING_TIM3_HWTIMER
  398. void TIM3_IRQHandler(void)
  399. {
  400. /* enter interrupt */
  401. rt_interrupt_enter();
  402. if (TIM_GetITStatus(hwtimer_device3.periph, TIM_IT_Update) == SET)
  403. {
  404. TIM_ClearITPendingBit(hwtimer_device3.periph, TIM_IT_Update);
  405. rt_device_hwtimer_isr(&hwtimer_device3.parent);
  406. }
  407. /* leave interrupt */
  408. rt_interrupt_leave();
  409. }
  410. #endif
  411. #ifdef BSP_USING_TIM4_HWTIMER
  412. void TIM4_IRQHandler(void)
  413. {
  414. /* enter interrupt */
  415. rt_interrupt_enter();
  416. if (TIM_GetITStatus(hwtimer_device4.periph, TIM_IT_Update) == SET)
  417. {
  418. TIM_ClearITPendingBit(hwtimer_device4.periph, TIM_IT_Update);
  419. rt_device_hwtimer_isr(&hwtimer_device4.parent);
  420. }
  421. /* leave interrupt */
  422. rt_interrupt_leave();
  423. }
  424. #endif
  425. #ifdef BSP_USING_TIM5_HWTIMER
  426. void TIM5_IRQHandler(void)
  427. {
  428. /* enter interrupt */
  429. rt_interrupt_enter();
  430. if (TIM_GetITStatus(hwtimer_device5.periph, TIM_IT_Update) == SET)
  431. {
  432. TIM_ClearITPendingBit(hwtimer_device5.periph, TIM_IT_Update);
  433. rt_device_hwtimer_isr(&hwtimer_device5.parent);
  434. }
  435. /* leave interrupt */
  436. rt_interrupt_leave();
  437. }
  438. #endif
  439. #ifdef BSP_USING_TIM6_HWTIMER
  440. void TIM6_IRQHandler(void)
  441. {
  442. /* enter interrupt */
  443. rt_interrupt_enter();
  444. if (TIM_GetITStatus(hwtimer_device6.periph, TIM_IT_Update) == SET)
  445. {
  446. TIM_ClearITPendingBit(hwtimer_device6.periph, TIM_IT_Update);
  447. rt_device_hwtimer_isr(&hwtimer_device6.parent);
  448. }
  449. /* leave interrupt */
  450. rt_interrupt_leave();
  451. }
  452. #endif
  453. #ifdef BSP_USING_TIM7_HWTIMER
  454. void TIM7_IRQHandler(void)
  455. {
  456. /* enter interrupt */
  457. rt_interrupt_enter();
  458. if (TIM_GetITStatus(hwtimer_device7.periph, TIM_IT_Update) == SET)
  459. {
  460. TIM_ClearITPendingBit(hwtimer_device7.periph, TIM_IT_Update);
  461. rt_device_hwtimer_isr(&hwtimer_device7.parent);
  462. }
  463. /* leave interrupt */
  464. rt_interrupt_leave();
  465. }
  466. #endif
  467. #ifdef BSP_USING_TIM8_HWTIMER
  468. void TIM8_UP_IRQHandler(void)
  469. {
  470. /* enter interrupt */
  471. rt_interrupt_enter();
  472. if (TIM_GetITStatus(hwtimer_device8.periph, TIM_IT_Update) == SET)
  473. {
  474. TIM_ClearITPendingBit(hwtimer_device8.periph, TIM_IT_Update);
  475. rt_device_hwtimer_isr(&hwtimer_device8.parent);
  476. }
  477. /* leave interrupt */
  478. rt_interrupt_leave();
  479. }
  480. #endif
  481. #ifdef BSP_USING_TIM9_HWTIMER
  482. void TIM9_UP_IRQHandler(void)
  483. {
  484. /* enter interrupt */
  485. rt_interrupt_enter();
  486. if (TIM_GetITStatus(hwtimer_device9.periph, TIM_IT_Update) == SET)
  487. {
  488. TIM_ClearITPendingBit(hwtimer_device9.periph, TIM_IT_Update);
  489. rt_device_hwtimer_isr(&hwtimer_device9.parent);
  490. }
  491. /* leave interrupt */
  492. rt_interrupt_leave();
  493. }
  494. #endif
  495. #ifdef BSP_USING_TIM10_HWTIMER
  496. void TIM10_UP_IRQHandler(void)
  497. {
  498. /* enter interrupt */
  499. rt_interrupt_enter();
  500. if (TIM_GetITStatus(hwtimer_device10.periph, TIM_IT_Update) == SET)
  501. {
  502. TIM_ClearITPendingBit(hwtimer_device10.periph, TIM_IT_Update);
  503. rt_device_hwtimer_isr(&hwtimer_device10.parent);
  504. }
  505. /* leave interrupt */
  506. rt_interrupt_leave();
  507. }
  508. #endif
  509. #endif /* BSP_USING_HWTIMER */