單片機(jī)操作系統(tǒng)RTX51原理分析與移植_第1頁
單片機(jī)操作系統(tǒng)RTX51原理分析與移植_第2頁
單片機(jī)操作系統(tǒng)RTX51原理分析與移植_第3頁
單片機(jī)操作系統(tǒng)RTX51原理分析與移植_第4頁
單片機(jī)操作系統(tǒng)RTX51原理分析與移植_第5頁
已閱讀5頁,還剩37頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

單片機(jī)操作系統(tǒng)RTX51原理分析與移植發(fā)布時(shí)間:2009-7-25

閱讀次數(shù):286

字體大小:【小】【中】【大】一.課題要求:仔細(xì)分析單片機(jī)操作系統(tǒng)RTX51的原理,將其移植到單片機(jī)實(shí)驗(yàn)平臺(tái)上,并要求編寫一個(gè)簡(jiǎn)短的程序來驗(yàn)證其運(yùn)行的正確性(比如,編寫兩個(gè)具有顯示不一樣內(nèi)容的任務(wù))。二.RTX51原理注釋:由于英文原文會(huì)比較好,所以我沒有翻譯過來(不過也寫了部分的翻譯),直接提取出來一些英文,總結(jié)在一起。1.

進(jìn)程管理(TaskManagement)1)

進(jìn)程類型(ClassesofTasks)RTX-51recognizestwoclassesoftasks:1.

FasttasksnContainespeciallyshortresponsesandinterruptdisabletimes.nContainaseparateregisterbankandaseparatestackarea(registerbanks1,2and3).(Figure1)nContainthehighesttaskpriority(priority3)andcanthereforeinterruptstandardtasks.nAllcontainthesamepriorityandcanthereforenotbemutuallyinterrupted.nCanbeinterruptedbyc51interruptfunctions.nAmaximumofthreefasttaskscanbeactiveinthesystem.2.

StandardtasksnRequiresomewhatmoretimeforthetaskswitchingcomparedtofasttasks.nShareacommonregisterbankandacommonstackarea(registerbank0).nThecurrentcontentsofregistersandstackarestoredintheexternal(XDATA)memoryduringataskchange.nCanbeinterruptedbyfasttasks.nCaninterruptthemselvesmutually.nCanbeinterruptbyc51interruptfunctions.nAmaximum16standardtaskscanbeactiveinthesystem.

<單片機(jī)操作系統(tǒng)RTX51原理分析>

Figure3:TaskClassesandMemoryAllocation每一個(gè)標(biāo)準(zhǔn)進(jìn)程都包含一個(gè)設(shè)備上下文在擴(kuò)展內(nèi)存(XDATA)中。在標(biāo)準(zhǔn)進(jìn)程執(zhí)行進(jìn)程切換的時(shí)候,會(huì)把它自己的Register和Stack存儲(chǔ)到對(duì)應(yīng)的設(shè)備上下文中(在擴(kuò)展內(nèi)存中的一個(gè)區(qū)域)。之后,Register和Statck又從設(shè)備上下文中重新載入,繼續(xù)執(zhí)行。(交換技術(shù))相比而言,快速進(jìn)程則不用這么麻煩,因?yàn)樗鼈冇懈髯元?dú)立的Register和Stack,所以只要激活對(duì)應(yīng)的Register(修改PSW)和指向Stack的指針(MovSP,#XX)即可。2)

進(jìn)程狀態(tài)(Taskstates)Time-slicetaskchange(round-robinscheduling)areexecutedifthefollowingconditionsaresatisfied:1.Round-robinschedulingmustbeenabled(seeconfiguration).2.TheRUNNINGtaskhasthepriorityof0andiscurrentlynotexecutingafloating-pointoperation(seesection"Floating-PointOperations",page28).3.AtleastonetaskwiththepriorityzeromustbeintheREADYstate.4.Thelasttaskchangemusthaveoccurredaftertheselectedsystemtimeinterval(seesystemfunction"os_set_slice").Thesystemtimeintervalcanbechangeddynamicallyduringprogramexecution.4)

進(jìn)程通信和同步(TaskCommunicationandSynchronisation)Therethreetwomechanisms:1.

SignalSignalsrepresentthesimplestandfastestformoftaskcommunication.Thesecanalwaysbeusedwhenapuretasksynchronisationisrequiredwithoutdataexchange.Eachactivetaskcontainsitsownsignalflagwithwhichthefollowingoperationscanbeexecuted:l

Waitforasignall

Sendsignall

ClearsignalThetasknumber(seesectionsection"TaskDeclaration")ofthereceivertaskisusedforidentifyingthesignalsfortheindividualoperations.2.

Message(viaMailBoxes(FIFO))nBymeansofthemailboxconcept,messagescanbeexchangedfreeofconflictsbetweentheindividualtasks.nRTX-51providesafixednumberofeightmailboxes.Messagescanbeexchangedinwords(2bytes)viathesemailboxes.Inthiscase,amessagecanrepresenttheactualdatatobetransferredortheidentificationofadatabuffer(definedbytheuser).Incomparisontothesignals,mailboxesarenotassignedafixedtask,butcanbefreelyusedbyalltasksandinterruptfunctions.Theseareidentifiedwithamailboxnumber.Mailboxesallowthefollowingoperations:l

Sendamessagel

ReadamessageEachmailboxisinternallyconsistsofthreewaitlists.(Figure2)

Figure21.

MessagelistListofthemessageswritteninthemailbox.Thesecompriseamaximumofeightmessages.2.

WritewaitlistWaitlistfortaskswhichwanttowriteamessageinthemessagelistofthemailbox(maximum16tasks).3.

ReadwaitlistWaitlistfortaskswhichwanttoreadamessagefromthemessagelistofthemailbox(maximum16tasks).3.

