2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT
\r
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
31 * Message buffers build functionality on top of FreeRTOS stream buffers.
\r
32 * Whereas stream buffers are used to send a continuous stream of data from one
\r
33 * task or interrupt to another, message buffers are used to send variable
\r
34 * length discrete messages from one task or interrupt to another. Their
\r
35 * implementation is light weight, making them particularly suited for interrupt
\r
36 * to task and core to core communication scenarios.
\r
38 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
39 * implementation (so also the message buffer implementation, as message buffers
\r
40 * are built on top of stream buffers) assumes there is only one task or
\r
41 * interrupt that will write to the buffer (the writer), and only one task or
\r
42 * interrupt that will read from the buffer (the reader). It is safe for the
\r
43 * writer and reader to be different tasks or interrupts, but, unlike other
\r
44 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
45 * multiple different readers. If there are to be multiple different writers
\r
46 * then the application writer must place each call to a writing API function
\r
47 * (such as xMessageBufferSend()) inside a critical section and set the send
\r
48 * block time to 0. Likewise, if there are to be multiple different readers
\r
49 * then the application writer must place each call to a reading API function
\r
50 * (such as xMessageBufferRead()) inside a critical section and set the receive
\r
53 * Message buffers hold variable length messages. To enable that, when a
\r
54 * message is written to the message buffer an additional sizeof( size_t ) bytes
\r
55 * are also written to store the message's length (that happens internally, with
\r
56 * the API function). sizeof( size_t ) is typically 4 bytes on a 32-bit
\r
57 * architecture, so writing a 10 byte message to a message buffer on a 32-bit
\r
58 * architecture will actually reduce the available space in the message buffer
\r
59 * by 14 bytes (10 byte are used by the message, and 4 bytes to hold the length
\r
63 #ifndef FREERTOS_MESSAGE_BUFFER_H
\r
64 #define FREERTOS_MESSAGE_BUFFER_H
\r
66 #ifndef INC_FREERTOS_H
\r
67 #error "include FreeRTOS.h must appear in source files before include message_buffer.h"
\r
70 /* Message buffers are built onto of stream buffers. */
\r
71 #include "stream_buffer.h"
\r
74 #if defined( __cplusplus )
\r
80 * Type by which message buffers are referenced. For example, a call to
\r
81 * xMessageBufferCreate() returns an MessageBufferHandle_t variable that can
\r
82 * then be used as a parameter to xMessageBufferSend(), xMessageBufferReceive(),
\r
85 typedef void * MessageBufferHandle_t;
\r
87 /*-----------------------------------------------------------*/
\r
93 * MessageBufferHandle_t xMessageBufferCreate( size_t xBufferSizeBytes );
\r
96 * Creates a new message buffer using dynamically allocated memory. See
\r
97 * xMessageBufferCreateStatic() for a version that uses statically allocated
\r
98 * memory (memory that is allocated at compile time).
\r
100 * configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 or left undefined in
\r
101 * FreeRTOSConfig.h for xMessageBufferCreate() to be available.
\r
103 * @param xBufferSizeBytes The total number of bytes (not messages) the message
\r
104 * buffer will be able to hold at any one time. When a message is written to
\r
105 * the message buffer an additional sizeof( size_t ) bytes are also written to
\r
106 * store the message's length. sizeof( size_t ) is typically 4 bytes on a
\r
107 * 32-bit architecture, so on most 32-bit architectures a 10 byte message will
\r
108 * take up 14 bytes of message buffer space.
\r
110 * @return If NULL is returned, then the message buffer cannot be created
\r
111 * because there is insufficient heap memory available for FreeRTOS to allocate
\r
112 * the message buffer data structures and storage area. A non-NULL value being
\r
113 * returned indicates that the message buffer has been created successfully -
\r
114 * the returned value should be stored as the handle to the created message
\r
120 * void vAFunction( void )
\r
122 * MessageBufferHandle_t xMessageBuffer;
\r
123 * const size_t xMessageBufferSizeBytes = 100;
\r
125 * // Create a message buffer that can hold 100 bytes. The memory used to hold
\r
126 * // both the message buffer structure and the messages themselves is allocated
\r
127 * // dynamically. Each message added to the buffer consumes an additional 4
\r
128 * // bytes which are used to hold the lengh of the message.
\r
129 * xMessageBuffer = xMessageBufferCreate( xMessageBufferSizeBytes );
\r
131 * if( xMessageBuffer == NULL )
\r
133 * // There was not enough heap memory space available to create the
\r
134 * // message buffer.
\r
138 * // The message buffer was created successfully and can now be used.
\r
142 * \defgroup xMessageBufferCreate xMessageBufferCreate
\r
143 * \ingroup MessageBufferManagement
\r
145 #define xMessageBufferCreate( xBufferSizeBytes ) \
\r
146 ( MessageBufferHandle_t ) xStreamBufferGenericCreate( xBufferSizeBytes, ( size_t ) 0, pdTRUE )
\r
152 * MessageBufferHandle_t xMessageBufferCreateStatic( size_t xBufferSizeBytes,
\r
153 * uint8_t *pucMessageBufferStorageArea,
\r
154 * StaticMessageBuffer_t *pxStaticMessageBuffer );
\r
156 * Creates a new message buffer using statically allocated memory. See
\r
157 * xMessageBufferCreate() for a version that uses dynamically allocated memory.
\r
159 * @param xBufferSizeBytes The size, in bytes, of the buffer pointed to by the
\r
160 * pucMessageBufferStorageArea parameter. When a message is written to the
\r
161 * message buffer an additional sizeof( size_t ) bytes are also written to store
\r
162 * the message's length. sizeof( size_t ) is typically 4 bytes on a 32-bit
\r
163 * architecture, so on most 32-bit architecture a 10 byte message will take up
\r
164 * 14 bytes of message buffer space. The maximum number of bytes that can be
\r
165 * stored in the message buffer is actually (xBufferSizeBytes - 1).
\r
167 * @param pucMessageBufferStorageArea Must point to a uint8_t array that is at
\r
168 * least xBufferSizeBytes big. This is the array to which messages are
\r
169 * copied when they are written to the message buffer.
\r
171 * @param pxStaticMessageBuffer Must point to a variable of type
\r
172 * StaticMessageBuffer_t, which will be used to hold the message buffer's data
\r
175 * @return If the message buffer is created successfully then a handle to the
\r
176 * created message buffer is returned. If either pucMessageBufferStorageArea or
\r
177 * pxStaticmessageBuffer are NULL then NULL is returned.
\r
182 * // Used to dimension the array used to hold the messages. The available space
\r
183 * // will actually be one less than this, so 999.
\r
184 #define STORAGE_SIZE_BYTES 1000
\r
186 * // Defines the memory that will actually hold the messages within the message
\r
188 * static uint8_t ucStorageBuffer[ STORAGE_SIZE_BYTES ];
\r
190 * // The variable used to hold the message buffer structure.
\r
191 * StaticMessageBuffer_t xMessageBufferStruct;
\r
193 * void MyFunction( void )
\r
195 * MessageBufferHandle_t xMessageBuffer;
\r
197 * xMessageBuffer = xMessageBufferCreateStatic( sizeof( ucStorageBuffer ),
\r
199 * &xMessageBufferStruct );
\r
201 * // As neither the pucMessageBufferStorageArea or pxStaticMessageBuffer
\r
202 * // parameters were NULL, xMessageBuffer will not be NULL, and can be used to
\r
203 * // reference the created message buffer in other message buffer API calls.
\r
205 * // Other code that uses the message buffer can go here.
\r
209 * \defgroup xMessageBufferCreateStatic xMessageBufferCreateStatic
\r
210 * \ingroup MessageBufferManagement
\r
212 #define xMessageBufferCreateStatic( xBufferSizeBytes, pucMessageBufferStorageArea, pxStaticMessageBuffer ) \
\r
213 ( MessageBufferHandle_t ) xStreamBufferGenericCreateStatic( xBufferSizeBytes, 0, pdTRUE, pucMessageBufferStorageArea, pxStaticMessageBuffer )
\r
219 * size_t xMessageBufferSend( MessageBufferHandle_t xMessageBuffer,
\r
220 * const void *pvTxData,
\r
221 * size_t xDataLengthBytes,
\r
222 * TickType_t xTicksToWait );
\r
225 * Sends a discrete message to the message buffer. The message can be any
\r
226 * length that fits within the buffer's free space, and is copied into the
\r
229 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
230 * implementation (so also the message buffer implementation, as message buffers
\r
231 * are built on top of stream buffers) assumes there is only one task or
\r
232 * interrupt that will write to the buffer (the writer), and only one task or
\r
233 * interrupt that will read from the buffer (the reader). It is safe for the
\r
234 * writer and reader to be different tasks or interrupts, but, unlike other
\r
235 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
236 * multiple different readers. If there are to be multiple different writers
\r
237 * then the application writer must place each call to a writing API function
\r
238 * (such as xMessageBufferSend()) inside a critical section and set the send
\r
239 * block time to 0. Likewise, if there are to be multiple different readers
\r
240 * then the application writer must place each call to a reading API function
\r
241 * (such as xMessageBufferRead()) inside a critical section and set the receive
\r
244 * Use xMessageBufferSend() to write to a message buffer from a task. Use
\r
245 * xMessageBufferSendFromISR() to write to a message buffer from an interrupt
\r
246 * service routine (ISR).
\r
248 * @param xMessageBuffer The handle of the message buffer to which a message is
\r
251 * @param pvTxData A pointer to the message that is to be copied into the
\r
254 * @param xDataLengthBytes The length of the message. That is, the number of
\r
255 * bytes to copy from pvTxData into the message buffer. When a message is
\r
256 * written to the message buffer an additional sizeof( size_t ) bytes are also
\r
257 * written to store the message's length. sizeof( size_t ) is typically 4 bytes
\r
258 * on a 32-bit architecture, so on most 32-bit architecture setting
\r
259 * xDataLengthBytes to 20 will reduce the free space in the message buffer by 24
\r
260 * bytes (20 bytes of message data and 4 bytes to hold the message length).
\r
262 * @param xTicksToWait The maximum amount of time the calling task should remain
\r
263 * in the Blocked state to wait for enough space to become available in the
\r
264 * message buffer, should the message buffer have insufficient space when
\r
265 * xMessageBufferSend() is called. The calling task will never block if
\r
266 * xTicksToWait is zero. The block time is specified in tick periods, so the
\r
267 * absolute time it represents is dependent on the tick frequency. The macro
\r
268 * pdMS_TO_TICKS() can be used to convert a time specified in milliseconds into
\r
269 * a time specified in ticks. Setting xTicksToWait to portMAX_DELAY will cause
\r
270 * the task to wait indefinitely (without timing out), provided
\r
271 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. Tasks do not use any
\r
272 * CPU time when they are in the Blocked state.
\r
274 * @return The number of bytes written to the message buffer. If the call to
\r
275 * xMessageBufferSend() times out before there was enough space to write the
\r
276 * message into the message buffer then zero is returned. If the call did not
\r
277 * time out then xDataLengthBytes is returned.
\r
281 * void vAFunction( MessageBufferHandle_t xMessageBuffer )
\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
288 * // Send an array to the message buffer, blocking for a maximum of 100ms to
\r
289 * // wait for enough space to be available in the message buffer.
\r
290 * xBytesSent = xMessageBufferSend( xMessageBuffer, ( void * ) ucArrayToSend, sizeof( ucArrayToSend ), x100ms );
\r
292 * if( xBytesSent != sizeof( ucArrayToSend ) )
\r
294 * // The call to xMessageBufferSend() times out before there was enough
\r
295 * // space in the buffer for the data to be written.
\r
298 * // Send the string to the message buffer. Return immediately if there is
\r
299 * // not enough space in the buffer.
\r
300 * xBytesSent = xMessageBufferSend( xMessageBuffer, ( void * ) pcStringToSend, strlen( pcStringToSend ), 0 );
\r
302 * if( xBytesSent != strlen( pcStringToSend ) )
\r
304 * // The string could not be added to the message buffer because there was
\r
305 * // not enough free space in the buffer.
\r
309 * \defgroup xMessageBufferSend xMessageBufferSend
\r
310 * \ingroup MessageBufferManagement
\r
312 #define xMessageBufferSend( xMessageBuffer, pvTxData, xDataLengthBytes, xTicksToWait ) \
\r
313 xStreamBufferSend( ( StreamBufferHandle_t ) xMessageBuffer, pvTxData, xDataLengthBytes, xTicksToWait )
\r
319 * size_t xMessageBufferSendFromISR( MessageBufferHandle_t xMessageBuffer,
\r
320 * const void *pvTxData,
\r
321 * size_t xDataLengthBytes,
\r
322 * BaseType_t *pxHigherPriorityTaskWoken );
\r
325 * Interrupt safe version of the API function that sends a discrete message to
\r
326 * the message buffer. The message can be any length that fits within the
\r
327 * buffer's free space, and is copied into the buffer.
\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 xMessageBufferSend()) 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 xMessageBufferRead()) inside a critical section and set the receive
\r
344 * Use xMessageBufferSend() to write to a message buffer from a task. Use
\r
345 * xMessageBufferSendFromISR() to write to a message buffer from an interrupt
\r
346 * service routine (ISR).
\r
348 * @param xMessageBuffer The handle of the message buffer to which a message is
\r
351 * @param pvTxData A pointer to the message that is to be copied into the
\r
354 * @param xDataLengthBytes The length of the message. That is, the number of
\r
355 * bytes to copy from pvTxData into the message buffer. When a message is
\r
356 * written to the message buffer an additional sizeof( size_t ) bytes are also
\r
357 * written to store the message's length. sizeof( size_t ) is typically 4 bytes
\r
358 * on a 32-bit architecture, so on most 32-bit architecture setting
\r
359 * xDataLengthBytes to 20 will reduce the free space in the message buffer by 24
\r
360 * bytes (20 bytes of message data and 4 bytes to hold the message length).
\r
362 * @param pxHigherPriorityTaskWoken It is possible that a message buffer will
\r
363 * have a task blocked on it waiting for data. Calling
\r
364 * xMessageBufferSendFromISR() can make data available, and so cause a task that
\r
365 * was waiting for data to leave the Blocked state. If calling
\r
366 * xMessageBufferSendFromISR() causes a task to leave the Blocked state, and the
\r
367 * unblocked task has a priority higher than the currently executing task (the
\r
368 * task that was interrupted), then, internally, xMessageBufferSendFromISR()
\r
369 * will set *pxHigherPriorityTaskWoken to pdTRUE. If
\r
370 * xMessageBufferSendFromISR() sets this value to pdTRUE, then normally a
\r
371 * context switch should be performed before the interrupt is exited. This will
\r
372 * ensure that the interrupt returns directly to the highest priority Ready
\r
373 * state task. *pxHigherPriorityTaskWoken should be set to pdFALSE before it
\r
374 * is passed into the function. See the code example below for an example.
\r
376 * @return The number of bytes actually written to the message buffer. If the
\r
377 * message buffer didn't have enough free space for the message to be stored
\r
378 * then 0 is returned, otherwise xDataLengthBytes is returned.
\r
382 * // A message buffer that has already been created.
\r
383 * MessageBufferHandle_t xMessageBuffer;
\r
385 * void vAnInterruptServiceRoutine( void )
\r
387 * size_t xBytesSent;
\r
388 * char *pcStringToSend = "String to send";
\r
389 * BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
\r
391 * // Attempt to send the string to the message buffer.
\r
392 * xBytesSent = xMessageBufferSendFromISR( xMessageBuffer,
\r
393 * ( void * ) pcStringToSend,
\r
394 * strlen( pcStringToSend ),
\r
395 * &xHigherPriorityTaskWoken );
\r
397 * if( xBytesSent != strlen( pcStringToSend ) )
\r
399 * // The string could not be added to the message buffer because there was
\r
400 * // not enough free space in the buffer.
\r
403 * // If xHigherPriorityTaskWoken was set to pdTRUE inside
\r
404 * // xMessageBufferSendFromISR() then a task that has a priority above the
\r
405 * // priority of the currently executing task was unblocked and a context
\r
406 * // switch should be performed to ensure the ISR returns to the unblocked
\r
407 * // task. In most FreeRTOS ports this is done by simply passing
\r
408 * // xHigherPriorityTaskWoken into portYIELD_FROM_ISR(), which will test the
\r
409 * // variables value, and perform the context switch if necessary. Check the
\r
410 * // documentation for the port in use for port specific instructions.
\r
411 * portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
414 * \defgroup xMessageBufferSendFromISR xMessageBufferSendFromISR
\r
415 * \ingroup MessageBufferManagement
\r
417 #define xMessageBufferSendFromISR( xMessageBuffer, pvTxData, xDataLengthBytes, pxHigherPriorityTaskWoken ) \
\r
418 xStreamBufferSendFromISR( ( StreamBufferHandle_t ) xMessageBuffer, pvTxData, xDataLengthBytes, pxHigherPriorityTaskWoken )
\r
424 * size_t xMessageBufferReceive( MessageBufferHandle_t xMessageBuffer,
\r
426 * size_t xBufferLengthBytes,
\r
427 * TickType_t xTicksToWait );
\r
430 * Receives a discrete message from a message buffer. Messages can be of
\r
431 * variable length and are copied out of the buffer.
\r
433 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
434 * implementation (so also the message buffer implementation, as message buffers
\r
435 * are built on top of stream buffers) assumes there is only one task or
\r
436 * interrupt that will write to the buffer (the writer), and only one task or
\r
437 * interrupt that will read from the buffer (the reader). It is safe for the
\r
438 * writer and reader to be different tasks or interrupts, but, unlike other
\r
439 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
440 * multiple different readers. If there are to be multiple different writers
\r
441 * then the application writer must place each call to a writing API function
\r
442 * (such as xMessageBufferSend()) inside a critical section and set the send
\r
443 * block time to 0. Likewise, if there are to be multiple different readers
\r
444 * then the application writer must place each call to a reading API function
\r
445 * (such as xMessageBufferRead()) inside a critical section and set the receive
\r
448 * Use xMessageBufferReceive() to read from a message buffer from a task. Use
\r
449 * xMessageBufferReceiveFromISR() to read from a message buffer from an
\r
450 * interrupt service routine (ISR).
\r
452 * @param xMessageBuffer The handle of the message buffer from which a message
\r
453 * is being received.
\r
455 * @param pvRxData A pointer to the buffer into which the received message is
\r
458 * @param xBufferLengthBytes The length of the buffer pointed to by the pvRxData
\r
459 * parameter. This sets the maximum length of the message that can be received.
\r
460 * If xBufferLengthBytes is too small to hold the next message then the message
\r
461 * will be left in the message buffer and 0 will be returned.
\r
463 * @param xTicksToWait The maximum amount of time the task should remain in the
\r
464 * Blocked state to wait for a message, should the message buffer be empty.
\r
465 * xMessageBufferReceive() will return immediately if xTicksToWait is zero and
\r
466 * the message buffer is empty. The block time is specified in tick periods, so
\r
467 * the absolute time it represents is dependent on the tick frequency. The
\r
468 * macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds
\r
469 * into a time specified in ticks. Setting xTicksToWait to portMAX_DELAY will
\r
470 * cause the task to wait indefinitely (without timing out), provided
\r
471 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. Tasks do not use any
\r
472 * CPU time when they are in the Blocked state.
\r
474 * @return The length, in bytes, of the message read from the message buffer, if
\r
475 * any. If xMessageBufferReceive() times out before a message became available
\r
476 * then zero is returned. If the length of the message is greater than
\r
477 * xBufferLengthBytes then the message will be left in the message buffer and
\r
478 * zero is returned.
\r
482 * void vAFunction( MessageBuffer_t xMessageBuffer )
\r
484 * uint8_t ucRxData[ 20 ];
\r
485 * size_t xReceivedBytes;
\r
486 * const TickType_t xBlockTime = pdMS_TO_TICKS( 20 );
\r
488 * // Receive the next message from the message buffer. Wait in the Blocked
\r
489 * // state (so not using any CPU processing time) for a maximum of 100ms for
\r
490 * // a message to become available.
\r
491 * xReceivedBytes = xMessageBufferReceive( xMessageBuffer,
\r
492 * ( void * ) ucRxData,
\r
493 * sizeof( ucRxData ),
\r
496 * if( xReceivedBytes > 0 )
\r
498 * // A ucRxData contains a message that is xReceivedBytes long. Process
\r
499 * // the message here....
\r
503 * \defgroup xMessageBufferReceive xMessageBufferReceive
\r
504 * \ingroup MessageBufferManagement
\r
506 #define xMessageBufferReceive( xMessageBuffer, pvRxData, xBufferLengthBytes, xTicksToWait ) \
\r
507 xStreamBufferReceive( ( StreamBufferHandle_t ) xMessageBuffer, pvRxData, xBufferLengthBytes, xTicksToWait )
\r
514 * size_t xMessageBufferReceiveFromISR( MessageBufferHandle_t xMessageBuffer,
\r
516 * size_t xBufferLengthBytes,
\r
517 * BaseType_t *pxHigherPriorityTaskWoken );
\r
520 * An interrupt safe version of the API function that receives a discrete
\r
521 * message from a message buffer. Messages can be of variable length and are
\r
522 * copied out of the buffer.
\r
524 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
525 * implementation (so also the message buffer implementation, as message buffers
\r
526 * are built on top of stream buffers) assumes there is only one task or
\r
527 * interrupt that will write to the buffer (the writer), and only one task or
\r
528 * interrupt that will read from the buffer (the reader). It is safe for the
\r
529 * writer and reader to be different tasks or interrupts, but, unlike other
\r
530 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
531 * multiple different readers. If there are to be multiple different writers
\r
532 * then the application writer must place each call to a writing API function
\r
533 * (such as xMessageBufferSend()) inside a critical section and set the send
\r
534 * block time to 0. Likewise, if there are to be multiple different readers
\r
535 * then the application writer must place each call to a reading API function
\r
536 * (such as xMessageBufferRead()) inside a critical section and set the receive
\r
539 * Use xMessageBufferReceive() to read from a message buffer from a task. Use
\r
540 * xMessageBufferReceiveFromISR() to read from a message buffer from an
\r
541 * interrupt service routine (ISR).
\r
543 * @param xMessageBuffer The handle of the message buffer from which a message
\r
544 * is being received.
\r
546 * @param pvRxData A pointer to the buffer into which the received message is
\r
549 * @param xBufferLengthBytes The length of the buffer pointed to by the pvRxData
\r
550 * parameter. This sets the maximum length of the message that can be received.
\r
551 * If xBufferLengthBytes is too small to hold the next message then the message
\r
552 * will be left in the message buffer and 0 will be returned.
\r
554 * @param pxHigherPriorityTaskWoken It is possible that a message buffer will
\r
555 * have a task blocked on it waiting for space to become available. Calling
\r
556 * xMessageBufferReceiveFromISR() can make space available, and so cause a task
\r
557 * that is waiting for space to leave the Blocked state. If calling
\r
558 * xMessageBufferReceiveFromISR() causes a task to leave the Blocked state, and
\r
559 * the unblocked task has a priority higher than the currently executing task
\r
560 * (the task that was interrupted), then, internally,
\r
561 * xMessageBufferReceiveFromISR() will set *pxHigherPriorityTaskWoken to pdTRUE.
\r
562 * If xMessageBufferReceiveFromISR() sets this value to pdTRUE, then normally a
\r
563 * context switch should be performed before the interrupt is exited. That will
\r
564 * ensure the interrupt returns directly to the highest priority Ready state
\r
565 * task. *pxHigherPriorityTaskWoken should be set to pdFALSE before it is
\r
566 * passed into the function. See the code example below for an example.
\r
568 * @return The length, in bytes, of the message read from the message buffer, if
\r
573 * // A message buffer that has already been created.
\r
574 * MessageBuffer_t xMessageBuffer;
\r
576 * void vAnInterruptServiceRoutine( void )
\r
578 * uint8_t ucRxData[ 20 ];
\r
579 * size_t xReceivedBytes;
\r
580 * BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
\r
582 * // Receive the next message from the message buffer.
\r
583 * xReceivedBytes = xMessageBufferReceiveFromISR( xMessageBuffer,
\r
584 * ( void * ) ucRxData,
\r
585 * sizeof( ucRxData ),
\r
586 * &xHigherPriorityTaskWoken );
\r
588 * if( xReceivedBytes > 0 )
\r
590 * // A ucRxData contains a message that is xReceivedBytes long. Process
\r
591 * // the message here....
\r
594 * // If xHigherPriorityTaskWoken was set to pdTRUE inside
\r
595 * // xMessageBufferReceiveFromISR() then a task that has a priority above the
\r
596 * // priority of the currently executing task was unblocked and a context
\r
597 * // switch should be performed to ensure the ISR returns to the unblocked
\r
598 * // task. In most FreeRTOS ports this is done by simply passing
\r
599 * // xHigherPriorityTaskWoken into portYIELD_FROM_ISR(), which will test the
\r
600 * // variables value, and perform the context switch if necessary. Check the
\r
601 * // documentation for the port in use for port specific instructions.
\r
602 * portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
605 * \defgroup xMessageBufferReceiveFromISR xMessageBufferReceiveFromISR
\r
606 * \ingroup MessageBufferManagement
\r
608 #define xMessageBufferReceiveFromISR( xMessageBuffer, pvRxData, xBufferLengthBytes, pxHigherPriorityTaskWoken ) \
\r
609 xStreamBufferReceiveFromISR( ( StreamBufferHandle_t ) xMessageBuffer, pvRxData, xBufferLengthBytes, pxHigherPriorityTaskWoken )
\r
615 * void vMessageBufferDelete( MessageBufferHandle_t xMessageBuffer );
\r
618 * Deletes a message buffer that was previously created using a call to
\r
619 * xMessageBufferCreate() or xMessageBufferCreateStatic(). If the message
\r
620 * buffer was created using dynamic memory (that is, by xMessageBufferCreate()),
\r
621 * then the allocated memory is freed.
\r
623 * A message buffer handle must not be used after the message buffer has been
\r
626 * @param xMessageBuffer The handle of the message buffer to be deleted.
\r
629 #define vMessageBufferDelete( xMessageBuffer ) \
\r
630 vStreamBufferDelete( ( StreamBufferHandle_t ) xMessageBuffer )
\r
635 * BaseType_t xMessageBufferIsFull( MessageBufferHandle_t xMessageBuffer );
\r
638 * Tests to see if a message buffer is full. A message buffer is full if it
\r
639 * cannot accept any more messages, of any size, until space is made available
\r
640 * by a message being removed from the message buffer.
\r
642 * @param xMessageBuffer The handle of the message buffer being queried.
\r
644 * @return If the message buffer referenced by xMessageBuffer is full then
\r
645 * pdTRUE is returned. Otherwise pdFALSE is returned.
\r
647 #define xMessageBufferIsFull( xMessageBuffer ) \
\r
648 xStreamBufferIsFull( ( StreamBufferHandle_t ) xMessageBuffer )
\r
653 * BaseType_t xMessageBufferIsEmpty( MessageBufferHandle_t xMessageBuffer );
\r
656 * Tests to see if a message buffer is empty (does not contain any messages).
\r
658 * @param xMessageBuffer The handle of the message buffer being queried.
\r
660 * @return If the message buffer referenced by xMessageBuffer is empty then
\r
661 * pdTRUE is returned. Otherwise pdFALSE is returned.
\r
664 #define xMessageBufferIsEmpty( xMessageBuffer ) \
\r
665 xStreamBufferIsEmpty( ( StreamBufferHandle_t ) xMessageBuffer )
\r
670 * BaseType_t xMessageBufferReset( MessageBufferHandle_t xMessageBuffer );
\r
673 * Resets a message buffer to its initial empty state, discarding any message it
\r
676 * A message buffer can only be reset if there are no tasks blocked on it.
\r
678 * @param xMessageBuffer The handle of the message buffer being reset.
\r
680 * @return If the message buffer was reset then pdPASS is returned. If the
\r
681 * message buffer could not be reset because either there was a task blocked on
\r
682 * the message queue to wait for space to become available, or to wait for a
\r
683 * a message to be available, then pdFAIL is returned.
\r
685 * \defgroup xMessageBufferReset xMessageBufferReset
\r
686 * \ingroup MessageBufferManagement
\r
688 #define xMessageBufferReset( xMessageBuffer ) \
\r
689 xStreamBufferReset( ( StreamBufferHandle_t ) xMessageBuffer )
\r
695 * size_t xMessageBufferSpaceAvailable( MessageBufferHandle_t xMessageBuffer );
\r
697 * Returns the number of bytes of free space in the message buffer.
\r
699 * @param xMessageBuffer The handle of the message buffer being queried.
\r
701 * @return The number of bytes that can be written to the message buffer before
\r
702 * the message buffer would be full. When a message is written to the message
\r
703 * buffer an additional sizeof( size_t ) bytes are also written to store the
\r
704 * message's length. sizeof( size_t ) is typically 4 bytes on a 32-bit
\r
705 * architecture, so if xMessageBufferSpacesAvailable() returns 10, then the size
\r
706 * of the largest message that can be written to the message buffer is 6 bytes.
\r
708 * \defgroup xMessageBufferSpaceAvailable xMessageBufferSpaceAvailable
\r
709 * \ingroup MessageBufferManagement
\r
711 #define xMessageBufferSpaceAvailable( xMessageBuffer ) \
\r
712 xStreamBufferSpacesAvailable( ( StreamBufferHandle_t ) xMessageBuffer )
\r
713 #define xMessageBufferSpacesAvailable( xMessageBuffer ) \
\r
714 xStreamBufferSpacesAvailable( ( StreamBufferHandle_t ) xMessageBuffer ) /* Corrects typo in original macro name. */
\r
719 * size_t xMessageBufferNextLengthBytes( MessageBufferHandle_t xMessageBuffer );
\r
721 * Returns the length (in bytes) of the next message in a message buffer.
\r
722 * Useful if xMessageBufferReceive() returned 0 because the size of the buffer
\r
723 * passed into xMessageBufferReceive() was too small to hold the next message.
\r
725 * @param xMessageBuffer The handle of the message buffer being queried.
\r
727 * @return The length (in bytes) of the next message in the message buffer, or 0
\r
728 * if the message buffer is empty.
\r
730 * \defgroup xMessageBufferNextLengthBytes xMessageBufferNextLengthBytes
\r
731 * \ingroup MessageBufferManagement
\r
733 #define xMessageBufferNextLengthBytes( xMessageBuffer ) \
\r
734 xStreamBufferNextMessageLengthBytes( ( StreamBufferHandle_t ) xMessageBuffer ) PRIVILEGED_FUNCTION;
\r
740 * BaseType_t xMessageBufferSendCompletedFromISR( MessageBufferHandle_t xMessageBuffer, BaseType_t *pxHigherPriorityTaskWoken );
\r
743 * For advanced users only.
\r
745 * The sbSEND_COMPLETED() macro is called from within the FreeRTOS APIs when
\r
746 * data is sent to a message buffer or stream buffer. If there was a task that
\r
747 * was blocked on the message or stream buffer waiting for data to arrive then
\r
748 * the sbSEND_COMPLETED() macro sends a notification to the task to remove it
\r
749 * from the Blocked state. xMessageBufferSendCompletedFromISR() does the same
\r
750 * thing. It is provided to enable application writers to implement their own
\r
751 * version of sbSEND_COMPLETED(), and MUST NOT BE USED AT ANY OTHER TIME.
\r
753 * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
\r
754 * additional information.
\r
756 * @param xMessageBuffer The handle of the stream buffer to which data was
\r
759 * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
\r
760 * initialised to pdFALSE before it is passed into
\r
761 * xMessageBufferSendCompletedFromISR(). If calling
\r
762 * xMessageBufferSendCompletedFromISR() removes a task from the Blocked state,
\r
763 * and the task has a priority above the priority of the currently running task,
\r
764 * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
\r
765 * context switch should be performed before exiting the ISR.
\r
767 * @return If a task was removed from the Blocked state then pdTRUE is returned.
\r
768 * Otherwise pdFALSE is returned.
\r
770 * \defgroup xMessageBufferSendCompletedFromISR xMessageBufferSendCompletedFromISR
\r
771 * \ingroup StreamBufferManagement
\r
773 #define xMessageBufferSendCompletedFromISR( xMessageBuffer, pxHigherPriorityTaskWoken ) \
\r
774 xStreamBufferSendCompletedFromISR( ( StreamBufferHandle_t ) xMessageBuffer, pxHigherPriorityTaskWoken )
\r
780 * BaseType_t xMessageBufferReceiveCompletedFromISR( MessageBufferHandle_t xMessageBuffer, BaseType_t *pxHigherPriorityTaskWoken );
\r
783 * For advanced users only.
\r
785 * The sbRECEIVE_COMPLETED() macro is called from within the FreeRTOS APIs when
\r
786 * data is read out of a message buffer or stream buffer. If there was a task
\r
787 * that was blocked on the message or stream buffer waiting for data to arrive
\r
788 * then the sbRECEIVE_COMPLETED() macro sends a notification to the task to
\r
789 * remove it from the Blocked state. xMessageBufferReceiveCompletedFromISR()
\r
790 * does the same thing. It is provided to enable application writers to
\r
791 * implement their own version of sbRECEIVE_COMPLETED(), and MUST NOT BE USED AT
\r
794 * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
\r
795 * additional information.
\r
797 * @param xMessageBuffer The handle of the stream buffer from which data was
\r
800 * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
\r
801 * initialised to pdFALSE before it is passed into
\r
802 * xMessageBufferReceiveCompletedFromISR(). If calling
\r
803 * xMessageBufferReceiveCompletedFromISR() removes a task from the Blocked state,
\r
804 * and the task has a priority above the priority of the currently running task,
\r
805 * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
\r
806 * context switch should be performed before exiting the ISR.
\r
808 * @return If a task was removed from the Blocked state then pdTRUE is returned.
\r
809 * Otherwise pdFALSE is returned.
\r
811 * \defgroup xMessageBufferReceiveCompletedFromISR xMessageBufferReceiveCompletedFromISR
\r
812 * \ingroup StreamBufferManagement
\r
814 #define xMessageBufferReceiveCompletedFromISR( xMessageBuffer, pxHigherPriorityTaskWoken ) \
\r
815 xStreamBufferReceiveCompletedFromISR( ( StreamBufferHandle_t ) xMessageBuffer, pxHigherPriorityTaskWoken )
\r
818 #if defined( __cplusplus )
\r
823 #endif /* !defined( FREERTOS_MESSAGE_BUFFER_H ) */
\r