drv_hwtimer.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-09-04 Rbb666 first version
  9. */
  10. #include "board.h"
  11. #include "drv_hwtimer.h"
  12. //#define DRV_DEBUG
  13. #define LOG_TAG "drv.timer"
  14. #include <rtdbg.h>
  15. #ifdef RT_USING_HWTIMER
  16. static struct ra_hwtimer ra_hwtimer_obj[BSP_TIMERS_NUM] =
  17. {
  18. #ifdef BSP_USING_TIM0
  19. [BSP_TIMER0_INDEX] = TIMER_DRV_INITIALIZER(0),
  20. #endif
  21. #ifdef BSP_USING_TIM1
  22. [BSP_TIMER1_INDEX] = TIMER_DRV_INITIALIZER(1),
  23. #endif
  24. };
  25. const rt_uint32_t PLCKD_FREQ_PRESCALER[PLCKD_PRESCALER_MAX_SELECT] =
  26. {
  27. #if defined(SOC_SERIES_R7FA6M3)
  28. PLCKD_PRESCALER_120M,
  29. PLCKD_PRESCALER_60M,
  30. PLCKD_PRESCALER_30M,
  31. PLCKD_PRESCALER_15M,
  32. PLCKD_PRESCALER_7_5M,
  33. PLCKD_PRESCALER_3_75M,
  34. PLCKD_PRESCALER_1_875M,
  35. #elif defined(SOC_SERIES_R9A07G0)
  36. PLCKD_PRESCALER_400M,
  37. PLCKD_PRESCALER_200M,
  38. PLCKD_PRESCALER_100M,
  39. PLCKD_PRESCALER_50M,
  40. PLCKD_PRESCALER_25M,
  41. PLCKD_PRESCALER_12_5M,
  42. PLCKD_PRESCALER_6_25M,
  43. PLCKD_PRESCALER_3_125M,
  44. PLCKD_PRESCALER_1_5625M
  45. #elif defined(SOC_SERIES_R7KA8P1)
  46. PLCKD_PRESCALER_250M,
  47. PLCKD_PRESCALER_200M,
  48. PLCKD_PRESCALER_100M,
  49. PLCKD_PRESCALER_50M,
  50. PLCKD_PRESCALER_25M,
  51. PLCKD_PRESCALER_12_5M,
  52. PLCKD_PRESCALER_6_25M,
  53. PLCKD_PRESCALER_3_125M,
  54. PLCKD_PRESCALER_1_5625M
  55. #endif
  56. };
  57. static void timer_init(struct rt_hwtimer_device *timer, rt_uint32_t state)
  58. {
  59. RT_ASSERT(timer != RT_NULL);
  60. struct ra_hwtimer *tim;
  61. tim = (struct ra_hwtimer *)timer->parent.user_data;
  62. if (state)
  63. {
  64. fsp_err_t fsp_err = FSP_SUCCESS;
  65. fsp_err = R_GPT_Open(tim->g_ctrl, tim->g_cfg);
  66. if (fsp_err != FSP_SUCCESS)
  67. {
  68. LOG_E("%s init fail", tim->name);
  69. }
  70. }
  71. }
  72. static rt_err_t timer_start(rt_hwtimer_t *timer, rt_uint32_t pr, rt_hwtimer_mode_t opmode)
  73. {
  74. RT_ASSERT(timer != RT_NULL);
  75. RT_ASSERT(opmode != RT_NULL);
  76. struct ra_hwtimer *tim;
  77. tim = (struct ra_hwtimer *)timer->parent.user_data;
  78. fsp_err_t err = FSP_SUCCESS;
  79. /* set timer count */
  80. R_GPT_CounterSet(tim->g_ctrl, 0);
  81. /* set timer period register */
  82. err = R_GPT_PeriodSet(tim->g_ctrl, pr);
  83. if (err != FSP_SUCCESS)
  84. {
  85. return -RT_ERROR;
  86. }
  87. /* set timer to one cycle mode */
  88. err = R_GPT_Start(tim->g_ctrl);
  89. return (err == FSP_SUCCESS) ? RT_EOK : -RT_ERROR;
  90. }
  91. static void timer_stop(rt_hwtimer_t *timer)
  92. {
  93. struct ra_hwtimer *tim = RT_NULL;
  94. RT_ASSERT(timer != RT_NULL);
  95. tim = (struct ra_hwtimer *)timer->parent.user_data;
  96. /* stop timer */
  97. R_GPT_Stop(tim->g_ctrl);
  98. /* set timer count */
  99. R_GPT_CounterSet(tim->g_ctrl, 0);
  100. }
  101. static rt_uint32_t timer_counter_get(rt_hwtimer_t *timer)
  102. {
  103. struct ra_hwtimer *tim = RT_NULL;
  104. RT_ASSERT(timer != RT_NULL);
  105. tim = (struct ra_hwtimer *)timer->parent.user_data;
  106. #if defined(SOC_SERIES_R7KA8P1)
  107. timer_info_t info;
  108. if (R_GPT_InfoGet(tim->g_ctrl, &info) != FSP_SUCCESS)
  109. return -RT_ERROR;
  110. return info.period_counts;
  111. #else
  112. timer_status_t status;
  113. if (R_GPT_StatusGet(tim->g_ctrl, &status) != FSP_SUCCESS)
  114. return -RT_ERROR;
  115. return status.counter;
  116. #endif
  117. }
  118. static rt_err_t timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
  119. {
  120. rt_err_t result = RT_EOK;
  121. struct ra_hwtimer *tim = RT_NULL;
  122. RT_ASSERT(timer != RT_NULL);
  123. RT_ASSERT(arg != RT_NULL);
  124. tim = (struct ra_hwtimer *)timer->parent.user_data;
  125. switch (cmd)
  126. {
  127. case HWTIMER_CTRL_FREQ_SET:
  128. {
  129. rt_uint8_t index = 0;
  130. rt_uint32_t freq = *((rt_uint32_t *)arg);
  131. for (rt_uint8_t i = 0; i < PLCKD_PRESCALER_MAX_SELECT; i++)
  132. {
  133. if (freq <= PLCKD_FREQ_PRESCALER[i])
  134. {
  135. index = i;
  136. }
  137. }
  138. tim->g_ctrl->p_reg->GTCR_b.TPCS = index;
  139. }
  140. break;
  141. default:
  142. {
  143. result = -RT_ENOSYS;
  144. }
  145. break;
  146. }
  147. return result;
  148. }
  149. static void timer_one_shot_check(void)
  150. {
  151. IRQn_Type irq = R_FSP_CurrentIrqGet();
  152. /* Recover ISR context saved in open. */
  153. gpt_instance_ctrl_t *p_instance_ctrl = (gpt_instance_ctrl_t *) R_FSP_IsrContextGet(irq);
  154. /* If one-shot mode is selected, stop the timer since period has expired. */
  155. if (TIMER_MODE_ONE_SHOT == p_instance_ctrl->p_cfg->mode)
  156. {
  157. p_instance_ctrl->p_reg->GTSTP = p_instance_ctrl->channel_mask;
  158. /* Clear the GPT counter and the overflow flag after the one shot pulse has being generated */
  159. p_instance_ctrl->p_reg->GTCNT = 0;
  160. p_instance_ctrl->p_reg->GTCCR[0U] = 0;
  161. p_instance_ctrl->p_reg->GTCCR[1U] = 0;
  162. /* Clear pending interrupt to make sure it doesn't fire again if another overflow has already occurred. */
  163. R_BSP_IrqClearPending(irq);
  164. }
  165. }
  166. #ifdef BSP_USING_TIM0
  167. void timer0_callback(timer_callback_args_t *p_args)
  168. {
  169. /* enter interrupt */
  170. rt_interrupt_enter();
  171. if (TIMER_EVENT_CYCLE_END == p_args->event)
  172. {
  173. rt_device_hwtimer_isr(&ra_hwtimer_obj[BSP_TIMER0_INDEX].tmr_device);
  174. timer_one_shot_check();
  175. }
  176. /* leave interrupt */
  177. rt_interrupt_leave();
  178. }
  179. #endif
  180. #ifdef BSP_USING_TIM1
  181. void timer1_callback(timer_callback_args_t *p_args)
  182. {
  183. /* enter interrupt */
  184. rt_interrupt_enter();
  185. if (TIMER_EVENT_CYCLE_END == p_args->event)
  186. {
  187. rt_device_hwtimer_isr(&ra_hwtimer_obj[BSP_TIMER1_INDEX].tmr_device);
  188. timer_one_shot_check();
  189. }
  190. /* leave interrupt */
  191. rt_interrupt_leave();
  192. }
  193. #endif
  194. static const struct rt_hwtimer_ops _ops =
  195. {
  196. .init = timer_init,
  197. .start = timer_start,
  198. .stop = timer_stop,
  199. .count_get = timer_counter_get,
  200. .control = timer_ctrl,
  201. };
  202. static const struct rt_hwtimer_info _info = TMR_DEV_INFO_CONFIG;
  203. static int rt_hw_hwtimer_init(void)
  204. {
  205. int result = RT_EOK;
  206. for (int i = 0; i < sizeof(ra_hwtimer_obj) / sizeof(ra_hwtimer_obj[0]); i++)
  207. {
  208. ra_hwtimer_obj[i].tmr_device.info = &_info;
  209. ra_hwtimer_obj[i].tmr_device.ops = &_ops;
  210. if (rt_device_hwtimer_register(&ra_hwtimer_obj[i].tmr_device, ra_hwtimer_obj[i].name, &ra_hwtimer_obj[i]) == RT_EOK)
  211. {
  212. LOG_D("%s register success", ra_hwtimer_obj[i].name);
  213. }
  214. else
  215. {
  216. LOG_E("%s register failed", ra_hwtimer_obj[i].name);
  217. result = -RT_ERROR;
  218. }
  219. }
  220. return result;
  221. }
  222. INIT_BOARD_EXPORT(rt_hw_hwtimer_init);
  223. /* This is a hwtimer example */
  224. #define HWTIMER_DEV_NAME "timer0" /* device name */
  225. static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
  226. {
  227. rt_kprintf("this is hwtimer timeout callback fucntion!\n");
  228. rt_kprintf("tick is :%d !\n", rt_tick_get());
  229. return RT_EOK;
  230. }
  231. int hwtimer_sample(void)
  232. {
  233. rt_err_t ret = RT_EOK;
  234. rt_hwtimerval_t timeout_s;
  235. rt_device_t hw_dev = RT_NULL;
  236. rt_hwtimer_mode_t mode;
  237. rt_uint32_t freq = 1875000; /* 1Mhz */
  238. hw_dev = rt_device_find(HWTIMER_DEV_NAME);
  239. if (hw_dev == RT_NULL)
  240. {
  241. rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
  242. return -RT_ERROR;
  243. }
  244. ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  245. if (ret != RT_EOK)
  246. {
  247. rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
  248. return ret;
  249. }
  250. rt_device_set_rx_indicate(hw_dev, timeout_cb);
  251. rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
  252. mode = HWTIMER_MODE_PERIOD;
  253. ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  254. if (ret != RT_EOK)
  255. {
  256. rt_kprintf("set mode failed! ret is :%d\n", ret);
  257. return ret;
  258. }
  259. /* Example Set the timeout period of the timer */
  260. timeout_s.sec = 1; /* secend */
  261. timeout_s.usec = 0; /* microsecend */
  262. if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  263. {
  264. rt_kprintf("set timeout value failed\n");
  265. return -RT_ERROR;
  266. }
  267. /* read hwtimer value */
  268. rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
  269. rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
  270. return ret;
  271. }
  272. MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
  273. #endif /* BSP_USING_HWTIMER */