drv_hwtimer.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. };
  43. struct n32_hwtimer
  44. {
  45. rt_hwtimer_t time_device;
  46. TIM_Module *tim_handle;
  47. IRQn_Type tim_irqn;
  48. char *name;
  49. };
  50. static struct n32_hwtimer n32_hwtimer_obj[] =
  51. {
  52. #ifdef BSP_USING_HWTIM1
  53. TIM1_CONFIG,
  54. #endif
  55. #ifdef BSP_USING_HWTIM2
  56. TIM2_CONFIG,
  57. #endif
  58. #ifdef BSP_USING_HWTIM3
  59. TIM3_CONFIG,
  60. #endif
  61. #ifdef BSP_USING_HWTIM4
  62. TIM4_CONFIG,
  63. #endif
  64. #ifdef BSP_USING_HWTIM5
  65. TIM5_CONFIG,
  66. #endif
  67. #ifdef BSP_USING_HWTIM6
  68. TIM6_CONFIG,
  69. #endif
  70. #ifdef BSP_USING_HWTIM7
  71. TIM7_CONFIG,
  72. #endif
  73. #ifdef BSP_USING_HWTIM8
  74. TIM8_CONFIG,
  75. #endif
  76. };
  77. static void n32_timer_init(struct rt_hwtimer_device *timer, rt_uint32_t state)
  78. {
  79. RCC_ClocksType RCC_ClockStruct;
  80. TIM_TimeBaseInitType TIM_TimeBaseStructure;
  81. NVIC_InitType NVIC_InitStructure;
  82. uint32_t freq = 0;
  83. uint32_t input_clock;
  84. uint32_t prescaler_value = 0;
  85. TIM_Module *tim = RT_NULL;
  86. struct n32_hwtimer *tim_device = RT_NULL;
  87. RT_ASSERT(timer != RT_NULL);
  88. if (state)
  89. {
  90. tim = (TIM_Module *)timer->parent.user_data;
  91. tim_device = (struct n32_hwtimer *)timer;
  92. RT_ASSERT((tim == TIM2) || (tim == TIM3) || (tim == TIM4) || (tim == TIM5)
  93. || (tim == TIM6) || (tim == TIM7));
  94. /* timer clock enable */
  95. n32_msp_hwtim_init(tim);
  96. freq = timer->freq;
  97. RCC_GetClocksFreqValue(&RCC_ClockStruct);
  98. if (1 == (RCC_ClockStruct.HclkFreq / RCC_ClockStruct.Pclk1Freq))
  99. input_clock = RCC_ClockStruct.Pclk1Freq;
  100. else
  101. input_clock = RCC_ClockStruct.Pclk1Freq * 2;
  102. prescaler_value = (uint32_t)(input_clock / freq) - 1;
  103. TIM_TimeBaseStructure.Period = freq - 1;
  104. TIM_TimeBaseStructure.Prescaler = prescaler_value;
  105. TIM_TimeBaseStructure.ClkDiv = TIM_CLK_DIV1;
  106. TIM_TimeBaseStructure.RepetCnt = 0;
  107. if (timer->info->cntmode == HWTIMER_CNTMODE_UP)
  108. {
  109. TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
  110. }
  111. else
  112. {
  113. TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_DOWN;
  114. }
  115. TIM_InitTimeBase(tim, &TIM_TimeBaseStructure);
  116. /* Enable the TIMx global Interrupt */
  117. NVIC_InitStructure.NVIC_IRQChannel = tim_device->tim_irqn;
  118. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  119. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  120. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  121. NVIC_Init(&NVIC_InitStructure);
  122. TIM_ConfigInt(tim, TIM_INT_UPDATE, ENABLE);
  123. TIM_ClrIntPendingBit(tim, TIM_INT_UPDATE);
  124. LOG_D("%s init success", tim_device->name);
  125. }
  126. }
  127. static rt_err_t n32_timer_start(rt_hwtimer_t *timer, rt_uint32_t t, rt_hwtimer_mode_t opmode)
  128. {
  129. rt_err_t result = RT_EOK;
  130. TIM_Module *tim = RT_NULL;
  131. RT_ASSERT(timer != RT_NULL);
  132. tim = (TIM_Module *)timer->parent.user_data;
  133. /* set tim cnt */
  134. TIM_SetCnt(tim, 0);
  135. /* set tim arr */
  136. TIM_SetAutoReload(tim, t - 1);
  137. if (opmode == HWTIMER_MODE_ONESHOT)
  138. {
  139. /* set timer to single mode */
  140. TIM_SelectOnePulseMode(tim, TIM_OPMODE_SINGLE);
  141. }
  142. else
  143. {
  144. TIM_SelectOnePulseMode(tim, TIM_OPMODE_REPET);
  145. }
  146. /* start timer */
  147. TIM_Enable(tim, ENABLE);
  148. return result;
  149. }
  150. static void n32_timer_stop(rt_hwtimer_t *timer)
  151. {
  152. TIM_Module *tim = RT_NULL;
  153. RT_ASSERT(timer != RT_NULL);
  154. tim = (TIM_Module *)timer->parent.user_data;
  155. /* stop timer */
  156. TIM_Enable(tim, DISABLE);
  157. /* set tim cnt */
  158. TIM_SetCnt(tim, 0);
  159. }
  160. static rt_uint32_t n32_timer_counter_get(rt_hwtimer_t *timer)
  161. {
  162. TIM_Module *tim = RT_NULL;
  163. RT_ASSERT(timer != RT_NULL);
  164. tim = (TIM_Module *)timer->parent.user_data;
  165. return tim->CNT;
  166. }
  167. static rt_err_t n32_timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
  168. {
  169. RCC_ClocksType RCC_ClockStruct;
  170. TIM_Module *tim = RT_NULL;
  171. rt_err_t result = RT_EOK;
  172. RT_ASSERT(timer != RT_NULL);
  173. RT_ASSERT(arg != RT_NULL);
  174. tim = (TIM_Module *)timer->parent.user_data;
  175. switch (cmd)
  176. {
  177. case HWTIMER_CTRL_FREQ_SET:
  178. {
  179. rt_uint32_t input_clock;
  180. rt_uint32_t freq;
  181. rt_uint16_t val;
  182. /* set timer frequence */
  183. freq = *((rt_uint32_t *)arg);
  184. /* time init */
  185. RCC_GetClocksFreqValue(&RCC_ClockStruct);
  186. if (1 == (RCC_ClockStruct.HclkFreq / RCC_ClockStruct.Pclk1Freq))
  187. input_clock = RCC_ClockStruct.Pclk1Freq;
  188. else
  189. input_clock = RCC_ClockStruct.Pclk1Freq * 2;
  190. val = input_clock / freq;
  191. TIM_ConfigPrescaler(tim, val - 1, TIM_PSC_RELOAD_MODE_IMMEDIATE);
  192. }
  193. break;
  194. default:
  195. {
  196. result = -RT_ENOSYS;
  197. }
  198. break;
  199. }
  200. return result;
  201. }
  202. static const struct rt_hwtimer_info _info = TIM_DEV_INFO_CONFIG;
  203. static const struct rt_hwtimer_ops _ops =
  204. {
  205. .init = n32_timer_init,
  206. .start = n32_timer_start,
  207. .stop = n32_timer_stop,
  208. .count_get = n32_timer_counter_get,
  209. .control = n32_timer_ctrl,
  210. };
  211. #ifdef BSP_USING_HWTIM2
  212. void TIM2_IRQHandler(void)
  213. {
  214. /* enter interrupt */
  215. rt_interrupt_enter();
  216. if (TIM_GetIntStatus(TIM2, TIM_INT_UPDATE) == SET)
  217. {
  218. rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM2_INDEX].time_device);
  219. TIM_ClrIntPendingBit(TIM2, TIM_INT_UPDATE);
  220. }
  221. /* leave interrupt */
  222. rt_interrupt_leave();
  223. }
  224. #endif
  225. #ifdef BSP_USING_HWTIM3
  226. void TIM3_IRQHandler(void)
  227. {
  228. /* enter interrupt */
  229. rt_interrupt_enter();
  230. if (TIM_GetIntStatus(TIM3, TIM_INT_UPDATE) == SET)
  231. {
  232. rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM3_INDEX].time_device);
  233. TIM_ClrIntPendingBit(TIM3, TIM_INT_UPDATE);
  234. }
  235. /* leave interrupt */
  236. rt_interrupt_leave();
  237. }
  238. #endif
  239. #ifdef BSP_USING_HWTIM4
  240. void TIM4_IRQHandler(void)
  241. {
  242. /* enter interrupt */
  243. rt_interrupt_enter();
  244. if (TIM_GetIntStatus(TIM4, TIM_INT_UPDATE) == SET)
  245. {
  246. rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM4_INDEX].time_device);
  247. TIM_ClrIntPendingBit(TIM4, TIM_INT_UPDATE);
  248. }
  249. /* leave interrupt */
  250. rt_interrupt_leave();
  251. }
  252. #endif
  253. #ifdef BSP_USING_HWTIM5
  254. void TIM5_IRQHandler(void)
  255. {
  256. /* enter interrupt */
  257. rt_interrupt_enter();
  258. if (TIM_GetIntStatus(TIM5, TIM_INT_UPDATE) == SET)
  259. {
  260. rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM5_INDEX].time_device);
  261. TIM_ClrIntPendingBit(TIM5, TIM_INT_UPDATE);
  262. }
  263. /* leave interrupt */
  264. rt_interrupt_leave();
  265. }
  266. #endif
  267. #ifdef BSP_USING_HWTIM6
  268. void TIM6_IRQHandler(void)
  269. {
  270. /* enter interrupt */
  271. rt_interrupt_enter();
  272. if (TIM_GetIntStatus(TIM6, TIM_INT_UPDATE) == SET)
  273. {
  274. rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM6_INDEX].time_device);
  275. TIM_ClrIntPendingBit(TIM6, TIM_INT_UPDATE);
  276. }
  277. /* leave interrupt */
  278. rt_interrupt_leave();
  279. }
  280. #endif
  281. #ifdef BSP_USING_HWTIM7
  282. void TIM7_IRQHandler(void)
  283. {
  284. /* enter interrupt */
  285. rt_interrupt_enter();
  286. if (TIM_GetIntStatus(TIM7, TIM_INT_UPDATE) == SET)
  287. {
  288. rt_device_hwtimer_isr(&n32_hwtimer_obj[TIM7_INDEX].time_device);
  289. TIM_ClrIntPendingBit(TIM7, TIM_INT_UPDATE);
  290. }
  291. /* leave interrupt */
  292. rt_interrupt_leave();
  293. }
  294. #endif
  295. static int rt_hw_hwtimer_init(void)
  296. {
  297. int i = 0;
  298. int result = RT_EOK;
  299. for (i = 0; i < sizeof(n32_hwtimer_obj) / sizeof(n32_hwtimer_obj[0]); i++)
  300. {
  301. n32_hwtimer_obj[i].time_device.info = &_info;
  302. n32_hwtimer_obj[i].time_device.ops = &_ops;
  303. if (rt_device_hwtimer_register(&n32_hwtimer_obj[i].time_device, n32_hwtimer_obj[i].name, n32_hwtimer_obj[i].tim_handle) == RT_EOK)
  304. {
  305. LOG_D("%s register success", n32_hwtimer_obj[i].name);
  306. }
  307. else
  308. {
  309. LOG_E("%s register failed", n32_hwtimer_obj[i].name);
  310. result = -RT_ERROR;
  311. }
  312. }
  313. return result;
  314. }
  315. INIT_BOARD_EXPORT(rt_hw_hwtimer_init);
  316. #endif /* BSP_USING_HWTIMER */