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