Missing bytes when using queues

kaarebach wrote on Friday, January 16, 2015:

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

rtel wrote on Friday, January 16, 2015:

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.

kaarebach wrote on Friday, January 16, 2015:

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

davedoors wrote on Friday, January 16, 2015:

portYIELD_FROM_ISR and portEND_SWITCHING_ISR are not available. Only portYIELD is.

which port are you using?

kaarebach wrote on Friday, January 16, 2015:

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

rtel wrote on Friday, January 16, 2015:

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();
}

kaarebach wrote on Friday, January 16, 2015:

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.

rtel wrote on Friday, January 16, 2015:

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

kaarebach wrote on Friday, January 16, 2015:

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