message_buffer.h 37 KB

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