drv_hwtimer.c 7.6 KB

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