message_buffer.h 36 KB

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