]> begriffs open source - freertos/blob - include/stream_buffer.h
Fix build failure introduced in PR #597 (#629)
[freertos] / include / stream_buffer.h
1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28
29 /*
30  * Stream buffers are used to send a continuous stream of data from one task or
31  * interrupt to another.  Their implementation is light weight, making them
32  * particularly suited for interrupt to task and core to core communication
33  * scenarios.
34  *
35  * ***NOTE***:  Uniquely among FreeRTOS objects, the stream buffer
36  * implementation (so also the message buffer implementation, as message buffers
37  * are built on top of stream buffers) assumes there is only one task or
38  * interrupt that will write to the buffer (the writer), and only one task or
39  * interrupt that will read from the buffer (the reader).  It is safe for the
40  * writer and reader to be different tasks or interrupts, but, unlike other
41  * FreeRTOS objects, it is not safe to have multiple different writers or
42  * multiple different readers.  If there are to be multiple different writers
43  * then the application writer must place each call to a writing API function
44  * (such as xStreamBufferSend()) inside a critical section and set the send
45  * block time to 0.  Likewise, if there are to be multiple different readers
46  * then the application writer must place each call to a reading API function
47  * (such as xStreamBufferReceive()) inside a critical section section and set the
48  * receive block time to 0.
49  *
50  */
51
52 #ifndef STREAM_BUFFER_H
53 #define STREAM_BUFFER_H
54
55 #ifndef INC_FREERTOS_H
56     #error "include FreeRTOS.h must appear in source files before include stream_buffer.h"
57 #endif
58
59 /* *INDENT-OFF* */
60 #if defined( __cplusplus )
61     extern "C" {
62 #endif
63 /* *INDENT-ON* */
64
65 /**
66  * Type by which stream buffers are referenced.  For example, a call to
67  * xStreamBufferCreate() returns an StreamBufferHandle_t variable that can
68  * then be used as a parameter to xStreamBufferSend(), xStreamBufferReceive(),
69  * etc.
70  */
71 struct StreamBufferDef_t;
72 typedef struct StreamBufferDef_t * StreamBufferHandle_t;
73
74 /**
75  *  Type used as a stream buffer's optional callback.
76  */
77 typedef void (* StreamBufferCallbackFunction_t)( StreamBufferHandle_t xStreamBuffer,
78                                                  BaseType_t xIsInsideISR,
79                                                  BaseType_t * const pxHigherPriorityTaskWoken );
80
81 /**
82  * stream_buffer.h
83  *
84  * @code{c}
85  * StreamBufferHandle_t xStreamBufferCreate( size_t xBufferSizeBytes, size_t xTriggerLevelBytes );
86  * @endcode
87  *
88  * Creates a new stream buffer using dynamically allocated memory.  See
89  * xStreamBufferCreateStatic() for a version that uses statically allocated
90  * memory (memory that is allocated at compile time).
91  *
92  * configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 or left undefined in
93  * FreeRTOSConfig.h for xStreamBufferCreate() to be available.
94  *
95  * @param xBufferSizeBytes The total number of bytes the stream buffer will be
96  * able to hold at any one time.
97  *
98  * @param xTriggerLevelBytes The number of bytes that must be in the stream
99  * buffer before a task that is blocked on the stream buffer to wait for data is
100  * moved out of the blocked state.  For example, if a task is blocked on a read
101  * of an empty stream buffer that has a trigger level of 1 then the task will be
102  * unblocked when a single byte is written to the buffer or the task's block
103  * time expires.  As another example, if a task is blocked on a read of an empty
104  * stream buffer that has a trigger level of 10 then the task will not be
105  * unblocked until the stream buffer contains at least 10 bytes or the task's
106  * block time expires.  If a reading task's block time expires before the
107  * trigger level is reached then the task will still receive however many bytes
108  * are actually available.  Setting a trigger level of 0 will result in a
109  * trigger level of 1 being used.  It is not valid to specify a trigger level
110  * that is greater than the buffer size.
111  *
112  * @param pxSendCompletedCallback Callback invoked when number of bytes at least equal to
113  * trigger level is sent to the stream buffer. If the parameter is NULL, it will use the default
114  * implementation provided by sbSEND_COMPLETED macro. To enable the callback,
115  * configUSE_SB_COMPLETED_CALLBACK must be set to 1 in FreeRTOSConfig.h.
116  *
117  * @param pxReceiveCompletedCallback Callback invoked when more than zero bytes are read from a
118  * stream buffer. If the parameter is NULL, it will use the default
119  * implementation provided by sbRECEIVE_COMPLETED macro. To enable the callback,
120  * configUSE_SB_COMPLETED_CALLBACK must be set to 1 in FreeRTOSConfig.h.
121  *
122  * @return If NULL is returned, then the stream buffer cannot be created
123  * because there is insufficient heap memory available for FreeRTOS to allocate
124  * the stream buffer data structures and storage area.  A non-NULL value being
125  * returned indicates that the stream buffer has been created successfully -
126  * the returned value should be stored as the handle to the created stream
127  * buffer.
128  *
129  * Example use:
130  * @code{c}
131  *
132  * void vAFunction( void )
133  * {
134  * StreamBufferHandle_t xStreamBuffer;
135  * const size_t xStreamBufferSizeBytes = 100, xTriggerLevel = 10;
136  *
137  *  // Create a stream buffer that can hold 100 bytes.  The memory used to hold
138  *  // both the stream buffer structure and the data in the stream buffer is
139  *  // allocated dynamically.
140  *  xStreamBuffer = xStreamBufferCreate( xStreamBufferSizeBytes, xTriggerLevel );
141  *
142  *  if( xStreamBuffer == NULL )
143  *  {
144  *      // There was not enough heap memory space available to create the
145  *      // stream buffer.
146  *  }
147  *  else
148  *  {
149  *      // The stream buffer was created successfully and can now be used.
150  *  }
151  * }
152  * @endcode
153  * \defgroup xStreamBufferCreate xStreamBufferCreate
154  * \ingroup StreamBufferManagement
155  */
156
157 #define xStreamBufferCreate( xBufferSizeBytes, xTriggerLevelBytes ) \
158     xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), pdFALSE, NULL, NULL )
159
160 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
161     #define xStreamBufferCreateWithCallback( xBufferSizeBytes, xTriggerLevelBytes, pxSendCompletedCallback, pxReceiveCompletedCallback ) \
162     xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), pdFALSE, ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
163 #endif
164
165 /**
166  * stream_buffer.h
167  *
168  * @code{c}
169  * StreamBufferHandle_t xStreamBufferCreateStatic( size_t xBufferSizeBytes,
170  *                                              size_t xTriggerLevelBytes,
171  *                                              uint8_t *pucStreamBufferStorageArea,
172  *                                              StaticStreamBuffer_t *pxStaticStreamBuffer );
173  * @endcode
174  * Creates a new stream buffer using statically allocated memory.  See
175  * xStreamBufferCreate() for a version that uses dynamically allocated memory.
176  *
177  * configSUPPORT_STATIC_ALLOCATION must be set to 1 in FreeRTOSConfig.h for
178  * xStreamBufferCreateStatic() to be available.
179  *
180  * @param xBufferSizeBytes The size, in bytes, of the buffer pointed to by the
181  * pucStreamBufferStorageArea parameter.
182  *
183  * @param xTriggerLevelBytes The number of bytes that must be in the stream
184  * buffer before a task that is blocked on the stream buffer to wait for data is
185  * moved out of the blocked state.  For example, if a task is blocked on a read
186  * of an empty stream buffer that has a trigger level of 1 then the task will be
187  * unblocked when a single byte is written to the buffer or the task's block
188  * time expires.  As another example, if a task is blocked on a read of an empty
189  * stream buffer that has a trigger level of 10 then the task will not be
190  * unblocked until the stream buffer contains at least 10 bytes or the task's
191  * block time expires.  If a reading task's block time expires before the
192  * trigger level is reached then the task will still receive however many bytes
193  * are actually available.  Setting a trigger level of 0 will result in a
194  * trigger level of 1 being used.  It is not valid to specify a trigger level
195  * that is greater than the buffer size.
196  *
197  * @param pucStreamBufferStorageArea Must point to a uint8_t array that is at
198  * least xBufferSizeBytes big.  This is the array to which streams are
199  * copied when they are written to the stream buffer.
200  *
201  * @param pxStaticStreamBuffer Must point to a variable of type
202  * StaticStreamBuffer_t, which will be used to hold the stream buffer's data
203  * structure.
204  *
205  * @param pxSendCompletedCallback Callback invoked when number of bytes at least equal to
206  * trigger level is sent to the stream buffer. If the parameter is NULL, it will use the default
207  * implementation provided by sbSEND_COMPLETED macro. To enable the callback,
208  * configUSE_SB_COMPLETED_CALLBACK must be set to 1 in FreeRTOSConfig.h.
209  *
210  * @param pxReceiveCompletedCallback Callback invoked when more than zero bytes are read from a
211  * stream buffer. If the parameter is NULL, it will use the default
212  * implementation provided by sbRECEIVE_COMPLETED macro. To enable the callback,
213  * configUSE_SB_COMPLETED_CALLBACK must be set to 1 in FreeRTOSConfig.h.
214  *
215  * @return If the stream buffer is created successfully then a handle to the
216  * created stream buffer is returned. If either pucStreamBufferStorageArea or
217  * pxStaticstreamBuffer are NULL then NULL is returned.
218  *
219  * Example use:
220  * @code{c}
221  *
222  * // Used to dimension the array used to hold the streams.  The available space
223  * // will actually be one less than this, so 999.
224  #define STORAGE_SIZE_BYTES 1000
225  *
226  * // Defines the memory that will actually hold the streams within the stream
227  * // buffer.
228  * static uint8_t ucStorageBuffer[ STORAGE_SIZE_BYTES ];
229  *
230  * // The variable used to hold the stream buffer structure.
231  * StaticStreamBuffer_t xStreamBufferStruct;
232  *
233  * void MyFunction( void )
234  * {
235  * StreamBufferHandle_t xStreamBuffer;
236  * const size_t xTriggerLevel = 1;
237  *
238  *  xStreamBuffer = xStreamBufferCreateStatic( sizeof( ucStorageBuffer ),
239  *                                             xTriggerLevel,
240  *                                             ucStorageBuffer,
241  *                                             &xStreamBufferStruct );
242  *
243  *  // As neither the pucStreamBufferStorageArea or pxStaticStreamBuffer
244  *  // parameters were NULL, xStreamBuffer will not be NULL, and can be used to
245  *  // reference the created stream buffer in other stream buffer API calls.
246  *
247  *  // Other code that uses the stream buffer can go here.
248  * }
249  *
250  * @endcode
251  * \defgroup xStreamBufferCreateStatic xStreamBufferCreateStatic
252  * \ingroup StreamBufferManagement
253  */
254
255 #define xStreamBufferCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, pucStreamBufferStorageArea, pxStaticStreamBuffer ) \
256     xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), pdFALSE, ( pucStreamBufferStorageArea ), ( pxStaticStreamBuffer ), NULL, NULL )
257
258 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
259     #define xStreamBufferCreateStaticWithCallback( xBufferSizeBytes, xTriggerLevelBytes, pucStreamBufferStorageArea, pxStaticStreamBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback ) \
260     xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), ( xTriggerLevelBytes ), pdFALSE, ( pucStreamBufferStorageArea ), ( pxStaticStreamBuffer ), ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
261 #endif
262
263 /**
264  * stream_buffer.h
265  *
266  * @code{c}
267  * size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
268  *                        const void *pvTxData,
269  *                        size_t xDataLengthBytes,
270  *                        TickType_t xTicksToWait );
271  * @endcode
272  *
273  * Sends bytes to a stream buffer.  The bytes are copied into the stream buffer.
274  *
275  * ***NOTE***:  Uniquely among FreeRTOS objects, the stream buffer
276  * implementation (so also the message buffer implementation, as message buffers
277  * are built on top of stream buffers) assumes there is only one task or
278  * interrupt that will write to the buffer (the writer), and only one task or
279  * interrupt that will read from the buffer (the reader).  It is safe for the
280  * writer and reader to be different tasks or interrupts, but, unlike other
281  * FreeRTOS objects, it is not safe to have multiple different writers or
282  * multiple different readers.  If there are to be multiple different writers
283  * then the application writer must place each call to a writing API function
284  * (such as xStreamBufferSend()) inside a critical section and set the send
285  * block time to 0.  Likewise, if there are to be multiple different readers
286  * then the application writer must place each call to a reading API function
287  * (such as xStreamBufferReceive()) inside a critical section and set the receive
288  * block time to 0.
289  *
290  * Use xStreamBufferSend() to write to a stream buffer from a task.  Use
291  * xStreamBufferSendFromISR() to write to a stream buffer from an interrupt
292  * service routine (ISR).
293  *
294  * @param xStreamBuffer The handle of the stream buffer to which a stream is
295  * being sent.
296  *
297  * @param pvTxData A pointer to the buffer that holds the bytes to be copied
298  * into the stream buffer.
299  *
300  * @param xDataLengthBytes   The maximum number of bytes to copy from pvTxData
301  * into the stream buffer.
302  *
303  * @param xTicksToWait The maximum amount of time the task should remain in the
304  * Blocked state to wait for enough space to become available in the stream
305  * buffer, should the stream buffer contain too little space to hold the
306  * another xDataLengthBytes bytes.  The block time is specified in tick periods,
307  * so the absolute time it represents is dependent on the tick frequency.  The
308  * macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds
309  * into a time specified in ticks.  Setting xTicksToWait to portMAX_DELAY will
310  * cause the task to wait indefinitely (without timing out), provided
311  * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h.  If a task times out
312  * before it can write all xDataLengthBytes into the buffer it will still write
313  * as many bytes as possible.  A task does not use any CPU time when it is in
314  * the blocked state.
315  *
316  * @return The number of bytes written to the stream buffer.  If a task times
317  * out before it can write all xDataLengthBytes into the buffer it will still
318  * write as many bytes as possible.
319  *
320  * Example use:
321  * @code{c}
322  * void vAFunction( StreamBufferHandle_t xStreamBuffer )
323  * {
324  * size_t xBytesSent;
325  * uint8_t ucArrayToSend[] = { 0, 1, 2, 3 };
326  * char *pcStringToSend = "String to send";
327  * const TickType_t x100ms = pdMS_TO_TICKS( 100 );
328  *
329  *  // Send an array to the stream buffer, blocking for a maximum of 100ms to
330  *  // wait for enough space to be available in the stream buffer.
331  *  xBytesSent = xStreamBufferSend( xStreamBuffer, ( void * ) ucArrayToSend, sizeof( ucArrayToSend ), x100ms );
332  *
333  *  if( xBytesSent != sizeof( ucArrayToSend ) )
334  *  {
335  *      // The call to xStreamBufferSend() times out before there was enough
336  *      // space in the buffer for the data to be written, but it did
337  *      // successfully write xBytesSent bytes.
338  *  }
339  *
340  *  // Send the string to the stream buffer.  Return immediately if there is not
341  *  // enough space in the buffer.
342  *  xBytesSent = xStreamBufferSend( xStreamBuffer, ( void * ) pcStringToSend, strlen( pcStringToSend ), 0 );
343  *
344  *  if( xBytesSent != strlen( pcStringToSend ) )
345  *  {
346  *      // The entire string could not be added to the stream buffer because
347  *      // there was not enough free space in the buffer, but xBytesSent bytes
348  *      // were sent.  Could try again to send the remaining bytes.
349  *  }
350  * }
351  * @endcode
352  * \defgroup xStreamBufferSend xStreamBufferSend
353  * \ingroup StreamBufferManagement
354  */
355 size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
356                           const void * pvTxData,
357                           size_t xDataLengthBytes,
358                           TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
359
360 /**
361  * stream_buffer.h
362  *
363  * @code{c}
364  * size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
365  *                               const void *pvTxData,
366  *                               size_t xDataLengthBytes,
367  *                               BaseType_t *pxHigherPriorityTaskWoken );
368  * @endcode
369  *
370  * Interrupt safe version of the API function that sends a stream of bytes to
371  * the stream buffer.
372  *
373  * ***NOTE***:  Uniquely among FreeRTOS objects, the stream buffer
374  * implementation (so also the message buffer implementation, as message buffers
375  * are built on top of stream buffers) assumes there is only one task or
376  * interrupt that will write to the buffer (the writer), and only one task or
377  * interrupt that will read from the buffer (the reader).  It is safe for the
378  * writer and reader to be different tasks or interrupts, but, unlike other
379  * FreeRTOS objects, it is not safe to have multiple different writers or
380  * multiple different readers.  If there are to be multiple different writers
381  * then the application writer must place each call to a writing API function
382  * (such as xStreamBufferSend()) inside a critical section and set the send
383  * block time to 0.  Likewise, if there are to be multiple different readers
384  * then the application writer must place each call to a reading API function
385  * (such as xStreamBufferReceive()) inside a critical section and set the receive
386  * block time to 0.
387  *
388  * Use xStreamBufferSend() to write to a stream buffer from a task.  Use
389  * xStreamBufferSendFromISR() to write to a stream buffer from an interrupt
390  * service routine (ISR).
391  *
392  * @param xStreamBuffer The handle of the stream buffer to which a stream is
393  * being sent.
394  *
395  * @param pvTxData A pointer to the data that is to be copied into the stream
396  * buffer.
397  *
398  * @param xDataLengthBytes The maximum number of bytes to copy from pvTxData
399  * into the stream buffer.
400  *
401  * @param pxHigherPriorityTaskWoken  It is possible that a stream buffer will
402  * have a task blocked on it waiting for data.  Calling
403  * xStreamBufferSendFromISR() can make data available, and so cause a task that
404  * was waiting for data to leave the Blocked state.  If calling
405  * xStreamBufferSendFromISR() causes a task to leave the Blocked state, and the
406  * unblocked task has a priority higher than the currently executing task (the
407  * task that was interrupted), then, internally, xStreamBufferSendFromISR()
408  * will set *pxHigherPriorityTaskWoken to pdTRUE.  If
409  * xStreamBufferSendFromISR() sets this value to pdTRUE, then normally a
410  * context switch should be performed before the interrupt is exited.  This will
411  * ensure that the interrupt returns directly to the highest priority Ready
412  * state task.  *pxHigherPriorityTaskWoken should be set to pdFALSE before it
413  * is passed into the function.  See the example code below for an example.
414  *
415  * @return The number of bytes actually written to the stream buffer, which will
416  * be less than xDataLengthBytes if the stream buffer didn't have enough free
417  * space for all the bytes to be written.
418  *
419  * Example use:
420  * @code{c}
421  * // A stream buffer that has already been created.
422  * StreamBufferHandle_t xStreamBuffer;
423  *
424  * void vAnInterruptServiceRoutine( void )
425  * {
426  * size_t xBytesSent;
427  * char *pcStringToSend = "String to send";
428  * BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
429  *
430  *  // Attempt to send the string to the stream buffer.
431  *  xBytesSent = xStreamBufferSendFromISR( xStreamBuffer,
432  *                                         ( void * ) pcStringToSend,
433  *                                         strlen( pcStringToSend ),
434  *                                         &xHigherPriorityTaskWoken );
435  *
436  *  if( xBytesSent != strlen( pcStringToSend ) )
437  *  {
438  *      // There was not enough free space in the stream buffer for the entire
439  *      // string to be written, ut xBytesSent bytes were written.
440  *  }
441  *
442  *  // If xHigherPriorityTaskWoken was set to pdTRUE inside
443  *  // xStreamBufferSendFromISR() then a task that has a priority above the
444  *  // priority of the currently executing task was unblocked and a context
445  *  // switch should be performed to ensure the ISR returns to the unblocked
446  *  // task.  In most FreeRTOS ports this is done by simply passing
447  *  // xHigherPriorityTaskWoken into portYIELD_FROM_ISR(), which will test the
448  *  // variables value, and perform the context switch if necessary.  Check the
449  *  // documentation for the port in use for port specific instructions.
450  *  portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
451  * }
452  * @endcode
453  * \defgroup xStreamBufferSendFromISR xStreamBufferSendFromISR
454  * \ingroup StreamBufferManagement
455  */
456 size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
457                                  const void * pvTxData,
458                                  size_t xDataLengthBytes,
459                                  BaseType_t * const pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
460
461 /**
462  * stream_buffer.h
463  *
464  * @code{c}
465  * size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
466  *                           void *pvRxData,
467  *                           size_t xBufferLengthBytes,
468  *                           TickType_t xTicksToWait );
469  * @endcode
470  *
471  * Receives bytes from a stream buffer.
472  *
473  * ***NOTE***:  Uniquely among FreeRTOS objects, the stream buffer
474  * implementation (so also the message buffer implementation, as message buffers
475  * are built on top of stream buffers) assumes there is only one task or
476  * interrupt that will write to the buffer (the writer), and only one task or
477  * interrupt that will read from the buffer (the reader).  It is safe for the
478  * writer and reader to be different tasks or interrupts, but, unlike other
479  * FreeRTOS objects, it is not safe to have multiple different writers or
480  * multiple different readers.  If there are to be multiple different writers
481  * then the application writer must place each call to a writing API function
482  * (such as xStreamBufferSend()) inside a critical section and set the send
483  * block time to 0.  Likewise, if there are to be multiple different readers
484  * then the application writer must place each call to a reading API function
485  * (such as xStreamBufferReceive()) inside a critical section and set the receive
486  * block time to 0.
487  *
488  * Use xStreamBufferReceive() to read from a stream buffer from a task.  Use
489  * xStreamBufferReceiveFromISR() to read from a stream buffer from an
490  * interrupt service routine (ISR).
491  *
492  * @param xStreamBuffer The handle of the stream buffer from which bytes are to
493  * be received.
494  *
495  * @param pvRxData A pointer to the buffer into which the received bytes will be
496  * copied.
497  *
498  * @param xBufferLengthBytes The length of the buffer pointed to by the
499  * pvRxData parameter.  This sets the maximum number of bytes to receive in one
500  * call.  xStreamBufferReceive will return as many bytes as possible up to a
501  * maximum set by xBufferLengthBytes.
502  *
503  * @param xTicksToWait The maximum amount of time the task should remain in the
504  * Blocked state to wait for data to become available if the stream buffer is
505  * empty.  xStreamBufferReceive() will return immediately if xTicksToWait is
506  * zero.  The block time is specified in tick periods, so the absolute time it
507  * represents is dependent on the tick frequency.  The macro pdMS_TO_TICKS() can
508  * be used to convert a time specified in milliseconds into a time specified in
509  * ticks.  Setting xTicksToWait to portMAX_DELAY will cause the task to wait
510  * indefinitely (without timing out), provided INCLUDE_vTaskSuspend is set to 1
511  * in FreeRTOSConfig.h.  A task does not use any CPU time when it is in the
512  * Blocked state.
513  *
514  * @return The number of bytes actually read from the stream buffer, which will
515  * be less than xBufferLengthBytes if the call to xStreamBufferReceive() timed
516  * out before xBufferLengthBytes were available.
517  *
518  * Example use:
519  * @code{c}
520  * void vAFunction( StreamBuffer_t xStreamBuffer )
521  * {
522  * uint8_t ucRxData[ 20 ];
523  * size_t xReceivedBytes;
524  * const TickType_t xBlockTime = pdMS_TO_TICKS( 20 );
525  *
526  *  // Receive up to another sizeof( ucRxData ) bytes from the stream buffer.
527  *  // Wait in the Blocked state (so not using any CPU processing time) for a
528  *  // maximum of 100ms for the full sizeof( ucRxData ) number of bytes to be
529  *  // available.
530  *  xReceivedBytes = xStreamBufferReceive( xStreamBuffer,
531  *                                         ( void * ) ucRxData,
532  *                                         sizeof( ucRxData ),
533  *                                         xBlockTime );
534  *
535  *  if( xReceivedBytes > 0 )
536  *  {
537  *      // A ucRxData contains another xReceivedBytes bytes of data, which can
538  *      // be processed here....
539  *  }
540  * }
541  * @endcode
542  * \defgroup xStreamBufferReceive xStreamBufferReceive
543  * \ingroup StreamBufferManagement
544  */
545 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
546                              void * pvRxData,
547                              size_t xBufferLengthBytes,
548                              TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;
549
550 /**
551  * stream_buffer.h
552  *
553  * @code{c}
554  * size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
555  *                                  void *pvRxData,
556  *                                  size_t xBufferLengthBytes,
557  *                                  BaseType_t *pxHigherPriorityTaskWoken );
558  * @endcode
559  *
560  * An interrupt safe version of the API function that receives bytes from a
561  * stream buffer.
562  *
563  * Use xStreamBufferReceive() to read bytes from a stream buffer from a task.
564  * Use xStreamBufferReceiveFromISR() to read bytes from a stream buffer from an
565  * interrupt service routine (ISR).
566  *
567  * @param xStreamBuffer The handle of the stream buffer from which a stream
568  * is being received.
569  *
570  * @param pvRxData A pointer to the buffer into which the received bytes are
571  * copied.
572  *
573  * @param xBufferLengthBytes The length of the buffer pointed to by the
574  * pvRxData parameter.  This sets the maximum number of bytes to receive in one
575  * call.  xStreamBufferReceive will return as many bytes as possible up to a
576  * maximum set by xBufferLengthBytes.
577  *
578  * @param pxHigherPriorityTaskWoken  It is possible that a stream buffer will
579  * have a task blocked on it waiting for space to become available.  Calling
580  * xStreamBufferReceiveFromISR() can make space available, and so cause a task
581  * that is waiting for space to leave the Blocked state.  If calling
582  * xStreamBufferReceiveFromISR() causes a task to leave the Blocked state, and
583  * the unblocked task has a priority higher than the currently executing task
584  * (the task that was interrupted), then, internally,
585  * xStreamBufferReceiveFromISR() will set *pxHigherPriorityTaskWoken to pdTRUE.
586  * If xStreamBufferReceiveFromISR() sets this value to pdTRUE, then normally a
587  * context switch should be performed before the interrupt is exited.  That will
588  * ensure the interrupt returns directly to the highest priority Ready state
589  * task.  *pxHigherPriorityTaskWoken should be set to pdFALSE before it is
590  * passed into the function.  See the code example below for an example.
591  *
592  * @return The number of bytes read from the stream buffer, if any.
593  *
594  * Example use:
595  * @code{c}
596  * // A stream buffer that has already been created.
597  * StreamBuffer_t xStreamBuffer;
598  *
599  * void vAnInterruptServiceRoutine( void )
600  * {
601  * uint8_t ucRxData[ 20 ];
602  * size_t xReceivedBytes;
603  * BaseType_t xHigherPriorityTaskWoken = pdFALSE;  // Initialised to pdFALSE.
604  *
605  *  // Receive the next stream from the stream buffer.
606  *  xReceivedBytes = xStreamBufferReceiveFromISR( xStreamBuffer,
607  *                                                ( void * ) ucRxData,
608  *                                                sizeof( ucRxData ),
609  *                                                &xHigherPriorityTaskWoken );
610  *
611  *  if( xReceivedBytes > 0 )
612  *  {
613  *      // ucRxData contains xReceivedBytes read from the stream buffer.
614  *      // Process the stream here....
615  *  }
616  *
617  *  // If xHigherPriorityTaskWoken was set to pdTRUE inside
618  *  // xStreamBufferReceiveFromISR() then a task that has a priority above the
619  *  // priority of the currently executing task was unblocked and a context
620  *  // switch should be performed to ensure the ISR returns to the unblocked
621  *  // task.  In most FreeRTOS ports this is done by simply passing
622  *  // xHigherPriorityTaskWoken into portYIELD_FROM_ISR(), which will test the
623  *  // variables value, and perform the context switch if necessary.  Check the
624  *  // documentation for the port in use for port specific instructions.
625  *  portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
626  * }
627  * @endcode
628  * \defgroup xStreamBufferReceiveFromISR xStreamBufferReceiveFromISR
629  * \ingroup StreamBufferManagement
630  */
631 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
632                                     void * pvRxData,
633                                     size_t xBufferLengthBytes,
634                                     BaseType_t * const pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
635
636 /**
637  * stream_buffer.h
638  *
639  * @code{c}
640  * void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer );
641  * @endcode
642  *
643  * Deletes a stream buffer that was previously created using a call to
644  * xStreamBufferCreate() or xStreamBufferCreateStatic().  If the stream
645  * buffer was created using dynamic memory (that is, by xStreamBufferCreate()),
646  * then the allocated memory is freed.
647  *
648  * A stream buffer handle must not be used after the stream buffer has been
649  * deleted.
650  *
651  * @param xStreamBuffer The handle of the stream buffer to be deleted.
652  *
653  * \defgroup vStreamBufferDelete vStreamBufferDelete
654  * \ingroup StreamBufferManagement
655  */
656 void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
657
658 /**
659  * stream_buffer.h
660  *
661  * @code{c}
662  * BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer );
663  * @endcode
664  *
665  * Queries a stream buffer to see if it is full.  A stream buffer is full if it
666  * does not have any free space, and therefore cannot accept any more data.
667  *
668  * @param xStreamBuffer The handle of the stream buffer being queried.
669  *
670  * @return If the stream buffer is full then pdTRUE is returned.  Otherwise
671  * pdFALSE is returned.
672  *
673  * \defgroup xStreamBufferIsFull xStreamBufferIsFull
674  * \ingroup StreamBufferManagement
675  */
676 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
677
678 /**
679  * stream_buffer.h
680  *
681  * @code{c}
682  * BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer );
683  * @endcode
684  *
685  * Queries a stream buffer to see if it is empty.  A stream buffer is empty if
686  * it does not contain any data.
687  *
688  * @param xStreamBuffer The handle of the stream buffer being queried.
689  *
690  * @return If the stream buffer is empty then pdTRUE is returned.  Otherwise
691  * pdFALSE is returned.
692  *
693  * \defgroup xStreamBufferIsEmpty xStreamBufferIsEmpty
694  * \ingroup StreamBufferManagement
695  */
696 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
697
698 /**
699  * stream_buffer.h
700  *
701  * @code{c}
702  * BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer );
703  * @endcode
704  *
705  * Resets a stream buffer to its initial, empty, state.  Any data that was in
706  * the stream buffer is discarded.  A stream buffer can only be reset if there
707  * are no tasks blocked waiting to either send to or receive from the stream
708  * buffer.
709  *
710  * @param xStreamBuffer The handle of the stream buffer being reset.
711  *
712  * @return If the stream buffer is reset then pdPASS is returned.  If there was
713  * a task blocked waiting to send to or read from the stream buffer then the
714  * stream buffer is not reset and pdFAIL is returned.
715  *
716  * \defgroup xStreamBufferReset xStreamBufferReset
717  * \ingroup StreamBufferManagement
718  */
719 BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
720
721 /**
722  * stream_buffer.h
723  *
724  * @code{c}
725  * size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer );
726  * @endcode
727  *
728  * Queries a stream buffer to see how much free space it contains, which is
729  * equal to the amount of data that can be sent to the stream buffer before it
730  * is full.
731  *
732  * @param xStreamBuffer The handle of the stream buffer being queried.
733  *
734  * @return The number of bytes that can be written to the stream buffer before
735  * the stream buffer would be full.
736  *
737  * \defgroup xStreamBufferSpacesAvailable xStreamBufferSpacesAvailable
738  * \ingroup StreamBufferManagement
739  */
740 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
741
742 /**
743  * stream_buffer.h
744  *
745  * @code{c}
746  * size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer );
747  * @endcode
748  *
749  * Queries a stream buffer to see how much data it contains, which is equal to
750  * the number of bytes that can be read from the stream buffer before the stream
751  * buffer would be empty.
752  *
753  * @param xStreamBuffer The handle of the stream buffer being queried.
754  *
755  * @return The number of bytes that can be read from the stream buffer before
756  * the stream buffer would be empty.
757  *
758  * \defgroup xStreamBufferBytesAvailable xStreamBufferBytesAvailable
759  * \ingroup StreamBufferManagement
760  */
761 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
762
763 /**
764  * stream_buffer.h
765  *
766  * @code{c}
767  * BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer, size_t xTriggerLevel );
768  * @endcode
769  *
770  * A stream buffer's trigger level is the number of bytes that must be in the
771  * stream buffer before a task that is blocked on the stream buffer to
772  * wait for data is moved out of the blocked state.  For example, if a task is
773  * blocked on a read of an empty stream buffer that has a trigger level of 1
774  * then the task will be unblocked when a single byte is written to the buffer
775  * or the task's block time expires.  As another example, if a task is blocked
776  * on a read of an empty stream buffer that has a trigger level of 10 then the
777  * task will not be unblocked until the stream buffer contains at least 10 bytes
778  * or the task's block time expires.  If a reading task's block time expires
779  * before the trigger level is reached then the task will still receive however
780  * many bytes are actually available.  Setting a trigger level of 0 will result
781  * in a trigger level of 1 being used.  It is not valid to specify a trigger
782  * level that is greater than the buffer size.
783  *
784  * A trigger level is set when the stream buffer is created, and can be modified
785  * using xStreamBufferSetTriggerLevel().
786  *
787  * @param xStreamBuffer The handle of the stream buffer being updated.
788  *
789  * @param xTriggerLevel The new trigger level for the stream buffer.
790  *
791  * @return If xTriggerLevel was less than or equal to the stream buffer's length
792  * then the trigger level will be updated and pdTRUE is returned.  Otherwise
793  * pdFALSE is returned.
794  *
795  * \defgroup xStreamBufferSetTriggerLevel xStreamBufferSetTriggerLevel
796  * \ingroup StreamBufferManagement
797  */
798 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer,
799                                          size_t xTriggerLevel ) PRIVILEGED_FUNCTION;
800
801 /**
802  * stream_buffer.h
803  *
804  * @code{c}
805  * BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken );
806  * @endcode
807  *
808  * For advanced users only.
809  *
810  * The sbSEND_COMPLETED() macro is called from within the FreeRTOS APIs when
811  * data is sent to a message buffer or stream buffer.  If there was a task that
812  * was blocked on the message or stream buffer waiting for data to arrive then
813  * the sbSEND_COMPLETED() macro sends a notification to the task to remove it
814  * from the Blocked state.  xStreamBufferSendCompletedFromISR() does the same
815  * thing.  It is provided to enable application writers to implement their own
816  * version of sbSEND_COMPLETED(), and MUST NOT BE USED AT ANY OTHER TIME.
817  *
818  * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
819  * additional information.
820  *
821  * @param xStreamBuffer The handle of the stream buffer to which data was
822  * written.
823  *
824  * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
825  * initialised to pdFALSE before it is passed into
826  * xStreamBufferSendCompletedFromISR().  If calling
827  * xStreamBufferSendCompletedFromISR() removes a task from the Blocked state,
828  * and the task has a priority above the priority of the currently running task,
829  * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
830  * context switch should be performed before exiting the ISR.
831  *
832  * @return If a task was removed from the Blocked state then pdTRUE is returned.
833  * Otherwise pdFALSE is returned.
834  *
835  * \defgroup xStreamBufferSendCompletedFromISR xStreamBufferSendCompletedFromISR
836  * \ingroup StreamBufferManagement
837  */
838 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
839                                               BaseType_t * pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
840
841 /**
842  * stream_buffer.h
843  *
844  * @code{c}
845  * BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken );
846  * @endcode
847  *
848  * For advanced users only.
849  *
850  * The sbRECEIVE_COMPLETED() macro is called from within the FreeRTOS APIs when
851  * data is read out of a message buffer or stream buffer.  If there was a task
852  * that was blocked on the message or stream buffer waiting for data to arrive
853  * then the sbRECEIVE_COMPLETED() macro sends a notification to the task to
854  * remove it from the Blocked state.  xStreamBufferReceiveCompletedFromISR()
855  * does the same thing.  It is provided to enable application writers to
856  * implement their own version of sbRECEIVE_COMPLETED(), and MUST NOT BE USED AT
857  * ANY OTHER TIME.
858  *
859  * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
860  * additional information.
861  *
862  * @param xStreamBuffer The handle of the stream buffer from which data was
863  * read.
864  *
865  * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
866  * initialised to pdFALSE before it is passed into
867  * xStreamBufferReceiveCompletedFromISR().  If calling
868  * xStreamBufferReceiveCompletedFromISR() removes a task from the Blocked state,
869  * and the task has a priority above the priority of the currently running task,
870  * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
871  * context switch should be performed before exiting the ISR.
872  *
873  * @return If a task was removed from the Blocked state then pdTRUE is returned.
874  * Otherwise pdFALSE is returned.
875  *
876  * \defgroup xStreamBufferReceiveCompletedFromISR xStreamBufferReceiveCompletedFromISR
877  * \ingroup StreamBufferManagement
878  */
879 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,
880                                                  BaseType_t * pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
881
882 /* Functions below here are not part of the public API. */
883 StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes,
884                                                  size_t xTriggerLevelBytes,
885                                                  BaseType_t xIsMessageBuffer,
886                                                  StreamBufferCallbackFunction_t pxSendCompletedCallback,
887                                                  StreamBufferCallbackFunction_t pxReceiveCompletedCallback ) PRIVILEGED_FUNCTION;
888
889
890 StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
891                                                        size_t xTriggerLevelBytes,
892                                                        BaseType_t xIsMessageBuffer,
893                                                        uint8_t * const pucStreamBufferStorageArea,
894                                                        StaticStreamBuffer_t * const pxStaticStreamBuffer,
895                                                        StreamBufferCallbackFunction_t pxSendCompletedCallback,
896                                                        StreamBufferCallbackFunction_t pxReceiveCompletedCallback ) PRIVILEGED_FUNCTION;
897
898 size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
899
900 #if ( configUSE_TRACE_FACILITY == 1 )
901     void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer,
902                                              UBaseType_t uxStreamBufferNumber ) PRIVILEGED_FUNCTION;
903     UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
904     uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;
905 #endif
906
907 /* *INDENT-OFF* */
908 #if defined( __cplusplus )
909     }
910 #endif
911 /* *INDENT-ON* */
912
913 #endif /* !defined( STREAM_BUFFER_H ) */