Author Topic: USB fnWrite not writing all data  (Read 6421 times)

Offline cmalan

  • Newbie
  • *
  • Posts: 14
    • View Profile
USB fnWrite not writing all data
« on: October 28, 2013, 02:29:38 PM »
Hi
Why would fnWrite not copy all the bytes to the USB endpoint.
Code: [Select]
if (fnWrite(USBPortID_mcu, 0, 1024) != 0) {     // check that there is space for a block of data

     if(fnMsgs(USBPortID_mcu) != 0) {
         fnDebugMsg("Usb Data ");
         Length = fnRead(USBPortID_mcu, ucInputMessage, BIG_MESSAGE); // read the content
         fnDebugDec(Length,0);
         fnDebugMsg(" bytes in ");
         Length = fnProcessMCUMessage("USB",0,ucInputMessage,Length,ucOutputMessage);
         fnDebugDec(Length,0);
         fnDebugMsg(" bytes to send ");
         if(Length > 0)
             Length = fnWrite(USBPortID_mcu,ucOutputMessage,Length);
         fnDebugDec(Length,0);
         fnDebugMsg(" bytes sent\r\n");
     }
    }
The function fnProcessMCUMessage dont return more than 512 bytes.

Quote
Usb Data 18 bytes in 34 bytes to send 34 bytes sent
Usb Data 18 bytes in 34 bytes to send 34 bytes sent
Usb Data 18 bytes in 34 bytes to send 34 bytes sent
Usb Data 34 bytes in 498 bytes to send 20 bytes sent
Usb Data 34 bytes in 66 bytes to send 66 bytes sent
Usb Data 34 bytes in 498 bytes to send 480 bytes sent
Usb Data 34 bytes in 66 bytes to send 66 bytes sent
Usb Data 34 bytes in 66 bytes to send 66 bytes sent
Usb Data 34 bytes in 498 bytes to send 498 bytes sent
Usb Data 34 bytes in 498 bytes to send 376 bytes sent

The buffer is supposed to have space 1024 bytes but sometimes the 498 bytes dont get sent.

The debug output is from the telnet session.

MCF52259 GNU compiler

Christo

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: USB fnWrite not writing all data
« Reply #1 on: October 29, 2013, 11:07:19 PM »
Hi Christo

At the moment I don't see why the write would not accept the full size. The USB driver uses the standard queue driver as input and so wouldn't expect any difference in behaviour compared to the UART, for example.

What value does fnWrite(USBPortID_mcu, 0, 1024) return?
And, what speed is the code being repeated at?
Is there any other code writing to the USB? For example from interrupt routines that could occur while this code is being executed (and thus filling the output buffer)?
When the write returns less bytes than expected what happens if you check with fnWrite(USBPortID_mcu, 0, 1024) again, or repeat the write (as experiment)?

Regards

Mark

Offline cmalan

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: USB fnWrite not writing all data
« Reply #2 on: October 30, 2013, 08:31:52 AM »
Hi Mark
I have to check what fnWrite(USBPortID_mcu, 0, 1024) returns.
It is just a simple request response protocol, I slowed it down to once a second made no difference.
No other code is writing to the USB yet, I still have to write that code.
If I write the remaining bytes immediately after the first write it works every time. I can even write the complete message again then it works.

To me it look like a problem at the point where the circular buffer wraps.

To get it working a changed my buffer size to 512 and padded all the messages up to 512 bytes, fnWrite(USBPortID_mcu,ucOutputMessage,BIG_MESSAGE), it works now with all the delays removed.

Christo