BUG or feature in using usbFunctionRead?

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
jokkebk
Posts: 4
Joined: Tue Apr 12, 2011 10:26 pm

BUG or feature in using usbFunctionRead?

Post by jokkebk » Fri Feb 24, 2012 1:43 pm

I tried to use usbFunctionRead today to transfer longer strings to PC. In a nutshell, the firmware code looked like this (nothing special):

Code: Select all

static int dataSent; // for USB_DATA_LONGOUT

USB_PUBLIC usbMsgLen_t usbFunctionSetup(uchar data[8]) {
    usbRequest_t *rq = (void *)data; // cast data to correct type
   
    if(rq->bRequest == USB_DATA_LONGOUT) {
        dataSent = 0;
        return USB_NO_MSG;
    }
}

USB_PUBLIC uchar usbFunctionRead(uchar *data, uchar len) {
    uchar i;

    for(i = 0; dataSent < 128 && i < len; i++, dataSent++)
        data[i] = '0'+i;
   
    if(i && dataSent == 128)
        data[i-1] = '\0';
      
    return i; // equals the amount of bytes written
}


Then I defined USB_CFG_IMPLEMENT_FN_READ and flashed. On PC side, I used libusb with essentially the following code (nothing special here either):

Code: Select all

char buffer[256] = "";

nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
    USB_DATA_LONGOUT, 0, 0, (char *)buffer, sizeof(buffer), 5000);

printf("Received %d bytes: %s\n", nBytes, buffer);
   
if(nBytes < 0)
    fprintf(stderr, "USB error: %s\n", usb_strerror());
else
    printf("Received %d bytes: %s\n", nBytes, buffer);      


  • With buffer size >= 256, I'm able to receive size&255 bytes. It seems while wLength is word-sized, V-USB is only using the lower byte
  • With buffer size == 255, I get "USB error: libusb0-dll:err [control_msg] sending control message failed, win error: The I/O operation has been aborted because of either a thread exit or an application request."
  • With buffer size <= 254, everything works as it should

It seems like a bug, or a very strange feature. Especially as the "standard" V-USB method of returning stuff with usbMsgPtr instead of usbFunctionRead completely works with any buffer size. Defining USB_CFG_LONG_TRANSFERS immediately fixes this problem.

Joonas

Post Reply