2009-07-03 06:48:23 +08:00
|
|
|
/*
|
2022-04-21 09:35:17 +08:00
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
2013-06-24 17:06:09 +08:00
|
|
|
*
|
2018-09-14 22:37:43 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
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.
|
2021-08-16 22:07:46 +08:00
|
|
|
* 2021-08-15 supperthomas add the comment
|
2022-01-08 07:35:44 +08:00
|
|
|
* 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to timer.c
|
2022-04-21 09:35:17 +08:00
|
|
|
* 2022-04-19 Stanley Correct descriptions
|
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 */
|
2021-08-17 17:56:39 +08:00
|
|
|
static rt_list_t _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
|
2020-09-30 23:50:18 +08:00
|
|
|
|
|
|
|
#define RT_SOFT_TIMER_IDLE 1
|
|
|
|
#define RT_SOFT_TIMER_BUSY 0
|
|
|
|
|
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
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_TIMER_THREAD_STACK_SIZE */
|
2012-12-15 10:17:17 +08:00
|
|
|
|
|
|
|
#ifndef RT_TIMER_THREAD_PRIO
|
2012-12-18 16:34:46 +08:00
|
|
|
#define RT_TIMER_THREAD_PRIO 0
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_TIMER_THREAD_PRIO */
|
2012-12-15 10:17:17 +08:00
|
|
|
|
2020-09-30 23:50:18 +08:00
|
|
|
/* soft timer status */
|
2021-08-17 17:56:39 +08:00
|
|
|
static rt_uint8_t _soft_timer_status = RT_SOFT_TIMER_IDLE;
|
2009-12-22 23:06:27 +08:00
|
|
|
/* soft timer list */
|
2021-08-17 17:56:39 +08:00
|
|
|
static rt_list_t _soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
|
|
|
|
static struct rt_thread _timer_thread;
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_align(RT_ALIGN_SIZE)
|
2021-08-17 17:56:39 +08:00
|
|
|
static rt_uint8_t _timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_TIMER_SOFT */
|
2009-12-22 23:06:27 +08:00
|
|
|
|
2022-01-08 07:35:44 +08:00
|
|
|
#ifndef __on_rt_object_take_hook
|
|
|
|
#define __on_rt_object_take_hook(parent) __ON_HOOK_ARGS(rt_object_take_hook, (parent))
|
|
|
|
#endif
|
|
|
|
#ifndef __on_rt_object_put_hook
|
|
|
|
#define __on_rt_object_put_hook(parent) __ON_HOOK_ARGS(rt_object_put_hook, (parent))
|
|
|
|
#endif
|
|
|
|
#ifndef __on_rt_timer_enter_hook
|
|
|
|
#define __on_rt_timer_enter_hook(t) __ON_HOOK_ARGS(rt_timer_enter_hook, (t))
|
|
|
|
#endif
|
|
|
|
#ifndef __on_rt_timer_exit_hook
|
|
|
|
#define __on_rt_timer_exit_hook(t) __ON_HOOK_ARGS(rt_timer_exit_hook, (t))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
|
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);
|
2018-11-13 11:32:01 +08:00
|
|
|
static void (*rt_timer_enter_hook)(struct rt_timer *timer);
|
|
|
|
static void (*rt_timer_exit_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
|
|
|
|
|
|
|
/**
|
2021-08-16 22:07:46 +08:00
|
|
|
* @brief This function will set a hook function on timer,
|
2021-08-28 23:31:35 +08:00
|
|
|
* which will be invoked when enter timer timeout callback function.
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param hook is the function point of timer
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
2018-11-13 11:32:01 +08:00
|
|
|
void rt_timer_enter_sethook(void (*hook)(struct rt_timer *timer))
|
2009-07-03 06:48:23 +08:00
|
|
|
{
|
2018-11-13 11:32:01 +08:00
|
|
|
rt_timer_enter_hook = hook;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-08-16 22:07:46 +08:00
|
|
|
* @brief This function will set a hook function, which will be
|
2021-08-28 23:31:35 +08:00
|
|
|
* invoked when exit timer timeout callback function.
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param hook is the function point of timer
|
2018-11-13 11:32:01 +08:00
|
|
|
*/
|
|
|
|
void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
|
|
|
|
{
|
|
|
|
rt_timer_exit_hook = hook;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
2016-08-19 10:19:46 +08:00
|
|
|
/**@}*/
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_HOOK */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2021-08-15 23:04:35 +08:00
|
|
|
|
|
|
|
/**
|
2021-08-28 23:31:35 +08:00
|
|
|
* @brief [internal] The init funtion of timer
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2021-08-28 23:31:35 +08:00
|
|
|
* The internal called function of rt_timer_init
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2021-08-15 23:04:35 +08:00
|
|
|
* @see rt_timer_init
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param timer is timer object
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param timeout is the timeout function
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param parameter is the parameter of timeout function
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param time is the tick of timer
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param flag the flag of timer
|
2021-08-15 23:04:35 +08:00
|
|
|
*/
|
2021-08-17 17:56:39 +08:00
|
|
|
static void _timer_init(rt_timer_t timer,
|
2022-04-29 08:59:53 +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
|
|
|
}
|
|
|
|
|
2021-08-15 23:04:35 +08:00
|
|
|
/**
|
2021-08-28 23:31:35 +08:00
|
|
|
* @brief Find the next emtpy timer ticks
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param timer_list is the array of time list
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2022-03-07 11:53:34 +08:00
|
|
|
* @param timeout_tick is the next timer's ticks
|
|
|
|
*
|
|
|
|
* @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
|
|
|
|
* If the return value is any other values, it means this operation failed.
|
2021-08-15 23:04:35 +08:00
|
|
|
*/
|
2022-03-07 11:53:34 +08:00
|
|
|
static rt_err_t _timer_list_next_timeout(rt_list_t timer_list[], rt_tick_t *timeout_tick)
|
2012-05-05 09:45:17 +08:00
|
|
|
{
|
2012-12-15 10:17:17 +08:00
|
|
|
struct rt_timer *timer;
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2012-06-02 16:40:29 +08:00
|
|
|
|
2020-01-15 09:34:47 +08:00
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
2013-06-24 17:06:09 +08:00
|
|
|
|
2020-01-15 09:34:47 +08:00
|
|
|
if (!rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
|
|
|
|
{
|
|
|
|
timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
|
|
|
|
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
|
2022-03-07 11:53:34 +08:00
|
|
|
*timeout_tick = timer->timeout_tick;
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
return RT_EOK;
|
2020-01-15 09:34:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
2012-06-02 16:40:29 +08:00
|
|
|
|
2022-03-07 11:53:34 +08:00
|
|
|
return -RT_ERROR;
|
2012-05-05 09:45:17 +08:00
|
|
|
}
|
|
|
|
|
2021-08-15 23:04:35 +08:00
|
|
|
/**
|
2021-08-28 23:31:35 +08:00
|
|
|
* @brief Remove the timer
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param timer the point of the timer
|
2021-08-15 23:04:35 +08:00
|
|
|
*/
|
2021-08-17 17:56:39 +08:00
|
|
|
rt_inline void _timer_remove(rt_timer_t timer)
|
2013-09-26 15:52:02 +08:00
|
|
|
{
|
|
|
|
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
|
2021-08-15 23:04:35 +08:00
|
|
|
/**
|
2021-08-28 23:31:35 +08:00
|
|
|
* @brief The number of timer
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2021-08-28 23:31:35 +08:00
|
|
|
* @param timer the head of timer
|
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @return count of timer
|
2021-08-15 23:04:35 +08:00
|
|
|
*/
|
2021-08-17 17:56:39 +08:00
|
|
|
static int _timer_count_height(struct rt_timer *timer)
|
2013-09-26 15:52:02 +08:00
|
|
|
{
|
|
|
|
int i, cnt = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
|
|
|
|
{
|
|
|
|
if (!rt_list_isempty(&timer->row[i]))
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
2021-08-15 23:04:35 +08:00
|
|
|
/**
|
|
|
|
* @brief dump the all timer information
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2021-08-15 23:04:35 +08:00
|
|
|
* @param timer_heads the head of timer
|
|
|
|
*/
|
2013-09-26 15:52:02 +08:00
|
|
|
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]);
|
2021-08-17 17:56:39 +08:00
|
|
|
rt_kprintf("%d", _timer_count_height(timer));
|
2013-09-26 15:52:02 +08:00
|
|
|
}
|
|
|
|
rt_kprintf("\n");
|
|
|
|
}
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_DEBUG_TIMER */
|
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
|
|
|
|
|
|
|
/**
|
2021-08-15 23:04:35 +08:00
|
|
|
* @brief This function will initialize a timer
|
|
|
|
* normally this function is used to initialize a static timer object.
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param timer is the point of timer
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param name is a pointer to the name of the timer
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param timeout is the callback of timer
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param parameter is the param of the callback
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2022-01-17 03:33:41 +08:00
|
|
|
* @param time is timeout ticks of timer
|
|
|
|
*
|
|
|
|
* NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1).
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param flag is the flag of timer
|
2022-01-17 03:33:41 +08:00
|
|
|
*
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
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
|
|
|
{
|
2022-01-15 01:24:12 +08:00
|
|
|
/* parameter check */
|
2012-12-15 10:17:17 +08:00
|
|
|
RT_ASSERT(timer != RT_NULL);
|
2022-01-17 03:33:41 +08:00
|
|
|
RT_ASSERT(timeout != RT_NULL);
|
2022-01-17 04:15:08 +08:00
|
|
|
RT_ASSERT(time < RT_TICK_MAX / 2);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* timer object initialization */
|
2021-02-28 12:00:44 +08:00
|
|
|
rt_object_init(&(timer->parent), RT_Object_Class_Timer, name);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2021-08-17 17:56:39 +08:00
|
|
|
_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
|
|
|
/**
|
2021-08-15 23:04:35 +08:00
|
|
|
* @brief This function will detach a timer from timer management.
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param timer is the timer to be detached
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @return the status of detach
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
|
|
|
rt_err_t rt_timer_detach(rt_timer_t timer)
|
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2009-12-23 21:52:42 +08:00
|
|
|
|
2022-01-15 01:24:12 +08:00
|
|
|
/* parameter check */
|
2012-12-15 10:17:17 +08:00
|
|
|
RT_ASSERT(timer != RT_NULL);
|
2018-07-07 13:57:36 +08:00
|
|
|
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
|
|
|
|
RT_ASSERT(rt_object_is_systemobject(&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
|
|
|
|
2021-08-17 17:56:39 +08:00
|
|
|
_timer_remove(timer);
|
2020-09-30 23:50:18 +08:00
|
|
|
/* stop timer */
|
|
|
|
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
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
|
|
|
|
2021-02-26 10:38:31 +08:00
|
|
|
rt_object_detach(&(timer->parent));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-08-23 15:57:45 +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
|
|
|
|
/**
|
2021-08-15 23:04:35 +08:00
|
|
|
* @brief This function will create a timer
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2022-01-17 03:33:41 +08:00
|
|
|
* @param name is the name of timer
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2022-01-17 03:33:41 +08:00
|
|
|
* @param timeout is the timeout function
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2022-01-17 03:33:41 +08:00
|
|
|
* @param parameter is the parameter of timeout function
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2022-01-17 03:33:41 +08:00
|
|
|
* @param time is timeout ticks of the timer
|
2021-08-28 23:31:35 +08:00
|
|
|
*
|
2022-04-21 09:35:17 +08:00
|
|
|
* NOTE: The max timeout tick should be no more than (RT_TICK_MAX/2 - 1).
|
2022-01-17 03:33:41 +08:00
|
|
|
*
|
2022-04-21 09:35:17 +08:00
|
|
|
* @param flag is the flag of timer. Timer will invoke the timeout function according to the selected values of flag, if one or more of the following flags is set.
|
|
|
|
*
|
|
|
|
* RT_TIMER_FLAG_ONE_SHOT One shot timing
|
|
|
|
* RT_TIMER_FLAG_PERIODIC Periodic timing
|
|
|
|
*
|
|
|
|
* RT_TIMER_FLAG_HARD_TIMER Hardware timer
|
|
|
|
* RT_TIMER_FLAG_SOFT_TIMER Software timer
|
|
|
|
*
|
|
|
|
* NOTE:
|
|
|
|
* You can use multiple values with "|" logical operator. By default, system will use the RT_TIME_FLAG_HARD_TIMER.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @return the created timer object
|
2009-07-03 06:48:23 +08:00
|
|
|
*/
|
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
|
|
|
|
2022-01-17 03:33:41 +08:00
|
|
|
/* parameter check */
|
|
|
|
RT_ASSERT(timeout != RT_NULL);
|
2022-01-17 04:15:08 +08:00
|
|
|
RT_ASSERT(time < RT_TICK_MAX / 2);
|
2022-01-17 03:33:41 +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
|
|
|
|
2021-08-17 17:56:39 +08:00
|
|
|
_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
|
|
|
|
|
|
|
/**
|
2021-08-15 23:04:35 +08:00
|
|
|
* @brief This function will delete a timer and release timer memory
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param timer the timer to be deleted
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2023-03-17 12:12:16 +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_delete(rt_timer_t timer)
|
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2009-12-23 21:52:42 +08:00
|
|
|
|
2022-01-15 01:24:12 +08:00
|
|
|
/* parameter check */
|
2012-12-15 10:17:17 +08:00
|
|
|
RT_ASSERT(timer != RT_NULL);
|
2018-07-07 13:57:36 +08:00
|
|
|
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
|
|
|
|
RT_ASSERT(rt_object_is_systemobject(&timer->parent) == RT_FALSE);
|
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
|
|
|
|
2021-08-17 17:56:39 +08:00
|
|
|
_timer_remove(timer);
|
2020-09-30 23:50:18 +08:00
|
|
|
/* stop timer */
|
|
|
|
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
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
|
|
|
|
2021-02-28 12:00:44 +08:00
|
|
|
rt_object_delete(&(timer->parent));
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-08-23 15:57:45 +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);
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_HEAP */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/**
|
2021-08-15 23:04:35 +08:00
|
|
|
* @brief This function will start the timer
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param timer the timer to be started
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +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;
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2022-04-29 06:34:42 +08:00
|
|
|
rt_bool_t need_schedule;
|
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
|
|
|
|
2022-01-15 01:24:12 +08:00
|
|
|
/* parameter check */
|
2012-12-15 10:17:17 +08:00
|
|
|
RT_ASSERT(timer != RT_NULL);
|
2018-07-07 13:57:36 +08:00
|
|
|
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
|
2014-07-12 15:59:45 +08:00
|
|
|
|
2021-08-31 10:39:44 +08:00
|
|
|
need_schedule = RT_FALSE;
|
|
|
|
|
2017-09-15 11:02:24 +08:00
|
|
|
/* stop timer firstly */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
/* remove timer from list */
|
2021-08-17 17:56:39 +08:00
|
|
|
_timer_remove(timer);
|
2014-07-12 15:59:45 +08:00
|
|
|
/* change status of timer */
|
|
|
|
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
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-15 10:17:17 +08:00
|
|
|
timer->timeout_tick = rt_tick_get() + timer->init_tick;
|
2010-05-23 07:26:08 +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 */
|
2021-08-17 17:56:39 +08:00
|
|
|
timer_list = _soft_timer_list;
|
2012-12-15 10:17:17 +08:00
|
|
|
}
|
|
|
|
else
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_TIMER_SOFT */
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
|
|
|
/* insert timer to system timer list */
|
2021-08-17 17:56:39 +08:00
|
|
|
timer_list = _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;
|
|
|
|
|
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 */
|
2021-08-17 17:56:39 +08:00
|
|
|
if ((_soft_timer_status == RT_SOFT_TIMER_IDLE) &&
|
2022-12-03 12:07:44 +08:00
|
|
|
((_timer_thread.stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK))
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
|
|
|
/* resume timer thread to check soft timer */
|
2021-08-17 17:56:39 +08:00
|
|
|
rt_thread_resume(&_timer_thread);
|
2021-08-31 10:39:44 +08:00
|
|
|
need_schedule = RT_TRUE;
|
2012-12-15 10:17:17 +08:00
|
|
|
}
|
|
|
|
}
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_TIMER_SOFT */
|
2012-05-05 09:45:17 +08:00
|
|
|
|
2021-08-31 10:39:44 +08:00
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
|
|
if (need_schedule)
|
|
|
|
{
|
|
|
|
rt_schedule();
|
|
|
|
}
|
|
|
|
|
2018-08-23 15:57:45 +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
|
|
|
|
|
|
|
/**
|
2021-08-15 23:04:35 +08:00
|
|
|
* @brief This function will stop the timer
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param timer the timer to be stopped
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +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)
|
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
|
|
|
/* timer check */
|
2012-12-15 10:17:17 +08:00
|
|
|
RT_ASSERT(timer != RT_NULL);
|
2018-07-07 13:57:36 +08:00
|
|
|
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
|
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
2022-12-03 12:07:44 +08:00
|
|
|
{
|
|
|
|
rt_hw_interrupt_enable(level);
|
2012-12-15 10:17:17 +08:00
|
|
|
return -RT_ERROR;
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|
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
|
|
|
|
2021-08-17 17:56:39 +08:00
|
|
|
_timer_remove(timer);
|
2020-09-30 23:50:18 +08:00
|
|
|
/* change status */
|
|
|
|
timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
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
|
|
|
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
|
|
|
|
|
|
|
/**
|
2021-08-15 23:04:35 +08:00
|
|
|
* @brief This function will get or set some options of the timer
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param timer the timer to be get or set
|
|
|
|
* @param cmd the control command
|
|
|
|
* @param arg the argument
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @return the statu of control
|
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
|
|
|
{
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2020-09-30 23:50:18 +08:00
|
|
|
|
2022-01-15 01:24:12 +08:00
|
|
|
/* parameter check */
|
2012-12-15 10:17:17 +08:00
|
|
|
RT_ASSERT(timer != RT_NULL);
|
2018-07-07 13:57:36 +08:00
|
|
|
RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2020-09-30 23:50:18 +08:00
|
|
|
level = rt_hw_interrupt_disable();
|
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:
|
2022-03-07 11:53:34 +08:00
|
|
|
RT_ASSERT((*(rt_tick_t *)arg) < RT_TICK_MAX / 2);
|
2012-12-15 10:17:17 +08:00
|
|
|
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;
|
2020-04-10 00:02:24 +08:00
|
|
|
|
2020-02-07 16:43:39 +08:00
|
|
|
case RT_TIMER_CTRL_GET_STATE:
|
|
|
|
if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
|
|
|
|
{
|
|
|
|
/*timer is start and run*/
|
2021-11-25 15:31:43 +08:00
|
|
|
*(rt_uint32_t *)arg = RT_TIMER_FLAG_ACTIVATED;
|
2020-02-07 16:43:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*timer is stop*/
|
2021-11-25 15:31:43 +08:00
|
|
|
*(rt_uint32_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
|
2020-02-07 16:43:39 +08:00
|
|
|
}
|
2022-05-28 08:32:11 +08:00
|
|
|
break;
|
|
|
|
|
2022-01-20 20:53:47 +08:00
|
|
|
case RT_TIMER_CTRL_GET_REMAIN_TIME:
|
|
|
|
*(rt_tick_t *)arg = timer->timeout_tick;
|
2020-07-27 11:49:52 +08:00
|
|
|
break;
|
2022-12-03 12:07:44 +08:00
|
|
|
case RT_TIMER_CTRL_GET_FUNC:
|
2023-03-01 11:38:51 +08:00
|
|
|
*(void **)arg = timer->timeout_func;
|
2022-12-03 12:07:44 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_TIMER_CTRL_SET_FUNC:
|
|
|
|
timer->timeout_func = (void (*)(void*))arg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_TIMER_CTRL_GET_PARM:
|
|
|
|
*(void **)arg = timer->parameter;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RT_TIMER_CTRL_SET_PARM:
|
|
|
|
timer->parameter = arg;
|
|
|
|
break;
|
2020-07-27 11:49:52 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2012-12-15 10:17:17 +08:00
|
|
|
}
|
2020-09-30 23:50:18 +08:00
|
|
|
rt_hw_interrupt_enable(level);
|
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
|
|
|
|
|
|
|
/**
|
2021-08-16 22:07:46 +08:00
|
|
|
* @brief This function will check timer list, if a timeout event happens,
|
2021-08-15 23:04:35 +08:00
|
|
|
* the corresponding timeout function will be invoked.
|
2009-07-03 06:48:23 +08:00
|
|
|
*
|
2021-08-28 23:31:35 +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;
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2021-05-25 18:28:30 +08:00
|
|
|
rt_list_t list;
|
|
|
|
|
|
|
|
rt_list_init(&list);
|
2012-12-15 10:17:17 +08:00
|
|
|
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
|
|
|
|
|
|
|
|
current_tick = rt_tick_get();
|
|
|
|
|
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
2021-08-17 17:56:39 +08:00
|
|
|
while (!rt_list_isempty(&_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
2021-08-17 17:56:39 +08:00
|
|
|
t = rt_list_entry(_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
|
2013-09-26 15:52:02 +08:00
|
|
|
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
|
|
|
{
|
2018-11-13 11:32:01 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
|
2012-12-15 10:17:17 +08:00
|
|
|
|
|
|
|
/* remove timer from timer list firstly */
|
2021-08-17 17:56:39 +08:00
|
|
|
_timer_remove(t);
|
2020-09-30 23:50:18 +08:00
|
|
|
if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
|
|
|
|
{
|
|
|
|
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
|
|
|
}
|
|
|
|
/* add timer to temporary list */
|
|
|
|
rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
|
2012-12-15 10:17:17 +08:00
|
|
|
/* call timeout function */
|
|
|
|
t->timeout_func(t->parameter);
|
|
|
|
|
|
|
|
/* re-get tick */
|
|
|
|
current_tick = rt_tick_get();
|
|
|
|
|
2018-11-13 11:32:01 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
|
2012-12-15 10:17:17 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
|
|
|
|
|
2020-09-30 23:50:18 +08:00
|
|
|
/* Check whether the timer object is detached or started again */
|
|
|
|
if (rt_list_isempty(&list))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-03 09:43:09 +08:00
|
|
|
rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-09-30 23:50:18 +08:00
|
|
|
else break;
|
2012-12-15 10:17:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
/**
|
2021-08-15 23:04:35 +08:00
|
|
|
* @brief This function will return the next timeout tick in the system.
|
2012-05-05 09:45:17 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @return the next timeout tick in the system
|
2012-05-05 09:45:17 +08:00
|
|
|
*/
|
|
|
|
rt_tick_t rt_timer_next_timeout_tick(void)
|
2009-12-23 23:15:05 +08:00
|
|
|
{
|
2022-03-07 11:53:34 +08:00
|
|
|
rt_tick_t next_timeout = RT_TICK_MAX;
|
|
|
|
_timer_list_next_timeout(_timer_list, &next_timeout);
|
|
|
|
return next_timeout;
|
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
|
|
|
/**
|
2021-08-15 23:04:35 +08:00
|
|
|
* @brief This function will check software-timer list, if a timeout event happens, the
|
2021-08-28 23:31:35 +08:00
|
|
|
* corresponding timeout function will be invoked.
|
2009-12-23 23:15:05 +08:00
|
|
|
*/
|
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;
|
|
|
|
struct rt_timer *t;
|
2022-04-20 10:56:11 +08:00
|
|
|
rt_base_t level;
|
2021-05-25 18:28:30 +08:00
|
|
|
rt_list_t list;
|
|
|
|
|
|
|
|
rt_list_init(&list);
|
2012-12-15 10:17:17 +08:00
|
|
|
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
|
|
|
|
|
2020-09-30 23:50:18 +08:00
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
2014-07-12 15:59:45 +08:00
|
|
|
|
2021-08-17 17:56:39 +08:00
|
|
|
while (!rt_list_isempty(&_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
2021-08-17 17:56:39 +08:00
|
|
|
t = rt_list_entry(_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
|
2020-08-15 20:15:29 +08:00
|
|
|
struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
|
|
|
|
|
|
|
|
current_tick = rt_tick_get();
|
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
|
|
|
{
|
2018-11-13 11:32:01 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
|
2012-12-15 10:17:17 +08:00
|
|
|
|
|
|
|
/* remove timer from timer list firstly */
|
2021-08-17 17:56:39 +08:00
|
|
|
_timer_remove(t);
|
2020-09-30 23:50:18 +08:00
|
|
|
if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
|
|
|
|
{
|
|
|
|
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
|
|
|
}
|
|
|
|
/* add timer to temporary list */
|
|
|
|
rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
|
|
|
|
|
2021-08-17 17:56:39 +08:00
|
|
|
_soft_timer_status = RT_SOFT_TIMER_BUSY;
|
2020-09-30 23:50:18 +08:00
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
2012-12-15 10:17:17 +08:00
|
|
|
|
|
|
|
/* call timeout function */
|
|
|
|
t->timeout_func(t->parameter);
|
|
|
|
|
2018-11-13 11:32:01 +08:00
|
|
|
RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
|
2012-12-15 10:17:17 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
|
|
|
|
|
2020-09-30 23:50:18 +08:00
|
|
|
/* disable interrupt */
|
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
|
|
2021-08-17 17:56:39 +08:00
|
|
|
_soft_timer_status = RT_SOFT_TIMER_IDLE;
|
2020-09-30 23:50:18 +08:00
|
|
|
/* Check whether the timer object is detached or started again */
|
|
|
|
if (rt_list_isempty(&list))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-03 09:43:09 +08:00
|
|
|
rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
|
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 break; /* not check anymore */
|
|
|
|
}
|
2020-09-30 23:50:18 +08:00
|
|
|
/* enable interrupt */
|
|
|
|
rt_hw_interrupt_enable(level);
|
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
|
|
|
}
|
|
|
|
|
2021-08-15 23:04:35 +08:00
|
|
|
/**
|
2021-08-28 23:31:35 +08:00
|
|
|
* @brief System timer thread entry
|
2021-08-16 22:07:46 +08:00
|
|
|
*
|
2021-08-30 07:28:36 +08:00
|
|
|
* @param parameter is the arg of the thread
|
2021-08-15 23:04:35 +08:00
|
|
|
*/
|
2021-08-17 17:56:39 +08:00
|
|
|
static void _timer_thread_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 */
|
2022-03-07 11:53:34 +08:00
|
|
|
if (_timer_list_next_timeout(_soft_timer_list, &next_timeout) != RT_EOK)
|
2012-12-15 10:17:17 +08:00
|
|
|
{
|
|
|
|
/* no software timer exist, suspend self. */
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_thread_suspend_with_flag(rt_thread_self(), RT_UNINTERRUPTIBLE);
|
2012-12-15 10:17:17 +08:00
|
|
|
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
|
|
|
}
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_TIMER_SOFT */
|
2009-12-22 23:06:27 +08:00
|
|
|
|
2009-12-23 21:52:42 +08:00
|
|
|
/**
|
|
|
|
* @ingroup SystemInit
|
|
|
|
*
|
2021-08-15 23:04:35 +08:00
|
|
|
* @brief 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
|
|
|
{
|
2022-08-03 00:09:49 +08:00
|
|
|
rt_size_t i;
|
2013-09-26 15:52:02 +08:00
|
|
|
|
2021-08-17 17:56:39 +08:00
|
|
|
for (i = 0; i < sizeof(_timer_list) / sizeof(_timer_list[0]); i++)
|
2013-09-26 15:52:02 +08:00
|
|
|
{
|
2021-08-17 17:56:39 +08:00
|
|
|
rt_list_init(_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
|
|
|
|
*
|
2021-08-15 23:04:35 +08:00
|
|
|
* @brief 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;
|
2021-08-17 17:56:39 +08:00
|
|
|
i < sizeof(_soft_timer_list) / sizeof(_soft_timer_list[0]);
|
2013-09-26 15:52:02 +08:00
|
|
|
i++)
|
|
|
|
{
|
2021-08-17 17:56:39 +08:00
|
|
|
rt_list_init(_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 */
|
2021-08-17 17:56:39 +08:00
|
|
|
rt_thread_init(&_timer_thread,
|
2012-12-15 10:17:17 +08:00
|
|
|
"timer",
|
2021-08-17 17:56:39 +08:00
|
|
|
_timer_thread_entry,
|
2012-12-18 16:34:46 +08:00
|
|
|
RT_NULL,
|
2021-08-17 17:56:39 +08:00
|
|
|
&_timer_thread_stack[0],
|
|
|
|
sizeof(_timer_thread_stack),
|
2012-12-18 16:34:46 +08:00
|
|
|
RT_TIMER_THREAD_PRIO,
|
|
|
|
10);
|
2009-12-22 23:06:27 +08:00
|
|
|
|
2012-12-15 10:17:17 +08:00
|
|
|
/* startup */
|
2021-08-17 17:56:39 +08:00
|
|
|
rt_thread_startup(&_timer_thread);
|
2021-06-10 18:33:47 +08:00
|
|
|
#endif /* RT_USING_TIMER_SOFT */
|
2009-12-22 23:06:27 +08:00
|
|
|
}
|
2009-12-23 21:52:42 +08:00
|
|
|
|
2016-08-19 10:19:46 +08:00
|
|
|
/**@}*/
|