stream_buffer.h 37 KB

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