对于一个单片机,定时器通常是最基础的功能。不过,这里的定时器是基于硬件的定时器。依靠时钟晶振,硬件电路来实现的。RT-Thread也有自己的定时器,这个定时器是由以下几个部分构成:
/** * Thread structure */ struct rt_thread { ....... struct rt_timer thread_timer; /**< built-in thread timer */ ....... } /** *timer structure */ struct rt_timer { struct rt_object parent; /**< inherit from rt_object */ rt_list_t row[RT_TIMER_SKIP_LIST_LEVEL]; void (*timeout_func)(void *parameter); /**< timeout function */ void *parameter; /**< timeout function's parameter */ rt_tick_t init_tick; /**< timer timeout tick */ rt_tick_t timeout_tick; /**< timeout tick */ }; typedef struct rt_timer *rt_timer_t;
内嵌到线程控制块之中,内嵌到线程之中是为了实现RT-Thread中线程的一个状态的实现挂起态(阻塞态),比如说我们要挂起一个线程。我们直接修改线程的状态位,成功将线程挂起,然后修改remaining_tick,设置需要延时的时间。嗯嗯嗯~~貌似这样也能行,不过这是之前的版本了。
我梳理了一下定时器的使用:
- 定时器单独使用
- 定时器嵌入线程中,用于挂起的
static void _rt_timer_init(rt_timer_t timer,
void (*timeout)(void *parameter),
void *parameter,
rt_tick_t time,
rt_uint8_t flag)
{
int i;
/* set flag */
timer->parent.flag = flag;
/* set deactivated */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
timer->timeout_func = timeout;
timer->parameter = parameter;
timer->timeout_tick = 0;
timer->init_tick = time;
/* initialize timer list */
for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
{
rt_list_init(&(timer->row[i]));
}
}
就功能而言,rt_time功能复杂度更类似于rt_thread。超时函数类似于线程的入口函数.
/**
* This function will check timer list, if a timeout event happens, the
* corresponding timeout function will be invoked.
*
* @note this function shall be invoked in operating system timer interrupt.
*/
void rt_timer_check(void)
{
struct rt_timer *t;
rt_tick_t current_tick;
register rt_base_t level;
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
current_tick = rt_tick_get();
/* disable interrupt */
level = rt_hw_interrupt_disable();
while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1])) //定时器列表不为空,就扫描定时器列表
{
t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
/*
* It supposes that the new tick shall less than the half duration of
* tick max.
*/
if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2) //这里并不写小于RT_TICK_MAX一定是有原因的,
//设想,如果timeout_tick是大于RT_TICK_MAX / 2
{
RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
/* remove timer from timer list firstly */
_rt_timer_remove(t);
/* 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-Thread中,设定了一个链表,static rt_list_t rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL],这个链表存在的意义就是:缩减了查询那么多定时器所花费的时间。有了这一个链表,那么只需要查询一次便可以得知有没有已经到达了时间的定时器。
原因是:这个链表是有顺序的,越靠近初始位的定时时间越短。
/**
* This function will start the timer
*
* @param timer the timer to be started
*
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
*/
rt_err_t rt_timer_start(rt_timer_t timer)
{
unsigned int row_lvl;
rt_list_t *timer_list;
register rt_base_t level;
rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
unsigned int tst_nr;
static unsigned int random_nr;
/* timer check */
RT_ASSERT(timer != RT_NULL);
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
/* stop timer firstly */
level = rt_hw_interrupt_disable();
/* remove timer from list */
_rt_timer_remove(timer);
/* change status of timer */
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
rt_hw_interrupt_enable(level);
RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
/*
* get timeout tick,
* the max timeout tick shall not great than RT_TICK_MAX/2
*/
RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
timer->timeout_tick = rt_tick_get() + timer->init_tick;
/* disable interrupt */
level = rt_hw_interrupt_disable();
#ifdef RT_USING_TIMER_SOFT
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
{
/* insert timer to soft timer list */
timer_list = rt_soft_timer_list;
}
else
#endif
//获取系统定时器列表的根节点地址
{
/* insert timer to system timer list */
timer_list = rt_timer_list;
}
//获取系统定时器列表第一条链表根节点地址
row_head[0] = &timer_list[0];
//这里的RT_TIMER_SKIP_LIST_LEVEL的大小定义并不在rtconfig.h文件中,写就是不是常用来修改的,且常为1,只执行一遍for循环这里
for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
{
//第一次进入rt_timer_start函数时,row_head[row_lvl] == timer_list[row_lvl].prev;也就是第一次不执行这个函数
//加入是第二次进入,那么,由于不满足函数体的条件会依次从第一项向下搜索
for (; row_head[row_lvl] != timer_list[row_lvl].prev;
row_head[row_lvl] = row_head[row_lvl]->next)
{
struct rt_timer *t;
rt_list_t *p = row_head[row_lvl]->next;
/* fix up the entry pointer */
t = rt_list_entry(p, struct rt_timer, row[row_lvl]);
/* If we have two timers that timeout at the same time, it's
* preferred that the timer inserted early get called early.
* So insert the new timer to the end the the some-timeout timer
* list.
*/
if ((t->timeout_tick - timer->timeout_tick) == 0)
{
continue;
}
else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
{
break;
}
}
////等等我再想想这点吧
if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
row_head[row_lvl + 1] = row_head[row_lvl] + 1;
}
/* Interestingly, this super simple timer insert counter works very very
* well on distributing the list height uniformly. By means of "very very
* well", I mean it beats the randomness of timer->timeout_tick very easily
* (actually, the timeout_tick is not random and easy to be attacked). */
//这里是为了计算添加了几个定时器
random_nr++;
tst_nr = random_nr;
//刚才找到了插入的位置,现在插入到链表中
rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],
&(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
//RT_TIMER_SKIP_LIST_LEVEL是等于1的,不会执行。
//等等我再想想这点吧
for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
{
if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK))
rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl],
&(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl]));
else
break;
/* Shift over the bits we have tested. Works well with 1 bit and 2
* bits. */
tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;
}
timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
/* enable interrupt */
rt_hw_interrupt_enable(level);
#ifdef RT_USING_TIMER_SOFT
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
{
/* check whether timer thread is ready */
if ((timer_thread.stat & RT_THREAD_STAT_MASK) != RT_THREAD_READY)
{
/* resume timer thread to check soft timer */
rt_thread_resume(&timer_thread);
rt_schedule();
}
}
#endif
return RT_EOK;
}
RTM_EXPORT(rt_timer_start);
至于为什么定时器表的第一个的定时时间是最短,是因为这个rt_timer_start函数来设置的。定时器的启动都会经过这个函数,就像是线程启动都会经过startup函数一样;定时器启动的函数就设置了这个定时器处于链表的哪一个位置。
硬定时器和软定时器的说法,你可能在视频或者什么里见过。至少我当时是听过的,我还记得他当时说,硬定时器的调度是在滴答定时器上的,软定时器是软定时上的,他们响应不是在同一个位置。
通过参看源码,也是这样的,软定时器是写了一个定时线程,这个定时线程的优先级为10,像是一个线程一样。而硬件定时器,每一个滴答周期都会去检索定时器表,以确定那个定时器已经到达时间。
/**
* @ingroup SystemInit
*
* This function will initialize system timer thread
*/
void rt_system_timer_thread_init(void)
{
#ifdef RT_USING_TIMER_SOFT
int i;
for (i = 0;
i < sizeof(rt_soft_timer_list) / sizeof(rt_soft_timer_list[0]);
i++)
{
rt_list_init(rt_soft_timer_list + i);
}
/* start software timer thread */
rt_thread_init(&timer_thread,
"timer",
rt_thread_timer_entry,
RT_NULL,
&timer_thread_stack[0],
sizeof(timer_thread_stack),
RT_TIMER_THREAD_PRIO,
10);
/* startup */
rt_thread_startup(&timer_thread);
#endif
}
时间片,是为了为同一个优先级下有多个线程时使用的。线程在这个线程就绪表中,且有剩余的时间片时间,才可以继续执行,类似于轮询。工作状态类似于轮询,因为看起来是每个都执行了。不过,就实况来看,轮询是执行完周期,这里是消耗自己的时间片时间。
/**
* Thread structure
*/
struct rt_thread
{
···
rt_ubase_t init_tick; /**< thread's initialized tick */
rt_ubase_t remaining_tick; /**< remaining tick */
···
}
static rt_err_t _rt_thread_init(struct rt_thread *thread,
const char *name,
void (*entry)(void *parameter),
void *parameter,
void *stack_start,
rt_uint32_t stack_size,
rt_uint8_t priority,
rt_uint32_t tick)
{
···
/* tick init */
thread->init_tick = tick;
thread->remaining_tick = tick;
```
}
/**
* This function will notify kernel there is one tick passed. Normally,
* this function is invoked by clock ISR.
*/
void rt_tick_increase(void)
{
struct rt_thread *thread;
/* increase the global tick */
++ rt_tick;
/* check time slice */
thread = rt_thread_self();
-- thread->remaining_tick;
if (thread->remaining_tick == 0)
{
/* change to initialized tick */
thread->remaining_tick = thread->init_tick;
/* yield */
rt_thread_yield(); //如果运行时间达到,出让CPU,查看这个函数,你可以发现是从就绪列表中删除
//从优先级列表删除后,重新插入优先级列表
}
/* check timer */
rt_timer_check();
}
长延时,比如说要延时10秒,用rt_thread_delay()函数明显是不行的。所以会出现这样的函数
tick = rt_tick_get();
while (rt_tick_get() - tick < (RT_TICK_PER_SECOND / 2)) ;
当在这个函数中时候,由于没有主动出让CPU,是不是以为程序会卡死在这端代码里。但是实际上,程序是不会卡死在这端代码中的。由于滴答定时器的原因,系统会检索当前线程的remain_tick大小,如果相同优先级下有另外一个线程处于就绪态那么就出让CPU,如果没有,那就继续占有CPU;或者,检索定时器,查看定时器是不是有定时到达的线程,若果有,那就执行定时器的回调函数。
- 就绪列表是每个优先级一个
- 在优先级列表里检索最大的已就绪优先级
- 优先级列表不会主动检索自己,检索的功能是rt_scheduler来操作的
- 定时器有定时器链表,每个滴答周期都会检索定时器链表