stream_buffer.h 38 KB

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