Adding Interrupt OUT to powerSwitch.

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
ComBoy

Adding Interrupt OUT to powerSwitch.

Post by ComBoy » Sun May 18, 2008 8:48 pm

hello,

I got the power switch app working on my mega644p.
but i've got problems expanding the code.

I tried sending data with Interrupt Out but don't get it to work. I defined:

Code: Select all

#define USB_CFG_IMPLEMENT_FN_WRITEOUT   1

and added the function:

Code: Select all

USB_PUBLIC void usbFunctionWriteOut(uchar *data, uchar len)
{
      buffer = 0xff;
      PORTA = buffer;
}

to my code. so whenever any Interrupt Out message arrives all LEDs on port A should light up.

But whenever I try to send

Code: Select all

int ep = 0x03;
char message[4] = {0xFF,0xff,0xff,0xff};
int size = sizeof(message);
int timeout = 5000;
int bytes = usb_interrupt_write(handle, ep, message, size,  timeout);


from the host i just get error return code -22

Do I have to define an additional endpoint in the avr device? or is some more code than the usb_interrupt_write() neccesary in the host app?

best wishes,

comboy[/code]

miroslav.talasek
Posts: 5
Joined: Mon May 19, 2008 8:19 am

Post by miroslav.talasek » Mon May 19, 2008 8:28 am

I have similar problem but i want use endpoint 2 to writeOut
i have

Code: Select all

void usbFunctionWriteOut(uchar *data, uchar len){
....

}

and

Code: Select all

#define USB_CFG_IMPLEMENT_FN_WRITEOUT   1

but lsusb -v return

Code: Select all

Bus 004 Device 100: ID 16c0:3e08
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass          255 Vendor Specific Class
  bDeviceSubClass         0
  bDeviceProtocol         0
  bMaxPacketSize0         8
  idVendor           0x16c0
  idProduct          0x3e08
  bcdDevice            1.00
  iManufacturer           1 miREC.ta
  iProduct                2 USBLcd
  iSerial                 0
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           18
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0
    bmAttributes         0x80
      (Bus Powered)
    MaxPower               40mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           0
      bInterfaceClass         0 (Defined at Interface level)
      bInterfaceSubClass      0
      bInterfaceProtocol      0
      iInterface              0
Device Status:     0x0000
  (Bus Powered)

there is zero endpoints
must i uncomment or change any other in usbconfig.h ?

on host side

Code: Select all

 usb_claim_interface(handle, 0);
char bufferOut[255];
   memset(bufferOut,0xff,0xff);
   usb_bulk_write(
            handle,             // handle obtained with usb_open()
           USB_ENDPOINT_OUT | 2,// identifies endpoint
           bufferOut,             // data buffer
           200,     // legth
           1000
       );



but messages to ENDPOINT_IN 0 works perfectly[/quote]

Grendel
Rank 4
Rank 4
Posts: 167
Joined: Sat Dec 16, 2006 9:53 pm
Location: Oregon, USA
Contact:

Post by Grendel » Mon May 19, 2008 9:42 am

Seems there's no endpoint descriptor for any OUT EP in the default configuration descriptor in usbdrv.c. What does error -22 mean ?

Guest

Post by Guest » Mon May 19, 2008 10:25 am

Meanwhile I assume error -22 means no endpoint found ;)

I added an endpoint descriptor manually to usbdrv.c, now the usb_interrupt_write() function returns a valid value.

it doesn't mean my device works now... no matter what instruction is in
usbFunctionWriteOut() all LEDs on port A light up when interrupt is received.
even if usbFunctionWriteOut contains PORTA = 0x00;

any ideas on this?

same happens when i try to send some payload data with a controller msg.
the host says transfer went allright, but on my usb device, again, all LEDs on port A light up even if usbFunctionWrite() contains only PORTA = 0x00;

so long,

ComBoy :)

miroslav.talasek
Posts: 5
Joined: Mon May 19, 2008 8:19 am

Post by miroslav.talasek » Mon May 19, 2008 10:58 am

it is not a solution , it must work !

ComBoy

Post by ComBoy » Mon May 19, 2008 10:08 pm

Hooray! it works.
defining my own out endpoint in the usb descriptor helped. i found some info here
USB in a nutshell

the problem with all of port A bekoming 0xff was a little typo in my code ;)

but what i ask myself now is if there is a proper way of using only one interrupt out point without modifying usbdrv.c?

so long,
comboy

Grendel
Rank 4
Rank 4
Posts: 167
Joined: Sat Dec 16, 2006 9:53 pm
Location: Oregon, USA
Contact:

Post by Grendel » Mon May 19, 2008 11:07 pm

Configure usbconfig.h to provide your own configuration descriptor (set USB_CFG_DESCR_PROPS_CONFIGURATION to USB_PROP_IS_DYNAMIC) and add the function uchar usbFunctionDescriptor( struct usbRequest *rq ) to your code.

I don't have the time to go into detail (@ work right now..), I did it in my project tho -- check out the 3DP-Vert project for details.

ComBoy

Post by ComBoy » Mon May 19, 2008 11:22 pm

ok i see :D

Thanks for the help all!
this should also solve the problem of miroslav.talasek.
for a bulk out you need to define your endpoint as well

so let's see what i can build with the newly aquired USB skills :D
comboy

miroslav.talasek
Posts: 5
Joined: Mon May 19, 2008 8:19 am

