stream_buffer.h 41 KB

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