μC/OS-II 信号量集

简介

在实际应用中,任务常常需要与多个事件同步,即要根据多个信号量组合作用的结果来决定任务的运行方式。μC/OS-II 为了实现多个信号量组合的功能定义了一种特殊的数据结构——信号量集。
信号量集所能管理的信号量都是一些二值信号,所有信号量集实质上是一种可以对多个输入的逻辑信号进行基本逻辑运算的组合逻辑,其示意图如下图所示:

1319058-20180407144506451-81231891.png

信号量集的标志组

不同于信号量、消息邮箱、消息队列等事件,μC/OS-II 不使用事件控制块来描述信号量集,而使用了一个叫做标志组的结构 OS_FLAG_GRP。
OS_FLAG_GRP 结构如下:

typedef struct{
    INT8U OSFlagType;        //识别是否为信号量集的标志
    void *OSFlagWaitList;    //指向等待任务链表的指针
    OS_FLAGS OSFlagFlags;    //所有信号列表
}OS_FLAG_GRP;

成员 OSFlagWaitList 是一个指针,当一个信号量集被创建后,这个指针指向了这个信号量集的等待任务链表。

1319058-20180407144600583-1108796336.png

1319058-20180407144617552-1790900.png

等待任务链表

与其他事件不同,信号量集用一个双向链表来组织等待任务,每一个等待任务都是该链表中的一个节点(Node)。标志组 OS_FLAG_GRP 的成员 OSFlagWaitList 就指向了信号量集的这个等待任务链表。
等待任务链表节点 OS_FLAG_NODE 的结构如下:

typedef struct {
    void *OSFlagNodeNext;        //指向下一个节点的指针
    void *OSFlagNodePrev;        //指向前一个节点的指针
    void *OSFlagNodeTCB;         //指向对应任务控制块的指针
    void *OSFlagNodeFlagGrp;     //反向指向信号量集的指针
    OS_FLAGS OSFlagNodeFlags;    //信号过滤器
    INT8U OSFlagNodeWaitType;    //定义逻辑运算关系的数据
} OS_FLAG_NODE;

1319058-20180407144711648-1969297478.png

1319058-20180407144719169-1607782381.png

1319058-20180407144733404-1219466862.png

添加任务结点

给等待任务链表添加节点的函数为 OS_FlagBlock( ),这个函数的原型为:

static  void  OS_FlagBlock (
OS_FLAG_GRP *pgrp,   //信号量集指针
OS_FLAG_NODE *pnode, //待添加的等待任务节点指针
OS_FLAGS flags,      //指定等待信号的数据
INT8U wait_type,     //信号与等待任务之间的逻辑
INT16U timeout       //等待时限
);      

这个函数将在请求信号量集函数 OSFlagPend ( ) 中被调用。

删除任务结点

从等待任务链表中删除一个节点的函数为 OS_FlagUnlink( ),这个函数的原型为:

void  OS_FlagUnlink (OS_FLAG_NODE *pnode);

这个函数将在发送信号量集函数 OSFlagPost( ) 中被调用。

创建信号量集

任务可以通过调用函数 OSFlagCreate ( ) 来创建一个信号量集。OSFlagCreate ( ) 的函数原型为:

OS_FLAG_GRP  *OSFlagCreate (
OS_FLAGS flags, //信号的初始值
INT8U *err      //错误信息
)

请求信号量集

任务可以通过调用函数 OSFlagPend( ) 请求一个信号量集,OSFlagPend( ) 函数的原型为:

OS_FLAGS  OSFlagPend (
OS_FLAG_GRP *pgrp,  //所请求的信号量集指针
OS_FLAGS flags,     //滤波器
INT8U wait_type,    //逻辑运算类型
INT16U timeout,     //等待时限
INT8U *err          //错误信息
);

向信号量集发送信号

任务可以通过调用函数 OSFlagPost ( ) 向信号量集发信号,OSFlagPost ( ) 函数的原型为:

OS_FLAGS  OSFlagPost (
OS_FLAG_GRP *pgrp, //信号量集指针
OS_FLAGS flags,    //选择所要发送的信号
INT8U opt,         //信号有效的选项
INT8U *err         //错误信息
);      

所谓任务向信号量集发信号,就是对信号量集标志组中的信号进行置“1”(置位)或置“0”(复位)的操作。至于对信号量集中的哪些信号进行操作,用函数中的参数flags来指定;对指定的信号是置“1”还是置“0”,用函数中的参数opt来指定(opt = OS_FLAG_SET 为置“1”操作;opt = OS_FLAG_CLR 为清“0”操作)。

参考自:《μC/OS-II 入门教程》

转载于:https://www.cnblogs.com/GyForever1004/p/8733297.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值