6-FreeRTOS流缓冲区

简介: 6-FreeRTOS流缓冲区

有关FreeRTOS中的函数后面都会讲到,前面章节部分主要是说一下原理,看的时候比较乏味,了解即可,没必要较真


1-简介

FreeRTOS流和缓冲区是任务和任务之间、中断和任务之间的通信原语。和其他多数FreeRTOS通信原语不同,他们针对读和写的单一性进行了方案的优化工作。例如将数据从中断服务例程传递到任务,或在双核cpu上从一个微控制器内核传递到另一个微控制器内核。数据通过拷贝传递——发送端将数据复制到缓冲区,读取端将数据复制出缓冲区。


流缓冲区传递连续的字节流。消息缓冲区传递大小可变但离散的消息。消息缓冲区使用流缓冲区进行数据传输。

在FreeRTOS对象是惟一的,流缓冲区的实现(也是消息缓冲区的实现,因为消息缓冲区是建立在流缓冲区之上的)假设只有一个任务或中断将写入缓冲区(写入器),并且仅有一个任务或中断将从缓冲区读取(读取器)。对不同的任务或者中断写入或者读取是安全的,但是有多个不同的写入或者读取是不安全的。若有多个不同的写入器,那么程序写入必须每次对写入的API函数(例如xStreamBufferSend())的调用都放在临界区中,并将发送阻塞时间设置为0。同样,如果有多个不同的读取器,那么应用程序编写器必须将每次对读取API函数(如xStreamBufferReceive())的调用都放在临界区中,并使用0的接收块时间。


2-中断服务到任务流


2.1 介绍


流缓冲区允许一个字节流从中断服务程序传递到一个任务,或者从一个任务传递到另一个任务。字节流可以是任意长度的,而且不一定有开头或结尾。一次可以写入任意数量的字节,一次可以读取任意数量的字节。数据通过复制传递——发送端将数据复制到缓冲区,读取端将数据复制出缓冲区。

与大多数其他FreeRTOS通信原语不同,流缓冲区是针对单读单写场景进行优化的,例如将数据从中断服务程序传递到任务,或在双核CPU上从一个微控制器内核传递到另一个微控制器内核。

在FreeRTOS/source/stream_buffer.c源文件来启用流缓冲区功能。可自行查阅。

流缓冲区的实现使用了任务的通知。因此,调用将调用任务置于阻塞状态的流缓冲区API函数可以更改调用任务的通知状态和值。


2.2 快速入门

在reeRTOS/Demo/Common/Minimal/StreamBufferInterrupt.c源文件 提供了一个示例,说明从中断服务程序到任务使用流缓冲区传递数据的例程。在这里我给大家复制过来了。

