message_buffer.h 37 KB

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