ucos之一初始化 OSInit 源码分析 1

ucos 系统应用过程详解之一 系统初始化

int main()
{
    SysTick_Config(tick);  //时钟配置,为系统提供时间片
    OSInit();  //初始化
    OSTaskCreate(Task,(void *)0,&stk[TASK_SIZE-1], Task_Prio); //任务创建
    OSStart();  //任务启动
}


OSInit() 详解

void  OSInit (void)
{
    OSInitHookBegin();    /* 钩子函数  */
    OS_InitMisc();       /*清零操作       */
    OS_InitRdyList();   /* 任务清零              */
    OS_InitTCBList();    /*初始化TCB        */
    OS_InitEventList();    /* 事件列表     */
    #if (OS_FLAG_EN > 0u) && (OS_MAX_FLAGS > 0u)
        OS_FlagInit();               //事件标志  
    #endif
    #if (OS_MEM_EN > 0u) && (OS_MAX_MEM_PART > 0u)
        OS_MemInit();         //内存       
    #endif
    #if (OS_Q_EN > 0u) && (OS_MAX_QS > 0u)
        OS_QInit();                    //消息队列    
    #endif
    OS_InitTaskIdle();         /*  空任务  */
    #if OS_TASK_STAT_EN > 0u
        OS_InitTaskStat();          /* 建立统计任务    */
    #endif
    #if OS_TMR_EN > 0u
        OSTmr_Init();                  /* 系统定时器       */
    #endif
    OSInitHookEnd();             /* 钩子函数添加用户代码       */
    #if OS_DEBUG_EN > 0u
        OSDebugInit();
    #endif
}

OS_InitMisc(),杂项清零

static  void  OS_InitMisc (void)  
    {
    #if OS_TIME_GET_SET_EN > 0u
        OSTime                    = 0uL;     /* 系统时间清零  */
    #endif   
        OSIntNesting              = 0u;      /* 中断嵌套层数清零    */
        OSLockNesting             = 0u;       /* 任务锁定层数清零     */
        OSTaskCtr                 = 0u;       /* 任务计数器清零              */
        OSRunning                 = OS_FALSE; /* 任务运行标志置零 */
        OSCtxSwCtr                = 0u;       /* 任务切换次数清零       */
        OSIdleCtr                 = 0uL;      /* 空闲任务计数器清零          */
    #if OS_TASK_STAT_EN > 0u
        OSIdleCtrRun              = 0uL;
        OSIdleCtrMax              = 0uL;
        OSStatRdy                 = OS_FALSE; /* Statistic task is not ready      */
    #endif
    #ifdef OS_SAFETY_CRITICAL_IEC61508
    //如果上面为1的话就不允许新建任务
        OSSafetyCriticalStartFlag = OS_FALSE; /* Still allow creation of objects          */
    #endif
    }

OS_InitRdyList() 任务就绪列表初始化

 static  void  OS_InitRdyList (void) 
    {
        INT8U  i;
        OSRdyGrp      = 0u;       /* Clear the ready list   任务就绪组清零   */
        for (i = 0u; i < OS_RDY_TBL_SIZE; i++) {  //任务就绪表清零
            OSRdyTbl[i] = 0u;
        }
        OSPrioCur     = 0u;   //当前任务优先级清零
        OSPrioHighRdy = 0u;   //最高优先级清零
        OSTCBHighRdy  = (OS_TCB *)0;
        OSTCBCur      = (OS_TCB *)0;
    }

OS_InitTCBList() TCB列表初始化