1/*
  2* FreeRTOS V202112.00
  3* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4*
  5* Permission is hereby granted, free of charge, to any person obtaining a copy of
  6* this software and associated documentation files (the "Software"), to deal in
  7* the Software without restriction, including without limitation the rights to
  8* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9* the Software, and to permit persons to whom the Software is furnished to do so,
 10* subject to the following conditions:
 11*
 12* The above copyright notice and this permission notice shall be included in all
 13* copies or substantial portions of the Software.
 14*
 15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 17* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 18* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 19* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 21*
 22* http://www.FreeRTOS.org
 23* http://aws.amazon.com/freertos
 24*
 25* 1 tab == 4 spaces!
 26*/
 27
 28/*
 29* A simple example that shows a stream buffer being used to pass data from an
 30* interrupt to a task.
 31*
 32* There are two strings, pcStringToSend and pcStringToReceive, where
 33* pcStringToReceive is a substring of pcStringToSend. The interrupt sends
 34* a few bytes of pcStringToSend to a stream buffer ever few times that it
 35* executes. A task reads the bytes from the stream buffer, looking for the
 36* substring, and flagging an error if the received data is invalid.
 37*/
 38
 39/* Standard includes. */
 40#include "stdio.h"
 41#include "string.h"
 42
 43/* FreeRTOS includes. */
 44#include "FreeRTOS.h"
 45#include "task.h"
 46#include "stream_buffer.h"
 47
 48/* Demo app includes. */
 49#include "StreamBufferInterrupt.h"
 50
 51#define sbiSTREAM_BUFFER_LENGTH_BYTES ( ( size_t ) 100 )
 52#define sbiSTREAM_BUFFER_TRIGGER_LEVEL_10 ( ( BaseType_t ) 10 )
 53
 54/*-----------------------------------------------------------*/
 55
 56/* Implements the task that receives a stream of bytes from the interrupt. */
 57static void prvReceivingTask( void * pvParameters );
 58
 59/*-----------------------------------------------------------*/
 60
 61/* The stream buffer that is used to send data from an interrupt to the task. */
 62static StreamBufferHandle_t xStreamBuffer = NULL;
 63
 64/* The string that is sent from the interrupt to the task four bytes at a
 65* time. Must be multiple of 4 bytes long as the ISR sends 4 bytes at a time*/
 66static const char * pcStringToSend = "_____Hello FreeRTOS_____";
 67
 68/* The string to task is looking for, which must be a substring of
 69* pcStringToSend. */
 70static const char * pcStringToReceive = "Hello FreeRTOS";
 71
 72/* Set to pdFAIL if anything unexpected happens. */
 73static BaseType_t xDemoStatus = pdPASS;
 74
 75/* Incremented each time pcStringToReceive is correctly received, provided no
 76* errors have occurred. Used so the check task can check this task is still
 77* running as expected. */
 78static uint32_t ulCycleCount = 0;
 79
 80/*-----------------------------------------------------------*/
 81
 82void vStartStreamBufferInterruptDemo( void )
 83{
 84/* Create the stream buffer that sends data from the interrupt to the
 85* task, and create the task. */
 86xStreamBuffer = xStreamBufferCreate( /* The buffer length in bytes. */
 87sbiSTREAM_BUFFER_LENGTH_BYTES,
 88/* The stream buffer's trigger level. */
 89sbiSTREAM_BUFFER_TRIGGER_LEVEL_10 );
 90
 91xTaskCreate( prvReceivingTask, /* The function that implements the task. */
 92"StrIntRx", /* Human readable name for the task. */
 93configMINIMAL_STACK_SIZE, /* Stack size (in words!). */
 94NULL, /* Task parameter is not used. */
 95tskIDLE_PRIORITY + 2, /* The priority at which the task is created. */
 96NULL ); /* No use for the task handle. */
 97}
 98/*-----------------------------------------------------------*/
 99
100static void prvReceivingTask( void * pvParameters )
101{
102char cRxBuffer[ 20 ];
103BaseType_t xNextByte = 0;
104
105/* Remove warning about unused parameters. */
106( void ) pvParameters;
107
108/* Make sure the string will fit in the Rx buffer, including the NULL
109* terminator. */
110configASSERT( sizeof( cRxBuffer ) > strlen( pcStringToReceive ) );
111
112/* Make sure the stream buffer has been created. */
113configASSERT( xStreamBuffer != NULL );
114
115/* Start with the Rx buffer in a known state. */
116memset( cRxBuffer, 0x00, sizeof( cRxBuffer ) );
117
118for( ; ; )
119{
120/* Keep receiving characters until the end of the string is received.
121* Note: An infinite block time is used to simplify the example. Infinite
122* block times are not recommended in production code as they do not allow
123* for error recovery. */
124xStreamBufferReceive( /* The stream buffer data is being received from. */
125xStreamBuffer,
126/* Where to place received data. */
127( void * ) &( cRxBuffer[ xNextByte ] ),
128/* The number of bytes to receive. */
129sizeof( char ),
130
131/* The time to wait for the next data if the buffer
132* is empty. */
133portMAX_DELAY );
134
135/* If xNextByte is 0 then this task is looking for the start of the
136* string, which is 'H'. */
137if( xNextByte == 0 )
138{
139if( cRxBuffer[ xNextByte ] == 'H' )
140{
141/* The start of the string has been found. Now receive
142* characters until the end of the string is found. */
143xNextByte++;
144}
145}
146else
147{
148/* Receiving characters while looking for the end of the string,
149* which is an 'S'. */
150if( cRxBuffer[ xNextByte ] == 'S' )
151{
152/* The string has now been received. Check its validity. */
153if( strcmp( cRxBuffer, pcStringToReceive ) != 0 )
154{
155xDemoStatus = pdFAIL;
156}
157
158/* Return to start looking for the beginning of the string
159* again. */
160memset( cRxBuffer, 0x00, sizeof( cRxBuffer ) );
161xNextByte = 0;
162
163/* Increment the cycle count as an indication to the check task
164* that this demo is still running. */
165if( xDemoStatus == pdPASS )
166{
167ulCycleCount++;
168}
169}
170else
171{
172/* Receive the next character the next time around, while
173* continuing to look for the end of the string. */
174xNextByte++;
175
176configASSERT( ( size_t ) xNextByte < sizeof( cRxBuffer ) );
177}
178}
179}
180}
181/*-----------------------------------------------------------*/
182
183void vBasicStreamBufferSendFromISR( void )
184{
185static size_t xNextByteToSend = 0;
186const BaseType_t xCallsBetweenSends = 100, xBytesToSend = 4;
187static BaseType_t xCallCount = 0;
188
189/* Is it time to write to the stream buffer again? */
190xCallCount++;
191
192if( xCallCount > xCallsBetweenSends )
193{
194xCallCount = 0;
195
196/* Send the next four bytes to the stream buffer. */
197xStreamBufferSendFromISR( xStreamBuffer,
198( const void * ) ( pcStringToSend + xNextByteToSend ),
199xBytesToSend,
200NULL );
201
202/* Send the next four bytes the next time around, wrapping to the start
203* of the string if necessary. */
204xNextByteToSend += xBytesToSend;
205
206if( xNextByteToSend >= strlen( pcStringToSend ) )
207{
208xNextByteToSend = 0;
209}
210}
211}
212/*-----------------------------------------------------------*/
213
214BaseType_t xIsInterruptStreamBufferDemoStillRunning( void )
215{
216uint32_t ulLastCycleCount = 0;
217
218/* Check the demo is still running. */
219if( ulLastCycleCount == ulCycleCount )
220{
221xDemoStatus = pdFAIL;
222}
223else
224{
225ulLastCycleCount = ulCycleCount;
226}
227
228return xDemoStatus;
229}

