Double buffered USB endpoints

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Double buffered USB endpoints

Post by Badger »

Hi,

I've been having some trouble getting double buffered endpoints working. I'm trying to put together a simple test program to demonstrate, but in the mean time what I am doing is this:

Defining the endpoint config:

Code: Select all

static const USBEndpointConfig uart1_ep_c = {
      .ep_mode       = USB_EP_MODE_TYPE_BULK | USB_EP_MODE_PACKET,
      .setup_cb       = NULL,
      .in_cb          = uart_tx,
      .out_cb       = uart_rx,
      .in_maxsize    = 0x0040,
      .out_maxsize    = 0x0040,
      .in_state       = NULL,
      .out_state      = NULL
};


Initialising the endpoint in the usb event handler:

Code: Select all

usbInitEndpointI(usbp, UART1_EPNUM, &uart1_ep_c);


UART1_EPNUM = 1.

The USB descriptors are set correctly and include the endpoint twice at addresses 0x81 and 0x01. The device enumerates fine and that is not a problem.

But I am finding that whilst the device responds fine to a bulk write, the bulk read call back is never used. I put together a very simple libusb-0.1 based test program to connect to the device. The write succeeds but the read fails due to timeout. I tried to upload this as an attachment but the board wont allow .c files...

Code: Select all

#include <stdio.h>
#include <sys/types.h>

#include <usb.h>

int main(void)
{
    struct usb_bus *bus;
    struct usb_device *dev;
   struct usb_dev_handle *device = NULL;
   int vendor = 0x1403;
   int product = 0x6010;
   char buf[64];
   int ret;
   int timeout = 1000;
   char iface;
   char out, in;
   
   int d = 0;
   
   if(d == 1) {
      iface = 1;
      out = 0x03;
      in = 0x81;
   } else {
      iface = 0;
      out = 0x01;
      in = 0x01;
   }
   
   usb_init();
   
   if (usb_find_busses() < 0)
        printf("usb_find_busses() failed\n");
    if (usb_find_devices() < 0)
        printf("usb_find_devices() failed\n");

   printf("Looking for usb device..\n");
   for (bus = usb_get_busses(); bus; bus = bus->next) {
        for (dev = bus->devices; dev; dev = dev->next) {
            if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) {
            printf("Found it!\n");
            device = usb_open(dev);
            bus = NULL;
            break;
         }
      }
      if(bus == NULL)
         break;
   }
   
   if(device == NULL) {
      printf("Couldn't find device\n");
      return -1;
   }
   usb_claim_interface(device, iface);

   ret = usb_bulk_write (device, out, buf, 64, timeout);
   if(ret < 0) {
      fprintf(stderr, "usb error: %d (%s)\n", ret, usb_strerror());
   } else {
      printf("wrote some data\n");
   }
   
   ret = usb_bulk_read (device, in, buf, 64, timeout);
   if(ret < 0) {
      fprintf(stderr, "usb error: %d (%s)\n", ret, usb_strerror());
   } else {
      printf("read some data\n");
   }   
   return 0;
}


In the end, after digging through the ChibiOS USB code I managed to get it working by changing usb_lld_init_endpoint() slightly from:

Code: Select all

  /* IN endpoint settings, always in NAK mode initially.*/
  if (epcp->in_cb != NULL)
    epr |= EPR_STAT_TX_NAK;

To:

Code: Select all

  /* IN endpoint settings, always in NAK mode initially.*/
  if (epcp->in_cb != NULL)
    epr |= EPR_STAT_TX_VALID;


Which leads me to my question: does the current usb stack handle double buffered endpoints?

Thanks!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Double buffered USB endpoints

Post by Giovanni »

Hi Badger,

You are right, the current driver implementation does not support double buffered endpoints and isochronous endpoints, double buffered endpoints are in to-do list for the current development branch, I wish to have them working before 2.4.0.

It is only matter of handling a configuration option for the endpoint and the double buffer into read/write functions.

Giovanni
User avatar
Badger
Posts: 346
Joined: Mon Apr 18, 2011 6:07 pm

Re: Double buffered USB endpoints

Post by Badger »

Hi Giovanni,

In which case I shall experiment with implementing the double buffered endpoints and post my findings.

Thanks!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Double buffered USB endpoints

Post by Giovanni »

Great, let me know, and BTW, thanks for the libusb example, I never used that, it could be useful for implementing a test suite for the USB drivers.

Giovanni
Post Reply