2 * FreeRTOS Kernel V10.0.1
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
30 * Message buffers build functionality on top of FreeRTOS stream buffers.
\r
31 * Whereas stream buffers are used to send a continuous stream of data from one
\r
32 * task or interrupt to another, message buffers are used to send variable
\r
33 * length discrete messages from one task or interrupt to another. Their
\r
34 * implementation is light weight, making them particularly suited for interrupt
\r
35 * to task and core to core communication scenarios.
\r
37 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
38 * implementation (so also the message buffer implementation, as message buffers
\r
39 * are built on top of stream buffers) assumes there is only one task or
\r
40 * interrupt that will write to the buffer (the writer), and only one task or
\r
41 * interrupt that will read from the buffer (the reader). It is safe for the
\r
42 * writer and reader to be different tasks or interrupts, but, unlike other
\r
43 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
44 * multiple different readers. If there are to be multiple different writers
\r
45 * then the application writer must place each call to a writing API function
\r
46 * (such as xMessageBufferSend()) inside a critical section and set the send
\r
47 * block time to 0. Likewise, if there are to be multiple different readers
\r
48 * then the application writer must place each call to a reading API function
\r
49 * (such as xMessageBufferRead()) inside a critical section and set the receive
\r
52 * Message buffers hold variable length messages. To enable that, when a
\r
53 * message is written to the message buffer an additional sizeof( size_t ) bytes
\r
54 * are also written to store the message's length (that happens internally, with
\r
55 * the API function). sizeof( size_t ) is typically 4 bytes on a 32-bit
\r
56 * architecture, so writing a 10 byte message to a message buffer on a 32-bit
\r
57 * architecture will actually reduce the available space in the message buffer
\r
58 * by 14 bytes (10 byte are used by the message, and 4 bytes to hold the length
\r
62 #ifndef FREERTOS_MESSAGE_BUFFER_H
\r
63 #define FREERTOS_MESSAGE_BUFFER_H
\r
65 /* Message buffers are built onto of stream buffers. */
\r
66 #include "stream_buffer.h"
\r
68 #if defined( __cplusplus )
\r
73 * Type by which message buffers are referenced. For example, a call to
\r
74 * xMessageBufferCreate() returns an MessageBufferHandle_t variable that can
\r
75 * then be used as a parameter to xMessageBufferSend(), xMessageBufferReceive(),
\r
78 typedef void * MessageBufferHandle_t;
\r
80 /*-----------------------------------------------------------*/
\r
86 MessageBufferHandle_t xMessageBufferCreate( size_t xBufferSizeBytes );
\r
89 * Creates a new message buffer using dynamically allocated memory. See
\r
90 * xMessageBufferCreateStatic() for a version that uses statically allocated
\r
91 * memory (memory that is allocated at compile time).
\r
93 * configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 or left undefined in
\r
94 * FreeRTOSConfig.h for xMessageBufferCreate() to be available.
\r
96 * @param xBufferSizeBytes The total number of bytes (not messages) the message
\r
97 * buffer will be able to hold at any one time. When a message is written to
\r
98 * the message buffer an additional sizeof( size_t ) bytes are also written to
\r
99 * store the message's length. sizeof( size_t ) is typically 4 bytes on a
\r
100 * 32-bit architecture, so on most 32-bit architectures a 10 byte message will
\r
101 * take up 14 bytes of message buffer space.
\r
103 * @return If NULL is returned, then the message buffer cannot be created
\r
104 * because there is insufficient heap memory available for FreeRTOS to allocate
\r
105 * the message buffer data structures and storage area. A non-NULL value being
\r
106 * returned indicates that the message buffer has been created successfully -
\r
107 * the returned value should be stored as the handle to the created message
\r
113 void vAFunction( void )
\r
115 MessageBufferHandle_t xMessageBuffer;
\r
116 const size_t xMessageBufferSizeBytes = 100;
\r
118 // Create a message buffer that can hold 100 bytes. The memory used to hold
\r
119 // both the message buffer structure and the messages themselves is allocated
\r
120 // dynamically. Each message added to the buffer consumes an additional 4
\r
121 // bytes which are used to hold the lengh of the message.
\r
122 xMessageBuffer = xMessageBufferCreate( xMessageBufferSizeBytes );
\r
124 if( xMessageBuffer == NULL )
\r
126 // There was not enough heap memory space available to create the
\r
131 // The message buffer was created successfully and can now be used.
\r
135 * \defgroup xMessageBufferCreate xMessageBufferCreate
\r
136 * \ingroup MessageBufferManagement
\r
138 #define xMessageBufferCreate( xBufferSizeBytes ) ( MessageBufferHandle_t ) xStreamBufferGenericCreate( xBufferSizeBytes, ( size_t ) 0, pdTRUE )
\r
144 MessageBufferHandle_t xMessageBufferCreateStatic( size_t xBufferSizeBytes,
\r
145 uint8_t *pucMessageBufferStorageArea,
\r
146 StaticMessageBuffer_t *pxStaticMessageBuffer );
\r
148 * Creates a new message buffer using statically allocated memory. See
\r
149 * xMessageBufferCreate() for a version that uses dynamically allocated memory.
\r
151 * @param xBufferSizeBytes The size, in bytes, of the buffer pointed to by the
\r
152 * pucMessageBufferStorageArea parameter. When a message is written to the
\r
153 * message buffer an additional sizeof( size_t ) bytes are also written to store
\r
154 * the message's length. sizeof( size_t ) is typically 4 bytes on a 32-bit
\r
155 * architecture, so on most 32-bit architecture a 10 byte message will take up
\r
156 * 14 bytes of message buffer space. The maximum number of bytes that can be
\r
157 * stored in the message buffer is actually (xBufferSizeBytes - 1).
\r
159 * @param pucMessageBufferStorageArea Must point to a uint8_t array that is at
\r
160 * least xBufferSizeBytes + 1 big. This is the array to which messages are
\r
161 * copied when they are written to the message buffer.
\r
163 * @param pxStaticMessageBuffer Must point to a variable of type
\r
164 * StaticMessageBuffer_t, which will be used to hold the message buffer's data
\r
167 * @return If the message buffer is created successfully then a handle to the
\r
168 * created message buffer is returned. If either pucMessageBufferStorageArea or
\r
169 * pxStaticmessageBuffer are NULL then NULL is returned.
\r
174 // Used to dimension the array used to hold the messages. The available space
\r
175 // will actually be one less than this, so 999.
\r
176 #define STORAGE_SIZE_BYTES 1000
\r
178 // Defines the memory that will actually hold the messages within the message
\r
180 static uint8_t ucStorageBuffer[ STORAGE_SIZE_BYTES ];
\r
182 // The variable used to hold the message buffer structure.
\r
183 StaticMessageBuffer_t xMessageBufferStruct;
\r
185 void MyFunction( void )
\r
187 MessageBufferHandle_t xMessageBuffer;
\r
189 xMessageBuffer = xMessageBufferCreateStatic( sizeof( ucBufferStorage ),
\r
191 &xMessageBufferStruct );
\r
193 // As neither the pucMessageBufferStorageArea or pxStaticMessageBuffer
\r
194 // parameters were NULL, xMessageBuffer will not be NULL, and can be used to
\r
195 // reference the created message buffer in other message buffer API calls.
\r
197 // Other code that uses the message buffer can go here.
\r
201 * \defgroup xMessageBufferCreateStatic xMessageBufferCreateStatic
\r
202 * \ingroup MessageBufferManagement
\r
204 #define xMessageBufferCreateStatic( xBufferSizeBytes, pucMessageBufferStorageArea, pxStaticMessageBuffer ) ( MessageBufferHandle_t ) xStreamBufferGenericCreateStatic( xBufferSizeBytes, 0, pdTRUE, pucMessageBufferStorageArea, pxStaticMessageBuffer )
\r
210 size_t xMessageBufferSend( MessageBufferHandle_t xMessageBuffer,
\r
211 const void *pvTxData,
\r
212 size_t xDataLengthBytes,
\r
213 TickType_t xTicksToWait );
\r
216 * Sends a discrete message to the message buffer. The message can be any
\r
217 * length that fits within the buffer's free space, and is copied into the
\r
220 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
221 * implementation (so also the message buffer implementation, as message buffers
\r
222 * are built on top of stream buffers) assumes there is only one task or
\r
223 * interrupt that will write to the buffer (the writer), and only one task or
\r
224 * interrupt that will read from the buffer (the reader). It is safe for the
\r
225 * writer and reader to be different tasks or interrupts, but, unlike other
\r
226 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
227 * multiple different readers. If there are to be multiple different writers
\r
228 * then the application writer must place each call to a writing API function
\r
229 * (such as xMessageBufferSend()) inside a critical section and set the send
\r
230 * block time to 0. Likewise, if there are to be multiple different readers
\r
231 * then the application writer must place each call to a reading API function
\r
232 * (such as xMessageBufferRead()) inside a critical section and set the receive
\r
235 * Use xMessageBufferSend() to write to a message buffer from a task. Use
\r
236 * xMessageBufferSendFromISR() to write to a message buffer from an interrupt
\r
237 * service routine (ISR).
\r
239 * @param xMessageBuffer The handle of the message buffer to which a message is
\r
242 * @param pvTxData A pointer to the message that is to be copied into the
\r
245 * @param xDataLengthBytes The length of the message. That is, the number of
\r
246 * bytes to copy from pvTxData into the message buffer. When a message is
\r
247 * written to the message buffer an additional sizeof( size_t ) bytes are also
\r
248 * written to store the message's length. sizeof( size_t ) is typically 4 bytes
\r
249 * on a 32-bit architecture, so on most 32-bit architecture setting
\r
250 * xDataLengthBytes to 20 will reduce the free space in the message buffer by 24
\r
251 * bytes (20 bytes of message data and 4 bytes to hold the message length).
\r
253 * @param xTicksToWait The maximum amount of time the calling task should remain
\r
254 * in the Blocked state to wait for enough space to become available in the
\r
255 * message buffer, should the message buffer have insufficient space when
\r
256 * xMessageBufferSend() is called. The calling task will never block if
\r
257 * xTicksToWait is zero. The block time is specified in tick periods, so the
\r
258 * absolute time it represents is dependent on the tick frequency. The macro
\r
259 * pdMS_TO_TICKS() can be used to convert a time specified in milliseconds into
\r
260 * a time specified in ticks. Setting xTicksToWait to portMAX_DELAY will cause
\r
261 * the task to wait indefinitely (without timing out), provided
\r
262 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. Tasks do not use any
\r
263 * CPU time when they are in the Blocked state.
\r
265 * @return The number of bytes written to the message buffer. If the call to
\r
266 * xMessageBufferSend() times out before there was enough space to write the
\r
267 * message into the message buffer then zero is returned. If the call did not
\r
268 * time out then xDataLengthBytes is returned.
\r
272 void vAFunction( MessageBufferHandle_t xMessageBuffer )
\r
275 uint8_t ucArrayToSend[] = { 0, 1, 2, 3 };
\r
276 char *pcStringToSend = "String to send";
\r
277 const TickType_t x100ms = pdMS_TO_TICKS( 100 );
\r
279 // Send an array to the message buffer, blocking for a maximum of 100ms to
\r
280 // wait for enough space to be available in the message buffer.
\r
281 xBytesSent = xMessageBufferSend( xMessageBuffer, ( void * ) ucArrayToSend, sizeof( ucArrayToSend ), x100ms );
\r
283 if( xBytesSent != sizeof( ucArrayToSend ) )
\r
285 // The call to xMessageBufferSend() times out before there was enough
\r
286 // space in the buffer for the data to be written.
\r
289 // Send the string to the message buffer. Return immediately if there is
\r
290 // not enough space in the buffer.
\r
291 xBytesSent = xMessageBufferSend( xMessageBuffer, ( void * ) pcStringToSend, strlen( pcStringToSend ), 0 );
\r
293 if( xBytesSent != strlen( pcStringToSend ) )
\r
295 // The string could not be added to the message buffer because there was
\r
296 // not enough free space in the buffer.
\r
300 * \defgroup xMessageBufferSend xMessageBufferSend
\r
301 * \ingroup MessageBufferManagement
\r
303 #define xMessageBufferSend( xMessageBuffer, pvTxData, xDataLengthBytes, xTicksToWait ) xStreamBufferSend( ( StreamBufferHandle_t ) xMessageBuffer, pvTxData, xDataLengthBytes, xTicksToWait )
\r
309 size_t xMessageBufferSendFromISR( MessageBufferHandle_t xMessageBuffer,
\r
310 const void *pvTxData,
\r
311 size_t xDataLengthBytes,
\r
312 BaseType_t *pxHigherPriorityTaskWoken );
\r
315 * Interrupt safe version of the API function that sends a discrete message to
\r
316 * the message buffer. The message can be any length that fits within the
\r
317 * buffer's free space, and is copied into the buffer.
\r
319 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
320 * implementation (so also the message buffer implementation, as message buffers
\r
321 * are built on top of stream buffers) assumes there is only one task or
\r
322 * interrupt that will write to the buffer (the writer), and only one task or
\r
323 * interrupt that will read from the buffer (the reader). It is safe for the
\r
324 * writer and reader to be different tasks or interrupts, but, unlike other
\r
325 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
326 * multiple different readers. If there are to be multiple different writers
\r
327 * then the application writer must place each call to a writing API function
\r
328 * (such as xMessageBufferSend()) inside a critical section and set the send
\r
329 * block time to 0. Likewise, if there are to be multiple different readers
\r
330 * then the application writer must place each call to a reading API function
\r
331 * (such as xMessageBufferRead()) inside a critical section and set the receive
\r
334 * Use xMessageBufferSend() to write to a message buffer from a task. Use
\r
335 * xMessageBufferSendFromISR() to write to a message buffer from an interrupt
\r
336 * service routine (ISR).
\r
338 * @param xMessageBuffer The handle of the message buffer to which a message is
\r
341 * @param pvTxData A pointer to the message that is to be copied into the
\r
344 * @param xDataLengthBytes The length of the message. That is, the number of
\r
345 * bytes to copy from pvTxData into the message buffer. When a message is
\r
346 * written to the message buffer an additional sizeof( size_t ) bytes are also
\r
347 * written to store the message's length. sizeof( size_t ) is typically 4 bytes
\r
348 * on a 32-bit architecture, so on most 32-bit architecture setting
\r
349 * xDataLengthBytes to 20 will reduce the free space in the message buffer by 24
\r
350 * bytes (20 bytes of message data and 4 bytes to hold the message length).
\r
352 * @param pxHigherPriorityTaskWoken It is possible that a message buffer will
\r
353 * have a task blocked on it waiting for data. Calling
\r
354 * xMessageBufferSendFromISR() can make data available, and so cause a task that
\r
355 * was waiting for data to leave the Blocked state. If calling
\r
356 * xMessageBufferSendFromISR() causes a task to leave the Blocked state, and the
\r
357 * unblocked task has a priority higher than the currently executing task (the
\r
358 * task that was interrupted), then, internally, xMessageBufferSendFromISR()
\r
359 * will set *pxHigherPriorityTaskWoken to pdTRUE. If
\r
360 * xMessageBufferSendFromISR() sets this value to pdTRUE, then normally a
\r
361 * context switch should be performed before the interrupt is exited. This will
\r
362 * ensure that the interrupt returns directly to the highest priority Ready
\r
363 * state task. *pxHigherPriorityTaskWoken should be set to pdFALSE before it
\r
364 * is passed into the function. See the code example below for an example.
\r
366 * @return The number of bytes actually written to the message buffer. If the
\r
367 * message buffer didn't have enough free space for the message to be stored
\r
368 * then 0 is returned, otherwise xDataLengthBytes is returned.
\r
372 // A message buffer that has already been created.
\r
373 MessageBufferHandle_t xMessageBuffer;
\r
375 void vAnInterruptServiceRoutine( void )
\r
378 char *pcStringToSend = "String to send";
\r
379 BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
\r
381 // Attempt to send the string to the message buffer.
\r
382 xBytesSent = xMessageBufferSendFromISR( xMessageBuffer,
\r
383 ( void * ) pcStringToSend,
\r
384 strlen( pcStringToSend ),
\r
385 &xHigherPriorityTaskWoken );
\r
387 if( xBytesSent != strlen( pcStringToSend ) )
\r
389 // The string could not be added to the message buffer because there was
\r
390 // not enough free space in the buffer.
\r
393 // If xHigherPriorityTaskWoken was set to pdTRUE inside
\r
394 // xMessageBufferSendFromISR() then a task that has a priority above the
\r
395 // priority of the currently executing task was unblocked and a context
\r
396 // switch should be performed to ensure the ISR returns to the unblocked
\r
397 // task. In most FreeRTOS ports this is done by simply passing
\r
398 // xHigherPriorityTaskWoken into taskYIELD_FROM_ISR(), which will test the
\r
399 // variables value, and perform the context switch if necessary. Check the
\r
400 // documentation for the port in use for port specific instructions.
\r
401 taskYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
404 * \defgroup xMessageBufferSendFromISR xMessageBufferSendFromISR
\r
405 * \ingroup MessageBufferManagement
\r
407 #define xMessageBufferSendFromISR( xMessageBuffer, pvTxData, xDataLengthBytes, pxHigherPriorityTaskWoken ) xStreamBufferSendFromISR( ( StreamBufferHandle_t ) xMessageBuffer, pvTxData, xDataLengthBytes, pxHigherPriorityTaskWoken )
\r
413 size_t xMessageBufferReceive( MessageBufferHandle_t xMessageBuffer,
\r
415 size_t xBufferLengthBytes,
\r
416 TickType_t xTicksToWait );
\r
419 * Receives a discrete message from a message buffer. Messages can be of
\r
420 * variable length and are copied out of the buffer.
\r
422 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
423 * implementation (so also the message buffer implementation, as message buffers
\r
424 * are built on top of stream buffers) assumes there is only one task or
\r
425 * interrupt that will write to the buffer (the writer), and only one task or
\r
426 * interrupt that will read from the buffer (the reader). It is safe for the
\r
427 * writer and reader to be different tasks or interrupts, but, unlike other
\r
428 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
429 * multiple different readers. If there are to be multiple different writers
\r
430 * then the application writer must place each call to a writing API function
\r
431 * (such as xMessageBufferSend()) inside a critical section and set the send
\r
432 * block time to 0. Likewise, if there are to be multiple different readers
\r
433 * then the application writer must place each call to a reading API function
\r
434 * (such as xMessageBufferRead()) inside a critical section and set the receive
\r
437 * Use xMessageBufferReceive() to read from a message buffer from a task. Use
\r
438 * xMessageBufferReceiveFromISR() to read from a message buffer from an
\r
439 * interrupt service routine (ISR).
\r
441 * @param xMessageBuffer The handle of the message buffer from which a message
\r
442 * is being received.
\r
444 * @param pvRxData A pointer to the buffer into which the received message is
\r
447 * @param xBufferLengthBytes The length of the buffer pointed to by the pvRxData
\r
448 * parameter. This sets the maximum length of the message that can be received.
\r
449 * If xBufferLengthBytes is too small to hold the next message then the message
\r
450 * will be left in the message buffer and 0 will be returned.
\r
452 * @param xTicksToWait The maximum amount of time the task should remain in the
\r
453 * Blocked state to wait for a message, should the message buffer be empty.
\r
454 * xMessageBufferReceive() will return immediately if xTicksToWait is zero and
\r
455 * the message buffer is empty. The block time is specified in tick periods, so
\r
456 * the absolute time it represents is dependent on the tick frequency. The
\r
457 * macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds
\r
458 * into a time specified in ticks. Setting xTicksToWait to portMAX_DELAY will
\r
459 * cause the task to wait indefinitely (without timing out), provided
\r
460 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h. Tasks do not use any
\r
461 * CPU time when they are in the Blocked state.
\r
463 * @return The length, in bytes, of the message read from the message buffer, if
\r
464 * any. If xMessageBufferReceive() times out before a message became available
\r
465 * then zero is returned. If the length of the message is greater than
\r
466 * xBufferLengthBytes then the message will be left in the message buffer and
\r
467 * zero is returned.
\r
471 void vAFunction( MessageBuffer_t xMessageBuffer )
\r
473 uint8_t ucRxData[ 20 ];
\r
474 size_t xReceivedBytes;
\r
475 const TickType_t xBlockTime = pdMS_TO_TICKS( 20 );
\r
477 // Receive the next message from the message buffer. Wait in the Blocked
\r
478 // state (so not using any CPU processing time) for a maximum of 100ms for
\r
479 // a message to become available.
\r
480 xReceivedBytes = xMessageBufferReceive( xMessageBuffer,
\r
481 ( void * ) ucRxData,
\r
482 sizeof( ucRxData ),
\r
485 if( xReceivedBytes > 0 )
\r
487 // A ucRxData contains a message that is xReceivedBytes long. Process
\r
488 // the message here....
\r
492 * \defgroup xMessageBufferReceive xMessageBufferReceive
\r
493 * \ingroup MessageBufferManagement
\r
495 #define xMessageBufferReceive( xMessageBuffer, pvRxData, xBufferLengthBytes, xTicksToWait ) xStreamBufferReceive( ( StreamBufferHandle_t ) xMessageBuffer, pvRxData, xBufferLengthBytes, xTicksToWait )
\r
502 size_t xMessageBufferReceiveFromISR( MessageBufferHandle_t xMessageBuffer,
\r
504 size_t xBufferLengthBytes,
\r
505 BaseType_t *pxHigherPriorityTaskWoken );
\r
508 * An interrupt safe version of the API function that receives a discrete
\r
509 * message from a message buffer. Messages can be of variable length and are
\r
510 * copied out of the buffer.
\r
512 * ***NOTE***: Uniquely among FreeRTOS objects, the stream buffer
\r
513 * implementation (so also the message buffer implementation, as message buffers
\r
514 * are built on top of stream buffers) assumes there is only one task or
\r
515 * interrupt that will write to the buffer (the writer), and only one task or
\r
516 * interrupt that will read from the buffer (the reader). It is safe for the
\r
517 * writer and reader to be different tasks or interrupts, but, unlike other
\r
518 * FreeRTOS objects, it is not safe to have multiple different writers or
\r
519 * multiple different readers. If there are to be multiple different writers
\r
520 * then the application writer must place each call to a writing API function
\r
521 * (such as xMessageBufferSend()) inside a critical section and set the send
\r
522 * block time to 0. Likewise, if there are to be multiple different readers
\r
523 * then the application writer must place each call to a reading API function
\r
524 * (such as xMessageBufferRead()) inside a critical section and set the receive
\r
527 * Use xMessageBufferReceive() to read from a message buffer from a task. Use
\r
528 * xMessageBufferReceiveFromISR() to read from a message buffer from an
\r
529 * interrupt service routine (ISR).
\r
531 * @param xMessageBuffer The handle of the message buffer from which a message
\r
532 * is being received.
\r
534 * @param pvRxData A pointer to the buffer into which the received message is
\r
537 * @param xBufferLengthBytes The length of the buffer pointed to by the pvRxData
\r
538 * parameter. This sets the maximum length of the message that can be received.
\r
539 * If xBufferLengthBytes is too small to hold the next message then the message
\r
540 * will be left in the message buffer and 0 will be returned.
\r
542 * @param pxHigherPriorityTaskWoken It is possible that a message buffer will
\r
543 * have a task blocked on it waiting for space to become available. Calling
\r
544 * xMessageBufferReceiveFromISR() can make space available, and so cause a task
\r
545 * that is waiting for space to leave the Blocked state. If calling
\r
546 * xMessageBufferReceiveFromISR() causes a task to leave the Blocked state, and
\r
547 * the unblocked task has a priority higher than the currently executing task
\r
548 * (the task that was interrupted), then, internally,
\r
549 * xMessageBufferReceiveFromISR() will set *pxHigherPriorityTaskWoken to pdTRUE.
\r
550 * If xMessageBufferReceiveFromISR() sets this value to pdTRUE, then normally a
\r
551 * context switch should be performed before the interrupt is exited. That will
\r
552 * ensure the interrupt returns directly to the highest priority Ready state
\r
553 * task. *pxHigherPriorityTaskWoken should be set to pdFALSE before it is
\r
554 * passed into the function. See the code example below for an example.
\r
556 * @return The length, in bytes, of the message read from the message buffer, if
\r
561 // A message buffer that has already been created.
\r
562 MessageBuffer_t xMessageBuffer;
\r
564 void vAnInterruptServiceRoutine( void )
\r
566 uint8_t ucRxData[ 20 ];
\r
567 size_t xReceivedBytes;
\r
568 BaseType_t xHigherPriorityTaskWoken = pdFALSE; // Initialised to pdFALSE.
\r
570 // Receive the next message from the message buffer.
\r
571 xReceivedBytes = xMessageBufferReceiveFromISR( xMessageBuffer,
\r
572 ( void * ) ucRxData,
\r
573 sizeof( ucRxData ),
\r
574 &xHigherPriorityTaskWoken );
\r
576 if( xReceivedBytes > 0 )
\r
578 // A ucRxData contains a message that is xReceivedBytes long. Process
\r
579 // the message here....
\r
582 // If xHigherPriorityTaskWoken was set to pdTRUE inside
\r
583 // xMessageBufferReceiveFromISR() then a task that has a priority above the
\r
584 // priority of the currently executing task was unblocked and a context
\r
585 // switch should be performed to ensure the ISR returns to the unblocked
\r
586 // task. In most FreeRTOS ports this is done by simply passing
\r
587 // xHigherPriorityTaskWoken into taskYIELD_FROM_ISR(), which will test the
\r
588 // variables value, and perform the context switch if necessary. Check the
\r
589 // documentation for the port in use for port specific instructions.
\r
590 taskYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
593 * \defgroup xMessageBufferReceiveFromISR xMessageBufferReceiveFromISR
\r
594 * \ingroup MessageBufferManagement
\r
596 #define xMessageBufferReceiveFromISR( xMessageBuffer, pvRxData, xBufferLengthBytes, pxHigherPriorityTaskWoken ) xStreamBufferReceiveFromISR( ( StreamBufferHandle_t ) xMessageBuffer, pvRxData, xBufferLengthBytes, pxHigherPriorityTaskWoken )
\r
602 void vMessageBufferDelete( MessageBufferHandle_t xMessageBuffer );
\r
605 * Deletes a message buffer that was previously created using a call to
\r
606 * xMessageBufferCreate() or xMessageBufferCreateStatic(). If the message
\r
607 * buffer was created using dynamic memory (that is, by xMessageBufferCreate()),
\r
608 * then the allocated memory is freed.
\r
610 * A message buffer handle must not be used after the message buffer has been
\r
613 * @param xMessageBuffer The handle of the message buffer to be deleted.
\r
616 #define vMessageBufferDelete( xMessageBuffer ) vStreamBufferDelete( ( StreamBufferHandle_t ) xMessageBuffer )
\r
621 BaseType_t xMessageBufferIsFull( MessageBufferHandle_t xMessageBuffer ) );
\r
624 * Tests to see if a message buffer is full. A message buffer is full if it
\r
625 * cannot accept any more messages, of any size, until space is made available
\r
626 * by a message being removed from the message buffer.
\r
628 * @param xMessageBuffer The handle of the message buffer being queried.
\r
630 * @return If the message buffer referenced by xMessageBuffer is full then
\r
631 * pdTRUE is returned. Otherwise pdFALSE is returned.
\r
633 #define xMessageBufferIsFull( xMessageBuffer ) xStreamBufferIsFull( ( StreamBufferHandle_t ) xMessageBuffer )
\r
638 BaseType_t xMessageBufferIsEmpty( MessageBufferHandle_t xMessageBuffer ) );
\r
641 * Tests to see if a message buffer is empty (does not contain any messages).
\r
643 * @param xMessageBuffer The handle of the message buffer being queried.
\r
645 * @return If the message buffer referenced by xMessageBuffer is empty then
\r
646 * pdTRUE is returned. Otherwise pdFALSE is returned.
\r
649 #define xMessageBufferIsEmpty( xMessageBuffer ) xStreamBufferIsEmpty( ( StreamBufferHandle_t ) xMessageBuffer )
\r
654 BaseType_t xMessageBufferReset( MessageBufferHandle_t xMessageBuffer );
\r
657 * Resets a message buffer to its initial empty state, discarding any message it
\r
660 * A message buffer can only be reset if there are no tasks blocked on it.
\r
662 * @param xMessageBuffer The handle of the message buffer being reset.
\r
664 * @return If the message buffer was reset then pdPASS is returned. If the
\r
665 * message buffer could not be reset because either there was a task blocked on
\r
666 * the message queue to wait for space to become available, or to wait for a
\r
667 * a message to be available, then pdFAIL is returned.
\r
669 * \defgroup xMessageBufferReset xMessageBufferReset
\r
670 * \ingroup MessageBufferManagement
\r
672 #define xMessageBufferReset( xMessageBuffer ) xStreamBufferReset( ( StreamBufferHandle_t ) xMessageBuffer )
\r
678 size_t xMessageBufferSpaceAvailable( MessageBufferHandle_t xMessageBuffer ) );
\r
680 * Returns the number of bytes of free space in the message buffer.
\r
682 * @param xMessageBuffer The handle of the message buffer being queried.
\r
684 * @return The number of bytes that can be written to the message buffer before
\r
685 * the message buffer would be full. When a message is written to the message
\r
686 * buffer an additional sizeof( size_t ) bytes are also written to store the
\r
687 * message's length. sizeof( size_t ) is typically 4 bytes on a 32-bit
\r
688 * architecture, so if xMessageBufferSpacesAvailable() returns 10, then the size
\r
689 * of the largest message that can be written to the message buffer is 6 bytes.
\r
691 * \defgroup xMessageBufferSpaceAvailable xMessageBufferSpaceAvailable
\r
692 * \ingroup MessageBufferManagement
\r
694 #define xMessageBufferSpaceAvailable( xMessageBuffer ) xStreamBufferSpacesAvailable( ( StreamBufferHandle_t ) xMessageBuffer )
\r
700 BaseType_t xMessageBufferSendCompletedFromISR( MessageBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken );
\r
703 * For advanced users only.
\r
705 * The sbSEND_COMPLETED() macro is called from within the FreeRTOS APIs when
\r
706 * data is sent to a message buffer or stream buffer. If there was a task that
\r
707 * was blocked on the message or stream buffer waiting for data to arrive then
\r
708 * the sbSEND_COMPLETED() macro sends a notification to the task to remove it
\r
709 * from the Blocked state. xMessageBufferSendCompletedFromISR() does the same
\r
710 * thing. It is provided to enable application writers to implement their own
\r
711 * version of sbSEND_COMPLETED(), and MUST NOT BE USED AT ANY OTHER TIME.
\r
713 * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
\r
714 * additional information.
\r
716 * @param xStreamBuffer The handle of the stream buffer to which data was
\r
719 * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
\r
720 * initialised to pdFALSE before it is passed into
\r
721 * xMessageBufferSendCompletedFromISR(). If calling
\r
722 * xMessageBufferSendCompletedFromISR() removes a task from the Blocked state,
\r
723 * and the task has a priority above the priority of the currently running task,
\r
724 * then *pxHigherPriorityTaskWoken will get set to pdTRUE indicating that a
\r
725 * context switch should be performed before exiting the ISR.
\r
727 * @return If a task was removed from the Blocked state then pdTRUE is returned.
\r
728 * Otherwise pdFALSE is returned.
\r
730 * \defgroup xMessageBufferSendCompletedFromISR xMessageBufferSendCompletedFromISR
\r
731 * \ingroup StreamBufferManagement
\r
733 #define xMessageBufferSendCompletedFromISR( xMessageBuffer, pxHigherPriorityTaskWoken ) xStreamBufferSendCompletedFromISR( ( StreamBufferHandle_t ) xMessageBuffer, pxHigherPriorityTaskWoken )
\r
739 BaseType_t xMessageBufferReceiveCompletedFromISR( MessageBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken );
\r
742 * For advanced users only.
\r
744 * The sbRECEIVE_COMPLETED() macro is called from within the FreeRTOS APIs when
\r
745 * data is read out of a message buffer or stream buffer. If there was a task
\r
746 * that was blocked on the message or stream buffer waiting for data to arrive
\r
747 * then the sbRECEIVE_COMPLETED() macro sends a notification to the task to
\r
748 * remove it from the Blocked state. xMessageBufferReceiveCompletedFromISR()
\r
749 * does the same thing. It is provided to enable application writers to
\r
750 * implement their own version of sbRECEIVE_COMPLETED(), and MUST NOT BE USED AT
\r
753 * See the example implemented in FreeRTOS/Demo/Minimal/MessageBufferAMP.c for
\r
754 * additional information.
\r
756 * @param xStreamBuffer The handle of the stream buffer from which data was
\r
759 * @param pxHigherPriorityTaskWoken *pxHigherPriorityTaskWoken should be
\r
760 * initialised to pdFALSE before it is passed into
\r
761 * xMessageBufferReceiveCompletedFromISR(). If calling
\r
762 * xMessageBufferReceiveCompletedFromISR() 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 xMessageBufferReceiveCompletedFromISR xMessageBufferReceiveCompletedFromISR
\r
771 * \ingroup StreamBufferManagement
\r
773 #define xMessageBufferReceiveCompletedFromISR( xMessageBuffer, pxHigherPriorityTaskWoken ) xStreamBufferReceiveCompletedFromISR( ( StreamBufferHandle_t ) xMessageBuffer, pxHigherPriorityTaskWoken )
\r
775 #if defined( __cplusplus )
\r
779 #endif /* !defined( FREERTOS_MESSAGE_BUFFER_H ) */
\r