2 * FreeRTOS Kernel V11.1.0
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
31 * Message buffers build functionality on top of FreeRTOS stream buffers.
32 * Whereas stream buffers are used to send a continuous stream of data from one
33 * task or interrupt to another, message buffers are used to send variable
34 * length discrete messages from one task or interrupt to another. Their
35 * implementation is light weight, making them particularly suited for interrupt
36 * to task and core to core communication scenarios.
38 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
39 * implementation (so also the message buffer implementation, as message buffers
40 * are built on top of stream buffers) assumes there is only one task or
41 * interrupt that will write to the buffer (the writer), and only one task or
42 * interrupt that will read from the buffer (the reader). It is safe for the
43 * writer and reader to be different tasks or interrupts, but, unlike other
44 * FreeRTOS objects, it is not safe to have multiple different writers or
45 * multiple different readers. If there are to be multiple different writers
46 * then the application writer must place each call to a writing API function
47 * (such as xMessageBufferSend()) inside a critical section and set the send
48 * block time to 0. Likewise, if there are to be multiple different readers
49 * then the application writer must place each call to a reading API function
50 * (such as xMessageBufferRead()) inside a critical section and set the receive
53 * Message buffers hold variable length messages. To enable that, when a
54 * message is written to the message buffer an additional sizeof( size_t ) bytes
55 * are also written to store the message's length (that happens internally, with
56 * the API function). sizeof( size_t ) is typically 4 bytes on a 32-bit
57 * architecture, so writing a 10 byte message to a message buffer on a 32-bit
58 * architecture will actually reduce the available space in the message buffer
59 * by 14 bytes (10 byte are used by the message, and 4 bytes to hold the length
63 #ifndef FREERTOS_MESSAGE_BUFFER_H
64 #define FREERTOS_MESSAGE_BUFFER_H
66 #ifndef INC_FREERTOS_H
67 #error "include FreeRTOS.h must appear in source files before include message_buffer.h"
70 /* Message buffers are built onto of stream buffers. */
71 #include "stream_buffer.h"
74 #if defined( __cplusplus )
80 * Type by which message buffers are referenced. For example, a call to
81 * xMessageBufferCreate() returns an MessageBufferHandle_t variable that can
82 * then be used as a parameter to xMessageBufferSend(), xMessageBufferReceive(),
83 * etc. Message buffer is essentially built as a stream buffer hence its handle
84 * is also set to same type as a stream buffer handle.
86 typedef StreamBufferHandle_t MessageBufferHandle_t;
88 /*-----------------------------------------------------------*/
94 * MessageBufferHandle_t xMessageBufferCreate( size_t xBufferSizeBytes );
97 * Creates a new message buffer using dynamically allocated memory. See
98 * xMessageBufferCreateStatic() for a version that uses statically allocated
99 * memory (memory that is allocated at compile time).
101 * configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 or left undefined in
102 * FreeRTOSConfig.h for xMessageBufferCreate() to be available.
103 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
104 * xMessageBufferCreate() to be available.
106 * @param xBufferSizeBytes The total number of bytes (not messages) the message
107 * buffer will be able to hold at any one time. When a message is written to
108 * the message buffer an additional sizeof( size_t ) bytes are also written to
109 * store the message's length. sizeof( size_t ) is typically 4 bytes on a
110 * 32-bit architecture, so on most 32-bit architectures a 10 byte message will
111 * take up 14 bytes of message buffer space.
113 * @param pxSendCompletedCallback Callback invoked when a send operation to the
114 * message buffer is complete. If the parameter is NULL or xMessageBufferCreate()
115 * is called without the parameter, then it will use the default implementation
116 * provided by sbSEND_COMPLETED macro. To enable the callback,
117 * configUSE_SB_COMPLETED_CALLBACK must be set to 1 in FreeRTOSConfig.h.
119 * @param pxReceiveCompletedCallback Callback invoked when a receive operation from
120 * the message buffer is complete. If the parameter is NULL or xMessageBufferCreate()
121 * is called without the parameter, it will use the default implementation provided
122 * by sbRECEIVE_COMPLETED macro. To enable the callback,
123 * configUSE_SB_COMPLETED_CALLBACK must be set to 1 in FreeRTOSConfig.h.
125 * @return If NULL is returned, then the message buffer cannot be created
126 * because there is insufficient heap memory available for FreeRTOS to allocate
127 * the message buffer data structures and storage area. A non-NULL value being
128 * returned indicates that the message buffer has been created successfully -
129 * the returned value should be stored as the handle to the created message
135 * void vAFunction( void )
137 * MessageBufferHandle_t xMessageBuffer;
138 * const size_t xMessageBufferSizeBytes = 100;
140 * // Create a message buffer that can hold 100 bytes. The memory used to hold
141 * // both the message buffer structure and the messages themselves is allocated
142 * // dynamically. Each message added to the buffer consumes an additional 4
143 * // bytes which are used to hold the length of the message.
144 * xMessageBuffer = xMessageBufferCreate( xMessageBufferSizeBytes );
146 * if( xMessageBuffer == NULL )
148 * // There was not enough heap memory space available to create the
153 * // The message buffer was created successfully and can now be used.
157 * \defgroup xMessageBufferCreate xMessageBufferCreate
158 * \ingroup MessageBufferManagement
160 #define xMessageBufferCreate( xBufferSizeBytes ) \
161 xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( size_t ) 0, sbTYPE_MESSAGE_BUFFER, NULL, NULL )
163 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
164 #define xMessageBufferCreateWithCallback( xBufferSizeBytes, pxSendCompletedCallback, pxReceiveCompletedCallback ) \
165 xStreamBufferGenericCreate( ( xBufferSizeBytes ), ( size_t ) 0, sbTYPE_MESSAGE_BUFFER, ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
172 * MessageBufferHandle_t xMessageBufferCreateStatic( size_t xBufferSizeBytes,
173 * uint8_t *pucMessageBufferStorageArea,
174 * StaticMessageBuffer_t *pxStaticMessageBuffer );
176 * Creates a new message buffer using statically allocated memory. See
177 * xMessageBufferCreate() for a version that uses dynamically allocated memory.
179 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
180 * xMessageBufferCreateStatic() to be available.
182 * @param xBufferSizeBytes The size, in bytes, of the buffer pointed to by the
183 * pucMessageBufferStorageArea parameter. When a message is written to the
184 * message buffer an additional sizeof( size_t ) bytes are also written to store
185 * the message's length. sizeof( size_t ) is typically 4 bytes on a 32-bit
186 * architecture, so on most 32-bit architecture a 10 byte message will take up
187 * 14 bytes of message buffer space. The maximum number of bytes that can be
188 * stored in the message buffer is actually (xBufferSizeBytes - 1).
190 * @param pucMessageBufferStorageArea Must point to a uint8_t array that is at
191 * least xBufferSizeBytes big. This is the array to which messages are
192 * copied when they are written to the message buffer.
194 * @param pxStaticMessageBuffer Must point to a variable of type
195 * StaticMessageBuffer_t, which will be used to hold the message buffer's data
198 * @param pxSendCompletedCallback Callback invoked when a new message is sent to the message buffer.
199 * If the parameter is NULL or xMessageBufferCreate() is called without the parameter, then it will use the default
200 * implementation provided by sbSEND_COMPLETED macro. To enable the callback,
201 * configUSE_SB_COMPLETED_CALLBACK must be set to 1 in FreeRTOSConfig.h.
203 * @param pxReceiveCompletedCallback Callback invoked when a message is read from a
204 * message buffer. If the parameter is NULL or xMessageBufferCreate() is called without the parameter, it will
205 * use the default implementation provided by sbRECEIVE_COMPLETED macro. To enable the callback,
206 * configUSE_SB_COMPLETED_CALLBACK must be set to 1 in FreeRTOSConfig.h.
208 * @return If the message buffer is created successfully then a handle to the
209 * created message buffer is returned. If either pucMessageBufferStorageArea or
210 * pxStaticmessageBuffer are NULL then NULL is returned.
215 * // Used to dimension the array used to hold the messages. The available space
216 * // will actually be one less than this, so 999.
217 #define STORAGE_SIZE_BYTES 1000
219 * // Defines the memory that will actually hold the messages within the message
221 * static uint8_t ucStorageBuffer[ STORAGE_SIZE_BYTES ];
223 * // The variable used to hold the message buffer structure.
224 * StaticMessageBuffer_t xMessageBufferStruct;
226 * void MyFunction( void )
228 * MessageBufferHandle_t xMessageBuffer;
230 * xMessageBuffer = xMessageBufferCreateStatic( sizeof( ucStorageBuffer ),
232 * &xMessageBufferStruct );
234 * // As neither the pucMessageBufferStorageArea or pxStaticMessageBuffer
235 * // parameters were NULL, xMessageBuffer will not be NULL, and can be used to
236 * // reference the created message buffer in other message buffer API calls.
238 * // Other code that uses the message buffer can go here.
242 * \defgroup xMessageBufferCreateStatic xMessageBufferCreateStatic
243 * \ingroup MessageBufferManagement
245 #define xMessageBufferCreateStatic( xBufferSizeBytes, pucMessageBufferStorageArea, pxStaticMessageBuffer ) \
246 xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), 0, sbTYPE_MESSAGE_BUFFER, ( pucMessageBufferStorageArea ), ( pxStaticMessageBuffer ), NULL, NULL )
248 #if ( configUSE_SB_COMPLETED_CALLBACK == 1 )
249 #define xMessageBufferCreateStaticWithCallback( xBufferSizeBytes, pucMessageBufferStorageArea, pxStaticMessageBuffer, pxSendCompletedCallback, pxReceiveCompletedCallback ) \
250 xStreamBufferGenericCreateStatic( ( xBufferSizeBytes ), 0, sbTYPE_MESSAGE_BUFFER, ( pucMessageBufferStorageArea ), ( pxStaticMessageBuffer ), ( pxSendCompletedCallback ), ( pxReceiveCompletedCallback ) )
257 * BaseType_t xMessageBufferGetStaticBuffers( MessageBufferHandle_t xMessageBuffer,
258 * uint8_t ** ppucMessageBufferStorageArea,
259 * StaticMessageBuffer_t ** ppxStaticMessageBuffer );
262 * Retrieve pointers to a statically created message buffer's data structure
263 * buffer and storage area buffer. These are the same buffers that are supplied
264 * at the time of creation.
266 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
267 * xMessageBufferGetStaticBuffers() to be available.
269 * @param xMessageBuffer The message buffer for which to retrieve the buffers.
271 * @param ppucMessageBufferStorageArea Used to return a pointer to the
272 * message buffer's storage area buffer.
274 * @param ppxStaticMessageBuffer Used to return a pointer to the message
275 * buffer's data structure buffer.
277 * @return pdTRUE if buffers were retrieved, pdFALSE otherwise..
279 * \defgroup xMessageBufferGetStaticBuffers xMessageBufferGetStaticBuffers
280 * \ingroup MessageBufferManagement
282 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
283 #define xMessageBufferGetStaticBuffers( xMessageBuffer, ppucMessageBufferStorageArea, ppxStaticMessageBuffer ) \
284 xStreamBufferGetStaticBuffers( ( xMessageBuffer ), ( ppucMessageBufferStorageArea ), ( ppxStaticMessageBuffer ) )
285 #endif /* configSUPPORT_STATIC_ALLOCATION */
291 * size_t xMessageBufferSend( MessageBufferHandle_t xMessageBuffer,
292 * const void *pvTxData,
293 * size_t xDataLengthBytes,
294 * TickType_t xTicksToWait );
297 * Sends a discrete message to the message buffer. The message can be any
298 * length that fits within the buffer's free space, and is copied into the
301 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
302 * implementation (so also the message buffer implementation, as message buffers
303 * are built on top of stream buffers) assumes there is only one task or
304 * interrupt that will write to the buffer (the writer), and only one task or
305 * interrupt that will read from the buffer (the reader). It is safe for the
306 * writer and reader to be different tasks or interrupts, but, unlike other
307 * FreeRTOS objects, it is not safe to have multiple different writers or
308 * multiple different readers. If there are to be multiple different writers
309 * then the application writer must place each call to a writing API function
310 * (such as xMessageBufferSend()) inside a critical section and set the send
311 * block time to 0. Likewise, if there are to be multiple different readers
312 * then the application writer must place each call to a reading API function
313 * (such as xMessageBufferRead()) inside a critical section and set the receive
316 * Use xMessageBufferSend() to write to a message buffer from a task. Use
317 * xMessageBufferSendFromISR() to write to a message buffer from an interrupt
318 * service routine (ISR).
320 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
321 * xMessageBufferSend() to be available.
323 * @param xMessageBuffer The handle of the message buffer to which a message is
326 * @param pvTxData A pointer to the message that is to be copied into the
329 * @param xDataLengthBytes The length of the message. That is, the number of
330 * bytes to copy from pvTxData into the message buffer. When a message is
331 * written to the message buffer an additional sizeof( size_t ) bytes are also
332 * written to store the message's length. sizeof( size_t ) is typically 4 bytes
333 * on a 32-bit architecture, so on most 32-bit architecture setting
334 * xDataLengthBytes to 20 will reduce the free space in the message buffer by 24
335 * bytes (20 bytes of message data and 4 bytes to hold the message length).
337 * @param xTicksToWait The maximum amount of time the calling task should remain
338 * in the Blocked state to wait for enough space to become available in the
339 * message buffer, should the message buffer have insufficient space when
340 * xMessageBufferSend() is called. The calling task will never block if
341 * xTicksToWait is zero. The block time is specified in tick periods, so the
342 * absolute time it represents is dependent on the tick frequency. The macro
343 * pdMS_TO_TICKS() can be used to convert a time specified in milliseconds into
344 * a time specified in ticks. Setting xTicksToWait to portMAX_DELAY will cause
345 * the task to wait indefinitely (without timing out), provided
346 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. Tasks do not use any
347 * CPU time when they are in the Blocked state.
349 * @return The number of bytes written to the message buffer. If the call to
350 * xMessageBufferSend() times out before there was enough space to write the
351 * message into the message buffer then zero is returned. If the call did not
352 * time out then xDataLengthBytes is returned.
356 * void vAFunction( MessageBufferHandle_t xMessageBuffer )
359 * uint8_t ucArrayToSend[] = { 0, 1, 2, 3 };
360 * char *pcStringToSend = "String to send";
361 * const TickType_t x100ms = pdMS_TO_TICKS( 100 );
363 * // Send an array to the message buffer, blocking for a maximum of 100ms to
364 * // wait for enough space to be available in the message buffer.
365 * xBytesSent = xMessageBufferSend( xMessageBuffer, ( void * ) ucArrayToSend, sizeof( ucArrayToSend ), x100ms );
367 * if( xBytesSent != sizeof( ucArrayToSend ) )
369 * // The call to xMessageBufferSend() times out before there was enough
370 * // space in the buffer for the data to be written.
373 * // Send the string to the message buffer. Return immediately if there is
374 * // not enough space in the buffer.
375 * xBytesSent = xMessageBufferSend( xMessageBuffer, ( void * ) pcStringToSend, strlen( pcStringToSend ), 0 );
377 * if( xBytesSent != strlen( pcStringToSend ) )
379 * // The string could not be added to the message buffer because there was
380 * // not enough free space in the buffer.
384 * \defgroup xMessageBufferSend xMessageBufferSend
385 * \ingroup MessageBufferManagement
387 #define xMessageBufferSend( xMessageBuffer, pvTxData, xDataLengthBytes, xTicksToWait ) \
388 xStreamBufferSend( ( xMessageBuffer ), ( pvTxData ), ( xDataLengthBytes ), ( xTicksToWait ) )
394 * size_t xMessageBufferSendFromISR( MessageBufferHandle_t xMessageBuffer,
395 * const void *pvTxData,
396 * size_t xDataLengthBytes,
397 * BaseType_t *pxHigherPriorityTaskWoken );
400 * Interrupt safe version of the API function that sends a discrete message to
401 * the message buffer. The message can be any length that fits within the
402 * buffer's free space, and is copied into the buffer.
404 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
405 * implementation (so also the message buffer implementation, as message buffers
406 * are built on top of stream buffers) assumes there is only one task or
407 * interrupt that will write to the buffer (the writer), and only one task or
408 * interrupt that will read from the buffer (the reader). It is safe for the
409 * writer and reader to be different tasks or interrupts, but, unlike other
410 * FreeRTOS objects, it is not safe to have multiple different writers or
411 * multiple different readers. If there are to be multiple different writers
412 * then the application writer must place each call to a writing API function
413 * (such as xMessageBufferSend()) inside a critical section and set the send
414 * block time to 0. Likewise, if there are to be multiple different readers
415 * then the application writer must place each call to a reading API function
416 * (such as xMessageBufferRead()) inside a critical section and set the receive
419 * Use xMessageBufferSend() to write to a message buffer from a task. Use
420 * xMessageBufferSendFromISR() to write to a message buffer from an interrupt
421 * service routine (ISR).
423 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
424 * xMessageBufferSendFromISR() to be available.
426 * @param xMessageBuffer The handle of the message buffer to which a message is
429 * @param pvTxData A pointer to the message that is to be copied into the
432 * @param xDataLengthBytes The length of the message. That is, the number of
433 * bytes to copy from pvTxData into the message buffer. When a message is
434 * written to the message buffer an additional sizeof( size_t ) bytes are also
435 * written to store the message's length. sizeof( size_t ) is typically 4 bytes
436 * on a 32-bit architecture, so on most 32-bit architecture setting
437 * xDataLengthBytes to 20 will reduce the free space in the message buffer by 24
438 * bytes (20 bytes of message data and 4 bytes to hold the message length).
440 * @param pxHigherPriorityTaskWoken It is possible that a message buffer will
441 * have a task blocked on it waiting for data. Calling
442 * xMessageBufferSendFromISR() can make data available, and so cause a task that
443 * was waiting for data to leave the Blocked state. If calling
444 * xMessageBufferSendFromISR() causes a task to leave the Blocked state, and the
445 * unblocked task has a priority higher than the currently executing task (the
446 * task that was interrupted), then, internally, xMessageBufferSendFromISR()
447 * will set *pxHigherPriorityTaskWoken to pdTRUE. If
448 * xMessageBufferSendFromISR() sets this value to pdTRUE, then normally a
449 * context switch should be performed before the interrupt is exited. This will
450 * ensure that the interrupt returns directly to the highest priority Ready
451 * state task. *pxHigherPriorityTaskWoken should be set to pdFALSE before it
452 * is passed into the function. See the code example below for an example.
454 * @return The number of bytes actually written to the message buffer. If the
455 * message buffer didn't have enough free space for the message to be stored
456 * then 0 is returned, otherwise xDataLengthBytes is returned.
460 * // A message buffer that has already been created.
461 * MessageBufferHandle_t xMessageBuffer;
463 * void vAnInterruptServiceRoutine( void )
466 * char *pcStringToSend = "String to send";
467 * BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
469 * // Attempt to send the string to the message buffer.
470 * xBytesSent = xMessageBufferSendFromISR( xMessageBuffer,
471 * ( void * ) pcStringToSend,
472 * strlen( pcStringToSend ),
473 * &xHigherPriorityTaskWoken );
475 * if( xBytesSent != strlen( pcStringToSend ) )
477 * // The string could not be added to the message buffer because there was
478 * // not enough free space in the buffer.
481 * // If xHigherPriorityTaskWoken was set to pdTRUE inside
482 * // xMessageBufferSendFromISR() then a task that has a priority above the
483 * // priority of the currently executing task was unblocked and a context
484 * // switch should be performed to ensure the ISR returns to the unblocked
485 * // task. In most FreeRTOS ports this is done by simply passing
486 * // xHigherPriorityTaskWoken into portYIELD_FROM_ISR(), which will test the
487 * // variables value, and perform the context switch if necessary. Check the
488 * // documentation for the port in use for port specific instructions.
489 * portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
492 * \defgroup xMessageBufferSendFromISR xMessageBufferSendFromISR
493 * \ingroup MessageBufferManagement
495 #define xMessageBufferSendFromISR( xMessageBuffer, pvTxData, xDataLengthBytes, pxHigherPriorityTaskWoken ) \
496 xStreamBufferSendFromISR( ( xMessageBuffer ), ( pvTxData ), ( xDataLengthBytes ), ( pxHigherPriorityTaskWoken ) )
502 * size_t xMessageBufferReceive( MessageBufferHandle_t xMessageBuffer,
504 * size_t xBufferLengthBytes,
505 * TickType_t xTicksToWait );
508 * Receives a discrete message from a message buffer. Messages can be of
509 * variable length and are copied out of the buffer.
511 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
512 * implementation (so also the message buffer implementation, as message buffers
513 * are built on top of stream buffers) assumes there is only one task or
514 * interrupt that will write to the buffer (the writer), and only one task or
515 * interrupt that will read from the buffer (the reader). It is safe for the
516 * writer and reader to be different tasks or interrupts, but, unlike other
517 * FreeRTOS objects, it is not safe to have multiple different writers or
518 * multiple different readers. If there are to be multiple different writers
519 * then the application writer must place each call to a writing API function
520 * (such as xMessageBufferSend()) inside a critical section and set the send
521 * block time to 0. Likewise, if there are to be multiple different readers
522 * then the application writer must place each call to a reading API function
523 * (such as xMessageBufferRead()) inside a critical section and set the receive
526 * Use xMessageBufferReceive() to read from a message buffer from a task. Use
527 * xMessageBufferReceiveFromISR() to read from a message buffer from an
528 * interrupt service routine (ISR).
530 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
531 * xMessageBufferReceive() to be available.
533 * @param xMessageBuffer The handle of the message buffer from which a message
536 * @param pvRxData A pointer to the buffer into which the received message is
539 * @param xBufferLengthBytes The length of the buffer pointed to by the pvRxData
540 * parameter. This sets the maximum length of the message that can be received.
541 * If xBufferLengthBytes is too small to hold the next message then the message
542 * will be left in the message buffer and 0 will be returned.
544 * @param xTicksToWait The maximum amount of time the task should remain in the
545 * Blocked state to wait for a message, should the message buffer be empty.
546 * xMessageBufferReceive() will return immediately if xTicksToWait is zero and
547 * the message buffer is empty. The block time is specified in tick periods, so
548 * the absolute time it represents is dependent on the tick frequency. The
549 * macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds
550 * into a time specified in ticks. Setting xTicksToWait to portMAX_DELAY will
551 * cause the task to wait indefinitely (without timing out), provided
552 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. Tasks do not use any
553 * CPU time when they are in the Blocked state.
555 * @return The length, in bytes, of the message read from the message buffer, if
556 * any. If xMessageBufferReceive() times out before a message became available
557 * then zero is returned. If the length of the message is greater than
558 * xBufferLengthBytes then the message will be left in the message buffer and
563 * void vAFunction( MessageBuffer_t xMessageBuffer )
565 * uint8_t ucRxData[ 20 ];
566 * size_t xReceivedBytes;
567 * const TickType_t xBlockTime = pdMS_TO_TICKS( 20 );
569 * // Receive the next message from the message buffer. Wait in the Blocked
570 * // state (so not using any CPU processing time) for a maximum of 100ms for
571 * // a message to become available.
572 * xReceivedBytes = xMessageBufferReceive( xMessageBuffer,
573 * ( void * ) ucRxData,
574 * sizeof( ucRxData ),
577 * if( xReceivedBytes > 0 )
579 * // A ucRxData contains a message that is xReceivedBytes long. Process
580 * // the message here....
584 * \defgroup xMessageBufferReceive xMessageBufferReceive
585 * \ingroup MessageBufferManagement
587 #define xMessageBufferReceive( xMessageBuffer, pvRxData, xBufferLengthBytes, xTicksToWait ) \
588 xStreamBufferReceive( ( xMessageBuffer ), ( pvRxData ), ( xBufferLengthBytes ), ( xTicksToWait ) )
595 * size_t xMessageBufferReceiveFromISR( MessageBufferHandle_t xMessageBuffer,
597 * size_t xBufferLengthBytes,
598 * BaseType_t *pxHigherPriorityTaskWoken );
601 * An interrupt safe version of the API function that receives a discrete
602 * message from a message buffer. Messages can be of variable length and are
603 * copied out of the buffer.
605 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
606 * implementation (so also the message buffer implementation, as message buffers
607 * are built on top of stream buffers) assumes there is only one task or
608 * interrupt that will write to the buffer (the writer), and only one task or
609 * interrupt that will read from the buffer (the reader). It is safe for the
610 * writer and reader to be different tasks or interrupts, but, unlike other
611 * FreeRTOS objects, it is not safe to have multiple different writers or
612 * multiple different readers. If there are to be multiple different writers
613 * then the application writer must place each call to a writing API function
614 * (such as xMessageBufferSend()) inside a critical section and set the send
615 * block time to 0. Likewise, if there are to be multiple different readers
616 * then the application writer must place each call to a reading API function
617 * (such as xMessageBufferRead()) inside a critical section and set the receive
620 * Use xMessageBufferReceive() to read from a message buffer from a task. Use
621 * xMessageBufferReceiveFromISR() to read from a message buffer from an
622 * interrupt service routine (ISR).
624 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
625 * xMessageBufferReceiveFromISR() to be available.
627 * @param xMessageBuffer The handle of the message buffer from which a message
630 * @param pvRxData A pointer to the buffer into which the received message is
633 * @param xBufferLengthBytes The length of the buffer pointed to by the pvRxData
634 * parameter. This sets the maximum length of the message that can be received.
635 * If xBufferLengthBytes is too small to hold the next message then the message
636 * will be left in the message buffer and 0 will be returned.
638 * @param pxHigherPriorityTaskWoken It is possible that a message buffer will
639 * have a task blocked on it waiting for space to become available. Calling
640 * xMessageBufferReceiveFromISR() can make space available, and so cause a task
641 * that is waiting for space to leave the Blocked state. If calling
642 * xMessageBufferReceiveFromISR() causes a task to leave the Blocked state, and
643 * the unblocked task has a priority higher than the currently executing task
644 * (the task that was interrupted), then, internally,
645 * xMessageBufferReceiveFromISR() will set *pxHigherPriorityTaskWoken to pdTRUE.
646 * If xMessageBufferReceiveFromISR() sets this value to pdTRUE, then normally a
647 * context switch should be performed before the interrupt is exited. That will
648 * ensure the interrupt returns directly to the highest priority Ready state
649 * task. *pxHigherPriorityTaskWoken should be set to pdFALSE before it is
650 * passed into the function. See the code example below for an example.
652 * @return The length, in bytes, of the message read from the message buffer, if
657 * // A message buffer that has already been created.
658 * MessageBuffer_t xMessageBuffer;
660 * void vAnInterruptServiceRoutine( void )
662 * uint8_t ucRxData[ 20 ];
663 * size_t xReceivedBytes;
664 * BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
666 * // Receive the next message from the message buffer.
667 * xReceivedBytes = xMessageBufferReceiveFromISR( xMessageBuffer,
668 * ( void * ) ucRxData,
669 * sizeof( ucRxData ),
670 * &xHigherPriorityTaskWoken );
672 * if( xReceivedBytes > 0 )
674 * // A ucRxData contains a message that is xReceivedBytes long. Process
675 * // the message here....
678 * // If xHigherPriorityTaskWoken was set to pdTRUE inside
679 * // xMessageBufferReceiveFromISR() then a task that has a priority above the
680 * // priority of the currently executing task was unblocked and a context
681 * // switch should be performed to ensure the ISR returns to the unblocked
682 * // task. In most FreeRTOS ports this is done by simply passing
683 * // xHigherPriorityTaskWoken into portYIELD_FROM_ISR(), which will test the
684 * // variables value, and perform the context switch if necessary. Check the
685 * // documentation for the port in use for port specific instructions.
686 * portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
689 * \defgroup xMessageBufferReceiveFromISR xMessageBufferReceiveFromISR
690 * \ingroup MessageBufferManagement
692 #define xMessageBufferReceiveFromISR( xMessageBuffer, pvRxData, xBufferLengthBytes, pxHigherPriorityTaskWoken ) \
693 xStreamBufferReceiveFromISR( ( xMessageBuffer ), ( pvRxData ), ( xBufferLengthBytes ), ( pxHigherPriorityTaskWoken ) )
699 * void vMessageBufferDelete( MessageBufferHandle_t xMessageBuffer );
702 * Deletes a message buffer that was previously created using a call to
703 * xMessageBufferCreate() or xMessageBufferCreateStatic(). If the message
704 * buffer was created using dynamic memory (that is, by xMessageBufferCreate()),
705 * then the allocated memory is freed.
707 * A message buffer handle must not be used after the message buffer has been
710 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
711 * vMessageBufferDelete() to be available.
713 * @param xMessageBuffer The handle of the message buffer to be deleted.
716 #define vMessageBufferDelete( xMessageBuffer ) \
717 vStreamBufferDelete( xMessageBuffer )
722 * BaseType_t xMessageBufferIsFull( MessageBufferHandle_t xMessageBuffer );
725 * Tests to see if a message buffer is full. A message buffer is full if it
726 * cannot accept any more messages, of any size, until space is made available
727 * by a message being removed from the message buffer.
729 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
730 * xMessageBufferIsFull() to be available.
732 * @param xMessageBuffer The handle of the message buffer being queried.
734 * @return If the message buffer referenced by xMessageBuffer is full then
735 * pdTRUE is returned. Otherwise pdFALSE is returned.
737 #define xMessageBufferIsFull( xMessageBuffer ) \
738 xStreamBufferIsFull( xMessageBuffer )
743 * BaseType_t xMessageBufferIsEmpty( MessageBufferHandle_t xMessageBuffer );
746 * Tests to see if a message buffer is empty (does not contain any messages).
748 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
749 * xMessageBufferIsEmpty() to be available.
751 * @param xMessageBuffer The handle of the message buffer being queried.
753 * @return If the message buffer referenced by xMessageBuffer is empty then
754 * pdTRUE is returned. Otherwise pdFALSE is returned.
757 #define xMessageBufferIsEmpty( xMessageBuffer ) \
758 xStreamBufferIsEmpty( xMessageBuffer )
763 * BaseType_t xMessageBufferReset( MessageBufferHandle_t xMessageBuffer );
766 * Resets a message buffer to its initial empty state, discarding any message it
769 * A message buffer can only be reset if there are no tasks blocked on it.
771 * Use xMessageBufferReset() to reset a message buffer from a task.
772 * Use xMessageBufferResetFromISR() to reset a message buffer from an
773 * interrupt service routine (ISR).
775 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
776 * xMessageBufferReset() to be available.
778 * @param xMessageBuffer The handle of the message buffer being reset.
780 * @return If the message buffer was reset then pdPASS is returned. If the
781 * message buffer could not be reset because either there was a task blocked on
782 * the message queue to wait for space to become available, or to wait for a
783 * a message to be available, then pdFAIL is returned.
785 * \defgroup xMessageBufferReset xMessageBufferReset
786 * \ingroup MessageBufferManagement
788 #define xMessageBufferReset( xMessageBuffer ) \
789 xStreamBufferReset( xMessageBuffer )
795 * BaseType_t xMessageBufferResetFromISR( MessageBufferHandle_t xMessageBuffer );
798 * An interrupt safe version of the API function that resets the message buffer.
799 * Resets a message buffer to its initial empty state, discarding any message it
802 * A message buffer can only be reset if there are no tasks blocked on it.
804 * Use xMessageBufferReset() to reset a message buffer from a task.
805 * Use xMessageBufferResetFromISR() to reset a message buffer from an
806 * interrupt service routine (ISR).
808 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
809 * xMessageBufferResetFromISR() to be available.
811 * @param xMessageBuffer The handle of the message buffer being reset.
813 * @return If the message buffer was reset then pdPASS is returned. If the
814 * message buffer could not be reset because either there was a task blocked on
815 * the message queue to wait for space to become available, or to wait for a
816 * a message to be available, then pdFAIL is returned.
818 * \defgroup xMessageBufferResetFromISR xMessageBufferResetFromISR
819 * \ingroup MessageBufferManagement
821 #define xMessageBufferResetFromISR( xMessageBuffer ) \
822 xStreamBufferResetFromISR( xMessageBuffer )
827 * size_t xMessageBufferSpaceAvailable( MessageBufferHandle_t xMessageBuffer );
829 * Returns the number of bytes of free space in the message buffer.
831 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
832 * xMessageBufferSpaceAvailable() to be available.
834 * @param xMessageBuffer The handle of the message buffer being queried.
836 * @return The number of bytes that can be written to the message buffer before
837 * the message buffer would be full. When a message is written to the message
838 * buffer an additional sizeof( size_t ) bytes are also written to store the
839 * message's length. sizeof( size_t ) is typically 4 bytes on a 32-bit
840 * architecture, so if xMessageBufferSpacesAvailable() returns 10, then the size
841 * of the largest message that can be written to the message buffer is 6 bytes.
843 * \defgroup xMessageBufferSpaceAvailable xMessageBufferSpaceAvailable
844 * \ingroup MessageBufferManagement
846 #define xMessageBufferSpaceAvailable( xMessageBuffer ) \
847 xStreamBufferSpacesAvailable( xMessageBuffer )
848 #define xMessageBufferSpacesAvailable( xMessageBuffer ) \
849 xStreamBufferSpacesAvailable( xMessageBuffer ) /* Corrects typo in original macro name. */
854 * size_t xMessageBufferNextLengthBytes( MessageBufferHandle_t xMessageBuffer );
856 * Returns the length (in bytes) of the next message in a message buffer.
857 * Useful if xMessageBufferReceive() returned 0 because the size of the buffer
858 * passed into xMessageBufferReceive() was too small to hold the next message.
860 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
861 * xMessageBufferNextLengthBytes() to be available.
863 * @param xMessageBuffer The handle of the message buffer being queried.
865 * @return The length (in bytes) of the next message in the message buffer, or 0
866 * if the message buffer is empty.
868 * \defgroup xMessageBufferNextLengthBytes xMessageBufferNextLengthBytes
869 * \ingroup MessageBufferManagement
871 #define xMessageBufferNextLengthBytes( xMessageBuffer ) \
872 xStreamBufferNextMessageLengthBytes( xMessageBuffer )
878 * BaseType_t xMessageBufferSendCompletedFromISR( MessageBufferHandle_t xMessageBuffer, BaseType_t *pxHigherPriorityTaskWoken );
881 * For advanced users only.
883 * The sbSEND_COMPLETED() macro is called from within the FreeRTOS APIs when
884 * data is sent to a message buffer or stream buffer. If there was a task that
885 * was blocked on the message or stream buffer waiting for data to arrive then
886 * the sbSEND_COMPLETED() macro sends a notification to the task to remove it
887 * from the Blocked state. xMessageBufferSendCompletedFromISR() does the same
888 * thing. It is provided to enable application writers to implement their own
889 * version of sbSEND_COMPLETED(), and MUST NOT BE USED AT ANY OTHER TIME.
891 * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
892 * additional information.
894 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
895 * xMessageBufferSendCompletedFromISR() to be available.
897 * @param xMessageBuffer The handle of the stream buffer to which data was
900 * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
901 * initialised to pdFALSE before it is passed into
902 * xMessageBufferSendCompletedFromISR(). If calling
903 * xMessageBufferSendCompletedFromISR() removes a task from the Blocked state,
904 * and the task has a priority above the priority of the currently running task,
905 * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
906 * context switch should be performed before exiting the ISR.
908 * @return If a task was removed from the Blocked state then pdTRUE is returned.
909 * Otherwise pdFALSE is returned.
911 * \defgroup xMessageBufferSendCompletedFromISR xMessageBufferSendCompletedFromISR
912 * \ingroup StreamBufferManagement
914 #define xMessageBufferSendCompletedFromISR( xMessageBuffer, pxHigherPriorityTaskWoken ) \
915 xStreamBufferSendCompletedFromISR( ( xMessageBuffer ), ( pxHigherPriorityTaskWoken ) )
921 * BaseType_t xMessageBufferReceiveCompletedFromISR( MessageBufferHandle_t xMessageBuffer, BaseType_t *pxHigherPriorityTaskWoken );
924 * For advanced users only.
926 * The sbRECEIVE_COMPLETED() macro is called from within the FreeRTOS APIs when
927 * data is read out of a message buffer or stream buffer. If there was a task
928 * that was blocked on the message or stream buffer waiting for data to arrive
929 * then the sbRECEIVE_COMPLETED() macro sends a notification to the task to
930 * remove it from the Blocked state. xMessageBufferReceiveCompletedFromISR()
931 * does the same thing. It is provided to enable application writers to
932 * implement their own version of sbRECEIVE_COMPLETED(), and MUST NOT BE USED AT
935 * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
936 * additional information.
938 * configUSE_STREAM_BUFFERS must be set to 1 in for FreeRTOSConfig.h for
939 * xMessageBufferReceiveCompletedFromISR() to be available.
941 * @param xMessageBuffer The handle of the stream buffer from which data was
944 * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
945 * initialised to pdFALSE before it is passed into
946 * xMessageBufferReceiveCompletedFromISR(). If calling
947 * xMessageBufferReceiveCompletedFromISR() removes a task from the Blocked state,
948 * and the task has a priority above the priority of the currently running task,
949 * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
950 * context switch should be performed before exiting the ISR.
952 * @return If a task was removed from the Blocked state then pdTRUE is returned.
953 * Otherwise pdFALSE is returned.
955 * \defgroup xMessageBufferReceiveCompletedFromISR xMessageBufferReceiveCompletedFromISR
956 * \ingroup StreamBufferManagement
958 #define xMessageBufferReceiveCompletedFromISR( xMessageBuffer, pxHigherPriorityTaskWoken ) \
959 xStreamBufferReceiveCompletedFromISR( ( xMessageBuffer ), ( pxHigherPriorityTaskWoken ) )
962 #if defined( __cplusplus )
967 #endif /* !defined( FREERTOS_MESSAGE_BUFFER_H ) */