《嵌入式操作系統(tǒng)原理與面向任務(wù)程序設(shè)計》課件第三講M_第1頁
《嵌入式操作系統(tǒng)原理與面向任務(wù)程序設(shè)計》課件第三講M_第2頁
《嵌入式操作系統(tǒng)原理與面向任務(wù)程序設(shè)計》課件第三講M_第3頁
《嵌入式操作系統(tǒng)原理與面向任務(wù)程序設(shè)計》課件第三講M_第4頁
《嵌入式操作系統(tǒng)原理與面向任務(wù)程序設(shè)計》課件第三講M_第5頁
已閱讀5頁,還剩63頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

μC/OS-II初始化空閑任務(wù)控制塊鏈表空閑事件控制塊鏈表空閑內(nèi)存控制塊鏈表任務(wù)就緒組和任務(wù)就緒表空閑任務(wù)統(tǒng)計任務(wù)μC/OS-II內(nèi)核定時器任務(wù)空閑事件標(biāo)志組鏈表空閑消息隊列鏈表時鐘節(jié)拍任務(wù)狀態(tài)任務(wù)調(diào)度與內(nèi)核函數(shù)多任務(wù)啟動1、調(diào)用OSInit():初始化μC/OS-II操作系統(tǒng)2、調(diào)用OSTaskCreate(或OSTaskCreateExt):創(chuàng)建用戶任務(wù)3、調(diào)用OSStart():開啟多任務(wù)μC/OS-II應(yīng)用程序的典型結(jié)構(gòu)1voidmain(void)2{3OSInit();4OSTaskCreateExt(AppTaskStart,5(void*)0,6&AppTaskStartStk[TASK_START_STK_SIZE-1],7AppTaskStartPrio,8AppTaskStartID,9&AppTaskStartStk[0],10TASK_START_STK_SIZE,11(void*)0,12OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);13OSStart();14}μC/OS-II應(yīng)用程序主函數(shù)1、μC/OS-II應(yīng)用程序設(shè)計的第一步是調(diào)用OSInit()函數(shù)對μC/OS-II操作系統(tǒng)初始化;2、初始化μC/OS-II操作系統(tǒng)的全局變量或數(shù)據(jù)結(jié)構(gòu);3、建立空閑任務(wù)、統(tǒng)計任務(wù)和定時器管理等系統(tǒng)服務(wù)。3.1μC/OS-II初始化完成哪些工作?1voidOSInit(void)2{3OSInitHookBegin();/*Callportspecificinitializationcode*/45OS_InitMisc();/*Initializemiscellaneousvariables*/67OS_InitRdyList();/*InitializetheReadyList*/89OS_InitTCBList();/*InitializethefreelistofOS_TCBs*/1011OS_InitEventList();/*InitializethefreelistofOS_EVENTs*/3.1.1OSInit函數(shù)代碼13#if(OS_FLAG_EN>0)&&(OS_MAX_FLAGS>0)14OS_FlagInit();/*Initializetheeventflagstructures*/15#endif1617#if(OS_MEM_EN>0)&&(OS_MAX_MEM_PART>0)18OS_MemInit();/*Initializethememorymanager*/19#endif2021#if(OS_Q_EN>0)&&(OS_MAX_QS>0)22OS_QInit();/*Initializethemessagequeuestructures*/23#endif25OS_InitTaskIdle();/*CreatetheIdleTask*/26#ifOS_TASK_STAT_EN>027OS_InitTaskStat();/*CreatetheStatisticTask*/28#endif2930#ifOS_TMR_EN>031OSTmr_Init();/*InitializetheTimerManager*/32#endif34OSInitHookEnd();/*Callportspecificinit.code*/3536#ifOS_DEBUG_EN>037OSDebugInit();38#endif39}所謂的鉤子函數(shù)是指那些插入到某些函數(shù)中為擴展這些函數(shù)功能的函數(shù),一般地,鉤子函數(shù)為第三方軟件開發(fā)人員提供擴充軟件功能的入口點,同樣道理,μC/OS-II中有大量的鉤子函數(shù),用戶不需要修改μC/OS-II內(nèi)核程序代碼,而只需要向鉤子函數(shù)添加代碼就可以擴充μC/OS-II的功能。3.1.2什么是鉤子函數(shù)?1#ifOS_CPU_HOOKS_EN>0&&OS_VERSION>2032voidOSInitHookBegin(void)3{4INT32Usize;5OS_STK*pstk;67/*Clearexceptionstackforstackchecking.*/8pstk=&OS_CPU_ExceptStk[0];9size=OS_CPU_EXCEPT_STK_SIZE;3.1.3OSInitHookBegin()函數(shù)代碼10while(size>0){11size--;12*pstk++=(OS_STK)0;13}1415#ifOS_STK_GROWTH==116OS_CPU_ExceptStkBase=&OS_CPU_ExceptStk[OS_CPU_EXCEPT_STK_SIZE-1];17#else18OS_CPU_ExceptStkBase=&OS_CPU_ExceptStk[0];19#endif21#ifOS_TMR_EN>022OSTmrCtr=0;23#endif24myInitHookBegin();25}26#endif1typedefunsignedcharBOOLEAN;2typedefunsignedcharINT8U;3typedefsignedcharINT8S;4typedefunsignedshortINT16U;5typedefsignedshortINT16S;6typedefunsignedintINT32U;7typedefsignedintINT32S;8typedeffloatFP32;9typedefdoubleFP64;1011typedefunsignedintOS_STK;12typedefunsignedintOS_CPU_SR;3.1.4μC/OS-II自定義變量類型有哪些1staticvoidOS_InitMisc(void)2{3#ifOS_TIME_GET_SET_EN>04OSTime=0L;5#endif67OSIntNesting=0;8OSLockNesting=0;910OSTaskCtr=0;3.1.5OS_InitMisc函數(shù)代碼1112OSRunning=OS_FALSE;1314OSCtxSwCtr=0;15OSIdleCtr=0L;1617#ifOS_TASK_STAT_EN>018OSIdleCtrRun=0L;19OSIdleCtrMax=0L;20OSStatRdy=OS_FALSE;21#endif22}3.2空閑任務(wù)控制塊鏈表1typedefstructos_tcb{2OS_STK*OSTCBStkPtr;34#ifOS_TASK_CREATE_EXT_EN>05void*OSTCBExtPtr;6OS_STK*OSTCBStkBottom;7INT32UOSTCBStkSize;8INT16UOSTCBOpt;9INT16UOSTCBId;10#endif3.2.1OS_TCB結(jié)構(gòu)體定義12structos_tcb*OSTCBNext;13structos_tcb*OSTCBPrev;1415#if(OS_EVENT_EN)||(OS_FLAG_EN>0)16OS_EVENT*OSTCBEventPtr;17#endif1819#if(OS_EVENT_EN)&&(OS_EVENT_MULTI_EN>0)20OS_EVENT**OSTCBEventMultiPtr;21#endif23#if((OS_Q_EN>0)&&(OS_MAX_QS>0))||(OS_MBOX_EN>0)24void*OSTCBMsg;25#endif2627#if(OS_FLAG_EN>0)&&(OS_MAX_FLAGS>0)28#ifOS_TASK_DEL_EN>029OS_FLAG_NODE*OSTCBFlagNode;30#endif31OS_FLAGSOSTCBFlagsRdy;32#endif34INT16UOSTCBDly;35INT8UOSTCBStat;36INT8UOSTCBStatPend;37INT8UOSTCBPrio;3839INT8UOSTCBX;40INT8UOSTCBY;41#ifOS_LOWEST_PRIO<=6342INT8UOSTCBBitX;43INT8UOSTCBBitY;44#else45INT16UOSTCBBitX;46INT16UOSTCBBitY;47#endif49#ifOS_TASK_DEL_EN>050INT8UOSTCBDelReq;51#endif5253#ifOS_TASK_PROFILE_EN>054INT32UOSTCBCtxSwCtr;55INT32UOSTCBCyclesTot;56INT32UOSTCBCyclesStart;57OS_STK*OSTCBStkBase;58INT32UOSTCBStkUsed;59#endif61#ifOS_TASK_NAME_SIZE>162INT8UOSTCBTaskName[OS_TASK_NAME_SIZE];63#endif64}OS_TCB;1staticvoidOS_InitTCBList(void)2{3INT8Ui;4OS_TCB*ptcb1;5OS_TCB*ptcb2;678OS_MemClr((INT8U*)&OSTCBTbl[0],sizeof(OSTCBTbl));9OS_MemClr((INT8U*)&OSTCBPrioTbl[0],sizeof(OSTCBPrioTbl));3.2.2初始化空閑TCB鏈表10ptcb1=&OSTCBTbl[0];11ptcb2=&OSTCBTbl[1];12for(i=0;i<(OS_MAX_TASKS+OS_N_SYS_TASKS-1);i++){13ptcb1->OSTCBNext=ptcb2;14#ifOS_TASK_NAME_SIZE>115ptcb1->OSTCBTaskName[0]='?';16ptcb1->OSTCBTaskName[1]=OS_ASCII_NUL;17#endif18ptcb1++;19ptcb2++;20}21ptcb1->OSTCBNext=(OS_TCB*)0;22#ifOS_TASK_NAME_SIZE>123ptcb1->OSTCBTaskName[0]='?';24ptcb1->OSTCBTaskName[1]=OS_ASCII_NUL;25#endif26OSTCBList=(OS_TCB*)0;27OSTCBFreeList=&OSTCBTbl[0];28}1voidOS_MemClr(INT8U*pdest,INT16Usize)2{3while(size>0){4*pdest++=(INT8U)0;5size--;6}7}3.2.3OS_MemClr函數(shù)3.2.4任務(wù)優(yōu)先級表3.3空閑事件控制塊鏈表3.3.1事件控制塊結(jié)構(gòu)體1#if(OS_EVENT_EN)&&(OS_MAX_EVENTS>0)2typedefstructos_event{3INT8UOSEventType;4void*OSEventPtr;5INT16UOSEventCnt;6#ifOS_LOWEST_PRIO<=637INT8UOSEventGrp;8INT8UOSEventTbl[OS_EVENT_TBL_SIZE];9#else10INT16UOSEventGrp;11INT16UOSEventTbl[OS_EVENT_TBL_SIZE];12#endif14#ifOS_EVENT_NAME_SIZE>115INT8UOSEventName[OS_EVENT_NAME_SIZE];16#endif17}OS_EVENT;18#endif3.3.2初始化空閑事件控制塊鏈表1staticvoidOS_InitEventList(void)2{3#if(OS_EVENT_EN)&&(OS_MAX_EVENTS>0)4#if(OS_MAX_EVENTS>1)5INT16Ui;6OS_EVENT*pevent1;7OS_EVENT*pevent2;810OS_MemClr((INT8U*)&OSEventTbl[0],sizeof(OSEventTbl));11pevent1=&OSEventTbl[0];12pevent2=&OSEventTbl[1];13for(i=0;i<(OS_MAX_EVENTS-1);i++){14pevent1->OSEventType=OS_EVENT_TYPE_UNUSED;15pevent1->OSEventPtr=pevent2;16#ifOS_EVENT_NAME_SIZE>117pevent1->OSEventName[0]='?';18pevent1->OSEventName[1]=OS_ASCII_NUL;19#endif20pevent1++;21pevent2++;}23pevent1->OSEventType=OS_EVENT_TYPE_UNUSED;24pevent1->OSEventPtr=(OS_EVENT*)0;25#ifOS_EVENT_NAME_SIZE>126pevent1->OSEventName[0]='?';27pevent1->OSEventName[1]=OS_ASCII_NUL;28#endif29OSEventFreeList=&OSEventTbl[0];30#else31OSEventFreeList=&OSEventTbl[0];32OSEventFreeList->OSEventType=OS_EVENT_TYPE_UNUSED;33OSEventFreeList->OSEventPtr=(OS_EVENT*)0;34#ifOS_EVENT_NAME_SIZE>135OSEventFreeList->OSEventName[0]='?';36OSEventFreeList->OSEventName[1]=OS_ASCII_NUL;37#endif38#endif39#endif40}3.3.3空閑事件控制塊指針OSEventFreeList和事件列表OSEventTbl定義1#if(OS_EVENT_EN)&&(OS_MAX_EVENTS>0)2OS_EXTOS_EVENT*OSEventFreeList;3OS_EXTOS_EVENTOSEventTbl[OS_MAX_EVENTS];4#endif3.4空閑內(nèi)存控制塊鏈表1#if(OS_MEM_EN>0)&&(OS_MAX_MEM_PART>0)2typedefstructos_mem{3void*OSMemAddr;4void*OSMemFreeList;5INT32UOSMemBlkSize;6INT32UOSMemNBlks;7INT32UOSMemNFree;8#ifOS_MEM_NAME_SIZE>19INT8UOSMemName[OS_MEM_NAME_SIZE];10#endif11}OS_MEM;3.4.1內(nèi)存控制塊(MCB)結(jié)構(gòu)體類型定義14typedefstructos_mem_data{15void*OSAddr;16void*OSFreeList;17INT32UOSBlkSize;18INT32UOSNBlks;19INT32UOSNFree;20INT32UOSNUsed;21}OS_MEM_DATA;22#endif1#if(OS_MEM_EN>0)&&(OS_MAX_MEM_PART>0)2OS_EXTOS_MEM*OSMemFreeList;3OS_EXTOS_MEMOSMemTbl[OS_MAX_MEM_PART4#endif3.4.2OSMemFreeList和OSMemTbl定義1voidOS_MemInit(void)2{3#ifOS_MAX_MEM_PART==14OS_MemClr((INT8U*)&OSMemTbl[0],sizeof(OSMemTbl));5OSMemFreeList=(OS_MEM*)&OSMemTbl[0];6#ifOS_MEM_NAME_SIZE>17OSMemFreeList->OSMemName[0]='?';8OSMemFreeList->OSMemName[1]=OS_ASCII_NUL;9#endif10#endif1112#ifOS_MAX_MEM_PART>=213OS_MEM*pmem;14INT16Ui;3.4.3初始化內(nèi)存管理器17OS_MemClr((INT8U*)&OSMemTbl[0],sizeof(OSMemTbl));18pmem=&OSMemTbl[0];19for(i=0;i<(OS_MAX_MEM_PART-1);i++){20pmem->OSMemFreeList=(void*)&OSMemTbl[i+1];21#ifOS_MEM_NAME_SIZE>122pmem->OSMemName[0]='?';23pmem->OSMemName[1]=OS_ASCII_NUL;24#endif25pmem++;26}27pmem->OSMemFreeList=(void*)0;28#ifOS_MEM_NAME_SIZE>129pmem->OSMemName[0]='?';30pmem->OSMemName[1]=OS_ASCII_NUL;31#endif3233OSMemFreeList=&OSMemTbl[0];34#endif35}3.5任務(wù)就緒組和任務(wù)就緒表1staticvoidOS_InitRdyList(void)2{3INT8Ui;4#ifOS_LOWEST_PRIO<=635INT8U*prdytbl;6#else7INT16U*prdytbl;8#endif91011OSRdyGrp=0;12prdytbl=&OSRdyTbl[0];13for(i=0;i<OS_RDY_TBL_SIZE;i++){14*prdytbl++=0;15}3.5.1任務(wù)就緒組和就緒表初始化函數(shù)OS_InitRdyList17OSPrioCur=0;18OSPrioHighRdy=0;1920OSTCBHighRdy=(OS_TCB*)0;21OSTCBCur=(OS_TCB*)0;22}1OS_EXTINT8UOSPrioCur;2OS_EXTINT8UOSPrioHighRdy;34#ifOS_LOWEST_PRIO<=635OS_EXTINT8UOSRdyGrp;6OS_EXTINT8UOSRdyTbl[OS_RDY_TBL_SIZE];7#else8OS_EXTINT16UOSRdyGrp;9OS_EXTINT16UOSRdyTbl[OS_RDY_TBL_SIZE];10#endif1112OS_EXTOS_TCB*OSTCBCur;13OS_EXTOS_TCB*OSTCBHighRdy;3.5.2任務(wù)就緒表和就緒組相關(guān)的全局變量1#ifOS_LOWEST_PRIO<=632#defineOS_EVENT_TBL_SIZE((OS_LOWEST_PRIO)/8+1)3#defineOS_RDY_TBL_SIZE((OS_LOWEST_PRIO)/8+1)4#else5#defineOS_EVENT_TBL_SIZE((OS_LOWEST_PRIO)/16+1)6#defineOS_RDY_TBL_SIZE((OS_LOWEST_PRIO)/16+1)7#endif1#ifOS_LOWEST_PRIO<=632ptcb->OSTCBY=(INT8U)(prio>>3);3ptcb->OSTCBX=(INT8U)(prio&0x07);4ptcb->OSTCBBitY=(INT8U)(1<<ptcb->OSTCBY);5ptcb->OSTCBBitX=(INT8U)(1<<ptcb->OSTCBX);6#else7ptcb->OSTCBY=(INT8U)((prio>>4)&0xFF);8ptcb->OSTCBX=(INT8U)(prio&0x0F);9ptcb->OSTCBBitY=(INT16U)(1<<ptcb->OSTCBY);10ptcb->OSTCBBitX=(INT16U)(1<<ptcb->OSTCBX);11#endif1213OSRdyGrp|=ptcb->OSTCBBitY;14OSRdyTbl[ptcb->OSTCBY]|=ptcb->OSTCBBitX;3.5.3根據(jù)就緒任務(wù)的優(yōu)先級prio計算其在任務(wù)就緒組和就緒表中的位置1#ifOS_LOWEST_PRIO<=632ptcb->OSTCBY=(INT8U)(prio>>3);3ptcb->OSTCBX=(INT8U)(prio&0x07);4ptcb->OSTCBBitY=(INT8U)(1<<ptcb->OSTCBY);5ptcb->OSTCBBitX=(INT8U)(1<<ptcb->OSTCBX);6#else7ptcb->OSTCBY=(INT8U)((prio>>4)&0xFF);8ptcb->OSTCBX=(INT8U)(prio&0x0F);9ptcb->OSTCBBitY=(INT16U)(1<<ptcb->OSTCBY);10ptcb->OSTCBBitX=(INT16U)(1<<ptcb->OSTCBX);11#endif12if((OSRdyTbl[ptcb->OSTCBY]&=~ptcb->OSTCBBitX)==0){14OSRdyGrp&=~ptcb->OSTCBBitY;15}3.5.4根據(jù)任務(wù)的優(yōu)先級prio清除其在任務(wù)就緒組和就緒表中的11INT8UconstOSUnMapTbl[256]={20,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,34,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,45,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,54,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,66,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,74,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,85,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,94,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,107,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,114,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,125,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,134,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,146,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,154,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,165,0,1,0,2,0,1,0,3,0,1,0,2,0,1,0,174,0,1,0,2,0,1,0,3,0,1,0,2,0,1,018};3.5.5優(yōu)先級解映射查找表OSUnMapTbl1#ifOS_LOWEST_PRIO<=632INT8Uy;345y=OSUnMapTbl[OSRdyGrp];6OSPrioHighRdy=(INT8U)((y<<3)+OSUnMapTbl[OSRdyTbl[y]]);7#else8INT8Uy;9INT16U*ptbl;103.5.6由任務(wù)就緒組和就緒表查最高優(yōu)先級任務(wù)優(yōu)先級號prio12if((OSRdyGrp&0xFF)!=0){13y=OSUnMapTbl[OSRdyGrp&0xFF];14}else{15y=OSUnMapTbl[(OSRdyGrp>>8)&0xFF]+8;16}17ptbl=&OSRdyTbl[y];18if((*ptbl&0xFF)!=0){19OSPrioHighRdy=(INT8U)((y<<4)+OSUnMapTbl[(*ptbl&0xFF)]);20}else{21OSPrioHighRdy=(INT8U)((y<<4)+OSUnMapTbl[(*ptbl>>8)&0xFF]+8);22}23#endif3.6什么是空閑任務(wù)空閑任務(wù)是μC/OS-II的系統(tǒng)任務(wù),當(dāng)沒有用戶任務(wù)執(zhí)行時,μC/OS-II將切換到空閑任務(wù)執(zhí)行,空閑任務(wù)具有最低的優(yōu)先級,空閑任務(wù)的優(yōu)先級總是OS_LOWEST_PRIO,并且空閑任務(wù)總是就緒的。1voidOS_TaskIdle(void*p_arg)2{3#ifOS_CRITICAL_METHOD==34OS_CPU_SRcpu_sr=0;5#endif6789(void)p_arg;10for(;;){11OS_ENTER_CRITICAL();12OSIdleCtr++;13OS_EXIT_CRITICAL();14OSTaskIdleHook();15}16}3.6.1空閑任務(wù)1staticvoidOS_InitTaskIdle(void)2{3#ifOS_TASK_NAME_SIZE>74INT8Uerr;5#endif678#ifOS_TASK_CREATE_EXT_EN>09#ifOS_STK_GROWTH==110(void)OSTaskCreateExt(OS_TaskIdle,3.6.2創(chuàng)建空閑任務(wù)函數(shù)OS_InitTaskIdle11(void*)0,12&OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE-1],13OS_TASK_IDLE_PRIO,14OS_TASK_IDLE_ID,15&OSTaskIdleStk[0],16OS_TASK_IDLE_STK_SIZE,17(void*)0,OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR19#else20(void)OSTaskCreateExt(OS_TaskIdle,21(void*)0,22&OSTaskIdleStk[0],23OS_TASK_IDLE_PRIO,24OS_TASK_IDLE_ID,25&OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE-1],26OS_TASK_IDLE_STK_SIZE,27(void*)0,28OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);#endif30#else31#ifOS_STK_GROWTH==132(void)OSTaskCreate(OS_TaskIdle,33(void*)0,34&OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE-1],35OS_TASK_IDLE_PRIO);36#else37(void)OSTaskCreate(OS_TaskIdle,38(void*)0,39&OSTaskIdleStk[0],40OS_TASK_IDLE_PRIO);41#endif#endif44#ifOS_TASK_NAME_SIZE>1445OSTaskNameSet(OS_TASK_IDLE_PRIO,(INT8U*)"uC/OS-IIIdle",&err);46#else47#ifOS_TASK_NAME_SIZE>748OSTaskNameSet(OS_TASK_IDLE_PRIO,(INT8U*)"OS-Idle",&err);49#endif50#endif51}3.6.3空閑任務(wù)創(chuàng)建過程統(tǒng)計任務(wù)為μC/OS-II系統(tǒng)任務(wù),統(tǒng)計任務(wù)每0.1秒執(zhí)行一次,用于計算CPU的利用率。3.7統(tǒng)計任務(wù)3.7.1統(tǒng)計任務(wù)1#ifOS_TASK_STAT_EN>02voidOS_TaskStat(void*p_arg)3{4#ifOS_CRITICAL_METHOD==35OS_CPU_SRcpu_sr=0;6#endif789(void)p_arg;11while(OSStatRdy==OS_FALSE){12OSTimeDly(2*OS_TICKS_PER_SEC/10);13}14OSIdleCtrMax/=100L;15if(OSIdleCtrMax==0L){16OSCPUUsage=0;17(void)OSTaskSuspend(OS_PRIO_SELF);}19for(;;){20OS_ENTER_CRITICAL();21OSIdleCtrRun=OSIdleCtr;22OSIdleCtr=0L;23OS_EXIT_CRITICAL();24OSCPUUsage=(INT8U)(100L-OSIdleCtrRun/OSIdleCtrMax);25OSTaskStatHook();26#if(OS_TASK_STAT_STK_CHK_EN>0)&&(OS_TASK_CREATE_EXT_EN>0)27OS_TaskStatStkChk();28#endif29OSTimeDly(OS_TICKS_PER_SEC/10);30}31}32#endif1#ifOS_TASK_STAT_EN>02voidOSStatInit(void)3{4#ifOS_CRITICAL_METHOD==35OS_CPU_SRcpu_sr=0;#endif78910OSTimeDly(2);3.7.2OSStatInit函數(shù)11OS_ENTER_CRITICAL();12OSIdleCtr=0L;13OS_EXIT_CRITICAL();14OSTimeDly(OS_TICKS_PER_SEC/10);15OS_ENTER_CRITICAL();16OSIdleCtrMax=OSIdleCtr;17OSStatRdy=OS_TRUE;18OS_EXIT_CRITICAL();19}20#endif3.7.3統(tǒng)計任務(wù)創(chuàng)建函數(shù)OS_InitTaskStat1#ifOS_TASK_STAT_EN>02staticvoidOS_InitTaskStat(void)3{4#ifOS_TASK_NAME_SIZE>75INT8Uerr;6#endif789#ifOS_TASK_CREATE_EXT_EN>010#ifOS_STK_GROWTH==111(void)OSTaskCreateExt(OS_TaskStat,12(void*)0,13&OSTaskStatStk[OS_TASK_STAT_STK_SIZE-1],14OS_TASK_STAT_PRIO,15OS_TASK_STAT_ID,16&OSTaskStatStk[0],17OS_TASK_STAT_STK_SIZE,18(void*)0,OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);20#else21(void)OSTaskCreateExt(OS_TaskStat,22(void*)0,23&OSTaskStatStk[0],24OS_TASK_STAT_PRIO,25OS_TASK_STAT_ID,26&OSTaskStatStk[OS_TASK_STAT_STK_SIZE-1],27OS_TASK_STAT_STK_SIZE,28(void*)0,29OS_TASK_OPT_STK_CHK|OS_TASK_OPT_STK_CLR);30#endif31#else32#ifOS_STK_GROWTH==133(void)OSTaskCreate(OS_TaskStat,34(void*)0,35&OSTaskStatStk[OS_TASK_STAT_STK_SIZE-1],36OS_TASK_STAT_PRIO);37#else38(void)OSTaskCreate(OS_TaskStat,39(void*)0,40&OSTaskStatStk[0],41OS_TASK_STAT_PRIO);42#endif43#endif45#ifOS_TASK_NAME_SIZE>1446OSTaskNameSet(OS_TASK_STAT_PRIO,(INT8U*)"uC/OS-IIStat",&err);47#else48#ifOS_TASK_NAME_SIZE>749OSTaskNameSet(OS_TASK_STAT_PRIO,(INT8U*)"OS-Stat",&err);50#endif51#endif52}53#endif3.7.4統(tǒng)計任務(wù)計算CPU使用率過程這里的定時器不是系統(tǒng)硬件平臺的定時器計數(shù)器,而是μC/OS-II操作系統(tǒng)的軟件定時器,最小分辨率為一個時鐘節(jié)拍。在μC/OS-II內(nèi)部使用定時器任務(wù)來管理定時器,定時器任務(wù)的優(yōu)先級由用戶指定,μC/OS-II只能定義一個定時器任務(wù),但這個定時器任務(wù)可以管理多個定時器,管理的定時器數(shù)量僅受INT16U變量數(shù)值范圍的限定,可以認(rèn)為最多能創(chuàng)建65535個定時器。3.8什么是定時器任務(wù)定時器任務(wù)可視為μC/OS-II的系統(tǒng)服務(wù),嚴(yán)格意義上講,定時器任務(wù)對用戶不可見,μC/OS-II提供了面向定時器的接口函數(shù),用戶應(yīng)用程序中只需要調(diào)用這些函數(shù)就可以使用定時器,而完全不用去關(guān)心定時器任務(wù)。每個定時器的定時時間到達(dá)后,定時器將啟動一個回調(diào)函數(shù),回調(diào)函數(shù)代碼一般十分短?。ㄏ喈?dāng)于硬件定時器的中斷服務(wù)程序),往往在回調(diào)函數(shù)中完成釋放信號量等操作,以啟動一個請求該信號量的用戶任務(wù)工作。3.8.1定時器任務(wù)的工作原理3.8.2空閑定時器鏈表1#ifOS_TMR_EN>02voidOSTmr_Init(void)3{4#ifOS_EVENT_NAME_SIZE>105INT8Uerr;6#endif7INT16Ui;8OS_TMR*ptmr1;9OS_TMR*ptmr2;103.8.3實始化定時器管理器OSTmr_Init函數(shù)1112OS_MemClr((INT8U*)&OSTmrTbl[0],sizeof(OSTmrTbl));OS_MemClr((INT8U*)&OSTmrWheelTbl[0],sizeof(OSTmrWheelTbl));1415ptmr1=&OSTmrTbl[0];16ptmr2=&OSTmrTbl[1];17for(i=0;i<(OS_TMR_CFG_MAX-1);i++){18ptmr1->OSTmrType=OS_TMR_TYPE;19ptmr1->OSTmrState=OS_TMR_STATE_UNUSED;20ptmr1->OSTmrNext=(void*)ptmr2;21#ifOS_TMR_CFG_NAME_SIZE>122ptmr1->OSTmrName[0]='?';23ptmr1->OSTmrName[1]=OS_ASCII_NUL;24#endif25ptmr1++;26ptmr2++;27}28ptmr1->OSTmrType=OS_TMR_TYPE;29ptmr1->OSTmrState=OS_TMR_STATE_UNUSED;30ptmr1->OSTmrNext=(void*)0;31#ifOS_TMR_CFG_NAME_SIZE>132ptmr1->OSTmrName[0]='?';33ptmr1->OSTmrName[1]=OS_ASCII_NUL;34#endif35OSTmrTime=0;36OSTmrUsed=0;37OSTmrFree=OS_TMR_CFG_MAX;38OSTmrFreeList=&OSTmrTbl[0];39OSTmrSem=OSSemCreate(1);40OSTmrSemSignal=OSSemCreate(0);4142#ifOS_EVENT_NAME_SIZE>1843OSEventNameSet(OSTmrSem,(INT8U*)"uC/OS-IITmrLock",&err44#else45#ifOS_EVENT_NAME_SIZE>1046OSEventNameSet(OSTmrSem,(INT8U*)"OS-TmrLock",&err);47#endif#endif4950#ifOS_EVENT_NAME_SIZE>1851OSEventNameSet(OSTmrSemSignal,(INT8U*)"uC/OS-IITmrSignal",&err);52#else53#ifOS_EVENT_NAME_SIZE>1054OSEventNameSet(OSTmrSemSignal,(INT8U*)"OS-TmrSig",&err);55#endif56#endif5758OSTmr_InitTask();59}60#endif1#ifOS_TMR_EN>02typedefvoid(*OS_TMR_CALLBACK)(void*ptmr,void*parg);34typedefstructos_tmr{5INT8UOSTmrType;6OS_TMR_CALLBACKOSTmrCallback;7void*OSTmrCallbackArg;8void*OSTmrNext;9void*OSTmrPrev;10INT32UOSTmrMatch;3.8.4OS_TMR和OS_TMR_WHEEL結(jié)構(gòu)體類型定義11INT32UOSTmrDly;12INT32UOSTmrPeriod;13#ifOS_TMR_CFG_NAME_SIZE>014INT8UOSTmrName[OS_TMR_CFG_NAME_SIZE];15#endif16INT8UOSTmrOpt;17INT8UOSTmrState;18}OS_TMR;1920212223typedefstructos_tmr_wheel{24OS_TMR*OSTmrFirst;25INT16UOSTmrEntries;26}OS_TMR_WHEEL;27#endif1#ifOS_TMR_EN>02OS_EXTINT16UOSTmrFree;3OS_EXTINT16UOSTmrUsed;4OS_EXTINT32UOSTmrTime;56OS_EXTOS_EVENT*OSTmrSem;7OS_EXTOS_EVENT*OSTmrSemSignal;

溫馨提示

  • 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論