通过前面的分析,我们已经大致知悉了硬件定时器通用实现模板,但这种理解是否正确,以及瑞萨完全实现了硬件定时器所需要的功能,我们并不了解,这部分只能通过查看瑞萨目前实现的硬件定时器代码才能清楚。
源码分析
注册入口
#ifndef TMR_DEV_INFO_CONFIG #define TMR_DEV_INFO_CONFIG \ { \ .maxfreq = 400000000, \ .minfreq = 1562500, \ .maxcnt = 0XFFFFFFFF, \ .cntmode = HWTIMER_CNTMODE_UP, \ } #endif /* TIM_DEV_INFO_CONFIG */ static struct ra_hwtimer ra_hwtimer_obj[BSP_TIMERS_NUM] = { #ifdef BSP_USING_TIM0 [BSP_TIMER0_INDEX] = TIMER_DRV_INITIALIZER(0), #endif #ifdef BSP_USING_TIM1 [BSP_TIMER1_INDEX] = TIMER_DRV_INITIALIZER(1), #endif }; static const struct rt_hwtimer_ops _ops = { .init = timer_init, .start = timer_start, .stop = timer_stop, .count_get = timer_counter_get, .control = timer_ctrl, }; static const struct rt_hwtimer_info _info = TMR_DEV_INFO_CONFIG; static int rt_hw_hwtimer_init(void) { int result = RT_EOK; for (int i = 0; i < sizeof(ra_hwtimer_obj) / sizeof(ra_hwtimer_obj[0]); i++) { ra_hwtimer_obj[i].tmr_device.info = &_info; ra_hwtimer_obj[i].tmr_device.ops = &_ops; if (rt_device_hwtimer_register(&ra_hwtimer_obj[i].tmr_device, ra_hwtimer_obj[i].name, &ra_hwtimer_obj[i]) == RT_EOK) { LOG_D("%s register success", ra_hwtimer_obj[i].name); } else { LOG_E("%s register failed", ra_hwtimer_obj[i].name); result = -RT_ERROR; } } return result; } INIT_BOARD_EXPORT(rt_hw_hwtimer_init);
从实现上看,注册函数并未对硬件定时器的硬件部分做任何操作,仅仅是对驱动内部参数赋了初始值并通过rt_device_hwtimer_register在系统中注册了硬件定时器,这也符合上层驱动要求暴露init接口的做法。
初始化入口
static void timer_init(struct rt_hwtimer_device *timer, rt_uint32_t state) { RT_ASSERT(timer != RT_NULL); struct ra_hwtimer *tim; tim = (struct ra_hwtimer *)timer->parent.user_data; if (state) { fsp_err_t fsp_err = FSP_SUCCESS; fsp_err = R_GPT_Open(tim->g_ctrl, tim->g_cfg); if (fsp_err != FSP_SUCCESS) { LOG_E("%s init fail", tim->name); } } }
从实现上看,目前瑞萨的代码并未实现解初始化部分,也就是说,如果应用上要实现低功耗的需求,可能就需要自行实现解初始化的部分。
启动入口
static rt_err_t timer_start(rt_hwtimer_t *timer, rt_uint32_t pr, rt_hwtimer_mode_t opmode) { RT_ASSERT(timer != RT_NULL); RT_ASSERT(opmode != RT_NULL); struct ra_hwtimer *tim; tim = (struct ra_hwtimer *)timer->parent.user_data; fsp_err_t err = FSP_SUCCESS; /* set timer count */ R_GPT_CounterSet(tim->g_ctrl, 0); /* set timer period register */ err = R_GPT_PeriodSet(tim->g_ctrl, pr); if (err != FSP_SUCCESS) { return -RT_ERROR; } /* set timer to one cycle mode */ err = R_GPT_Start(tim->g_ctrl); return (err == FSP_SUCCESS) ? RT_EOK : -RT_ERROR; }
启动入口主要是初始值设置并启动定时器。
中断处理入口
static void timer_one_shot_check(void) { IRQn_Type irq = R_FSP_CurrentIrqGet(); /* Recover ISR context saved in open. */ gpt_instance_ctrl_t *p_instance_ctrl = (gpt_instance_ctrl_t *) R_FSP_IsrContextGet(irq); /* If one-shot mode is selected, stop the timer since period has expired. */ if (TIMER_MODE_ONE_SHOT == p_instance_ctrl->p_cfg->mode) { p_instance_ctrl->p_reg->GTSTP = p_instance_ctrl->channel_mask; /* Clear the GPT counter and the overflow flag after the one shot pulse has being generated */ p_instance_ctrl->p_reg->GTCNT = 0; p_instance_ctrl->p_reg->GTCCR[0U] = 0; p_instance_ctrl->p_reg->GTCCR[1U] = 0; /* Clear pending interrupt to make sure it doesn't fire again if another overflow has already occurred. */ R_BSP_IrqClearPending(irq); } } #ifdef BSP_USING_TIM0 void timer0_callback(timer_callback_args_t *p_args) { /* enter interrupt */ rt_interrupt_enter(); if (TIMER_EVENT_CYCLE_END == p_args->event) { rt_device_hwtimer_isr(&ra_hwtimer_obj[BSP_TIMER0_INDEX].tmr_device); timer_one_shot_check(); } /* leave interrupt */ rt_interrupt_leave(); } #endif #ifdef BSP_USING_TIM1 void timer1_callback(timer_callback_args_t *p_args) { /* enter interrupt */ rt_interrupt_enter(); if (TIMER_EVENT_CYCLE_END == p_args->event) { rt_device_hwtimer_isr(&ra_hwtimer_obj[BSP_TIMER1_INDEX].tmr_device); timer_one_shot_check(); } /* leave interrupt */ rt_interrupt_leave(); } #endif
具体的功能是在中断处理中调用rt_device_hwtimer_isr回调,通知上层硬件定时器事件发生,并检查定时器设置是否是单次触发,如果是单次触发,则内部停用定时器。
停止入口
static void timer_stop(rt_hwtimer_t *timer) { struct ra_hwtimer *tim = RT_NULL; RT_ASSERT(timer != RT_NULL); tim = (struct ra_hwtimer *)timer->parent.user_data; /* stop timer */ R_GPT_Stop(tim->g_ctrl); /* set timer count */ R_GPT_CounterSet(tim->g_ctrl, 0); }
停止入口是停用定时器并清空计数值,但个人认为其实停用计数值没有啥必要,因为这个启用时又会重新清0,而不使用时,这个计数值也不会被使用。
获取计数值入口
static rt_uint32_t timer_counter_get(rt_hwtimer_t *timer) { struct ra_hwtimer *tim = RT_NULL; RT_ASSERT(timer != RT_NULL); tim = (struct ra_hwtimer *)timer->parent.user_data; timer_info_t info; if (R_GPT_InfoGet(tim->g_ctrl, &info) != FSP_SUCCESS) return -RT_ERROR; return info.period_counts; }
获取计数值接口,并没有什么特殊的部分,就是从瑞萨自己的硬件适配层接口中获取实际的计数值并上报。
控制入口
const rt_uint32_t PLCKD_FREQ_PRESCALER[PLCKD_PRESCALER_MAX_SELECT] = { #ifdef SOC_SERIES_R7FA6M3 PLCKD_PRESCALER_120M, PLCKD_PRESCALER_60M, PLCKD_PRESCALER_30M, PLCKD_PRESCALER_15M, PLCKD_PRESCALER_7_5M, PLCKD_PRESCALER_3_75M, PLCKD_PRESCALER_1_875M, #endif }; static rt_err_t timer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg) { rt_err_t result = RT_EOK; struct ra_hwtimer *tim = RT_NULL; RT_ASSERT(timer != RT_NULL); RT_ASSERT(arg != RT_NULL); tim = (struct ra_hwtimer *)timer->parent.user_data; switch (cmd) { case HWTIMER_CTRL_FREQ_SET: { rt_uint8_t index = 0; rt_uint32_t freq = *((rt_uint32_t *)arg); for (rt_uint8_t i = 0; i < PLCKD_PRESCALER_MAX_SELECT; i++) { if (freq <= PLCKD_FREQ_PRESCALER[i]) { index = i; } } tim->g_ctrl->p_reg->GTCR_b.TPCS = index; } break; default: { result = -RT_ENOSYS; } break; } return result; }
控制接口只实现了HWTIMER_CTRL_FREQ_SET,也就是定时器的工作频率设定。
总结
至此,瑞萨的硬件定时器实现已经分析完毕,从实现代码上看,我们可以发现以下信息:
1.瑞萨的硬件定时器,并没有实现解初始化部分
2. 瑞萨实现的控制入口,仅仅实现了设置工作频率入口,其他直接上报不支持
这些未实现的部分,在使用瑞萨代码时需要注意区分。