drv_hwtimer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * Copyright (c) 2006-2022, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-07-01 lik first version
  9. */
  10. #include "drv_hwtimer.h"
  11. #ifdef RT_USING_HWTIMER
  12. #ifdef BSP_USING_TIM
  13. //#define DRV_DEBUG
  14. #define LOG_TAG "drv.hwtimer"
  15. #include <drv_log.h>
  16. #if !defined(BSP_USING_TIM0) && !defined(BSP_USING_TIM1) && !defined(BSP_USING_TIM2) && !defined(BSP_USING_TIM3) \
  17. && !defined(BSP_USING_TIM4) && !defined(BSP_USING_BTIM0) && !defined(BSP_USING_BTIM1) && !defined(BSP_USING_BTIM2) \
  18. && !defined(BSP_USING_BTIM3) && !defined(BSP_USING_BTIM4) && !defined(BSP_USING_BTIM5) && !defined(BSP_USING_BTIM6) \
  19. && !defined(BSP_USING_BTIM7) && !defined(BSP_USING_BTIM8) && !defined(BSP_USING_BTIM9) && !defined(BSP_USING_BTIM10) \
  20. && !defined(BSP_USING_BTIM11)
  21. #error "Please define at least one BSP_USING_TIMx or BSP_USING_BTIMx"
  22. /* this driver can be disabled at menuconfig ? RT-Thread Components ? Device Drivers */
  23. #endif
  24. #ifndef TIM_DEV_INFO_CONFIG
  25. #define TIM_DEV_INFO_CONFIG \
  26. { \
  27. .maxfreq = 1000000, \
  28. .minfreq = 1000000, \
  29. .maxcnt = 0xFFFFFFFF, \
  30. .cntmode = HWTIMER_CNTMODE_DW, \
  31. }
  32. #endif /* TIM_DEV_INFO_CONFIG */
  33. #ifdef BSP_USING_TIM0
  34. #ifndef TIM0_CFG
  35. #define TIM0_CFG \
  36. { \
  37. .name = "timer0", \
  38. .TIMRx = TIMR0, \
  39. }
  40. #endif /* TIM0_CFG */
  41. #endif /* BSP_USING_TIM0 */
  42. #ifdef BSP_USING_TIM1
  43. #ifndef TIM1_CFG
  44. #define TIM1_CFG \
  45. { \
  46. .name = "timer1", \
  47. .TIMRx = TIMR1, \
  48. }
  49. #endif /* TIM1_CFG */
  50. #endif /* BSP_USING_TIM1 */
  51. #ifdef BSP_USING_TIM2
  52. #ifndef TIM2_CFG
  53. #define TIM2_CFG \
  54. { \
  55. .name = "timer2", \
  56. .TIMRx = TIMR2, \
  57. }
  58. #endif /* TIM2_CFG */
  59. #endif /* BSP_USING_TIM2 */
  60. #ifdef BSP_USING_TIM3
  61. #ifndef TIM3_CFG
  62. #define TIM3_CFG \
  63. { \
  64. .name = "timer3", \
  65. .TIMRx = TIMR3, \
  66. }
  67. #endif /* TIM3_CFG */
  68. #endif /* BSP_USING_TIM3 */
  69. #ifdef BSP_USING_TIM4
  70. #ifndef TIM4_CFG
  71. #define TIM4_CFG \
  72. { \
  73. .name = "timer4", \
  74. .TIMRx = TIMR4, \
  75. }
  76. #endif /* TIM4_CFG */
  77. #endif /* BSP_USING_TIM4 */
  78. #ifdef BSP_USING_BTIM0
  79. #ifndef BTIM0_CFG
  80. #define BTIM0_CFG \
  81. { \
  82. .name = "btimer0", \
  83. .TIMRx = BTIMR0, \
  84. }
  85. #endif /* BTIM0_CFG */
  86. #endif /* BSP_USING_BTIM0 */
  87. #ifdef BSP_USING_BTIM1
  88. #ifndef BTIM1_CFG
  89. #define BTIM1_CFG \
  90. { \
  91. .name = "btimer1", \
  92. .TIMRx = BTIMR1, \
  93. }
  94. #endif /* BTIM1_CFG */
  95. #endif /* BSP_USING_BTIM1 */
  96. #ifdef BSP_USING_BTIM2
  97. #ifndef BTIM2_CFG
  98. #define BTIM2_CFG \
  99. { \
  100. .name = "btimer2", \
  101. .TIMRx = BTIMR2, \
  102. }
  103. #endif /* BTIM2_CFG */
  104. #endif /* BSP_USING_BTIM2 */
  105. #ifdef BSP_USING_BTIM3
  106. #ifndef BTIM3_CFG
  107. #define BTIM3_CFG \
  108. { \
  109. .name = "btimer3", \
  110. .TIMRx = BTIMR3, \
  111. }
  112. #endif /* BTIM3_CFG */
  113. #endif /* BSP_USING_BTIM3 */
  114. #ifdef BSP_USING_BTIM4
  115. #ifndef BTIM4_CFG
  116. #define BTIM4_CFG \
  117. { \
  118. .name = "btimer4", \
  119. .TIMRx = BTIMR4, \
  120. }
  121. #endif /* BTIM4_CFG */
  122. #endif /* BSP_USING_BTIM4 */
  123. #ifdef BSP_USING_BTIM5
  124. #ifndef BTIM5_CFG
  125. #define BTIM5_CFG \
  126. { \
  127. .name = "btimer5", \
  128. .TIMRx = BTIMR5, \
  129. }
  130. #endif /* BTIM5_CFG */
  131. #endif /* BSP_USING_BTIM5 */
  132. #ifdef BSP_USING_BTIM6
  133. #ifndef BTIM6_CFG
  134. #define BTIM6_CFG \
  135. { \
  136. .name = "btimer6", \
  137. .TIMRx = BTIMR6, \
  138. }
  139. #endif /* BTIM6_CFG */
  140. #endif /* BSP_USING_BTIM6 */
  141. #ifdef BSP_USING_BTIM7
  142. #ifndef BTIM7_CFG
  143. #define BTIM7_CFG \
  144. { \
  145. .name = "btimer7", \
  146. .TIMRx = BTIMR7, \
  147. }
  148. #endif /* BTIM7_CFG */
  149. #endif /* BSP_USING_BTIM7 */
  150. #ifdef BSP_USING_BTIM8
  151. #ifndef BTIM8_CFG
  152. #define BTIM8_CFG \
  153. { \
  154. .name = "btimer8", \
  155. .TIMRx = BTIMR8, \
  156. }
  157. #endif /* BTIM8_CFG */
  158. #endif /* BSP_USING_BTIM8 */
  159. #ifdef BSP_USING_BTIM9
  160. #ifndef BTIM9_CFG
  161. #define BTIM9_CFG \
  162. { \
  163. .name = "btimer9", \
  164. .TIMRx = BTIMR9, \
  165. }
  166. #endif /* BTIM9_CFG */
  167. #endif /* BSP_USING_BTIM9 */
  168. #ifdef BSP_USING_BTIM10
  169. #ifndef BTIM10_CFG
  170. #define BTIM10_CFG \
  171. { \
  172. .name = "btimer10", \
  173. .TIMRx = BTIMR10, \
  174. }
  175. #endif /* BTIM10_CFG */
  176. #endif /* BSP_USING_BTIM10 */
  177. #ifdef BSP_USING_BTIM11
  178. #ifndef BTIM11_CFG
  179. #define BTIM11_CFG \
  180. { \
  181. .name = "btimer11", \
  182. .TIMRx = BTIMR11, \
  183. }
  184. #endif /* BTIM11_CFG */
  185. #endif /* BSP_USING_BTIM11 */
  186. struct swm_hwtimer_cfg
  187. {
  188. char *name;
  189. TIMR_TypeDef *TIMRx;
  190. };
  191. struct swm_hwtimer_device
  192. {
  193. struct swm_hwtimer_cfg *hwtimer_cfg;
  194. rt_hwtimer_t time_device;
  195. };
  196. enum
  197. {
  198. #ifdef BSP_USING_TIM0
  199. TIM0_INDEX,
  200. #endif
  201. #ifdef BSP_USING_TIM1
  202. TIM1_INDEX,
  203. #endif
  204. #ifdef BSP_USING_TIM2
  205. TIM2_INDEX,
  206. #endif
  207. #ifdef BSP_USING_TIM3
  208. TIM3_INDEX,
  209. #endif
  210. #ifdef BSP_USING_TIM4
  211. TIM4_INDEX,
  212. #endif
  213. #ifdef BSP_USING_BTIM0
  214. BTIM0_INDEX,
  215. #endif
  216. #ifdef BSP_USING_BTIM1
  217. BTIM1_INDEX,
  218. #endif
  219. #ifdef BSP_USING_BTIM2
  220. BTIM2_INDEX,
  221. #endif
  222. #ifdef BSP_USING_BTIM3
  223. BTIM3_INDEX,
  224. #endif
  225. #ifdef BSP_USING_BTIM4
  226. BTIM4_INDEX,
  227. #endif
  228. #ifdef BSP_USING_BTIM5
  229. BTIM5_INDEX,
  230. #endif
  231. #ifdef BSP_USING_BTIM6
  232. BTIM6_INDEX,
  233. #endif
  234. #ifdef BSP_USING_BTIM7
  235. BTIM7_INDEX,
  236. #endif
  237. #ifdef BSP_USING_BTIM8
  238. BTIM8_INDEX,
  239. #endif
  240. #ifdef BSP_USING_BTIM9
  241. BTIM9_INDEX,
  242. #endif
  243. #ifdef BSP_USING_BTIM10
  244. BTIM10_INDEX,
  245. #endif
  246. #ifdef BSP_USING_BTIM11
  247. BTIM11_INDEX,
  248. #endif
  249. };
  250. static struct swm_hwtimer_cfg swm_hwtimer_cfg[] =
  251. {
  252. #ifdef BSP_USING_TIM0
  253. TIM0_CFG,
  254. #endif
  255. #ifdef BSP_USING_TIM1
  256. TIM1_CFG,
  257. #endif
  258. #ifdef BSP_USING_TIM2
  259. TIM2_CFG,
  260. #endif
  261. #ifdef BSP_USING_TIM3
  262. TIM3_CFG,
  263. #endif
  264. #ifdef BSP_USING_TIM4
  265. TIM4_CFG,
  266. #endif
  267. #ifdef BSP_USING_BTIM0
  268. BTIM0_CFG,
  269. #endif
  270. #ifdef BSP_USING_BTIM1
  271. BTIM1_CFG,
  272. #endif
  273. #ifdef BSP_USING_BTIM2
  274. BTIM2_CFG,
  275. #endif
  276. #ifdef BSP_USING_BTIM3
  277. BTIM3_CFG,
  278. #endif
  279. #ifdef BSP_USING_BTIM4
  280. BTIM4_CFG,
  281. #endif
  282. #ifdef BSP_USING_BTIM5
  283. BTIM5_CFG,
  284. #endif
  285. #ifdef BSP_USING_BTIM6
  286. BTIM6_CFG,
  287. #endif
  288. #ifdef BSP_USING_BTIM7
  289. BTIM7_CFG,
  290. #endif
  291. #ifdef BSP_USING_BTIM8
  292. BTIM8_CFG,
  293. #endif
  294. #ifdef BSP_USING_BTIM9
  295. BTIM9_CFG,
  296. #endif
  297. #ifdef BSP_USING_BTIM10
  298. BTIM10_CFG,
  299. #endif
  300. #ifdef BSP_USING_BTIM11
  301. BTIM11_CFG,
  302. #endif
  303. };
  304. static struct swm_hwtimer_device hwtimer_obj[sizeof(swm_hwtimer_cfg) / sizeof(swm_hwtimer_cfg[0])] = {0};
  305. static void swm_timer_configure(struct rt_hwtimer_device *timer_device, rt_uint32_t state)
  306. {
  307. struct swm_hwtimer_cfg *hwtimer_cfg = RT_NULL;
  308. RT_ASSERT(timer_device != RT_NULL);
  309. if (state)
  310. {
  311. hwtimer_cfg = timer_device->parent.user_data;
  312. TIMR_Init(hwtimer_cfg->TIMRx, TIMR_MODE_TIMER, CyclesPerUs, 1000000, 1);
  313. timer_device->freq = 1000000;
  314. }
  315. }
  316. static rt_err_t swm_timer_start(rt_hwtimer_t *timer_device, rt_uint32_t cnt, rt_hwtimer_mode_t opmode)
  317. {
  318. rt_err_t result = RT_EOK;
  319. struct swm_hwtimer_cfg *hwtimer_cfg = RT_NULL;
  320. RT_ASSERT(timer_device != RT_NULL);
  321. hwtimer_cfg = timer_device->parent.user_data;
  322. if (opmode == HWTIMER_MODE_ONESHOT)
  323. {
  324. /* set timer to single mode */
  325. timer_device->mode = HWTIMER_MODE_ONESHOT;
  326. }
  327. else
  328. {
  329. timer_device->mode = HWTIMER_MODE_PERIOD;
  330. }
  331. hwtimer_cfg->TIMRx->LOAD = cnt - 1;
  332. TIMR_Stop(hwtimer_cfg->TIMRx);
  333. TIMR_Start(hwtimer_cfg->TIMRx);
  334. return result;
  335. }
  336. static void swm_timer_stop(rt_hwtimer_t *timer_device)
  337. {
  338. struct swm_hwtimer_cfg *hwtimer_cfg = RT_NULL;
  339. RT_ASSERT(timer_device != RT_NULL);
  340. hwtimer_cfg = timer_device->parent.user_data;
  341. /* stop timer */
  342. TIMR_Stop(hwtimer_cfg->TIMRx);
  343. }
  344. static rt_uint32_t swm_timer_count_get(rt_hwtimer_t *timer_device)
  345. {
  346. struct swm_hwtimer_cfg *hwtimer_cfg = RT_NULL;
  347. RT_ASSERT(timer_device != RT_NULL);
  348. hwtimer_cfg = timer_device->parent.user_data;
  349. return TIMR_GetCurValue(hwtimer_cfg->TIMRx);
  350. }
  351. static rt_err_t swm_timer_control(rt_hwtimer_t *timer_device, rt_uint32_t cmd, void *args)
  352. {
  353. struct swm_hwtimer_cfg *hwtimer_cfg = RT_NULL;
  354. rt_err_t result = RT_EOK;
  355. RT_ASSERT(timer_device != RT_NULL);
  356. RT_ASSERT(args != RT_NULL);
  357. hwtimer_cfg = timer_device->parent.user_data;
  358. switch (cmd)
  359. {
  360. case HWTIMER_CTRL_FREQ_SET:
  361. {
  362. rt_uint32_t freq;
  363. freq = *(rt_uint32_t *)args;
  364. TIMR_Init(hwtimer_cfg->TIMRx, TIMR_MODE_TIMER, CyclesPerUs, freq, 1);
  365. }
  366. break;
  367. default:
  368. {
  369. result = -RT_ENOSYS;
  370. }
  371. break;
  372. }
  373. return result;
  374. }
  375. static const struct rt_hwtimer_info _info = TIM_DEV_INFO_CONFIG;
  376. static const struct rt_hwtimer_ops swm_timer_ops =
  377. {
  378. .init = swm_timer_configure,
  379. .start = swm_timer_start,
  380. .stop = swm_timer_stop,
  381. .count_get = swm_timer_count_get,
  382. .control = swm_timer_control};
  383. void swm_timer_isr(rt_hwtimer_t *timer_device)
  384. {
  385. struct swm_hwtimer_cfg *hwtimer_cfg = RT_NULL;
  386. RT_ASSERT(timer_device != RT_NULL);
  387. hwtimer_cfg = timer_device->parent.user_data;
  388. TIMR_INTClr(hwtimer_cfg->TIMRx);
  389. rt_device_hwtimer_isr(timer_device);
  390. }
  391. #ifdef BSP_USING_TIM0
  392. void TIMR0_Handler(void)
  393. {
  394. rt_interrupt_enter();
  395. swm_timer_isr(&(hwtimer_obj[TIM0_INDEX].time_device));
  396. rt_interrupt_leave();
  397. }
  398. #endif // BSP_USING_TIM0
  399. #ifdef BSP_USING_TIM1
  400. void TIMR1_Handler(void)
  401. {
  402. rt_interrupt_enter();
  403. swm_timer_isr(&(hwtimer_obj[TIM1_INDEX].time_device));
  404. rt_interrupt_leave();
  405. }
  406. #endif // BSP_USING_TIM1
  407. #ifdef BSP_USING_TIM2
  408. void TIMR2_Handler(void)
  409. {
  410. rt_interrupt_enter();
  411. swm_timer_isr(&(hwtimer_obj[TIM2_INDEX].time_device));
  412. rt_interrupt_leave();
  413. }
  414. #endif // BSP_USING_TIM2
  415. #ifdef BSP_USING_TIM3
  416. void TIMR3_Handler(void)
  417. {
  418. rt_interrupt_enter();
  419. swm_timer_isr(&(hwtimer_obj[TIM3_INDEX].time_device));
  420. rt_interrupt_leave();
  421. }
  422. #endif // BSP_USING_TIM3
  423. #ifdef BSP_USING_TIM4
  424. void TIMR4_Handler(void)
  425. {
  426. rt_interrupt_enter();
  427. swm_timer_isr(&(hwtimer_obj[TIM4_INDEX].time_device));
  428. rt_interrupt_leave();
  429. }
  430. #endif // BSP_USING_TIM4
  431. #ifdef BSP_USING_BTIM0
  432. void BTIMR0_Handler(void)
  433. {
  434. rt_interrupt_enter();
  435. swm_timer_isr(&(hwtimer_obj[BTIM0_INDEX].time_device));
  436. rt_interrupt_leave();
  437. }
  438. #endif // BSP_USING_BTIM0
  439. #ifdef BSP_USING_BTIM1
  440. void BTIMR1_Handler(void)
  441. {
  442. rt_interrupt_enter();
  443. swm_timer_isr(&(hwtimer_obj[BTIM1_INDEX].time_device));
  444. rt_interrupt_leave();
  445. }
  446. #endif // BSP_USING_BTIM1
  447. #ifdef BSP_USING_BTIM2
  448. void BTIMR2_Handler(void)
  449. {
  450. rt_interrupt_enter();
  451. swm_timer_isr(&(hwtimer_obj[BTIM2_INDEX].time_device));
  452. rt_interrupt_leave();
  453. }
  454. #endif // BSP_USING_BTIM2
  455. #ifdef BSP_USING_BTIM3
  456. void BTIMR3_Handler(void)
  457. {
  458. rt_interrupt_enter();
  459. swm_timer_isr(&(hwtimer_obj[BTIM3_INDEX].time_device));
  460. rt_interrupt_leave();
  461. }
  462. #endif // BSP_USING_BTIM3
  463. #ifdef BSP_USING_BTIM4
  464. void BTIMR4_Handler(void)
  465. {
  466. rt_interrupt_enter();
  467. swm_timer_isr(&(hwtimer_obj[BTIM4_INDEX].time_device));
  468. rt_interrupt_leave();
  469. }
  470. #endif // BSP_USING_BTIM4
  471. #ifdef BSP_USING_BTIM5
  472. void BTIMR5_Handler(void)
  473. {
  474. rt_interrupt_enter();
  475. swm_timer_isr(&(hwtimer_obj[BTIM5_INDEX].time_device));
  476. rt_interrupt_leave();
  477. }
  478. #endif // BSP_USING_BTIM5
  479. #ifdef BSP_USING_BTIM6
  480. void BTIMR6_Handler(void)
  481. {
  482. rt_interrupt_enter();
  483. swm_timer_isr(&(hwtimer_obj[BTIM6_INDEX].time_device));
  484. rt_interrupt_leave();
  485. }
  486. #endif // BSP_USING_BTIM6
  487. #ifdef BSP_USING_BTIM7
  488. void BTIMR7_Handler(void)
  489. {
  490. rt_interrupt_enter();
  491. swm_timer_isr(&(hwtimer_obj[BTIM7_INDEX].time_device));
  492. rt_interrupt_leave();
  493. }
  494. #endif // BSP_USING_BTIM7
  495. #ifdef BSP_USING_BTIM8
  496. void BTIMR8_Handler(void)
  497. {
  498. rt_interrupt_enter();
  499. swm_timer_isr(&(hwtimer_obj[BTIM8_INDEX].time_device));
  500. rt_interrupt_leave();
  501. }
  502. #endif // BSP_USING_BTIM8
  503. #ifdef BSP_USING_BTIM9
  504. void BTIMR9_Handler(void)
  505. {
  506. rt_interrupt_enter();
  507. swm_timer_isr(&(hwtimer_obj[BTIM9_INDEX].time_device));
  508. rt_interrupt_leave();
  509. }
  510. #endif // BSP_USING_BTIM9
  511. #ifdef BSP_USING_BTIM10
  512. void BTIMR10_Handler(void)
  513. {
  514. rt_interrupt_enter();
  515. swm_timer_isr(&(hwtimer_obj[BTIM10_INDEX].time_device));
  516. rt_interrupt_leave();
  517. }
  518. #endif // BSP_USING_BTIM10
  519. #ifdef BSP_USING_BTIM11
  520. void BTIMR11_Handler(void)
  521. {
  522. rt_interrupt_enter();
  523. swm_timer_isr(&(hwtimer_obj[BTIM11_INDEX].time_device));
  524. rt_interrupt_leave();
  525. }
  526. #endif // BSP_USING_BTIM11
  527. int swm_timer_init(void)
  528. {
  529. int i = 0;
  530. int result = RT_EOK;
  531. for (i = 0; i < sizeof(swm_hwtimer_cfg) / sizeof(swm_hwtimer_cfg[0]); i++)
  532. {
  533. hwtimer_obj[i].hwtimer_cfg = &swm_hwtimer_cfg[i];
  534. hwtimer_obj[i].time_device.info = &_info;
  535. hwtimer_obj[i].time_device.ops = &swm_timer_ops;
  536. result = rt_device_hwtimer_register(&hwtimer_obj[i].time_device, hwtimer_obj[i].hwtimer_cfg->name, hwtimer_obj[i].hwtimer_cfg);
  537. if (result != RT_EOK)
  538. {
  539. LOG_E("%s register fail.", hwtimer_obj[i].hwtimer_cfg->name);
  540. }
  541. else
  542. {
  543. LOG_D("%s register success.", hwtimer_obj[i].hwtimer_cfg->name);
  544. }
  545. }
  546. return result;
  547. }
  548. INIT_BOARD_EXPORT(swm_timer_init);
  549. #endif /* BSP_USING_TIM */
  550. #endif /* RT_USING_HWTIMER */