码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-05-13 12:01:30      阅读:81      评论:0      收藏: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

 

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

标签:

原文地址:http://www.cnblogs.com/lcchuguo/p/4499682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!