📄 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
*********************************************************************************************************
*/
#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.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
改变任务优先级
描述:此函数允许你动态改变一个任务的优先级。但新的优先级必须可行
参数:oldp :旧的优先级
newp:新的优先级
返回:OS_NO_ERR:改变成功
OS_PRIO_INVALID:指定的优先级不合法:超过最大值
OS_PRIO_EXIST:新优先级已经存在
OS_PRIO_ERR:旧优先级任务不存在
*********************************************************************************************************
*/
#if OS_TASK_CHANGE_PRIO_EN > 0 //如果OS_TASK_CHANGE_PRIO_EN设置为1,能使包含下面代码
INT8U OSTaskChangePrio (INT8U oldprio, INT8U newprio)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
//为CPU状态寄存器分配存储器
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 定义为:能使队列代码产生&&申请队列控制块最大数不为零||能使邮箱代码产生||
//能使信号量代码产生||能使互斥量代码产生
OS_EVENT *pevent;
#endif
OS_TCB *ptcb;
INT8U x;
INT8U y;
INT8U bitx;
INT8U bity;
#if OS_ARG_CHK_EN > 0//允许参数检测
if ((oldprio >= OS_LOWEST_PRIO && oldprio != OS_PRIO_SELF) ||
newprio >= OS_LOWEST_PRIO) {//旧新优先级都不合法
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);//新优先级必须不存在,存在就重复了
} else {
OSTCBPrioTbl[newprio] = (OS_TCB *)1; /* Reserve the entry to prevent others */
//保留入口,防止其它任务占用此优先级
OS_EXIT_CRITICAL();
y = newprio >> 3; /* Precompute to reduce INT. latency */
//此函数会预先计算新优先级任务的任务控制块中的某些值,使用这些值
//可以将任务放入就绪步或者从该表中移除任务.
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 */
}//是的,得到优先级
ptcb = OSTCBPrioTbl[oldprio];//得到该优先级TCB指针
if (ptcb != (OS_TCB *)0) { /* Task to change must exist */
//优先级存在,如果要改变的是当前任务,由一定会成功
OSTCBPrioTbl[oldprio] = (OS_TCB *)0; /* Remove TCB from old priority */
//通过放入空闲指针,将指向当前任务的TCB指针从优先级列表中删除,
//使当前旧的优先级空闲,可以被其它任务占用.
if ((OSRdyTbl[ptcb->OSTCBY] & ptcb->OSTCBBitX) != 0x00) { /* If task is ready make it not */
if ((OSRdyTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0x00) { //如果要改变优先级的任务就绪
OSRdyGrp &= ~ptcb->OSTCBBitY;//不能让它就绪,
}
OSRdyGrp |= bity; /* Make new priority ready to run */
OSRdyTbl[y] |= bitx;//从就绪表中移除,然后在新优先级下,将任务插入就绪表,
//注意:OSTaskChangePrio是利用预先计算的值(见前面)将任务插入就绪表中的.
#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))
//能使队列代码产生&&申请队列控制块最大数不为零||能使邮箱代码产生||
//能使信号量代码产生||能使互斥量代码产生
} else {
pevent = ptcb->OSTCBEventPtr;
if (pevent != (OS_EVENT *)0) { /* Remove from event wait list */
//如果任务没有就绪,那么可能在等一个信号量,一个互斥型信号量,一个邮箱,队列
//等,如果OSTCBEventPtr非空,那么此函数会知道任务正在等以上的某件事.
if ((pevent->OSEventTbl[ptcb->OSTCBY] &= ~ptcb->OSTCBBitX) == 0) {
pevent->OSEventGrp &= ~ptcb->OSTCBBitY;
}
pevent->OSEventGrp |= bity; /* Add new priority to wait list */
//如果任务正在等某事件发生,OSTCBEventPtr必须将任务从事件控制块的等待队列(旧
//的优先级下)中移除,并在新的优先级下将事件插入到等待队列中.任务也可能正
//在等待延时时间到,或被挂起,上面几行可以省略
pevent->OSEventTbl[y] |= bitx;
}
#endif
}
OSTCBPrioTbl[newprio] = ptcb; /* Place pointer to TCB @ new priority */
//将指向任务的OS-TCB的指针存到OSTCBPrioTbl[]中.
ptcb->OSTCBPrio = newprio; /* Set new task priority */
//新的优先级保存在OSTCB中,预先值也保存在OSTCB中.
ptcb->OSTCBY = y;
ptcb->OSTCBX = x;
ptcb->OSTCBBitY = bity;
ptcb->OSTCBBitX = bitx;
OS_EXIT_CRITICAL();
OS_Sched(); /* Run highest priority task ready */
//任务调度,运行最高优先级任务,在新的优先级高于旧的优先有或者新的优先级高于调
//用此函数任务优先级的时候,此函数会被调用
return (OS_NO_ERR);
} else {
OSTCBPrioTbl[newprio] = (OS_TCB *)0; /* Release the reserved prio. */
//如果任务不存在,释放新优先级的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)
*********************************************************************************************************
*/
/*
*********************************************************************************************
建立一个任务
描述:这个函数用于ucosII处理完成一个任务,它要么在多任务处理之前建立,
要么运行任务建立,它不够由中断服务程序建立。
参数:task: 指向任务代码的指针。
pdata:是一个指向非强制性数据区域的指针,当任务优先运行时传递
参数给任务。任务有关部分假想它被唤醒,然后按照以下方式传递pdata:
* void Task (void *pdata)
* {
* for (;;) {
* Task code;
* }
* }
ptos:指向任务堆栈顶部的指针,如果配置常数 OS_STK_GROWTH 设置为1的话,堆栈则会由高到低增长(由高地址向低地址存储)。所以
“pstk”会指向堆栈存储器位置的最高地址;如果 OS_STK_GROWTH 设置为0的话,“pstk”将指向堆栈
最低存储器位置,堆栈将按存储器位置递增。
prio:是任务的优先级,一个独特的优先级必须指定给每个任务,最小的数对应最高优先级。
返回:OS_NO_ERR :如果函数成功。
OS_PRIO_EXIT :如果优先级已经存在。
OS_PRIO_INVALID:如果定义优先级的数大于最大值
*********************************************************************************************
*/
#if OS_TASK_CREATE_EN > 0 //能使包含任务创建代码
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状态寄存器分配存储空间
OS_CPU_SR cpu_sr; //CPU状态字是十六位 OS_CPU_SR为unsigned int
#endif
OS_STK *psp;
INT8U err;
#if OS_ARG_CHK_EN > 0
//如果OS_ARG_CHK_EN 设为1,OSTaskCreate会检查分配给任务的优先级是否有效。
//系统在执行初始化的时候,已经把最低优先级分配给了空闲任务。
//所以不能用最低优先级来创建任务。
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(); //退出临界状态
psp = (OS_STK *)OSTaskStkInit(task, pdata, ptos, 0); /* Initialize the task's stack */
//初始化任务堆栈,即建立任务堆栈
err = OS_TCBInit(prio, psp, (OS_STK *)0, 0, 0, (void *)0, 0);
//初始化任务控制块,从空闲的OS_TCB缓冲池
//中获得并初始化一个任务控制块
if (err == OS_NO_ERR) { //如果初始化没有错
OS_ENTER_CRITICAL();//进入临界状态
OSTaskCtr++; /* Increment the #tasks counter */
//任务数量加一
OS_EXIT_CRITICAL();//退出临界状态
if (OSRunning == TRUE) { /* Find highest priority task if multitasking has started */
//如果多任务开始,寻找最高优先级任务
OS_Sched();
}
} else { //如果初始化任务控制块有错
OS_ENTER_CRITICAL();//进入临界状态
OSTCBPrioTbl[prio] = (OS_TCB *)0;/* Make this priority available to others */
//把这一优先级给其它任务
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -