OS_TCBInit 是被OSTaskCreate调用的两个函数之一
函数的参数为
prio优先级
ptos栈顶地址
pbos栈底地址
id
stk_size 栈大小
pext 用于ext的栈
opt选项
功能:当任务建立时初始化,TCB
INT8U OS_TCBInit (INT8U prio, OS_STK *ptos, OS_STK *pbos, INT16U id, INT32U stk_size, void *pext, INT16U opt)
{OS_TCB *ptcb;
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0;
#endif
OS_ENTER_CRITICAL();
ptcb = OSTCBFreeList; /* Get a free TCB from the free TCB list 从空闲的TCB连上去下一个,这个链实际是从OSTcbTbl[0] 以此练下去的, OSTCBFreeList原本指向OSTcbTbl[0] */
if (ptcb != (OS_TCB *)0) {
OSTCBFreeList = ptcb->OSTCBNext; /* Update pointer to free TCB list 如果是第一次创建任务的话 OSTCBFreeList会指向OSTcbTbl[1]*/
OS_EXIT_CRITICAL();
ptcb->OSTCBStkPtr = ptos; /* Load Stack pointer in TCB 栈顶地址赋值 */
ptcb->OSTCBPrio = prio; /* Load task priority into TCB 对优先级赋值*/
ptcb->OSTCBStat = OS_STAT_RDY; /* Task is ready to run 对任务状态赋值 */
ptcb->OSTCBStatPend = OS_STAT_PEND_OK; /* Clear pend status 对任务的挂起状态赋值 */
ptcb->OSTCBDly = 0; /* Task is not delayed 对人物的延时赋值 */
#if OS_TASK_CREATE_EXT_EN > 0 如果不考虑这个宏定义的话这段可以忽略
ptcb->OSTCBExtPtr = pext; /* Store pointer to TCB extension */
ptcb->OSTCBStkSize = stk_size; /* Store stack size */
ptcb->OSTCBStkBottom = pbos; /* Store pointer to bottom of stack */
ptcb->OSTCBOpt = opt; /* Store task options */
ptcb->OSTCBId = id; /* Store task ID */
#else
pext = pext; /* Prevent compiler warning if not used */
stk_size = stk_size;
pbos = pbos;
opt = opt;
id = id;
#endif
#if OS_TASK_DEL_EN > 0
ptcb->OSTCBDelReq = OS_ERR_NONE;
#endif
#if OS_LOWEST_PRIO <= 63 64优先级的
ptcb->OSTCBY = (INT8U)(prio >> 3); /* Pre-compute X, Y, BitX and BitY */
ptcb->OSTCBX = (INT8U)(prio & 0x07);
ptcb->OSTCBBitY = (INT8U)(1 << ptcb->OSTCBY);
ptcb->OSTCBBitX = (INT8U)(1 << ptcb->OSTCBX);
#else
ptcb->OSTCBY = (INT8U)((prio >> 4) & 0xFF); /* Pre-compute X, Y, BitX and BitY */
ptcb->OSTCBX = (INT8U) (prio & 0x0F);
ptcb->OSTCBBitY = (INT16U)(1 << ptcb->OSTCBY);
ptcb->OSTCBBitX = (INT16U)(1 << ptcb->OSTCBX);
#endif
#if (OS_EVENT_EN)
ptcb->OSTCBEventPtr = (OS_EVENT *)0; /* Task is not pending on an event */
#if (OS_EVENT_MULTI_EN > 0)
ptcb->OSTCBEventMultiPtr = (OS_EVENT **)0; /* Task is not pending on any events */
#endif
#endif
#if (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0) && (OS_TASK_DEL_EN > 0)
ptcb->OSTCBFlagNode = (OS_FLAG_NODE *)0; /* Task is not pending on an event flag */
#endif
#if (OS_MBOX_EN > 0) || ((OS_Q_EN > 0) && (OS_MAX_QS > 0))
ptcb->OSTCBMsg = (void *)0; /* No message received */
#endif
#if OS_TASK_PROFILE_EN > 0
ptcb->OSTCBCtxSwCtr = 0L; /* Initialize profiling variables */
ptcb->OSTCBCyclesStart = 0L;
ptcb->OSTCBCyclesTot = 0L;
ptcb->OSTCBStkBase = (OS_STK *)0;
ptcb->OSTCBStkUsed = 0L;
#endif
#if OS_TASK_NAME_SIZE > 1
ptcb->OSTCBTaskName[0] = '?'; /* Unknown name at task creation */
ptcb->OSTCBTaskName[1] = OS_ASCII_NUL;
#endif
OSTCBInitHook(ptcb);
OSTaskCreateHook(ptcb); /* Call user defined hook */
OS_ENTER_CRITICAL();
OSTCBPrioTbl[prio] = ptcb; /*这张表 OSTCBPrioTb对应下表表示优先级,表中的元素表示TCB*/
ptcb->OSTCBNext = OSTCBList; /* Link into TCB chain OSTCBList这个链表中的元素师已经创建的任务的TCB ,这句话的含义是要把新创建的TCB加到这个表头,并且形成双向的连接 */
ptcb->OSTCBPrev = (OS_TCB *)0;
if (OSTCBList != (OS_TCB *)0) {
OSTCBList->OSTCBPrev = ptcb;
}
OSTCBList = ptcb;
OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
OSTaskCtr++; /* Increment the #tasks counter OSTaskCtr 全局变量自加 */
OS_EXIT_CRITICAL();
return (OS_ERR_NONE);
}
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NO_MORE_TCB);
}