USB Generic Interface Device

The USB device stack provides the ability to create generic (vendor specific) interfaces.

Code Example

The code snippet below demonstrates how to use the device stack to create a single generic interface with 2 endpoints, one for receiving data and the other for transmitting data.

#include "USB/Device/usbd.h"
#include <assert.h>

static USBD usb;                                    /* Allocate a USB device stack instance */
static USBDINTERFACE intf;                          /* Allocate a generic USB interface instance */
static USBDENDPOINT eprx;                           /* Allocate an endpoint for receiving */
static USBDENDPOINT eptx;                           /* Allocate an endpoint for transmitting */


void APP_InitUSB(void)
{
    STATUS status;


    status = USBD_Create(&usb,                              /* Create and initialize the USB stack instance */
                         0,                                 /* The number of the port to be used by the USB stack */
                         0x0000,                            /* Vendor ID */
                         0x0012);                           /* Product ID */

    assert(status == SUCCESS);

    status = USBD_SetDeviceName(&usb,                       /* Assign name strings to the USB device */
                                "DZX",                      /* Manufacturer name */
                                "USB Demo",                 /* Product name */
                                NULL);                      /* Serial number (NULL for none) */

    assert(status == SUCCESS);

    status = USBD_CreateInterface(&usb                      /* Initialize the generic USB interface */
                                  &intf,                    /* A pointer to the interface to be initialized */
                                  USBINTFCLASSVENDOR,       /* Vendor defined class code */
                                  USBINTFSUBCLASSVENDOR,    /* Vendor defined subclass code */
                                  USBINTFPROTOCOLVENDOR);   /* Vendor defined protocol code */
    assert(status == SUCCESS);
    
    status = USBD_CreateEndpoint(&intf,                     /* Initialize and add the endpoint for receiving data */
                                 &eprx,                     /* A pointer to the endpoint to be initialized */
                                 USBTRANSFERBULK,           /* Use bulk transfers */
                                 0x01,                      /* Endpoint address */
                                 64,                        /* Max packet size for full speed */
                                 512,                       /* Max packet size for high speed */
                                 1,                         /* Interval */
                                 OPT_SHORTPKT);             /* Options => Terminate with short packets */
    assert(status == SUCCESS);

    status = USBD_CreateEndpoint(&intf,                     /* Initialize and add the endpoint for transmitting data */
                                 &eptx,                     /* A pointer to the endpoint to be initialized */
                                 USBTRANSFERBULK,           /* Use bulk transfers */
                                 0x81,                      /* Endpoint address */
                                 64,                        /* Max packet size for full speed */
                                 512,                       /* Max packet size for high speed */
                                 1,                         /* Interval */
                                 OPT_SHORTPKT);             /* Options => Terminate with short packets */
    assert(status == SUCCESS);

    status = USBD_SetMicrosoftDescriptorSupport(&usb,       /* Enable automatic loading of the WinUSB driver on Windows */
                                                TRUE);
    assert(status == SUCCESS);

    status = USBD_Open(&usb);                               /* Open and start communications with an attached host */
    assert(status == SUCCESS);
}

The code snippet below shows a thread function that continually reads the receive endpoint. Any data that is received is echoed and transmitted over the transmit endpoint.

#include "USB/Device/usbd.h"
#include <assert.h>

void APP_Thread(void* arg)
{
    ALIGNED(4) static BYTE buffer[256];
    STATUS status;
    UINT32 len;


    for (;;) {                                              /* A forever loop */

        status = USBD_Read(&eprx,                           /* Read a message from the receive endpoint */
                           buffer,                          /* A pointer to the buffer to receive the data */
                           sizeof(buffer),                  /* The size of the buffer (max amount that can be received) */
                           &len,                            /* A pointer to a value to receive actual amount received */
                           INFINITE);                       /* Wait indefinitely until data is received */

        if (status == SUCCESS) {

            status = USBD_Write(&eptx,                      /* Transmit the data that has been received */
                                buffer,                     /* A pointer to the data to be transmitted */
                                len,                        /* The number of data bytes to be transmitted */
                                NULL,                       /* A pointer to a variable to receive the actual amount transmitted */
                                1000);                      /* Wait for up to 1000 kernel ticks (msec) for the transfer to complete */

            assert(status == SUCCESS);                      /* Success? */
        }
    }
}

Host Software

The generic interface can be tested using the USB Terminal utility tool. The USB Terminal provides the ability to send and receive USB messages from a Windows PC.

Use the .NET USB Device Software to develop custom PC applications that communicate with the generic USB interfaces.