drv_hwtimer.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-20 breo.com first version
  9. */
  10. #include <board.h>
  11. #include "drv_hwtimer.h"
  12. #define DRV_DEBUG
  13. #define LOG_TAG "drv.hwtimer"
  14. #include <drv_log.h>
  15. #ifdef BSP_USING_HWTIMER
  16. enum
  17. {
  18. #ifdef BSP_USING_HWTIM1
  19. TIM1_INDEX,
  20. #endif
  21. #ifdef BSP_USING_HWTIM2
  22. TIM2_INDEX,
  23. #endif
  24. #ifdef BSP_USING_HWTIM3
  25. TIM3_INDEX,
  26. #endif
  27. #ifdef BSP_USING_HWTIM4
  28. TIM4_INDEX,
  29. #endif
  30. #ifdef BSP_USING_HWTIM5
  31. TIM5_INDEX,
  32. #endif
  33. #ifdef BSP_USING_HWTIM6
  34. TIM6_INDEX,
  35. #endif
  36. #ifdef BSP_USING_HWTIM7
  37. TIM7_INDEX,
  38. #endif
  39. #ifdef BSP_USING_HW_TIM8
  40. TIM8_INDEX,
  41. #endif
  42. #ifdef BSP_USING_HWTIM9
  43. TIM9_INDEX,
  44. #endif
  45. #ifdef BSP_USING_HWTIM10
  46. TIM10_INDEX,
  47. #endif
  48. #ifdef BSP_USING_HWTIM11
  49. TIM11_INDEX,
  50. #endif
  51. #ifdef BSP_USING_HWTIM12
  52. TIM12_INDEX,
  53. #endif
  54. #ifdef BSP_USING_HWTIM13
  55. TIM13_INDEX,
  56. #endif
  57. #ifdef BSP_USING_HWTIM14
  58. TIM14_INDEX,
  59. #endif
  60. #ifdef BSP_USING_HWTIM15
  61. TIM15_INDEX,
  62. #endif
  63. };
  64. struct n32_hwtimer
  65. {
  66. rt_hwtimer_t time_device;
  67. TIM_Module *tim_handle;
  68. IRQn_Type tim_irqn;
  69. char *name;
  70. };
  71. static struct n32_hwtimer n32_hwtimer_obj[] =
  72. {
  73. #ifdef BSP_USING_HWTIM1
  74. TIM1_CONFIG,
  75. #endif
  76. #ifdef BSP_USING_HWTIM2
  77. TIM2_CONFIG,
  78. #endif
  79. #ifdef BSP_USING_HWTIM3
  80. TIM3_CONFIG,
  81. #endif
  82. #ifdef BSP_USING_HWTIM4
  83. TIM4_CONFIG,
  84. #endif
  85. #ifdef BSP_USING_HWTIM5
  86. TIM5_CONFIG,
  87. #endif
  88. #ifdef BSP_USING_HWTIM6
  89. TIM6_CONFIG,
  90. #endif
  91. #ifdef BSP_USING_HWTIM7
  92. TIM7_CONFIG,
  93. #endif
  94. #ifdef BSP_USING_HWTIM8
  95. TIM8_CONFIG,
  96. #endif
  97. #ifdef BSP_USING_HWTIM9
  98. TIM9_CONFIG,
  99. #endif
  100. #ifdef BSP_USING_HWTIM10
  101. TIM10_CONFIG,
  102. #endif
  103. #ifdef BSP_USING_HWTIM11
  104. TIM11_CONFIG,
  105. #endif
  106. #ifdef BSP_USING_HWTIM12
  107. TIM12_CONFIG,
  108. #endif
  109. #ifdef BSP_USING_HWTIM13
  110. TIM13_CONFIG,
  111. #endif
  112. #ifdef BSP_USING_HWTIM14
  113. TIM14_CONFIG,
  114. #endif
  115. #ifdef BSP_USING_HWTIM15
  116. TIM15_CONFIG,
  117. #endif
  118. };
  119. static void n32_timer_init(struct rt_hwtimer_device *timer, rt_uint32_t state)
  120. {
  121. RCC_ClocksType RCC_ClockStruct;
  122. TIM_TimeBaseInitType TIM_TimeBaseStructure;
  123. NVIC_InitType NVIC_InitStructure;
  124. uint32_t prescaler_value = 0;
  125. TIM_Module *tim = RT_NULL;
  126. struct n32_hwtimer *tim_device = RT_NULL;
  127. RT_ASSERT(timer != RT_NULL);
  128. if (state)
  129. {
  130. tim = (TIM_Module *)timer->parent.user_data;
  131. tim_device = (struct n32_hwtimer *)timer;
  132. /* timer clock enable */
  133. n32_msp_hwtim_init(tim);
  134. /* timer init */
  135. RCC_GetClocksFreqValue(&RCC_ClockStruct);
  136. /* Set timer clock is 1Mhz */
  137. prescaler_value = (uint32_t)(RCC_ClockStruct.SysclkFreq / 10000) - 1;
  138. TIM_TimeBaseStructure.Period = 10000 - 1;
  139. rt_kprintf("Period=[%d]", TIM_TimeBaseStructure.Period);
  140. TIM_TimeBaseStructure.Prescaler = prescaler_value;
  141. rt_kprintf("Prescaler=[%d]", TIM_TimeBaseStructure.Prescaler);
  142. TIM_TimeBaseStructure.ClkDiv = TIM_CLK_DIV1;
  143. TIM_TimeBaseStructure.RepetCnt = 0;
  144. if (timer->info->cntmode == HWTIMER_CNTMODE_UP)
  145. {
  146. TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
  147. }
  148. else
  149. {
  150. TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_DOWN;
  151. }
  152. TIM_InitTimeBase(tim, &TIM_TimeBaseStructure);
  153. /* Enable the TIMx global Interrupt */
  154. NVIC_InitStructure.NVIC_IRQChannel = tim_device->tim_irqn;
  155. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  156. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  157. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  158. NVIC_Init(&NVIC_InitStructure);
  159. TIM_ConfigInt(tim, TIM_INT_UPDATE, ENABLE);
  160. TIM_ClrIntPendingBit(tim, TIM_INT_UPDATE);
  161. LOG_D("%s init success", tim_device->name);
  162. }
  163. }
  164. static rt_err_t n32_timer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode)
  165. {
  166. rt_err_t result = RT_EOK;
  167. TIM_Module *tim = RT_NULL;
  168. RT_ASSERT(timer != RT_NULL);
  169. tim = (TIM_Module *)timer->parent.user_data;
  170. /* set tim cnt */
  171. TIM_SetCnt(tim, 0);
  172. /* set tim arr */
  173. TIM_SetAutoReload(tim, t - 1);
  174. if (opmode == HWTIMER_MODE_ONESHOT)
  175. {
  176. /* set timer to single mode */
  177. TIM_SelectOnePulseMode(tim, TIM_OPMODE_SINGLE);
  178. }
  179. else
  180. {
  181. TIM_SelectOnePulseMode(tim, TIM_OPMODE_REPET);
  182. }
  183. /* start timer */
  184. TIM_Enable(tim, ENABLE);
  185. return result;
  186. }
  187. static void n32_timer_stop(rt_hwtimer_t *timer)
  188. {
  189. TIM_Module *tim = RT_NULL;
  190. RT_ASSERT(timer != RT_NULL);
  191. tim = (TIM_Module *)timer->parent.user_data;
  192. /* stop timer */
  193. TIM_Enable(tim, DISABLE);
  194. /* set tim cnt */
  195. TIM_SetCnt(tim, 0);
  196. }
  197. static rt_uint32_t n32_timer_counter_get(rt_hwtimer_t *timer)
  198. {
  199. TIM_Module *tim = RT_NULL;
  200. RT_ASSERT(timer != RT_NULL);
  201. tim = (TIM_Module *)timer->parent.user_data;
  202. return tim->CNT;
  203. }
  204. static rt_err_t n32_timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
  205. {
  206. RCC_ClocksType RCC_ClockStruct;
  207. TIM_Module *tim = RT_NULL;
  208. rt_err_t result = RT_EOK;
  209. RT_ASSERT(timer != RT_NULL);
  210. RT_ASSERT(arg != RT_NULL);
  211. tim = (TIM_Module *)timer->parent.user_data;
  212. switch (cmd)
  213. {
  214. case HWTIMER_CTRL_FREQ_SET:
  215. {
  216. rt_uint32_t freq;
  217. rt_uint16_t val;
  218. /* set timer frequence */
  219. freq = *((rt_uint32_t *)arg);
  220. /* time init */
  221. RCC_GetClocksFreqValue(&RCC_ClockStruct);
  222. val = RCC_ClockStruct.SysclkFreq / freq;
  223. TIM_ConfigPrescaler(tim, val - 1, TIM_PSC_RELOAD_MODE_IMMEDIATE);
  224. }
  225. break;
  226. default:
  227. {
  228. result = -RT_ENOSYS;
  229. }
  230. break;
  231. }
  232. return result;
  233. }
  234. static const struct rt_hwtimer_info _info = TIM_DEV_INFO_CONFIG;
  235. static const struct rt_hwtimer_ops _ops =
  236. {
  237. .init = n32_timer_init,
  238. .start = n32_timer_start,
  239. .stop = n32_timer_stop,
  240. .count_get = n32_timer_counter_get,
  241. .control = n32_timer_ctrl,
  242. };
  243. #ifdef BSP_USING_HWTIM2
  244. void TIM2_IRQHandler(void)
  245. {
  246. /* enter interrupt */
  247. rt_interrupt_enter();
  248. if (TIM_GetIntStatus(TIM2, TIM_INT_UPDATE) == SET)
  249. {
  250. rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM2_INDEX].time_device);
  251. TIM_ClrIntPendingBit(TIM2, TIM_INT_UPDATE);
  252. }
  253. /* leave interrupt */
  254. rt_interrupt_leave();
  255. }
  256. #endif
  257. #ifdef BSP_USING_HWTIM3
  258. void TIM3_IRQHandler(void)
  259. {
  260. /* enter interrupt */
  261. rt_interrupt_enter();
  262. if (TIM_GetIntStatus(TIM3, TIM_INT_UPDATE) == SET)
  263. {
  264. rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM3_INDEX].time_device);
  265. TIM_ClrIntPendingBit(TIM3, TIM_INT_UPDATE);
  266. }
  267. /* leave interrupt */
  268. rt_interrupt_leave();
  269. }
  270. #endif
  271. #ifdef BSP_USING_HWTIM4
  272. void TIM4_IRQHandler(void)
  273. {
  274. /* enter interrupt */
  275. rt_interrupt_enter();
  276. if (TIM_GetIntStatus(TIM4, TIM_INT_UPDATE) == SET)
  277. {
  278. rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM4_INDEX].time_device);
  279. TIM_ClrIntPendingBit(TIM4, TIM_INT_UPDATE);
  280. }
  281. /* leave interrupt */
  282. rt_interrupt_leave();
  283. }
  284. #endif
  285. #ifdef BSP_USING_HWTIM5
  286. void TIM5_IRQHandler(void)
  287. {
  288. /* enter interrupt */
  289. rt_interrupt_enter();
  290. if (TIM_GetIntStatus(TIM5, TIM_INT_UPDATE) == SET)
  291. {
  292. rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM5_INDEX].time_device);
  293. TIM_ClrIntPendingBit(TIM5, TIM_INT_UPDATE);
  294. }
  295. /* leave interrupt */
  296. rt_interrupt_leave();
  297. }
  298. #endif
  299. #ifdef BSP_USING_HWTIM6
  300. void TIM6_IRQHandler(void)
  301. {
  302. /* enter interrupt */
  303. rt_interrupt_enter();
  304. if (TIM_GetIntStatus(TIM6, TIM_INT_UPDATE) == SET)
  305. {
  306. rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM6_INDEX].time_device);
  307. TIM_ClrIntPendingBit(TIM6, TIM_INT_UPDATE);
  308. }
  309. /* leave interrupt */
  310. rt_interrupt_leave();
  311. }
  312. #endif
  313. #ifdef BSP_USING_HWTIM7
  314. void TIM7_IRQHandler(void)
  315. {
  316. /* enter interrupt */
  317. rt_interrupt_enter();
  318. if (TIM_GetIntStatus(TIM7, TIM_INT_UPDATE) == SET)
  319. {
  320. rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM7_INDEX].time_device);
  321. TIM_ClrIntPendingBit(TIM7, TIM_INT_UPDATE);
  322. }
  323. /* leave interrupt */
  324. rt_interrupt_leave();
  325. }
  326. #endif
  327. static int rt_hw_hwtimer_init(void)
  328. {
  329. int i = 0;
  330. int result = RT_EOK;
  331. for (i = 0; i < sizeof(n32_hwtimer_obj) / sizeof(n32_hwtimer_obj[0]); i++)
  332. {
  333. n32_hwtimer_obj[i].time_device.info = &_info;
  334. n32_hwtimer_obj[i].time_device.ops = &_ops;
  335. if (rt_device_hwtimer_register(&n32_hwtimer_obj[i].time_device, n32_hwtimer_obj[i].name, n32_hwtimer_obj[i].tim_handle) == RT_EOK)
  336. {
  337. LOG_D("%s register success", n32_hwtimer_obj[i].name);
  338. }
  339. else
  340. {
  341. LOG_E("%s register failed", n32_hwtimer_obj[i].name);
  342. result = -RT_ERROR;
  343. }
  344. }
  345. return result;
  346. }
  347. INIT_BOARD_EXPORT(rt_hw_hwtimer_init);
  348. #endif /* BSP_USING_HWTIMER */