message_buffer.h 39 KB

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