drv_hwtimer.c 7.6 KB

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