Post by miroslav.talasek » Tue May 20, 2008 8:05 am

I buils my own descriptor with 2 endpoints

in lsusb now i can see

Code: Select all

Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass         0 (Defined at Interface level)
      bInterfaceSubClass      0
      bInterfaceProtocol      0
      iInterface              0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x00  EP 0 OUT
        bmAttributes            0
          Transfer Type            Control
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0008  1x 8 bytes
        bInterval              10
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x00ff  1x 255 bytes
        bInterval              10
Device Status:     0x0000
  (Bus Powered)


it seems all is ok
but function

Code: Select all

char bufferOut[255];
   memset(bufferOut,255,255);
   int test = usb_bulk_write(
            handle,             // handle obtained with usb_open()
           0x02,// identifies endpoint 1
           bufferOut,             // data buffer
           200,     // maximum amount to read
           1000
       );

return not any error but always zero

messages to control endpoint still work perfectly

Guest

Post by Guest » Tue May 20, 2008 11:43 am

did you set the configuration and claimed the interface on host side?

the device open should look something like this:

Code: Select all

usb_dev_handle *handle;
 
if(!usbOpenDevice(&handle, VENDOR_ID, NULL, PRODUCT_ID, NULL))
    abortWithDeviceWasNotFondError();
usb_set_configuration(handle, 1);
usb_claim_interface(handle, 0);



from docu wiki

so long,
comboy

miroslav.talasek
Posts: 5
Joined: Mon May 19, 2008 8:19 am

Post by miroslav.talasek » Tue May 20, 2008 8:00 pm

my host code is

Code: Select all

usb_init();
    if(usbOpenDevice(&handle, USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT) != 0){
        fprintf(stderr, "Could not find USB device \"PowerSwitch\" with vid=0x%x pid=0x%x\n", USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT);
        exit(1);
    }
    usb_set_configuration(handle, 1);

    usb_claim_interface(handle, 0);
   

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


   char bufferOut[255];
   memset(bufferOut,255,255);
   int test = usb_bulk_write(
            handle,             // handle obtained with usb_open()
           0x02,// identifies endpoint 2
           bufferOut,             // data buffer
           200,     
           10000
       );

kwebdesigns
Rank 1
Rank 1
Posts: 23
Joined: Sat Mar 15, 2008 7:46 pm

Post by kwebdesigns » Sat May 24, 2008 7:03 pm

I am having trouble getting a descriptor for Bulk-Out Endpoint 1. I too get -22 returned from writing to this endpoint.

In usbconfig.h I have:

Code: Select all

#define USB_CFG_DESCR_PROPS_CONFIGURATION           USB_PROP_IS_DYNAMIC


In main.c I have this for the descriptor:

Code: Select all

PROGMEM char configDescriptor[] = {    /* USB configuration descriptor */ 
    9,          /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
    USBDESCR_CONFIG,    /* descriptor type */
    25,   /* total length of data returned (including inlined descriptors) */
   0,
    1,          /* number of interfaces in this configuration */
    1,          /* index of this configuration */
    0,          /* configuration name string index */
#if USB_CFG_IS_SELF_POWERED
    USBATTR_SELFPOWER,  /* attributes */
#else
    USBATTR_BUSPOWER,   /* attributes */
#endif
    USB_CFG_MAX_BUS_POWER/2,            /* max USB current in 2mA units */
/* interface descriptor follows inline: */
    9,          /* sizeof(usbDescrInterface): length of descriptor in bytes */
    USBDESCR_INTERFACE, /* descriptor type */
    0,          /* index of this interface */
    0,          /* alternate setting for this interface */
    1,   /* endpoints excl 0: number of endpoint descriptors to follow */
    USB_CFG_INTERFACE_CLASS,
    USB_CFG_INTERFACE_SUBCLASS,
    USB_CFG_INTERFACE_PROTOCOL,
    0,          /* string index for interface */
/* endpoint descriptor for endpoint 1 out */
    7,          /* sizeof(usbDescrEndpoint) */
    USBDESCR_ENDPOINT,  /* descriptor type = endpoint */
    0x01,       /* Out endpoint number 1 */
    0x02,       /* attrib: Bulk endpoint */
    8, 0,      /* maximum packet size */       
    USB_CFG_INTR_POLL_INTERVAL+25 /* in ms */
};

uchar usbFunctionDescriptor( struct usbRequest *rq ) {
   uchar *p = NULL, len = 0;
   
   if(rq->wValue.bytes[1] == USBDESCR_CONFIG){
      p = (uchar *)configDescriptor;
      len = sizeof(configDescriptor);
   }
   
    usbMsgPtr = p;
    return len;
}


libusb shows this about my device:

Code: Select all

  wTotalLength:         18
  bNumInterfaces:       1
  bConfigurationValue:  1
  iConfiguration:       0
  bmAttributes:         80h
  MaxPower:             50
    bInterfaceNumber:   0
    bAlternateSetting:  0
    bNumEndpoints:      0
    bInterfaceClass:    0
    bInterfaceSubClass: 0
    bInterfaceProtocol: 0
    iInterface:         0

Any help would be appreciated. Thanks in advance.

EDIT: I got it to work. Oddly enough I had removed the symbol to oddebug.o from the Makefile and I added it back to try and debug the device. After adding the symbol it worked.

Post Reply