drv_hwtimer.c 7.7 KB

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