μC/OS-II--任务

本文介绍了μC/OS-II操作系统中的任务管理,包括任务的5种状态:睡眠态、就绪态、运行态、等待状态和中断服务态。详细讲解了任务控制块,它是保存任务参数和状态的数据结构。此外,还提到了任务创建与删除的函数OSTaskCreate和OSTaskCreateExt,以及任务删除函数OSTaskDel。最后,讨论了系统中的空闲任务和统计任务,它们分别在没有其他任务就绪时运行和用于计算CPU利用率。
摘要由CSDN通过智能技术生成

μC/OS-II最小资源分配单元是任务。

任务状态

μC/OS-II中任务有5种状态,分别为睡眠态,就绪态,运行态,就绪态,等待状态,中断服务态。睡眠态指任务还没有创建或者创建后被删除。就绪态是指任务准备运行,等待CPU使用权。运行态指获得CPU使用权,任务执行。等待状态是指正在运行的任务调用等待或延时函数时进入的状态。中断服务态是指运行态被打断,进入中断服务程序。

任务控制块

任务控制块是一个数据结构,用于保存任务的参数和状态。

typedef struct os_tcb {
    //OS_STK定义为32位无符号数据,该行定义当前任务所分配的堆栈的栈顶指针
    //(该栈顶指针是指任务切换后自动保存的r0-r15等一系列数据后的堆栈指针)
    //对于堆栈,ucos可以对每一个任务分配任意大小的堆栈
    OS_STK          *OSTCBStkPtr;      /* Pointer to current top of stack                              */

#if OS_TASK_CREATE_EXT_EN > 0//如果使能了扩展任务控制块,则定义以下数据

    //扩展任务块的指针,扩展任务快的引入,使得可以在不改变ucos源码的情况下,
    //加入扩展功能,此外如果需要使用该功能,则需在OS_CFG.H(ucos配置文件)
    //中将OS_TASK_CREATE_EXT_EN 置1,允许建立任务函数扩展
    void            *OSTCBExtPtr;      /* Pointer to user definable data for TCB extension             */

    //指向任务堆栈的栈底(就是数据最后进入的地址),如果堆栈增长方式是递增的,那么他执向堆栈的最高地址
    //反之指向最低地址,该参数在使用OSTaskStkChk函数时需要调用
    OS_STK          *OSTCBStkBottom;   /* Pointer to bottom of stack  */
    
    //该参数是任务堆栈的大小,对于堆栈大小是指栈中所能容纳的指针数目,而不是字节数目,假设堆栈容量为1000,
    //如果地址宽度为32位,那么堆栈包含4000个字节。但是其容量是1000
    INT32U           OSTCBStkSize;     /* Size of task stack (in number of stack elements)             */

    //选择项,支持3种选择e:
    //OS_TASK_OPT_STK_CHK,该参数用于告知TaskCreateExt函数在建立任务时对堆栈进行检查(不定义这个ucos不会进行检查)
    //OS_TASK_OPT_STK_CLR,在任务建立 时将任务栈清零,至于在需要使用栈检验功能时才将栈清零
    //OS_TASK_OPT_SAVE_FP,通知任务需要做浮点运算
    INT16U           OSTCBOpt;         /* Task options as passed by OSTaskCreateExt()                  */

    //用于存储任务的识别码,现在还没使用,感觉他会发展成为linux的pid
    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_EVENT_EN	|| (OS_FLAG_EN > 0)
    //如果使能了事件,定义指向事件控制块的指针。
    OS_EVENT        *OSTCBEventPtr;    /* Pointer to event control block                               */
#endif
   
#if ((OS_Q_EN > 0) && (OS_MAX_QS > 0)) || (OS_MBOX_EN > 0)
    //如果定义了队列,消息邮箱,信号量系统资源,则使用该指针指向所要传递的消息
    void            *OSTCBMsg;         /* Message received from OSMboxPost() or OSQPost()              */
#endif

#if (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)
#if OS_TASK_DEL_EN > 0
    //指向事件标志节点的指针
    OS_FLAG_NODE    *OSTCBFlagNode;    /* Pointer to event flag node                                   */
#endif
    //准备运行的事件标志任务
    OS_FLAGS         OSTCBFlagsRdy;    /* Event flags that made task ready to run                      */
#endif

    //任务延时时间,或者等待事件发生的超时时间,在时钟每发生一次中断的时候,时钟街拍函数OSTimeTick
    //将通过任务控制块的链表访问该变量,并将其减1,刷新变量
    INT16U           OSTCBDl
This book describes the design and implementation of mC/OS-II (pronounced "Micro C O S 2") which stands for<br>Micro-Controller Operating System Version 2. μC/OS -II is based on μC/OS, The Real-Time Kernel which was first<br>published in 1992. Thousands of people around the world are using μC/OS in all kinds of applications such as<br>cameras, medical instruments, musical instruments, engine controls, network adapters, highway telephone call boxes,<br>ATM machines, industrial robots, and many more. Nu merous colleges and Universities have also used μC/OS to<br>teach students about real-time systems.<br>μC/OS-II is upward compatible with μC/OS (V1.11) but provides many improvements over μC/OS such as the<br>addition of a fixed-sized memory manager, user definable callouts on task creation, task deletion, task switch and<br>system tick, supports TCB extensions, stack checking and, much more. I also added comments to just about every<br>function and I made μC/OS -II much easier to port to different processors. The source code in μC/OS was found in two<br>source files. Because μC/OS-II contains many new features and functions, I decided to split μC/OS-II in a few source<br>files to make the code easier to maintain.<br>If you currently have an application (i.e. product) that runs with μC/OS, your application should be able to run,<br>virtually unchanged, with μC/OS-II. All of the services (i.e. function calls) provided by μC/OS have been preserved.<br>You may, however, have to change include files and product build files to ‘point’ to the new file names.<br>This book contains ALL the source code for μC/OS-II and a port for the Intel 80x86 processor running in Real-Mode<br>and for the Large Model. The code was developed on a PC running the Microsoft Windows 95 operating system.<br>Examples run in a DOS compatible box under the Windows 95 environment. Development was done using the<br>Borland International C/C++ compiler version 3.1. Although μC/OS-II was developed and tested on a PC, mC/OS-II<br>was actually targeted for embedded systems and can easily be ported to many different processor architectures.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值