启动ucosii之OSInit()
第一步: OSInit();//初始化uCOS_II.該函數位于OS_CORE.C,主要工作:
函數原型位于OS_CORE.C
OS_InitMisc();??/* 基礎參數初始化 Initialize miscellaneous(混雜的,各種各樣的) variables */
OS_InitRdyList();?/* 初始化任務就緒表 Initialize the Ready List */
OS_InitTCBList();?/* 初始化任務控制塊 Initialize the free list of OS_TCBs */
OS_InitEventList();?/* 初始化事件控制塊 Initialize the free list of OS_EVENTs */
函數原型位于OS_FLAG.C
#if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)
??? OS_FlagInit();?/* 事件標志組初始化 Initialize the event flag structures */
#endif
函數原型位于OS_MEM.C
#if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)
??? OS_MemInit();?/* 內存初始化 Initialize the memory manager */
#endif
函數原型位于OS_Q.C
#if (OS_Q_EN > 0) && (OS_MAX_QS > 0)
??? OS_QInit();?/* 消息隊列初始化 Initialize the message queue structures */
#endif
函數原型位于OS_CORE.C
??? OS_InitTaskIdle();?/* 創建空閑任務(無條件)Create the Idle Task */
#if OS_TASK_STAT_EN > 0
??? OS_InitTaskStat();?/* 創建統計任務Create the Statistic Task */
#endif
1:static? void? OS_InitMisc (void)
{
#if OS_TIME_GET_SET_EN > 0?
??? //0L→代表這個整型常量為long型的??
??? OSTime??????? = 0L;?/* 32位系統時鐘變量,標示系統運行時間 Clear the 32-bit system clock*/
#endif
??? OSIntNesting? = 0;??/* 中斷嵌套次數 Clear the interrupt nesting counter */
??? OSLockNesting = 0;??/* 調度器嵌套上鎖次數 Clear the scheduling lock counter */
??? OSTaskCtr???? = 0;??/* 任務計時器,標示系統創建了多少個任務 Clear the number of tasks*/
??? OSRunning???? = FALSE;?/* 標示內核是否運行 Indicate that multitasking not started*/
????
??? OSCtxSwCtr??? = 0;?/* 系統任務切換次數 Clear the context switch counter */
??? OSIdleCtr???? = 0L;?/* 空閑時間計數器 Clear the 32-bit idle counter */
#if (OS_TASK_STAT_EN > 0) && (OS_TASK_CREATE_EXT_EN > 0)
??? OSIdleCtrRun? = 0L;//1秒前空閑任務計數器
??? OSIdleCtrMax? = 0L;//1秒內空閑任務計數器可達最大值
??? OSStatRdy???? = FALSE;?/* 統計任務標示,即是否執行統計任務 Statistic task is not ready */
#endif
}
2:static? void? OS_InitRdyList (void)
{
??? INT16U?? i;
??? INT8U?? *prdytbl;
??? OSRdyGrp????? = 0x00;?/* Clear the ready list */
??? prdytbl?????? = &OSRdyTbl[0];
??? for (i = 0; i < OS_RDY_TBL_SIZE; i++) {
??????? *prdytbl++ = 0x00;
??? }
??? OSPrioCur???? = 0;
??? OSPrioHighRdy = 0;
??? OSTCBHighRdy? = (OS_TCB *)0;?????????????????????????????????
??? OSTCBCur????? = (OS_TCB *)0;
}
//空閑任務鍵表OSTCBFreeList建立
3:static? void? OS_InitTCBList (void)
{
??? INT8U??? i;
??? OS_TCB? *ptcb1;
??? OS_TCB? *ptcb2;
??? OSTCBList???? = (OS_TCB *)0;???????????????????????????????? /* TCB Initialization?????????????????????? */
??? for (i = 0; i < (OS_LOWEST_PRIO + 1); i++) {???????????????? /* Clear the priority table???????????????? */
??????? OSTCBPrioTbl[i] = (OS_TCB *)0;
??? }
//建立空閑任務列表
??? ptcb1 = &OSTCBTbl[0];
??? ptcb2 = &OSTCBTbl[1];
??? for (i = 0; i < (OS_MAX_TASKS + OS_N_SYS_TASKS - 1); i++) {? /* Init. list of free TCBs????????????????? */
??????? ptcb1->OSTCBNext = ptcb2;
??????? ptcb1++;
??????? ptcb2++;
??? }
//列表最后一個任務的OSTCBNext設置為空
??? ptcb1->OSTCBNext = (OS_TCB *)0;????????????????????????????? /* Last OS_TCB????????????????????????????? */
//將一整塊空閑列表交給指針OSTCBFreeList
??? OSTCBFreeList??? = &OSTCBTbl[0];
}
4:static? void? OS_InitEventList (void)//初始化ECB
{
#if (OS_EVENT_EN > 0) && (OS_MAX_EVENTS > 0)
#if (OS_MAX_EVENTS > 1)
??? INT16U???? i;
??? OS_EVENT? *pevent1;
??? OS_EVENT? *pevent2;
??? pevent1 = &OSEventTbl[0];
??? pevent2 = &OSEventTbl[1];
??? for (i = 0; i < (OS_MAX_EVENTS - 1); i++) {?/* Init. list of free EVENT control blocks? */
??????? pevent1->OSEventType = OS_EVENT_TYPE_UNUSED;
??????? pevent1->OSEventPtr? = pevent2;
??????? pevent1++;
??????? pevent2++;
??? }
??? pevent1->OSEventType = OS_EVENT_TYPE_UNUSED;
??? pevent1->OSEventPtr? = (OS_EVENT *)0;
??? OSEventFreeList????? = &OSEventTbl[0];
#else
??? OSEventFreeList????????????? = &OSEventTbl[0];?/* Only have ONE event control block??????? */
??? OSEventFreeList->OSEventType = OS_EVENT_TYPE_UNUSED;
??? OSEventFreeList->OSEventPtr? = (OS_EVENT *)0;
#endif
#endif
}
5:void? OS_FlagInit (void)
{
#if OS_MAX_FLAGS == 1
??? OSFlagFreeList???????????????? = (OS_FLAG_GRP *)&OSFlagTbl[0];? /* Only ONE event flag group!????? */
??? OSFlagFreeList->OSFlagType???? = OS_EVENT_TYPE_UNUSED;
??? OSFlagFreeList->OSFlagWaitList = (void *)0;
#endif
#if OS_MAX_FLAGS >= 2
??? INT8U??????? i;
??? OS_FLAG_GRP *pgrp1;
??? OS_FLAG_GRP *pgrp2;
??? pgrp1 = &OSFlagTbl[0];
??? pgrp2 = &OSFlagTbl[1];
??? for (i = 0; i < (OS_MAX_FLAGS - 1); i++) {????????????????????? /* Init. list of free EVENT FLAGS? */
??????? pgrp1->OSFlagType???? = OS_EVENT_TYPE_UNUSED;
??????? pgrp1->OSFlagWaitList = (void *)pgrp2;
??????? pgrp1++;
??????? pgrp2++;
??? }
??? pgrp1->OSFlagWaitList = (void *)0;
??? OSFlagFreeList??????? = (OS_FLAG_GRP *)&OSFlagTbl[0];
#endif
}
6:void? OS_MemInit (void)
{
#if OS_MAX_MEM_PART == 1
??? OSMemFreeList??????????????? = (OS_MEM *)&OSMemTbl[0]; /* Point to beginning of free list????????? */
??? OSMemFreeList->OSMemFreeList = (void *)0;????????????? /* Initialize last node???????????????????? */
??? OSMemFreeList->OSMemAddr???? = (void *)0;????????????? /* Store start address of memory partition? */
??? OSMemFreeList->OSMemNFree??? = 0;????????????????????? /* No free blocks?????????????????????????? */
??? OSMemFreeList->OSMemNBlks??? = 0;????????????????????? /* No blocks??????????????????????????????? */
??? OSMemFreeList->OSMemBlkSize? = 0;????????????????????? /* Zero size??????????????????????????????? */
#endif
#if OS_MAX_MEM_PART >= 2
??? OS_MEM? *pmem;
??? INT16U?? i;
??? pmem = (OS_MEM *)&OSMemTbl[0];??????????????????? /* Point to memory control block (MCB)?????????? */
??? for (i = 0; i < (OS_MAX_MEM_PART - 1); i++) {???? /* Init. list of free memory partitions????????? */
??????? pmem->OSMemFreeList = (void *)&OSMemTbl[i+1]; /* Chain list of free partitions???????????????? */
??????? pmem->OSMemAddr???? = (void *)0;????????????? /* Store start address of memory partition?????? */
??????? pmem->OSMemNFree??? = 0;????????????????????? /* No free blocks??????????????????????????????? */
??????? pmem->OSMemNBlks??? = 0;????????????????????? /* No blocks???????????????????????????????????? */
??????? pmem->OSMemBlkSize? = 0;????????????????????? /* Zero size???????????????????????????????????? */
??????? pmem++;
??? }
??? pmem->OSMemFreeList = (void *)0;????????????????? /* Initialize last node????????????????????????? */
??? pmem->OSMemAddr???? = (void *)0;????????????????? /* Store start address of memory partition?????? */
??? pmem->OSMemNFree??? = 0;????????????????????????? /* No free blocks??????????????????????????????? */
??? pmem->OSMemNBlks??? = 0;????????????????????????? /* No blocks???????????????????????????????????? */
??? pmem->OSMemBlkSize? = 0;????????????????????????? /* Zero size???????????????????????????????????? */
??? OSMemFreeList?????? = (OS_MEM *)&OSMemTbl[0];???? /* Point to beginning of free list?????????????? */
#endif
}
7:void? OS_QInit (void)
{
#if OS_MAX_QS == 1
??? OSQFreeList???????? = &OSQTbl[0];??????????? /* Only ONE queue!??????????????????????????????????? */
??? OSQFreeList->OSQPtr = (OS_Q *)0;
#endif
#if OS_MAX_QS >= 2
??? INT16U? i;
??? OS_Q?? *pq1;
??? OS_Q?? *pq2;
??? pq1 = &OSQTbl[0];
??? pq2 = &OSQTbl[1];
??? for (i = 0; i < (OS_MAX_QS - 1); i++) {????? /* Init. list of free QUEUE control blocks??????????? */
??????? pq1->OSQPtr = pq2;
??????? pq1++;
??????? pq2++;
??? }
??? pq1->OSQPtr = (OS_Q *)0;
??? OSQFreeList = &OSQTbl[0];
#endif
}
/* 創建空閑任務(無條件)Create the Idle Task */
//變量在OS_CFG.H、uCOS_II.H中定義
8:static? void? OS_InitTaskIdle (void)
{
#if OS_TASK_CREATE_EXT_EN > 0
??? #if OS_STK_GROWTH == 1
??? (void)OSTaskCreateExt(OS_TaskIdle,
????????????????????????? (void *)0,???????????????????????????????? /* No arguments passed to OS_TaskIdle() */
????????????????????????? &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], /* Set Top-Of-Stack???????????????????? */
????????????????????????? OS_IDLE_PRIO,????????????????????????????? /* Lowest priority level??????????????? */
????????????????????????? OS_TASK_IDLE_ID,
????????????????????????? &OSTaskIdleStk[0],???????????????????????? /* Set Bottom-Of-Stack????????????????? */
????????????????????????? OS_TASK_IDLE_STK_SIZE,
????????????????????????? (void *)0,???????????????????????????????? /* No TCB extension???????????????????? */
????????????????????????? OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stack? */
??? #else
??? (void)OSTaskCreateExt(OS_TaskIdle,
????????????????????????? (void *)0,???????????????????????????????? /* No arguments passed to OS_TaskIdle() */
????????????????????????? &OSTaskIdleStk[0],???????????????????????? /* Set Top-Of-Stack???????????????????? */
????????????????????????? OS_IDLE_PRIO,????????????????????????????? /* Lowest priority level??????????????? */
????????????????????????? OS_TASK_IDLE_ID,
????????????????????????? &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], /* Set Bottom-Of-Stack????????????????? */
????????????????????????? OS_TASK_IDLE_STK_SIZE,
????????????????????????? (void *)0,???????????????????????????????? /* No TCB extension???????????????????? */
????????????????????????? OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stack? */
??? #endif
#else
??? #if OS_STK_GROWTH == 1
??? (void)OSTaskCreate(OS_TaskIdle,
?????????????????????? (void *)0,
?????????????????????? &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1],
?????????????????????? OS_IDLE_PRIO);
??? #else
??? (void)OSTaskCreate(OS_TaskIdle,
?????????????????????? (void *)0,
?????????????????????? &OSTaskIdleStk[0],
?????????????????????? OS_IDLE_PRIO);
??? #endif
#endif
}
/* 創建統計任務Create the Statistic Task */
9:static? void? OS_InitTaskStat (void)
{
#if OS_TASK_CREATE_EXT_EN > 0
??? #if OS_STK_GROWTH == 1
??? (void)OSTaskCreateExt(OS_TaskStat,
????????????????????????? (void *)0,?????????????????????????????????? /* No args passed to OS_TaskStat()*/
????????????????????????? &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1],?? /* Set Top-Of-Stack?????????????? */
????????????????????????? OS_STAT_PRIO,??????????????????????????????? /* One higher than the idle task? */
????????????????????????? OS_TASK_STAT_ID,
????????????????????????? &OSTaskStatStk[0],?????????????????????????? /* Set Bottom-Of-Stack??????????? */
????????????????????????? OS_TASK_STAT_STK_SIZE,
????????????????????????? (void *)0,?????????????????????????????????? /* No TCB extension?????????????? */
????????????????????????? OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);? /* Enable stack checking + clear? */
??? #else
??? (void)OSTaskCreateExt(OS_TaskStat,
????????????????????????? (void *)0,?????????????????????????????????? /* No args passed to OS_TaskStat()*/
????????????????????????? &OSTaskStatStk[0],?????????????????????????? /* Set Top-Of-Stack?????????????? */
????????????????????????? OS_STAT_PRIO,??????????????????????????????? /* One higher than the idle task? */
????????????????????????? OS_TASK_STAT_ID,
????????????????????????? &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1],?? /* Set Bottom-Of-Stack??????????? */
????????????????????????? OS_TASK_STAT_STK_SIZE,
????????????????????????? (void *)0,?????????????????????????????????? /* No TCB extension?????????????? */
????????????????????????? OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);? /* Enable stack checking + clear? */
??? #endif
#else
??? #if OS_STK_GROWTH == 1
??? (void)OSTaskCreate(OS_TaskStat,
?????????????????????? (void *)0,????????????????????????????????????? /* No args passed to OS_TaskStat()*/
?????????????????????? &OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1],????? /* Set Top-Of-Stack?????????????? */
?????????????????????? OS_STAT_PRIO);????????????????????????????????? /* One higher than the idle task? */
??? #else
??? (void)OSTaskCreate(OS_TaskStat,
?????????????????????? (void *)0,????????????????????????????????????? /* No args passed to OS_TaskStat()*/
?????????????????????? &OSTaskStatStk[0],????????????????????????????? /* Set Top-Of-Stack?????????????? */
?????????????????????? OS_STAT_PRIO);????????????????????????????????? /* One higher than the idle task? */
??? #endif
#endif
}
總結
以上是生活随笔為你收集整理的启动ucosii之OSInit()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: uCos的内存管理
- 下一篇: 启动之OS_CPU_C