2.2 阻塞读取和触发电平

xStreamBufferReceive()用于读取来自 RTOS 任务的流缓冲区的数据。xStreamBufferReceiveFromISR())是 用于从中断服务例程 (ISR) 从流缓冲区中读取数据。

下面是这两个函数的用法举例:

1//1-xStreamBufferReceive()
 2void vAFunction( StreamBuffer_t xStreamBuffer )
 3{
 4uint8_t ucRxData[ 20 ];
 5size_t xReceivedBytes;
 6const TickType_t xBlockTime = pdMS_TO_TICKS( 20 );
 7
 8/* Receive up to another sizeof( ucRxData ) bytes from the stream buffer.
 9Wait in the Blocked state (so not using any CPU processing time) for a
10maximum of 100ms for the full sizeof( ucRxData ) number of bytes to be
11available. */
12xReceivedBytes = xStreamBufferReceive( xStreamBuffer,
13( void * ) ucRxData,
14sizeof( ucRxData ),
15xBlockTime );
16
17if( xReceivedBytes > 0 )
18{
19/* A ucRxData contains another xRecievedBytes bytes of data, which can
20be processed here.... */
21}
22}
23
242--xStreamBufferReceiveFromISR()
25
26/* A stream buffer that has already been created. */
27StreamBuffer_t xStreamBuffer;
28
29void vAnInterruptServiceRoutine( void )
30{
31uint8_t ucRxData[ 20 ];
32size_t xReceivedBytes;
33BaseType_t xHigherPriorityTaskWoken = pdFALSE; /* Initialised to pdFALSE. */
34
35/* Receive the next stream from the stream buffer. */
36xReceivedBytes = xStreamBufferReceiveFromISR( xStreamBuffer,
37( void * ) ucRxData,
38sizeof( ucRxData ),
39&xHigherPriorityTaskWoken );
40
41if( xReceivedBytes > 0 )
42{
43/* ucRxData contains xReceivedBytes read from the stream buffer.
44Process the stream here.... */
45}
46
47/* If xHigherPriorityTaskWoken was set to pdTRUE inside
48xStreamBufferReceiveFromISR() then a task that has a priority above the
49priority of the currently executing task was unblocked and a context
50switch should be performed to ensure the ISR returns to the unblocked
51task. In most FreeRTOS ports this is done by simply passing
52xHigherPriorityTaskWoken into taskYIELD_FROM_ISR(), which will test the
53variables value, and perform the context switch if necessary. Check the
54documentation for the port in use for port specific instructions. */
55taskYIELD_FROM_ISR( xHigherPriorityTaskWoken );
56}

xStreamBufferReceive()允许指定阻塞时间如果任务使用xStreamBufferReceive()从一个空的流缓冲区中读取数据时指定了一个非零的阻塞时间,那么任务将被放置到阻塞状态(因此它不会消耗任何CPU时间,其他任务可以运行),直到流缓冲区中有指定数量的数据可用,或者块时间到期。在等待数据的任务从阻塞状态移除之前,流缓冲区中必须存在的数据量称为流缓冲区的触发级别。例如:

