source: S-port/trunk/Middlewares/Third_Party/FreeRTOS/Source/include/stream_buffer.h

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