]> begriffs open source - freertos/blob - include/stream_buffer.h
Update version number to 10.4.1 (#173)
[freertos] / include / stream_buffer.h
1 /*\r
2  * FreeRTOS Kernel V10.4.1\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * https://www.FreeRTOS.org\r
23  * https://github.com/FreeRTOS\r
24  *\r
25  */\r
26 \r
27 /*\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
31  * scenarios.\r
32  *\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
47  *\r
48  */\r
49 \r
50 #ifndef STREAM_BUFFER_H\r
51 #define STREAM_BUFFER_H\r
52 \r
53 #ifndef INC_FREERTOS_H\r
54     #error "include FreeRTOS.h must appear in source files before include stream_buffer.h"\r
55 #endif\r
56 \r
57 /* *INDENT-OFF* */\r
58 #if defined( __cplusplus )\r
59     extern "C" {\r
60 #endif\r
61 /* *INDENT-ON* */\r
62 \r
63 /**\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
67  * etc.\r
68  */\r
69 struct StreamBufferDef_t;\r
70 typedef struct StreamBufferDef_t * StreamBufferHandle_t;\r
71 \r
72 \r
73 /**\r
74  * message_buffer.h\r
75  *\r
76  * <pre>\r
77  * StreamBufferHandle_t xStreamBufferCreate( size_t xBufferSizeBytes, size_t xTriggerLevelBytes );\r
78  * </pre>\r
79  *\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
83  *\r
84  * configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 or left undefined in\r
85  * FreeRTOSConfig.h for xStreamBufferCreate() to be available.\r
86  *\r
87  * @param xBufferSizeBytes The total number of bytes the stream buffer will be\r
88  * able to hold at any one time.\r
89  *\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
103  *\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
109  * buffer.\r
110  *\r
111  * Example use:\r
112  * <pre>\r
113  *\r
114  * void vAFunction( void )\r
115  * {\r
116  * StreamBufferHandle_t xStreamBuffer;\r
117  * const size_t xStreamBufferSizeBytes = 100, xTriggerLevel = 10;\r
118  *\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
123  *\r
124  *  if( xStreamBuffer == NULL )\r
125  *  {\r
126  *      // There was not enough heap memory space available to create the\r
127  *      // stream buffer.\r
128  *  }\r
129  *  else\r
130  *  {\r
131  *      // The stream buffer was created successfully and can now be used.\r
132  *  }\r
133  * }\r
134  * </pre>\r
135  * \defgroup xStreamBufferCreate xStreamBufferCreate\r
136  * \ingroup StreamBufferManagement\r
137  */\r
138 #define xStreamBufferCreate( xBufferSizeBytes, xTriggerLevelBytes )    xStreamBufferGenericCreate( xBufferSizeBytes, xTriggerLevelBytes, pdFALSE )\r
139 \r
140 /**\r
141  * stream_buffer.h\r
142  *\r
143  * <pre>\r
144  * StreamBufferHandle_t xStreamBufferCreateStatic( size_t xBufferSizeBytes,\r
145  *                                              size_t xTriggerLevelBytes,\r
146  *                                              uint8_t *pucStreamBufferStorageArea,\r
147  *                                              StaticStreamBuffer_t *pxStaticStreamBuffer );\r
148  * </pre>\r
149  * Creates a new stream buffer using statically allocated memory.  See\r
150  * xStreamBufferCreate() for a version that uses dynamically allocated memory.\r
151  *\r
152  * configSUPPORT_STATIC_ALLOCATION must be set to 1 in FreeRTOSConfig.h for\r
153  * xStreamBufferCreateStatic() to be available.\r
154  *\r
155  * @param xBufferSizeBytes The size, in bytes, of the buffer pointed to by the\r
156  * pucStreamBufferStorageArea parameter.\r
157  *\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
171  *\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
175  *\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
178  * structure.\r
179  *\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
183  *\r
184  * Example use:\r
185  * <pre>\r
186  *\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
190  *\r
191  * // Defines the memory that will actually hold the streams within the stream\r
192  * // buffer.\r
193  * static uint8_t ucStorageBuffer[ STORAGE_SIZE_BYTES ];\r
194  *\r
195  * // The variable used to hold the stream buffer structure.\r
196  * StaticStreamBuffer_t xStreamBufferStruct;\r
197  *\r
198  * void MyFunction( void )\r
199  * {\r
200  * StreamBufferHandle_t xStreamBuffer;\r
201  * const size_t xTriggerLevel = 1;\r
202  *\r
203  *  xStreamBuffer = xStreamBufferCreateStatic( sizeof( ucBufferStorage ),\r
204  *                                             xTriggerLevel,\r
205  *                                             ucBufferStorage,\r
206  *                                             &xStreamBufferStruct );\r
207  *\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
211  *\r
212  *  // Other code that uses the stream buffer can go here.\r
213  * }\r
214  *\r
215  * </pre>\r
216  * \defgroup xStreamBufferCreateStatic xStreamBufferCreateStatic\r
217  * \ingroup StreamBufferManagement\r
218  */\r
219 #define xStreamBufferCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, pucStreamBufferStorageArea, pxStaticStreamBuffer ) \\r
220     xStreamBufferGenericCreateStatic( xBufferSizeBytes, xTriggerLevelBytes, pdFALSE, pucStreamBufferStorageArea, pxStaticStreamBuffer )\r
221 \r
222 /**\r
223  * stream_buffer.h\r
224  *\r
225  * <pre>\r
226  * size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,\r
227  *                        const void *pvTxData,\r
228  *                        size_t xDataLengthBytes,\r
229  *                        TickType_t xTicksToWait );\r
230  * </pre>\r
231  *\r
232  * Sends bytes to a stream buffer.  The bytes are copied into the stream buffer.\r
233  *\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
247  * block time to 0.\r
248  *\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
252  *\r
253  * @param xStreamBuffer The handle of the stream buffer to which a stream is\r
254  * being sent.\r
255  *\r
256  * @param pvTxData A pointer to the buffer that holds the bytes to be copied\r
257  * into the stream buffer.\r
258  *\r
259  * @param xDataLengthBytes   The maximum number of bytes to copy from pvTxData\r
260  * into the stream buffer.\r
261  *\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
274  *\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
278  *\r
279  * Example use:\r
280  * <pre>\r
281  * void vAFunction( StreamBufferHandle_t xStreamBuffer )\r
282  * {\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
287  *\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
291  *\r
292  *  if( xBytesSent != sizeof( ucArrayToSend ) )\r
293  *  {\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
297  *  }\r
298  *\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
302  *\r
303  *  if( xBytesSent != strlen( pcStringToSend ) )\r
304  *  {\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
308  *  }\r
309  * }\r
310  * </pre>\r
311  * \defgroup xStreamBufferSend xStreamBufferSend\r
312  * \ingroup StreamBufferManagement\r
313  */\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
318 \r
319 /**\r
320  * stream_buffer.h\r
321  *\r
322  * <pre>\r
323  * size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,\r
324  *                               const void *pvTxData,\r
325  *                               size_t xDataLengthBytes,\r
326  *                               BaseType_t *pxHigherPriorityTaskWoken );\r
327  * </pre>\r
328  *\r
329  * Interrupt safe version of the API function that sends a stream of bytes to\r
330  * the stream buffer.\r
331  *\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
345  * block time to 0.\r
346  *\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
350  *\r
351  * @param xStreamBuffer The handle of the stream buffer to which a stream is\r
352  * being sent.\r
353  *\r
354  * @param pvTxData A pointer to the data that is to be copied into the stream\r
355  * buffer.\r
356  *\r
357  * @param xDataLengthBytes The maximum number of bytes to copy from pvTxData\r
358  * into the stream buffer.\r
359  *\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
373  *\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
377  *\r
378  * Example use:\r
379  * <pre>\r
380  * // A stream buffer that has already been created.\r
381  * StreamBufferHandle_t xStreamBuffer;\r
382  *\r
383  * void vAnInterruptServiceRoutine( void )\r
384  * {\r
385  * size_t xBytesSent;\r
386  * char *pcStringToSend = "String to send";\r
387  * BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.\r
388  *\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
394  *\r
395  *  if( xBytesSent != strlen( pcStringToSend ) )\r
396  *  {\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
399  *  }\r
400  *\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
410  * }\r
411  * </pre>\r
412  * \defgroup xStreamBufferSendFromISR xStreamBufferSendFromISR\r
413  * \ingroup StreamBufferManagement\r
414  */\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
419 \r
420 /**\r
421  * stream_buffer.h\r
422  *\r
423  * <pre>\r
424  * size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,\r
425  *                           void *pvRxData,\r
426  *                           size_t xBufferLengthBytes,\r
427  *                           TickType_t xTicksToWait );\r
428  * </pre>\r
429  *\r
430  * Receives bytes from a stream buffer.\r
431  *\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
445  * block time to 0.\r
446  *\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
450  *\r
451  * @param xStreamBuffer The handle of the stream buffer from which bytes are to\r
452  * be received.\r
453  *\r
454  * @param pvRxData A pointer to the buffer into which the received bytes will be\r
455  * copied.\r
456  *\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
461  *\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
471  * Blocked state.\r
472  *\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
476  *\r
477  * Example use:\r
478  * <pre>\r
479  * void vAFunction( StreamBuffer_t xStreamBuffer )\r
480  * {\r
481  * uint8_t ucRxData[ 20 ];\r
482  * size_t xReceivedBytes;\r
483  * const TickType_t xBlockTime = pdMS_TO_TICKS( 20 );\r
484  *\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
488  *  // available.\r
489  *  xReceivedBytes = xStreamBufferReceive( xStreamBuffer,\r
490  *                                         ( void * ) ucRxData,\r
491  *                                         sizeof( ucRxData ),\r
492  *                                         xBlockTime );\r
493  *\r
494  *  if( xReceivedBytes > 0 )\r
495  *  {\r
496  *      // A ucRxData contains another xRecievedBytes bytes of data, which can\r
497  *      // be processed here....\r
498  *  }\r
499  * }\r
500  * </pre>\r
501  * \defgroup xStreamBufferReceive xStreamBufferReceive\r
502  * \ingroup StreamBufferManagement\r
503  */\r
504 size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,\r
505                              void * pvRxData,\r
506                              size_t xBufferLengthBytes,\r
507                              TickType_t xTicksToWait ) PRIVILEGED_FUNCTION;\r
508 \r
509 /**\r
510  * stream_buffer.h\r
511  *\r
512  * <pre>\r
513  * size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,\r
514  *                                  void *pvRxData,\r
515  *                                  size_t xBufferLengthBytes,\r
516  *                                  BaseType_t *pxHigherPriorityTaskWoken );\r
517  * </pre>\r
518  *\r
519  * An interrupt safe version of the API function that receives bytes from a\r
520  * stream buffer.\r
521  *\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
525  *\r
526  * @param xStreamBuffer The handle of the stream buffer from which a stream\r
527  * is being received.\r
528  *\r
529  * @param pvRxData A pointer to the buffer into which the received bytes are\r
530  * copied.\r
531  *\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
536  *\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
550  *\r
551  * @return The number of bytes read from the stream buffer, if any.\r
552  *\r
553  * Example use:\r
554  * <pre>\r
555  * // A stream buffer that has already been created.\r
556  * StreamBuffer_t xStreamBuffer;\r
557  *\r
558  * void vAnInterruptServiceRoutine( void )\r
559  * {\r
560  * uint8_t ucRxData[ 20 ];\r
561  * size_t xReceivedBytes;\r
562  * BaseType_t xHigherPriorityTaskWoken = pdFALSE;  // Initialised to pdFALSE.\r
563  *\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
569  *\r
570  *  if( xReceivedBytes > 0 )\r
571  *  {\r
572  *      // ucRxData contains xReceivedBytes read from the stream buffer.\r
573  *      // Process the stream here....\r
574  *  }\r
575  *\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
585  * }\r
586  * </pre>\r
587  * \defgroup xStreamBufferReceiveFromISR xStreamBufferReceiveFromISR\r
588  * \ingroup StreamBufferManagement\r
589  */\r
590 size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,\r
591                                     void * pvRxData,\r
592                                     size_t xBufferLengthBytes,\r
593                                     BaseType_t * const pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;\r
594 \r
595 /**\r
596  * stream_buffer.h\r
597  *\r
598  * <pre>\r
599  * void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer );\r
600  * </pre>\r
601  *\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
606  *\r
607  * A stream buffer handle must not be used after the stream buffer has been\r
608  * deleted.\r
609  *\r
610  * @param xStreamBuffer The handle of the stream buffer to be deleted.\r
611  *\r
612  * \defgroup vStreamBufferDelete vStreamBufferDelete\r
613  * \ingroup StreamBufferManagement\r
614  */\r
615 void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;\r
616 \r
617 /**\r
618  * stream_buffer.h\r
619  *\r
620  * <pre>\r
621  * BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer );\r
622  * </pre>\r
623  *\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
626  *\r
627  * @param xStreamBuffer The handle of the stream buffer being queried.\r
628  *\r
629  * @return If the stream buffer is full then pdTRUE is returned.  Otherwise\r
630  * pdFALSE is returned.\r
631  *\r
632  * \defgroup xStreamBufferIsFull xStreamBufferIsFull\r
633  * \ingroup StreamBufferManagement\r
634  */\r
635 BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;\r
636 \r
637 /**\r
638  * stream_buffer.h\r
639  *\r
640  * <pre>\r
641  * BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer );\r
642  * </pre>\r
643  *\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
646  *\r
647  * @param xStreamBuffer The handle of the stream buffer being queried.\r
648  *\r
649  * @return If the stream buffer is empty then pdTRUE is returned.  Otherwise\r
650  * pdFALSE is returned.\r
651  *\r
652  * \defgroup xStreamBufferIsEmpty xStreamBufferIsEmpty\r
653  * \ingroup StreamBufferManagement\r
654  */\r
655 BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;\r
656 \r
657 /**\r
658  * stream_buffer.h\r
659  *\r
660  * <pre>\r
661  * BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer );\r
662  * </pre>\r
663  *\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
667  * buffer.\r
668  *\r
669  * @param xStreamBuffer The handle of the stream buffer being reset.\r
670  *\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
674  *\r
675  * \defgroup xStreamBufferReset xStreamBufferReset\r
676  * \ingroup StreamBufferManagement\r
677  */\r
678 BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;\r
679 \r
680 /**\r
681  * stream_buffer.h\r
682  *\r
683  * <pre>\r
684  * size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer );\r
685  * </pre>\r
686  *\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
689  * is full.\r
690  *\r
691  * @param xStreamBuffer The handle of the stream buffer being queried.\r
692  *\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
695  *\r
696  * \defgroup xStreamBufferSpacesAvailable xStreamBufferSpacesAvailable\r
697  * \ingroup StreamBufferManagement\r
698  */\r
699 size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;\r
700 \r
701 /**\r
702  * stream_buffer.h\r
703  *\r
704  * <pre>\r
705  * size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer );\r
706  * </pre>\r
707  *\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
711  *\r
712  * @param xStreamBuffer The handle of the stream buffer being queried.\r
713  *\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
716  *\r
717  * \defgroup xStreamBufferBytesAvailable xStreamBufferBytesAvailable\r
718  * \ingroup StreamBufferManagement\r
719  */\r
720 size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;\r
721 \r
722 /**\r
723  * stream_buffer.h\r
724  *\r
725  * <pre>\r
726  * BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer, size_t xTriggerLevel );\r
727  * </pre>\r
728  *\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
742  *\r
743  * A trigger level is set when the stream buffer is created, and can be modified\r
744  * using xStreamBufferSetTriggerLevel().\r
745  *\r
746  * @param xStreamBuffer The handle of the stream buffer being updated.\r
747  *\r
748  * @param xTriggerLevel The new trigger level for the stream buffer.\r
749  *\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
753  *\r
754  * \defgroup xStreamBufferSetTriggerLevel xStreamBufferSetTriggerLevel\r
755  * \ingroup StreamBufferManagement\r
756  */\r
757 BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer,\r
758                                          size_t xTriggerLevel ) PRIVILEGED_FUNCTION;\r
759 \r
760 /**\r
761  * stream_buffer.h\r
762  *\r
763  * <pre>\r
764  * BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken );\r
765  * </pre>\r
766  *\r
767  * For advanced users only.\r
768  *\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
776  *\r
777  * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for\r
778  * additional information.\r
779  *\r
780  * @param xStreamBuffer The handle of the stream buffer to which data was\r
781  * written.\r
782  *\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
790  *\r
791  * @return If a task was removed from the Blocked state then pdTRUE is returned.\r
792  * Otherwise pdFALSE is returned.\r
793  *\r
794  * \defgroup xStreamBufferSendCompletedFromISR xStreamBufferSendCompletedFromISR\r
795  * \ingroup StreamBufferManagement\r
796  */\r
797 BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer,\r
798                                               BaseType_t * pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;\r
799 \r
800 /**\r
801  * stream_buffer.h\r
802  *\r
803  * <pre>\r
804  * BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken );\r
805  * </pre>\r
806  *\r
807  * For advanced users only.\r
808  *\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
816  * ANY OTHER TIME.\r
817  *\r
818  * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for\r
819  * additional information.\r
820  *\r
821  * @param xStreamBuffer The handle of the stream buffer from which data was\r
822  * read.\r
823  *\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
831  *\r
832  * @return If a task was removed from the Blocked state then pdTRUE is returned.\r
833  * Otherwise pdFALSE is returned.\r
834  *\r
835  * \defgroup xStreamBufferReceiveCompletedFromISR xStreamBufferReceiveCompletedFromISR\r
836  * \ingroup StreamBufferManagement\r
837  */\r
838 BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer,\r
839                                                  BaseType_t * pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;\r
840 \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
845 \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
851 \r
852 size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer ) PRIVILEGED_FUNCTION;\r
853 \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
859 #endif\r
860 \r
861 /* *INDENT-OFF* */\r
862 #if defined( __cplusplus )\r
863     }\r
864 #endif\r
865 /* *INDENT-ON* */\r
866 \r
867 #endif /* !defined( STREAM_BUFFER_H ) */\r