如果一个任务在读取触发级别为1的空流缓冲区时被阻塞,那么当向缓冲区写入一个字节或任务的阻塞时间到期时,该任务将被解除阻塞。

如果一个任务在读取触发级别为10的空流缓冲区时被阻塞,那么该任务将不会被解除阻塞,直到流缓冲区包含至少10个字节或任务的阻塞时间到期。

如果读取任务的阻塞时间在触发级别到达之前超时,那么无论实际可用的字节数是多少,该任务仍然会接收到。


2.3 阻塞写入

xStreamBufferSend())用于从RTOS任务向流缓冲区发送数据。xStreamBufferSendFromISR())用于从中断服务例程(ISR)向流缓冲区发送数据。

如果任务使用xStreamBufferSend()向流缓冲区写入数据时指定了一个非零的阻塞时间,那么该任务将被放置到阻塞状态(因此它不会消耗任何CPU时间,其他任务可以运行),直到流缓冲区中有空间可用,或者阻塞时间到达。


2.4 发送和接收的回调

流和消息缓冲区在每次发送和接收操作完成后都会执行一个回调:

使用xStreamBufferCreate()和xMessageBufferCreate() API函数(以及它们静态分配的等价函数)创建的流和消息缓冲区共享相同的回调函数,这些回调函数是使用sbSEND_COMPLETED()和sbRECEIVE_COMPLETED()宏定义的。

使用xStreamBufferCreateWithCallback()和xMessageBufferCreateWithCallback() API函数(以及它们静态分配的等价函数)创建的流缓冲区和消息缓冲区都可以有自己唯一的回调函数。

sbSEND_COMPLETED() (and sbSEND_COMPLETED_FROM_ISR())

sbSEND_COMPLETED()是一个宏,当数据写入使用xStreamBufferCreate()或xStreamBufferCreateStatic() API创建的流缓冲区时,会调用该宏(在FreeRTOS API函数内部)。它接受一个参数,即更新后的流缓冲区句柄。

默认情况下(如果应用程序编写者没有提供自己的宏实现),sbSEND_COMPLETED()会检查是否有任务阻塞在流缓冲区上等待数据,如果有,则从阻塞状态中删除该任务。

应用程序编写人员可以通过在FreeRTOSConfig.h中提供他们自己的sbSEND_COMPLETED()实现来改变这种默认行为。当使用流缓冲区在多核处理器的核之间传递数据时,这很有用。在这种情况下,可以实现sbSEND_COMPLETED()在另一个CPU核心中生成一个中断,然后中断的服务例程可以使用xStreamBufferSendCompletedFromISR() API函数来检查(如果必要的话)等待数据的任务。

在FreeRTOS/Demo/Common/Minimal/MessageBufferAMP.c文件中有更详细的应用过程。有时间到时候会单独的把FreeRTOS所有函数给介绍一下以及应用。

sbRECEIVE_COMPLETED() (and sbRECEIVE_COMPLETED_FROM_ISR())

sbRECEIVE_COMPLETED()是与sbSEND_COMPLETED()等价的接收方。当从流缓冲区读取数据时,它会被调用(在FreeRTOS API函数内部)。默认情况下(如果应用程序编写人员没有提供自己的宏实现),宏会检查流缓冲区上是否有任务阻塞,以等待缓冲区内的空间可用,如果有,则将该任务从阻塞状态中移除。

与bSEND_COMPLETED()一样,应用程序编写人员可以通过在FreeRTOSConfig.h(后续会单独讲述这个文件)中提供替代实现来更改sbRECEIVE_COMPLETED()的默认行为。如果你需要每个流缓冲区都有自己的“接收完成”行为,可以使用xStreamBufferCreateWithCallback()或xStreamBufferCreateStaticWithCallback() API函数来创建流缓冲区。


3-内核到内核


3.1 介绍


消息缓冲区允许不同字节的离散消息从中断服务程序传递到一个进程,或从一个进程传递到另一个进程。例如,长度为10、20和123字节的消息都可以写入和读取到同一个消息缓冲区。与使用流缓冲区不同的是,10字节的消息只能作为10字节消息读取,而不能作为单个字节读取。消息缓冲区建立在流缓冲区之上(也就是说,它们使用流缓冲区的实现)。

数据通过复制的方式通过消息缓冲区——发送方将数据复制到缓冲区中,读取方将数据复制出缓冲区。

在文件目录FreeRTOS/Demo/Common/Minimal/MessageBufferAMP.c下提供了一个从一个MCU到另一个MCU的数据,可以看看。