SemaphorenBymeansofthesemaphoreconcept,resourcescanbesharedfreeofconflictsbetweentheindividualtasks.nAsemaphorecontainsatokenthatyourcodeacquirestocontinueexecution.Iftheresourceisalreadyinuse,therequestingtaskisblockeduntilthetokenisreturnedtothesemaphorebyitscurrentowner.nTherearetwotypesofsemaphores:binarysemaphoresandcountingsemaphores.Asitsnameimplies,abinarysemaphorecanonlytaketwovalues:zeroorone(tokenisinorout).Acountingsemaphore,however,allowsvaluesbetweenzeroand65535.RTX-51providesafixednumberofeightsemaphoresofthebinarytype.Semaphoresallowthefollowingoperations:l

Waitfortokenl

Return(send)token2.

中斷管理RTX-51performstasksynchronisationforexternaleventsbymeansoftheinterruptsystem.Twotypesofinterruptprocessingarebasicallysupportedinthiscase:1.單片機(jī)c語言InterruptFunctions(Interruptareprocessedbyc51interruptfuncions)lVerysudden,periodicallyoccurringinterruptswithoutlargecouplingwiththerestofthesystem(onlyinfrequentcommunicationwithRTX-51tasks,etc.).lVeryimportantinterruptswhichmustbeservedimmediatelyindependentofthecurrentsystemstate.2.TaskInterrupts(InterruptareprocessedbyfastorstandardtasksofRTX-51l

FastTaskInterruptsImportantorperiodicinterruptswhichmustheavilycommunicatewiththerestofthesystemwhentheyoccur.l

StandardTaskInterruptsOnlyseldomoccurringinterruptswhichmustnotbeservedimmediately.RTX-51showsconsiderabledifferentresponsetimesforfastandstandardtasks.u

TheINTERRUPTENABLEregistersofthe8051aremanagedbyRTX-51andmustnotbedirectlymanipulatedbytheuser!uTheInterruptPriorityregistersofthe8051(nottobeconfusedwiththesoftwaretaskpriorities)arenotinfluencedbyRTX-51.3.

動(dòng)態(tài)內(nèi)存管理RTX-51usesasimpleandeffectivealgorithm,whichfunctionswithmemoryblocksofafixedsize.Allmemoryblocksofthesamesizearemanagedinasocalledmemorypool.Amaximumof16memorypoolseachadifferentblocksizecanbedefined.Amaximumof255memoryblockscanbemanagedineachpool.n

GenerateMemoryPoolTheapplicationcangenerateamaximumof16memorypoolswithvariousblocksizes.TheapplicationmustprovideanXDATAareaforthispurpose.ThepoolisstoredandmanagedbyRTX-51in

thisarea(seesystemfunction"os_create_pool").n

RequestMemoryBlockfromPoolAssoonasapoolhasbeengenerated,theapplicationcanrequestmemoryblocks.Theindividualpoolsareidentifiedbytheirblocksizeinthiscase.Ifanadditionalblockisstillfreeinthepool,RTX-51suppliesthestartaddressofthisblocktotheapplication.Ifnoblockisfree,anullpointerisreturned(seesystemfunction"os_get_block").n

ReturnMemoryBlocktoPoolIftheapplicationnolongerneedsarequestedmemoryblock,itcanbereturnedtothepoolforadditionaluse(seesystemfunction"os_free_block").4.

時(shí)間管理RTX-51maintainsaninternaltimecounter,whichmeasurestherelativetimepassedsincesystemstart.Thephysicalsourceofthistimebaseisahardwaretimerthatgeneratesaninterruptperiodically.Thetimepassedbetweentheseinterruptsiscalledasystemtimesliceorasystemtick.Thistimebaseisusedtosupporttimedependentservices,suchaspauseortimeoutonataskwait.Threetime-relatedfunctionsaresupported:n

SetsystemtimesliceTheperiodbetweentheinterruptsofthesystemtimersetsthe"granularity"ofthetimebase.Thelengthofthisperiod,alsocalledatimeslice,canbesetbytheapplicationinawiderange(seesystemfunction"os_set_slice").n

DelayataskAtaskmaybedelayedforaselectablenumberoftimeslices.Uponcallingthissystemfunctionthetaskwillbeblocked(sleep)untilthespecifiednumberofsystemtickshaspassed(seesystemfunction"os_wait").n

CyclictaskactivationFormanyreal-timeapplicationsitisarequirementtodosomethingonaregularbasis.AperiodictaskactivationcanbeachievedbytheRTXintervalwaitfunction(seesystemfunction"os_wait").Theamountoftimespentbetweentwoexecutionperiodsofthesametaskiscontrolled,usingos_wait,andismeasuredinnumberofsystemticksandmaybesetbytheapplication.5.

RTX(FULL)函數(shù)縱覽(FunctionsOverview)InitializeandStarttheSystem:os_start_system(task_number)TaskManagement:os_create_task(task_number)os_delete_task(task_number)os_running_task_id()InterruptManagement:os_attach_interrupt(interrupt)os_detach_interrupt(interrupt)os_enable_isr(interrupt)os_disable_isr(interrupt)os_wait(event_selector,timeout,0)oi_set_int_masks(ien0,ien1,ien2)oi_reset_int_masks(ien0,ien1,ien2)SignalFunctions:os_send_signal(task_number)os_wait(event_selector,timeout,0)os_clear_signal(task_number)isr_send_signal(task_number)MessageFunctions:os_send_message(mailbox,message,timeout)os_wait(event_selector,timeout,*message)isr_send_message(mailbox,message)isr_recv_message(mailbox,*message)SemaphoreFunctions:os_send_token(semaphore)os_wait(event_selector,timeout,0)DynamicMemoryManagement:os_create_pool(block_size,*memory,mem_size)os_get_block(block_size)os_free_block(block_size,*block)FunctionswiththeSystemClock:os_set_slice(timeslice)os_wait(event_selector,timeout,0)DebugFunctions:os_check_tasks(*table)os_check_task(task_number,*table)os_check_mailboxes(*table)os_check_mailbox(mailbox,*table)os_check_semaphores(*table)os_check_semaphore(semaphore,*table)os_check_pool(block_size,*table)三.RTX51移植本試驗(yàn)是用的RTX的Tiny版本。也就是說沒有優(yōu)先級(jí)之分,沒有郵箱機(jī)制,沒有動(dòng)態(tài)內(nèi)存的管理。移植它很簡(jiǎn)單,就是配置一下它帶的配置文件,然后和寫好的程序一起編譯連接,連接的時(shí)候加一個(gè)rtxtny參數(shù),意思是說當(dāng)我連接的時(shí)候,我把RTXtiny的庫文件連接上,也就等于是程序和操作系統(tǒng)編譯在一起了。該配置文件能在安裝目的rtxtiny2底下找到。文件名稱為Conf_tny.A51,例如,在我的電腦中,路徑為:D:\Keil\單片機(jī)c語言\RtxTiny2\SourceCode\Conf_tny.A51。如下圖所示:由于試驗(yàn)箱里面的芯片是AT89C51,所以要配置Conf_tny.A51的RAMTOPEQU

07F,目的是說配置內(nèi)部RAM為128字節(jié)。四.源程序代碼:源程序代碼如下,說明請(qǐng)看代碼里面的注釋。/***RTX-51的移植**移植到AT89S52**此程序是循環(huán)花樣顯示LED燈**有三個(gè)顯示樣式,分別對(duì)應(yīng)下面的三個(gè)進(jìn)程**下面的算法中用到了“時(shí)間到空間”的轉(zhuǎn)換,使得算法簡(jiǎn)化不少**此程序我已在最小系統(tǒng)板上試驗(yàn)通過。*/#include#includeconstunsignedchartable[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0xFF,0x00};/*時(shí)間到空間的轉(zhuǎn)換,如果table是:constunsignedchartable[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};則算法和代碼會(huì)多出一倍來。table數(shù)組雖然增加了一倍,算法也隨之減少了一半,好處當(dāng)然不止在這里體現(xiàn),下面的進(jìn)程2也減少了一半*///進(jìn)程0左->右->左voidLED0(void)_task_0{inti;os_create_task(1);//創(chuàng)建進(jìn)程1os_create_task(2);//創(chuàng)建進(jìn)程2while(1){for(i=0;i<15;i++){P1=table[i];os_wait(K_TMO,30,0);//等待30*10000微妙=0.3秒}os_send_signal(1);//發(fā)送Signal信號(hào),激活進(jìn)程1os_wait(K_SIG,0,0);//等待信號(hào)}}//進(jìn)程1全亮->全滅->全亮voidLED1(void)_task_1{inti;while(1){os_wait(K_SIG,0,0);for(i=0;i<3;i++){P1=table[15];//全亮os_wait(K_TMO,30,0);P1=table[16];//全滅os_wait(K_TMO,30,0);}os_send_signal(2);}}//進(jìn)程2兩邊->中間中間->兩邊voidLED2(void)_task_2{inti;while(1){os_wait(K_SIG,0,0);for(i=0;i<8;i++){P1=table[i]|table[i+7];//由于table長(zhǎng)度多一倍,省去了一個(gè)循環(huán),而且算法也簡(jiǎn)化了。os_wait(K_TMO,30,0);}os_send_signal(0);}}五.總結(jié):本試驗(yàn)用的RTX的Tiny版本。許多比較高級(jí)的功能沒有去實(shí)現(xiàn)。目的主要是理解RTX的原理,然后移植它到某個(gè)單片機(jī)上面,編寫個(gè)小程序來測(cè)試一下。通過閱讀RTX附帶的英文文檔,我對(duì)此操作系統(tǒng)有了深刻的認(rèn)識(shí),感到此操作系統(tǒng)有很多優(yōu)點(diǎn),也有很多不足的地方。比如支持的任務(wù)較少,不過由于是單片機(jī),“承受”能力也有限,也能理解??偟膩碚f,對(duì)于單片機(jī)來說是個(gè)不錯(cuò)的操作系統(tǒng)。六.參考文檔:[1]《RTX-51官方英文文檔》(包括FULL版,和Tiny版)[2]《KeilSoftware–Cx51編譯器用戶手冊(cè)》中文版

文檔內(nèi)容版權(quán)所有;代碼開源,能復(fù)制應(yīng)用單片機(jī)操作系統(tǒng)RTX51原理分析與移植單片機(jī),

操作系統(tǒng),

原理,

移植分享到:新浪微博QQ空間開心網(wǎng)人人網(wǎng)一.課題要求:

仔細(xì)分析單片機(jī)操作系統(tǒng)RTX51的原理,將其移植到單片機(jī)實(shí)驗(yàn)平臺(tái)上,并要求編寫一個(gè)簡(jiǎn)短的程序來驗(yàn)證其運(yùn)行的正確性(比如,編寫兩個(gè)具有顯示不一樣內(nèi)容的任務(wù))。

二.RTX51原理

注釋:由于英文原文會(huì)比較好,所以我沒有翻譯過來(不過也寫了部分的翻譯),直接提取出來一些英文,總結(jié)在一起。

1.進(jìn)程管理(TaskManagement)

1)進(jìn)程類型(ClassesofTasks)

