[RTOS Stream Buffer API]
size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
TickType_t xTicksToWait );
Receives bytes from a stream buffer.
NOTE: Uniquely among FreeRTOS objects, the stream buffer implementation (so also the message buffer implementation, as message buffers are built on top of stream buffers) assumes there is only one task or interrupt that will write to the buffer (the writer), and only one task or interrupt that will read from the buffer (the reader). It is safe for the writer and reader to be different tasks or interrupts, but, unlike other FreeRTOS objects, it is not safe to have multiple different writers or multiple different readers. If there are to be multiple different writers then the application writer must place each call to a writing API function (such as xStreamBufferSend()) inside a critical section and use a send block time of 0. Likewise, if there are to be multiple different readers then the application writer must place each call to a reading API function (such as xStreamBufferReceive()) inside a critical section and use a receive block time of 0.
Use xStreamBufferReceive() to read from a stream buffer from a task. Use xStreamBufferReceiveFromISR() to read from a stream buffer from an interrupt service routine (ISR).
Stream buffer functionality is enabled by including the FreeRTOS/source/stream_buffer.c source file in the build.
xStreamBuffer The handle of the stream buffer from which bytes are to be received. pvRxData A pointer to the buffer into which the received bytes will be copied. xBufferLengthBytes The length of the buffer pointed to by the pvRxData parameter. This sets the maximum number of bytes to receive in one call. xStreamBufferReceive will return as many bytes as possible up to a maximum set by xBufferLengthBytes. xTicksToWait The maximum amount of time the task should remain in the Blocked state to wait for data to become available if the stream buffer is empty. xStreamBufferReceive() will return immediately if xTicksToWait is zero. The block time is specified in tick periods, so the absolute time it represents is dependent on the tick frequency. The macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds into a time specified in ticks. Setting xTicksToWait to portMAX_DELAY will cause the task to wait indefinitely (without timing out), provided INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. A task does not use any CPU time when it is in the Blocked state.
- The number of bytes actually read from the stream buffer, which will be less than xBufferLengthBytes if the call to xStreamBufferReceive() timed out before xBufferLengthBytes were available.
void vAFunction( StreamBuffer_t xStreamBuffer )
uint8_t ucRxData[ 20 ];
const TickType_t xBlockTime = pdMS_TO_TICKS( 20 );
/* Receive up to another sizeof( ucRxData ) bytes from the stream buffer.
Wait in the Blocked state (so not using any CPU processing time) for a
maximum of 100ms for the full sizeof( ucRxData ) number of bytes to be
xReceivedBytes = xStreamBufferReceive( xStreamBuffer,
( void * ) ucRxData,
sizeof( ucRxData ),
if( xReceivedBytes > 0 )
/* A ucRxData contains another xRecievedBytes bytes of data, which can
be processed here.... */