当前位置: 代码迷 >> 综合 >> uC/OS II 函数说明 之–OSTaskCreate()与OSTaskCreateExt()
  详细解决方案

uC/OS II 函数说明 之–OSTaskCreate()与OSTaskCreateExt()

热度:34   发布时间:2023-12-09 03:14:06.0

1. OSTaskCreate()
    OSTaskCreate()建立一个新任务,可以在多任务环境启动之前,或者运行任务中建立任务。注意,ISR中禁止建立任务,一个任务必须为无限循环结构。   
    源代码如下:

#if OS_TASK_CREATE_EN > 0                    /* 条件编译,是否允许任务的创建               */
INT8U  OSTaskCreate (void (*task)(void *pd), /* 函数指针,void *pd为函数的参数             */
                     void *pdata,            /* 建立任务时,传递的参数                     */
                     OS_STK *ptos,           /* 指向堆栈任务栈顶的指针                     */
                     INT8U prio)             /* 任务优先级                                 */
{
#if OS_CRITICAL_METHOD == 3                  /* Allocate storage for CPU status register   */
    OS_CPU_SR  cpu_sr;
#endif
    OS_STK    *psp;
    INT8U      err;

#if OS_ARG_CHK_EN > 0
    if (prio > OS_LOWEST_PRIO) {             /* 参数检查,优先级是否处于允许优先级之内     */
        return (OS_PRIO_INVALID);
    }
#endif
    OS_ENTER_CRITICAL();                     /* 关中断                                     */
    if (OSTCBPrioTbl[prio] == (OS_TCB *)0) { /* 判断任务的优先级是否存在,如不存在,设置任 */
        OSTCBPrioTbl[prio] = (OS_TCB *)1;    /* 务优先级为1设置优先级后,就可以开中断了,  */
                                             /* 不用担心冲突,因为该优先级已经被占了       */
        OS_EXIT_CRITICAL();
        psp = (OS_STK *)OSTaskStkInit(task, pdata, ptos, 0);   
                                    /* 初始化堆栈,此函数与具体的硬件有关,OS_CPU_C.C      */
        err = OS_TCBInit(prio, psp, (OS_STK *)0, 0, 0, (void *)0, 0); /* 详见此函数的说明  */
        if (err == OS_NO_ERR) {
            OS_ENTER_CRITICAL();
            OSTaskCtr++;                     /* 任务计数器加1,统计运行的任务数            */
            OS_EXIT_CRITICAL();
            if (OSRunning == TRUE) {         /* 如果是在任务运行过程中新建任务,需要进行   */
                OS_Sched();                  /* 任务调度,保证 优先级最高的任务处于运行态  */
            }
        } else {
            OS_ENTER_CRITICAL();
            OSTCBPrioTbl[prio] = (OS_TCB *)0;/* 如果任务创建失败,优先级设置为0,放弃该    */
                                             /* 任务的优先级保证别的任务创建时可以使用     */
                                             /* 此优先级                                   */
            OS_EXIT_CRITICAL();
        }
        return (err);                        /* 创建任务失败,返回错误代码                 */
    }
    OS_EXIT_CRITICAL();
    return (OS_PRIO_EXIST);                  /* 返回OS_PRIO_EXIST,告知任务优先级已经存在  */
}
#endif

2. OSTaskCreateExt()
    OSTaskCreateExt()为OSTaskCreate()的扩展函数,允许更多的内容设置。
    源代码如下:

#if OS_TASK_CREATE_EXT_EN > 0
INT8U  OSTaskCreateExt (void   (*task)(void *pd), /* 同上                                    */
                        void    *pdata,    /* 同上                                           */
                        OS_STK  *ptos,     /* 同上                                           */
                        INT8U    prio,     /* 同上                                           */
                        INT16U   id,       /* 任务ID,2.52版本,无实际作用,保留作为扩展用   */
                        OS_STK  *pbos,     /* 指向堆栈底部的指针,用于OSTaskStkChk()函数     */
                        INT32U   stk_size, /* 指定任务堆栈的大小,由OS_STK类型决定           */
                        void    *pext,     /* 定义数据结构的指针,作为TCB的扩展              */
                        INT16U   opt)      /* 存放于任务操作相关的信息,详见uCOS-II.H        */
{
#if OS_CRITICAL_METHOD == 3                /* Allocate storage for CPU status register       */
    OS_CPU_SR  cpu_sr;
#endif
    OS_STK    *psp;
    INT8U      err;

#if OS_ARG_CHK_EN > 0
    if (prio > OS_LOWEST_PRIO) {           /* Make sure priority is within allowable range   */
        return (OS_PRIO_INVALID);
    }
#endif
    OS_ENTER_CRITICAL();
    if (OSTCBPrioTbl[prio] == (OS_TCB *)0) {
       /* Make sure task doesn’t already exist at this priority  */

       /* Reserve the priority to prevent others from doing …  */
       /* … the same thing until task is created.              */

        OSTCBPrioTbl[prio] = (OS_TCB *)1;                OS_EXIT_CRITICAL();

        if (((opt & OS_TASK_OPT_STK_CHK) != 0×0000) ||  
           /* See if stack checking has been enabled     */
            ((opt & OS_TASK_OPT_STK_CLR) != 0×0000)) {  
           /* See if stack needs to be cleared           */
            #if OS_STK_GROWTH == 1
            (void)memset(pbos, 0, stk_size * sizeof(OS_STK));
            #else
            (void)memset(ptos, 0, stk_size * sizeof(OS_STK));
            #endif
        }

        psp = (OS_STK *)OSTaskStkInit(task, pdata, ptos, opt);
                                          /* Initialize the task’s stack                   */
        err = OS_TCBInit(prio, psp, pbos, id, stk_size, pext, opt);
        if (err == OS_NO_ERR) {
            OS_ENTER_CRITICAL();
            OSTaskCtr++;                  /* Increment the #tasks counter                  */
            OS_EXIT_CRITICAL();
            if (OSRunning == TRUE) {      /* Find HPT if multitasking has started          */
                OS_Sched();
            }
        } else {
            OS_ENTER_CRITICAL();
            OSTCBPrioTbl[prio] = (OS_TCB *)0;      /* Make this priority avail. to others  */
            OS_EXIT_CRITICAL();
        }
        return (err);
    }
    OS_EXIT_CRITICAL();
    return (OS_PRIO_EXIST);
}
#endif