RTX-51recognizestwoclassesoftasks:

1.Fasttasks

nContainespeciallyshortresponsesandinterruptdisabletimes.

nContainaseparateregisterbankandaseparatestackarea(registerbanks1,2and3).(Figure1)

nContainthehighesttaskpriority(priority3)andcanthereforeinterruptstandardtasks.

nAllcontainthesamepriorityandcanthereforenotbemutuallyinterrupted.

nCanbeinterruptedbyc51interruptfunctions.

nAmaximumofthreefasttaskscanbeactiveinthesystem.

2.Standardtasks

nRequiresomewhatmoretimeforthetaskswitchingcomparedtofasttasks.

nShareacommonregisterbankandacommonstackarea(registerbank0).

nThecurrentcontentsofregistersandstackarestoredintheexternal(XDATA)memoryduringataskchange.

nCanbeinterruptedbyfasttasks.

nCaninterruptthemselvesmutually.

nCanbeinterruptbyc51interruptfunctions.

nAmaximum16standardtaskscanbeactiveinthesystem.

<單片機(jī)操作系統(tǒng)RTX51原理分析>

Figure3:TaskClassesandMemoryAllocation

每一個(gè)標(biāo)準(zhǔn)進(jìn)程都包含一個(gè)設(shè)備上下文在擴(kuò)展內(nèi)存(XDATA)中。在標(biāo)準(zhǔn)進(jìn)程執(zhí)行進(jìn)程切換的時(shí)候,會(huì)把它自己的Register和Stack存儲(chǔ)到對(duì)應(yīng)的設(shè)備上下文中(在擴(kuò)展內(nèi)存中的一個(gè)區(qū)域)。之后,Register和Statck又從設(shè)備上下文中重新載入,繼續(xù)執(zhí)行。(交換技術(shù))

