stream_buffer.h 37 KB

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