ucosII这个函数是如何理解?
本帖最后由 cjh8894 于 2014-9-19 16:02 编辑现在正在学习UCOSII请问os_err =之后是如何创建任务的, 一般是这样去创建新任务的OSTaskCreate( TCP_Test,
(void *)0,
&GstkTcp_Test,
TASK_TCP_Test_PRIO ); /*建立WEB_TCP的处理任务*/
OSInit(); /* Initialize "uC/OS-II, The Real-Time Kernel". */
os_err = OSTaskCreateExt((void (*)(void *)) App_TaskStart,/* Create the start task. */
(void * ) 0,
(OS_STK * )&App_TaskStartStk,
(INT8U ) APP_TASK_START_PRIO,
(INT16U ) APP_TASK_START_PRIO,
(OS_STK * )&App_TaskStartStk,
(INT32U ) APP_TASK_START_STK_SIZE,
(void * )0,
(INT16U )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));
#if OS_TASK_NAME_EN > 0
OSTaskNameSet(APP_TASK_START_PRIO, (CPU_INT08U *)"Start Task", &os_err);
#endif 翻看OSTaskCreateExt的源代码
#if OS_TASK_CREATE_EXT_EN > 0
INT8UOSTaskCreateExt (void (*task)(void *p_arg),
void *p_arg,
OS_STK*ptos,
INT8U prio,
INT16U id,
OS_STK*pbos,
INT32U stk_size,
void *pext,
INT16U opt)
{
OS_STK *psp;
INT8U err;
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SRcpu_sr = 0;
#endif
#if OS_ARG_CHK_EN > 0
if (prio > OS_LOWEST_PRIO) { /* Make sure priority is within allowable range */
return (OS_ERR_PRIO_INVALID);
}
#endif
OS_ENTER_CRITICAL();
if (OSIntNesting > 0) { /* Make sure we don't create the task from within an ISR*/
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_CREATE_ISR);
}
if (OSTCBPrioTbl == (OS_TCB *)0) { /* Make sure task doesn't already exist at this priority*/
OSTCBPrioTbl = OS_TCB_RESERVED;/* Reserve the priority to prevent others from doing ...*/
/* ... the same thing until task is created. */
OS_EXIT_CRITICAL();
#if (OS_TASK_STAT_STK_CHK_EN > 0)
OS_TaskStkClr(pbos, stk_size, opt); /* Clear the task stack (if needed) */
#endif
psp = OSTaskStkInit(task, p_arg, ptos, opt); /* Initialize the task's stack */
err = OS_TCBInit(prio, psp, pbos, id, stk_size, pext, opt);
if (err == OS_ERR_NONE) {
if (OSRunning == OS_TRUE) { /* Find HPT if multitasking has started */
OS_Sched();
}
} else {
OS_ENTER_CRITICAL();
OSTCBPrioTbl = (OS_TCB *)0; /* Make this priority avail. to others*/
OS_EXIT_CRITICAL();
}
return (err);
}
OS_EXIT_CRITICAL();
return (OS_ERR_PRIO_EXIST);
}
#endif
里边用到了OSTaskStkInit和OS_TCBInit
OS_STK *OSTaskStkInit (void (*task)(void *p_arg), void *p_arg, OS_STK *ptos, INT16U opt)
{
OS_STK *stk;
(void)opt; /* 'opt' is not used, prevent warning */
stk = ptos; /* Load stack pointer */
/* Registers stacked as if auto-saved on exception */
*(stk) = (INT32U)0x01000000L; /* xPSR */
*(--stk)= (INT32U)task; /* Entry Point */
*(--stk)= (INT32U)0xFFFFFFFEL; /* R14 (LR) (init value will cause fault if ever used)*/
*(--stk)= (INT32U)0x12121212L; /* R12 */
*(--stk)= (INT32U)0x03030303L; /* R3 */
*(--stk)= (INT32U)0x02020202L; /* R2 */
*(--stk)= (INT32U)0x01010101L; /* R1 */
*(--stk)= (INT32U)p_arg; /* R0 : argument */
/* Remaining registers saved on process stack */
*(--stk)= (INT32U)0x11111111L; /* R11 */
*(--stk)= (INT32U)0x10101010L; /* R10 */
*(--stk)= (INT32U)0x09090909L; /* R9 */
*(--stk)= (INT32U)0x08080808L; /* R8 */
*(--stk)= (INT32U)0x07070707L; /* R7 */
*(--stk)= (INT32U)0x06060606L; /* R6 */
*(--stk)= (INT32U)0x05050505L; /* R5 */
*(--stk)= (INT32U)0x04040404L; /* R4 */
return (stk);
}
INT8UOS_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_SRcpu_sr = 0;
#endif
OS_ENTER_CRITICAL();
ptcb = OSTCBFreeList; /* Get a free TCB from the free TCB list */
if (ptcb != (OS_TCB *)0) {
OSTCBFreeList = ptcb->OSTCBNext; /* Update pointer to free TCB list */
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
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 anevent */
#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 = '?'; /* Unknown name at task creation */
ptcb->OSTCBTaskName = OS_ASCII_NUL;
#endif
OSTCBInitHook(ptcb);
OSTaskCreateHook(ptcb); /* Call user defined hook */
OS_ENTER_CRITICAL();
OSTCBPrioTbl = ptcb;
ptcb->OSTCBNext = OSTCBList; /* Link into TCB chain */
ptcb->OSTCBPrev = (OS_TCB *)0;
if (OSTCBList != (OS_TCB *)0) {
OSTCBList->OSTCBPrev = ptcb;
}
OSTCBList = ptcb;
OSRdyGrp |= ptcb->OSTCBBitY; /* Make task ready to run */
OSRdyTbl |= ptcb->OSTCBBitX;
OSTaskCtr++; /* Increment the #tasks counter */
OS_EXIT_CRITICAL();
return (OS_ERR_NONE);
}
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NO_MORE_TCB);
} 我在看卢友亮的那本书,挺不错的
页:
[1]