3.2消息缓冲区大小


为了能让消息缓冲区能够处理不同字节的消息,每个消息的字节大小在消息之前写入消息缓冲区(这在FreeRTOS API函数内部发生)。字节长度保存在一个变量中,其类型由FreeRTOSConfig.h中的configMESSAGE_BUFFER_LENGTH_TYPE常数设置。

如果没有额外的定义,configMESSAGE_BUFFER_LENGTH_TYPE默认为size_t类型,,size_t在32位体系结构上通常是4字节。所以呢,当configMESSAGE_BUFFER_LENGTH_TYPE为4字节时,向缓冲区写入一个10字节的消息,但是实际上占用的字节是超过10个字节的,实际上是占用了14个字节。同样,把100个字节的消息写入到缓冲区,实际上占用104个字节空间。


3.3 阻塞的读取和写入

xMessageBufferReceive())是用来读取任务消息缓冲区的数据的,xMessageBufferReceiveFromISR())是 用于从中断服务程序 (ISR) 的消息缓冲区读取数据。
用于从RTOS任务的消息缓冲区中读取数据。用于从中断服务例程(ISR)的消息缓冲区中读取数据xMessageBufferSend()用于发送 数据从 RTOS 任务发送到消息缓冲区。xMessageBufferSendFromISR()是用于将数据从中断服务程序 (ISR) 发送到消息缓冲区。
如果在任务中使用xMessageBufferReceive()说明了阻塞时间,从消息缓冲区,而这个缓冲区刚好是空的,则这个函数置于阻塞状态(因此它不会消耗任何CPU时间,其他任务可以运行),直到数据在消息缓冲区中可用,或阻塞时间到达。)
如果在任务中使用xMessageBufferSend()函数写入到消息缓冲区,这个缓冲区已经满了,则任务将会被置位到阻塞状态(因此它不消耗任何 CPU 时间,并且可以运行其他任务) 直到消息缓冲区中有空间可用或阻塞时间结束。)

相关文章
|
1月前
|
C语言 C++
文件底层的理解之缓冲区
文件底层的理解之缓冲区
|
存储 消息中间件 NoSQL
计网 - 流和缓冲区:缓冲区的 flip 是怎么回事?
计网 - 流和缓冲区:缓冲区的 flip 是怎么回事?
76 0
|
存储 安全 API
22-RTOS 流缓冲区 API 函数
22-RTOS 流缓冲区 API 函数
|
存储 缓存 Linux
流的读写
流的读写
80 0
|
监控 Android开发 开发者
【Android 高性能音频】AAudio 音频流 缓冲区 简介 ( AAudio 音频流内部缓冲区 | 缓冲区帧容量 | 缓冲区帧大小 | 音频数据读写缓冲区 )
【Android 高性能音频】AAudio 音频流 缓冲区 简介 ( AAudio 音频流内部缓冲区 | 缓冲区帧容量 | 缓冲区帧大小 | 音频数据读写缓冲区 )
423 0
【Android 高性能音频】AAudio 音频流 缓冲区 简介 ( AAudio 音频流内部缓冲区 | 缓冲区帧容量 | 缓冲区帧大小 | 音频数据读写缓冲区 )
|
Java 开发者
内存操作流|学习笔记
快速学习 内存操作流
117 0
内存操作流|学习笔记
|
C语言
【C 语言】文件操作 ( C 语言中的文件操作函数 | 磁盘与内存缓冲区 | 缓冲区工作机制 )
【C 语言】文件操作 ( C 语言中的文件操作函数 | 磁盘与内存缓冲区 | 缓冲区工作机制 )
142 0
|
Java 开发者
内存操作流(内存流基本操作)|学习笔记
快速学习 内存操作流(内存流基本操作)
|
Android开发
【Android 高性能音频】AAudio 缓冲区控制 ( XRun | 欠载 UnderRun | 超限 OverRun | 获取缓冲区大小 | 设置缓冲区大小 )
【Android 高性能音频】AAudio 缓冲区控制 ( XRun | 欠载 UnderRun | 超限 OverRun | 获取缓冲区大小 | 设置缓冲区大小 )
702 0
|
存储 Android开发 数据格式
【Android 高性能音频】AAudio 音频流 读写操作 ( 音频流读写数据 | 阻塞时间设定 | 注意事项 | AAudioStream_read | AAudioStream_write )
【Android 高性能音频】AAudio 音频流 读写操作 ( 音频流读写数据 | 阻塞时间设定 | 注意事项 | AAudioStream_read | AAudioStream_write )
496 0