|
@@ -5,7 +5,7 @@
|
|
|
*
|
|
|
* The license and distribution terms for this file may be
|
|
|
* found in the file LICENSE in this distribution or at
|
|
|
- * http://openlab.rt-thread.com/license/LICENSE
|
|
|
+ * http://www.rt-thread.org/license/LICENSE
|
|
|
*
|
|
|
* Change Logs:
|
|
|
* Date Author Notes
|
|
@@ -22,13 +22,15 @@
|
|
|
|
|
|
#include "kservice.h"
|
|
|
|
|
|
-/* #define TIMER_DEBUG */
|
|
|
+/* #define RT_TIMER_DEBUG */
|
|
|
|
|
|
/* hard timer list */
|
|
|
static rt_list_t rt_timer_list;
|
|
|
|
|
|
+#ifdef RT_USING_TIMER_SOFT
|
|
|
/* soft timer list */
|
|
|
static rt_list_t rt_soft_timer_list;
|
|
|
+#endif
|
|
|
|
|
|
#ifdef RT_USING_HOOK
|
|
|
extern void (*rt_object_take_hook)(struct rt_object* object);
|
|
@@ -43,7 +45,7 @@ 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.
|
|
|
- *
|
|
|
+ *
|
|
|
* @param hook the hook function
|
|
|
*/
|
|
|
void rt_timer_timeout_sethook(void (*hook)(struct rt_timer* timer))
|
|
@@ -54,21 +56,9 @@ void rt_timer_timeout_sethook(void (*hook)(struct rt_timer* timer))
|
|
|
/*@}*/
|
|
|
#endif
|
|
|
|
|
|
-/**
|
|
|
- * @ingroup SystemInit
|
|
|
- *
|
|
|
- * This function will init system timer
|
|
|
- *
|
|
|
- */
|
|
|
-void rt_system_timer_init()
|
|
|
-{
|
|
|
- rt_list_init(&rt_timer_list);
|
|
|
- rt_list_init(&rt_soft_timer_list);
|
|
|
-}
|
|
|
-
|
|
|
-static void _rt_timer_init(rt_timer_t timer,
|
|
|
- void (*timeout)(void* parameter), void* parameter,
|
|
|
- rt_tick_t time, rt_uint8_t flag)
|
|
|
+static void _rt_timer_init(rt_timer_t timer,
|
|
|
+ void (*timeout)(void* parameter), void* parameter,
|
|
|
+ rt_tick_t time, rt_uint8_t flag)
|
|
|
{
|
|
|
/* set flag */
|
|
|
timer->parent.flag = flag;
|
|
@@ -92,7 +82,7 @@ static void _rt_timer_init(rt_timer_t timer,
|
|
|
/*@{*/
|
|
|
|
|
|
/**
|
|
|
- * This function will init a timer, normally this function is used to initialize
|
|
|
+ * This function will init a timer, normally this function is used to initialize
|
|
|
* a static timer object.
|
|
|
*
|
|
|
* @param timer the static timer object
|
|
@@ -102,10 +92,10 @@ static void _rt_timer_init(rt_timer_t timer,
|
|
|
* @param time the tick of timer
|
|
|
* @param flag the flag of timer
|
|
|
*/
|
|
|
-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)
|
|
|
+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)
|
|
|
{
|
|
|
/* timer check */
|
|
|
RT_ASSERT(timer != RT_NULL);
|
|
@@ -116,17 +106,17 @@ void rt_timer_init(rt_timer_t timer,
|
|
|
_rt_timer_init(timer, timeout, parameter, time, flag);
|
|
|
}
|
|
|
|
|
|
-/**
|
|
|
+/**
|
|
|
* This function will detach a timer from timer management.
|
|
|
*
|
|
|
* @param timer the static timer object
|
|
|
- *
|
|
|
+ *
|
|
|
* @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;
|
|
|
-
|
|
|
+
|
|
|
/* timer check */
|
|
|
RT_ASSERT(timer != RT_NULL);
|
|
|
|
|
@@ -183,7 +173,7 @@ rt_timer_t rt_timer_create(const char* name, void (*timeout)(void* parameter), v
|
|
|
rt_err_t rt_timer_delete(rt_timer_t timer)
|
|
|
{
|
|
|
register rt_base_t level;
|
|
|
-
|
|
|
+
|
|
|
/* timer check */
|
|
|
RT_ASSERT(timer != RT_NULL);
|
|
|
|
|
@@ -228,8 +218,10 @@ rt_err_t rt_timer_start(rt_timer_t timer)
|
|
|
|
|
|
/* disable interrupt */
|
|
|
level = rt_hw_interrupt_disable();
|
|
|
+#ifdef RT_USING_TIMER_SOFT
|
|
|
if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
|
|
|
- {/* insert timer to soft timer list */
|
|
|
+ {
|
|
|
+ /* insert timer to soft timer list */
|
|
|
for (n = rt_soft_timer_list.next; n != &rt_soft_timer_list; n = n->next)
|
|
|
{
|
|
|
t = rt_list_entry(n, struct rt_timer, list);
|
|
@@ -246,24 +238,25 @@ rt_err_t rt_timer_start(rt_timer_t timer)
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
- {/* insert timer to system timer list */
|
|
|
- /* insert timer to system timer list */
|
|
|
- for (n = rt_timer_list.next; n != &rt_timer_list; n = n->next)
|
|
|
+#endif
|
|
|
{
|
|
|
- t = rt_list_entry(n, struct rt_timer, list);
|
|
|
- if (t->timeout_tick > timer->timeout_tick)
|
|
|
+ /* insert timer to system timer list */
|
|
|
+ for (n = rt_timer_list.next; n != &rt_timer_list; n = n->next)
|
|
|
{
|
|
|
- rt_list_insert_before(n, &(timer->list));
|
|
|
- break;
|
|
|
+ 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 */
|
|
|
- if (n == &rt_timer_list)
|
|
|
- {
|
|
|
- rt_list_insert_before(n, &(timer->list));
|
|
|
+ /* no found suitable position in timer list */
|
|
|
+ if (n == &rt_timer_list)
|
|
|
+ {
|
|
|
+ rt_list_insert_before(n, &(timer->list));
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;
|
|
|
|
|
@@ -346,11 +339,10 @@ rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void* arg)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * This function will check timer list, if a timeout event happens, the
|
|
|
+ * This function will check timer list, if a timeout event happens, the
|
|
|
* corresponding timeout function will be invoked.
|
|
|
*
|
|
|
*/
|
|
|
-void rt_soft_timer_tick_hook (void);
|
|
|
void rt_timer_check()
|
|
|
{
|
|
|
rt_tick_t current_tick;
|
|
@@ -358,7 +350,7 @@ void rt_timer_check()
|
|
|
struct rt_timer *t;
|
|
|
register rt_base_t level;
|
|
|
|
|
|
-#ifdef TIMER_DEBUG
|
|
|
+#ifdef RT_TIMER_DEBUG
|
|
|
rt_kprintf("timer check enter\n");
|
|
|
#endif
|
|
|
|
|
@@ -387,12 +379,12 @@ void rt_timer_check()
|
|
|
/* reget tick */
|
|
|
current_tick = rt_tick_get();
|
|
|
|
|
|
-#ifdef TIMER_DEBUG
|
|
|
+#ifdef RT_TIMER_DEBUG
|
|
|
rt_kprintf("current tick: %d\n", current_tick);
|
|
|
#endif
|
|
|
|
|
|
- if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
|
|
|
- (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
|
|
+ if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
|
|
|
+ (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
|
|
{
|
|
|
/* start it */
|
|
|
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
|
@@ -410,54 +402,30 @@ void rt_timer_check()
|
|
|
/* enable interrupt */
|
|
|
rt_hw_interrupt_enable(level);
|
|
|
|
|
|
- /**/
|
|
|
- rt_soft_timer_tick_hook ( );
|
|
|
-
|
|
|
-#ifdef TIMER_DEBUG
|
|
|
+#ifdef RT_TIMER_DEBUG
|
|
|
rt_kprintf("timer check leave\n");
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-static struct rt_thread soft_timer_thread;
|
|
|
-static rt_uint8_t rt_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
|
|
|
+#ifdef RT_USING_TIMER_SOFT
|
|
|
+static struct rt_thread timer_thread;
|
|
|
+static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
|
|
|
static struct rt_semaphore timer_sem;
|
|
|
|
|
|
-static rt_uint16_t timer_ex_cnt;
|
|
|
-
|
|
|
-
|
|
|
-rt_err_t timer_signal (void)
|
|
|
-{
|
|
|
- return rt_sem_release(&timer_sem);
|
|
|
-}
|
|
|
-
|
|
|
-void rt_soft_timer_tick_hook (void)
|
|
|
-{
|
|
|
- timer_ex_cnt++;
|
|
|
- if (timer_ex_cnt >= (RT_TICK_PER_SECOND / RT_TIMER_EX_TICKS_PER_SEC))
|
|
|
- {
|
|
|
- timer_ex_cnt = 0;
|
|
|
- timer_signal();
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * This function will check timer list, if a timeout event happens, the
|
|
|
- * corresponding timeout function will be invoked.
|
|
|
- *
|
|
|
- */
|
|
|
-void rt_soft_timer_check()
|
|
|
+/* check software timer list. if a timeout event happens, the corresponding
|
|
|
+ timeout function will be invoked. */
|
|
|
+static void rt_soft_timer_check()
|
|
|
{
|
|
|
rt_tick_t current_tick;
|
|
|
rt_list_t *n;
|
|
|
struct rt_timer *t;
|
|
|
-
|
|
|
-#ifdef TIMER_DEBUG
|
|
|
- rt_kprintf("timer check enter\n");
|
|
|
+
|
|
|
+#ifdef RT_TIMER_DEBUG
|
|
|
+ rt_kprintf("software timer check enter\n");
|
|
|
#endif
|
|
|
|
|
|
- current_tick = rt_tick_get();
|
|
|
-
|
|
|
+ current_tick = rt_tick_get();
|
|
|
+
|
|
|
for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list); )
|
|
|
{
|
|
|
t = rt_list_entry(n, struct rt_timer, list);
|
|
@@ -478,12 +446,12 @@ void rt_soft_timer_check()
|
|
|
/* reget tick */
|
|
|
current_tick = rt_tick_get();
|
|
|
|
|
|
-#ifdef TIMER_DEBUG
|
|
|
+#ifdef RT_TIMER_DEBUG
|
|
|
rt_kprintf("current tick: %d\n", current_tick);
|
|
|
#endif
|
|
|
|
|
|
- if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
|
|
|
- (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
|
|
+ if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
|
|
|
+ (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
|
|
|
{
|
|
|
/* start it */
|
|
|
t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
|
|
@@ -496,27 +464,50 @@ void rt_soft_timer_check()
|
|
|
}
|
|
|
}
|
|
|
else break;
|
|
|
- }
|
|
|
-#ifdef TIMER_DEBUG
|
|
|
- rt_kprintf("timer check leave\n");
|
|
|
+ }
|
|
|
+
|
|
|
+#ifdef RT_TIMER_DEBUG
|
|
|
+ rt_kprintf("software timer check leave\n");
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
+/* system timer thread entry */
|
|
|
static void rt_thread_timer_entry(void* parameter)
|
|
|
{
|
|
|
- (void)parameter;
|
|
|
while (1)
|
|
|
{
|
|
|
- rt_sem_take(&timer_sem,RT_WAITING_FOREVER);
|
|
|
+ /* take the semaphore as software timer tick */
|
|
|
+ rt_sem_take(&timer_sem, RT_TICK_PER_SECOND / RT_TIMER_TICK_PER_SECOND);
|
|
|
+
|
|
|
+ /* reach here because thread takes semaphore timeout */
|
|
|
|
|
|
+ /* lock scheduler */
|
|
|
rt_enter_critical();
|
|
|
-
|
|
|
- rt_soft_timer_check( );
|
|
|
-
|
|
|
+
|
|
|
+ /* check software timer */
|
|
|
+ rt_soft_timer_check();
|
|
|
+
|
|
|
+ /* unlock scheduler */
|
|
|
rt_exit_critical();
|
|
|
- }
|
|
|
+ }
|
|
|
}
|
|
|
+#endif
|
|
|
|
|
|
+/**
|
|
|
+ * @ingroup SystemInit
|
|
|
+ *
|
|
|
+ * This function will init system timer
|
|
|
+ *
|
|
|
+ */
|
|
|
+void rt_system_timer_init()
|
|
|
+{
|
|
|
+ rt_list_init(&rt_timer_list);
|
|
|
+
|
|
|
+#ifdef RT_USING_TIMER_SOFT
|
|
|
+ rt_list_init(&rt_soft_timer_list);
|
|
|
+ rt_sem_init(&timer_sem, "timer", 0, RT_IPC_FLAG_FIFO);
|
|
|
+#endif
|
|
|
+}
|
|
|
|
|
|
/**
|
|
|
* @ingroup SystemInit
|
|
@@ -524,18 +515,19 @@ static void rt_thread_timer_entry(void* parameter)
|
|
|
* This function will init system timer thread
|
|
|
*
|
|
|
*/
|
|
|
-void rt_timer_thread_init(void)
|
|
|
+void rt_system_timer_thread_init()
|
|
|
{
|
|
|
- rt_sem_init(&timer_sem, "timer", 1, RT_IPC_FLAG_FIFO);
|
|
|
- timer_ex_cnt = 0;
|
|
|
- /*init timer_ex thread*/
|
|
|
- rt_thread_init(&soft_timer_thread,
|
|
|
- "timer",
|
|
|
- rt_thread_timer_entry, RT_NULL,
|
|
|
- &rt_thread_stack[0], sizeof(rt_thread_stack),
|
|
|
- RT_TIMER_THREAD_PRIO, 10);
|
|
|
+#ifdef RT_USING_TIMER_SOFT
|
|
|
+ /* 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);
|
|
|
|
|
|
/* startup */
|
|
|
- rt_thread_startup(&soft_timer_thread);
|
|
|
+ rt_thread_startup(&timer_thread);
|
|
|
+#endif
|
|
|
}
|
|
|
+
|
|
|
/*@}*/
|