message_buffer.h 40 KB

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