第一步: 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
}