μCOS-II中OS_TASK.C源码中文注释版.doc_第1页
μCOS-II中OS_TASK.C源码中文注释版.doc_第2页
μCOS-II中OS_TASK.C源码中文注释版.doc_第3页
μCOS-II中OS_TASK.C源码中文注释版.doc_第4页
μCOS-II中OS_TASK.C源码中文注释版.doc_第5页
已阅读5页,还剩33页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

xilentz的网络文摘博客园 首页 新随笔 联系 订阅 管理 随笔 - 204 文章 - 0评论 - 10trackbacks - 0 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 (OSTCBPrioTblnewprio != (OS_TCB *)0) /* New priority must not already exist */ OS_EXIT_CRITICAL(); return (OS_PRIO_EXIST);/新优先级必须不存在,存在就重复了 else OSTCBPrioTblnewprio = (OS_TCB *)1; /* Reserve the entry to prevent others */ /保留入口,防止其它任务占用此优先级 OS_EXIT_CRITICAL(); y = newprio 3; /* Precompute to reduce INT. latency */ /此函数会预先计算新优先级任务的任务控制块中的某些值,使用这些值 /可以将任务放入就绪步或者从该表中移除任务. bity = OSMapTbly; x = newprio & 0x07; bitx = OSMapTblx; OS_ENTER_CRITICAL(); if (oldprio = OS_PRIO_SELF) /* See if changing self */ /如果改变自己 oldprio = OSTCBCur-OSTCBPrio; /* Yes, get priority */ /是的,得到优先级 ptcb = OSTCBPrioTbloldprio;/得到该优先级TCB指针 if (ptcb != (OS_TCB *)0) /* Task to change must exist */ /优先级存在,如果要改变的是当前任务,由一定会成功 OSTCBPrioTbloldprio = (OS_TCB *)0; /* Remove TCB from old priority */ /通过放入空闲指针,将指向当前任务的TCB指针从优先级列表中删除, /使当前旧的优先级空闲,可以被其它任务占用. if (OSRdyTblptcb-OSTCBY & ptcb-OSTCBBitX) != 0x00) /* If task is ready make it not */ if (OSRdyTblptcb-OSTCBY &= ptcb-OSTCBBitX) = 0x00) /如果要改变优先级的任务就绪 OSRdyGrp &= ptcb-OSTCBBitY;/不能让它就绪, OSRdyGrp |= bity; /* Make new priority ready to run */ OSRdyTbly |= 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-OSEventTblptcb-OSTCBY &= ptcb-OSTCBBitX) = 0) pevent-OSEventGrp &= ptcb-OSTCBBitY; pevent-OSEventGrp |= bity; /* Add new priority to wait list */ /如果任务正在等某事件发生,OSTCBEventPtr必须将任务从事件控制块的等待队列(旧 /的优先级下)中移除,并在新的优先级下将事件插入到等待队列中.任务也可能正 /在等待延时时间到,或被挂起,上面几行可以省略 pevent-OSEventTbly |= bitx; #endif OSTCBPrioTblnewprio = 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 OSTCBPrioTblnewprio = (OS_TCB *)0; /* Release the reserved prio. */ /如果任务不存在,释放新优先级的TCB OS_EXIT_CRITICAL(); return (OS_PRIO_ERR); /* Task to change didnt 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 tasks 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 tasks 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 tasks 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 (OSTCBPrioTblprio = (OS_TCB *)0) /* Make sure task doesnt already exist at this priority */ /保证优先级没有被其它任务占用 OSTCBPrioTblprio = (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 tasks 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();/进入临界状态 OSTCBPrioTblprio = (OS_TCB *)0;/* Make this priority available to others */ /把这一优先级给其它任务 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 tasks 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 tasks 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 tasks priority. A unique priority MUST be assigned to each task and the* lower the number, the higher the priority.* id is the tasks ID (0.65535)* pbos is a pointer to the tasks 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_CREAT

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论