drv_hwtimer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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-10-19 Nations first version
  9. */
  10. #include "drv_hwtimer.h"
  11. #ifdef RT_USING_HWTIMER
  12. #if defined(BSP_USING_HWTIMER1) || defined(BSP_USING_HWTIMER2) || defined(BSP_USING_HWTIMER3) || \
  13. defined(BSP_USING_HWTIMER4) || defined(BSP_USING_HWTIMER5) || defined(BSP_USING_HWTIMER6) || \
  14. defined(BSP_USING_HWTIMER7) || defined(BSP_USING_HWTIMER8) || defined(BSP_USING_HWTIMER9)
  15. static struct n32_hwtimer_config hwtimer_config[] =
  16. {
  17. #ifdef BSP_USING_HWTIMER1
  18. {
  19. "timer1",
  20. TIM1,
  21. TIM1_UP_IRQn,
  22. },
  23. #endif
  24. #ifdef BSP_USING_HWTIMER2
  25. {
  26. "timer2",
  27. TIM2,
  28. TIM2_IRQn,
  29. },
  30. #endif
  31. #ifdef BSP_USING_HWTIMER3
  32. {
  33. "timer3",
  34. TIM3,
  35. TIM3_IRQn,
  36. },
  37. #endif
  38. #ifdef BSP_USING_HWTIMER4
  39. {
  40. "timer4",
  41. TIM4,
  42. TIM4_IRQn,
  43. },
  44. #endif
  45. #ifdef BSP_USING_HWTIMER5
  46. {
  47. "timer5",
  48. TIM5,
  49. TIM5_IRQn,
  50. },
  51. #endif
  52. #ifdef BSP_USING_HWTIMER6
  53. {
  54. "timer6",
  55. TIM6,
  56. TIM6_IRQn,
  57. },
  58. #endif
  59. #ifdef BSP_USING_HWTIMER7
  60. {
  61. "timer7",
  62. TIM7,
  63. TIM7_IRQn,
  64. },
  65. #endif
  66. #ifdef BSP_USING_HWTIMER8
  67. {
  68. "timer8",
  69. TIM8,
  70. TIM8_UP_IRQn,
  71. },
  72. #endif
  73. #ifdef BSP_USING_HWTIMER9
  74. {
  75. "timer9",
  76. TIM9,
  77. TIM9_IRQn,
  78. },
  79. #endif
  80. };
  81. uint8_t tim1_count = 0, tim2_count = 0, tim3_count = 0, tim4_count = 0,tim5_count = 0, tim6_count = 0, tim7_count = 0, tim8_count = 0;
  82. #if defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  83. uint8_t tim9_count = 0;
  84. #endif
  85. static void caculate_tim_count()
  86. {
  87. uint8_t count = 0;
  88. #ifdef BSP_USING_HWTIMER1
  89. tim1_count = count;
  90. count++;
  91. #endif
  92. #ifdef BSP_USING_HWTIMER2
  93. tim2_count = count;
  94. count++;
  95. #endif
  96. #ifdef BSP_USING_HWTIMER3
  97. tim3_count = count;
  98. count++;
  99. #endif
  100. #ifdef BSP_USING_HWTIMER4
  101. tim4_count = count;
  102. count++;
  103. #endif
  104. #ifdef BSP_USING_HWTIMER5
  105. tim5_count = count;
  106. count++;
  107. #endif
  108. #ifdef BSP_USING_HWTIMER6
  109. tim6_count = count;
  110. count++;
  111. #endif
  112. #ifdef BSP_USING_HWTIMER7
  113. tim7_count = count;
  114. count++;
  115. #endif
  116. #ifdef BSP_USING_HWTIMER8
  117. tim8_count = count;
  118. count++;
  119. #endif
  120. #ifdef BSP_USING_HWTIMER9
  121. tim9_count = count;
  122. count++;
  123. #endif
  124. }
  125. #define BITS(start, end) ((0xFFFFFFFFUL << (start)) & (0xFFFFFFFFUL >> (31U - (uint32_t)(end))))
  126. #define GET_BITS(regval, start, end) (((regval) & BITS((start),(end))) >> (start))
  127. static struct n32_hwtimer hwtimer_obj[sizeof(hwtimer_config) / sizeof(hwtimer_config[0])] = {0};
  128. static rt_err_t n32_hwtimer_control(rt_hwtimer_t *timer, rt_uint32_t cmd, void *args)
  129. {
  130. rt_err_t err = RT_EOK;
  131. struct n32_hwtimer_config *config;
  132. RCC_ClocksType RCC_ClockFreq;
  133. RT_ASSERT(timer != RT_NULL);
  134. config = (struct n32_hwtimer_config *)timer->parent.user_data;
  135. RCC_GetClocksFreqValue(&RCC_ClockFreq);
  136. switch (cmd)
  137. {
  138. case HWTIMER_CTRL_FREQ_SET:
  139. {
  140. uint32_t clk;
  141. uint8_t clkpre;
  142. uint32_t pre;
  143. if (config->timer_periph != TIM1 && config->timer_periph != TIM8)
  144. {
  145. clk = RCC_ClockFreq.Pclk1Freq;
  146. clkpre = GET_BITS(RCC->CFG, 8, 10);
  147. }
  148. else
  149. {
  150. clk = RCC_ClockFreq.Pclk2Freq;
  151. clkpre = GET_BITS(RCC->CFG, 11, 13);
  152. }
  153. if (clkpre >= 4)
  154. {
  155. clk = clk * 2;
  156. }
  157. pre = (clk / * ((uint32_t *)args)) - 1;
  158. TIM_ConfigPrescaler(config->timer_periph, pre, TIM_PSC_RELOAD_MODE_IMMEDIATE);
  159. config->timer_periph->EVTGEN |= TIM_EVTGEN_UDGN;
  160. }
  161. break;
  162. case HWTIMER_CTRL_STOP:
  163. TIM_Enable(config->timer_periph, DISABLE);
  164. break;
  165. default:
  166. err = -RT_ENOSYS;
  167. break;
  168. }
  169. return err;
  170. }
  171. static rt_uint32_t n32_hwtimer_count_get(rt_hwtimer_t *timer)
  172. {
  173. rt_uint32_t CurrentTimer_Count;
  174. struct n32_hwtimer_config *config;
  175. RT_ASSERT(timer != RT_NULL);
  176. config = (struct n32_hwtimer_config *)timer->parent.user_data;
  177. CurrentTimer_Count = TIM_GetCnt(config->timer_periph);
  178. return CurrentTimer_Count;
  179. }
  180. /**
  181. * @brief Configures the NVIC for TIM.
  182. */
  183. void TIM_NVIC_Config(IRQn_Type IRQn, uint8_t PreemptionPriority, uint8_t SubPriority,FunctionalState cmd)
  184. {
  185. NVIC_InitType NVIC_InitStructure;
  186. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  187. NVIC_InitStructure.NVIC_IRQChannel = IRQn;
  188. NVIC_InitStructure.NVIC_IRQChannelCmd = cmd;
  189. if (cmd)
  190. {
  191. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriority;
  192. NVIC_InitStructure.NVIC_IRQChannelSubPriority = SubPriority;
  193. }
  194. NVIC_Init(&NVIC_InitStructure);
  195. }
  196. static void n32_hwtimer_init(rt_hwtimer_t *timer, rt_uint32_t state)
  197. {
  198. struct n32_hwtimer_config *config;
  199. TIM_TimeBaseInitType TIM_TimeBaseStructure;
  200. RCC_ClocksType RCC_ClockFreq;
  201. RT_ASSERT(timer != RT_NULL);
  202. config = (struct n32_hwtimer_config *)timer->parent.user_data;
  203. if (state == 1)
  204. {
  205. uint32_t clk;
  206. uint8_t clkpre;
  207. uint32_t pre;
  208. RCC_GetClocksFreqValue(&RCC_ClockFreq);
  209. TIM_DeInit(config->timer_periph);
  210. if (config->timer_periph != TIM1 && config->timer_periph != TIM8)
  211. {
  212. clk = RCC_ClockFreq.Pclk1Freq;
  213. clkpre = GET_BITS(RCC->CFG, 8, 10);
  214. }
  215. else
  216. {
  217. clk = RCC_ClockFreq.Pclk2Freq;
  218. clkpre = GET_BITS(RCC->CFG, 11, 13);
  219. }
  220. if (clkpre >= 4)
  221. {
  222. clk = clk * 2;
  223. }
  224. pre = (clk / 10000) - 1;
  225. /* Time Base configuration */
  226. TIM_TimeBaseStructure.Prescaler = pre;
  227. TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
  228. TIM_TimeBaseStructure.Period = 10000 - 1;
  229. TIM_TimeBaseStructure.ClkDiv = TIM_CLK_DIV1;
  230. TIM_TimeBaseStructure.RepetCnt = 0;
  231. if (timer->info->cntmode == HWTIMER_CNTMODE_UP)
  232. {
  233. TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
  234. }
  235. else
  236. {
  237. TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_DOWN;
  238. }
  239. TIM_InitTimeBase(config->timer_periph, &TIM_TimeBaseStructure);
  240. /* set the TIMx priority */
  241. TIM_NVIC_Config(config->irqn, 3, 0, ENABLE);
  242. /* clear update flag */
  243. TIM_ClearFlag(config->timer_periph, TIM_FLAG_UPDATE);
  244. }
  245. else
  246. {
  247. TIM_Enable(config->timer_periph, DISABLE);
  248. TIM_ConfigInt(config->timer_periph, TIM_INT_UPDATE, ENABLE);
  249. TIM_NVIC_Config(config->irqn, 3, 0, DISABLE);
  250. }
  251. }
  252. static rt_err_t n32_hwtimer_start(rt_hwtimer_t *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode)
  253. {
  254. struct n32_hwtimer_config *config;
  255. RT_ASSERT(timer != RT_NULL);
  256. config = (struct n32_hwtimer_config *)timer->parent.user_data;
  257. /* set tim cnt */
  258. TIM_SetCnt(config->timer_periph, 0);
  259. /* set tim arr */
  260. TIM_SetAutoReload(config->timer_periph, cnt - 1);
  261. if (mode == HWTIMER_MODE_ONESHOT)
  262. {
  263. TIM_SelectOnePulseMode(config->timer_periph, TIM_OPMODE_SINGLE);
  264. }
  265. else
  266. {
  267. TIM_SelectOnePulseMode(config->timer_periph, TIM_OPMODE_REPET);
  268. }
  269. /* start timer */
  270. TIM_ConfigInt(config->timer_periph, TIM_INT_UPDATE, ENABLE);
  271. /* TIM counter enable */
  272. TIM_Enable(config->timer_periph, ENABLE);
  273. TIM_NVIC_Config(config->irqn, 3, 0, ENABLE);
  274. return RT_EOK;
  275. }
  276. static void n32_hwtimer_stop(rt_hwtimer_t *timer)
  277. {
  278. struct n32_hwtimer_config *config;
  279. RT_ASSERT(timer != RT_NULL);
  280. config = (struct n32_hwtimer_config *)timer->parent.user_data;
  281. TIM_Enable(config->timer_periph, DISABLE);
  282. TIM_NVIC_Config(config->irqn, 3, 0, DISABLE);
  283. }
  284. static const struct rt_hwtimer_ops n32_hwtimer_ops =
  285. {
  286. .init = n32_hwtimer_init,
  287. .start = n32_hwtimer_start,
  288. .stop = n32_hwtimer_stop,
  289. .count_get = n32_hwtimer_count_get,
  290. .control = n32_hwtimer_control,
  291. };
  292. static const struct rt_hwtimer_info n32_hwtimer_info =
  293. {
  294. 1000000, /* the maximum count frequency can be set */
  295. 2000, /* the minimum count frequency can be set */
  296. 0xFFFF,
  297. HWTIMER_CNTMODE_UP,
  298. };
  299. /**
  300. * @brief This function handles TIM interrupts requests.
  301. * @param htim TIM handle
  302. * @retval None
  303. */
  304. void TIM_IRQHandler(TIM_Module* timer_periph)
  305. {
  306. /* Capture compare 1 event */
  307. if (TIM_GetFlagStatus(timer_periph, TIM_FLAG_CC1) != RESET)
  308. {
  309. if (TIM_GetIntStatus(timer_periph, TIM_INT_CC1) !=RESET)
  310. {
  311. TIM_ClrIntPendingBit(timer_periph, TIM_INT_CC1);
  312. }
  313. }
  314. /* Capture compare 2 event */
  315. if (TIM_GetFlagStatus(timer_periph, TIM_FLAG_CC2) != RESET)
  316. {
  317. if (TIM_GetIntStatus(timer_periph, TIM_INT_CC2) !=RESET)
  318. {
  319. TIM_ClrIntPendingBit(timer_periph, TIM_INT_CC2);
  320. }
  321. }
  322. /* Capture compare 3 event */
  323. if (TIM_GetFlagStatus(timer_periph, TIM_FLAG_CC3) != RESET)
  324. {
  325. if (TIM_GetIntStatus(timer_periph, TIM_INT_CC3) !=RESET)
  326. {
  327. TIM_ClrIntPendingBit(timer_periph, TIM_INT_CC3);
  328. }
  329. }
  330. /* Capture compare 4 event */
  331. if (TIM_GetFlagStatus(timer_periph, TIM_FLAG_CC4) != RESET)
  332. {
  333. if (TIM_GetIntStatus(timer_periph, TIM_INT_CC4) !=RESET)
  334. {
  335. TIM_ClrIntPendingBit(timer_periph, TIM_INT_CC4);
  336. }
  337. }
  338. /* TIM Update event */
  339. if (TIM_GetFlagStatus(timer_periph, TIM_FLAG_UPDATE) != RESET)
  340. {
  341. if (TIM_GetIntStatus(timer_periph, TIM_INT_UPDATE) !=RESET)
  342. {
  343. TIM_ClrIntPendingBit(timer_periph, TIM_INT_UPDATE);
  344. }
  345. }
  346. /* TIM Break input event */
  347. if (TIM_GetFlagStatus(timer_periph, TIM_FLAG_BREAK) != RESET)
  348. {
  349. if (TIM_GetIntStatus(timer_periph, TIM_INT_BREAK) !=RESET)
  350. {
  351. TIM_ClrIntPendingBit(timer_periph, TIM_INT_BREAK);
  352. }
  353. }
  354. /* TIM Trigger detection event */
  355. if (TIM_GetFlagStatus(timer_periph, TIM_FLAG_TRIG) != RESET)
  356. {
  357. if (TIM_GetIntStatus(timer_periph, TIM_INT_TRIG) !=RESET)
  358. {
  359. TIM_ClrIntPendingBit(timer_periph, TIM_INT_TRIG);
  360. }
  361. }
  362. /* TIM commutation event */
  363. if (TIM_GetFlagStatus(timer_periph, TIM_FLAG_COM) != RESET)
  364. {
  365. if (TIM_GetIntStatus(timer_periph, TIM_INT_COM) !=RESET)
  366. {
  367. TIM_ClrIntPendingBit(timer_periph, TIM_INT_COM);
  368. }
  369. }
  370. }
  371. #ifdef BSP_USING_HWTIMER1
  372. void TIM1_UP_IRQHandler(void)
  373. {
  374. /* enter interrupt */
  375. rt_interrupt_enter();
  376. TIM_ClrIntPendingBit(hwtimer_obj[tim1_count].config->timer_periph, TIM_INT_UPDATE);
  377. rt_device_hwtimer_isr(&hwtimer_obj[tim1_count].time_device);
  378. /* leave interrupt */
  379. rt_interrupt_leave();
  380. }
  381. #endif
  382. #ifdef BSP_USING_HWTIMER2
  383. void TIM2_IRQHandler(void)
  384. {
  385. /* enter interrupt */
  386. rt_interrupt_enter();
  387. TIM_ClrIntPendingBit(hwtimer_obj[tim2_count].config->timer_periph, TIM_INT_UPDATE);
  388. rt_device_hwtimer_isr(&hwtimer_obj[tim2_count].time_device);
  389. /* leave interrupt */
  390. rt_interrupt_leave();
  391. }
  392. #endif
  393. #ifdef BSP_USING_HWTIMER3
  394. void TIM3_IRQHandler(void)
  395. {
  396. /* enter interrupt */
  397. rt_interrupt_enter();
  398. TIM_ClrIntPendingBit(hwtimer_obj[tim3_count].config->timer_periph, TIM_INT_UPDATE);
  399. rt_device_hwtimer_isr(&hwtimer_obj[tim3_count].time_device);
  400. /* leave interrupt */
  401. rt_interrupt_leave();
  402. }
  403. #endif
  404. #ifdef BSP_USING_HWTIMER4
  405. void TIM4_IRQHandler(void)
  406. {
  407. /* enter interrupt */
  408. rt_interrupt_enter();
  409. TIM_ClrIntPendingBit(hwtimer_obj[tim4_count].config->timer_periph, TIM_INT_UPDATE);
  410. rt_device_hwtimer_isr(&hwtimer_obj[tim4_count].time_device);
  411. /* leave interrupt */
  412. rt_interrupt_leave();
  413. }
  414. #endif
  415. #ifdef BSP_USING_HWTIMER5
  416. void TIM5_IRQHandler(void)
  417. {
  418. /* enter interrupt */
  419. rt_interrupt_enter();
  420. TIM_ClrIntPendingBit(hwtimer_obj[tim5_count].config->timer_periph, TIM_INT_UPDATE);
  421. rt_device_hwtimer_isr(&hwtimer_obj[tim5_count].time_device);
  422. /* leave interrupt */
  423. rt_interrupt_leave();
  424. }
  425. #endif
  426. #ifdef BSP_USING_HWTIMER6
  427. void TIM6_IRQHandler(void)
  428. {
  429. /* enter interrupt */
  430. rt_interrupt_enter();
  431. TIM_ClrIntPendingBit(hwtimer_obj[tim6_count].config->timer_periph, TIM_INT_UPDATE);
  432. rt_device_hwtimer_isr(&hwtimer_obj[tim6_count].time_device);
  433. /* leave interrupt */
  434. rt_interrupt_leave();
  435. }
  436. #endif
  437. #ifdef BSP_USING_HWTIMER7
  438. void TIM7_IRQHandler(void)
  439. {
  440. /* enter interrupt */
  441. rt_interrupt_enter();
  442. TIM_ClrIntPendingBit(hwtimer_obj[tim7_count].config->timer_periph, TIM_INT_UPDATE);
  443. rt_device_hwtimer_isr(&hwtimer_obj[tim7_count].time_device);
  444. /* leave interrupt */
  445. rt_interrupt_leave();
  446. }
  447. #endif
  448. #ifdef BSP_USING_HWTIMER8
  449. void TIM8_UP_IRQHandler(void)
  450. {
  451. /* enter interrupt */
  452. rt_interrupt_enter();
  453. TIM_ClrIntPendingBit(hwtimer_obj[tim8_count].config->timer_periph, TIM_INT_UPDATE);
  454. rt_device_hwtimer_isr(&hwtimer_obj[tim8_count].time_device);
  455. /* leave interrupt */
  456. rt_interrupt_leave();
  457. }
  458. #endif
  459. #ifdef BSP_USING_HWTIMER9
  460. void TIM9_IRQHandler(void)
  461. {
  462. /* enter interrupt */
  463. rt_interrupt_enter();
  464. TIM_ClrIntPendingBit(hwtimer_obj[tim9_count].config->timer_periph, TIM_INT_UPDATE);
  465. rt_device_hwtimer_isr(&hwtimer_obj[tim9_count].time_device);
  466. /* leave interrupt */
  467. rt_interrupt_leave();
  468. }
  469. #endif
  470. int rt_hwtimer_init(void)
  471. {
  472. int i = 0;
  473. int result = RT_EOK;
  474. #ifdef BSP_USING_HWTIMER1
  475. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);
  476. #endif
  477. #ifdef BSP_USING_HWTIMER2
  478. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2, ENABLE);
  479. #endif
  480. #ifdef BSP_USING_HWTIMER3
  481. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM3, ENABLE);
  482. #endif
  483. #ifdef BSP_USING_HWTIMER4
  484. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM4, ENABLE);
  485. #endif
  486. #ifdef BSP_USING_HWTIMER5
  487. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM5, ENABLE);
  488. #endif
  489. #ifdef BSP_USING_HWTIMER6
  490. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM6, ENABLE);
  491. #endif
  492. #ifdef BSP_USING_HWTIMER7
  493. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM7, ENABLE);
  494. #endif
  495. #ifdef BSP_USING_HWTIMER8
  496. RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM8, ENABLE);
  497. #endif
  498. #ifdef BSP_USING_HWTIMER9
  499. RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM9, ENABLE);
  500. #endif
  501. caculate_tim_count();
  502. for (i = 0; i < sizeof(hwtimer_obj) / sizeof(hwtimer_obj[0]); i++)
  503. {
  504. hwtimer_obj[i].time_device.info = &n32_hwtimer_info;
  505. hwtimer_obj[i].time_device.ops = &n32_hwtimer_ops;
  506. hwtimer_obj[i].config = &hwtimer_config[i];
  507. rt_device_hwtimer_register(&hwtimer_obj[i].time_device, \
  508. hwtimer_obj[i].config->name, hwtimer_obj[i].config);
  509. }
  510. return result;
  511. }
  512. INIT_DEVICE_EXPORT(rt_hwtimer_init);
  513. #endif /* defined(BSP_USING_HWTIMERx) */
  514. #endif /* RT_USING_HWTIMER */