?? os_task.c
字號:
//把這一優先級給其它任務
OS_EXIT_CRITICAL();//退出臨界狀態
}
return (err);//返回錯誤信息
}
OS_EXIT_CRITICAL();//如果優先級占用,退出臨界狀態
return (OS_PRIO_EXIST);//返回優先級存在
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* CREATE A TASK (Extended Version)
*
* 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. This function is similar to OSTaskCreate() except that it allows
* additional information about a task to be specified.
*
* 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. 'pstk' MUST point to a valid 'free' data item.
*
* prio is the task's priority. A unique priority MUST be assigned to each task and the
* lower the number, the higher the priority.
*
* id is the task's ID (0..65535)
*
* pbos is a pointer to the task's bottom 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). 'pbos' will thus point to the LOWEST (valid) memory
* location of the stack. If OS_STK_GROWTH is set to 0, 'pbos' will point to the
* HIGHEST memory location of the stack and the stack will grow with increasing
* memory locations. 'pbos' MUST point to a valid 'free' data item.
*
* stk_size is the size of the stack in number of elements. If OS_STK is set to INT8U,
* 'stk_size' corresponds to the number of bytes available. If OS_STK is set to
* INT16U, 'stk_size' contains the number of 16-bit entries available. Finally, if
* OS_STK is set to INT32U, 'stk_size' contains the number of 32-bit entries
* available on the stack.
*
* pext is a pointer to a user supplied memory location which is used as a TCB extension.
* For example, this user memory can hold the contents of floating-point registers
* during a context switch, the time each task takes to execute, the number of times
* the task has been switched-in, etc.
*
* opt contains additional information (or options) about the behavior of the task. The
* LOWER 8-bits are reserved by uC/OS-II while the upper 8 bits can be application
* specific. See OS_TASK_OPT_??? in uCOS-II.H.
*
* 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)
*********************************************************************************************************
*/
/*
*****************************************************************************
建立一個任務的擴展版函數
描述:這個函數用于ucosII處理完成一個任務,它要么在多任務處理之前建立,
要么運行任務建立,它不夠由中斷服務程序建立。
它與OSTaskCreate()功能相似,但它允許一個特殊任務的更多信息。
參數:task: 指向任務代碼的指針
pdata:任務開始執行時,傳遞給任務參數的指針,用法見上。
ptos:分配給任務堆棧的棧頂指針
prio:分配給任務的優先級
id:為任務創建一個特殊標志符,擴展版本有用。在這里設為和優先級一樣即可。
pbos:指向任務堆棧棧底指針,用于堆棧檢驗
stk_size:用于指定堆棧容量。例:如果堆棧入口寬度為4B,那么stk_size為1000,
說明堆棧有4000B
pext:指向用戶附加的數據域指針,用來擴展任務的任務控制塊OS_TCB
opt:設定OSTaskCreateExt的選項,指定是否允許堆棧檢驗,是否將堆棧清零,是否
進行浮點數操作等。具體見uCOS_II中每一位定義。只要將相應位與opt相或即可
返回:OS_NO_ERR :如果函數成功。
OS_PRIO_EXIT :如果優先級已經存在。
OS_PRIO_INVALID:如果定義優先級的數大于最大值
*****************************************************************************
*/
/*$PAGE*/
#if OS_TASK_CREATE_EXT_EN > 0 //能使包含創建代碼
INT8U OSTaskCreateExt (void (*task)(void *pd),
void *pdata,
OS_STK *ptos,
INT8U prio,
INT16U id,
OS_STK *pbos,
INT32U stk_size,
void *pext,
INT16U opt)//具體含義見上
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
//為CPU狀態寄存器分配存儲空間
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 */
//保證此優先級不存在
OSTCBPrioTbl[prio] = (OS_TCB *)1; /* Reserve the priority to prevent others from doing ... */
/* ... the same thing until task is created. */
//保留優先級,說明此優先級已經占用
OS_EXIT_CRITICAL();//退出臨界狀態,這樣可以使此函數在定義任務數據結構
//其它部分的時候,能夠開中斷
if (((opt & OS_TASK_OPT_STK_CHK) != 0x0000) || /* See if stack checking has been enabled */
//允許檢驗堆棧
((opt & OS_TASK_OPT_STK_CLR) != 0x0000)) { /* See if stack needs to be cleared */
//任務建立時清除堆棧
#if OS_STK_GROWTH == 1//堆棧從低到高增長
(void)memset(pbos, 0, stk_size * sizeof(OS_STK));
//memset是一個標準的ANSI函數,編譯器商家會使之最優化
#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_TCBInit返回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
/*$PAGE*/
/*
*********************************************************************************************************
* DELETE A TASK
*
* Description: This function allows you to delete a task. The calling task can delete itself by
* its own priority number. The deleted task is returned to the dormant state and can be
* re-activated by creating the deleted task again.
*
* Arguments : prio is the priority of the task to delete. Note that you can explicitely delete
* the current task without knowing its priority level by setting 'prio' to
* OS_PRIO_SELF.
*
* Returns : OS_NO_ERR if the call is successful
* OS_TASK_DEL_IDLE if you attempted to delete uC/OS-II's idle task
* OS_PRIO_INVALID if the priority you specify is higher that the maximum allowed
* (i.e. >= OS_LOWEST_PRIO) or, you have not specified OS_PRIO_SELF.
* OS_TASK_DEL_ERR if the task you want to delete does not exist
* OS_TASK_DEL_ISR if you tried to delete a task from an ISR
*
* Notes : 1) To reduce interrupt latency, OSTaskDel() 'disables' the task:
* a) by making it not ready
* b) by removing it from any wait lists
* c) by preventing OSTimeTick() from making the task ready to run.
* The task can then be 'unlinked' from the miscellaneous structures in uC/OS-II.
* 2) The function OS_Dummy() is called after OS_EXIT_CRITICAL() because, on most processors,
* the next instruction following the enable interrupt instruction is ignored.
* 3) An ISR cannot delete a task.
* 4) The lock nesting counter is incremented because, for a brief instant, if the current
* task is being deleted, the current task would not be able to be rescheduled because it
* is removed from the ready list. Incrementing the nesting counter prevents another task
* from being schedule. This means that an ISR would return to the current task which is
* being deleted. The rest of the deletion would thus be able to be completed.
*********************************************************************************************************
*/
/*
*****************************************************************************************************
刪除一個任務
描述:此函數允許你刪除一個任務,這個正在調用的任務能通過它自己的優先級數刪除它自己,
被刪除的任務返回睡眠狀態,它能通過創建一個已刪除任務再次激活。
參數:prio:被刪除任務的優先級,當將prio設成OS_PRIO_SELF時,我們不知道它的優先級也
能夠刪除它
返回:OS_NO_ERR:刪除成功
OS_TASK_DEL_IDLE:如果試圖刪除空閑任務
OS_PRIO_INVALID:如果指定優先級高過允許值
OS_TASK_DEL_ERR:如果想刪除的任務不存在
OS_TASK_DEL_ISR:如果想從中斷服務程序中刪除任務
備注:1、為減少中斷延時,OSTaskDel()通過三個手段不能使任務:
a、指定它沒有就緒b、從等待列表中刪除c、在OSTimeTick()中設置阻止任務準備運行。
然后任務會被從ucos的復雜結構中解鏈
2、函數OS_Dummy() 在OS_EXIT_CRITICAL()調用之后調用是因為在多數處理器中,在能使中斷
指令的下一條指令將被忽略。
3、一個中斷服務程序不能刪除一個任務
4、鎖定嵌套數增加是因為作為一個即時啟動,如果當前任務被刪除,這個當前任務不能重新被
安排因為它已經從就緒列表中刪除。增加嵌套數防止另一個任務被調度。這意味著中斷服務程序
將返回被刪除的當前任務,余下的刪除工作將被中止。
*********************************************************************************************************
*/
/*$PAGE*/
#if OS_TASK_DEL_EN > 0//允許包含任務刪除代碼
INT8U OSTaskDel (INT8U prio)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;//為CPU狀態寄存器分配存儲空間
#endif
#if OS_EVENT_EN > 0
//能使隊列代碼產生&&申請隊列控制塊最大數不為零||能使郵箱代碼產生||
//能使信號量代碼產生||能使互斥量代碼產生
OS_EVENT *pevent;//OS_EVENT:事件控制塊
#endif
#if (OS_VERSION >= 251) && (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)
//OS版本大于等于251,能使事件標志,事件標志最在值大于零
OS_FLAG_NODE *pnode;//事件標志等待列表
#endif
OS_TCB *ptcb;//任務控制塊
BOOLEAN self;//#define BOOLEAN unsigned char
if (OSIntNesting > 0) { /* See if trying to delete from ISR */
//看是不是要從ISR中刪除
return (OS_TASK_DEL_ISR);//返回
}
#if OS_ARG_CHK_EN > 0//允許參數檢測?
if (prio == OS_IDLE_PRIO) { /* Not allowed to delete idle task */
//保證刪除的不是空閑任務
return (OS_TASK_DEL_IDLE);
}
if (prio >= OS_LOWEST_PRIO && prio != OS_PRIO_SELF) { /* Task priority valid ? */
//如果被刪除任務優先級違法
return (OS_PRIO_INVALID);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -