2009-07-03 06:48:23 +08:00
|
|
|
/*
|
|
|
|
* File : timer.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
2012-03-17 14:43:49 +08:00
|
|
|
* COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2013-06-24 17:06:09 +08:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
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.
|
2010-11-26 07:44:58 +08:00
|
|
|
* 2010-11-02 Charlie re-implement tick overflow issue
|
2012-12-15 10:17:17 +08:00
|
|
|
* 2012-12-15 Bernard fix the next timeout issue in soft timer
|
2017-09-15 11:02:24 +08:00
|
|
|
* 2014-07-12 Bernard does not lock scheduler when invoking soft-timer
|
2014-07-12 15:59:45 +08:00
|
|
|
* timeout function.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rthw.h>
|
|
|
|
|
2009-12-22 23:06:27 +08:00
|
|
|
/* hard timer list */
|
2013-09-26 15:52:02 +08:00
|
|
|
static rt_list_t rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
2012-12-15 10:17:17 +08:00
|
|
|
#ifndef RT_TIMER_THREAD_STACK_SIZE
|
2012-12-18 16:34:46 +08:00
|
|
|
#define RT_TIMER_THREAD_STACK_SIZE 512
|
2012-12-15 10:17:17 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef RT_TIMER_THREAD_PRIO
|
2012-12-18 16:34:46 +08:00
|
|
|
#define RT_TIMER_THREAD_PRIO 0
|
2012-12-15 10:17:17 +08:00
|
|
|
#endif
|
|
|
|
|
2009-12-22 23:06:27 +08:00
|
|
|
/* soft timer list */
|
2013-09-26 15:52:02 +08:00
|
|
|
static rt_list_t rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
|
2012-05-05 09:45:17 +08:00
|
|
|
static struct rt_thread timer_thread;
|
|
|
|
ALIGN(RT_ALIGN_SIZE)
|
|
|
|
static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
|
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
|
2011-09-21 11:56:42 +08:00
|
|
|
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);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup Hook
|
|
|
|
*/
|
2012-03-17 14:43:49 +08:00
|
|
|
|
2016-08-19 10:19:46 +08:00
|
|
|
/**@{*/
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-15 10:17:17 +08:00
|
|
|
rt_timer_timeout_hook = hook;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
2016-08-19 10:19:46 +08:00
|
|
|
/**@}*/
|
2009-07-03 06:48:23 +08:00
|
|
|
#endif
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
static void _rt_timer_init(rt_timer_t timer,
|
2012-12-18 16:34:46 +08:00
|
|
|
void (*timeout)(void *parameter),
|
|
|
|
void *parameter,
|
|
|
|
rt_tick_t time,
|
|
|
|
rt_uint8_t flag)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2013-09-26 15:52:02 +08:00
|
|
|
int i;
|
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* set flag */
|
|
|
|
timer->parent.flag = flag;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* set deactivated */
|
|
|
|
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
timer->timeout_func = timeout;
|
|
|
|
timer->parameter = parameter;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
timer->timeout_tick = 0;
|
|
|
|
timer->init_tick = time;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* initialize timer list */
|
2013-09-26 15:52:02 +08:00
|
|
|
for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
|
|
|
|
{
|
|
|
|
rt_list_init(&(timer->row[i]));
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
2013-09-26 15:52:02 +08:00
|
|
|
/* the fist timer always in the last row */
|
|
|
|
static rt_tick_t rt_timer_list_next_timeout(rt_list_t timer_list[])
|
2012-05-05 09:45:17 +08:00
|
|
|
{
|
2012-12-15 10:17:17 +08:00
|
|
|
struct rt_timer *timer;
|
2012-06-02 16:40:29 +08:00
|
|
|
|
2013-09-26 15:52:02 +08:00
|
|
|
if (rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
|
2012-12-15 10:17:17 +08:00
|
|
|
return RT_TICK_MAX;
|
2013-06-24 17:06:09 +08:00
|
|
|
|
2013-09-26 15:52:02 +08:00
|
|
|
timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
|
|
|
|
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
|
2012-06-02 16:40:29 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
return timer->timeout_tick;
|
2012-05-05 09:45:17 +08:00
|
|
|
}
|
|
|
|
|
2013-09-26 15:52:02 +08:00
|
|
|
rt_inline void _rt_timer_remove(rt_timer_t timer)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
|
|
|
|
{
|
|
|
|
rt_list_remove(&timer->row[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-16 22:05:02 +08:00
|
|
|
#if RT_DEBUG_TIMER
|
2013-09-26 15:52:02 +08:00
|
|
|
static int rt_timer_count_height(struct rt_timer *timer)
|
|
|
|
{
|
|
|
|
int i, cnt = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
|
|
|
|
{
|
|
|
|
if (!rt_list_isempty(&timer->row[i]))
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rt_timer_dump(rt_list_t timer_heads[])
|
|
|
|
{
|
|
|
|
rt_list_t *list;
|
|
|
|
|
2017-09-15 11:02:24 +08:00
|
|
|
for (list = timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1].next;
|
|
|
|
list != &timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1];
|
2013-09-26 15:52:02 +08:00
|
|
|
list = list->next)
|
|
|
|
{
|
|
|
|
struct rt_timer *timer = rt_list_entry(list,
|
|
|
|
struct rt_timer,
|
2017-09-15 11:02:24 +08:00
|
|
|
row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
|
2013-09-26 15:52:02 +08:00
|
|
|
rt_kprintf("%d", rt_timer_count_height(timer));
|
|
|
|
}
|
|
|
|
rt_kprintf("\n");
|
|
|
|
}
|
2014-07-12 15:59:45 +08:00
|
|
|
#endif
|
2013-09-26 15:52:02 +08:00
|
|
|
|
2009-07-03 06:48:23 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup Clock
|
|
|
|
*/
|
2012-03-17 14:43:49 +08:00
|
|
|
|
2016-08-19 10:19:46 +08:00
|
|
|
/**@{*/
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
2012-05-16 14:18:58 +08:00
|
|
|
* This function will initialize a timer, normally this function is used to
|
|
|
|
* initialize a static timer object.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2012-12-18 16:34:46 +08:00
|
|
|
void rt_timer_init(rt_timer_t timer,
|
2012-12-15 10:17:17 +08:00
|
|
|
const char *name,
|
2012-12-18 16:34:46 +08:00
|
|
|
void (*timeout)(void *parameter),
|
|
|
|
void *parameter,
|
|
|
|
rt_tick_t time,
|
|
|
|
rt_uint8_t flag)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-15 10:17:17 +08:00
|
|
|
/* timer check */
|
|
|
|
RT_ASSERT(timer != RT_NULL);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* timer object initialization */
|
|
|
|
rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
_rt_timer_init(timer, timeout, parameter, time, flag);
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_timer_init);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
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)
|
|
|
|
{
|
2012-12-15 10:17:17 +08:00
|
|
|
register rt_base_t level;
|
2009-12-23 21:52:42 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* timer check */
|
|
|
|
RT_ASSERT(timer != RT_NULL);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2013-09-26 15:52:02 +08:00
|
|
|
_rt_timer_remove(timer);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
rt_object_detach((rt_object_t)timer);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
return -RT_EOK;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_timer_detach);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
#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
|
|
|
|
*/
|
2012-12-18 16:34:46 +08:00
|
|
|
rt_timer_t rt_timer_create(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
|
|
|
{
|
2012-12-15 10:17:17 +08:00
|
|
|
struct rt_timer *timer;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* allocate a object */
|
|
|
|
timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
|
|
|
|
if (timer == RT_NULL)
|
|
|
|
{
|
|
|
|
return RT_NULL;
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
_rt_timer_init(timer, timeout, parameter, time, flag);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
return timer;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_timer_create);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2012-12-15 10:17:17 +08:00
|
|
|
register rt_base_t level;
|
2009-12-23 21:52:42 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* timer check */
|
|
|
|
RT_ASSERT(timer != RT_NULL);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2013-09-26 15:52:02 +08:00
|
|
|
_rt_timer_remove(timer);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
rt_object_delete((rt_object_t)timer);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
return -RT_EOK;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_timer_delete);
|
2009-07-03 06:48:23 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will start the timer
|
|
|
|
*
|
|
|
|
* @param timer the timer to be started
|
|
|
|
*
|
2010-11-29 08:04:55 +08:00
|
|
|
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
|
|
|
rt_err_t rt_timer_start(rt_timer_t timer)
|
|
|
|
{
|
2014-10-14 16:00:42 +08:00
|
|
|
unsigned int row_lvl;
|
2013-09-26 15:52:02 +08:00
|
|
|
rt_list_t *timer_list;
|
2012-12-15 10:17:17 +08:00
|
|
|
register rt_base_t level;
|
2013-09-26 15:52:02 +08:00
|
|
|
rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
|
|
|
|
unsigned int tst_nr;
|
|
|
|
static unsigned int random_nr;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* timer check */
|
|
|
|
RT_ASSERT(timer != RT_NULL);
|
2014-07-12 15:59:45 +08:00
|
|
|
|
2017-09-15 11:02:24 +08:00
|
|
|
/* stop timer firstly */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
/* remove timer from list */
|
2014-07-12 15:59:45 +08:00
|
|
|
_rt_timer_remove(timer);
|
|
|
|
/* change status of timer */
|
|
|
|
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
|
|
|
rt_hw_interrupt_enable(level);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-18 16:34:46 +08:00
|
|
|
/*
|
|
|
|
* get timeout tick,
|
|
|
|
* the max timeout tick shall not great than RT_TICK_MAX/2
|
|
|
|
*/
|
|
|
|
RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
|
2012-12-15 10:17:17 +08:00
|
|
|
timer->timeout_tick = rt_tick_get() + timer->init_tick;
|
2010-05-23 07:26:08 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
2012-05-05 09:45:17 +08:00
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
2012-12-15 10:17:17 +08:00
|
|
|
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
|
|
|
|
{
|
|
|
|
/* insert timer to soft timer list */
|
2013-09-26 15:52:02 +08:00
|
|
|
timer_list = rt_soft_timer_list;
|
2012-12-15 10:17:17 +08:00
|
|
|
}
|
|
|
|
else
|
2009-12-23 21:52:42 +08:00
|
|
|
#endif
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
|
|
|
/* insert timer to system timer list */
|
2013-09-26 15:52:02 +08:00
|
|
|
timer_list = rt_timer_list;
|
2012-12-15 10:17:17 +08:00
|
|
|
}
|
|
|
|
|
2013-09-26 15:52:02 +08:00
|
|
|
row_head[0] = &timer_list[0];
|
|
|
|
for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
2017-09-15 11:02:24 +08:00
|
|
|
for (; row_head[row_lvl] != timer_list[row_lvl].prev;
|
2013-09-26 15:52:02 +08:00
|
|
|
row_head[row_lvl] = row_head[row_lvl]->next)
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
2013-09-26 15:52:02 +08:00
|
|
|
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;
|
|
|
|
}
|
2012-12-15 10:17:17 +08:00
|
|
|
}
|
2013-09-26 15:52:02 +08:00
|
|
|
if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
|
2017-09-15 11:02:24 +08:00
|
|
|
row_head[row_lvl + 1] = row_head[row_lvl] + 1;
|
2012-12-15 10:17:17 +08:00
|
|
|
}
|
2013-09-26 15:52:02 +08:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2017-09-15 11:02:24 +08:00
|
|
|
rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],
|
|
|
|
&(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
|
2013-09-26 15:52:02 +08:00
|
|
|
for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
2013-09-26 15:52:02 +08:00
|
|
|
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. */
|
2017-09-15 11:02:24 +08:00
|
|
|
tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;
|
2012-12-15 10:17:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-05-05 09:45:17 +08:00
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
2012-12-15 10:17:17 +08:00
|
|
|
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
|
|
|
|
{
|
|
|
|
/* 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();
|
|
|
|
}
|
|
|
|
}
|
2012-05-05 09:45:17 +08:00
|
|
|
#endif
|
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
return -RT_EOK;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_timer_start);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will stop the timer
|
|
|
|
*
|
|
|
|
* @param timer the timer to be stopped
|
|
|
|
*
|
2010-11-29 08:04:55 +08:00
|
|
|
* @return the operation status, RT_EOK on OK, -RT_ERROR on error
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
|
|
|
rt_err_t rt_timer_stop(rt_timer_t timer)
|
|
|
|
{
|
2012-12-15 10:17:17 +08:00
|
|
|
register rt_base_t level;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* timer check */
|
|
|
|
RT_ASSERT(timer != RT_NULL);
|
|
|
|
if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
|
|
|
return -RT_ERROR;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2013-09-26 15:52:02 +08:00
|
|
|
_rt_timer_remove(timer);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* change stat */
|
|
|
|
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
2012-05-05 09:45:17 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
return RT_EOK;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_timer_stop);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2010-11-29 08:04:55 +08:00
|
|
|
* @return RT_EOK
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2017-10-15 22:31:53 +08:00
|
|
|
rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-15 10:17:17 +08:00
|
|
|
/* timer check */
|
|
|
|
RT_ASSERT(timer != RT_NULL);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case RT_TIMER_CTRL_GET_TIME:
|
|
|
|
*(rt_tick_t *)arg = timer->init_tick;
|
|
|
|
break;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
case RT_TIMER_CTRL_SET_TIME:
|
|
|
|
timer->init_tick = *(rt_tick_t *)arg;
|
|
|
|
break;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
case RT_TIMER_CTRL_SET_ONESHOT:
|
|
|
|
timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
|
|
|
|
break;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
case RT_TIMER_CTRL_SET_PERIODIC:
|
|
|
|
timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
|
|
|
|
break;
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
return RT_EOK;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
2012-08-27 09:21:57 +08:00
|
|
|
RTM_EXPORT(rt_timer_control);
|
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.
|
|
|
|
*
|
2010-11-29 08:04:55 +08:00
|
|
|
* @note this function shall be invoked in operating system timer interrupt.
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2010-04-14 08:40:20 +08:00
|
|
|
void rt_timer_check(void)
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2012-12-15 10:17:17 +08:00
|
|
|
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();
|
|
|
|
|
2017-09-15 11:02:24 +08:00
|
|
|
while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
2013-09-26 15:52:02 +08:00
|
|
|
t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
|
|
|
|
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
|
2012-12-15 10:17:17 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* It supposes that the new tick shall less than the half duration of
|
|
|
|
* tick max.
|
|
|
|
*/
|
2017-09-15 11:02:24 +08:00
|
|
|
if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
|
|
|
RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
|
|
|
|
|
|
|
|
/* remove timer from timer list firstly */
|
2013-09-26 15:52:02 +08:00
|
|
|
_rt_timer_remove(t);
|
2012-12-15 10:17:17 +08:00
|
|
|
|
|
|
|
/* 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) &&
|
2012-12-18 16:34:46 +08:00
|
|
|
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
2012-12-15 10:17:17 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
2012-05-05 09:45:17 +08:00
|
|
|
/**
|
|
|
|
* This function will return the next timeout tick in the system.
|
|
|
|
*
|
|
|
|
* @return the next timeout tick in the system
|
|
|
|
*/
|
|
|
|
rt_tick_t rt_timer_next_timeout_tick(void)
|
2009-12-23 23:15:05 +08:00
|
|
|
{
|
2013-09-26 15:52:02 +08:00
|
|
|
return rt_timer_list_next_timeout(rt_timer_list);
|
2009-12-23 23:15:05 +08:00
|
|
|
}
|
|
|
|
|
2012-05-05 09:45:17 +08:00
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
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.
|
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_soft_timer_check(void)
|
2009-12-22 23:06:27 +08:00
|
|
|
{
|
2012-12-15 10:17:17 +08:00
|
|
|
rt_tick_t current_tick;
|
|
|
|
rt_list_t *n;
|
|
|
|
struct rt_timer *t;
|
|
|
|
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
|
|
|
|
|
|
|
|
current_tick = rt_tick_get();
|
|
|
|
|
2017-09-15 11:02:24 +08:00
|
|
|
/* lock scheduler */
|
|
|
|
rt_enter_critical();
|
2014-07-12 15:59:45 +08:00
|
|
|
|
2017-09-15 11:02:24 +08:00
|
|
|
for (n = rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next;
|
|
|
|
n != &(rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]);)
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
2017-09-15 11:02:24 +08:00
|
|
|
t = rt_list_entry(n, struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
|
2012-12-15 10:17:17 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* It supposes that the new tick shall less than the half duration of
|
|
|
|
* tick max.
|
|
|
|
*/
|
2012-12-18 16:34:46 +08:00
|
|
|
if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
|
|
|
RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
|
|
|
|
|
|
|
|
/* move node to the next */
|
|
|
|
n = n->next;
|
|
|
|
|
|
|
|
/* remove timer from timer list firstly */
|
2013-10-19 21:07:14 +08:00
|
|
|
_rt_timer_remove(t);
|
2012-12-15 10:17:17 +08:00
|
|
|
|
2017-09-15 11:02:24 +08:00
|
|
|
/* not lock scheduler when performing timeout function */
|
|
|
|
rt_exit_critical();
|
2012-12-15 10:17:17 +08:00
|
|
|
/* 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));
|
|
|
|
|
2017-09-15 11:02:24 +08:00
|
|
|
/* lock scheduler */
|
|
|
|
rt_enter_critical();
|
2014-07-12 15:59:45 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
|
2012-12-18 16:34:46 +08:00
|
|
|
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
2012-12-15 10:17:17 +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; /* not check anymore */
|
|
|
|
}
|
|
|
|
|
2017-09-15 11:02:24 +08:00
|
|
|
/* unlock scheduler */
|
|
|
|
rt_exit_critical();
|
2014-07-12 15:59:45 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
|
2009-12-22 23:06:27 +08:00
|
|
|
}
|
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
/* system timer thread entry */
|
2011-09-21 11:56:42 +08:00
|
|
|
static void rt_thread_timer_entry(void *parameter)
|
2009-12-22 23:06:27 +08:00
|
|
|
{
|
2012-12-15 10:17:17 +08:00
|
|
|
rt_tick_t next_timeout;
|
2013-06-24 17:06:09 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* get the next timeout tick */
|
2013-09-26 15:52:02 +08:00
|
|
|
next_timeout = rt_timer_list_next_timeout(rt_soft_timer_list);
|
2012-12-15 10:17:17 +08:00
|
|
|
if (next_timeout == RT_TICK_MAX)
|
|
|
|
{
|
|
|
|
/* no software timer exist, suspend self. */
|
|
|
|
rt_thread_suspend(rt_thread_self());
|
|
|
|
rt_schedule();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_tick_t current_tick;
|
|
|
|
|
|
|
|
/* get current tick */
|
|
|
|
current_tick = rt_tick_get();
|
|
|
|
|
2017-09-15 11:02:24 +08:00
|
|
|
if ((next_timeout - current_tick) < RT_TICK_MAX / 2)
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
|
|
|
/* get the delta timeout tick */
|
|
|
|
next_timeout = next_timeout - current_tick;
|
|
|
|
rt_thread_delay(next_timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check software timer */
|
|
|
|
rt_soft_timer_check();
|
|
|
|
}
|
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
|
|
|
|
*
|
2010-11-26 07:44:58 +08:00
|
|
|
* This function will initialize system timer
|
2009-12-23 21:52:42 +08:00
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_system_timer_init(void)
|
2009-12-23 21:52:42 +08:00
|
|
|
{
|
2013-09-26 15:52:02 +08:00
|
|
|
int i;
|
|
|
|
|
2017-09-15 11:02:24 +08:00
|
|
|
for (i = 0; i < sizeof(rt_timer_list) / sizeof(rt_timer_list[0]); i++)
|
2013-09-26 15:52:02 +08:00
|
|
|
{
|
2017-09-15 11:02:24 +08:00
|
|
|
rt_list_init(rt_timer_list + i);
|
2013-09-26 15:52:02 +08:00
|
|
|
}
|
2009-12-24 08:03:57 +08:00
|
|
|
}
|
2009-12-22 23:06:27 +08:00
|
|
|
|
2009-12-24 08:03:57 +08:00
|
|
|
/**
|
|
|
|
* @ingroup SystemInit
|
|
|
|
*
|
2010-11-26 07:44:58 +08:00
|
|
|
* This function will initialize system timer thread
|
2009-12-24 08:03:57 +08:00
|
|
|
*/
|
2011-09-21 11:56:42 +08:00
|
|
|
void rt_system_timer_thread_init(void)
|
2009-12-24 08:03:57 +08:00
|
|
|
{
|
|
|
|
#ifdef RT_USING_TIMER_SOFT
|
2013-09-26 15:52:02 +08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0;
|
2017-09-15 11:02:24 +08:00
|
|
|
i < sizeof(rt_soft_timer_list) / sizeof(rt_soft_timer_list[0]);
|
2013-09-26 15:52:02 +08:00
|
|
|
i++)
|
|
|
|
{
|
2017-09-15 11:02:24 +08:00
|
|
|
rt_list_init(rt_soft_timer_list + i);
|
2013-09-26 15:52:02 +08:00
|
|
|
}
|
2012-04-14 11:51:34 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* start software timer thread */
|
|
|
|
rt_thread_init(&timer_thread,
|
|
|
|
"timer",
|
2012-12-18 16:34:46 +08:00
|
|
|
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
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* startup */
|
|
|
|
rt_thread_startup(&timer_thread);
|
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
|
|
|
|
2016-08-19 10:19:46 +08:00
|
|
|
/**@}*/
|