Author Topic: USB buffer overrun?  (Read 12639 times)

dpenney

  • Member
  • ***
  • Posts: 8
USB buffer overrun?
« on: October 02, 2012, 07:24:45 pm »
Hello,
I am using USB HID with mostly excellent results, but I have a few questions.

1) Looking at MSN documentation for CreateFile() & ReadFile(), it seems like there is a buffer/cache automatically created for it, at least when used for disk i/o.  Is such a buffer created also for USB comm?

2) I don't see how to detect if there is a buffer overrun for the above, is there such a way?

3) I am currently polling GetOverlappedResult() while doing other tasks in the loop and I have an error that makes me suspect I might be missing some USB data.  If I take out the other tasks, the error is resolved.

So I am about to put the GetOverlappedResult() in a separate thread and just use it to only load a circular buffer (that is unloaded by the main thread) with the USB data, but even in this case I would like to be able to check some overrun function to make sure even this tight loop did not miss anything.  Is this the right approach?

Thanks,
dpenney

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: USB buffer overrun?
« Reply #1 on: October 03, 2012, 09:55:09 am »
The HID driver creates a buffer for received reports.

If the ReadFile buffer is full, on receiving a new report, the driver drops the oldest report. To prevent losing reports, use HidD_SetNumInputBuffers to increase the buffer size, use longer reports sent less frequently, have the application read reports more often, or increase the size of the buffer in the call to ReadFile. (ReadFile returns as many reports as are available, up to ReadFile's buffer size.)

I don't know of any way to detect missing reports unless you build it into your report data (giving each report a number for example).

If you absolutely can't afford to miss any data, use Feature reports.
« Last Edit: October 03, 2012, 11:03:18 am by Jan Axelson »

dpenney

  • Member
  • ***
  • Posts: 8
Re: USB buffer overrun?
« Reply #2 on: October 03, 2012, 11:53:05 am »
Jan, thanks very much for reply, it is very helpful.

I'm trying to make sure I fully understand your comment "If the ReadFile buffer is full...".
Just to be certain, the driver doesn't literally check (or have any way to do so) that the Readfile buffer is full, right?

Because based on what you said, I am thinking of this as there is the HID driver buffer which is asynchronously catching the reports and then every time I call the ReadFile function it takes as much as is available from the HID driver buffer into the buffer specified in the ReadFile argument, up to that ReadFile buffer's size.  And if I don't call ReadFile often enough (or with a large enough buffer specified in it's argument), the full HID driver starts throwing away the oldest as it gets new reports.

Thanks again.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: USB buffer overrun?
« Reply #3 on: October 03, 2012, 12:25:33 pm »
yes