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 * https://www.FreeRTOS.org
\r
23 * https://github.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
58 #if defined( __cplusplus )
\r
64 * Type by which stream buffers are referenced. For example, a call to
\r
65 * xStreamBufferCreate() returns an StreamBufferHandle_t variable that can
\r
66 * then be used as a parameter to xStreamBufferSend(), xStreamBufferReceive(),
\r
69 struct StreamBufferDef_t;
\r
70 typedef struct StreamBufferDef_t * StreamBufferHandle_t;
\r
77 * StreamBufferHandle_t xStreamBufferCreate( size_t xBufferSizeBytes, size_t xTriggerLevelBytes );
\r
80 * Creates a new stream buffer using dynamically allocated memory. See
\r
81 * xStreamBufferCreateStatic() for a version that uses statically allocated
\r
82 * memory (memory that is allocated at compile time).
\r
84 * configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 or left undefined in
\r
85 * FreeRTOSConfig.h for xStreamBufferCreate() to be available.
\r
87 * @param xBufferSizeBytes The total number of bytes the stream buffer will be
\r
88 * able to hold at any one time.
\r
90 * @param xTriggerLevelBytes The number of bytes that must be in the stream
\r
91 * buffer before a task that is blocked on the stream buffer to wait for data is
\r
92 * moved out of the blocked state. For example, if a task is blocked on a read
\r
93 * of an empty stream buffer that has a trigger level of 1 then the task will be
\r
94 * unblocked when a single byte is written to the buffer or the task's block
\r
95 * time expires. As another example, if a task is blocked on a read of an empty
\r
96 * stream buffer that has a trigger level of 10 then the task will not be
\r
97 * unblocked until the stream buffer contains at least 10 bytes or the task's
\r
98 * block time expires. If a reading task's block time expires before the
\r
99 * trigger level is reached then the task will still receive however many bytes
\r
100 * are actually available. Setting a trigger level of 0 will result in a
\r
101 * trigger level of 1 being used. It is not valid to specify a trigger level
\r
102 * that is greater than the buffer size.
\r
104 * @return If NULL is returned, then the stream buffer cannot be created
\r
105 * because there is insufficient heap memory available for FreeRTOS to allocate
\r
106 * the stream buffer data structures and storage area. A non-NULL value being
\r
107 * returned indicates that the stream buffer has been created successfully -
\r
108 * the returned value should be stored as the handle to the created stream
\r
114 * void vAFunction( void )
\r
116 * StreamBufferHandle_t xStreamBuffer;
\r
117 * const size_t xStreamBufferSizeBytes = 100, xTriggerLevel = 10;
\r
119 * // Create a stream buffer that can hold 100 bytes. The memory used to hold
\r
120 * // both the stream buffer structure and the data in the stream buffer is
\r
121 * // allocated dynamically.
\r
122 * xStreamBuffer = xStreamBufferCreate( xStreamBufferSizeBytes, xTriggerLevel );
\r
124 * if( xStreamBuffer == NULL )
\r
126 * // There was not enough heap memory space available to create the
\r
127 * // stream buffer.
\r
131 * // The stream buffer was created successfully and can now be used.
\r
135 * \defgroup xStreamBufferCreate xStreamBufferCreate
\r
136 * \ingroup StreamBufferManagement
\r
138 #define xStreamBufferCreate( xBufferSizeBytes, xTriggerLevelBytes ) xStreamBufferGenericCreate( xBufferSizeBytes, xTriggerLevelBytes, pdFALSE )
\r
144 * StreamBufferHandle_t xStreamBufferCreateStatic( size_t xBufferSizeBytes,
\r
145 * size_t xTriggerLevelBytes,
\r
146 * uint8_t *pucStreamBufferStorageArea,
\r
147 * StaticStreamBuffer_t *pxStaticStreamBuffer );
\r
149 * Creates a new stream buffer using statically allocated memory. See
\r
150 * xStreamBufferCreate() for a version that uses dynamically allocated memory.
\r
152 * configSUPPORT_STATIC_ALLOCATION must be set to 1 in FreeRTOSConfig.h for
\r
153 * xStreamBufferCreateStatic() to be available.
\r
155 * @param xBufferSizeBytes The size, in bytes, of the buffer pointed to by the
\r
156 * pucStreamBufferStorageArea parameter.
\r
158 * @param xTriggerLevelBytes The number of bytes that must be in the stream
\r
159 * buffer before a task that is blocked on the stream buffer to wait for data is
\r
160 * moved out of the blocked state. For example, if a task is blocked on a read
\r
161 * of an empty stream buffer that has a trigger level of 1 then the task will be
\r
162 * unblocked when a single byte is written to the buffer or the task's block
\r
163 * time expires. As another example, if a task is blocked on a read of an empty
\r
164 * stream buffer that has a trigger level of 10 then the task will not be
\r
165 * unblocked until the stream buffer contains at least 10 bytes or the task's
\r
166 * block time expires. If a reading task's block time expires before the
\r
167 * trigger level is reached then the task will still receive however many bytes
\r
168 * are actually available. Setting a trigger level of 0 will result in a
\r
169 * trigger level of 1 being used. It is not valid to specify a trigger level
\r
170 * that is greater than the buffer size.
\r
172 * @param pucStreamBufferStorageArea Must point to a uint8_t array that is at
\r
173 * least xBufferSizeBytes + 1 big. This is the array to which streams are
\r
174 * copied when they are written to the stream buffer.
\r
176 * @param pxStaticStreamBuffer Must point to a variable of type
\r
177 * StaticStreamBuffer_t, which will be used to hold the stream buffer's data
\r
180 * @return If the stream buffer is created successfully then a handle to the
\r
181 * created stream buffer is returned. If either pucStreamBufferStorageArea or
\r
182 * pxStaticstreamBuffer are NULL then NULL is returned.
\r
187 * // Used to dimension the array used to hold the streams. The available space
\r
188 * // will actually be one less than this, so 999.
\r
189 #define STORAGE_SIZE_BYTES 1000
\r
191 * // Defines the memory that will actually hold the streams within the stream
\r
193 * static uint8_t ucStorageBuffer[ STORAGE_SIZE_BYTES ];
\r
195 * // The variable used to hold the stream buffer structure.
\r
196 * StaticStreamBuffer_t xStreamBufferStruct;
\r
198 * void MyFunction( void )
\r
200 * StreamBufferHandle_t xStreamBuffer;
\r
201 * const size_t xTriggerLevel = 1;
\r
203 * xStreamBuffer = xStreamBufferCreateStatic( sizeof( ucBufferStorage ),
\r
206 * &xStreamBufferStruct );
\r
208 * // As neither the pucStreamBufferStorageArea or pxStaticStreamBuffer
\r
209 * // parameters were NULL, xStreamBuffer will not be NULL, and can be used to
\r
210 * // reference the created stream buffer in other stream buffer API calls.
\r
212 * // Other code that uses the stream buffer can go here.
\r
216 * \defgroup xStreamBufferCreateStatic xStreamBufferCreateStatic
\r
217 * \ingroup StreamBufferManagement
\r
219 #define xStreamBufferCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, pucStreamBufferStorageArea, pxStaticStreamBuffer ) \
\r
220 xStreamBufferGenericCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, pdFALSE, pucStreamBufferStorageArea, pxStaticStreamBuffer )
\r
226 * size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
\r
227 * const void *pvTxData,
\r
228 * size_t xDataLengthBytes,
\r
229 * TickType_t xTicksToWait );
\r
232 * Sends bytes to a stream buffer. The bytes are copied into the stream buffer.
\r
234 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
235 * implementation (so also the message buffer implementation, as message buffers
\r
236 * are built on top of stream buffers) assumes there is only one task or
\r
237 * interrupt that will write to the buffer (the writer), and only one task or
\r
238 * interrupt that will read from the buffer (the reader). It is safe for the
\r
239 * writer and reader to be different tasks or interrupts, but, unlike other
\r
240 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
241 * multiple different readers. If there are to be multiple different writers
\r
242 * then the application writer must place each call to a writing API function
\r
243 * (such as xStreamBufferSend()) inside a critical section and set the send
\r
244 * block time to 0. Likewise, if there are to be multiple different readers
\r
245 * then the application writer must place each call to a reading API function
\r
246 * (such as xStreamBufferReceive()) inside a critical section and set the receive
\r
249 * Use xStreamBufferSend() to write to a stream buffer from a task. Use
\r
250 * xStreamBufferSendFromISR() to write to a stream buffer from an interrupt
\r
251 * service routine (ISR).
\r
253 * @param xStreamBuffer The handle of the stream buffer to which a stream is
\r
256 * @param pvTxData A pointer to the buffer that holds the bytes to be copied
\r
257 * into the stream buffer.
\r
259 * @param xDataLengthBytes The maximum number of bytes to copy from pvTxData
\r
260 * into the stream buffer.
\r
262 * @param xTicksToWait The maximum amount of time the task should remain in the
\r
263 * Blocked state to wait for enough space to become available in the stream
\r
264 * buffer, should the stream buffer contain too little space to hold the
\r
265 * another xDataLengthBytes bytes. The block time is specified in tick periods,
\r
266 * so the absolute time it represents is dependent on the tick frequency. The
\r
267 * macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds
\r
268 * into a time specified in ticks. Setting xTicksToWait to portMAX_DELAY will
\r
269 * cause the task to wait indefinitely (without timing out), provided
\r
270 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. If a task times out
\r
271 * before it can write all xDataLengthBytes into the buffer it will still write
\r
272 * as many bytes as possible. A task does not use any CPU time when it is in
\r
273 * the blocked state.
\r
275 * @return The number of bytes written to the stream buffer. If a task times
\r
276 * out before it can write all xDataLengthBytes into the buffer it will still
\r
277 * write as many bytes as possible.
\r
281 * void vAFunction( StreamBufferHandle_t xStreamBuffer )
\r
283 * size_t xBytesSent;
\r
284 * uint8_t ucArrayToSend[] = { 0, 1, 2, 3 };
\r
285 * char *pcStringToSend = "String to send";
\r
286 * const TickType_t x100ms = pdMS_TO_TICKS( 100 );
\r
288 * // Send an array to the stream buffer, blocking for a maximum of 100ms to
\r
289 * // wait for enough space to be available in the stream buffer.
\r
290 * xBytesSent = xStreamBufferSend( xStreamBuffer, ( void * ) ucArrayToSend, sizeof( ucArrayToSend ), x100ms );
\r
292 * if( xBytesSent != sizeof( ucArrayToSend ) )
\r
294 * // The call to xStreamBufferSend() times out before there was enough
\r
295 * // space in the buffer for the data to be written, but it did
\r
296 * // successfully write xBytesSent bytes.
\r
299 * // Send the string to the stream buffer. Return immediately if there is not
\r
300 * // enough space in the buffer.
\r
301 * xBytesSent = xStreamBufferSend( xStreamBuffer, ( void * ) pcStringToSend, strlen( pcStringToSend ), 0 );
\r
303 * if( xBytesSent != strlen( pcStringToSend ) )
\r
305 * // The entire string could not be added to the stream buffer because
\r
306 * // there was not enough free space in the buffer, but xBytesSent bytes
\r
307 * // were sent. Could try again to send the remaining bytes.
\r
311 * \defgroup xStreamBufferSend xStreamBufferSend
\r
312 * \ingroup StreamBufferManagement
\r
314 size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
\r
315 const void * pvTxData,
\r
316 size_t xDataLengthBytes,
\r
317 TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
\r
323 * size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
\r
324 * const void *pvTxData,
\r
325 * size_t xDataLengthBytes,
\r
326 * BaseType_t *pxHigherPriorityTaskWoken );
\r
329 * Interrupt safe version of the API function that sends a stream of bytes to
\r
330 * the stream buffer.
\r
332 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
333 * implementation (so also the message buffer implementation, as message buffers
\r
334 * are built on top of stream buffers) assumes there is only one task or
\r
335 * interrupt that will write to the buffer (the writer), and only one task or
\r
336 * interrupt that will read from the buffer (the reader). It is safe for the
\r
337 * writer and reader to be different tasks or interrupts, but, unlike other
\r
338 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
339 * multiple different readers. If there are to be multiple different writers
\r
340 * then the application writer must place each call to a writing API function
\r
341 * (such as xStreamBufferSend()) inside a critical section and set the send
\r
342 * block time to 0. Likewise, if there are to be multiple different readers
\r
343 * then the application writer must place each call to a reading API function
\r
344 * (such as xStreamBufferReceive()) inside a critical section and set the receive
\r
347 * Use xStreamBufferSend() to write to a stream buffer from a task. Use
\r
348 * xStreamBufferSendFromISR() to write to a stream buffer from an interrupt
\r
349 * service routine (ISR).
\r
351 * @param xStreamBuffer The handle of the stream buffer to which a stream is
\r
354 * @param pvTxData A pointer to the data that is to be copied into the stream
\r
357 * @param xDataLengthBytes The maximum number of bytes to copy from pvTxData
\r
358 * into the stream buffer.
\r
360 * @param pxHigherPriorityTaskWoken It is possible that a stream buffer will
\r
361 * have a task blocked on it waiting for data. Calling
\r
362 * xStreamBufferSendFromISR() can make data available, and so cause a task that
\r
363 * was waiting for data to leave the Blocked state. If calling
\r
364 * xStreamBufferSendFromISR() causes a task to leave the Blocked state, and the
\r
365 * unblocked task has a priority higher than the currently executing task (the
\r
366 * task that was interrupted), then, internally, xStreamBufferSendFromISR()
\r
367 * will set *pxHigherPriorityTaskWoken to pdTRUE. If
\r
368 * xStreamBufferSendFromISR() sets this value to pdTRUE, then normally a
\r
369 * context switch should be performed before the interrupt is exited. This will
\r
370 * ensure that the interrupt returns directly to the highest priority Ready
\r
371 * state task. *pxHigherPriorityTaskWoken should be set to pdFALSE before it
\r
372 * is passed into the function. See the example code below for an example.
\r
374 * @return The number of bytes actually written to the stream buffer, which will
\r
375 * be less than xDataLengthBytes if the stream buffer didn't have enough free
\r
376 * space for all the bytes to be written.
\r
380 * // A stream buffer that has already been created.
\r
381 * StreamBufferHandle_t xStreamBuffer;
\r
383 * void vAnInterruptServiceRoutine( void )
\r
385 * size_t xBytesSent;
\r
386 * char *pcStringToSend = "String to send";
\r
387 * BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
\r
389 * // Attempt to send the string to the stream buffer.
\r
390 * xBytesSent = xStreamBufferSendFromISR( xStreamBuffer,
\r
391 * ( void * ) pcStringToSend,
\r
392 * strlen( pcStringToSend ),
\r
393 * &xHigherPriorityTaskWoken );
\r
395 * if( xBytesSent != strlen( pcStringToSend ) )
\r
397 * // There was not enough free space in the stream buffer for the entire
\r
398 * // string to be written, ut xBytesSent bytes were written.
\r
401 * // If xHigherPriorityTaskWoken was set to pdTRUE inside
\r
402 * // xStreamBufferSendFromISR() then a task that has a priority above the
\r
403 * // priority of the currently executing task was unblocked and a context
\r
404 * // switch should be performed to ensure the ISR returns to the unblocked
\r
405 * // task. In most FreeRTOS ports this is done by simply passing
\r
406 * // xHigherPriorityTaskWoken into taskYIELD_FROM_ISR(), which will test the
\r
407 * // variables value, and perform the context switch if necessary. Check the
\r
408 * // documentation for the port in use for port specific instructions.
\r
409 * taskYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
412 * \defgroup xStreamBufferSendFromISR xStreamBufferSendFromISR
\r
413 * \ingroup StreamBufferManagement
\r
415 size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
\r
416 const void * pvTxData,
\r
417 size_t xDataLengthBytes,
\r
418 BaseType_t * const pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
\r
424 * size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
\r
426 * size_t xBufferLengthBytes,
\r
427 * TickType_t xTicksToWait );
\r
430 * Receives bytes from a stream buffer.
\r
432 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
433 * implementation (so also the message buffer implementation, as message buffers
\r
434 * are built on top of stream buffers) assumes there is only one task or
\r
435 * interrupt that will write to the buffer (the writer), and only one task or
\r
436 * interrupt that will read from the buffer (the reader). It is safe for the
\r
437 * writer and reader to be different tasks or interrupts, but, unlike other
\r
438 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
439 * multiple different readers. If there are to be multiple different writers
\r
440 * then the application writer must place each call to a writing API function
\r
441 * (such as xStreamBufferSend()) inside a critical section and set the send
\r
442 * block time to 0. Likewise, if there are to be multiple different readers
\r
443 * then the application writer must place each call to a reading API function
\r
444 * (such as xStreamBufferReceive()) inside a critical section and set the receive
\r
447 * Use xStreamBufferReceive() to read from a stream buffer from a task. Use
\r
448 * xStreamBufferReceiveFromISR() to read from a stream buffer from an
\r
449 * interrupt service routine (ISR).
\r
451 * @param xStreamBuffer The handle of the stream buffer from which bytes are to
\r
454 * @param pvRxData A pointer to the buffer into which the received bytes will be
\r
457 * @param xBufferLengthBytes The length of the buffer pointed to by the
\r
458 * pvRxData parameter. This sets the maximum number of bytes to receive in one
\r
459 * call. xStreamBufferReceive will return as many bytes as possible up to a
\r
460 * maximum set by xBufferLengthBytes.
\r
462 * @param xTicksToWait The maximum amount of time the task should remain in the
\r
463 * Blocked state to wait for data to become available if the stream buffer is
\r
464 * empty. xStreamBufferReceive() will return immediately if xTicksToWait is
\r
465 * zero. The block time is specified in tick periods, so the absolute time it
\r
466 * represents is dependent on the tick frequency. The macro pdMS_TO_TICKS() can
\r
467 * be used to convert a time specified in milliseconds into a time specified in
\r
468 * ticks. Setting xTicksToWait to portMAX_DELAY will cause the task to wait
\r
469 * indefinitely (without timing out), provided INCLUDE_vTaskSuspend is set to 1
\r
470 * in FreeRTOSConfig.h. A task does not use any CPU time when it is in the
\r
473 * @return The number of bytes actually read from the stream buffer, which will
\r
474 * be less than xBufferLengthBytes if the call to xStreamBufferReceive() timed
\r
475 * out before xBufferLengthBytes were available.
\r
479 * void vAFunction( StreamBuffer_t xStreamBuffer )
\r
481 * uint8_t ucRxData[ 20 ];
\r
482 * size_t xReceivedBytes;
\r
483 * const TickType_t xBlockTime = pdMS_TO_TICKS( 20 );
\r
485 * // Receive up to another sizeof( ucRxData ) bytes from the stream buffer.
\r
486 * // Wait in the Blocked state (so not using any CPU processing time) for a
\r
487 * // maximum of 100ms for the full sizeof( ucRxData ) number of bytes to be
\r
489 * xReceivedBytes = xStreamBufferReceive( xStreamBuffer,
\r
490 * ( void * ) ucRxData,
\r
491 * sizeof( ucRxData ),
\r
494 * if( xReceivedBytes > 0 )
\r
496 * // A ucRxData contains another xRecievedBytes bytes of data, which can
\r
497 * // be processed here....
\r
501 * \defgroup xStreamBufferReceive xStreamBufferReceive
\r
502 * \ingroup StreamBufferManagement
\r
504 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
\r
506 size_t xBufferLengthBytes,
\r
507 TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
\r
513 * size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
\r
515 * size_t xBufferLengthBytes,
\r
516 * BaseType_t *pxHigherPriorityTaskWoken );
\r
519 * An interrupt safe version of the API function that receives bytes from a
\r
522 * Use xStreamBufferReceive() to read bytes from a stream buffer from a task.
\r
523 * Use xStreamBufferReceiveFromISR() to read bytes from a stream buffer from an
\r
524 * interrupt service routine (ISR).
\r
526 * @param xStreamBuffer The handle of the stream buffer from which a stream
\r
527 * is being received.
\r
529 * @param pvRxData A pointer to the buffer into which the received bytes are
\r
532 * @param xBufferLengthBytes The length of the buffer pointed to by the
\r
533 * pvRxData parameter. This sets the maximum number of bytes to receive in one
\r
534 * call. xStreamBufferReceive will return as many bytes as possible up to a
\r
535 * maximum set by xBufferLengthBytes.
\r
537 * @param pxHigherPriorityTaskWoken It is possible that a stream buffer will
\r
538 * have a task blocked on it waiting for space to become available. Calling
\r
539 * xStreamBufferReceiveFromISR() can make space available, and so cause a task
\r
540 * that is waiting for space to leave the Blocked state. If calling
\r
541 * xStreamBufferReceiveFromISR() causes a task to leave the Blocked state, and
\r
542 * the unblocked task has a priority higher than the currently executing task
\r
543 * (the task that was interrupted), then, internally,
\r
544 * xStreamBufferReceiveFromISR() will set *pxHigherPriorityTaskWoken to pdTRUE.
\r
545 * If xStreamBufferReceiveFromISR() sets this value to pdTRUE, then normally a
\r
546 * context switch should be performed before the interrupt is exited. That will
\r
547 * ensure the interrupt returns directly to the highest priority Ready state
\r
548 * task. *pxHigherPriorityTaskWoken should be set to pdFALSE before it is
\r
549 * passed into the function. See the code example below for an example.
\r
551 * @return The number of bytes read from the stream buffer, if any.
\r
555 * // A stream buffer that has already been created.
\r
556 * StreamBuffer_t xStreamBuffer;
\r
558 * void vAnInterruptServiceRoutine( void )
\r
560 * uint8_t ucRxData[ 20 ];
\r
561 * size_t xReceivedBytes;
\r
562 * BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
\r
564 * // Receive the next stream from the stream buffer.
\r
565 * xReceivedBytes = xStreamBufferReceiveFromISR( xStreamBuffer,
\r
566 * ( void * ) ucRxData,
\r
567 * sizeof( ucRxData ),
\r
568 * &xHigherPriorityTaskWoken );
\r
570 * if( xReceivedBytes > 0 )
\r
572 * // ucRxData contains xReceivedBytes read from the stream buffer.
\r
573 * // Process the stream here....
\r
576 * // If xHigherPriorityTaskWoken was set to pdTRUE inside
\r
577 * // xStreamBufferReceiveFromISR() then a task that has a priority above the
\r
578 * // priority of the currently executing task was unblocked and a context
\r
579 * // switch should be performed to ensure the ISR returns to the unblocked
\r
580 * // task. In most FreeRTOS ports this is done by simply passing
\r
581 * // xHigherPriorityTaskWoken into taskYIELD_FROM_ISR(), which will test the
\r
582 * // variables value, and perform the context switch if necessary. Check the
\r
583 * // documentation for the port in use for port specific instructions.
\r
584 * taskYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
587 * \defgroup xStreamBufferReceiveFromISR xStreamBufferReceiveFromISR
\r
588 * \ingroup StreamBufferManagement
\r
590 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
\r
592 size_t xBufferLengthBytes,
\r
593 BaseType_t * const pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
\r
599 * void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer );
\r
602 * Deletes a stream buffer that was previously created using a call to
\r
603 * xStreamBufferCreate() or xStreamBufferCreateStatic(). If the stream
\r
604 * buffer was created using dynamic memory (that is, by xStreamBufferCreate()),
\r
605 * then the allocated memory is freed.
\r
607 * A stream buffer handle must not be used after the stream buffer has been
\r
610 * @param xStreamBuffer The handle of the stream buffer to be deleted.
\r
612 * \defgroup vStreamBufferDelete vStreamBufferDelete
\r
613 * \ingroup StreamBufferManagement
\r
615 void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
621 * BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer );
\r
624 * Queries a stream buffer to see if it is full. A stream buffer is full if it
\r
625 * does not have any free space, and therefore cannot accept any more data.
\r
627 * @param xStreamBuffer The handle of the stream buffer being queried.
\r
629 * @return If the stream buffer is full then pdTRUE is returned. Otherwise
\r
630 * pdFALSE is returned.
\r
632 * \defgroup xStreamBufferIsFull xStreamBufferIsFull
\r
633 * \ingroup StreamBufferManagement
\r
635 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
641 * BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer );
\r
644 * Queries a stream buffer to see if it is empty. A stream buffer is empty if
\r
645 * it does not contain any data.
\r
647 * @param xStreamBuffer The handle of the stream buffer being queried.
\r
649 * @return If the stream buffer is empty then pdTRUE is returned. Otherwise
\r
650 * pdFALSE is returned.
\r
652 * \defgroup xStreamBufferIsEmpty xStreamBufferIsEmpty
\r
653 * \ingroup StreamBufferManagement
\r
655 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
661 * BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer );
\r
664 * Resets a stream buffer to its initial, empty, state. Any data that was in
\r
665 * the stream buffer is discarded. A stream buffer can only be reset if there
\r
666 * are no tasks blocked waiting to either send to or receive from the stream
\r
669 * @param xStreamBuffer The handle of the stream buffer being reset.
\r
671 * @return If the stream buffer is reset then pdPASS is returned. If there was
\r
672 * a task blocked waiting to send to or read from the stream buffer then the
\r
673 * stream buffer is not reset and pdFAIL is returned.
\r
675 * \defgroup xStreamBufferReset xStreamBufferReset
\r
676 * \ingroup StreamBufferManagement
\r
678 BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
684 * size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer );
\r
687 * Queries a stream buffer to see how much free space it contains, which is
\r
688 * equal to the amount of data that can be sent to the stream buffer before it
\r
691 * @param xStreamBuffer The handle of the stream buffer being queried.
\r
693 * @return The number of bytes that can be written to the stream buffer before
\r
694 * the stream buffer would be full.
\r
696 * \defgroup xStreamBufferSpacesAvailable xStreamBufferSpacesAvailable
\r
697 * \ingroup StreamBufferManagement
\r
699 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
705 * size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer );
\r
708 * Queries a stream buffer to see how much data it contains, which is equal to
\r
709 * the number of bytes that can be read from the stream buffer before the stream
\r
710 * buffer would be empty.
\r
712 * @param xStreamBuffer The handle of the stream buffer being queried.
\r
714 * @return The number of bytes that can be read from the stream buffer before
\r
715 * the stream buffer would be empty.
\r
717 * \defgroup xStreamBufferBytesAvailable xStreamBufferBytesAvailable
\r
718 * \ingroup StreamBufferManagement
\r
720 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
726 * BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer, size_t xTriggerLevel );
\r
729 * A stream buffer's trigger level is the number of bytes that must be in the
\r
730 * stream buffer before a task that is blocked on the stream buffer to
\r
731 * wait for data is moved out of the blocked state. For example, if a task is
\r
732 * blocked on a read of an empty stream buffer that has a trigger level of 1
\r
733 * then the task will be unblocked when a single byte is written to the buffer
\r
734 * or the task's block time expires. As another example, if a task is blocked
\r
735 * on a read of an empty stream buffer that has a trigger level of 10 then the
\r
736 * task will not be unblocked until the stream buffer contains at least 10 bytes
\r
737 * or the task's block time expires. If a reading task's block time expires
\r
738 * before the trigger level is reached then the task will still receive however
\r
739 * many bytes are actually available. Setting a trigger level of 0 will result
\r
740 * in a trigger level of 1 being used. It is not valid to specify a trigger
\r
741 * level that is greater than the buffer size.
\r
743 * A trigger level is set when the stream buffer is created, and can be modified
\r
744 * using xStreamBufferSetTriggerLevel().
\r
746 * @param xStreamBuffer The handle of the stream buffer being updated.
\r
748 * @param xTriggerLevel The new trigger level for the stream buffer.
\r
750 * @return If xTriggerLevel was less than or equal to the stream buffer's length
\r
751 * then the trigger level will be updated and pdTRUE is returned. Otherwise
\r
752 * pdFALSE is returned.
\r
754 * \defgroup xStreamBufferSetTriggerLevel xStreamBufferSetTriggerLevel
\r
755 * \ingroup StreamBufferManagement
\r
757 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer,
\r
758 size_t xTriggerLevel ) PRIVILEGED_FUNCTION;
\r
764 * BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken );
\r
767 * For advanced users only.
\r
769 * The sbSEND_COMPLETED() macro is called from within the FreeRTOS APIs when
\r
770 * data is sent to a message buffer or stream buffer. If there was a task that
\r
771 * was blocked on the message or stream buffer waiting for data to arrive then
\r
772 * the sbSEND_COMPLETED() macro sends a notification to the task to remove it
\r
773 * from the Blocked state. xStreamBufferSendCompletedFromISR() does the same
\r
774 * thing. It is provided to enable application writers to implement their own
\r
775 * version of sbSEND_COMPLETED(), and MUST NOT BE USED AT ANY OTHER TIME.
\r
777 * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
\r
778 * additional information.
\r
780 * @param xStreamBuffer The handle of the stream buffer to which data was
\r
783 * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
\r
784 * initialised to pdFALSE before it is passed into
\r
785 * xStreamBufferSendCompletedFromISR(). If calling
\r
786 * xStreamBufferSendCompletedFromISR() removes a task from the Blocked state,
\r
787 * and the task has a priority above the priority of the currently running task,
\r
788 * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
\r
789 * context switch should be performed before exiting the ISR.
\r
791 * @return If a task was removed from the Blocked state then pdTRUE is returned.
\r
792 * Otherwise pdFALSE is returned.
\r
794 * \defgroup xStreamBufferSendCompletedFromISR xStreamBufferSendCompletedFromISR
\r
795 * \ingroup StreamBufferManagement
\r
797 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
\r
798 BaseType_t * pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
\r
804 * BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken );
\r
807 * For advanced users only.
\r
809 * The sbRECEIVE_COMPLETED() macro is called from within the FreeRTOS APIs when
\r
810 * data is read out of a message buffer or stream buffer. If there was a task
\r
811 * that was blocked on the message or stream buffer waiting for data to arrive
\r
812 * then the sbRECEIVE_COMPLETED() macro sends a notification to the task to
\r
813 * remove it from the Blocked state. xStreamBufferReceiveCompletedFromISR()
\r
814 * does the same thing. It is provided to enable application writers to
\r
815 * implement their own version of sbRECEIVE_COMPLETED(), and MUST NOT BE USED AT
\r
818 * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
\r
819 * additional information.
\r
821 * @param xStreamBuffer The handle of the stream buffer from which data was
\r
824 * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
\r
825 * initialised to pdFALSE before it is passed into
\r
826 * xStreamBufferReceiveCompletedFromISR(). If calling
\r
827 * xStreamBufferReceiveCompletedFromISR() removes a task from the Blocked state,
\r
828 * and the task has a priority above the priority of the currently running task,
\r
829 * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
\r
830 * context switch should be performed before exiting the ISR.
\r
832 * @return If a task was removed from the Blocked state then pdTRUE is returned.
\r
833 * Otherwise pdFALSE is returned.
\r
835 * \defgroup xStreamBufferReceiveCompletedFromISR xStreamBufferReceiveCompletedFromISR
\r
836 * \ingroup StreamBufferManagement
\r
838 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
\r
839 BaseType_t * pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
\r
841 /* Functions below here are not part of the public API. */
\r
842 StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
\r
843 size_t xTriggerLevelBytes,
\r
844 BaseType_t xIsMessageBuffer ) PRIVILEGED_FUNCTION;
\r
846 StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
\r
847 size_t xTriggerLevelBytes,
\r
848 BaseType_t xIsMessageBuffer,
\r
849 uint8_t * const pucStreamBufferStorageArea,
\r
850 StaticStreamBuffer_t * const pxStaticStreamBuffer ) PRIVILEGED_FUNCTION;
\r
852 size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
854 #if ( configUSE_TRACE_FACILITY == 1 )
\r
855 void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
\r
856 UBaseType_t uxStreamBufferNumber ) PRIVILEGED_FUNCTION;
\r
857 UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
858 uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
\r
862 #if defined( __cplusplus )
\r
867 #endif /* !defined( STREAM_BUFFER_H ) */
\r