static  void  OS_InitTCBList (void)  
    {
        INT8U    ix;
        INT8U    ix_next;
        OS_TCB  *ptcb1;
        OS_TCB  *ptcb2;
        OS_MemClr((INT8U *)&OSTCBTbl[0],     sizeof(OSTCBTbl));      /* 清空所有的TCB                */
        OS_MemClr((INT8U *)&OSTCBPrioTbl[0], sizeof(OSTCBPrioTbl));  /* 清空所有任务表    */
        for (ix = 0u; ix < (OS_MAX_TASKS + OS_N_SYS_TASKS - 1u); ix++) {    /*初始化TCB   */
            ix_next =  ix + 1u;
            ptcb1   = &OSTCBTbl[ix];    //取第一个TCB
            ptcb2   = &OSTCBTbl[ix_next];  //取下一个TCB
            ptcb1->OSTCBNext = ptcb2;   //连接起来
    #if OS_TASK_NAME_EN > 0u
            ptcb1->OSTCBTaskName = (INT8U *)(void *)"?";         
    #endif
        }
        ptcb1                   = &OSTCBTbl[ix];   //系统任务+用户定义化任务的末尾 
        ptcb1->OSTCBNext        = (OS_TCB *)0;      /* Last OS_TCB  下一个TCB为NULL   */
    #if OS_TASK_NAME_EN > 0u
        ptcb1->OSTCBTaskName    = (INT8U *)(void *)"?";              /* Unknown name                       */
    #endif
        OSTCBList               = (OS_TCB *)0;      /* 也就是还没有TCB,因为还没有Task产生*/
        OSTCBFreeList           = &OSTCBTbl[0]; //所有的TCB都处于free状态
    }


OS_InitTaskIdle 空任务初始化

static  void  OS_InitTaskIdle (void)  
    {
    #if OS_TASK_NAME_EN > 0u
        INT8U  err;
    #endif
    #if OS_TASK_CREATE_EXT_EN > 0u
        #if OS_STK_GROWTH == 1u
        (void)OSTaskCreateExt(OS_TaskIdle,
                              (void *)0,          /* 没有参数传递给 OS_TaskIdle() */
                              &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1u],/*栈顶      */
                              OS_TASK_IDLE_PRIO,                         /* 最低优先级       */
                              OS_TASK_IDLE_ID,
                              &OSTaskIdleStk[0],                         /*   栈底             */
                              OS_TASK_IDLE_STK_SIZE,     //栈大小
                              (void *)0,                                 /* 没有TCB扩展            */
                              OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking +                                     clear stack  */
        #else
        (void)OSTaskCreateExt(OS_TaskIdle,
                              (void *)0,                                 /* No arguments passed to OS_TaskIdle() */
                              &OSTaskIdleStk[0],          /* 栈从低到高增长   */
                              OS_TASK_IDLE_PRIO,                         /* Lowest priority level                */
                              OS_TASK_IDLE_ID,
                              &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1u],/* Bottom-Of-Stack    */
                              OS_TASK_IDLE_STK_SIZE,
                              (void *)0,                                 /* No TCB extension                     */
                              OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking +                                             clear stack  */
        #endif
    #else
        #if OS_STK_GROWTH == 1u
        (void)OSTaskCreate(OS_TaskIdle,
                           (void *)0,
                           &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1u],
                           OS_TASK_IDLE_PRIO);
        #else
        (void)OSTaskCreate(OS_TaskIdle,
                           (void *)0,
                           &OSTaskIdleStk[0],
                           OS_TASK_IDLE_PRIO);
        #endif
    #endif
    #if OS_TASK_NAME_EN > 0u
        OSTaskNameSet(OS_TASK_IDLE_PRIO, (INT8U *)(void *)"uC/OS-II Idle", &err); //TASK 名字
    #endif
    }

OS_TaskIdle() 空任务

void  OS_TaskIdle (void *p_arg) 
    {
    #if OS_CRITICAL_METHOD == 3u                     /* cpu中断模式3           */
        OS_CPU_SR  cpu_sr = 0u;    //OS_ENTER_CRITICAL()中应用
    #endif
        p_arg = p_arg;                               /* Prevent compiler warning for not using 'p_arg'     */
        for (;;) {
            OS_ENTER_CRITICAL();
            OSIdleCtr++;       //空任务运行次数
            OS_EXIT_CRITICAL();
            OSTaskIdleHook();                        /* Call user definable HOOK                           */
        }
    }


系统初始化主要做的事情有,清空所有标志,包含TCB、任务、时间、优先级、内存等;

然后创建TCB链表,事件链表,创建空任务,用户还可以选择创建系统定时器和状态任务等

你可能感兴趣的:(ucos-II,OS_InitMisc(),OSInit(),OS_TaskIdle(),OS_InitTaskIdle)