rt-thread的定时器管理源码分析(四)
oses that the new tick shall less than the half duration of
* tick max.
*/
if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2)
{
RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
/* remove timer from timer list firstly */
rt_list_remove(&(t->list));
/* call timeout function */
t->timeout_func(t->parameter);
/* re-get tick */
current_tick = rt_tick_get();
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
{
/* start it */
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
rt_timer_start(t);
}
else
{
/* stop timer */
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
}
}
else
break;
}
/* enable interrupt */
rt_hw_interrupt_enable(level);
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
}
此函数与rt_soft_timer_check基本大致相同,只不过一个是查找硬件定时器链表rt_timer_list,一个是查找rt_soft_timer_list.
在此,硬件定时器管理模式基本上介绍完毕,接下来介绍一些定时器接口.
4 定时器接口
4.1 定时器初始化
静态初始化定义器
[cpp]
/**
* This function will initialize a timer, normally this function is used to
* initialize a static timer object.
*
* @param timer the static timer object
* @param name the name of timer
* @param timeout the timeout function
* @param parameter the parameter of timeout function
* @param time the tick of timer
* @param flag the flag of timer
*/
void rt_timer_init(rt_timer_t timer,
const char *name,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
{
/* timer check */
RT_ASSERT(timer != RT_NULL);
/* timer object initialization */
rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);//初始化内核对象
_rt_timer_init(timer, timeout, parameter, time, flag);
}
_rt_timer_init函数如下定义:
[cpp]
static void _rt_timer_init(rt_timer_t timer,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
{
/* set flag */
timer->parent.flag = flag;//置flag
/* set deactivated */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;//初始化时,设置为非激活状态
timer->timeout_func = timeout;//设置超时事件处理函数
timer->parameter = parameter;//超时事件处理函数的传入参数
timer->timeout_tick = 0;//定时器的超时时间点初始化时为0
timer->init_tick = time;//置超时时间
/* initialize timer list */
rt_list_init(&(timer->list));//初始化本身节点
}
动态创建定时器
[cpp]
/**
* This function will create a timer
*
* @param name the name of timer
* @param timeout the timeout function
* @param parameter the parameter of timeout function
* @param time the tick of timer
* @param flag the flag of timer
*
* @return the created timer object
*/
rt_timer_t rt_timer_create(const char *name,
void (*timeout)(void