This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to set USBBUFFERSIZE for USBBufferRead()



Hi!

I am testing the CDC Device Demo. USBBUFFERSIZE is set to 256 and this value is passed to my receive buffer which is a tUSBBuffer.

Now it turns out that although the buffer size is 256, USBBufferSpaceAvailable() returns only 255 for an empty buffer and only 255 bytes can be read at once when a call to USBBufferRead() is performed. I don't see this behavior documented anywhere in StellarisWare, to the contrary in the USB lib guide says:

"This function reads up to ulLength bytes of data received from the USB host into the supplied application buffer."

My questions are: Why does the buffered read read one byte less than the buffer size and is it efficient to set the buffer size to 256 as done in the demo when the USB packets are 64 bytes each? Or should I set the buffer to 257 to use it more efficiently?

Thanks,

Anguel

  • Hello Anguel,

    This behavior is due to the use of the ring buffer and is based on the way a buffer full is indicated (when pointers are equal then buffer empty, when pointers are one apart then buffer full).  So you do lose one byte in the ring buffer. 

    Hope that helps.

    Regards,

    Craig

  • Craig, thank you for the reply. Losing one byte is not a big problem, however I wonder what is better for efficiency. Should I leave the buffer be a multiple of 64 (e.g. 256) or should I instead add one byte (i.e. 257) so that a multiple of 64 can really fit inside.

  • Hello Anguel,

    Implementing the ring buffer this way saves a few bytes over implementing a counter based approach.  The buffer, the way it is defined already, provides for multiple 64 byte data packets to be queued (> 2 packets).  Depending on the system loading/bandwidth this is generally sufficient, but your system may be different.  Also don't know what the data traffic looks like to determine if there are many low byte count USB transfers that are happening.  Overall, I would say you can leave the buffer size the way it is, however, you can adjust it to suit your needs (may even be able to drop the size down).

    Hope that helps.

    Regards,

    Craig

  • Craig,

    Thank you once again for your help, I did some measurements with streaming 10 MB from the host to the MCU with USBBUFFERSIZE = 256 and USBBUFFERSIZE = 257 and did not see big differences.

    Regards,

    Anguel