xMessageBufferCreate / xMessageBufferCreateWithCallback
[RTOS Message Buffer API]
message_buffer.h
MessageBufferHandle_t xMessageBufferCreate( size_t xBufferSizeBytes );
MessageBufferHandle_t xMessageBufferCreateWithCallback(
size_t xBufferSizeBytes,
StreamBufferCallbackFunction_t pxSendCompletedCallback,
StreamBufferCallbackFunction_t pxReceiveCompletedCallback );
Creates a new message buffer that uses dynamically allocated memory.
Message buffers execute a callback upon completion of each send and receive operation. Message buffers created using the
xMessageBufferCreate() API share the same send and receive completed callback functions, which are defined using the
sbSEND_COMPLETED() and sbRECEIVE_COMPLETED() macros. Message buffers created using the xMessageBufferCreateWithCallback() API
can each have their own unique send and receive completed callback functions. See
xMessageBufferCreateStatic() and xMessageBufferCreateStaticWithCallback()
for corresponding versions that use statically allocated memory (memory that is allocated at compile time).
configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 or left undefined in
FreeRTOSConfig.h for xMessageBufferCreate() to be available. Additionally, configUSE_SB_COMPLETED_CALLBACK must be set to 1 in
FreeRTOSConfig.h for xMessageBufferCreateWithCallback() to be available.
Message buffer functionality is enabled by including the FreeRTOS/source/stream_buffer.c source file in the build (as message
buffers use stream buffers).
Parameters:
-
xBufferSizeBytes
- The total number of bytes (not messages) the message buffer will be able to hold at any one time. When a message is written
to the message buffer an additional sizeof( size_t ) bytes are also written to store the message's length. sizeof( size_t )
is typically 4 bytes on a 32-bit architecture, so on most 32-bit architectures a 10 byte message will take up 14 bytes of
message buffer space.
pxSendCompletedCallback
- The callback function invoked when a message is written to the message buffer. If the parameter is NULL, the default
implementation provided by the sbSEND_COMPLETED macro is used. The send completed callback function must have the prototype
defined by StreamBufferCallbackFunction_t, which is:
void vSendCallbackFunction( MessageBufferHandle_t xMessageBuffer,
BaseType_t xIsInsideISR,
BaseType_t * const pxHigherPriorityTaskWoken );
pxReceiveCompletedCallback
- The callback function invoked when a message is read from a message buffer. If the parameter is NULL, the default
implementation provided by the sbRECEIVE_COMPLETED macro is used. The receive completed callback function must have the
prototype defined by StreamBufferCallbackFunction_t, which is:
void vReceiveCallbackFunction( MessageBufferHandle_t xMessageBuffer,
BaseType_t xIsInsideISR,
BaseType_t * const pxHigherPriorityTaskWoken );
Returns:
If NULL is returned, then the message buffer cannot be created because there is insufficient heap memory available for FreeRTOS
to allocate the message buffer data structures and storage area. A non-NULL value being returned indicates that the message
buffer has been created successfully - the returned value should be stored as the handle to the created message buffer.
Example usage:
void vSendCallbackFunction( MessageBufferHandle_t xMessageBuffer,
BaseType_t xIsInsideISR,
BaseType_t * const pxHigherPriorityTaskWoken )
{
}
void vReceiveCallbackFunction( MessageBufferHandle_t xMessageBuffer,
BaseType_t xIsInsideISR,
BaseType_t * const pxHigherPriorityTaskWoken )
{
}
void vAFunction( void )
{
MessageBufferHandle_t xMessageBuffer, xMessageBufferWithCallback;
const size_t xMessageBufferSizeBytes = 100;
xMessageBuffer = xMessageBufferCreate( xMessageBufferSizeBytes );
if( xMessageBuffer == NULL )
{
}
else
{
}
xMessageBufferWithCallback = xMessageBufferCreateWithCallback(
xMessageBufferSizeBytes,
vSendCallbackFunction,
vReceiveCallbackFunction );
if( xMessageBufferWithCallback == NULL )
{
}
else
{
}
}
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.