前言
这个任务管理源代码,是整个系统最核心的部分,也是最难的部分,多看几遍吧。其中的核心结构体是:
typedef struct os_tcb {
OS_STK *OSTCBStkPtr; /* Pointer to current top of stack */
#if OS_TASK_CREATE_EXT_EN > 0u
void *OSTCBExtPtr; /* Pointer to user definable data for TCB extension */
OS_STK *OSTCBStkBottom; /* Pointer to bottom of stack */
INT32U OSTCBStkSize; /* Size of task stack (in number of stack elements) */
INT16U OSTCBOpt; /* Task options as passed by OSTaskCreateExt() */
INT16U OSTCBId; /* Task ID (0..65535) */
#endif
struct os_tcb *OSTCBNext; /* Pointer to next TCB in the TCB list */
struct os_tcb *OSTCBPrev; /* Pointer to previous TCB in the TCB list */
#if OS_TASK_CREATE_EXT_EN > 0u
#if defined(OS_TLS_TBL_SIZE) && (OS_TLS_TBL_SIZE > 0u)
OS_TLS OSTCBTLSTbl[OS_TLS_TBL_SIZE];
#endif
#endif
#if (OS_EVENT_EN)
OS_EVENT *OSTCBEventPtr; /* Pointer to event control block */
#endif
#if (OS_EVENT_EN) && (OS_EVENT_MULTI_EN > 0u)
OS_EVENT **OSTCBEventMultiPtr; /* Pointer to multiple event control blocks */
#endif
#if ((OS_Q_EN > 0u) && (OS_MAX_QS > 0u)) || (OS_MBOX_EN > 0u)
void *OSTCBMsg; /* Message received from OSMboxPost() or OSQPost() */
#endif
#if (OS_FLAG_EN > 0u) && (OS_MAX_FLAGS > 0u)
#if OS_TASK_DEL_EN > 0u
OS_FLAG_NODE *OSTCBFlagNode; /* Pointer to event flag node */
#endif
OS_FLAGS OSTCBFlagsRdy; /* Event flags that made task ready to run */
#endif
INT32U OSTCBDly; /* Nbr ticks to delay task or, timeout waiting for event */
INT8U OSTCBStat; /* Task status */
INT8U OSTCBStatPend; /* Task PEND status */
INT8U OSTCBPrio; /* Task priority (0 == highest) */
INT8U OSTCBX; /* Bit position in group corresponding to task priority */
INT8U OSTCBY; /* Index into ready table corresponding to task priority */
OS_PRIO OSTCBBitX; /* Bit mask to access bit position in ready table */
OS_PRIO OSTCBBitY; /* Bit mask to access bit position in ready group */
#if OS_TASK_DEL_EN > 0u
INT8U OSTCBDelReq; /* Indicates whether a task needs to delete itself */
#endif
#if OS_TASK_PROFILE_EN > 0u
INT32U OSTCBCtxSwCtr; /* Number of time the task was switched in */
INT32U OSTCBCyclesTot; /* Total number of clock cycles the task has been running */
INT32U OSTCBCyclesStart; /* Snapshot of cycle counter at start of task resumption */
OS_STK *OSTCBStkBase; /* Pointer to the beginning of the task stack */
INT32U OSTCBStkUsed; /* Number of bytes used from the stack */
#endif
#if OS_TASK_NAME_EN > 0u
INT8U *OSTCBTaskName;
#endif
#if OS_TASK_REG_TBL_SIZE > 0u
INT32U OSTCBRegTbl[OS_TASK_REG_TBL_SIZE];
#endif
} OS_TCB;
/*任务管理(已看)
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
* TASK MANAGEMENT
*
* (c) Copyright 1992-2017; Micrium, Inc.; Weston; FL
* All Rights Reserved
*
* File : OS_TASK.C
* By : Jean J. Labrosse
* Version : V2.92.13
*
* LICENSING TERMS:
* ---------------
* uC/OS-II is provided in source form for FREE evaluation, for educational use or for peaceful research.
* If you plan on using uC/OS-II in a commercial product you need to contact Micrium to properly license
* its use in your product. We provide ALL the source code for your convenience and to help you experience
* uC/OS-II. The fact that the source is provided does NOT mean that you can use it without paying a
* licensing fee.
*
* Knowledge of the source code may NOT be used to develop a similar product.
*
* Please help us continue to provide the embedded community with the finest software available.
* Your honesty is greatly appreciated.
*
* You can find our product's user manual, API reference, release notes and
* more information at https://doc.micrium.com.
* You can contact us at www.micrium.com.
*********************************************************************************************************
*/
#define MICRIUM_SOURCE
#ifndef OS_MASTER_FILE
#include <ucos_ii.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_ERR_NONE is the call was successful
* 正确执行
* OS_ERR_PRIO_INVALID if the priority you specify is higher that the maximum allowed
* (i.e. >= OS_LOWEST_PRIO)
* 如果你赋予的优先级比系统所允许的优先级还要高
* OS_ERR_PRIO_EXIST if the new priority already exist.
* 如果新的优先级已经存在
* OS_ERR_PRIO there is no task with the specified OLD priority (i.e. the OLD task does
* not exist.
* 旧优先级的任务并不存在
* OS_ERR_TASK_NOT_EXIST if the task is assigned to a Mutex PIP.
* 新的优先级已经被优先级反转所占用
*********************************************************************************************************
*/
//修改任务优先级
#if OS_TASK_CHANGE_PRIO_EN > 0u
INT8U OSTaskChangePrio (INT8U oldprio,
INT8U newprio)
{
//#define OS_EVENT_EN (((OS_Q_EN > 0u) && (OS_MAX_QS > 0u)) || (OS_MBOX_EN > 0u) || (OS_SEM_EN > 0u) || (OS_MUTEX_EN > 0u))
#if (OS_EVENT_EN)//如果允许定义事件,则定义一个指向ECB的指针
OS_EVENT *pevent;
//#define OS_EVENT_MULTI_EN 1u /* Include code for OSEventPendMulti() */
#if (OS_EVENT_MULTI_EN > 0u)
OS_EVENT **pevents;
#endif
#endif
OS_TCB *ptcb;//指向TCB的指针
INT8U y_new;
INT8U x_new;
INT8U y_old;
OS_PRIO bity_new;
OS_PRIO bitx_new;
OS_PRIO bity_old;
OS_PRIO bitx_old;
#if OS_CRITICAL_METHOD == 3u
OS_CPU_SR cpu_sr = 0u; /* Storage for CPU status register */
#endif
#if OS_ARG_CHK_EN > 0u //是否允许参数检查
if (oldprio >= OS_LOWEST_PRIO) {
if (oldprio != OS_PRIO_SELF) {
//#define OS_PRIO_SELF 0xFFu /* Indicate SELF priority */
return (OS_ERR_PRIO_INVALID);
}
}
if (newprio >= OS_LOWEST_PRIO) {
return (OS_ERR_PRIO_INVALID);
}
#endif
OS_ENTER_CRITICAL();//关中断
if (OSTCBPrioTbl[newprio] != (OS_TCB *)0) { /* New priority must not already exist */
OS_EXIT_CRITICAL();//开中断
return (OS_ERR_PRIO_EXIST);
}
if (oldprio == OS_PRIO_SELF) { /* See if changing self */
oldprio = OSTCBCur->OSTCBPrio; /* Yes, get priority */
}//OS_EXT OS_TCB *OSTCBCur; /* Pointer to currently running TCB */
//ucos_ii.h中定义了外部变量,直接引用即可
ptcb = OSTCBPrioTbl[oldprio];//在优先级表中获取对应的优先级的TCB指针
if (ptcb == (OS_TCB *)0) { /* Does task to change exist? */
OS_EXIT_CRITICAL(); /* No, can't change its priority! */
return (OS_ERR_PRIO);
}
if (ptcb == OS_TCB_RESERVED) { /* Is task assigned to Mutex */
//OS_TCB_RESERVED 用于优先级反转时反转的优先级
OS_EXIT_CRITICAL(); /* No, can't change its priority! */
return (OS_ERR_TASK_NOT_EXIST);
}
#if OS_LOWEST_PRIO <= 63u
//旧版的ucos_ii有64个任务
//左移三位获得优先级高三位,强转成8位
y_new = (INT8U)(newprio >> 3u); /* Yes, compute new TCB fields */
//与00000111B逐位相与获得低三位,强转成8位
x_new = (INT8U)(newprio & 0x07u);
#else
//较新版的ucos_ii中有256个任务,为8位
//左移4位获得高4位,强转成8位,与00001111逐位相与获得低四位
y_new = (INT8U)((INT8U)(newprio >> 4u) & 0x0Fu);
//与00001111逐位相与获得低四位
x_new = (INT8U)(newprio & 0x0Fu);
//当优先级需要8位来存储的时候,高四位为就绪组中的,低四位为就绪表中的,如果存储高四位的位置,则需要16位来存储位置
#endif
//在就绪组/表中所对应的位置
/*
#if OS_LOWEST_PRIO <= 63u
typedef INT8U OS_PRIO;
#else
typedef INT16U OS_PRIO;
#endif
*/
//如果获取的高三位,说明1左移最多可以左移7位,即,就绪表为INT8U
//如果获取的高四位,说明1左移最多可以左移15位,即,就绪表为INT16U
bity_new = (OS_PRIO)(1uL << y_new);
bitx_new = (OS_PRIO)(1uL << x_new);
OSTCBPrioTbl[oldprio] = (OS_TCB *)0; /* Remove TCB from old priority */
OSTCBPrioTbl[newprio] = ptcb; /* Place pointer to TCB @ new priority */
y_old = ptcb->OSTCBY;
bity_old = ptcb->OSTCBBitY;
bitx_old = ptcb->OSTCBBitX;
//修改就绪组/表,使任务取消就绪状态
if ((OSRdyTbl[y_old] & bitx_old) != 0u) { /* If task is ready make it not */
OSRdyTbl[y_old] &= (OS_PRIO)~bitx_old;
if (OSRdyTbl[y_old] == 0u) {
OSRdyGrp &= (OS_PRIO)~bity_old;
}//取消原有就绪状态,此处比较巧妙,先修改就绪表,再修改就绪组
OSRdyGrp |= bity_new; /* Make new priority ready to run */
OSRdyTbl[y_new] |= bitx_new;
OS_TRACE_TASK_READY(ptcb);
}
//如果TCB在等待事件的话,修改事件等待组/表
#if (OS_EVENT_EN)
pevent = ptcb->OSTCBEventPtr;
if (pevent != (OS_EVENT *)0) {
pevent->OSEventTbl[y_old] &= (OS_PRIO)~bitx_old; /* Remove old task prio from wait list */
if (pevent->OSEventTbl[y_old] == 0u) {
pevent->OSEventGrp &= (OS_PRIO)~bity_old;
}
pevent->OSEventGrp |= bity_new; /* Add new task prio to wait list */
pevent->OSEventTbl[y_new] |= bitx_new;
}
#if (OS_EVENT_MULTI_EN > 0u)
if (ptcb->OSTCBEventMultiPtr != (OS_EVENT **)0) {
//pevents是一个二级指针,但是指向的是一维数组,数组元素是一级指针
pevents = ptcb->OSTCBEventMultiPtr;
//获得一级指针
pevent = *pevents;
while (pevent != (OS_EVENT *)0) {
pevent->OSEventTbl[y_old] &= (OS_PRIO)~bitx_old; /* Remove old task prio from wait lists */
if (pevent->OSEventTbl[y_old] == 0u) {
pevent->OSEventGrp &= (OS_PRIO)~bity_old;
}
pevent->OSEventGrp |= bity_new; /* Add new task prio to wait lists */
pevent->OSEventTbl[y_new] |= bitx_new;
pevents++;//转向下一个一级指针
pevent = *pevents;
}
}
#endif
#endif
//修改TCB为新设置的值
ptcb->OSTCBPrio = newprio; /* Set new task priority */
ptcb->OSTCBY = y_new;
ptcb->OSTCBX = x_new;
ptcb->OSTCBBitY = bity_new;
ptcb->OSTCBBitX = bitx_new;
OS_EXIT_CRITICAL();//开中断
if (OSRunning == OS_TRUE) {
OS_Sched(); /* Find new highest priority task */
}//如果系统正在运行,执行一次调度
return (OS_ERR_NONE);
}
#endif
/*
*********************************************************************************************************
* 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.(此功能用于让uC / OS-II管理任务的执行。 可以在开始多任务之前创建任务,也可以通过正在运行的任务创建任务。 ISR无法创建任务)
*
* Arguments : task is a pointer to the task's code
* //是一个指向任务代码的指针
* p_arg 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 'p_arg' as follows:
*
* void Task (void *p_arg)
* {
* for (;;) {
* Task code;
* }
* }
* //是指向可选数据区域的指针,当任务首次执行时,该数据区域可用于将参数传递给任务。 在涉及任务的地方,它认为它已被调用并传递了参数“ p_arg”
* 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.
* //是指向任务堆栈栈顶的指针。如果配置常量OS_STK_GROWTH被设置为1,那么这个堆栈就被认为是向下增长的(从高址向低址)。因此,‘pstk’会指向堆栈最高的有效地址。如果OS_STK_GROWTH被设置为0,‘ptsk’会指向堆栈最低的有效地址,并且堆栈的增长方向是向上增长的
* 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_ERR_NONE if the function was successful.
* //函数正常执行
* OS_ERR_PRIO_EXIST if the task priority already exist
* //优先级已经存在
* (each task MUST have a unique priority).
* OS_ERR_PRIO_INVALID if the priority you specify is higher that the maximum
* allowed (i.e. >= OS_LOWEST_PRIO)
* //优先级大于系统所允许的优先级
* OS_ERR_TASK_CREATE_ISR if you tried to create a task from an ISR.
* //ISR中禁止创建任务
* OS_ERR_ILLEGAL_CREATE_RUN_TIME if you tried to create a task after safety critical
* operation started.
* //如果您在安全关键操作开始后尝试创建任务。
*********************************************************************************************************
*/
#if OS_TASK_CREATE_EN > 0u //允许任务创建
INT8U OSTaskCreate (void (*task)(void *p_arg),//函数指针,指向函数的指针,也就是说任务的代码是一个函数
void *p_arg,//函数的参数
OS_STK *ptos,//堆栈的栈顶
INT8U prio)//任务的优先级
{
OS_STK *psp;//指向任务堆栈的指针
INT8U err;//返回的状态码
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL_IEC61508
if (OSSafetyCriticalStartFlag == OS_TRUE) {
OS_SAFETY_CRITICAL_EXCEPTION();
return (OS_ERR_ILLEGAL_CREATE_RUN_TIME);
}
#endif
#if OS_ARG_CHK_EN > 0u //是否允许参数检查
if (prio > OS_LOWEST_PRIO) { /* Make sure priority is within allowable range */
return (OS_ERR_PRIO_INVALID);
//创建的任务的优先级大于最低优先级,优先级无效
}
#endif
OS_ENTER_CRITICAL();//关中断
if (OSIntNesting > 0u) { /* Make sure we don't create the task from within an ISR */
//ISR中禁止创建任务,ISR为,中断服务程序
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_CREATE_ISR);
}
if (OSTCBPrioTbl[prio] == (OS_TCB *)0) { /* Make sure task doesn't already exist at this priority */
OSTCBPrioTbl[prio] = OS_TCB_RESERVED;/* Reserve the priority to prevent others from doing ... */
/* ... the same thing until task is created. */
OS_EXIT_CRITICAL();//及时的退出临界区,开中断
psp = OSTaskStkInit(task, p_arg, ptos, 0u); /* Initialize the task's stack */
err = OS_TCBInit(prio, psp, (OS_STK *)0, 0u, 0u, (void *)0, 0u);
if (err == OS_ERR_NONE) {
OS_TRACE_TASK_CREATE(OSTCBPrioTbl[prio]);
if (OSRunning == OS_TRUE) { /* Find highest priority task if multitasking has started */
OS_Sched();
}
} else {
OS_TRACE_TASK_CREATE_FAILED(OSTCBPrioTbl[prio]);
OS_ENTER_CRITICAL();
OSTCBPrioTbl[prio] = (OS_TCB *)0;/* Make this priority available to others */
OS_EXIT_CRITICAL();
}
return (err);
}
OS_EXIT_CRITICAL();
return (OS_ERR_PRIO_EXIST);
}
#endif
/*
*********************************************************************************************************
* 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.
*(这个函数是uC/OS-II用来管理任务的执行的。任务既可以在多任务开始之前被创建,也可以被正在运行的任务创建。不能再ISR中创建任务。这个函数与OSTaskCreate()类似,不同之处在于它允许指定有关任务的其他信息。)
* Arguments : task is a pointer to the task's code
*
* p_arg 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 'p_arg' as follows:
*
* void Task (void *p_arg)
* {
* 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). 'ptos' will thus point to the highest (valid) memory
* location of the stack. If OS_STK_GROWTH is set to 0, 'ptos' will point to the
* lowest memory location of the stack and the stack will grow with increasing
* memory locations. 'ptos' 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.
* 是一个用户提供的内存地址指针,用来作为TCB的扩展,例如,这段用户提供的内存,在上下文切换的时候可以用来存储浮点寄存器的内容,每个任务的执行时间,任务切换的次数等等
* 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. Current choices are:
* //包含任务行为的其他额外信息,低8位是被系统所占有的,高8位,可以由用户程序自定义,目前的选项如下所示:
* OS_TASK_OPT_STK_CHK Stack checking to be allowed for the task
* OS_TASK_OPT_STK_CLR Clear the stack when the task is created
* OS_TASK_OPT_SAVE_FP If the CPU has floating-point registers, save them
* during a context switch.
*
* Returns : OS_ERR_NONE if the function was successful.
* OS_ERR_PRIO_EXIST if the task priority already exist
* (each task MUST have a unique priority).
* OS_ERR_PRIO_INVALID if the priority you specify is higher that the maximum
* allowed (i.e. > OS_LOWEST_PRIO)
* OS_ERR_TASK_CREATE_ISR if you tried to create a task from an ISR.
* OS_ERR_ILLEGAL_CREATE_RUN_TIME if you tried to create a task after safety critical
* operation started.
*********************************************************************************************************
*/
#if OS_TASK_CREATE_EXT_EN > 0u //是否允许创建扩展版本的任务
INT8U OSTaskCreateExt (void (*task)(void *p_arg),//函数指针
void *p_arg,//函数参数
OS_STK *ptos,//堆栈栈顶
INT8U prio,//任务优先级
INT16U id,//任务ID
OS_STK *pbos,//堆栈栈底
INT32U stk_size,//堆栈大小
void *pext,//扩展块
INT16U opt)
{
OS_STK *psp;//定义堆栈指针
INT8U err;//返回值
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
//?????
#ifdef OS_SAFETY_CRITICAL_IEC61508
if (OSSafetyCriticalStartFlag == OS_TRUE) {
OS_SAFETY_CRITICAL_EXCEPTION();
return (OS_ERR_ILLEGAL_CREATE_RUN_TIME);
}
#endif
#if OS_ARG_CHK_EN > 0u //是否允许参数检查
if (prio > OS_LOWEST_PRIO) { /* Make sure priority is within allowable range */
return (OS_ERR_PRIO_INVALID);
}
#endif
OS_ENTER_CRITICAL();
if (OSIntNesting > 0u) { /* Make sure we don't create the task from within an ISR */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_CREATE_ISR);
}
if (OSTCBPrioTbl[prio] == (OS_TCB *)0) { /* Make sure task doesn't already exist at this priority */
//先占用优先级列表,在OS_TCBInit()设置具体的TCB指针
OSTCBPrioTbl[prio] = OS_TCB_RESERVED;/* Reserve the priority to prevent others from doing ... */
/* ... the same thing until task is created. */
OS_EXIT_CRITICAL();
#if (OS_TASK_STAT_STK_CHK_EN > 0u)//因为opt中会有对应的参数,所以要进行一下参数确认,是否清空堆栈
OS_TaskStkClr(pbos, stk_size, opt); /* Clear the task stack (if needed) */
#endif
psp = OSTaskStkInit(task, p_arg, ptos, opt); /* Initialize the task's stack */
err = OS_TCBInit(prio, psp, pbos, id, stk_size, pext, opt);
if (err == OS_ERR_NONE) {
OS_TRACE_TASK_CREATE(OSTCBPrioTbl[prio]);
if (OSRunning == OS_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_ERR_PRIO_EXIST);
}
#endif
/*
*********************************************************************************************************
* 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 explicitly delete
* the current task without knowing its priority level by setting 'prio' to
* OS_PRIO_SELF.
*是将要被删除的任务的优先级,注意,你可以很明确的在不知道任务自身的优先级的情况下删除目前的任务,只需要将参数设置为OS_PRIO_SELF
* Returns : OS_ERR_NONE if the call is successful
* OS_ERR_ILLEGAL_DEL_RUN_TIME if you tried to delete a task after safety critical operation
* started.
* OS_ERR_TASK_DEL_IDLE if you attempted to delete uC/OS-II's idle task(空闲任务)
* OS_ERR_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_ERR_TASK_DEL if the task is assigned to a Mutex PIP.
* OS_ERR_TASK_NOT_EXIST if the task you want to delete does not exist.
* OS_ERR_TASK_DEL_ISR if you tried to delete a task from an ISR
*
* Notes : 1) To reduce interrupt latency, OSTaskDel() 'disables' the task:
//为了减少中断等待时间,OSTaskDel()禁用任务通过以下方法:
* 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.
//然后可以将任务与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.
//在OS_EXIT_CRITICAL()之后调用了OS_Dummy()因为在大多数处理器上,enable interrupt指令之后的下一个指令都被忽略了
* 3) An ISR cannot delete a task.//ISR中禁止删除任务
* 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.
调度器锁定计数增加,因为在很短的时间内,如果当前的任务被删除了,那么当前任务将无法再次被调度,因为它已经被从就绪列表中删除了,增加调度器锁定计数,可以阻止调度其他任务,这就意味着,如果运行完ISR程序后,就会再次回到当前被删除的任务,使得剩下的删除步骤可以正确的完成
*********************************************************************************************************
*/
#if OS_TASK_DEL_EN > 0u //如果允许删除任务
INT8U OSTaskDel (INT8U prio)
{
//如果允许使用事件标志组,则定义事件标志组节点
#if (OS_FLAG_EN > 0u) && (OS_MAX_FLAGS > 0u)
OS_FLAG_NODE *pnode;
#endif
OS_TCB *ptcb;//创建指向TCB的指针
//涉及到进入临界区的就得用这种方法,因为,前两种方法,漏洞大,目前主流的操作系统都是使用的局部变量的方法,但是ucos为了兼容前两种,才这么做。
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
//CPU_INT32U
#endif
//关于cpu_sr,是要用局部变量来保存中断状态,体现在OS_ENTER_CRITICAL()进入中断时将中断状态保存进cpu_sr中,在OS_EXIT_CRITICAL()将cpu_sr中的状态恢复,详情见【https://blog.csdn.net/qb_2008/article/details/7201340】,主要是为了中断嵌套,但是在同一个函数中不允许中断嵌套
//这个是ucos做的安全认证,如果想要通过IEC61508认证就得符合相应的软件规范,这个是规范的一部分。
#ifdef OS_SAFETY_CRITICAL_IEC61508
if (OSSafetyCriticalStartFlag == OS_TRUE) {
OS_SAFETY_CRITICAL_EXCEPTION();
return (OS_ERR_ILLEGAL_DEL_RUN_TIME);
}
#endif
if (OSIntNesting > 0u) { /* See if trying to delete from ISR */
//不允许中断嵌套
return (OS_ERR_TASK_DEL_ISR);
}
if (prio == OS_TASK_IDLE_PRIO) { /* Not allowed to delete idle task */
return (OS_ERR_TASK_DEL_IDLE);
}
#if OS_ARG_CHK_EN > 0u //是否允许参数检查
if (prio >= OS_LOWEST_PRIO) { /* Task priority valid ? */
if (prio != OS_PRIO_SELF) {
return (OS_ERR_PRIO_INVALID);
}
}
#endif
OS_ENTER_CRITICAL();//关中断
if (prio == OS_PRIO_SELF) { /* See if requesting to delete self */
//如果选择的是OS_PRIO_SELF,则为获得当前任务TCB
prio = OSTCBCur->OSTCBPrio; /* Set priority to delete to current */
}
ptcb = OSTCBPrioTbl[prio];
if (ptcb == (OS_TCB *)0) { /* Task to delete must exist */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NOT_EXIST);
}
if (ptcb == OS_TCB_RESERVED) { /* Must not be assigned to Mutex */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_DEL);
}
OSRdyTbl[ptcb->OSTCBY] &= (OS_PRIO)~ptcb->OSTCBBitX;//取消就绪表中的状态
OS_TRACE_TASK_SUSPENDED(ptcb);
//取消就绪组中的状态
if (OSRdyTbl[ptcb->OSTCBY] == 0u) { /* Make task not ready */
OSRdyGrp &= (OS_PRIO)~ptcb->OSTCBBitY;
}
//如果允许等待事件
#if (OS_EVENT_EN)
//等待单个事件
if (ptcb->OSTCBEventPtr != (OS_EVENT *)0) {
//取消等待组表标志,断开TCB与ECB链接
OS_EventTaskRemove(ptcb, ptcb->OSTCBEventPtr); /* Remove this task from any event wait list */
}
//等待多个事件
#if (OS_EVENT_MULTI_EN > 0u)
if (ptcb->OSTCBEventMultiPtr != (OS_EVENT **)0) { /* Remove this task from any events' wait lists*/
OS_EventTaskRemoveMulti(ptcb, ptcb->OSTCBEventMultiPtr);
}//ptcb->OSTCBEventMultiPtr感觉像是一个数组,支持随机访问
#endif
#endif
//是否允许事件标志组
#if (OS_FLAG_EN > 0u) && (OS_MAX_FLAGS > 0u)
//获取事件标志组节点
pnode = ptcb->OSTCBFlagNode;
if (pnode != (OS_FLAG_NODE *)0) { /* If task is waiting on event flag */
//从事件标志组的等待链表中移除指定的事件标志组节点
OS_FlagUnlink(pnode); /* Remove from wait list */
}
#endif
//防止时钟滴答服务将其唤醒为就绪态
ptcb->OSTCBDly = 0u; /* Prevent OSTimeTick() from updating */
//防止任务被唤醒,设置任务为RDY状态
ptcb->OSTCBStat = OS_STAT_RDY; /* Prevent task from being resumed */
ptcb->OSTCBStatPend = OS_STAT_PEND_OK;
//给调度器上锁,因为任务修改就绪表后,就没有了优先级,所以不会被调度,上锁后,ISR后,仍然会回到这里
if (OSLockNesting < 255u) { /* Make sure we don't context switch */
OSLockNesting++;
}
OS_EXIT_CRITICAL(); /* Enabling INT. ignores next instruc. */
//任务执行时间太长了,影响中断,故退出临界区,开中断,处理中断任务
OS_Dummy(); /* ... Dummy ensures that INTs will be */
//再次进入临界区,关中断
OS_ENTER_CRITICAL(); /* ... disabled HERE! */
//将调度器的锁去掉
if (OSLockNesting > 0u) { /* Remove context switch lock */
OSLockNesting--;
}
//调用钩子函数
OSTaskDelHook(ptcb); /* Call user defined hook */
#if OS_TASK_CREATE_EXT_EN > 0u
#if defined(OS_TLS_TBL_SIZE) && (OS_TLS_TBL_SIZE > 0u)
OS_TLS_TaskDel(ptcb); /* Call TLS hook */
#endif
#endif
OSTaskCtr--; /* One less task being managed */
OSTCBPrioTbl[prio] = (OS_TCB *)0; /* Clear old priority entry */
//将TCB从就绪链表中移除
if (ptcb->OSTCBPrev == (OS_TCB *)0) { /* Remove from TCB chain */
ptcb->OSTCBNext->OSTCBPrev = (OS_TCB *)0;
OSTCBList = ptcb->OSTCBNext;
} else {
ptcb->OSTCBPrev->OSTCBNext = ptcb->OSTCBNext;
ptcb->OSTCBNext->OSTCBPrev = ptcb->OSTCBPrev;
}
//将TCB移入空闲链表
ptcb->OSTCBNext = OSTCBFreeList; /* Return TCB to free TCB list */
OSTCBFreeList = ptcb;
#if OS_TASK_NAME_EN > 0u
ptcb->OSTCBTaskName = (INT8U *)(void *)"?";
#endif
OS_EXIT_CRITICAL();
if (OSRunning == OS_TRUE) {
OS_Sched(); /* Find new highest priority task */
}
return (OS_ERR_NONE);
}
#endif
/*
*********************************************************************************************************
* REQUEST THAT A TASK DELETE ITSELF(请求任务删除它自己)
*
* Description: This function is used to:
* a) notify a task to delete itself.
* b) to see if a task requested that the current task delete itself.
* This function is a little tricky to understand. Basically, you have a task that needs
* to be deleted however, this task has resources that it has allocated (memory buffers,
* semaphores, mailboxes, queues etc.). The task cannot be deleted otherwise these
* resources would not be freed. The requesting task calls OSTaskDelReq() to indicate that
* the task needs to be deleted. Deleting of the task is however, deferred to the task to
* be deleted. For example, suppose that task #10 needs to be deleted. The requesting task
* example, task #5, would call OSTaskDelReq(10). When task #10 gets to execute, it calls
* this function by specifying OS_PRIO_SELF and monitors the returned value. If the return
* value is OS_ERR_TASK_DEL_REQ, another task requested a task delete. Task #10 would look like
* this:
*这个函数的作用是:
*1. 请求一个任务去删除它自己
*2. 查看是否有任务请求删除自己
*该功能有点难以理解。 基本上,您有一个需要删除的任务,但是,此任务具有已分配的资源(内存缓冲区,信号量,邮箱,队列等)。 无法删除任务,否则将无法释放这些资源。 请求任务将调用OSTaskDelReq()以指示需要删除该任务。 任务是无论如何都要被删除的,只不过被延迟到任务需要被删除的时候。 例如,假设需要删除任务#10。 示例,请求任务#5将调用OSTaskDelReq(10)。 当任务#10开始执行时,它通过指定OS_PRIO_SELF调用此函数并监视返回的值。 如果返回值为OS_ERR_TASK_DEL_REQ,则另一个任务请求删除任务。 任务10如下所示:
* void Task(void *p_arg)
* {
* .
* .
* while (1) {
* OSTimeDly(1);
* if (OSTaskDelReq(OS_PRIO_SELF) == OS_ERR_TASK_DEL_REQ) {
* Release any owned resources;
* De-allocate any dynamic memory;
* OSTaskDel(OS_PRIO_SELF);
* }
* }
* }
*
* Arguments : prio is the priority of the task to request the delete from
*请求删除的任务的优先级
* Returns : OS_ERR_NONE if the task exist and the request has been registered
* OS_ERR_ILLEGAL_DEL_RUN_TIME if you tried to delete a task after safety critical operation
* started.
* OS_ERR_TASK_NOT_EXIST if the task has been deleted. This allows the caller to know
* whether the request has been executed.
* OS_ERR_TASK_DEL if the task is assigned to a Mutex.
* OS_ERR_TASK_DEL_IDLE if you requested to delete uC/OS-II's idle task
* OS_ERR_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_ERR_TASK_DEL_REQ if a task (possibly another task) requested that the running
* task be deleted.
*********************************************************************************************************
*/
//查看是否允许删除任务
#if OS_TASK_DEL_EN > 0u
INT8U OSTaskDelReq (INT8U prio)
{
INT8U stat;
OS_TCB *ptcb;
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL_IEC61508
if (OSSafetyCriticalStartFlag == OS_TRUE) {
OS_SAFETY_CRITICAL_EXCEPTION();
return (OS_ERR_ILLEGAL_DEL_RUN_TIME);
}
#endif
//不允许删除空闲任务
if (prio == OS_TASK_IDLE_PRIO) { /* Not allowed to delete idle task */
return (OS_ERR_TASK_DEL_IDLE);
}
//是否进行参数检查
#if OS_ARG_CHK_EN > 0u
if (prio >= OS_LOWEST_PRIO) { /* Task priority valid ? */
if (prio != OS_PRIO_SELF) {
return (OS_ERR_PRIO_INVALID);
}
}
#endif
//查看是否有任务请求删除自己
if (prio == OS_PRIO_SELF) { /* See if a task is requesting to ... */
OS_ENTER_CRITICAL(); /* ... this task to delete itself */
//获取本TCB的OSTCBDelReq
stat = OSTCBCur->OSTCBDelReq; /* Return request status to caller */
OS_EXIT_CRITICAL();
return (stat);
}
OS_ENTER_CRITICAL();
ptcb = OSTCBPrioTbl[prio];
//TCB不存在
if (ptcb == (OS_TCB *)0) { /* Task to delete must exist */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NOT_EXIST); /* Task must already be deleted */
}
//TCB用于互斥信号量
if (ptcb == OS_TCB_RESERVED) { /* Must NOT be assigned to a Mutex */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_DEL);
}
//置位,将被请求的TCB的请求删除位,赋值
ptcb->OSTCBDelReq = OS_ERR_TASK_DEL_REQ; /* Set flag indicating task to be DEL. */
OS_EXIT_CRITICAL();
return (OS_ERR_NONE);
}
#endif
/*
*********************************************************************************************************
* GET THE NAME OF A TASK(获取任务的名字)
*
* Description: This function is called to obtain the name of a task.
*这个函数用于获得任务的名字
* Arguments : prio is the priority of the task that you want to obtain the name from.
*你想要获得名字的任务的优先级
* pname is a pointer to a pointer to an ASCII string that will receive the name of the task.
*一个用于接收任务名字的指针,这个指针是2级指针,因为指向名字的是一级指针,所以,传入二级指针后,*一下,将其一级指针内容赋值为字符串首地址
* perr is a pointer to an error code that can contain one of the following values:
*一个用于返回错误状态码的指针
* OS_ERR_NONE if the requested task is resumed
* OS_ERR_TASK_NOT_EXIST if the task has not been created or is assigned to a Mutex
* OS_ERR_PRIO_INVALID if you specified an invalid priority:
* A higher value than the idle task or not OS_PRIO_SELF.
* OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
* OS_ERR_NAME_GET_ISR You called this function from an ISR
*
*
* Returns : The length of the string or 0 if the task does not exist.
返回string串的长度或者如果任务不存在的话就返回0
*********************************************************************************************************
*/
//是否允许任务使用名字
#if OS_TASK_NAME_EN > 0u
INT8U OSTaskNameGet (INT8U prio,
INT8U **pname,
INT8U *perr)
{
OS_TCB *ptcb;
INT8U len;
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
if (perr == (INT8U *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return (0u);
}
#endif
//是否允许参数检查
#if OS_ARG_CHK_EN > 0u
if (prio > OS_LOWEST_PRIO) { /* Task priority valid ? */
if (prio != OS_PRIO_SELF) {
*perr = OS_ERR_PRIO_INVALID; /* No */
return (0u);
}
}
if (pname == (INT8U **)0) { /* Is 'pname' a NULL pointer? */
*perr = OS_ERR_PNAME_NULL; /* Yes */
return (0u);
}
#endif
//ISR中不允许调用此函数
if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
*perr = OS_ERR_NAME_GET_ISR;
return (0u);
}
OS_ENTER_CRITICAL();//关中断,进入临界区
if (prio == OS_PRIO_SELF) { /* See if caller desires it's own name */
prio = OSTCBCur->OSTCBPrio;
}
//获取TCB指针
ptcb = OSTCBPrioTbl[prio];
//任务不存在
if (ptcb == (OS_TCB *)0) { /* Does task exist? */
OS_EXIT_CRITICAL(); /* No */
*perr = OS_ERR_TASK_NOT_EXIST;
return (0u);
}
//任务被互斥信号量所占用
if (ptcb == OS_TCB_RESERVED) { /* Task assigned to a Mutex? */
OS_EXIT_CRITICAL(); /* Yes */
*perr = OS_ERR_TASK_NOT_EXIST;
return (0u);
}
//pname本身为二级指针
*pname = ptcb->OSTCBTaskName;
len = OS_StrLen(*pname);
OS_EXIT_CRITICAL();
*perr = OS_ERR_NONE;
return (len);
}
#endif
/*
*********************************************************************************************************
* ASSIGN A NAME TO A TASK(为一个任务指定名字)
*
* Description: This function is used to set the name of a task.
*(这个函数被用来指定一个任务的名字)
* Arguments : prio is the priority of the task that you want the assign a name to.
*你想要指定任务名字的任务优先级
* pname is a pointer to an ASCII string that contains the name of the task.
*指向任务名字的指针
* perr is a pointer to an error code that can contain one of the following values:
*指示错误状态码的指针
* OS_ERR_NONE if the requested task is resumed
* OS_ERR_TASK_NOT_EXIST if the task has not been created or is assigned to a Mutex
* OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
* OS_ERR_PRIO_INVALID if you specified an invalid priority:
* A higher value than the idle task or not OS_PRIO_SELF.
* OS_ERR_NAME_SET_ISR if you called this function from an ISR
*
* Returns : None
*********************************************************************************************************
*/
//是否允许任务使用名字
#if OS_TASK_NAME_EN > 0u
void OSTaskNameSet (INT8U prio,
INT8U *pname,
INT8U *perr)
{
OS_TCB *ptcb;
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
#ifdef OS_SAFETY_CRITICAL
if (perr == (INT8U *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return;
}
#endif
//参数检查
#if OS_ARG_CHK_EN > 0u
if (prio > OS_LOWEST_PRIO) { /* Task priority valid ? */
if (prio != OS_PRIO_SELF) {
*perr = OS_ERR_PRIO_INVALID; /* No */
return;
}
}
if (pname == (INT8U *)0) { /* Is 'pname' a NULL pointer? */
*perr = OS_ERR_PNAME_NULL; /* Yes */
return;
}
#endif
//ISR中不允许调用此函数
if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
*perr = OS_ERR_NAME_SET_ISR;
return;
}
OS_ENTER_CRITICAL();//关中断,进入临界区
if (prio == OS_PRIO_SELF) { /* See if caller desires to set it's own name */
prio = OSTCBCur->OSTCBPrio;
}//统一优先级表示
ptcb = OSTCBPrioTbl[prio];//从优先级列表中获得TCB指针
if (ptcb == (OS_TCB *)0) { /* Does task exist? */
OS_EXIT_CRITICAL(); /* No */
*perr = OS_ERR_TASK_NOT_EXIST;
return;
}
if (ptcb == OS_TCB_RESERVED) { /* Task assigned to a Mutex? */
OS_EXIT_CRITICAL(); /* Yes */
*perr = OS_ERR_TASK_NOT_EXIST;
return;
}
ptcb->OSTCBTaskName = pname;
OS_TRACE_TASK_NAME_SET(ptcb);
OS_EXIT_CRITICAL();
*perr = OS_ERR_NONE;
}
#endif
/*
*********************************************************************************************************
* RESUME A SUSPENDED TASK(唤醒一个被挂起的任务)
*
* Description: This function is called to resume a previously suspended task. This is the only call that
* will remove an explicit task suspension.
*这个函数被用来唤醒一个在之前被挂起的任务。这是唯一一个能够唤醒被挂起任务的方法
* Arguments : prio is the priority of the task to resume.
*需要被唤醒的人物的优先级
* Returns : OS_ERR_NONE if the requested task is resumed
* OS_ERR_PRIO_INVALID if the priority you specify is higher that the maximum allowed
* (i.e. >= OS_LOWEST_PRIO)
* OS_ERR_TASK_RESUME_PRIO if the task to resume does not exist
* OS_ERR_TASK_NOT_EXIST if the task is assigned to a Mutex PIP
* OS_ERR_TASK_NOT_SUSPENDED if the task to resume has not been suspended
*********************************************************************************************************
*/
//任务是否允许被挂起
#if OS_TASK_SUSPEND_EN > 0u
INT8U OSTaskResume (INT8U prio)
{
OS_TCB *ptcb;
#if OS_CRITICAL_METHOD == 3u /* Storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
//参数检查
#if OS_ARG_CHK_EN > 0u
if (prio >= OS_LOWEST_PRIO) { /* Make sure task priority is valid */
return (OS_ERR_PRIO_INVALID);
}
#endif
OS_ENTER_CRITICAL();
ptcb = OSTCBPrioTbl[prio];
//任务不存在
if (ptcb == (OS_TCB *)0) { /* Task to suspend must exist */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_RESUME_PRIO);
}
if (ptcb == OS_TCB_RESERVED) { /* See if assigned to Mutex */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NOT_EXIST);
}
//查看是否任务是被挂起状态
//#define OS_STAT_SUSPEND 0x08u --> 00001000 /* Task is suspended
//只是定义了状态码的数值,是8位的,但是真正需要用的时候,还是需要强转一下
if ((ptcb->OSTCBStat & OS_STAT_SUSPEND) != OS_STAT_RDY) { /* Task must be suspended */
//取消任务状态的挂起标志
ptcb->OSTCBStat &= (INT8U)~(INT8U)OS_STAT_SUSPEND; /* Remove suspension */
//查看任务是否还在等待其他事件
//#define OS_STAT_PEND_ANY (OS_STAT_SEM | OS_STAT_MBOX | OS_STAT_Q | OS_STAT_MUTEX | OS_STAT_FLAG)
if ((ptcb->OSTCBStat & OS_STAT_PEND_ANY) == OS_STAT_RDY) { /* See if task is now ready */
//如果任务仅仅只是挂起态,没有等待其他事件,那么就可以被唤醒了
//TCB中是否延时为0
if (ptcb->OSTCBDly == 0u) {
OSRdyGrp |= ptcb->OSTCBBitY; /* Yes, Make task ready to run */
OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
OS_TRACE_TASK_READY(ptcb);
OS_EXIT_CRITICAL();
if (OSRunning == OS_TRUE) {
OS_TRACE_TASK_RESUME(ptcb);
OS_Sched(); /* Find new highest priority task */
}
} else {
OS_EXIT_CRITICAL();
}
} else { /* Must be pending on event */
OS_EXIT_CRITICAL();
}
return (OS_ERR_NONE);
}
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NOT_SUSPENDED);
}
#endif
/*
*********************************************************************************************************
* STACK CHECKING (检查堆栈)
*
* Description: This function is called to check the amount of free memory left on the specified task's
* stack.
*这个函数是用来检查特定的任务堆栈所剩余的空闲内存空间
* Arguments : prio is the task priority
*任务的优先级
* p_stk_data is a pointer to a data structure of type OS_STK_DATA.
*指向OS_STK_DATA的指针
* Returns : OS_ERR_NONE upon success
* OS_ERR_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_ERR_TASK_NOT_EXIST if the desired task has not been created or is assigned to a Mutex PIP
* OS_ERR_TASK_OPT if you did NOT specified OS_TASK_OPT_STK_CHK when the task was created
* OS_ERR_PDATA_NULL if 'p_stk_data' is a NULL pointer
*********************************************************************************************************
*/
//查看是否允许堆栈检查,以及是否允许使用扩展版本来创建任务
#if (OS_TASK_STAT_STK_CHK_EN > 0u) && (OS_TASK_CREATE_EXT_EN > 0u)
INT8U OSTaskStkChk (INT8U prio,
OS_STK_DATA *p_stk_data)
/**
#if OS_TASK_CREATE_EXT_EN > 0u
typedef struct os_stk_data {
INT32U OSFree; // Number of free entries on the stack
INT32U OSUsed; // Number of entries used on the stack
} OS_STK_DATA;
#endif
*/
{
OS_TCB *ptcb;//指向任务TCB
OS_STK *pchk;//指向任务堆栈
INT32U nfree;//number free
INT32U size;
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
#if OS_ARG_CHK_EN > 0u
if (prio > OS_LOWEST_PRIO) { /* Make sure task priority is valid */
if (prio != OS_PRIO_SELF) {
return (OS_ERR_PRIO_INVALID);
}
}
if (p_stk_data == (OS_STK_DATA *)0) { /* Validate 'p_stk_data' */
return (OS_ERR_PDATA_NULL);
}
#endif
//初始化p_stk_data信息
p_stk_data->OSFree = 0u; /* Assume failure, set to 0 size */
p_stk_data->OSUsed = 0u;
OS_ENTER_CRITICAL();
if (prio == OS_PRIO_SELF) { /* See if check for SELF */
prio = OSTCBCur->OSTCBPrio;
}
ptcb = OSTCBPrioTbl[prio];
if (ptcb == (OS_TCB *)0) { /* Make sure task exist */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NOT_EXIST);
}
if (ptcb == OS_TCB_RESERVED) {
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NOT_EXIST);
}
//这个是创建任务的时候使用的opt选项,
/**
* 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. Current choices are:
* //包含任务行为的其他额外信息,低8位是被系统所占有的,高8位,可以由用户程序自定义,目前的选项如下所示:
* OS_TASK_OPT_STK_CHK Stack checking to be allowed for the task
* OS_TASK_OPT_STK_CLR Clear the stack when the task is created
* OS_TASK_OPT_SAVE_FP If the CPU has floating-point registers, save them during a context switch.
*/
//查看是否任务支持检查堆栈
if ((ptcb->OSTCBOpt & OS_TASK_OPT_STK_CHK) == 0u) { /* Make sure stack checking option is set */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_OPT);
}
nfree = 0u;
size = ptcb->OSTCBStkSize;//获取堆栈大小
pchk = ptcb->OSTCBStkBottom;//获取栈底,因为无论堆栈是向下增长还是向上增长,都是栈顶向栈底增长,如果一个堆栈是有空闲空间,那么一定在栈底哪里
OS_EXIT_CRITICAL();
//如果堆栈是自上向下增长
#if OS_STK_GROWTH == 1u
while (*pchk++ == (OS_STK)0) { /* Compute the number of zero entries on the stk */
nfree++;
}
#else
while (*pchk-- == (OS_STK)0) {
nfree++;
}
#endif
//对p_stk_data进行最终的赋值
p_stk_data->OSFree = nfree; /* Store number of free entries on the stk */
p_stk_data->OSUsed = size - nfree; /* Compute number of entries used on the stk */
return (OS_ERR_NONE);
}
#endif
/*
*********************************************************************************************************
* SUSPEND A TASK(挂起一个任务)
*
* Description: This function is called to suspend a task. The task can be the calling task if the
* priority passed to OSTaskSuspend() is the priority of the calling task or OS_PRIO_SELF.
*这个函数被用来挂起一个任务,被挂起的任务可以是调用此函数的任务本身,如果传给OSTaskSuspend()的参数是任务自己的优先级,或者OS_PRIO_SELF
* Arguments : prio is the priority of the task to suspend. If you specify OS_PRIO_SELF, the
* calling task will suspend itself and rescheduling will occur.
*是要被挂起的任务的优先级,如果你传递了OS_PRIO_SELF,那么调用此函数的任务本身将被挂起,然后会进行新一次的调度
* Returns : OS_ERR_NONE if the requested task is suspended
* OS_ERR_TASK_SUSPEND_IDLE if you attempted to suspend the idle task which is not allowed.
* OS_ERR_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_ERR_TASK_SUSPEND_PRIO if the task to suspend does not exist
* OS_ERR_TASK_NOT_EXITS if the task is assigned to a Mutex PIP
*
* Note : You should use this function with great care. If you suspend a task that is waiting for
* an event (i.e. a message, a semaphore, a queue ...) you will prevent this task from
* running when the event arrives.
*********************************************************************************************************
*/
//查看是否允许任务挂起
#if OS_TASK_SUSPEND_EN > 0u
INT8U OSTaskSuspend (INT8U prio)
{
BOOLEAN self;//记录是否任务自己挂起自己,如果是自己挂起自己,则进行一次调度
OS_TCB *ptcb;
INT8U y;
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
//参数检查
#if OS_ARG_CHK_EN > 0u
if (prio == OS_TASK_IDLE_PRIO) { /* Not allowed to suspend idle task */
return (OS_ERR_TASK_SUSPEND_IDLE);
}
if (prio >= OS_LOWEST_PRIO) { /* Task priority valid ? */
if (prio != OS_PRIO_SELF) {
return (OS_ERR_PRIO_INVALID);
}
}
#endif
OS_ENTER_CRITICAL();//关中断,进入临界区
if (prio == OS_PRIO_SELF) { /* See if suspend SELF */
prio = OSTCBCur->OSTCBPrio;
self = OS_TRUE;
} else if (prio == OSTCBCur->OSTCBPrio) { /* See if suspending self */
self = OS_TRUE;
} else {
self = OS_FALSE; /* No suspending another task */
}//对self进行赋值
ptcb = OSTCBPrioTbl[prio];
if (ptcb == (OS_TCB *)0) { /* Task to suspend must exist */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_SUSPEND_PRIO);
}
if (ptcb == OS_TCB_RESERVED) { /* See if assigned to Mutex */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NOT_EXIST);
}
//获取优先级高位
y = ptcb->OSTCBY;
//取消任务就绪表和就绪组
OSRdyTbl[y] &= (OS_PRIO)~ptcb->OSTCBBitX; /* Make task not ready */
if (OSRdyTbl[y] == 0u) {
OSRdyGrp &= (OS_PRIO)~ptcb->OSTCBBitY;
}
//将任务状态置上挂起态,置上挂起态后,任务只是取消了就绪标志,但是仍然在就绪链表里面,也没有等待事间,也不会有事件等待组表之类的操作
ptcb->OSTCBStat |= OS_STAT_SUSPEND; /* Status of task is 'SUSPENDED' */
OS_EXIT_CRITICAL();
OS_TRACE_TASK_SUSPEND(ptcb);
OS_TRACE_TASK_SUSPENDED(ptcb);
if (self == OS_TRUE) { /* Context switch only if SELF */
OS_Sched(); /* Find new highest priority task */
}
return (OS_ERR_NONE);
}
#endif
/*
*********************************************************************************************************
* QUERY A TASK(查询一个任务)
*
* Description: This function is called to obtain a copy of the desired task's TCB.
*这个函数用来获得一个期望的TCB的备份
* Arguments : prio is the priority of the task to obtain information from.
*想要获得的任务信息的任务的优先级
* p_task_data is a pointer to where the desired task's OS_TCB will be stored.
*指向任务信息存储结构体的指针
* Returns : OS_ERR_NONE if the requested task is suspended
* OS_ERR_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_ERR_PRIO if the desired task has not been created
* OS_ERR_TASK_NOT_EXIST if the task is assigned to a Mutex PIP
* OS_ERR_PDATA_NULL if 'p_task_data' is a NULL pointer
*********************************************************************************************************
*/
//是否允许进行任务查询
#if OS_TASK_QUERY_EN > 0u
INT8U OSTaskQuery (INT8U prio,
OS_TCB *p_task_data)
{
OS_TCB *ptcb;
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
//参数检查
#if OS_ARG_CHK_EN > 0u
if (prio > OS_LOWEST_PRIO) { /* Task priority valid ? */
if (prio != OS_PRIO_SELF) {
return (OS_ERR_PRIO_INVALID);
}
}
if (p_task_data == (OS_TCB *)0) { /* Validate 'p_task_data' */
return (OS_ERR_PDATA_NULL);
}
#endif
OS_ENTER_CRITICAL();
if (prio == OS_PRIO_SELF) { /* See if suspend SELF */
prio = OSTCBCur->OSTCBPrio;
}
ptcb = OSTCBPrioTbl[prio];
if (ptcb == (OS_TCB *)0) { /* Task to query must exist */
OS_EXIT_CRITICAL();
return (OS_ERR_PRIO);
}
if (ptcb == OS_TCB_RESERVED) { /* Task to query must not be assigned to a Mutex */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_NOT_EXIST);
}
/* Copy TCB into user storage area */
//因为p_task_data是OS_TCB类型,所以直接调用按字节拷贝即可,使用的时候,要将指针调成指向一字节的指针
OS_MemCopy((INT8U *)p_task_data, (INT8U *)ptcb, sizeof(OS_TCB));
OS_EXIT_CRITICAL();
return (OS_ERR_NONE);
}
#endif
/*
*********************************************************************************************************
* GET THE CURRENT VALUE OF A TASK REGISTER(获取当前任务寄存器的值)
*
* Description: This function is called to obtain the current value of a task register. Task registers
* are application specific and can be used to store task specific values such as 'error
* numbers' (i.e. errno), statistics, etc. Each task register can hold a 32-bit value.
*这个函数被用来获取当前任务寄存器的值,任务寄存器针对于不同的应用程序,可以存储特定的值,例如:错误状态码,统计信息等,每个任务寄存器可以保持32bit的内容
* Arguments : prio is the priority of the task you want to get the task register from. If you
* specify OS_PRIO_SELF then the task register of the current task will be obtained.
*是你想获取的指定的任务寄存器的值的任务的优先级。如果你使用OS_PRIO_SELF那么将会获得当前任务的任务寄存器的值
* id is the 'id' of the desired task register. Note that the 'id' must be less
* than OS_TASK_REG_TBL_SIZE
*想要获得任务寄存器的ID值,注意,这个id必须小于OS_TASK_REG_TBL_SIZE
* perr is a pointer to a variable that will hold an error code related to this call.
*返回错误代码的指针
* OS_ERR_NONE if the call was successful
* OS_ERR_PRIO_INVALID if you specified an invalid priority
* OS_ERR_ID_INVALID if the 'id' is not between 0 and OS_TASK_REG_TBL_SIZE-1
*
* Returns : The current value of the task's register or 0 if an error is detected.
*任务寄存器的当前的值,如果出现错误的话,将返回0
* Note(s) : The maximum number of task variables is 254
注意:任务的最大数值为254
*********************************************************************************************************
*/
//????
//是否允许任务寄存器列表?
#if OS_TASK_REG_TBL_SIZE > 0u
INT32U OSTaskRegGet (INT8U prio,
INT8U id,
INT8U *perr)
{
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
INT32U value;
OS_TCB *ptcb;
#ifdef OS_SAFETY_CRITICAL
if (perr == (INT8U *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return (0u);
}
#endif
//参数检查
#if OS_ARG_CHK_EN > 0u
if (prio >= OS_LOWEST_PRIO) {
if (prio != OS_PRIO_SELF) {
*perr = OS_ERR_PRIO_INVALID;
return (0u);
}
}
if (id >= OS_TASK_REG_TBL_SIZE) {
*perr = OS_ERR_ID_INVALID;
return (0u);
}
#endif
OS_ENTER_CRITICAL();
if (prio == OS_PRIO_SELF) { /* See if need to get register from current task */
ptcb = OSTCBCur;
} else {
ptcb = OSTCBPrioTbl[prio];
}//获取TCB的指针
value = ptcb->OSTCBRegTbl[id];//每个TCB都有各自对应的寄存器列表,但是id是全局唯一的
OS_EXIT_CRITICAL();
*perr = OS_ERR_NONE;
return (value);
}
#endif
/*
************************************************************************************************************************
* ALLOCATE THE NEXT AVAILABLE TASK REGISTER ID(分配下一个可用的任务寄存器ID)
*
* Description: This function is called to obtain a task register ID. This function thus allows task registers IDs to be
* allocated dynamically instead of statically.
*这个函数被用来获得任务寄存器的id,这个函数允许动态分配任务寄存器id,而不是静态分配
* Arguments : p_err is a pointer to a variable that will hold an error code related to this call.
*指向错误信息的变量的指针
* OS_ERR_NONE if the call was successful
* OS_ERR_NO_MORE_ID_AVAIL if you are attempting to assign more task register IDs than you
* have available through OS_TASK_REG_TBL_SIZE.
*
* Returns : The next available task register 'id' or OS_TASK_REG_TBL_SIZE if an error is detected.
下一个可用的寄存器id,如果发生了一个错误的话,就返回OS_TASK_REG_TBL_SIZE
************************************************************************************************************************
*/
//是否拥有任务寄存器列表
#if OS_TASK_REG_TBL_SIZE > 0u
INT8U OSTaskRegGetID (INT8U *perr)
{
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
INT8U id;
#ifdef OS_SAFETY_CRITICAL
if (perr == (INT8U *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return ((INT8U)OS_TASK_REG_TBL_SIZE);
}
#endif
OS_ENTER_CRITICAL();//关中断,进入临界区
//OSTaskRegNextAvailID是全局变量,也就是寄存器是占有之后,就独占,除非释放
if (OSTaskRegNextAvailID >= OS_TASK_REG_TBL_SIZE) { /* See if we exceeded the number of IDs available */
*perr = OS_ERR_NO_MORE_ID_AVAIL; /* Yes, cannot allocate more task register IDs */
OS_EXIT_CRITICAL();
return ((INT8U)OS_TASK_REG_TBL_SIZE);
}
id = OSTaskRegNextAvailID; /* Assign the next available ID */
OSTaskRegNextAvailID++; /* Increment available ID for next request */
OS_EXIT_CRITICAL();
*perr = OS_ERR_NONE;
return (id);
}
#endif
/*
*********************************************************************************************************
* SET THE CURRENT VALUE OF A TASK VARIABLE(设置当前任务变量的值)
*
* Description: This function is called to change the current value of a task register. Task registers
* are application specific and can be used to store task specific values such as 'error
* numbers' (i.e. errno), statistics, etc. Each task register can hold a 32-bit value.
*这个函数被用来改变当前任务寄存器的值。任务寄存器是针对于特定任务的,并且可以存储特定的值,比如:错误状态码,统计信息等,每个任务寄存器可以存储32位的值
* Arguments : prio is the priority of the task you want to set the task register for. If you
* specify OS_PRIO_SELF then the task register of the current task will be obtained.
*是你想要设置的任务寄存器的任务的优先级。如果你指定了OS_PRIO_SELF,则会获得当前任务的任务寄存器的值
* id is the 'id' of the desired task register. Note that the 'id' must be less
* than OS_TASK_REG_TBL_SIZE
*你期待的任务寄存器的值。注意,传过来的id必须小于OS_TASK_REG_TBL_SIZE
* value is the desired value for the task register.
*你想要设置的值
* perr is a pointer to a variable that will hold an error code related to this call.
*指向错误信息的变量的指针
* OS_ERR_NONE if the call was successful
* OS_ERR_PRIO_INVALID if you specified an invalid priority
* OS_ERR_ID_INVALID if the 'id' is not between 0 and OS_TASK_REG_TBL_SIZE-1
*
* Returns : The current value of the task's variable or 0 if an error is detected.
*返回当前任务变量的值,如果发生错误的话,将会返回0
* Note(s) : The maximum number of task variables is 254
任务变量的最大值为254
*********************************************************************************************************
*/
//任务变量指的就是任务寄存器里面所存储的值
//如果允许寄存器列表
#if OS_TASK_REG_TBL_SIZE > 0u
void OSTaskRegSet (INT8U prio,
INT8U id,
INT32U value,
INT8U *perr)
{
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
OS_TCB *ptcb;
#ifdef OS_SAFETY_CRITICAL
if (perr == (INT8U *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return;
}
#endif
//参数检查
#if OS_ARG_CHK_EN > 0u
if (prio >= OS_LOWEST_PRIO) {
if (prio != OS_PRIO_SELF) {
*perr = OS_ERR_PRIO_INVALID;
return;
}
}
if (id >= OS_TASK_REG_TBL_SIZE) {
*perr = OS_ERR_ID_INVALID;
return;
}
#endif
OS_ENTER_CRITICAL();
if (prio == OS_PRIO_SELF) { /* See if need to get register from current task */
ptcb = OSTCBCur;
} else {
ptcb = OSTCBPrioTbl[prio];
}
ptcb->OSTCBRegTbl[id] = value;
OS_EXIT_CRITICAL();
*perr = OS_ERR_NONE;
}
#endif
/*
*********************************************************************************************************
* CATCH ACCIDENTAL TASK RETURN (捕获意外地任务返回)
*
* Description: This function is called if a task accidentally returns without deleting itself. In other
* words, a task should either be an infinite loop or delete itself if it's done.
*当一个任务如果意外停止,又没有删除自己,这个函数将被调用。换句话说,就是一个任务要不然是无限循环,要不就是在结束的时候删除自己
* Arguments : none
*
* Returns : none
*
* Note(s) : This function is INTERNAL to uC/OS-II and your application should not call it.
这个函数是系统内部函数,你的应用程序不能调用它
*********************************************************************************************************
*/
void OS_TaskReturn (void)
{
OSTaskReturnHook(OSTCBCur); /* Call hook to let user decide on what to do */
//如果允许任务删除
#if OS_TASK_DEL_EN > 0u
//调用此函数的任务删除自身
(void)OSTaskDel(OS_PRIO_SELF); /* Delete task if it accidentally returns! */
#else
//如果不允许任务删除,那么就一直循环下去
for (;;) {
OSTimeDly(OS_TICKS_PER_SEC);
}
#endif
}
/*
*********************************************************************************************************
* CLEAR TASK STACK(清空堆栈)
*
* Description: This function is used to clear the stack of a task (i.e. write all zeros)
*(这个函数是用来清空任务堆栈的)
* Arguments : 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.
* //是指向任务堆栈栈底的指针
* size is the number of 'stack elements' to clear.
* //任务堆栈需要清理的单元数量
* 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 : none
*********************************************************************************************************
*/
//只有在允许了堆栈检查,并且是允许创建带有扩展块的TCB
#if (OS_TASK_STAT_STK_CHK_EN > 0u) && (OS_TASK_CREATE_EXT_EN > 0u)
void OS_TaskStkClr (OS_STK *pbos,//堆栈栈底
INT32U size,//清空的单元数量
INT16U opt)
{
if ((opt & OS_TASK_OPT_STK_CHK) != 0x0000u) { /* See if stack checking has been enabled */
//查看是否允许堆栈检查
if ((opt & OS_TASK_OPT_STK_CLR) != 0x0000u) { /* See if stack needs to be cleared */
//查看是否允许堆栈清空操作
//分两种情况进行编译
#if OS_STK_GROWTH == 1u
while (size > 0u) { /* Stack grows from HIGH to LOW memory */
size--;
*pbos++ = (OS_STK)0; /* Clear from bottom of stack and up! */
}
#else
while (size > 0u) { /* Stack grows from LOW to HIGH memory */
size--;
*pbos-- = (OS_STK)0; /* Clear from bottom of stack and down */
}
#endif
}
}
}
#endif