相比而言,快速進(jìn)程則不用這么麻煩,因?yàn)樗鼈冇懈髯元?dú)立的Register和Stack,所以只要激活對(duì)應(yīng)的Register(修改PSW)和指向Stack的指針(MovSP,#XX)即可。

2)進(jìn)程狀態(tài)(Taskstates)

RTX-51recognizesfourtaskstates:

1.READYAlltaskswhichcanrunareREADY.OneofthesetasksistheRUNNING(ACTIVE)task.

2.RUNNING(ACTIVE)Taskwhichiscurrentlybeingexecutedbytheprocessor.Onlyonetask(maximum)canbeinthisstateatatime.

3.BLOCKED(WAITING)Taskwaitsforanevent.

4.SLEEPINGAlltaskswhichwerenotstartedorwhichhaveterminatedthemselvesareinthisstate.

3)進(jìn)程調(diào)度(Taskswitch)

TheRTX-51systemsectionwhichtheprocessorsassignstotheindividualtasksisreferredtoasthescheduler(alsodispatcher).

TheRTX_51schedulerworksaccordingtothefollowingrules:

1.ThetaskwiththehighestpriorityofalltasksintheREADYstateisexecuted.

2.IfseveraltasksofthesamepriorityareintheREADYstate,thetaskthathasbeenreadythelongestwillbethenexttoexecute.

3.Taskswitchingsareonlyexecutedifthefirstrulewouldhavebeenotherwiseviolated(exception:round-robinscheduling).

Time-slicetaskchange(round-robinscheduling)areexecutedifthefollowingconditionsaresatisfied:

1.Round-robinschedulingmustbeenabled

(seeconfiguration).

2.TheRUNNINGtaskhasthepriorityof0andiscurrentlynotexecutingafloating-pointoperation(seesection"Floating-PointOperations",page28).

3.AtleastonetaskwiththepriorityzeromustbeintheREADYstate.

4.Thelasttaskchangemusthaveoccurredaftertheselectedsystemtimeinterval(seesystemfunction"os_set_slice").Thesystemtimeintervalcanbechangeddynamicallyduringprogramexecution.

4)進(jìn)程通信和同步(TaskCommunicationandSynchronisation)

Therethreetwomechanisms:

1.Signal

Signalsrepresentthesimplestandfastestformoftaskcommunication.Thesecanalwaysbeusedwhenapuretasksynchronisationisrequiredwithoutdataexchange.

Eachactivetaskcontainsitsownsignalflagwithwhichthefollowingoperationscanbeexecuted:

lWaitforasignal

lSendsignal

lClearsignal

Thetasknumber(seesectionsection"TaskDeclaration")ofthereceivertaskisusedforidentifyingthesignalsfortheindividualoperations.

2.Message(viaMailBoxes(FIFO))

nBymeansofthemailboxconcept,messagescanbeexchangedfreeofconflictsbetweentheindividualtasks.

nRTX-51providesafixednumberofeightmailboxes.Messagescanbeexchangedinwords(2bytes)viathesemailboxes.Inthiscase,amessagecanrepresenttheactualdatatobetransferredortheidentificationofadatabuffer(definedbytheuser).Incomparisontothesignals,mailboxesarenotassignedafixedtask,butcanbefreelyusedbyalltasksandinterruptfunctions.Theseareidentifiedwithamailboxnumber.

Mailboxesallowthefollowingoperations:

lSendamessage

lReadamessage

Eachmailboxisinternallyconsistsofthreewaitlists.(Figure2)

點(diǎn)此在新窗口瀏覽圖片

Figure2

1.Messagelist

Listofthemessageswritteninthemailbox.Thesecompriseamaximumofeightmessages.

2.Writewaitlist

Waitlistfortaskswhichwanttowriteamessageinthemessagelistofthemailbox(maximum16tasks).

3.Readwaitlist

Waitlistfortaskswhichwanttoreadamessagefromthemessagelistofthemailbox(maximum16tasks).

3.Semaphore

nBymeansofthesemaphoreconcept,resourcescanbesharedfreeofconflictsbetweentheindividualtasks.

nAsemaphorecontainsatokenthatyourcodeacquirestocontinueexecution.Iftheresourceisalreadyinuse,therequestingtaskisblockeduntilthetokenisreturnedtothesemaphorebyitscurrentowner.

nTherearetwotypesofsemaphores:binarysemaphoresandcountingsemaphores.Asitsnameimplies,abinarysemaphorecanonlytaketwovalues:zeroorone(tokenisinorout).Acountingsemaphore,however,allowsvaluesbetweenzeroand65535.

RTX-51providesafixednumberofeightsemaphoresofthebinarytype.

Semaphoresallowthefollowingoperations:

lWaitfortoken

lReturn(send)token

2.中斷管理

RTX-51performstasksynchronisationforexternaleventsbymeansoftheinterruptsystem.

Twotypesofinterruptprocessingarebasicallysupportedinthiscase:

1.單片機(jī)c語言InterruptFunctions(Interruptareprocessedbyc51interruptfuncions)

lVerysudden,periodicallyoccurringinterruptswithoutlargecouplingwiththerestofthesystem(onlyinfrequentcommunicationwithRTX-51tasks,etc.).

lVeryimportantinterruptswhichmustbeservedimmediatelyindependentofthecurrentsystemstate.

2.TaskInterrupts(InterruptareprocessedbyfastorstandardtasksofRTX-51

lFastTaskInterrupts

Importantorperiodicinterruptswhichmustheavilycommunicatewiththerestofthesystemwhentheyoccur.

lStandardTaskInterrupts

Onlyseldomoccurringinterruptswhichmustnotbeservedimmediately.

RTX-51showsconsiderabledifferentresponsetimesforfastandstandardtasks.

uTheINTERRUPTENABLEregistersofthe8051aremanagedbyRTX-51andmustnotbedirectlymanipulatedbytheuser!

uTheInterruptPriorityregistersofthe8051(nottobeconfusedwiththesoftwaretaskpriorities)arenotinfluencedbyRTX-51.

3.動(dòng)態(tài)內(nèi)存管理

RTX-51usesasimpleandeffectivealgorithm,whichfunctionswithmemoryblocksofafixedsize.Allmemoryblocksofthesamesizearemanagedinasocalledmemorypool.Amaximumof16memorypoolseachadifferentblocksizecanbedefined.Amaximumof255memoryblockscanbemanagedineachpool.

nGenerateMemoryPool

Theapplicationcangenerateamaximumof16memorypoolswithvariousblocksizes.TheapplicationmustprovideanXDATAareaforthispurpose.ThepoolisstoredandmanagedbyRTX-51inthisarea(seesystemfunction"os_create_pool").

nRequestMemoryBlockfromPool

Assoonasapoolhasbeengenerated,theapplicationcanrequestmemory

blocks.Theindividualpoolsareidentifiedbytheirblocksizeinthiscase.

Ifanadditionalblockisstillfreeinthepool,RTX-51suppliesthestartaddress

ofthisblocktotheapplication.Ifnoblockisfree,anullpointerisreturned(see

systemfunction"os_get_block").

nReturnMemoryBlocktoPool

Iftheapplicationnolongerneedsarequestedmemoryblock,itcanbereturned

tothepoolforadditionaluse(seesystemfunction"os_free_block").

4.

時(shí)間管理

RTX-51maintainsaninternaltimecounter,whichmeasurestherelativetimepassedsincesystemstart.Thephysicalsourceofthistimebaseisahardwaretimerthatgeneratesaninterruptperiodically.Thetimepassedbetweentheseinterruptsiscalledasystemtimesliceorasystemtick.

Thistimebaseisusedtosupporttimedependentservices,suchaspauseortimeoutonataskwait.

Threetime-relatedfunctionsaresupported:

nSetsystemtimeslice

Theperiodbetweentheinterruptsofthesystemtimersetsthe"granularity"ofthetimebase.Thelengthofthisperiod,alsocalledatimeslice,canbesetbytheapplicationinawiderange(seesystemfunction"os_set_slice").

nDelayatask

Ataskmaybedelayedforaselectablenumberoftimeslices.Uponcallingthissystemfunctionthetaskwillbeblocked(sleep)untilthespecifiednumberofsystemtickshaspassed(seesystemfunction"os_wait").

nCyclictaskactivation

Formanyreal-timeapplicationsitisarequirementtodosomethingonaregularbasis.AperiodictaskactivationcanbeachievedbytheRTXintervalwaitfunction(seesystemfunction"os_wait").Theamountoftimespentbetweentwoexecutionperiodsofthesametaskiscontrolled,usingos_wait,andismeasuredinnumberofsystemticksandmaybesetbytheapplication.

5.RTX(FULL)函數(shù)縱覽(FunctionsOverview)

InitializeandStarttheSystem:

os_start_system(task_number)

TaskManagement:

os_create_task(task_number)

os_delete_task(task_number)

os_running_task_id()

InterruptManagement:

os_attach_interrupt(interrupt)

os_detach_interrupt(interrupt)

os_enable_isr(interrupt)

os_disable_isr(interrupt)

os_wait(event_selector,timeout,0)

oi_set_int_masks(ien0,ien1,ien2)

oi_reset_int_masks(ien0,ien1,ien2)

SignalFunctions:

os_send_signal(task_number)

os_wait(event_selector,timeout,0)

os_clear_signal(task_number)

isr_send_signal(task_number)

MessageFunctions:

os_send_message(mailbox,message,timeout)

os_wait(event_selector,timeout,*message)

isr_send_message(mailbox,message)

isr_recv_message(mailbox,*message)

SemaphoreFunctions:

os_send_token(semaphore)

os_wait(event_selector,timeout,0)

DynamicMemoryManagement:

os_create_pool(block_size,*memory,mem_size)

os_get_block(block_size)

os_free_block(block_size,*block)

FunctionswiththeSystemClock:

os_set_slice(timeslice)

os_wait(event_selector,timeout,0)

DebugFunctions:

os_check_tasks(*table)

os_check_task(task_number,*table)

os_check_mailboxes(*table)

os_check_mailbox(mailbox,*table)

os_check_semaphores(*table)

os_check_semaphore(semaphore,*table)

os_check_pool(block_size,*table)

三.RTX51移植

本試驗(yàn)是用的RTX的Tiny版本。也就是說沒有優(yōu)先級(jí)之分,沒有郵箱機(jī)制,沒有動(dòng)態(tài)內(nèi)存的管理。移植它很簡(jiǎn)單,就是配置一下它帶的配置文件,然后和寫好的程序一起編譯連接,連接的時(shí)候加一個(gè)rtxtny參數(shù),意思是說當(dāng)我連接的時(shí)候,我把RTXtiny的庫文件連接上,也就等于是程序和操作系統(tǒng)編譯在一起了。該配置文件能在安裝目的rtxtiny2底下找到。文件名稱為Conf_tny.A51,例如,在我的電腦中,路徑為:D:\Keil\單片機(jī)c語言\RtxTiny2\SourceCode\Conf_tny.A51。如下圖所示:

由于試驗(yàn)箱里面的芯片是AT89C51,所以要配置Conf_tny.A51的RAMTOPEQU07F,目的是說配置內(nèi)部RAM為128字節(jié)。

四.源程序代碼:

源程序代碼如下,說明請(qǐng)看代碼里面的注釋。

/*

**RTX-51的移植

**移植到AT89S52

**此程序是循環(huán)花樣顯示LED燈

**有三個(gè)顯示樣式,分別對(duì)應(yīng)下面的三個(gè)進(jìn)程

**下面的算法中用到了“時(shí)間到空間”的轉(zhuǎn)換,使得算法簡(jiǎn)化不少

**此程序我已在最小系統(tǒng)板上試驗(yàn)通過。

*/

#include

#include

constunsignedchartable[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80

,0x40,0x20,0x10,0x08,0x04,0x02,0x01,0xFF,0x00};

/*時(shí)間到空間的轉(zhuǎn)換,如果table是:

constunsignedchartable[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

則算法和代碼會(huì)多出一倍來。table數(shù)組雖然增加了一倍,算法也隨之減少了一半,

好處當(dāng)然不止在這里體現(xiàn),下面的進(jìn)程2也減少了一半*/

//進(jìn)程0左->右->左

voidLED0(void)_task_0

{

inti;

os_create_task(1);//創(chuàng)建進(jìn)程1

os_create_task(2);//創(chuàng)建進(jìn)程2

while(1)

{

for(i=0;i<15;i++)

{

P1=table[i];

os_wait(K_TMO,30,0);//等待30*10000微妙=0.3秒

}

os_send_signal(1);//發(fā)送Signal信號(hào),激活進(jìn)程1

os_wait(K_SIG,0,0);//等待信號(hào)

}

}

//進(jìn)程1全亮->全滅->全亮

voidLED1(void)_task_1

{

inti;

while(1)

{

os_wait(K_SIG,0,0);

for(i=0;i<3;i++)

{

P1=table[15];//全亮

os_wait(K_TMO,30,0);

P1=table[16];//全滅

os_wait(K_TMO,30,0);

}

os_send_signal(2);

}

}

//進(jìn)程2兩邊->中間中間->兩邊

voidLED2(void)_task_2

{

inti;

while(1)

{

os_wait(K_SIG,0,0);

for(i=0;i<8;i++)

{

P1=table[i]|table[i+7];//由于table長(zhǎng)度多一倍,省去了一個(gè)循環(huán),而且算法也簡(jiǎn)化了。

os_wait(K_TMO,30,0);

}

os_send_signal(0);

}

}

五.總結(jié):

本試驗(yàn)用的RTX的Tiny版本。許多比較高級(jí)的功能沒有去實(shí)現(xiàn)。目的主要是理解RTX的原理,然后移植它到某個(gè)單片機(jī)上面,編寫個(gè)小程序來測(cè)試一下。通過閱讀RTX附帶的英文文檔,我對(duì)此操作系統(tǒng)有了深刻的認(rèn)識(shí),感到此操作系統(tǒng)有很多優(yōu)點(diǎn),也有很多不足的地方。比如支持的任務(wù)較少,不過由于是單片機(jī),“承受”能力也有限,也能理解??偟膩碚f,對(duì)于單片機(jī)來說是個(gè)不錯(cuò)的操作系統(tǒng)。

六.參考文檔:

[1]《RTX-51官方英文文檔》(包括FULL版,和Tiny版)

[2]《KeilSoftware–Cx51編譯器用戶手冊(cè)》中文版單片機(jī)實(shí)時(shí)多任務(wù)操作系統(tǒng)分析[摘要]簡(jiǎn)要介紹美國Keil公司開發(fā)的應(yīng)用于MCS51系列單片機(jī)的實(shí)時(shí)多任務(wù)操作系統(tǒng)RTX51的基本情況和使用方法;分析這個(gè)內(nèi)核的任務(wù)管理和內(nèi)存管理的運(yùn)行機(jī)制及其對(duì)硬件的要求,給出其在軟件設(shè)計(jì)中應(yīng)注意的問題。

[關(guān)鍵詞]單片機(jī)C51RTX51

一、概述

一般說來,單片機(jī)的應(yīng)用系統(tǒng)中都需要同時(shí)執(zhí)行很多任務(wù),我們可以利用實(shí)時(shí)操作系統(tǒng)來靈活地安排系統(tǒng)資源。RTX51是美國Keil公司開發(fā)的一種小型的面向MCS51系列單片機(jī)的實(shí)時(shí)多任務(wù)操作系統(tǒng),它可以工作于8051單片機(jī)以及派生家族中,簡(jiǎn)化了復(fù)雜的軟件設(shè)計(jì),縮短了項(xiàng)目周期。

二、RTX51介紹

RTX51有2個(gè)模式:RTX51完全模式和最小模式。RTX51最小模式版是RTX51完全版的一個(gè)子集,可以很容易地運(yùn)行在8051系統(tǒng)上,而不需要外部RAM(DXATA)。RTX51完全模式有4個(gè)任務(wù)優(yōu)先級(jí),可以和中斷函數(shù)并行處理,各個(gè)任務(wù)之間通過使用“郵箱”系統(tǒng)來進(jìn)行信號(hào)和消息的傳遞,可以動(dòng)態(tài)地申請(qǐng)和釋放內(nèi)存;同時(shí),可以強(qiáng)制一個(gè)任務(wù)停止執(zhí)行,等待一個(gè)中斷,或者是其它中斷傳來的信號(hào)量或者消息。

(一)RTX51任務(wù)

RTX51包括2類任務(wù):快速任務(wù)和標(biāo)準(zhǔn)任務(wù)。快速任務(wù)有很快的響應(yīng)速度,每個(gè)快速任務(wù)使用8051一個(gè)單獨(dú)的寄存器組,并且有自己的堆棧區(qū)域。RTX51支持最大同時(shí)有3個(gè)快速任務(wù)。標(biāo)準(zhǔn)任務(wù)需要多一點(diǎn)的時(shí)間來進(jìn)行任務(wù)切換,因此使用的內(nèi)部RAM相對(duì)快速任務(wù)要少,所有的標(biāo)準(zhǔn)任務(wù)共用1個(gè)寄存器組和堆棧。當(dāng)任務(wù)切換的時(shí)候,當(dāng)前任務(wù)的寄存器狀態(tài)和堆棧內(nèi)容轉(zhuǎn)移到外部存儲(chǔ)器中。RTX51支持最大16個(gè)標(biāo)準(zhǔn)任務(wù)。

(二)RTX51任務(wù)狀態(tài)

(1)運(yùn)行(RUNNIGN)當(dāng)前正在運(yùn)行的任務(wù)處于RUNNING狀態(tài),同一時(shí)間只有1個(gè)任務(wù)可以運(yùn)行。(2)就緒(READY)等待運(yùn)行的任務(wù)處于READY狀態(tài),在當(dāng)前運(yùn)行的任務(wù)退出運(yùn)行狀態(tài)后,就緒隊(duì)列中優(yōu)先級(jí)最高的任務(wù)進(jìn)入到運(yùn)行狀態(tài)。

(3)阻塞(BLOCKED)等待一個(gè)事件的任務(wù)處于BLOCKED狀態(tài),如果事件發(fā)生且優(yōu)先級(jí)比正在運(yùn)行的任務(wù)高,此任務(wù)進(jìn)入運(yùn)行狀態(tài);如果優(yōu)先級(jí)比正在運(yùn)行的任務(wù)低,此任務(wù)進(jìn)入READY狀態(tài)。(4)刪除(DELETED)沒有開始的任務(wù)處于刪除狀態(tài)。

(三)RTX51中斷處理

RTX51完全模式提供2種方法來處理中斷:一種是C51的中斷函數(shù),另一種是RTX51的中斷。它又可以分為快速任務(wù)中斷和標(biāo)準(zhǔn)任務(wù)中斷。對(duì)于中斷函數(shù)這種方法,它同時(shí)也可以在不使用RTX51的情況下使用,當(dāng)中斷發(fā)生的時(shí)候,程序就跳到了相應(yīng)的中斷函數(shù),它和正在運(yùn)行的任務(wù)是互相獨(dú)立的,中斷的處理是在RTX51系統(tǒng)之外,和任務(wù)切換規(guī)則沒有關(guān)聯(lián)。對(duì)于任務(wù)中斷的方法,不管使用快速是標(biāo)準(zhǔn)任務(wù)來處理中斷,如果中斷發(fā)生,等待中斷的任務(wù)就從“等待”狀態(tài)進(jìn)入到就緒狀態(tài),并按照任務(wù)切換規(guī)則進(jìn)行切換。

(四)任務(wù)調(diào)度RTX51

包含一個(gè)事件驅(qū)動(dòng)的任務(wù)調(diào)度機(jī)制,它能夠按照任務(wù)的優(yōu)先級(jí)進(jìn)行切換,也就是搶占式多任務(wù)系統(tǒng);另外還有一個(gè)可選的時(shí)間片輪轉(zhuǎn)切換任務(wù)模式,在這種模式下,RTX51使用8051內(nèi)部定時(shí)器T0來產(chǎn)生定時(shí)節(jié)拍,各任務(wù)只在各自分配的定時(shí)節(jié)拍數(shù)(時(shí)間片)內(nèi)執(zhí)行。當(dāng)時(shí)間片用完后,切換至下一任務(wù)運(yùn)行,因此,各任務(wù)是并發(fā)執(zhí)行的。同級(jí)別的任務(wù)是按照時(shí)間片分別占用CPU的。RTX51任務(wù)有4個(gè)優(yōu)先級(jí):0、1、2可以分配給標(biāo)準(zhǔn)任務(wù),優(yōu)先級(jí)3是為快速任務(wù)保留的。每個(gè)任務(wù)都可以等待事件的發(fā)生,而并不增加系統(tǒng)的負(fù)擔(dān);任務(wù)可以等待消息、信號(hào)、中斷、超時(shí)事件或者它們的組合。

(五)任務(wù)控制

為了能描述和控制任務(wù)的運(yùn)行,RTX51為每個(gè)任務(wù)定義了稱作任務(wù)控制塊的數(shù)據(jù)結(jié)構(gòu),主要包括四項(xiàng)內(nèi)容:任務(wù)的代碼入口地址,位于CODE空間,2字節(jié)為一個(gè)單位。

任務(wù)所使用堆棧棧底位置,位于IDATA空間,1字節(jié)為一個(gè)單位。

任務(wù)的定時(shí)節(jié)拍計(jì)數(shù)器,在每一次定時(shí)節(jié)拍中斷后都自減一次。

任務(wù)狀態(tài)寄存器,用其各個(gè)位來表示任務(wù)所處的狀態(tài)。位于IDATA空間,以2字節(jié)為一單位。

(六)定時(shí)器T0中斷服務(wù)程序的任務(wù)

(1)RTX51使用定時(shí)器T0作為定時(shí)節(jié)拍發(fā)生器,是任務(wù)切換、時(shí)間片輪轉(zhuǎn)的依據(jù)。

(2)更新各個(gè)任務(wù)節(jié)拍數(shù):將各任務(wù)節(jié)拍數(shù)寄存器減1,如果某任務(wù)超時(shí)(STATE[taskid].timer=0),并且該任務(wù)正在等待超時(shí)事件,則將該任務(wù)置為“READY”狀態(tài),使其返回任務(wù)隊(duì)列。

(3)檢查自由堆??臻g:若自由堆??臻g范圍小于FREESTACK(默認(rèn)為20字節(jié))時(shí),可以調(diào)用宏STACK_ERROR,進(jìn)行堆棧出錯(cuò)處理。

三、RTX51應(yīng)用中應(yīng)注意的問題

原則講RTX51可以應(yīng)用到基于MCS51系列單片機(jī)組成的任何系統(tǒng)中。RTX51的優(yōu)點(diǎn)在于軟件開發(fā)周期短、效率高;由于RTX51在運(yùn)行時(shí)需要占用CPU的部分硬件資源,如通常占用定時(shí)/計(jì)數(shù)器T0,且對(duì)堆棧深度要求較高,因此,在使用時(shí)應(yīng)注意RTX51對(duì)硬件配置的要求。

(一)RTX51對(duì)硬件的要求

RTX51分為完全模式和最小模式,對(duì)于RTX51完全模式,要求系統(tǒng)要有外部RAM,同時(shí),還應(yīng)合理分配CPU的硬件資源,留出定時(shí)/計(jì)數(shù)器T0作為系統(tǒng)多任務(wù)執(zhí)行的時(shí)間片發(fā)生器;對(duì)于簡(jiǎn)單的嵌入式系統(tǒng),由于通常沒有外部RAM,通常選擇RTX51最小模式。在最小模式下,定時(shí)/計(jì)數(shù)器T0用戶不能使用,而是留給系統(tǒng)作多任務(wù)切換的時(shí)間片發(fā)生器。

(二)合理分配任務(wù)的時(shí)間片數(shù)和任務(wù)優(yōu)先級(jí)

在執(zhí)行多任務(wù)時(shí),由于各個(gè)任務(wù)處理時(shí),需要的時(shí)間不同,任務(wù)對(duì)實(shí)時(shí)性要求不同,所以,應(yīng)合理配置各個(gè)任務(wù)的時(shí)間片數(shù),盡量使任務(wù)在規(guī)定的時(shí)間片內(nèi)完成,減少任務(wù)間的消息傳遞,確保系統(tǒng)資源的使用效率。在RTX51完全模式下,應(yīng)注意各個(gè)任務(wù)的優(yōu)先級(jí)配置;在RTX51最小模式下,各個(gè)任務(wù)沒有優(yōu)先級(jí)規(guī)定,各任務(wù)只能在時(shí)間片定時(shí)器的控制下并行執(zhí)行。

四、結(jié)論

在CAN局域網(wǎng)節(jié)點(diǎn)設(shè)計(jì)過程中,我們成功地將RTX51最小模式應(yīng)用到系統(tǒng)之中,簡(jiǎn)化了系統(tǒng)的軟件設(shè)計(jì),提高了開發(fā)效率,縮短了開發(fā)周期。單片機(jī)實(shí)時(shí)多任務(wù)操作系統(tǒng)分析[摘要]簡(jiǎn)要介紹美國Keil公司開發(fā)的應(yīng)用于MCS51系列單片機(jī)的實(shí)時(shí)多任務(wù)操作系統(tǒng)RTX51的基本情況和使用方法;分析這個(gè)內(nèi)核的任務(wù)管理和內(nèi)存管理的運(yùn)行機(jī)制及其對(duì)硬件的要求,給出其在軟件設(shè)計(jì)中應(yīng)注意的問題。[關(guān)鍵詞]單片機(jī)C51RTX51

一、概述

一般說來,單片機(jī)的應(yīng)用系統(tǒng)中都需要同時(shí)執(zhí)行很多任務(wù),我們可以利用實(shí)時(shí)操作系統(tǒng)來靈活地安排系統(tǒng)資源。RTX51是美國Keil公司開發(fā)的一種小型的面向MCS51系列單片機(jī)的實(shí)時(shí)多任務(wù)操作系統(tǒng),它可以工作于8051單片機(jī)以及派生家族中,簡(jiǎn)化了復(fù)雜的軟件設(shè)計(jì),縮短了項(xiàng)目周期。

二、RTX51介紹

RTX51有2個(gè)模式:RTX51完全模式和最小模式。RTX51最小模式版是RT

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論