# Missing bytes when using queues

**URL:** <https://forums.freertos.org/t/missing-bytes-when-using-queues/2750>\
**Category:** Kernel\
**Created:** [January 16, 2015, 10:42am UTC](https://forums.freertos.org/t/missing-bytes-when-using-queues/2750 "2015-01-16T10:42:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 16, 2015, 10:42am UTC](https://forums.freertos.org/t/missing-bytes-when-using-queues/2750/1 "2015-01-16T10:42:03Z")

</div>

**[kaarebach](https://sourceforge.net/u/kaarebach/)** wrote on [Friday, January 16, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/79c93c90/#0650):

Hi

I have a very simple UART application, which I have ported from another  
RTOS to FreeRTOS.  
But now I got problems:

very simple task:

- In my UART RX interrupt, i send byte to a queue.
- In a task, I read the bytes from the queue.

ISR:

// Read byte from UART:  
u8UART\_Data = UART\_ReceiveByte(UART);

// Send the byte to the queue:  
xQueueSendToBackFromISR(xByteQueue, &u8UART\_Data, &xHigherPriorityTaskWoken  
);

In my receiving task, I wait for a byte in the queue:

xQueueReceive( xByteQueue, &u8Received\_byte, portMAX\_DELAY );

Extremely simple…

My problem is that I sometimes looses a byte. Not many, but maybe 1 out of  
10.000.

The ISR receives EVERY bytes sent to it.  
But when reading the queue, some bytes are missing.

By mistake, I used the NON-ISR xQueueSendToBack in the first version.  
When using this, I never lost any bytes, but entered HardfaultHandler once  
in a while.

Anyone knows why I loose bytes between xQueueSend and xQueueReceive?

thanks in advance!

br  
Kaare

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [January 16, 2015, 10:51am UTC](https://forums.freertos.org/t/missing-bytes-when-using-queues/2750/2 "2015-01-16T10:51:24Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, January 16, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/79c93c90/#d688):

Do you check the return value of xQueueSendToBackFromISR()? If it returns pdFALSE then the queue was full.

Also, do you call portYIELD\_FROM\_ISR( xHigherPriorityTaskWoken ) (or portEND\_SWITCHING\_ISR( xHigherPriorityTaskWoken ) - depending on the port) at the end of your interrupt handler. If not then adding it may allow the receiving task to drain the queue sooner.

Generally it is not recommended to use a queue to send every received byte unless the data is very slow (like typed characters from a keyboard, as an example). It will be more efficient to store received bytes in a RAM buffer, then use a semaphore (or task notification in V8.2.0) to unblock a task when there is a whole message that needs processing.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 16, 2015, 11:17am UTC](https://forums.freertos.org/t/missing-bytes-when-using-queues/2750/3 "2015-01-16T11:17:46Z")

</div>

**[kaarebach](https://sourceforge.net/u/kaarebach/)** wrote on [Friday, January 16, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/79c93c90/#d29a):

Thanks for quick response!

I send 35 characters every second, so the data amount is very small.  
It is not typed, but, still, i should be able to handle them

My interrupt never misses a character, and my queue is never full (set to 100 bytes length). When I check the queue free size, there are never more than 5 characters in the queue.

I know it is not the most efficient way to do it, but it should be OK with this amount of data.

portYIELD\_FROM\_ISR and portEND\_SWITCHING\_ISR are not available.  
Only portYIELD is.

br  
Kaare

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 16, 2015, 11:28am UTC](https://forums.freertos.org/t/missing-bytes-when-using-queues/2750/4 "2015-01-16T11:28:50Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Friday, January 16, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/79c93c90/#d0a5):

> portYIELD\_FROM\_ISR and portEND\_SWITCHING\_ISR are not available. Only portYIELD is.

which port are you using?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 16, 2015, 12:05pm UTC](https://forums.freertos.org/t/missing-bytes-when-using-queues/2750/5 "2015-01-16T12:05:08Z")

</div>

**[kaarebach](https://sourceforge.net/u/kaarebach/)** wrote on [Friday, January 16, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/79c93c90/#d0a5/f52b):

I have added this:

vPortYield();

And I have received more than 70.000 bytes without any missed bytes :o)

I am using V8.0.1 with Rowley / GCC on an LPC1788

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [January 16, 2015, 12:13pm UTC](https://forums.freertos.org/t/missing-bytes-when-using-queues/2750/6 "2015-01-16T12:13:12Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, January 16, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/79c93c90/#d0a5/f52b/6efe):

On that chip the expected way of doing this would be to add

portEND\_SWITCHING\_ISR( xHigherPriorityTaskWoken ).

Alternatively you can expand the macro yourself and add

if( xHigherPriorityTaskWoken != pdFALSE )  
{  
vPortYield();  
}

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 16, 2015, 12:28pm UTC](https://forums.freertos.org/t/missing-bytes-when-using-queues/2750/7 "2015-01-16T12:28:26Z")

</div>

**[kaarebach](https://sourceforge.net/u/kaarebach/)** wrote on [Friday, January 16, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/79c93c90/#b1ba):

portEND\_SWITCHING\_ISR() is not available. Should I include something, or enable something in the config file?  
I can’t find anything in the reference manual.

Your alternative seems to work OK until now.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [January 16, 2015, 12:50pm UTC](https://forums.freertos.org/t/missing-bytes-when-using-queues/2750/8 "2015-01-16T12:50:06Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, January 16, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/79c93c90/#b1ba/39d0):

portEND\_SWITCHING\_ISR() is defined in portmacro.h, so should be  
available if you have included FreeRTOS.h at the top of the file.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [January 16, 2015, 1:57pm UTC](https://forums.freertos.org/t/missing-bytes-when-using-queues/2750/9 "2015-01-16T13:57:47Z")

</div>

**[kaarebach](https://sourceforge.net/u/kaarebach/)** wrote on [Friday, January 16, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/79c93c90/#ad8f):

Ah, yes it is.  
I think I’m getting a bit tired :o)

It also works with this, so I am happy, and can close this week with a little succes!

Thanks for your help!

br  
Kaare
