创建一个任务时,应该传入参数:入口函数,函数所需要的参数指针,函数对应的栈指针,优先级
INT8U OSTaskCreate (void (*task)(void *pd), void *ppdata, OS_STK *ptos, INT8U prio) reentrant
{
void *psp;
INT8U err;
if (prio > OS_LOWEST_PRIO) { /* Make sure priority is within allowable range */
return (OS_PRIO_INVALID);
}
OS_ENTER_CRITICAL();
if (OSTCBPrioTbl[prio] == (OS_TCB *)0) { /* Make sure task doesn't already exist at this priority */
OSTCBPrioTbl[prio] = (OS_TCB *)1; /* Reserve the priority to prevent others from doing ... */
/* ... the same thing until task is created. */
OS_EXIT_CRITICAL();
//初始化任务的栈,也就是初始化函数的栈
psp = (void *)OSTaskStkInit(task, ppdata, ptos, 0); /* Initialize the task's stack */
//初始化任务对应的TCB,也就是写任务的TCB结构体
err = OSTCBInit(prio, psp, (void *)0, 0, 0, (void *)0, 0);
if (err == OS_NO_ERR) {
OS_ENTER_CRITICAL();
OSTaskCtr++; /* Increment the #tasks counter */
OSTaskCreateHook(OSTCBPrioTbl[prio]); /* Call user defined hook */
OS_EXIT_CRITICAL();
//OSRunning:如果为真,表示系统已经初始化完毕,一旦创建任务,立马调用OSsched函数
//OSRunning:如果为假,表示系统正在初始化,创建任务后不调用OSSched函数
if (OSRunning) { /* Find highest priority task if multitasking has started */
OSSched();
}
} else {
OS_ENTER_CRITICAL();
OSTCBPrioTbl[prio] = (OS_TCB *)0;/* Make this priority available to others */
OS_EXIT_CRITICAL();
}
return (err);
} else {
OS_EXIT_CRITICAL();
return (OS_PRIO_EXIST);
}
}