2009-07-03 06:48:23 +08:00
|
|
|
/*
|
|
|
|
* File : timer.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
2009-12-25 20:18:53 +08:00
|
|
|
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
|
|
|
* The license and distribution terms for this file may be
|
|
|
|
* found in the file LICENSE in this distribution or at
|
2009-12-23 21:52:42 +08:00
|
|
|
* http://www.rt-thread.org/license/LICENSE
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2006-03-12 Bernard first version
|
|
|
|
* 2006-04-29 Bernard implement thread timer
|
|
|
|
* 2006-06-04 Bernard implement rt_timer_control
|
|
|
|
* 2006-08-10 Bernard fix the periodic timer bug
|
|
|
|
* 2006-09-03 Bernard implement rt_timer_detach
|
2009-12-22 23:06:27 +08:00
|
|
|
* 2009-11-11 LiJin add soft timer
|
2010-05-23 07:26:08 +08:00
|
|
|
* 2010-05-12 Bernard fix the timer check bug.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rthw.h>
|
|
|
|
|
|
|
|
#include "kservice.h"
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
/* #define RT_TIMER_DEBUG */
|
2009-12-22 23:06:27 +08:00
|
|
|
|
|
|
|
/* hard timer list */
|
2010-05-23 07:26:08 +08:00
|
|
|
static rt_list_t rt_timer_list, rt_long_timer_list;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
2009-12-22 23:06:27 +08:00
|
|
|
/* soft timer list */
|
2010-05-23 07:26:08 +08:00
|
|
|
static rt_list_t rt_soft_timer_list, rt_soft_long_timer_list;
|
2009-12-23 21:52:42 +08:00
|
|
|
#endif
|
2009-12-22 23:06:27 +08:00
|
|
|
|
2009-07-03 06:48:23 +08:00
|
|
|
#ifdef RT_USING_HOOK
|
|
|
|
extern void (*rt_object_take_hook)(struct rt_object* object);
|
|
|
|
extern void (*rt_object_put_hook)(struct rt_object* object);
|
|
|
|
static void (*rt_timer_timeout_hook)(struct rt_timer* timer);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup Hook
|
|
|
|
*/
|
|
|
|
/*@{*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will set a hook function, which will be invoked when timer
|
|
|
|
* is timeout.
|
2009-12-23 21:52:42 +08:00
|
|
|
*
|
2009-07-03 06:48:23 +08:00
|
|
|
* @param hook the hook function
|
|
|
|
*/
|
|
|
|
void rt_timer_timeout_sethook(void (*hook)(struct rt_timer* timer))
|
|
|
|
{
|
|
|
|
rt_timer_timeout_hook = hook;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*@}*/
|
|
|
|
#endif
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
static void _rt_timer_init(rt_timer_t timer,
|
|
|
|
void (*timeout)(void* parameter), void* parameter,
|
|
|
|
rt_tick_t time, rt_uint8_t flag)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* init timer list */
|
|
|
|
rt_list_init(&(timer->list));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup Clock
|
|
|
|
*/
|
|
|
|
/*@{*/
|
|
|
|
|
|
|
|
/**
|
2009-12-23 21:52:42 +08:00
|
|
|
* This function will init a timer, normally this function is used to initialize
|
2009-07-03 06:48:23 +08:00
|
|
|
* 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
|
|
|
|
*/
|
2009-12-23 21:52:42 +08:00
|
|
|
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)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
|
|
|
/* timer check */
|
|
|
|
RT_ASSERT(timer != RT_NULL);
|
|
|
|
|
|
|
|
/* timer object init */
|
|
|
|
rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);
|
|
|
|
|
|
|
|
_rt_timer_init(timer, timeout, parameter, time, flag);
|
|
|
|
}
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
/**
|
2009-07-03 06:48:23 +08:00
|
|
|
* This function will detach a timer from timer management.
|
|
|
|
*
|
|
|
|
* @param timer the static timer object
|
2009-12-23 21:52:42 +08:00
|
|
|
*
|
2009-07-03 06:48:23 +08:00
|
|
|
* @return the operation status, RT_EOK on OK; RT_ERROR on error
|
|
|
|
*/
|
|
|
|
rt_err_t rt_timer_detach(rt_timer_t timer)
|
|
|
|
{
|
|
|
|
register rt_base_t level;
|
2009-12-23 21:52:42 +08:00
|
|
|
|
2009-07-03 06:48:23 +08:00
|
|
|
/* timer check */
|
|
|
|
RT_ASSERT(timer != RT_NULL);
|
|
|
|
|
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
|
|
|
/* remove it from timer list */
|
|
|
|
rt_list_remove(&(timer->list));
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
rt_object_detach((rt_object_t)timer);
|
|
|
|
|
|
|
|
return -RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
/**
|
|
|
|
* 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* parameter), void* parameter, rt_tick_t time, rt_uint8_t flag)
|
|
|
|
{
|
|
|
|
struct rt_timer* timer;
|
|
|
|
|
|
|
|
/* allocate a object */
|
|
|
|
timer = (struct rt_timer*)rt_object_allocate(RT_Object_Class_Timer, name);
|
|
|
|
if (timer == RT_NULL)
|
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
_rt_timer_init(timer, timeout, parameter, time, flag);
|
|
|
|
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will delete a timer and release timer memory
|
|
|
|
*
|
|
|
|
* @param timer the timer to be deleted
|
|
|
|
*
|
|
|
|
* @return the operation status, RT_EOK on OK; RT_ERROR on error
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
rt_err_t rt_timer_delete(rt_timer_t timer)
|
|
|
|
{
|
|
|
|
register rt_base_t level;
|
2009-12-23 21:52:42 +08:00
|
|
|
|
2009-07-03 06:48:23 +08:00
|
|
|
/* timer check */
|
|
|
|
RT_ASSERT(timer != RT_NULL);
|
|
|
|
|
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
|
|
|
/* remove it from timer list */
|
|
|
|
rt_list_remove(&(timer->list));
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
rt_object_delete((rt_object_t)timer);
|
|
|
|
|
|
|
|
return -RT_EOK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
struct rt_timer* t;
|
|
|
|
register rt_base_t level;
|
2010-05-23 07:26:08 +08:00
|
|
|
rt_list_t *n, *timer_list;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/* timer check */
|
|
|
|
RT_ASSERT(timer != RT_NULL);
|
|
|
|
if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) return -RT_ERROR;
|
|
|
|
|
|
|
|
#ifdef RT_USING_HOOK
|
|
|
|
if (rt_object_take_hook != RT_NULL) rt_object_take_hook(&(timer->parent));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
2010-05-23 07:26:08 +08:00
|
|
|
|
|
|
|
/* get timeout tick, which will wrap around if it reaches max ticks */
|
|
|
|
timer->timeout_tick = rt_tick_get() + timer->init_tick;
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
2009-12-22 23:06:27 +08:00
|
|
|
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
|
2009-12-23 21:52:42 +08:00
|
|
|
{
|
2010-05-23 07:26:08 +08:00
|
|
|
if ((RT_TICK_MAX - timer->init_tick) < rt_tick_get())
|
|
|
|
/* it's a long timer */
|
|
|
|
timer_list = &rt_soft_long_timer_list;
|
|
|
|
else
|
|
|
|
/* it's a short timer */
|
|
|
|
timer_list = &rt_soft_timer_list;
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
/* insert timer to soft timer list */
|
2010-05-23 07:26:08 +08:00
|
|
|
for (n = timer_list->next; n != timer_list; n = n->next)
|
2009-12-22 23:06:27 +08:00
|
|
|
{
|
|
|
|
t = rt_list_entry(n, struct rt_timer, list);
|
|
|
|
if (t->timeout_tick > timer->timeout_tick)
|
|
|
|
{
|
|
|
|
rt_list_insert_before(n, &(timer->list));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* no found suitable position in timer list */
|
2010-05-23 07:26:08 +08:00
|
|
|
if (n == timer_list)
|
2009-12-22 23:06:27 +08:00
|
|
|
{
|
|
|
|
rt_list_insert_before(n, &(timer->list));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2009-12-23 21:52:42 +08:00
|
|
|
#endif
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2010-05-23 07:26:08 +08:00
|
|
|
if ((RT_TICK_MAX - timer->init_tick) < rt_tick_get())
|
|
|
|
/* it's a long timer */
|
|
|
|
timer_list = &rt_long_timer_list;
|
|
|
|
else
|
|
|
|
/* it's a short timer */
|
|
|
|
timer_list = &rt_timer_list;
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
/* insert timer to system timer list */
|
2010-05-23 07:26:08 +08:00
|
|
|
for (n = timer_list->next; n != timer_list; n = n->next)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2009-12-23 21:52:42 +08:00
|
|
|
t = rt_list_entry(n, struct rt_timer, list);
|
|
|
|
if (t->timeout_tick > timer->timeout_tick)
|
|
|
|
{
|
|
|
|
rt_list_insert_before(n, &(timer->list));
|
|
|
|
break;
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
2010-05-23 07:26:08 +08:00
|
|
|
/* not found suitable position in timer list */
|
|
|
|
if (n == timer_list)
|
2009-12-23 21:52:42 +08:00
|
|
|
{
|
|
|
|
rt_list_insert_before(n, &(timer->list));
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
return -RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will stop the timer
|
|
|
|
*
|
|
|
|
* @param timer the timer to be stopped
|
|
|
|
*
|
|
|
|
* @return the operation status, RT_EOK on OK; RT_ERROR on error
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
rt_err_t rt_timer_stop(rt_timer_t timer)
|
|
|
|
{
|
|
|
|
register rt_base_t level;
|
|
|
|
|
|
|
|
/* timer check */
|
|
|
|
RT_ASSERT(timer != RT_NULL);
|
|
|
|
if(!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)) return -RT_ERROR;
|
|
|
|
|
|
|
|
#ifdef RT_USING_HOOK
|
|
|
|
if (rt_object_put_hook != RT_NULL) rt_object_put_hook(&(timer->parent));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
2009-10-19 20:11:17 +08:00
|
|
|
/* change stat */
|
|
|
|
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
|
|
|
|
2009-07-03 06:48:23 +08:00
|
|
|
/* remove it from timer list */
|
|
|
|
rt_list_remove(&(timer->list));
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will get or set some options of the timer
|
|
|
|
*
|
|
|
|
* @param timer the timer to be get or set
|
|
|
|
* @param cmd the control command
|
|
|
|
* @param arg the argument
|
|
|
|
*
|
|
|
|
* @return the operation status, RT_EOK on OK; RT_ERROR on error
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void* arg)
|
|
|
|
{
|
|
|
|
/* timer check */
|
|
|
|
RT_ASSERT(timer != RT_NULL);
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case RT_TIMER_CTRL_GET_TIME:
|
|
|
|
*(rt_tick_t*)arg = timer->init_tick;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_TIMER_CTRL_SET_TIME:
|
|
|
|
timer->init_tick = *(rt_tick_t*)arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_TIMER_CTRL_SET_ONESHOT:
|
|
|
|
timer->parent.flag &= ~(1 << RT_TIMER_FLAG_PERIODIC);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_TIMER_CTRL_SET_PERIODIC:
|
|
|
|
timer->parent.flag |= (1 << RT_TIMER_FLAG_PERIODIC);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2010-05-23 07:26:08 +08:00
|
|
|
void rt_timer_switch(void)
|
|
|
|
{
|
|
|
|
rt_base_t level;
|
|
|
|
rt_timer_t t;
|
|
|
|
|
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
|
|
|
/* switch system timer list to long timer list */
|
|
|
|
|
|
|
|
/* remove all timer in the old timer list */
|
|
|
|
while (!rt_list_isempty(&rt_timer_list))
|
|
|
|
{
|
|
|
|
t = rt_list_entry(rt_timer_list.next, struct rt_timer, list);
|
|
|
|
#ifdef RT_USING_HOOK
|
|
|
|
if (rt_timer_timeout_hook != RT_NULL) rt_timer_timeout_hook(t);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* remove timer from timer list firstly */
|
|
|
|
rt_list_remove(&(t->list));
|
|
|
|
|
|
|
|
/* call timeout function */
|
|
|
|
t->timeout_func(t->parameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_timer_list.next = rt_long_timer_list.next;
|
|
|
|
rt_timer_list.prev = rt_long_timer_list.prev;
|
|
|
|
rt_long_timer_list.next->prev = &rt_timer_list;
|
|
|
|
rt_long_timer_list.prev->next = &rt_timer_list;
|
|
|
|
rt_list_init(&rt_long_timer_list);
|
|
|
|
|
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
|
|
|
/* remove all timer in the old timer list */
|
|
|
|
while (!rt_list_isempty(&rt_soft_timer_list))
|
|
|
|
{
|
|
|
|
t = rt_list_entry(rt_soft_timer_list.next, struct rt_timer, list);
|
|
|
|
#ifdef RT_USING_HOOK
|
|
|
|
if (rt_timer_timeout_hook != RT_NULL) rt_timer_timeout_hook(t);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* remove timer from timer list firstly */
|
|
|
|
rt_list_remove(&(t->list));
|
|
|
|
|
|
|
|
/* call timeout function */
|
|
|
|
t->timeout_func(t->parameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_soft_timer_list.next = rt_soft_long_timer_list.next;
|
|
|
|
rt_soft_timer_list.prev = rt_soft_long_timer_list.prev;
|
|
|
|
rt_soft_long_timer_list.next->prev = &rt_soft_timer_list;
|
|
|
|
rt_soft_long_timer_list.prev->next = &rt_soft_timer_list;
|
|
|
|
rt_list_init(&rt_soft_long_timer_list);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
}
|
|
|
|
|
2009-07-03 06:48:23 +08:00
|
|
|
/**
|
2009-12-23 21:52:42 +08:00
|
|
|
* This function will check timer list, if a timeout event happens, the
|
2009-07-03 06:48:23 +08:00
|
|
|
* corresponding timeout function will be invoked.
|
|
|
|
*
|
|
|
|
*/
|
2009-12-23 23:15:05 +08:00
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
2009-12-24 08:03:57 +08:00
|
|
|
void rt_soft_timer_tick_increase (void);
|
2009-12-23 23:15:05 +08:00
|
|
|
#endif
|
2010-04-14 08:40:20 +08:00
|
|
|
void rt_timer_check(void)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
|
|
|
struct rt_timer *t;
|
2010-05-23 07:26:08 +08:00
|
|
|
rt_tick_t current_tick;
|
2009-07-03 06:48:23 +08:00
|
|
|
register rt_base_t level;
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
#ifdef RT_TIMER_DEBUG
|
2009-07-03 06:48:23 +08:00
|
|
|
rt_kprintf("timer check enter\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
current_tick = rt_tick_get();
|
|
|
|
|
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
2010-05-23 07:26:08 +08:00
|
|
|
while (!rt_list_isempty(&rt_timer_list))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2010-05-23 07:26:08 +08:00
|
|
|
t = rt_list_entry(rt_timer_list.next, struct rt_timer, list);
|
2009-07-03 06:48:23 +08:00
|
|
|
if (current_tick >= t->timeout_tick)
|
|
|
|
{
|
|
|
|
#ifdef RT_USING_HOOK
|
|
|
|
if (rt_timer_timeout_hook != RT_NULL) rt_timer_timeout_hook(t);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* remove timer from timer list firstly */
|
|
|
|
rt_list_remove(&(t->list));
|
|
|
|
|
|
|
|
/* call timeout function */
|
|
|
|
t->timeout_func(t->parameter);
|
|
|
|
|
2010-05-23 07:26:08 +08:00
|
|
|
/* re-get tick */
|
2009-07-03 06:48:23 +08:00
|
|
|
current_tick = rt_tick_get();
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
#ifdef RT_TIMER_DEBUG
|
2009-07-03 06:48:23 +08:00
|
|
|
rt_kprintf("current tick: %d\n", current_tick);
|
|
|
|
#endif
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
|
|
|
|
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
|
|
|
/* start it */
|
2009-10-19 20:11:17 +08:00
|
|
|
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
2009-07-03 06:48:23 +08:00
|
|
|
rt_timer_start(t);
|
|
|
|
}
|
2009-10-19 20:11:17 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* stop timer */
|
|
|
|
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
2009-12-24 08:03:57 +08:00
|
|
|
/* increase soft timer tick */
|
2009-12-23 23:15:05 +08:00
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
2009-12-24 08:03:57 +08:00
|
|
|
rt_soft_timer_tick_increase ( );
|
2009-12-23 23:15:05 +08:00
|
|
|
#endif
|
|
|
|
|
2010-05-23 07:26:08 +08:00
|
|
|
#ifdef RT_TIMER_DEBUG
|
2009-07-03 06:48:23 +08:00
|
|
|
rt_kprintf("timer check leave\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
|
|
|
static struct rt_thread timer_thread;
|
|
|
|
static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
|
2009-12-22 23:06:27 +08:00
|
|
|
static struct rt_semaphore timer_sem;
|
|
|
|
|
2009-12-25 20:18:53 +08:00
|
|
|
static rt_uint16_t timer_ex_cnt;
|
2009-12-23 23:15:05 +08:00
|
|
|
|
2009-12-24 08:03:57 +08:00
|
|
|
void rt_soft_timer_tick_increase (void)
|
2009-12-23 23:15:05 +08:00
|
|
|
{
|
|
|
|
timer_ex_cnt++;
|
2009-12-25 20:18:53 +08:00
|
|
|
if (timer_ex_cnt >= (RT_TICK_PER_SECOND / RT_TIMER_TICK_PER_SECOND))
|
2009-12-23 23:15:05 +08:00
|
|
|
{
|
|
|
|
timer_ex_cnt = 0;
|
2009-12-24 08:03:57 +08:00
|
|
|
rt_sem_release(&timer_sem);
|
2009-12-23 23:15:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will check timer list, if a timeout event happens, the
|
|
|
|
* corresponding timeout function will be invoked.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void rt_soft_timer_check()
|
2009-12-22 23:06:27 +08:00
|
|
|
{
|
|
|
|
rt_tick_t current_tick;
|
|
|
|
rt_list_t *n;
|
|
|
|
struct rt_timer *t;
|
2009-12-23 21:52:42 +08:00
|
|
|
|
|
|
|
#ifdef RT_TIMER_DEBUG
|
|
|
|
rt_kprintf("software timer check enter\n");
|
2009-12-22 23:06:27 +08:00
|
|
|
#endif
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
current_tick = rt_tick_get();
|
|
|
|
|
2009-12-22 23:06:27 +08:00
|
|
|
for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list); )
|
|
|
|
{
|
|
|
|
t = rt_list_entry(n, struct rt_timer, list);
|
|
|
|
if (current_tick >= t->timeout_tick)
|
|
|
|
{
|
|
|
|
#ifdef RT_USING_HOOK
|
|
|
|
if (rt_timer_timeout_hook != RT_NULL) rt_timer_timeout_hook(t);
|
|
|
|
#endif
|
|
|
|
/* move node to the next */
|
|
|
|
n = n->next;
|
|
|
|
|
|
|
|
/* remove timer from timer list firstly */
|
|
|
|
rt_list_remove(&(t->list));
|
|
|
|
|
|
|
|
/* call timeout function */
|
|
|
|
t->timeout_func(t->parameter);
|
|
|
|
|
|
|
|
/* reget tick */
|
|
|
|
current_tick = rt_tick_get();
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
#ifdef RT_TIMER_DEBUG
|
2009-12-22 23:06:27 +08:00
|
|
|
rt_kprintf("current tick: %d\n", current_tick);
|
|
|
|
#endif
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
|
|
|
|
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
2009-12-22 23:06:27 +08:00
|
|
|
{
|
|
|
|
/* 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;
|
2009-12-23 21:52:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RT_TIMER_DEBUG
|
|
|
|
rt_kprintf("software timer check leave\n");
|
2009-12-22 23:06:27 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
/* system timer thread entry */
|
2009-12-22 23:06:27 +08:00
|
|
|
static void rt_thread_timer_entry(void* parameter)
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
2009-12-24 08:03:57 +08:00
|
|
|
/* take software timer semaphore */
|
2009-12-23 23:15:05 +08:00
|
|
|
rt_sem_take(&timer_sem,RT_WAITING_FOREVER);
|
2009-12-22 23:06:27 +08:00
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
/* lock scheduler */
|
2009-12-22 23:06:27 +08:00
|
|
|
rt_enter_critical();
|
2009-12-23 21:52:42 +08:00
|
|
|
|
|
|
|
/* check software timer */
|
|
|
|
rt_soft_timer_check();
|
|
|
|
|
|
|
|
/* unlock scheduler */
|
2009-12-22 23:06:27 +08:00
|
|
|
rt_exit_critical();
|
2009-12-23 21:52:42 +08:00
|
|
|
}
|
2009-12-22 23:06:27 +08:00
|
|
|
}
|
2009-12-23 21:52:42 +08:00
|
|
|
#endif
|
2009-12-22 23:06:27 +08:00
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
/**
|
|
|
|
* @ingroup SystemInit
|
|
|
|
*
|
|
|
|
* This function will init system timer
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void rt_system_timer_init()
|
|
|
|
{
|
|
|
|
rt_list_init(&rt_timer_list);
|
2010-05-23 07:26:08 +08:00
|
|
|
rt_list_init(&rt_long_timer_list);
|
2009-12-23 21:52:42 +08:00
|
|
|
|
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
|
|
|
rt_list_init(&rt_soft_timer_list);
|
2010-05-23 07:26:08 +08:00
|
|
|
rt_list_init(&rt_soft_long_timer_list);
|
2009-12-23 21:52:42 +08:00
|
|
|
rt_sem_init(&timer_sem, "timer", 0, RT_IPC_FLAG_FIFO);
|
2009-12-24 08:03:57 +08:00
|
|
|
#endif
|
|
|
|
}
|
2009-12-22 23:06:27 +08:00
|
|
|
|
2009-12-24 08:03:57 +08:00
|
|
|
/**
|
|
|
|
* @ingroup SystemInit
|
|
|
|
*
|
|
|
|
* This function will init system timer thread
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void rt_system_timer_thread_init()
|
|
|
|
{
|
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
2009-12-23 21:52:42 +08:00
|
|
|
/* 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);
|
2009-12-22 23:06:27 +08:00
|
|
|
|
|
|
|
/* startup */
|
2009-12-23 21:52:42 +08:00
|
|
|
rt_thread_startup(&timer_thread);
|
|
|
|
#endif
|
2009-12-22 23:06:27 +08:00
|
|
|
}
|
2009-12-23 21:52:42 +08:00
|
|
|
|
2009-07-03 06:48:23 +08:00
|
|
|
/*@}*/
|