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