rt-thread的定时器管理源码分析(三)

2014-11-24 08:11:12 · 作者: · 浏览: 1
oses that the new tick shall less than the half duration of
* tick max.
*/
if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
{
rt_list_insert_before(n, &(timer->list));//将定时器timer插入到t之前
break;
}
}
/* no found suitable position in timer list */
if (n == timer_list)//没有找到合适的位置,则放到链表头
{
rt_list_insert_before(n, &(timer->list));
}
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)//如果 系统采用的是软件定时器管理模式,且软件定时器线程处理ready状态,则恢复此线程
{
/* check whether timer thread is ready */
if (timer_thread.stat != RT_THREAD_READY)
{
/* resume timer thread to check soft timer */
rt_thread_resume(&timer_thread);//恢复定时器线程
rt_schedule();//开始线程调度
}
}
#endif
return -RT_EOK;
}
软件定时器管理模式的源码分析完了,接下来介绍RTT的硬件定时器管理模式。
3.3 RTT的硬件定时器管理模式
硬件定时器管理模式顾名思义,就是说与硬件相关,因此,不用的MCU,其部分源码是不一样的,因为其要采用MCU的系统时钟中断例程来实现。
以STM32F2XX为例,先找到其启动汇编,位置在:RTT/bsp/stm32f2xx/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F2xx/startup/arm/startup_stm32f2xx.s
找到中断向量:
[plain]
DCD SysTick_Handler ; SysTick Handler
这是系统时钟中断向量,再找到其中断例程实现:
在bsp/stm32f2xx/drivers/board.c文件中:
[cpp]
/**
* This is the timer interrupt service routine.
*
*/
void SysTick_Handler(void)//系统时钟中断例程
{
/* enter interrupt */
rt_interrupt_enter();
rt_tick_increase();
/* leave interrupt */
rt_interrupt_leave();
}
其中rt_tick_increase函数在RTT/src/clock.c文件中的实现如下:
[cpp]
/**
* 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;//全局rt_tick加1
/* check time slice */
thread = rt_thread_self();//得到当前正在运行的线程
-- thread->remaining_tick;//纯种剩下时间减1
if (thread->remaining_tick == 0)//如果线程剩余时间为0,即调度时间已到
{
/* change to initialized tick */
thread->remaining_tick = thread->init_tick;//将线程剩余时间重新设置初始化值
/* yield */
rt_thread_yield();//调度时间到,切换到其它线程
}
/* check timer */
rt_timer_check();//检查硬件定时器链表是否有定时器产生超时事件
}
其中rt_timer_check函数在RTT/src/timer.c文件中如下定义:
[cpp]
/**
* 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))
{
t = rt_list_entry(rt_timer_list.next, struct rt_timer, list);
/*
* It supp