?? os_task.c
字號:
/*
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
* TASK MANAGEMENT
*
* (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
* All Rights Reserved
*
* File : OS_TASK.C
* By : Jean J. Labrosse
* 翻譯: likee
*********************************************************************************************************
*/
#ifndef OS_MASTER_FILE
#include "includes.h"
#endif
/*
*********************************************************************************************************
* CHANGE PRIORITY OF A TASK
*
* Description: This function allows you to change the priority of a task dynamically. Note that the new
* priority MUST be available.
*
* Arguments : oldp is the old priority
*
* newp is the new priority
*
* Returns : OS_NO_ERR is the call was successful
* OS_PRIO_INVALID if the priority you specify is higher that the maximum allowed
* (i.e. >= OS_LOWEST_PRIO)
* OS_PRIO_EXIST if the new priority already exist.
* OS_PRIO_ERR there is no task with the specified OLD priority (i.e. the OLD task does
* not exist.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
改變?nèi)蝿?wù)優(yōu)先級
描述:此函數(shù)允許你動態(tài)改變一個任務(wù)的優(yōu)先級。但新的優(yōu)先級必須可行
參數(shù):oldp :舊的優(yōu)先級
newp:新的優(yōu)先級
返回:OS_NO_ERR:改變成功
OS_PRIO_INVALID:指定的優(yōu)先級不合法:超過最大值
OS_PRIO_EXIST:新優(yōu)先級已經(jīng)存在
OS_PRIO_ERR:舊優(yōu)先級任務(wù)不存在
*********************************************************************************************************
*/
#if OS_TASK_CHANGE_PRIO_EN > 0 //如果OS_TASK_CHANGE_PRIO_EN設(shè)置為1,能使包含下面代碼
INT8U OSTaskChangePrio (INT8U oldprio, INT8U newprio)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
//為CPU狀態(tài)寄存器分配存儲器
OS_CPU_SR cpu_sr; //OS_CPU_SR即為unsigned int
#endif
#if OS_EVENT_EN > 0
//OS_EVENT_EN 定義為:(((OS_Q_EN > 0) && (OS_MAX_QS > 0)) || (OS_MBOX_EN > 0) || (OS_SEM_EN > 0) || (OS_MUTEX_EN > 0))
//OS_EVENT_EN 定義為:能使隊列代碼產(chǎn)生&&申請隊列控制塊最大數(shù)不為零||能使郵箱代碼產(chǎn)生||
//能使信號量代碼產(chǎn)生||能使互斥量代碼產(chǎn)生
OS_EVENT *pevent;
#endif
OS_TCB *ptcb;
INT8U x;
INT8U y;
INT8U bitx;
INT8U bity;
#if OS_ARG_CHK_EN > 0//允許參數(shù)檢測
if ((oldprio >= OS_LOWEST_PRIO && oldprio != OS_PRIO_SELF) ||
newprio >= OS_LOWEST_PRIO) {//舊新優(yōu)先級都不合法
return (OS_PRIO_INVALID);
}
#endif
OS_ENTER_CRITICAL();//如果合法
if (OSTCBPrioTbl[newprio] != (OS_TCB *)0) { /* New priority must not already exist */
OS_EXIT_CRITICAL();
return (OS_PRIO_EXIST);//新優(yōu)先級必須不存在,存在就重復(fù)了
} else {
OSTCBPrioTbl[newprio] = (OS_TCB *)1; /* Reserve the entry to prevent others */
//保留入口,防止其它任務(wù)占用此優(yōu)先級
OS_EXIT_CRITICAL();
y = newprio >> 3; /* Precompute to reduce INT. latency */
//此函數(shù)會預(yù)先計算新優(yōu)先級任務(wù)的任務(wù)控制塊中的某些值,使用這些值
//可以將任務(wù)放入就緒步或者從該表中移除任務(wù).
bity = OSMapTbl[y];
x = newprio & 0x07;
bitx = OSMapTbl[x];
OS_ENTER_CRITICAL();
if (oldprio == OS_PRIO_SELF) { /* See if changing self */
//如果改變自己
oldprio = OSTCBCur->OSTCBPrio; /* Yes, get priority */
}//是的,得到優(yōu)先級
ptcb = OSTCBPrioTbl[oldprio];//得到該優(yōu)先級TCB指針
if (ptcb != (OS_TCB *)0) { /* Task to change must exist */
//優(yōu)先級存在,如果要改變的是當(dāng)前任務(wù),由一定會成功
OSTCBPrioTbl[oldprio] = (OS_TCB *)0; /* Remove TCB from old priority */
//通過放入空閑指針,將指向當(dāng)前任務(wù)的TCB指針從優(yōu)先級列表中刪除,
//使當(dāng)前舊的優(yōu)先級空閑,可以被其它任務(wù)占用.
if ((OSRdyTbl[ptcb->OSTCBY] & ptcb->OSTCBBitX) != 0x00) { /* If task is ready make it not */
if ((OSRdyTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0x00) { //如果要改變優(yōu)先級的任務(wù)就緒
OSRdyGrp &= ~ptcb->OSTCBBitY;//不能讓它就緒,
}
OSRdyGrp |= bity; /* Make new priority ready to run */
OSRdyTbl[y] |= bitx;//從就緒表中移除,然后在新優(yōu)先級下,將任務(wù)插入就緒表,
//注意:OSTaskChangePrio是利用預(yù)先計算的值(見前面)將任務(wù)插入就緒表中的.
#if OS_EVENT_EN > 0
//#define OS_EVENT_EN (((OS_Q_EN > 0) && (OS_MAX_QS > 0)) || (OS_MBOX_EN > 0) || (OS_SEM_EN > 0) || (OS_MUTEX_EN > 0))
//能使隊列代碼產(chǎn)生&&申請隊列控制塊最大數(shù)不為零||能使郵箱代碼產(chǎn)生||
//能使信號量代碼產(chǎn)生||能使互斥量代碼產(chǎn)生
} else {
pevent = ptcb->OSTCBEventPtr;
if (pevent != (OS_EVENT *)0) { /* Remove from event wait list */
//如果任務(wù)沒有就緒,那么可能在等一個信號量,一個互斥型信號量,一個郵箱,隊列
//等,如果OSTCBEventPtr非空,那么此函數(shù)會知道任務(wù)正在等以上的某件事.
if ((pevent->OSEventTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0) {
pevent->OSEventGrp &= ~ptcb->OSTCBBitY;
}
pevent->OSEventGrp |= bity; /* Add new priority to wait list */
//如果任務(wù)正在等某事件發(fā)生,OSTCBEventPtr必須將任務(wù)從事件控制塊的等待隊列(舊
//的優(yōu)先級下)中移除,并在新的優(yōu)先級下將事件插入到等待隊列中.任務(wù)也可能正
//在等待延時時間到,或被掛起,上面幾行可以省略
pevent->OSEventTbl[y] |= bitx;
}
#endif
}
OSTCBPrioTbl[newprio] = ptcb; /* Place pointer to TCB @ new priority */
//將指向任務(wù)的OS-TCB的指針存到OSTCBPrioTbl[]中.
ptcb->OSTCBPrio = newprio; /* Set new task priority */
//新的優(yōu)先級保存在OSTCB中,預(yù)先值也保存在OSTCB中.
ptcb->OSTCBY = y;
ptcb->OSTCBX = x;
ptcb->OSTCBBitY = bity;
ptcb->OSTCBBitX = bitx;
OS_EXIT_CRITICAL();
OS_Sched(); /* Run highest priority task ready */
//任務(wù)調(diào)度,運行最高優(yōu)先級任務(wù),在新的優(yōu)先級高于舊的優(yōu)先有或者新的優(yōu)先級高于調(diào)
//用此函數(shù)任務(wù)優(yōu)先級的時候,此函數(shù)會被調(diào)用
return (OS_NO_ERR);
} else {
OSTCBPrioTbl[newprio] = (OS_TCB *)0; /* Release the reserved prio. */
//如果任務(wù)不存在,釋放新優(yōu)先級的TCB
OS_EXIT_CRITICAL();
return (OS_PRIO_ERR); /* Task to change didn't exist */
}//返回
}
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* CREATE A TASK
*
* Description: This function is used to have uC/OS-II manage the execution of a task. Tasks can either
* be created prior to the start of multitasking or by a running task. A task cannot be
* created by an ISR.
*
* Arguments : task is a pointer to the task's code
*
* pdata is a pointer to an optional data area which can be used to pass parameters to
* the task when the task first executes. Where the task is concerned it thinks
* it was invoked and passed the argument 'pdata' as follows:
*
* void Task (void *pdata)
* {
* for (;;) {
* Task code;
* }
* }
*
* ptos is a pointer to the task's top of stack. If the configuration constant
* OS_STK_GROWTH is set to 1, the stack is assumed to grow downward (i.e. from high
* memory to low memory). 'pstk' will thus point to the highest (valid) memory
* location of the stack. If OS_STK_GROWTH is set to 0, 'pstk' will point to the
* lowest memory location of the stack and the stack will grow with increasing
* memory locations.
*
* prio is the task's priority. A unique priority MUST be assigned to each task and the
* lower the number, the higher the priority.
*
* Returns : OS_NO_ERR if the function was successful.
* OS_PRIO_EXIT if the task priority already exist
* (each task MUST have a unique priority).
* OS_PRIO_INVALID if the priority you specify is higher that the maximum allowed
* (i.e. >= OS_LOWEST_PRIO)
*********************************************************************************************************
*/
/*
*********************************************************************************************
建立一個任務(wù)
描述:這個函數(shù)用于ucosII處理完成一個任務(wù),它要么在多任務(wù)處理之前建立,
要么運行任務(wù)建立,它不夠由中斷服務(wù)程序建立。
參數(shù):task: 指向任務(wù)代碼的指針。
pdata:是一個指向非強制性數(shù)據(jù)區(qū)域的指針,當(dāng)任務(wù)優(yōu)先運行時傳遞
參數(shù)給任務(wù)。任務(wù)有關(guān)部分假想它被喚醒,然后按照以下方式傳遞pdata:
* void Task (void *pdata)
* {
* for (;;) {
* Task code;
* }
* }
ptos:指向任務(wù)堆棧頂部的指針,如果配置常數(shù) OS_STK_GROWTH 設(shè)置為1的話,堆棧則會由高到低增長(由高地址向低地址存儲)。所以
“pstk”會指向堆棧存儲器位置的最高地址;如果 OS_STK_GROWTH 設(shè)置為0的話,“pstk”將指向堆棧
最低存儲器位置,堆棧將按存儲器位置遞增。
prio:是任務(wù)的優(yōu)先級,一個獨特的優(yōu)先級必須指定給每個任務(wù),最小的數(shù)對應(yīng)最高優(yōu)先級。
返回:OS_NO_ERR :如果函數(shù)成功。
OS_PRIO_EXIT :如果優(yōu)先級已經(jīng)存在。
OS_PRIO_INVALID:如果定義優(yōu)先級的數(shù)大于最大值
*********************************************************************************************
*/
#if OS_TASK_CREATE_EN > 0 //能使包含任務(wù)創(chuàng)建代碼
INT8U OSTaskCreate (void (*task)(void *pd), void *pdata, OS_STK *ptos, INT8U prio)//上面有說明
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
// 為CPU狀態(tài)寄存器分配存儲空間
OS_CPU_SR cpu_sr; //CPU狀態(tài)字是十六位 OS_CPU_SR為unsigned int
#endif
OS_STK *psp;
INT8U err;
#if OS_ARG_CHK_EN > 0
//如果OS_ARG_CHK_EN 設(shè)為1,OSTaskCreate會檢查分配給任務(wù)的優(yōu)先級是否有效。
//系統(tǒng)在執(zhí)行初始化的時候,已經(jīng)把最低優(yōu)先級分配給了空閑任務(wù)。
//所以不能用最低優(yōu)先級來創(chuàng)建任務(wù)。
if (prio > OS_LOWEST_PRIO) { /* Make sure priority is within allowable range */
//保證優(yōu)先級在允許范圍內(nèi)
return (OS_PRIO_INVALID);
}
#endif
OS_ENTER_CRITICAL(); //進入臨界狀態(tài)
if (OSTCBPrioTbl[prio] == (OS_TCB *)0) { /* Make sure task doesn't already exist at this priority */
//保證優(yōu)先級沒有被其它任務(wù)占用
OSTCBPrioTbl[prio] = (OS_TCB *)1; /* Reserve the priority to prevent others from doing ... */
/* ... the same thing until task is created. */
//放置一個非空指針,表示已經(jīng)占用
OS_EXIT_CRITICAL(); //退出臨界狀態(tài)
psp = (OS_STK *)OSTaskStkInit(task, pdata, ptos, 0); /* Initialize the task's stack */
//初始化任務(wù)堆棧,即建立任務(wù)堆棧
err = OS_TCBInit(prio, psp, (OS_STK *)0, 0, 0, (void *)0, 0);
//初始化任務(wù)控制塊,從空閑的OS_TCB緩沖池
//中獲得并初始化一個任務(wù)控制塊
if (err == OS_NO_ERR) { //如果初始化沒有錯
OS_ENTER_CRITICAL();//進入臨界狀態(tài)
OSTaskCtr++; /* Increment the #tasks counter */
//任務(wù)數(shù)量加一
OS_EXIT_CRITICAL();//退出臨界狀態(tài)
if (OSRunning == TRUE) { /* Find highest priority task if multitasking has started */
//如果多任務(wù)開始,尋找最高優(yōu)先級任務(wù)
OS_Sched();
}
} else { //如果初始化任務(wù)控制塊有錯
OS_ENTER_CRITICAL();//進入臨界狀態(tài)
OSTCBPrioTbl[prio] = (OS_TCB *)0;/* Make this priority available to others */
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -