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