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、任務、時間、優先級、內存等;spa

而後建立TCB鏈表,事件鏈表,建立空任務,用戶還能夠選擇建立系統定時器和狀態任務等code

相關文章
相關標籤/搜索