1 前言
rt-thread可以採用軟件定時器或硬件定時器來實現定時器管理的,所謂軟件定時器是指由操作系統提供的一類系統接口,它構建在硬件定時器基礎之上,使系統能夠提供不受數目限制的定時器服務。而硬件定時器是芯片本身提供的定時功能。一般是由外部晶振提供給芯片輸入時鐘,芯片向軟件模塊提供一組配置寄存器,接受控制輸入,到達設定時間值後芯片中斷控制器產生時鐘中斷。硬件定時器的精度一般很高,可以達到納秒級別,並且是中斷觸發方式。軟件定時器的精度取決於它使用的硬件定時器精度。而rt-thread操作系統在默認情況下是採用的硬件定時器的方式,用戶可以通過修改宏定義#ifdef RT_USING_TIMER_SOFT來修改採用哪種。
2 rt-thread的定時器的基本工作原理
在RT-Thread定時器模塊維護兩個重要的全局變量,一個是當前系統的時間rt_tick(當硬件定時器中斷來臨時,它將加1),另一個是定時器鏈表rt_timer_list,系統中新創建的定時期都會被以排序的方式插入到rt_timer_list(硬件定時器模式下使用)鏈表中,rt_timer_list的每個節點保留了一個定時器的信息,並且在這個節點加入鏈表時就計算好了產生時間到達時的時間點,即tick,在rt-thread系統中如果採用軟件定時器模式,則存在一定時器線程rt_thread_timer_entry,不斷獲取當前TICK值並與定時器鏈表rt_timer_list上的定時器對比判斷是否時間已到,一旦發現就調用對應的回調函數,即事件處理函數進行處理,而如果採用硬件定時器管理模式的話,則該檢查過程放到系統時鐘中斷例程中進行處理,此時,是不存在定時器線程的。如下圖:注:如果採用軟件定時器軟件定時器,則該定時器鏈表爲rt_soft_timer_list。
3 源碼分析
3.1 數據定義
- /**
- * timer structure
- */
- struct rt_timer
- {
- struct rt_object parent; //內核對象
- rt_list_t list; //鏈表節點
- void (*timeout_func)(void *parameter); //定時器超時例程
- void *parameter; //定時器例程的傳入參數
- rt_tick_t init_tick; //定時器的超時時間,即總共多長時間將產生超時事件
- rt_tick_t timeout_tick; //定時器超時的時間點,即產生超時事件時那一該的時間點
- };
- typedef struct rt_timer *rt_timer_t;
3.2 rt-thread的軟件定時器模式
軟件定時器線程初始化及啓動:
- /**
- * @ingroup SystemInit
- *
- * This function will initialize system timer thread
- */
- void rt_system_timer_thread_init(void)
- {
- #ifdef RT_USING_TIMER_SOFT//如果採用軟件定時器管理模式,則啓動定時器線程
- rt_list_init(&rt_soft_timer_list);//初始化軟件定時器鏈表
- /* 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(&timer_thread);
- #endif
- }
軟件定時器線程如下:
- /* system timer thread entry */
- static void rt_thread_timer_entry(void *parameter)
- {
- rt_tick_t next_timeout;
- while (1)
- {
- /* get the next timeout tick */
- next_timeout = rt_timer_list_next_timeout(&rt_soft_timer_list);//得到軟件定時器鏈表上的下一個定時器的超時時間點
- 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();//獲取當前時間點
- if ((next_timeout - current_tick) < RT_TICK_MAX/2)//離下箇中斷時間點還差些時候
- {
- /* get the delta timeout tick */
- next_timeout = next_timeout - current_tick;//計算還差多長時間
- rt_thread_delay(next_timeout);//休眠一段時間
- }
- }
- /* lock scheduler */
- rt_enter_critical();//時間到,進入臨界區
- /* check software timer */
- rt_soft_timer_check();//檢查是否該產生超時事件
- /* unlock scheduler */
- rt_exit_critical();//退出臨界區
- }
- }
檢查是否產生中斷函數rt_soft_timer_check函數如下定義:
- /**
- * This function will check timer list, if a timeout event happens, the
- * corresponding timeout function will be invoked.
- */
- void rt_soft_timer_check(void)
- {
- 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();//得到當前時間點
- for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list);)//得到下一定時器節點
- {
- t = rt_list_entry(n, struct rt_timer, list);//t指向rt_timer定時器
- /*
- * It supposes that the new tick shall less than the half duration of
- * tick max.
- */
- if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)//如果當前的時間點超過定時器的超時時間點
- {
- RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));//使用鉤子函數
- /* 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);//產生定時器超時事件,調用對應處理函數
- /* 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) &&//如果當前定時器是週期性定時器,則將其再次按序放入軟件定時器鏈表
- (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
- {
- /* start it */
- t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;//置標誌爲非激活狀態
- rt_timer_start(t);//再次將定時器t放入軟件定時器鏈表末尾
- }
- else
- {
- /* stop timer */
- t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;//置標誌爲非激活狀態
- }
- }
- else break; /* not check anymore */
- }
- RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
- }
上面代碼中,爲什麼定時器裏判斷超時的條件是((current_tick - t→timeout_tick) < RT_TICK_MAX/2)?
因爲系統時鐘溢出後會自動迴繞。取定時器比較最大值是定時器最大值的一半,即RT_TICK_MAX/2(在比較兩個定時器值時,值是32位無符號數,相減運算將會自動迴繞)。系統支持的定時器最大長度就是RT_TICK_MAX的一半:即248天(10ms/tick),124天(5ms/tick),24.5天(1ms/tick),以下內容相同道理。
其上rt_timer_start函數如下定義:
- /**
- * 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;
- rt_list_t *n, *timer_list;
- /* timer check */
- RT_ASSERT(timer != RT_NULL);
- if (timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)//如果傳入的定時器已經激活,則直接返回錯誤
- return -RT_ERROR;
- RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));//使用鉤子函數
- /*
- * get timeout tick,
- * the max timeout tick shall not great than RT_TICK_MAX/2
- */
- RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
- timer->timeout_tick = rt_tick_get() + timer->init_tick;//得到定時器超時的時間點
- /* disable interrupt */
- level = rt_hw_interrupt_disable();//關中斷
- #ifdef RT_USING_TIMER_SOFT//如果採用的是軟件定時器管理模式,則將定時器加入到rt_soft_timer_list中
- if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
- {
- /* insert timer to soft timer list */
- timer_list = &rt_soft_timer_list;
- }
- else
- #endif
- {
- /* insert timer to system timer list */
- timer_list = &rt_timer_list;
- }
- for (n = timer_list->next; n != timer_list; n = n->next)//將定時器按序加入到定時器鏈表中
- {
- t = rt_list_entry(n, struct rt_timer, list);
- /*
- * It supposes that the new tick shall less than the half duration of
- * tick max.
- */
- if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
- {
- rt_list_insert_before(n, &(timer->list));//將定時器timer插入到t之前
- break;
- }
- }
- /* no found suitable position in timer list */
- if (n == timer_list)//沒有找到合適的位置,則放到鏈表頭
- {
- rt_list_insert_before(n, &(timer->list));
- }
- timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;//置定時器爲激活狀態
- /* enable interrupt */
- rt_hw_interrupt_enable(level);
- #ifdef RT_USING_TIMER_SOFT
- if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)//如果系統採用的是軟件定時器管理模式,且軟件定時器線程處理ready狀態,則恢復此線程
- {
- /* 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();//開始線程調度
- }
- }
- #endif
- return -RT_EOK;
- }
軟件定時器管理模式的源碼分析完了,接下來介紹RTT的硬件定時器管理模式。
3.3 RTT的硬件定時器管理模式
硬件定時器管理模式顧名思義,就是說與硬件相關,因此,不用的MCU,其部分源碼是不一樣的,因爲其要採用MCU的系統時鐘中斷例程來實現。
以STM32F2XX爲例,先找到其啓動彙編,位置在:RTT/bsp/stm32f2xx/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F2xx/startup/arm/startup_stm32f2xx.s
找到中斷向量:
- DCD SysTick_Handler ; SysTick Handler
在bsp/stm32f2xx/drivers/board.c文件中:
- /**
- * This is the timer interrupt service routine.
- *
- */
- void SysTick_Handler(void)//系統時鐘中斷例程
- {
- /* enter interrupt */
- rt_interrupt_enter();
- rt_tick_increase();
- /* leave interrupt */
- rt_interrupt_leave();
- }
其中rt_tick_increase函數在RTT/src/clock.c文件中的實現如下:
- /**
- * This function will notify kernel there is one tick passed. Normally,
- * this function is invoked by clock ISR.
- */
- void rt_tick_increase(void)
- {
- struct rt_thread *thread;
- /* increase the global tick */
- ++ rt_tick;//全局rt_tick加1
- /* check time slice */
- thread = rt_thread_self();//得到當前正在運行的線程
- -- thread->remaining_tick;//純種剩下時間減1
- if (thread->remaining_tick == 0)//如果線程剩餘時間爲0,即調度時間已到
- {
- /* change to initialized tick */
- thread->remaining_tick = thread->init_tick;//將線程剩餘時間重新設置初始化值
- /* yield */
- rt_thread_yield();//調度時間到,切換到其它線程
- }
- /* check timer */
- rt_timer_check();//檢查硬件定時器鏈表是否有定時器產生超時事件
- }
其中rt_timer_check函數在RTT/src/timer.c文件中如下定義:
- /**
- * This function will check timer list, if a timeout event happens, the
- * corresponding timeout function will be invoked.
- *
- * @note this function shall be invoked in operating system timer interrupt.
- */
- void rt_timer_check(void)
- {
- 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();
- while (!rt_list_isempty(&rt_timer_list))
- {
- t = rt_list_entry(rt_timer_list.next, struct rt_timer, list);
- /*
- * It supposes that the new tick shall less than the half duration of
- * tick max.
- */
- if ((current_tick - t->timeout_tick) < RT_TICK_MAX/2)
- {
- RT_OBJECT_HOOK_CALL(rt_timer_timeout_hook, (t));
- /* remove timer from timer list firstly */
- rt_list_remove(&(t->list));
- /* 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) &&
- (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
- {
- /* 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"));
- }
此函數與rt_soft_timer_check基本大致相同,只不過一個是查找硬件定時器鏈表rt_timer_list,一個是查找rt_soft_timer_list.
在此,硬件定時器管理模式基本上介紹完畢,接下來介紹一些定時器接口.
4 定時器接口
4.1 定時器初始化
靜態初始化定義器
- /**
- * This function will initialize a timer, normally this function is used to
- * initialize 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
- */
- 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);
- /* timer object initialization */
- rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);//初始化內核對象
- _rt_timer_init(timer, timeout, parameter, time, flag);
- }
_rt_timer_init函數如下定義:
- 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;//置flag
- /* set deactivated */
- timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;//初始化時,設置爲非激活狀態
- timer->timeout_func = timeout;//設置超時事件處理函數
- timer->parameter = parameter;//超時事件處理函數的傳入參數
- timer->timeout_tick = 0;//定時器的超時時間點初始化時爲0
- timer->init_tick = time;//置超時時間
- /* initialize timer list */
- rt_list_init(&(timer->list));//初始化本身節點
- }
動態創建定時器
- /**
- * 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;
- }
4.2 脫離和刪除
脫離:
- /**
- * 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);
- /* 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;
- }
刪除動態創建的定時器
- /**
- * 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;
- /* 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;
- }
4.3 啓動定時器
- /**
- * 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)
4.4 停止定時器
- /**
- * 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;
- RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));//使用鉤子函數
- /* disable interrupt */
- level = rt_hw_interrupt_disable();//關中斷
- /* remove it from timer list */
- rt_list_remove(&(timer->list));//從定時器鏈表中移除
- /* enable interrupt */
- rt_hw_interrupt_enable(level);//開中斷
- /* change stat */
- timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;//置非激活狀態
- return RT_EOK;
- }
4.5 控制
此接口是用來修改一個定時器的參數,如下代碼:
- /**
- * 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 RT_EOK
- */
- 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 &= ~RT_TIMER_FLAG_PERIODIC;
- break;
- case RT_TIMER_CTRL_SET_PERIODIC://修改定時器爲週期觸發定時器
- timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
- break;
- }
- return RT_EOK;
- }
完!