uC/OS-II主函数如下:
main(){
OSInit(); //初始化uC/OS_II
TaskCreate(...); //创建用户任务
OSStart(); //启动任务且之前不易打开中断
}
下面首先对uC/OS-II的初始化函数OSInit()进行分析:
OSInit(): 最先看看OSInit完成哪些初始化:
void OSInit (void)
{
#if OS_VERSION >= 204
OSInitHookBegin(); /* Call port specific initialization code */
#endif
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 */
#if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)
OS_FlagInit(); /* Initialize the event flag structures */
#endif
#if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)
OS_MemInit(); /* Initialize the memory manager */
#endif
#if (OS_Q_EN > 0) && (OS_MAX_QS > 0)
OS_QInit(); /* Initialize the message queue structures */
#endif
OS_InitTaskIdle(); /* Create the Idle Task 初始化空闲任务 */
#if OS_TASK_STAT_EN > 0
OS_InitTaskStat(); /* Create the Statistic Task初始化统计任务*/
#endif
#if OS_VERSION >= 204
OSInitHookEnd(); /* Call port specific init. code */
#endif
#if OS_VERSION >= 270 && OS_DEBUG_EN > 0
OSDebugInit();
#endif
}
static void OS_InitMisc (void)
{
#if OS_TIME_GET_SET_EN > 0
OSTime = 0L; /* 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;
OSIdleCtrMax = 0L;
OSStatRdy = FALSE; /* Statistic task is not ready */
#endif
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~void OSInit (void)
{
#if OS_VERSION >= 204
OSInitHookBegin(); /* 调用用户特定的初始化代码(通过一个接口函数实现用户要求的插件式进入系统中)*/
#endif
OS_InitMisc(); /* 初始化变量*/
OS_InitRdyList(); /* 初始化就绪列表*/
OS_InitTCBList(); /* 初始化OS_TCB空闲列表*/
OS_InitEventList(); /* 初始化OS_EVENT空闲列表*/
OS_InitTaskIdle(); /*创建空闲任务*/
#if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)
OS_FlagInit(); /* 初始化事件标志结构*/
#endif
#if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0) //允许内存管理
OS_MemInit(); /* 初始化内存管理器*/
#endif
#if (OS_Q_EN > 0) && (OS_MAX_QS > 0) //允许消息队列
OS_QInit(); /* 初始化消息队列结构*/
#endif
#if OS_TASK_STAT_EN > 0
OS_InitTaskStat(); /* 创建统计任务*/
#endif
#if OS_VERSION >= 204
OSInitHookEnd(); /*调用用户特定的初始化代码(参考OSInitHookBegin())*/
#endif
}
/*$PAGE*/
static void OS_InitMisc (void)
{
#if OS_TIME_GET_SET_EN > 0
OSTime = 0L; /* 32位的系统时钟清零*/
#endif
OSIntNesting = 0; /* 中断嵌套层数计数器清零*/
OSLockNesting = 0; /* 调度器锁的嵌套层数计数器清零*/
OSTaskCtr = 0; /* 任务数清零*/
OSRunning = OS_FALSE; /*指明多任务未开始*/
OSCtxSwCtr = 0; /* 任务切换次数计数器清零*/
OSIdleCtr = 0L; /*32位空闲计数器清零*/
#if OS_TASK_STAT_EN > 0 /*运行统计任务*/
OSIdleCtrRun = 0L; /* 空闲计数器过去S内达到的最大值 */
OSIdleCtrMax = 0L; /*保存空闲计数器S内达到的最大值,一般是在初始化的时候*/
OSStatRdy = OS_FALSE; /* 统计任务未就绪*/
#endif
}
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++) {//OS_RDY_TBL_SIZE=((OS_LOWEST_PRIO) / 8 + 1)
*prdytbl++ = 0x00;
}
OSPrioCur = 0; /*当前任务的优先级*/
OSPrioHighRdy = 0; /*最高优先级就绪任务的优先级*/
OSTCBHighRdy = (OS_TCB *)0; /*最高优先级就绪任务*/
OSTCBCur = (OS_TCB *)0; /*当前任务*/
}
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 = (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++;
}
ptcb1->OSTCBNext = (OS_TCB *)0; /* Last OS_TCB */
OSTCBFreeList = &OSTCBTbl[0];
}