Tips please :-) SAM7 USB-CDC FreeRTOS Driver

ckronen wrote on Monday, July 18, 2011:

Dear Community,

since I need a USB-CDC Driver for some projects, I tried the USB-CDC driver from the demo “lwIP_Demo_Rowley_ARM7”.

In the description, it says that it does not have a great performance due to heavy queue usage. I benchmarked the RX performance and compared it to the driver provided by ATMEL. The results when I do not process the received bytes any further:

FreeRTOS CDC driver: ~71 kbyte/s (RX)
ATMEL CDC driver: ~850kbyte/s (RX), which is pretty close to the theoretical maximum

Since the ~71 kbyte/s are not sufficient, I thought about implementing the ATMEL driver into the FreeRTOS environment. The biggest difference I can see, is how the received data gets taken out of the endpoint FIFO:

FreeRTOS: The bytes are taken out of the FIFO in a Task and written to a queue (byte for byte)

ATMEL: The bytes from the FIFO are copied into a buffer during the Interrupt

What walls would I run into when taking the ATMEL driver ? Where do you see the problems ?

Thank you very much for reading all this and giving me suggestions :slight_smile:



richard_damon wrote on Monday, July 18, 2011:

The biggest issue with a single buffer is that if a second message shows up before the “USB Task” processes the previous one, you need to throw away the new message. If the task can make sure this doesn’t happen (limited tasks in competition to it, and not needing to wait for some other task/resource) then it might not be a serious issue. The driver (I am not familiar with it) might be built with this sort of assumption, and maybe your application allows it too.

One solution to this is to have several buffers that you use in rotation, so that if the task isn’t done yet, you still have a place to put the data rather than just discarding it. You will still have the possibility that you fall far enough behind that all the buffers are in use, but you can greatly cut down the risk, or the level of analysis needed to prove that you can generally keep up.

ckronen wrote on Wednesday, July 20, 2011:

Thanks for your answer,

the USB Task will always have the highest priority in the projects, this will not be an issue. Both drivers already use 2 FIFO banks.

I think before I port something new I will try to find out why the FreeRTOS USB-CDC driver is so slow.



rtel wrote on Wednesday, July 20, 2011:

I think before I port something new I will try to find out why the FreeRTOS USB-CDC driver is so slow.

I should point out, there is no such thing as a FreeRTOS USB-CDC driver, only demo projects that include drivers that are not intended to be commercial quality and are normally supplied by silicon vendors.  FreeRTOS is a kernel for which there is a wide ecosystem that comes from third parties - not FreeRTOS itself.


ckronen wrote on Wednesday, July 20, 2011:

Yes of course Richard, sorry. I always think of it as the “FreeRTOS USB-CDC” driver, I know it is just a sample from a demo. It even says in the description that it does not have a great performance due to heavy queue usage.



heinbali01 wrote on Monday, January 02, 2012:

Hi Chris,

Although your post was quite some time ago:
I think the FreeRTOS USB-CDC was slow because the data were stored/queued byte-per-byte
You better store packages of 64 bytes at a time, along with a length field
Try to wake up the USB task from the USB-EP interrupt, both for Rx as well as Tx events, using xSemaphoreGiveFromISR().
It is safer to keep interrupts short, the price is low: only a few uS delay between interrupt and wakeup.
In this way I managed to get a throughput of a few hundred kB/sec (on a slower UC3A)

Best regards, Hein

tuxscreen wrote on Tuesday, April 10, 2012:

Hi Hein,

you replied that you get a throughput of several 100kB/sec. May I ask, how much exactly? I tried to solve it, just as you described - I tried to read as much bytes as they were available and I put them into a “test” queue, just for speed measurements. I’m getting ~ 230kb/sec but I need more, something about 500kb/sec…