embOS实时操作系统 - 任务通讯

本文深入探讨了信号量和消息机制在操作系统中的应用,包括二进制信号量、计数信号量、邮箱、队列、事件等核心概念及其工作原理。

1. 轮询全局变量

2. 使用事件驱动

A. 信号量

(1) 二进制信号量(resource/binary semaphore)< 互斥锁 Mutex( Mutual exclusion ) >

信号量只有二进制的0或1

OS_Use() Claims a resource and blocks it for other tasks

OS_Unuse() Releases a semaphore currently in use by a task.

OS_Request()  Requests a specified semaphore, blocks it for other tasks if it is available. Continues execution in any case.

Claims a resource and blocks it for other tasks.

Requests a specified semaphore and blocks it for other tasks if it is available.

Continues execution in any case

 

 

(2) 计数信号量(counting/general semaphore)

信号量是一个任意的整数

OS_SignalCSema() Increments the counter of a semaphore

OS_WaitCSema() Decrements the counter of a semaphore.

OS_CSemaRequest() Decrements the counter of a semaphore, if available.

B. 消息

(1) 邮箱 - 1..127 字节的消息 ( 消息字节数 -- 固定的)

OS_PutMail() Stores a new message of a predefined size in a mailbox.

OS_WaitMail() Waits until a mail is available, but does not retrieve the message from the mailbox.

OS_GetMail() Retrieves a message of a predefined size from a mailbox.

OS_PeekMail() Reads a mail from a mailbox without removing it

(2) 队列 - 大于 127 字节的消息 ( 消息字节数 -- 可变的 )

OS_Q_Put() Stores a new message of given size in a queue.

OS_Q_PutBlocked() Stores a new message of given size in a queue. Blocks the calling task when queue is full.

OS_Q_GetPtr() Retrieves a message from a queue.

OS_Q_GetPtrCond() Retrieves a message from a queue, if one message is available or returns without suspension.

OS_Q_GetPtrTimed() Retrieves a message from a queue within a specified time, if one message is available.

OS_Q_Purge() Deletes the last retrieved message in a queue.

C. 事件

(1) 任务事件 - 每一个任务有 1 字节 事件掩码 ( 8 个事件 )

OS_SignalEvent() Signals event(s) to a specified task.

OS_WaitEvent() Waits for one of the events specified in the bitmask and clears the event memory after an event occurs.

OS_WaitSingleEvent() Waits for one of the events specified as bitmask and clears only that event after it occurs.

OS_WaitEvent_Timed() Waits for the specified events for a given time, and clears the event memory after an event occurs.

OS_WaitSingleEventTimed() Waits for the specified events for a given time; after an event occurs, only that event is cleared.

(2) 事件对象 - the signaling function does not need to know which task is waiting for the event to occur

OS_EVENT_Wait() Waits for an event. 
OS_EVENT_WaitTimed() Waits for an event with timeout and resets the event after it occurs. 
OS_EVENT_Set() Sets the events, or resumes waiting tasks. 
OS_EVENT_Reset() Resets the event to none-signaled state.
OS_EVENT_Pulse() Sets the event, resumes waiting tasks, if any,and then resets the event.
OS_EVENT_Get() Returns the state of an event object.

 

由于RTOS需占用一定的系统资源(尤其是RAM资源),只有μC/OS-II、embOS、salvo、FreeRTOS等少数实时操作系统能在小RAM单片机上运行。相对于C/OS-II、 embOS等商业操作系统,FreeRTOS操作系统是完全免费的操作系统,具有源码公开、可移植、可裁减、调度策略灵活的特点,可以方便地移植到各种单片机上运行,其最新版本为6.0版。 作为一个轻量级的操作系统,FreeRTOS提供的功能包括:任务管理、时间管理、信号量、消息队列、内存管理、记录功能等,可基本满足较小系统的需要。 FreeRTOS内核支持优先级调度算法,每个任务可根据重要程度的不同被赋予一定的优先级,CPU总是让处于就绪态的、优先级最高的任务先运行。 FreeRT0S内核同时支持轮换调度算法,系统允许不同的任务使用相同的优先级,在没有更高优先级任务就绪的情况下,同一优先级的任务共享CPU的使用 时间。 FreeRTOS的内核可根据用户需要设置为可剥夺型内核或不可剥夺型内核。当 FreeRTOS被设置为可剥夺型内核时,处于就绪态的高优先级任务能剥夺低优先级任务的CPU使用权,这样可保证系统满足实时性的要求;当 FreeRTOS被设置为不可剥夺型内核时,处于就绪态的高优先级任务只有等当前运行任务主动释放CPU的使用权后才能获得运行,这样可提高CPU的运行 效率。 FreeRTOS的移植:FreeRTOS操作系统可以被方便地移植到不同处理器上工作,现已提供了ARM、MSP430、 AVR、PIC、C8051F等多款处理器的移植。FrceRTOS在不同处理器上的移植类似于μC/0S一II,故本文不再详述FreeRTOS的移 植。此外,TCP/IP协议栈μIP已被移植到FreeRTOS上,具体代码可见FreeRTOS网站 相对于常见的μC/OS—II操作系统,FreeRTOS操作系统既有优点也存在不足。其不足之处, 一方面体现在系统的服务功能上,如FreeRTOS只提供了消息队列和信号量的实现,无法以后进先出的顺序向消息队列发送消息;另一方 面,FreeRTOS只是一个操作系统内核,需外扩第三方的GUI(图形用户界面)、TCP/IP协议栈、FS(文件系统)等才能实现一个较复杂的系统, 不像μC/OS-II可以和μC/GUI、μC/FS、μC/TCP-IP等无缝结合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值