




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、Good is good, but better carries it.精益求精,善益求善。rt_thread 的定時器管理源碼分析-1前言rt-thread可以采用軟件定時器或硬件定時器來實(shí)現(xiàn)定時器管理的,所謂軟件定時器是指由操作系統(tǒng)提供的一類系統(tǒng)接口,它構(gòu)建在硬件定時器基礎(chǔ)之上,使系統(tǒng)能夠提供不受數(shù)目限制的定時器服務(wù)。而硬件定時器是芯片本身提供的定時功能。一般是由外部晶振提供給芯片輸入時鐘,芯片向軟件模塊提供一組配置寄存器,接受控制輸入,到達(dá)設(shè)定時間值后芯片中斷控制器產(chǎn)生時鐘中斷。硬件定時器的精度一般很高,可以達(dá)到納秒級別,并且是中斷觸發(fā)方式。軟件定時器的精度取決于它使用的硬件定時器精度。
2、而rt-thread操作系統(tǒng)在默認(rèn)情況下是采用的硬件定時器的方式,用戶可以通過修改宏定義#ifdefRT_USING_TIMER_SOFT來修改采用哪種。2rt-thread的定時器的基本工作原理在RT-Thread定時器模塊維護(hù)兩個重要的全局變量,一個是當(dāng)前系統(tǒng)的時間rt_tick(當(dāng)硬件定時器中斷來臨時,它將加1),另一個是定時器鏈表rt_timer_list,系統(tǒng)中新創(chuàng)建的定時期都會被以排序的方式插入到rt_timer_list(硬件定時器模式下使用)鏈表中,rt_timer_list的每個節(jié)點(diǎn)保留了一個定時器的信息,并且在這個節(jié)點(diǎn)加入鏈表時就計(jì)算好了產(chǎn)生時間到達(dá)時的時間點(diǎn),即tick,
3、在rt-thread系統(tǒng)中如果采用軟件定時器模式,則存在一定時器線程rt_thread_timer_entry,不斷獲取當(dāng)前TICK值并與定時器鏈表rt_timer_list上的定時器對比判斷是否時間已到,一旦發(fā)現(xiàn)就調(diào)用對應(yīng)的回調(diào)函數(shù),即事件處理函數(shù)進(jìn)行處理,而如果采用硬件定時器管理模式的話,則該檢查過程放到系統(tǒng)時鐘中斷例程中進(jìn)行處理,此時,是不存在定時器線程的。如下圖:注:如果采用軟件定時器軟件定時器,則該定時器鏈表為rt_soft_timer_list。3源碼分析3.1數(shù)據(jù)定義cppHYPERLINK/flydream0/article/details/8570841oviewplainv
4、iewplainHYPERLINK/flydream0/article/details/8570841ocopycopyHYPERLINK/flydream0/article/details/8570841oprintprintHYPERLINK/flydream0/article/details/8570841o?/*timerstructure*/structrt_timerstructrt_objectparent;/內(nèi)核對象rt_list_tlist;/鏈表節(jié)點(diǎn)void(*timeout_func)(void*parameter);/定時器超時例程void*parameter;/定時器
5、例程的傳入?yún)?shù)rt_tick_tinit_tick;/定時器的超時時間,即總共多長時間將產(chǎn)生超時事件rt_tick_ttimeout_tick;/定時器超時的時間點(diǎn),即產(chǎn)生超時事件時那一該的時間點(diǎn);typedefstructrt_timer*rt_timer_t;/*timerstructure*/structrt_timerstructrt_objectparent;/內(nèi)核對象rt_list_tlist;/鏈表節(jié)點(diǎn)void(*timeout_func)(void*parameter);/定時器超時例程void*parameter;/定時器例程的傳入?yún)?shù)rt_tick_tinit_tick;/
6、定時器的超時時間,即總共多長時間將產(chǎn)生超時事件rt_tick_ttimeout_tick;/定時器超時的時間點(diǎn),即產(chǎn)生超時事件時那一該的時間點(diǎn);typedefstructrt_timer*rt_timer_t;3.2rt-thread的軟件定時器模式軟件定時器線程初始化及啟動:cppHYPERLINK/flydream0/article/details/8570841oviewplainviewplainHYPERLINK/flydream0/article/details/8570841ocopycopyHYPERLINK/flydream0/article/details/8570841o
7、printprintHYPERLINK/flydream0/article/details/8570841o?/*ingroupSystemInit*Thisfunctionwillinitializesystemtimerthread*/voidrt_system_timer_thread_init(void)#ifdefRT_USING_TIMER_SOFT/如果采用軟件定時器管理模式,則啟動定時器線程rt_list_init(&rt_soft_timer_list);/初始化軟件定時器鏈表/*startsoftwaretimerthread*/rt_thread_init(&timer_
8、thread,/初始化軟件定時器線程,并啟動timer,rt_thread_timer_entry,RT_NULL,&timer_thread_stack0,sizeof(timer_thread_stack),RT_TIMER_THREAD_PRIO,10);/*startup*/rt_thread_startup(&timer_thread);#endif/*ingroupSystemInit*Thisfunctionwillinitializesystemtimerthread*/voidrt_system_timer_thread_init(void)#ifdefRT_USING_TI
9、MER_SOFT/如果采用軟件定時器管理模式,則啟動定時器線程rt_list_init(&rt_soft_timer_list);/初始化軟件定時器鏈表/*startsoftwaretimerthread*/rt_thread_init(&timer_thread,/初始化軟件定時器線程,并啟動timer,rt_thread_timer_entry,RT_NULL,&timer_thread_stack0,sizeof(timer_thread_stack),RT_TIMER_THREAD_PRIO,10);/*startup*/rt_thread_startup(&timer_thread)
10、;#endif軟件定時器線程如下:cppHYPERLINK/flydream0/article/details/8570841oviewplainviewplainHYPERLINK/flydream0/article/details/8570841ocopycopyHYPERLINK/flydream0/article/details/8570841oprintprintHYPERLINK/flydream0/article/details/8570841o?/*systemtimerthreadentry*/staticvoidrt_thread_timer_entry(void*para
11、meter)rt_tick_tnext_timeout;while(1)/*getthenexttimeouttick*/next_timeout=rt_timer_list_next_timeout(&rt_soft_timer_list);/得到軟件定時器鏈表上的下一個定時器的超時時間點(diǎn)if(next_timeout=RT_TICK_MAX)/如果超過范圍,則掛起當(dāng)前線程,繼續(xù)線程調(diào)度/*nosoftwaretimerexist,suspendself.*/rt_thread_suspend(rt_thread_self();rt_schedule();elsert_tick_tcurre
12、nt_tick;/*getcurrenttick*/current_tick=rt_tick_get();/獲取當(dāng)前時間點(diǎn)if(next_timeout-current_tick)RT_TICK_MAX/2)/離下個中斷時間點(diǎn)還差些時候/*getthedeltatimeouttick*/next_timeout=next_timeout-current_tick;/計(jì)算還差多長時間rt_thread_delay(next_timeout);/休眠一段時間/*lockscheduler*/rt_enter_critical();/時間到,進(jìn)入臨界區(qū)/*checksoftwaretimer*/rt
13、_soft_timer_check();/檢查是否該產(chǎn)生超時事件/*unlockscheduler*/rt_exit_critical();/退出臨界區(qū)/*systemtimerthreadentry*/staticvoidrt_thread_timer_entry(void*parameter)rt_tick_tnext_timeout;while(1)/*getthenexttimeouttick*/next_timeout=rt_timer_list_next_timeout(&rt_soft_timer_list);/得到軟件定時器鏈表上的下一個定時器的超時時間點(diǎn)if(next_tim
14、eout=RT_TICK_MAX)/如果超過范圍,則掛起當(dāng)前線程,繼續(xù)線程調(diào)度/*nosoftwaretimerexist,suspendself.*/rt_thread_suspend(rt_thread_self();rt_schedule();elsert_tick_tcurrent_tick;/*getcurrenttick*/current_tick=rt_tick_get();/獲取當(dāng)前時間點(diǎn)if(next_timeout-current_tick)timeout_tick)next;/指向下一定時器/*removetimerfromtimerlistfirstly*/rt_lis
15、t_remove(&(t-list);/移除當(dāng)前定時器/*calltimeoutfunction*/t-timeout_func(t-parameter);/產(chǎn)生定時器超時事件,調(diào)用對應(yīng)處理函數(shù)/*re-gettick*/current_tick=rt_tick_get();/再次獲取當(dāng)前時間點(diǎn)RT_DEBUG_LOG(RT_DEBUG_TIMER,(currenttick:%dn,current_tick);if(t-parent.flag&RT_TIMER_FLAG_PERIODIC)&/如果當(dāng)前定時器是周期性定時器,則將其再次按序放入軟件定時器鏈表(t-parent.flag&RT_TI
16、MER_FLAG_ACTIVATED)/*startit*/t-parent.flag&=RT_TIMER_FLAG_ACTIVATED;/置標(biāo)志為非激活狀態(tài)rt_timer_start(t);/再次將定時器t放入軟件定時器鏈表末尾else/*stoptimer*/t-parent.flag&=RT_TIMER_FLAG_ACTIVATED;/置標(biāo)志為非激活狀態(tài)elsebreak;/*notcheckanymore*/RT_DEBUG_LOG(RT_DEBUG_TIMER,(softwaretimercheckleaven);/*Thisfunctionwillchecktimerlist,i
17、fatimeouteventhappens,the*correspondingtimeoutfunctionwillbeinvoked.*/voidrt_soft_timer_check(void)rt_tick_tcurrent_tick;rt_list_t*n;structrt_timer*t;RT_DEBUG_LOG(RT_DEBUG_TIMER,(softwaretimercheckentern);current_tick=rt_tick_get();/得到當(dāng)前時間點(diǎn)for(n=rt_soft_timer_list.next;n!=&(rt_soft_timer_list);)/得到下
18、一定時器節(jié)點(diǎn)t=rt_list_entry(n,structrt_timer,list);/t指向rt_timer定時器/*Itsupposesthatthenewtickshalllessthanthehalfdurationof*tickmax.*/if(current_tick-t-timeout_tick)next;/指向下一定時器/*removetimerfromtimerlistfirstly*/rt_list_remove(&(t-list);/移除當(dāng)前定時器/*calltimeoutfunction*/t-timeout_func(t-parameter);/產(chǎn)生定時器超時事件
19、,調(diào)用對應(yīng)處理函數(shù)/*re-gettick*/current_tick=rt_tick_get();/再次獲取當(dāng)前時間點(diǎn)RT_DEBUG_LOG(RT_DEBUG_TIMER,(currenttick:%dn,current_tick);if(t-parent.flag&RT_TIMER_FLAG_PERIODIC)&/如果當(dāng)前定時器是周期性定時器,則將其再次按序放入軟件定時器鏈表(t-parent.flag&RT_TIMER_FLAG_ACTIVATED)/*startit*/t-parent.flag&=RT_TIMER_FLAG_ACTIVATED;/置標(biāo)志為非激活狀態(tài)rt_timer_
20、start(t);/再次將定時器t放入軟件定時器鏈表末尾else/*stoptimer*/t-parent.flag&=RT_TIMER_FLAG_ACTIVATED;/置標(biāo)志為非激活狀態(tài)elsebreak;/*notcheckanymore*/RT_DEBUG_LOG(RT_DEBUG_TIMER,(softwaretimercheckleaven);上面代碼中,為什么定時器里判斷超時的條件是(current_tick-ttimeout_tick)parent.flag&RT_TIMER_FLAG_ACTIVATED)/如果傳入的定時器已經(jīng)激活,則直接返回錯誤return-RT_ERROR;
21、RT_OBJECT_HOOK_CALL(rt_object_take_hook,(&(timer-parent);/使用鉤子函數(shù)/*gettimeouttick,*themaxtimeouttickshallnotgreatthanRT_TICK_MAX/2*/RT_ASSERT(timer-init_ticktimeout_tick=rt_tick_get()+timer-init_tick;/得到定時器超時的時間點(diǎn)/*disableinterrupt*/level=rt_hw_interrupt_disable();/關(guān)中斷#ifdefRT_USING_TIMER_SOFT/如果采用的是軟
22、件定時器管理模式,則將定時器加入到rt_soft_timer_list中if(timer-parent.flag&RT_TIMER_FLAG_SOFT_TIMER)/*inserttimertosofttimerlist*/timer_list=&rt_soft_timer_list;else#endif/*inserttimertosystemtimerlist*/timer_list=&rt_timer_list;for(n=timer_list-next;n!=timer_list;n=n-next)/將定時器按序加入到定時器鏈表中t=rt_list_entry(n,structrt_t
23、imer,list);/*Itsupposesthatthenewtickshalllessthanthehalfdurationof*tickmax.*/if(t-timeout_tick-timer-timeout_tick)list);/將定時器timer插入到t之前break;/*nofoundsuitablepositionintimerlist*/if(n=timer_list)/沒有找到合適的位置,則放到鏈表頭rt_list_insert_before(n,&(timer-list);timer-parent.flag|=RT_TIMER_FLAG_ACTIVATED;/置定時器
24、為激活狀態(tài)/*enableinterrupt*/rt_hw_interrupt_enable(level);#ifdefRT_USING_TIMER_SOFTif(timer-parent.flag&RT_TIMER_FLAG_SOFT_TIMER)/如果系統(tǒng)采用的是軟件定時器管理模式,且軟件定時器線程處理ready狀態(tài),則恢復(fù)此線程/*checkwhethertimerthreadisready*/if(timer_thread.stat!=RT_THREAD_READY)/*resumetimerthreadtochecksofttimer*/rt_thread_resume(&timer
25、_thread);/恢復(fù)定時器線程rt_schedule();/開始線程調(diào)度#endifreturn-RT_EOK;/*Thisfunctionwillstartthetimer*paramtimerthetimertobestarted*returntheoperationstatus,RT_EOKonOK,-RT_ERRORonerror*/rt_err_trt_timer_start(rt_timer_ttimer)structrt_timer*t;registerrt_base_tlevel;rt_list_t*n,*timer_list;/*timercheck*/RT_ASSERT
26、(timer!=RT_NULL);if(timer-parent.flag&RT_TIMER_FLAG_ACTIVATED)/如果傳入的定時器已經(jīng)激活,則直接返回錯誤return-RT_ERROR;RT_OBJECT_HOOK_CALL(rt_object_take_hook,(&(timer-parent);/使用鉤子函數(shù)/*gettimeouttick,*themaxtimeouttickshallnotgreatthanRT_TICK_MAX/2*/RT_ASSERT(timer-init_ticktimeout_tick=rt_tick_get()+timer-init_tick;/得
27、到定時器超時的時間點(diǎn)/*disableinterrupt*/level=rt_hw_interrupt_disable();/關(guān)中斷#ifdefRT_USING_TIMER_SOFT/如果采用的是軟件定時器管理模式,則將定時器加入到rt_soft_timer_list中if(timer-parent.flag&RT_TIMER_FLAG_SOFT_TIMER)/*inserttimertosofttimerlist*/timer_list=&rt_soft_timer_list;else#endif/*inserttimertosystemtimerlist*/timer_list=&rt_t
28、imer_list;for(n=timer_list-next;n!=timer_list;n=n-next)/將定時器按序加入到定時器鏈表中t=rt_list_entry(n,structrt_timer,list);/*Itsupposesthatthenewtickshalllessthanthehalfdurationof*tickmax.*/if(t-timeout_tick-timer-timeout_tick)list);/將定時器timer插入到t之前break;/*nofoundsuitablepositionintimerlist*/if(n=timer_list)/沒有找
29、到合適的位置,則放到鏈表頭rt_list_insert_before(n,&(timer-list);timer-parent.flag|=RT_TIMER_FLAG_ACTIVATED;/置定時器為激活狀態(tài)/*enableinterrupt*/rt_hw_interrupt_enable(level);#ifdefRT_USING_TIMER_SOFTif(timer-parent.flag&RT_TIMER_FLAG_SOFT_TIMER)/如果系統(tǒng)采用的是軟件定時器管理模式,且軟件定時器線程處理ready狀態(tài),則恢復(fù)此線程/*checkwhethertimerthreadisready*
30、/if(timer_thread.stat!=RT_THREAD_READY)/*resumetimerthreadtochecksofttimer*/rt_thread_resume(&timer_thread);/恢復(fù)定時器線程rt_schedule();/開始線程調(diào)度#endifreturn-RT_EOK;軟件定時器管理模式的源碼分析完了,接下來介紹RTT的硬件定時器管理模式。3.3RTT的硬件定時器管理模式硬件定時器管理模式顧名思義,就是說與硬件相關(guān),因此,不用的MCU,其部分源碼是不一樣的,因?yàn)槠湟捎肕CU的系統(tǒng)時鐘中斷例程來實(shí)現(xiàn)。以STM32F2XX為例,先找到其啟動匯編,位置在
31、:RTT/bsp/stm32f2xx/Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F2xx/startup/arm/startup_stm32f2xx.s找到中斷向量:plainHYPERLINK/flydream0/article/details/8570841oviewplainviewplainHYPERLINK/flydream0/article/details/8570841ocopycopyHYPERLINK/flydream0/article/details/8570841oprintprintHYPERLINK/flydream0/art
32、icle/details/8570841o?DCDSysTick_Handler;SysTickHandlerDCDSysTick_Handler;SysTickHandler這是系統(tǒng)時鐘中斷向量,再找到其中斷例程實(shí)現(xiàn):在bsp/stm32f2xx/drivers/board.c文件中:cppHYPERLINK/flydream0/article/details/8570841oviewplainviewplainHYPERLINK/flydream0/article/details/8570841ocopycopyHYPERLINK/flydream0/article/details/857
33、0841oprintprintHYPERLINK/flydream0/article/details/8570841o?/*Thisisthetimerinterruptserviceroutine.*/voidSysTick_Handler(void)/系統(tǒng)時鐘中斷例程/*enterinterrupt*/rt_interrupt_enter();rt_tick_increase();/*leaveinterrupt*/rt_interrupt_leave();/*Thisisthetimerinterruptserviceroutine.*/voidSysTick_Handler(void)
34、/系統(tǒng)時鐘中斷例程/*enterinterrupt*/rt_interrupt_enter();rt_tick_increase();/*leaveinterrupt*/rt_interrupt_leave();其中rt_tick_increase函數(shù)在RTT/src/clock.c文件中的實(shí)現(xiàn)如下:cppHYPERLINK/flydream0/article/details/8570841oviewplainviewplainHYPERLINK/flydream0/article/details/8570841ocopycopyHYPERLINK/flydream0/article/deta
35、ils/8570841oprintprintHYPERLINK/flydream0/article/details/8570841o?/*Thisfunctionwillnotifykernelthereisonetickpassed.Normally,*thisfunctionisinvokedbyclockISR.*/voidrt_tick_increase(void)structrt_thread*thread;/*increasetheglobaltick*/+rt_tick;/全局rt_tick加1/*checktimeslice*/thread=rt_thread_self();/
36、得到當(dāng)前正在運(yùn)行的線程-thread-remaining_tick;/純種剩下時間減1if(thread-remaining_tick=0)/如果線程剩余時間為0,即調(diào)度時間已到/*changetoinitializedtick*/thread-remaining_tick=thread-init_tick;/將線程剩余時間重新設(shè)置初始化值/*yield*/rt_thread_yield();/調(diào)度時間到,切換到其它線程/*checktimer*/rt_timer_check();/檢查硬件定時器鏈表是否有定時器產(chǎn)生超時事件/*Thisfunctionwillnotifykernelthere
37、isonetickpassed.Normally,*thisfunctionisinvokedbyclockISR.*/voidrt_tick_increase(void)structrt_thread*thread;/*increasetheglobaltick*/+rt_tick;/全局rt_tick加1/*checktimeslice*/thread=rt_thread_self();/得到當(dāng)前正在運(yùn)行的線程-thread-remaining_tick;/純種剩下時間減1if(thread-remaining_tick=0)/如果線程剩余時間為0,即調(diào)度時間已到/*changetoini
38、tializedtick*/thread-remaining_tick=thread-init_tick;/將線程剩余時間重新設(shè)置初始化值/*yield*/rt_thread_yield();/調(diào)度時間到,切換到其它線程/*checktimer*/rt_timer_check();/檢查硬件定時器鏈表是否有定時器產(chǎn)生超時事件其中rt_timer_check函數(shù)在RTT/src/timer.c文件中如下定義:cppHYPERLINK/flydream0/article/details/8570841oviewplainviewplainHYPERLINK/flydream0/article/de
39、tails/8570841ocopycopyHYPERLINK/flydream0/article/details/8570841oprintprintHYPERLINK/flydream0/article/details/8570841o?/*Thisfunctionwillchecktimerlist,ifatimeouteventhappens,the*correspondingtimeoutfunctionwillbeinvoked.*notethisfunctionshallbeinvokedinoperatingsystemtimerinterrupt.*/voidrt_timer
40、_check(void)structrt_timer*t;rt_tick_tcurrent_tick;registerrt_base_tlevel;RT_DEBUG_LOG(RT_DEBUG_TIMER,(timercheckentern);current_tick=rt_tick_get();/*disableinterrupt*/level=rt_hw_interrupt_disable();while(!rt_list_isempty(&rt_timer_list)t=rt_list_entry(rt_timer_list.next,structrt_timer,list);/*Itsu
41、pposesthatthenewtickshalllessthanthehalfdurationof*tickmax.*/if(current_tick-t-timeout_tick)list);/*calltimeoutfunction*/t-timeout_func(t-parameter);/*re-gettick*/current_tick=rt_tick_get();RT_DEBUG_LOG(RT_DEBUG_TIMER,(currenttick:%dn,current_tick);if(t-parent.flag&RT_TIMER_FLAG_PERIODIC)&(t-parent.
42、flag&RT_TIMER_FLAG_ACTIVATED)/*startit*/t-parent.flag&=RT_TIMER_FLAG_ACTIVATED;rt_timer_start(t);else/*stoptimer*/t-parent.flag&=RT_TIMER_FLAG_ACTIVATED;elsebreak;/*enableinterrupt*/rt_hw_interrupt_enable(level);RT_DEBUG_LOG(RT_DEBUG_TIMER,(timercheckleaven);/*Thisfunctionwillchecktimerlist,ifatimeo
43、uteventhappens,the*correspondingtimeoutfunctionwillbeinvoked.*notethisfunctionshallbeinvokedinoperatingsystemtimerinterrupt.*/voidrt_timer_check(void)structrt_timer*t;rt_tick_tcurrent_tick;registerrt_base_tlevel;RT_DEBUG_LOG(RT_DEBUG_TIMER,(timercheckentern);current_tick=rt_tick_get();/*disableinter
44、rupt*/level=rt_hw_interrupt_disable();while(!rt_list_isempty(&rt_timer_list)t=rt_list_entry(rt_timer_list.next,structrt_timer,list);/*Itsupposesthatthenewtickshalllessthanthehalfdurationof*tickmax.*/if(current_tick-t-timeout_tick)list);/*calltimeoutfunction*/t-timeout_func(t-parameter);/*re-gettick*
45、/current_tick=rt_tick_get();RT_DEBUG_LOG(RT_DEBUG_TIMER,(currenttick:%dn,current_tick);if(t-parent.flag&RT_TIMER_FLAG_PERIODIC)&(t-parent.flag&RT_TIMER_FLAG_ACTIVATED)/*startit*/t-parent.flag&=RT_TIMER_FLAG_ACTIVATED;rt_timer_start(t);else/*stoptimer*/t-parent.flag&=RT_TIMER_FLAG_ACTIVATED;elsebreak
46、;/*enableinterrupt*/rt_hw_interrupt_enable(level);RT_DEBUG_LOG(RT_DEBUG_TIMER,(timercheckleaven);此函數(shù)與rt_soft_timer_check基本大致相同,只不過一個是查找硬件定時器鏈表rt_timer_list,一個是查找rt_soft_timer_list.在此,硬件定時器管理模式基本上介紹完畢,接下來介紹一些定時器接口.4定時器接口4.1定時器初始化靜態(tài)初始化定義器cppHYPERLINK/flydream0/article/details/8570841oviewplainviewplai
47、nHYPERLINK/flydream0/article/details/8570841ocopycopyHYPERLINK/flydream0/article/details/8570841oprintprintHYPERLINK/flydream0/article/details/8570841o?/*Thisfunctionwillinitializeatimer,normallythisfunctionisusedto*initializeastatictimerobject.*paramtimerthestatictimerobject*paramnamethenameoftimer
48、*paramtimeoutthetimeoutfunction*paramparametertheparameteroftimeoutfunction*paramtimethetickoftimer*paramflagtheflagoftimer*/voidrt_timer_init(rt_timer_ttimer,constchar*name,void(*timeout)(void*parameter),void*parameter,rt_tick_ttime,rt_uint8_tflag)/*timercheck*/RT_ASSERT(timer!=RT_NULL);/*timerobje
49、ctinitialization*/rt_object_init(rt_object_t)timer,RT_Object_Class_Timer,name);/初始化內(nèi)核對象_rt_timer_init(timer,timeout,parameter,time,flag);/*Thisfunctionwillinitializeatimer,normallythisfunctionisusedto*initializeastatictimerobject.*paramtimerthestatictimerobject*paramnamethenameoftimer*paramtimeoutth
50、etimeoutfunction*paramparametertheparameteroftimeoutfunction*paramtimethetickoftimer*paramflagtheflagoftimer*/voidrt_timer_init(rt_timer_ttimer,constchar*name,void(*timeout)(void*parameter),void*parameter,rt_tick_ttime,rt_uint8_tflag)/*timercheck*/RT_ASSERT(timer!=RT_NULL);/*timerobjectinitializatio
51、n*/rt_object_init(rt_object_t)timer,RT_Object_Class_Timer,name);/初始化內(nèi)核對象_rt_timer_init(timer,timeout,parameter,time,flag);_rt_timer_init函數(shù)如下定義:cppHYPERLINK/flydream0/article/details/8570841oviewplainviewplainHYPERLINK/flydream0/article/details/8570841ocopycopyHYPERLINK/flydream0/article/details/8570
52、841oprintprintHYPERLINK/flydream0/article/details/8570841o?staticvoid_rt_timer_init(rt_timer_ttimer,void(*timeout)(void*parameter),void*parameter,rt_tick_ttime,rt_uint8_tflag)/*setflag*/timer-parent.flag=flag;/置flag/*setdeactivated*/timer-parent.flag&=RT_TIMER_FLAG_ACTIVATED;/初始化時,設(shè)置為非激活狀態(tài)timer-time
53、out_func=timeout;/設(shè)置超時事件處理函數(shù)timer-parameter=parameter;/超時事件處理函數(shù)的傳入?yún)?shù)timer-timeout_tick=0;/定時器的超時時間點(diǎn)初始化時為0timer-init_tick=time;/置超時時間/*initializetimerlist*/rt_list_init(&(timer-list);/初始化本身節(jié)點(diǎn)staticvoid_rt_timer_init(rt_timer_ttimer,void(*timeout)(void*parameter),void*parameter,rt_tick_ttime,rt_uint8_
54、tflag)/*setflag*/timer-parent.flag=flag;/置flag/*setdeactivated*/timer-parent.flag&=RT_TIMER_FLAG_ACTIVATED;/初始化時,設(shè)置為非激活狀態(tài)timer-timeout_func=timeout;/設(shè)置超時事件處理函數(shù)timer-parameter=parameter;/超時事件處理函數(shù)的傳入?yún)?shù)timer-timeout_tick=0;/定時器的超時時間點(diǎn)初始化時為0timer-init_tick=time;/置超時時間/*initializetimerlist*/rt_list_init(&
55、(timer-list);/初始化本身節(jié)點(diǎn)動態(tài)創(chuàng)建定時器cppHYPERLINK/flydream0/article/details/8570841oviewplainviewplainHYPERLINK/flydream0/article/details/8570841ocopycopyHYPERLINK/flydream0/article/details/8570841oprintprintHYPERLINK/flydream0/article/details/8570841o?/*Thisfunctionwillcreateatimer*paramnamethenameoftimer*p
56、aramtimeoutthetimeoutfunction*paramparametertheparameteroftimeoutfunction*paramtimethetickoftimer*paramflagtheflagoftimer*returnthecreatedtimerobject*/rt_timer_trt_timer_create(constchar*name,void(*timeout)(void*parameter),void*parameter,rt_tick_ttime,rt_uint8_tflag)structrt_timer*timer;/*allocateao
57、bject*/timer=(structrt_timer*)rt_object_allocate(RT_Object_Class_Timer,name);/動態(tài)分配定時器內(nèi)核對象if(timer=RT_NULL)returnRT_NULL;_rt_timer_init(timer,timeout,parameter,time,flag);/調(diào)用上述的初始化接口returntimer;/*Thisfunctionwillcreateatimer*paramnamethenameoftimer*paramtimeoutthetimeoutfunction*paramparameterthepara
58、meteroftimeoutfunction*paramtimethetickoftimer*paramflagtheflagoftimer*returnthecreatedtimerobject*/rt_timer_trt_timer_create(constchar*name,void(*timeout)(void*parameter),void*parameter,rt_tick_ttime,rt_uint8_tflag)structrt_timer*timer;/*allocateaobject*/timer=(structrt_timer*)rt_object_allocate(RT
59、_Object_Class_Timer,name);/動態(tài)分配定時器內(nèi)核對象if(timer=RT_NULL)returnRT_NULL;_rt_timer_init(timer,timeout,parameter,time,flag);/調(diào)用上述的初始化接口returntimer;4.2脫離和刪除脫離:cppHYPERLINK/flydream0/article/details/8570841oviewplainviewplainHYPERLINK/flydream0/article/details/8570841ocopycopyHYPERLINK/flydream0/article/de
60、tails/8570841oprintprintHYPERLINK/flydream0/article/details/8570841o?/*Thisfunctionwilldetachatimerfromtimermanagement.*paramtimerthestatictimerobject*returntheoperationstatus,RT_EOKonOK;RT_ERRORonerror*/rt_err_trt_timer_detach(rt_timer_ttimer)registerrt_base_tlevel;/*timercheck*/RT_ASSERT(timer!=RT
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 房屋信托合同
- 委托技術(shù)開發(fā)合同樣本
- 紹興市柯橋區(qū)事業(yè)單位招聘真題2024
- 河南鄭州大學(xué)第二附屬醫(yī)院招聘真題2024
- 海南省公考真題2024
- 2024年鳳城市市屬事業(yè)單位考試真題
- 2025年離子及射線檢測、分析儀器合作協(xié)議書
- 2025年秋季學(xué)生活動組織計(jì)劃
- 整形外科危急值報(bào)告及流程管理
- 高速公路預(yù)制箱梁施工技術(shù)措施
- 中小學(xué)校長在教師大會上講話:以八項(xiàng)規(guī)定精神引領(lǐng)教育高質(zhì)量發(fā)展根深?重明?規(guī)立?法新?行遠(yuǎn)
- 食品安全管理制度打印版
- GB/T 45251-2025互聯(lián)網(wǎng)金融個人網(wǎng)絡(luò)消費(fèi)信貸貸后催收風(fēng)控指引
- 關(guān)于除顫儀的試題及答案
- 2025年北京電子科技職業(yè)學(xué)院高職單招高職單招英語2016-2024歷年頻考點(diǎn)試題含答案解析
- 第一屆貴州技能大賽銅仁市選拔賽平面設(shè)計(jì)技術(shù)文件
- 2025年陜西農(nóng)業(yè)發(fā)展集團(tuán)有限公司(陜西省土地工程建設(shè)集團(tuán))招聘(200人)筆試參考題庫附帶答案詳解
- 2024-2025學(xué)年度一年級第二學(xué)期月考第一二單元語文試題(含答案)
- 2024-2025學(xué)年湖南省長沙市芙蓉區(qū)長郡雙語洋湖實(shí)驗(yàn)中學(xué)九年級下學(xué)期入學(xué)考試英語試題(含答案)
- GB/T 6433-2025飼料中粗脂肪的測定
- 【MOOC期末】《英美文學(xué)里的生態(tài)》(北京林業(yè)大學(xué))期末中國大學(xué)慕課MOOC答案
評論
0/150
提交評論