message_buffer.h 38 KB

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