2 * FreeRTOS Kernel V10.3.1
\r
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
28 * Stream buffers are used to send a continuous stream of data from one task or
\r
29 * interrupt to another. Their implementation is light weight, making them
\r
30 * particularly suited for interrupt to task and core to core communication
\r
33 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
34 * implementation (so also the message buffer implementation, as message buffers
\r
35 * are built on top of stream buffers) assumes there is only one task or
\r
36 * interrupt that will write to the buffer (the writer), and only one task or
\r
37 * interrupt that will read from the buffer (the reader). It is safe for the
\r
38 * writer and reader to be different tasks or interrupts, but, unlike other
\r
39 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
40 * multiple different readers. If there are to be multiple different writers
\r
41 * then the application writer must place each call to a writing API function
\r
42 * (such as xStreamBufferSend()) inside a critical section and set the send
\r
43 * block time to 0. Likewise, if there are to be multiple different readers
\r
44 * then the application writer must place each call to a reading API function
\r
45 * (such as xStreamBufferReceive()) inside a critical section section and set the
\r
46 * receive block time to 0.
\r
50 #ifndef STREAM_BUFFER_H
\r
51 #define STREAM_BUFFER_H
\r
53 #ifndef INC_FREERTOS_H
\r
54 #error "include FreeRTOS.h must appear in source files before include stream_buffer.h"
\r
57 #if defined( __cplusplus )
\r
62 * Type by which stream buffers are referenced. For example, a call to
\r
63 * xStreamBufferCreate() returns an StreamBufferHandle_t variable that can
\r
64 * then be used as a parameter to xStreamBufferSend(), xStreamBufferReceive(),
\r
67 struct StreamBufferDef_t;
\r
68 typedef struct StreamBufferDef_t * StreamBufferHandle_t;
\r
75 StreamBufferHandle_t xStreamBufferCreate( size_t xBufferSizeBytes, size_t xTriggerLevelBytes );
\r
78 * Creates a new stream buffer using dynamically allocated memory. See
\r
79 * xStreamBufferCreateStatic() for a version that uses statically allocated
\r
80 * memory (memory that is allocated at compile time).
\r
82 * configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 or left undefined in
\r
83 * FreeRTOSConfig.h for xStreamBufferCreate() to be available.
\r
85 * @param xBufferSizeBytes The total number of bytes the stream buffer will be
\r
86 * able to hold at any one time.
\r
88 * @param xTriggerLevelBytes The number of bytes that must be in the stream
\r
89 * buffer before a task that is blocked on the stream buffer to wait for data is
\r
90 * moved out of the blocked state. For example, if a task is blocked on a read
\r
91 * of an empty stream buffer that has a trigger level of 1 then the task will be
\r
92 * unblocked when a single byte is written to the buffer or the task's block
\r
93 * time expires. As another example, if a task is blocked on a read of an empty
\r
94 * stream buffer that has a trigger level of 10 then the task will not be
\r
95 * unblocked until the stream buffer contains at least 10 bytes or the task's
\r
96 * block time expires. If a reading task's block time expires before the
\r
97 * trigger level is reached then the task will still receive however many bytes
\r
98 * are actually available. Setting a trigger level of 0 will result in a
\r
99 * trigger level of 1 being used. It is not valid to specify a trigger level
\r
100 * that is greater than the buffer size.
\r
102 * @return If NULL is returned, then the stream buffer cannot be created
\r
103 * because there is insufficient heap memory available for FreeRTOS to allocate
\r
104 * the stream buffer data structures and storage area. A non-NULL value being
\r
105 * returned indicates that the stream buffer has been created successfully -
\r
106 * the returned value should be stored as the handle to the created stream
\r
112 void vAFunction( void )
\r
114 StreamBufferHandle_t xStreamBuffer;
\r
115 const size_t xStreamBufferSizeBytes = 100, xTriggerLevel = 10;
\r
117 // Create a stream buffer that can hold 100 bytes. The memory used to hold
\r
118 // both the stream buffer structure and the data in the stream buffer is
\r
119 // allocated dynamically.
\r
120 xStreamBuffer = xStreamBufferCreate( xStreamBufferSizeBytes, xTriggerLevel );
\r
122 if( xStreamBuffer == NULL )
\r
124 // There was not enough heap memory space available to create the
\r
129 // The stream buffer was created successfully and can now be used.
\r
133 * \defgroup xStreamBufferCreate xStreamBufferCreate
\r
134 * \ingroup StreamBufferManagement
\r
136 #define xStreamBufferCreate( xBufferSizeBytes, xTriggerLevelBytes ) xStreamBufferGenericCreate( xBufferSizeBytes, xTriggerLevelBytes, pdFALSE )
\r
142 StreamBufferHandle_t xStreamBufferCreateStatic( size_t xBufferSizeBytes,
\r
143 size_t xTriggerLevelBytes,
\r
144 uint8_t *pucStreamBufferStorageArea,
\r
145 StaticStreamBuffer_t *pxStaticStreamBuffer );
\r
147 * Creates a new stream buffer using statically allocated memory. See
\r
148 * xStreamBufferCreate() for a version that uses dynamically allocated memory.
\r
150 * configSUPPORT_STATIC_ALLOCATION must be set to 1 in FreeRTOSConfig.h for
\r
151 * xStreamBufferCreateStatic() to be available.
\r
153 * @param xBufferSizeBytes The size, in bytes, of the buffer pointed to by the
\r
154 * pucStreamBufferStorageArea parameter.
\r
156 * @param xTriggerLevelBytes The number of bytes that must be in the stream
\r
157 * buffer before a task that is blocked on the stream buffer to wait for data is
\r
158 * moved out of the blocked state. For example, if a task is blocked on a read
\r
159 * of an empty stream buffer that has a trigger level of 1 then the task will be
\r
160 * unblocked when a single byte is written to the buffer or the task's block
\r
161 * time expires. As another example, if a task is blocked on a read of an empty
\r
162 * stream buffer that has a trigger level of 10 then the task will not be
\r
163 * unblocked until the stream buffer contains at least 10 bytes or the task's
\r
164 * block time expires. If a reading task's block time expires before the
\r
165 * trigger level is reached then the task will still receive however many bytes
\r
166 * are actually available. Setting a trigger level of 0 will result in a
\r
167 * trigger level of 1 being used. It is not valid to specify a trigger level
\r
168 * that is greater than the buffer size.
\r
170 * @param pucStreamBufferStorageArea Must point to a uint8_t array that is at
\r
171 * least xBufferSizeBytes + 1 big. This is the array to which streams are
\r
172 * copied when they are written to the stream buffer.
\r
174 * @param pxStaticStreamBuffer Must point to a variable of type
\r
175 * StaticStreamBuffer_t, which will be used to hold the stream buffer's data
\r
178 * @return If the stream buffer is created successfully then a handle to the
\r
179 * created stream buffer is returned. If either pucStreamBufferStorageArea or
\r
180 * pxStaticstreamBuffer are NULL then NULL is returned.
\r
185 // Used to dimension the array used to hold the streams. The available space
\r
186 // will actually be one less than this, so 999.
\r
187 #define STORAGE_SIZE_BYTES 1000
\r
189 // Defines the memory that will actually hold the streams within the stream
\r
191 static uint8_t ucStorageBuffer[ STORAGE_SIZE_BYTES ];
\r
193 // The variable used to hold the stream buffer structure.
\r
194 StaticStreamBuffer_t xStreamBufferStruct;
\r
196 void MyFunction( void )
\r
198 StreamBufferHandle_t xStreamBuffer;
\r
199 const size_t xTriggerLevel = 1;
\r
201 xStreamBuffer = xStreamBufferCreateStatic( sizeof( ucBufferStorage ),
\r
204 &xStreamBufferStruct );
\r
206 // As neither the pucStreamBufferStorageArea or pxStaticStreamBuffer
\r
207 // parameters were NULL, xStreamBuffer will not be NULL, and can be used to
\r
208 // reference the created stream buffer in other stream buffer API calls.
\r
210 // Other code that uses the stream buffer can go here.
\r
214 * \defgroup xStreamBufferCreateStatic xStreamBufferCreateStatic
\r
215 * \ingroup StreamBufferManagement
\r
217 #define xStreamBufferCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, pucStreamBufferStorageArea, pxStaticStreamBuffer ) xStreamBufferGenericCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, pdFALSE, pucStreamBufferStorageArea, pxStaticStreamBuffer )
\r
223 size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
\r
224 const void *pvTxData,
\r
225 size_t xDataLengthBytes,
\r
226 TickType_t xTicksToWait );
\r
229 * Sends bytes to a stream buffer. The bytes are copied into the stream buffer.
\r
231 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
232 * implementation (so also the message buffer implementation, as message buffers
\r
233 * are built on top of stream buffers) assumes there is only one task or
\r
234 * interrupt that will write to the buffer (the writer), and only one task or
\r
235 * interrupt that will read from the buffer (the reader). It is safe for the
\r
236 * writer and reader to be different tasks or interrupts, but, unlike other
\r
237 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
238 * multiple different readers. If there are to be multiple different writers
\r
239 * then the application writer must place each call to a writing API function
\r
240 * (such as xStreamBufferSend()) inside a critical section and set the send
\r
241 * block time to 0. Likewise, if there are to be multiple different readers
\r
242 * then the application writer must place each call to a reading API function
\r
243 * (such as xStreamBufferReceive()) inside a critical section and set the receive
\r
246 * Use xStreamBufferSend() to write to a stream buffer from a task. Use
\r
247 * xStreamBufferSendFromISR() to write to a stream buffer from an interrupt
\r
248 * service routine (ISR).
\r
250 * @param xStreamBuffer The handle of the stream buffer to which a stream is
\r
253 * @param pvTxData A pointer to the buffer that holds the bytes to be copied
\r
254 * into the stream buffer.
\r
256 * @param xDataLengthBytes The maximum number of bytes to copy from pvTxData
\r
257 * into the stream buffer.
\r
259 * @param xTicksToWait The maximum amount of time the task should remain in the
\r
260 * Blocked state to wait for enough space to become available in the stream
\r
261 * buffer, should the stream buffer contain too little space to hold the
\r
262 * another xDataLengthBytes bytes. The block time is specified in tick periods,
\r
263 * so the absolute time it represents is dependent on the tick frequency. The
\r
264 * macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds
\r
265 * into a time specified in ticks. Setting xTicksToWait to portMAX_DELAY will
\r
266 * cause the task to wait indefinitely (without timing out), provided
\r
267 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. If a task times out
\r
268 * before it can write all xDataLengthBytes into the buffer it will still write
\r
269 * as many bytes as possible. A task does not use any CPU time when it is in
\r
270 * the blocked state.
\r
272 * @return The number of bytes written to the stream buffer. If a task times
\r
273 * out before it can write all xDataLengthBytes into the buffer it will still
\r
274 * write as many bytes as possible.
\r
278 void vAFunction( StreamBufferHandle_t xStreamBuffer )
\r
281 uint8_t ucArrayToSend[] = { 0, 1, 2, 3 };
\r
282 char *pcStringToSend = "String to send";
\r
283 const TickType_t x100ms = pdMS_TO_TICKS( 100 );
\r
285 // Send an array to the stream buffer, blocking for a maximum of 100ms to
\r
286 // wait for enough space to be available in the stream buffer.
\r
287 xBytesSent = xStreamBufferSend( xStreamBuffer, ( void * ) ucArrayToSend, sizeof( ucArrayToSend ), x100ms );
\r
289 if( xBytesSent != sizeof( ucArrayToSend ) )
\r
291 // The call to xStreamBufferSend() times out before there was enough
\r
292 // space in the buffer for the data to be written, but it did
\r
293 // successfully write xBytesSent bytes.
\r
296 // Send the string to the stream buffer. Return immediately if there is not
\r
297 // enough space in the buffer.
\r
298 xBytesSent = xStreamBufferSend( xStreamBuffer, ( void * ) pcStringToSend, strlen( pcStringToSend ), 0 );
\r
300 if( xBytesSent != strlen( pcStringToSend ) )
\r
302 // The entire string could not be added to the stream buffer because
\r
303 // there was not enough free space in the buffer, but xBytesSent bytes
\r
304 // were sent. Could try again to send the remaining bytes.
\r
308 * \defgroup xStreamBufferSend xStreamBufferSend
\r
309 * \ingroup StreamBufferManagement
\r
311 size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
\r
312 const void *pvTxData,
\r
313 size_t xDataLengthBytes,
\r
314 TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
\r
320 size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
\r
321 const void *pvTxData,
\r
322 size_t xDataLengthBytes,
\r
323 BaseType_t *pxHigherPriorityTaskWoken );
\r
326 * Interrupt safe version of the API function that sends a stream of bytes to
\r
327 * the stream buffer.
\r
329 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
330 * implementation (so also the message buffer implementation, as message buffers
\r
331 * are built on top of stream buffers) assumes there is only one task or
\r
332 * interrupt that will write to the buffer (the writer), and only one task or
\r
333 * interrupt that will read from the buffer (the reader). It is safe for the
\r
334 * writer and reader to be different tasks or interrupts, but, unlike other
\r
335 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
336 * multiple different readers. If there are to be multiple different writers
\r
337 * then the application writer must place each call to a writing API function
\r
338 * (such as xStreamBufferSend()) inside a critical section and set the send
\r
339 * block time to 0. Likewise, if there are to be multiple different readers
\r
340 * then the application writer must place each call to a reading API function
\r
341 * (such as xStreamBufferReceive()) inside a critical section and set the receive
\r
344 * Use xStreamBufferSend() to write to a stream buffer from a task. Use
\r
345 * xStreamBufferSendFromISR() to write to a stream buffer from an interrupt
\r
346 * service routine (ISR).
\r
348 * @param xStreamBuffer The handle of the stream buffer to which a stream is
\r
351 * @param pvTxData A pointer to the data that is to be copied into the stream
\r
354 * @param xDataLengthBytes The maximum number of bytes to copy from pvTxData
\r
355 * into the stream buffer.
\r
357 * @param pxHigherPriorityTaskWoken It is possible that a stream buffer will
\r
358 * have a task blocked on it waiting for data. Calling
\r
359 * xStreamBufferSendFromISR() can make data available, and so cause a task that
\r
360 * was waiting for data to leave the Blocked state. If calling
\r
361 * xStreamBufferSendFromISR() causes a task to leave the Blocked state, and the
\r
362 * unblocked task has a priority higher than the currently executing task (the
\r
363 * task that was interrupted), then, internally, xStreamBufferSendFromISR()
\r
364 * will set *pxHigherPriorityTaskWoken to pdTRUE. If
\r
365 * xStreamBufferSendFromISR() sets this value to pdTRUE, then normally a
\r
366 * context switch should be performed before the interrupt is exited. This will
\r
367 * ensure that the interrupt returns directly to the highest priority Ready
\r
368 * state task. *pxHigherPriorityTaskWoken should be set to pdFALSE before it
\r
369 * is passed into the function. See the example code below for an example.
\r
371 * @return The number of bytes actually written to the stream buffer, which will
\r
372 * be less than xDataLengthBytes if the stream buffer didn't have enough free
\r
373 * space for all the bytes to be written.
\r
377 // A stream buffer that has already been created.
\r
378 StreamBufferHandle_t xStreamBuffer;
\r
380 void vAnInterruptServiceRoutine( void )
\r
383 char *pcStringToSend = "String to send";
\r
384 BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
\r
386 // Attempt to send the string to the stream buffer.
\r
387 xBytesSent = xStreamBufferSendFromISR( xStreamBuffer,
\r
388 ( void * ) pcStringToSend,
\r
389 strlen( pcStringToSend ),
\r
390 &xHigherPriorityTaskWoken );
\r
392 if( xBytesSent != strlen( pcStringToSend ) )
\r
394 // There was not enough free space in the stream buffer for the entire
\r
395 // string to be written, ut xBytesSent bytes were written.
\r
398 // If xHigherPriorityTaskWoken was set to pdTRUE inside
\r
399 // xStreamBufferSendFromISR() then a task that has a priority above the
\r
400 // priority of the currently executing task was unblocked and a context
\r
401 // switch should be performed to ensure the ISR returns to the unblocked
\r
402 // task. In most FreeRTOS ports this is done by simply passing
\r
403 // xHigherPriorityTaskWoken into taskYIELD_FROM_ISR(), which will test the
\r
404 // variables value, and perform the context switch if necessary. Check the
\r
405 // documentation for the port in use for port specific instructions.
\r
406 taskYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
409 * \defgroup xStreamBufferSendFromISR xStreamBufferSendFromISR
\r
410 * \ingroup StreamBufferManagement
\r
412 size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
\r
413 const void *pvTxData,
\r
414 size_t xDataLengthBytes,
\r
415 BaseType_t * const pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
\r
421 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
\r
423 size_t xBufferLengthBytes,
\r
424 TickType_t xTicksToWait );
\r
427 * Receives bytes from a stream buffer.
\r
429 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
430 * implementation (so also the message buffer implementation, as message buffers
\r
431 * are built on top of stream buffers) assumes there is only one task or
\r
432 * interrupt that will write to the buffer (the writer), and only one task or
\r
433 * interrupt that will read from the buffer (the reader). It is safe for the
\r
434 * writer and reader to be different tasks or interrupts, but, unlike other
\r
435 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
436 * multiple different readers. If there are to be multiple different writers
\r
437 * then the application writer must place each call to a writing API function
\r
438 * (such as xStreamBufferSend()) inside a critical section and set the send
\r
439 * block time to 0. Likewise, if there are to be multiple different readers
\r
440 * then the application writer must place each call to a reading API function
\r
441 * (such as xStreamBufferReceive()) inside a critical section and set the receive
\r
444 * Use xStreamBufferReceive() to read from a stream buffer from a task. Use
\r
445 * xStreamBufferReceiveFromISR() to read from a stream buffer from an
\r
446 * interrupt service routine (ISR).
\r
448 * @param xStreamBuffer The handle of the stream buffer from which bytes are to
\r
451 * @param pvRxData A pointer to the buffer into which the received bytes will be
\r
454 * @param xBufferLengthBytes The length of the buffer pointed to by the
\r
455 * pvRxData parameter. This sets the maximum number of bytes to receive in one
\r
456 * call. xStreamBufferReceive will return as many bytes as possible up to a
\r
457 * maximum set by xBufferLengthBytes.
\r
459 * @param xTicksToWait The maximum amount of time the task should remain in the
\r
460 * Blocked state to wait for data to become available if the stream buffer is
\r
461 * empty. xStreamBufferReceive() will return immediately if xTicksToWait is
\r
462 * zero. The block time is specified in tick periods, so the absolute time it
\r
463 * represents is dependent on the tick frequency. The macro pdMS_TO_TICKS() can
\r
464 * be used to convert a time specified in milliseconds into a time specified in
\r
465 * ticks. Setting xTicksToWait to portMAX_DELAY will cause the task to wait
\r
466 * indefinitely (without timing out), provided INCLUDE_vTaskSuspend is set to 1
\r
467 * in FreeRTOSConfig.h. A task does not use any CPU time when it is in the
\r
470 * @return The number of bytes actually read from the stream buffer, which will
\r
471 * be less than xBufferLengthBytes if the call to xStreamBufferReceive() timed
\r
472 * out before xBufferLengthBytes were available.
\r
476 void vAFunction( StreamBuffer_t xStreamBuffer )
\r
478 uint8_t ucRxData[ 20 ];
\r
479 size_t xReceivedBytes;
\r
480 const TickType_t xBlockTime = pdMS_TO_TICKS( 20 );
\r
482 // Receive up to another sizeof( ucRxData ) bytes from the stream buffer.
\r
483 // Wait in the Blocked state (so not using any CPU processing time) for a
\r
484 // maximum of 100ms for the full sizeof( ucRxData ) number of bytes to be
\r
486 xReceivedBytes = xStreamBufferReceive( xStreamBuffer,
\r
487 ( void * ) ucRxData,
\r
488 sizeof( ucRxData ),
\r
491 if( xReceivedBytes > 0 )
\r
493 // A ucRxData contains another xRecievedBytes bytes of data, which can
\r
494 // be processed here....
\r
498 * \defgroup xStreamBufferReceive xStreamBufferReceive
\r
499 * \ingroup StreamBufferManagement
\r
501 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
\r
503 size_t xBufferLengthBytes,
\r
504 TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
\r
510 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
\r
512 size_t xBufferLengthBytes,
\r
513 BaseType_t *pxHigherPriorityTaskWoken );
\r
516 * An interrupt safe version of the API function that receives bytes from a
\r
519 * Use xStreamBufferReceive() to read bytes from a stream buffer from a task.
\r
520 * Use xStreamBufferReceiveFromISR() to read bytes from a stream buffer from an
\r
521 * interrupt service routine (ISR).
\r
523 * @param xStreamBuffer The handle of the stream buffer from which a stream
\r
524 * is being received.
\r
526 * @param pvRxData A pointer to the buffer into which the received bytes are
\r
529 * @param xBufferLengthBytes The length of the buffer pointed to by the
\r
530 * pvRxData parameter. This sets the maximum number of bytes to receive in one
\r
531 * call. xStreamBufferReceive will return as many bytes as possible up to a
\r
532 * maximum set by xBufferLengthBytes.
\r
534 * @param pxHigherPriorityTaskWoken It is possible that a stream buffer will
\r
535 * have a task blocked on it waiting for space to become available. Calling
\r
536 * xStreamBufferReceiveFromISR() can make space available, and so cause a task
\r
537 * that is waiting for space to leave the Blocked state. If calling
\r
538 * xStreamBufferReceiveFromISR() causes a task to leave the Blocked state, and
\r
539 * the unblocked task has a priority higher than the currently executing task
\r
540 * (the task that was interrupted), then, internally,
\r
541 * xStreamBufferReceiveFromISR() will set *pxHigherPriorityTaskWoken to pdTRUE.
\r
542 * If xStreamBufferReceiveFromISR() sets this value to pdTRUE, then normally a
\r
543 * context switch should be performed before the interrupt is exited. That will
\r
544 * ensure the interrupt returns directly to the highest priority Ready state
\r
545 * task. *pxHigherPriorityTaskWoken should be set to pdFALSE before it is
\r
546 * passed into the function. See the code example below for an example.
\r
548 * @return The number of bytes read from the stream buffer, if any.
\r
552 // A stream buffer that has already been created.
\r
553 StreamBuffer_t xStreamBuffer;
\r
555 void vAnInterruptServiceRoutine( void )
\r
557 uint8_t ucRxData[ 20 ];
\r
558 size_t xReceivedBytes;
\r
559 BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
\r
561 // Receive the next stream from the stream buffer.
\r
562 xReceivedBytes = xStreamBufferReceiveFromISR( xStreamBuffer,
\r
563 ( void * ) ucRxData,
\r
564 sizeof( ucRxData ),
\r
565 &xHigherPriorityTaskWoken );
\r
567 if( xReceivedBytes > 0 )
\r
569 // ucRxData contains xReceivedBytes read from the stream buffer.
\r
570 // Process the stream here....
\r
573 // If xHigherPriorityTaskWoken was set to pdTRUE inside
\r
574 // xStreamBufferReceiveFromISR() then a task that has a priority above the
\r
575 // priority of the currently executing task was unblocked and a context
\r
576 // switch should be performed to ensure the ISR returns to the unblocked
\r
577 // task. In most FreeRTOS ports this is done by simply passing
\r
578 // xHigherPriorityTaskWoken into taskYIELD_FROM_ISR(), which will test the
\r
579 // variables value, and perform the context switch if necessary. Check the
\r
580 // documentation for the port in use for port specific instructions.
\r
581 taskYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
584 * \defgroup xStreamBufferReceiveFromISR xStreamBufferReceiveFromISR
\r
585 * \ingroup StreamBufferManagement
\r
587 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
\r
589 size_t xBufferLengthBytes,
\r
590 BaseType_t * const pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
\r
596 void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer );
\r
599 * Deletes a stream buffer that was previously created using a call to
\r
600 * xStreamBufferCreate() or xStreamBufferCreateStatic(). If the stream
\r
601 * buffer was created using dynamic memory (that is, by xStreamBufferCreate()),
\r
602 * then the allocated memory is freed.
\r
604 * A stream buffer handle must not be used after the stream buffer has been
\r
607 * @param xStreamBuffer The handle of the stream buffer to be deleted.
\r
609 * \defgroup vStreamBufferDelete vStreamBufferDelete
\r
610 * \ingroup StreamBufferManagement
\r
612 void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
618 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer );
\r
621 * Queries a stream buffer to see if it is full. A stream buffer is full if it
\r
622 * does not have any free space, and therefore cannot accept any more data.
\r
624 * @param xStreamBuffer The handle of the stream buffer being queried.
\r
626 * @return If the stream buffer is full then pdTRUE is returned. Otherwise
\r
627 * pdFALSE is returned.
\r
629 * \defgroup xStreamBufferIsFull xStreamBufferIsFull
\r
630 * \ingroup StreamBufferManagement
\r
632 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
638 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer );
\r
641 * Queries a stream buffer to see if it is empty. A stream buffer is empty if
\r
642 * it does not contain any data.
\r
644 * @param xStreamBuffer The handle of the stream buffer being queried.
\r
646 * @return If the stream buffer is empty then pdTRUE is returned. Otherwise
\r
647 * pdFALSE is returned.
\r
649 * \defgroup xStreamBufferIsEmpty xStreamBufferIsEmpty
\r
650 * \ingroup StreamBufferManagement
\r
652 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
658 BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer );
\r
661 * Resets a stream buffer to its initial, empty, state. Any data that was in
\r
662 * the stream buffer is discarded. A stream buffer can only be reset if there
\r
663 * are no tasks blocked waiting to either send to or receive from the stream
\r
666 * @param xStreamBuffer The handle of the stream buffer being reset.
\r
668 * @return If the stream buffer is reset then pdPASS is returned. If there was
\r
669 * a task blocked waiting to send to or read from the stream buffer then the
\r
670 * stream buffer is not reset and pdFAIL is returned.
\r
672 * \defgroup xStreamBufferReset xStreamBufferReset
\r
673 * \ingroup StreamBufferManagement
\r
675 BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
681 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer );
\r
684 * Queries a stream buffer to see how much free space it contains, which is
\r
685 * equal to the amount of data that can be sent to the stream buffer before it
\r
688 * @param xStreamBuffer The handle of the stream buffer being queried.
\r
690 * @return The number of bytes that can be written to the stream buffer before
\r
691 * the stream buffer would be full.
\r
693 * \defgroup xStreamBufferSpacesAvailable xStreamBufferSpacesAvailable
\r
694 * \ingroup StreamBufferManagement
\r
696 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
702 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer );
\r
705 * Queries a stream buffer to see how much data it contains, which is equal to
\r
706 * the number of bytes that can be read from the stream buffer before the stream
\r
707 * buffer would be empty.
\r
709 * @param xStreamBuffer The handle of the stream buffer being queried.
\r
711 * @return The number of bytes that can be read from the stream buffer before
\r
712 * the stream buffer would be empty.
\r
714 * \defgroup xStreamBufferBytesAvailable xStreamBufferBytesAvailable
\r
715 * \ingroup StreamBufferManagement
\r
717 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
723 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer, size_t xTriggerLevel );
\r
726 * A stream buffer's trigger level is the number of bytes that must be in the
\r
727 * stream buffer before a task that is blocked on the stream buffer to
\r
728 * wait for data is moved out of the blocked state. For example, if a task is
\r
729 * blocked on a read of an empty stream buffer that has a trigger level of 1
\r
730 * then the task will be unblocked when a single byte is written to the buffer
\r
731 * or the task's block time expires. As another example, if a task is blocked
\r
732 * on a read of an empty stream buffer that has a trigger level of 10 then the
\r
733 * task will not be unblocked until the stream buffer contains at least 10 bytes
\r
734 * or the task's block time expires. If a reading task's block time expires
\r
735 * before the trigger level is reached then the task will still receive however
\r
736 * many bytes are actually available. Setting a trigger level of 0 will result
\r
737 * in a trigger level of 1 being used. It is not valid to specify a trigger
\r
738 * level that is greater than the buffer size.
\r
740 * A trigger level is set when the stream buffer is created, and can be modified
\r
741 * using xStreamBufferSetTriggerLevel().
\r
743 * @param xStreamBuffer The handle of the stream buffer being updated.
\r
745 * @param xTriggerLevel The new trigger level for the stream buffer.
\r
747 * @return If xTriggerLevel was less than or equal to the stream buffer's length
\r
748 * then the trigger level will be updated and pdTRUE is returned. Otherwise
\r
749 * pdFALSE is returned.
\r
751 * \defgroup xStreamBufferSetTriggerLevel xStreamBufferSetTriggerLevel
\r
752 * \ingroup StreamBufferManagement
\r
754 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer, size_t xTriggerLevel ) PRIVILEGED_FUNCTION;
\r
760 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken );
\r
763 * For advanced users only.
\r
765 * The sbSEND_COMPLETED() macro is called from within the FreeRTOS APIs when
\r
766 * data is sent to a message buffer or stream buffer. If there was a task that
\r
767 * was blocked on the message or stream buffer waiting for data to arrive then
\r
768 * the sbSEND_COMPLETED() macro sends a notification to the task to remove it
\r
769 * from the Blocked state. xStreamBufferSendCompletedFromISR() does the same
\r
770 * thing. It is provided to enable application writers to implement their own
\r
771 * version of sbSEND_COMPLETED(), and MUST NOT BE USED AT ANY OTHER TIME.
\r
773 * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
\r
774 * additional information.
\r
776 * @param xStreamBuffer The handle of the stream buffer to which data was
\r
779 * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
\r
780 * initialised to pdFALSE before it is passed into
\r
781 * xStreamBufferSendCompletedFromISR(). If calling
\r
782 * xStreamBufferSendCompletedFromISR() removes a task from the Blocked state,
\r
783 * and the task has a priority above the priority of the currently running task,
\r
784 * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
\r
785 * context switch should be performed before exiting the ISR.
\r
787 * @return If a task was removed from the Blocked state then pdTRUE is returned.
\r
788 * Otherwise pdFALSE is returned.
\r
790 * \defgroup xStreamBufferSendCompletedFromISR xStreamBufferSendCompletedFromISR
\r
791 * \ingroup StreamBufferManagement
\r
793 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
\r
799 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken );
\r
802 * For advanced users only.
\r
804 * The sbRECEIVE_COMPLETED() macro is called from within the FreeRTOS APIs when
\r
805 * data is read out of a message buffer or stream buffer. If there was a task
\r
806 * that was blocked on the message or stream buffer waiting for data to arrive
\r
807 * then the sbRECEIVE_COMPLETED() macro sends a notification to the task to
\r
808 * remove it from the Blocked state. xStreamBufferReceiveCompletedFromISR()
\r
809 * does the same thing. It is provided to enable application writers to
\r
810 * implement their own version of sbRECEIVE_COMPLETED(), and MUST NOT BE USED AT
\r
813 * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
\r
814 * additional information.
\r
816 * @param xStreamBuffer The handle of the stream buffer from which data was
\r
819 * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
\r
820 * initialised to pdFALSE before it is passed into
\r
821 * xStreamBufferReceiveCompletedFromISR(). If calling
\r
822 * xStreamBufferReceiveCompletedFromISR() removes a task from the Blocked state,
\r
823 * and the task has a priority above the priority of the currently running task,
\r
824 * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
\r
825 * context switch should be performed before exiting the ISR.
\r
827 * @return If a task was removed from the Blocked state then pdTRUE is returned.
\r
828 * Otherwise pdFALSE is returned.
\r
830 * \defgroup xStreamBufferReceiveCompletedFromISR xStreamBufferReceiveCompletedFromISR
\r
831 * \ingroup StreamBufferManagement
\r
833 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
\r
835 /* Functions below here are not part of the public API. */
\r
836 StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
\r
837 size_t xTriggerLevelBytes,
\r
838 BaseType_t xIsMessageBuffer ) PRIVILEGED_FUNCTION;
\r
840 StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
\r
841 size_t xTriggerLevelBytes,
\r
842 BaseType_t xIsMessageBuffer,
\r
843 uint8_t * const pucStreamBufferStorageArea,
\r
844 StaticStreamBuffer_t * const pxStaticStreamBuffer ) PRIVILEGED_FUNCTION;
\r
846 size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
848 #if( configUSE_TRACE_FACILITY == 1 )
\r
849 void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer, UBaseType_t uxStreamBufferNumber ) PRIVILEGED_FUNCTION;
\r
850 UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
851 uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
854 #if defined( __cplusplus )
\r
858 #endif /* !defined( STREAM_BUFFER_H ) */
\r