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