# Can't understand how xStreamBufferSetTriggerLevel and xStreamBufferReceive works

**URL:** <https://forums.freertos.org/t/cant-understand-how-xstreambuffersettriggerlevel-and-xstreambufferreceive-works/13178>\
**Category:** Kernel\
**Created:** [August 6, 2021, 4:25pm UTC](https://forums.freertos.org/t/cant-understand-how-xstreambuffersettriggerlevel-and-xstreambufferreceive-works/13178 "2021-08-06T16:25:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArtDen](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@ArtDen](https://forums.freertos.org/u/ArtDen)\
**Post date:** [August 6, 2021, 4:25pm UTC](https://forums.freertos.org/t/cant-understand-how-xstreambuffersettriggerlevel-and-xstreambufferreceive-works/13178/1 "2021-08-06T16:25:26Z")

</div>

I expect `xStreamBufferReceive` will wait until all 52 bytes read:

```auto
	bool set_res = xStreamBufferSetTriggerLevel(stream, 52);
	configASSERT(set_res);
	size_t bytes_read = xStreamBufferReceive(stream, read_buffer, 52, portMAX_DELAY);

```

but it returns immediately with `bytes_read = 3`. How it possible?

Part of my `FreeRTOSConfig.h`:

```auto
	#define configUSE_TRACE_FACILITY 1
	#define INCLUDE_uxTaskGetStackHighWaterMark 1
	#define configCHECK_FOR_STACK_OVERFLOW 2
	#define configASSERT(x) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }

```

Tested with FreeRTOS 10.4.3 and 10.4.4

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [August 6, 2021, 7:12pm UTC](https://forums.freertos.org/t/cant-understand-how-xstreambuffersettriggerlevel-and-xstreambufferreceive-works/13178/2 "2021-08-06T19:12:46Z")

</div>

According to the API docs ([xStreamBufferSetTriggerLevel() RTOS API documentation](https://freertos.org/xStreamBufferSetTriggerLevel.html)) the trigger level is used only if the receive function must block. And the receive function blocks only if the stream is empty.

---

<div class="post-metadata">

**Author:** ![ArtDen](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@ArtDen](https://forums.freertos.org/u/ArtDen)\
**Post date:** [August 6, 2021, 7:41pm UTC](https://forums.freertos.org/t/cant-understand-how-xstreambuffersettriggerlevel-and-xstreambufferreceive-works/13178/3 "2021-08-06T19:41:33Z")

</div>

> [@jefftenney](#):
>
> receive function blocks only if the stream is empty

Hmm… I can’t find information about it in FreeRTOS docs

---

<div class="post-metadata">

**Author:** ![jefftenney](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jefftenney/32/1314_2.png) [@jefftenney](https://forums.freertos.org/u/jefftenney)\
**Post date:** [August 6, 2021, 7:55pm UTC](https://forums.freertos.org/t/cant-understand-how-xstreambuffersettriggerlevel-and-xstreambufferreceive-works/13178/4 "2021-08-06T19:55:29Z")

</div>

On this page ([xStreamBufferReceive() RTOS API function documentation](https://freertos.org/xStreamBufferReceive.html)), see the description of parameter _xTicksToWait_.

 ![image](https://d2v6vdsk2p900z.cloudfront.net/original/2X/4/48cc11301e5fec7b1ff0764cfdd181fe01086a13.png)

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [August 6, 2021, 10:13pm UTC](https://forums.freertos.org/t/cant-understand-how-xstreambuffersettriggerlevel-and-xstreambufferreceive-works/13178/5 "2021-08-06T22:13:34Z")

</div>

I was going to suggest look at the docs for the trigger level. That is how many bytes the task will block for.

---

<div class="post-metadata">

**Author:** ![ArtDen](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@ArtDen](https://forums.freertos.org/u/ArtDen)\
**Post date:** [August 7, 2021, 6:04am UTC](https://forums.freertos.org/t/cant-understand-how-xstreambuffersettriggerlevel-and-xstreambufferreceive-works/13178/6 "2021-08-07T06:04:04Z")

</div>

> [@jefftenney](#):
>
> see the description of parameter _xTicksToWait_.

Thanks. But it is still not clear for me after reading this doc how xTicksToWait affects function’s behavior if buffer is NOT empty  
It is only clearly said that “xStreamBufferReceive() will return immediately if xTicksToWait is zero”

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [August 7, 2021, 5:35pm UTC](https://forums.freertos.org/t/cant-understand-how-xstreambuffersettriggerlevel-and-xstreambufferreceive-works/13178/7 "2021-08-07T17:35:48Z")

</div>

If the buffer is not empty, the is no wait, you just get what is there. If the buffer is empty, it will wait for the first of trigger bytes or the time out to happen.
