summaryrefslogtreecommitdiff
path: root/src/timer.c
diff options
context:
space:
mode:
authorxph <[email protected]>2020-04-09 16:35:28 +0800
committerGitHub <[email protected]>2020-04-09 16:35:28 +0800
commitca7f01d79b3e0a997c771cec298e4ebd65b75809 (patch)
tree3d2c48aae7203b14038ba7b98b500b5343ed7f2a /src/timer.c
parentf763f8e7284b0492c5a65ad0abf94ee1963d7dcc (diff)
parentbaa3145bfe61b3330b6a160523cc5470fa5db3a7 (diff)
Merge pull request #1 from RT-Thread/master
sync rtt
Diffstat (limited to 'src/timer.c')
-rw-r--r--src/timer.c62
1 files changed, 49 insertions, 13 deletions
diff --git a/src/timer.c b/src/timer.c
index dc489a2d0d..0034f0246c 100644
--- a/src/timer.c
+++ b/src/timer.c
@@ -43,7 +43,8 @@ static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
#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);
+static void (*rt_timer_enter_hook)(struct rt_timer *timer);
+static void (*rt_timer_exit_hook)(struct rt_timer *timer);
/**
* @addtogroup Hook
@@ -52,14 +53,25 @@ static void (*rt_timer_timeout_hook)(struct rt_timer *timer);
/**@{*/
/**
- * This function will set a hook function, which will be invoked when timer
- * is timeout.
+ * This function will set a hook function, which will be invoked when enter
+ * timer timeout callback function.
*
* @param hook the hook function
*/
-void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer))
+void rt_timer_enter_sethook(void (*hook)(struct rt_timer *timer))
{
- rt_timer_timeout_hook = hook;
+ rt_timer_enter_hook = hook;
+}
+
+/**
+ * This function will set a hook function, which will be invoked when exit
+ * timer timeout callback function.
+ *
+ * @param hook the hook function
+ */
+void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
+{
+ rt_timer_exit_hook = hook;
}
/**@}*/
@@ -96,14 +108,23 @@ static void _rt_timer_init(rt_timer_t timer,
static rt_tick_t rt_timer_list_next_timeout(rt_list_t timer_list[])
{
struct rt_timer *timer;
+ register rt_base_t level;
+ rt_tick_t timeout_tick = RT_TICK_MAX;
- if (rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
- return RT_TICK_MAX;
+ /* disable interrupt */
+ level = rt_hw_interrupt_disable();
- timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
- struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
+ 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]);
+ timeout_tick = timer->timeout_tick;
+ }
+
+ /* enable interrupt */
+ rt_hw_interrupt_enable(level);
- return timer->timeout_tick;
+ return timeout_tick;
}
rt_inline void _rt_timer_remove(rt_timer_t timer)
@@ -387,7 +408,7 @@ rt_err_t rt_timer_start(rt_timer_t timer)
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
{
/* check whether timer thread is ready */
- if ((timer_thread.stat & RT_THREAD_STAT_MASK) != RT_THREAD_READY)
+ if ((timer_thread.stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)
{
/* resume timer thread to check soft timer */
rt_thread_resume(&timer_thread);
@@ -467,6 +488,19 @@ rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)
case RT_TIMER_CTRL_SET_PERIODIC:
timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
break;
+ /* zhaoshimin 20191204 add query the timer state */
+ case RT_TIMER_CTRL_GET_STATE:
+ if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
+ {
+ /*timer is start and run*/
+ *(rt_tick_t *)arg = RT_TIMER_FLAG_ACTIVATED;
+ }
+ else
+ {
+ /*timer is stop*/
+ *(rt_tick_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
+ }
+ break;
}
return RT_EOK;
@@ -503,7 +537,7 @@ void rt_timer_check(void)
*/
if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
{
- RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
+ RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
/* remove timer from timer list firstly */
_rt_timer_remove(t);
@@ -514,6 +548,7 @@ void rt_timer_check(void)
/* re-get tick */
current_tick = rt_tick_get();
+ RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
@@ -578,7 +613,7 @@ void rt_soft_timer_check(void)
*/
if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
{
- RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
+ RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
/* move node to the next */
n = n->next;
@@ -594,6 +629,7 @@ void rt_soft_timer_check(void)
/* re-get tick */
current_tick = rt_tick_get();
+ RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
/* lock scheduler */