Commit 63ddf6ed authored by Theodor-Adrian Stana's avatar Theodor-Adrian Stana

usb: Now able to blink LEDs from USB

usbled host-side s/w controls blinking the LEDs
EFM32 code in usbdcdc
parent a3dd4e81
/***************************************************************************//** /***************************************************************************//**
* @file descriptors.h * @file descriptors.h
* @brief USB descriptors for CDC Serial Port adapter example project. * @brief USB descriptors for Vendor Unique Device example project.
* @version 3.20.5 * @version 3.20.5
******************************************************************************* *******************************************************************************
* @section License * @section License
...@@ -23,7 +23,7 @@ static const USB_DeviceDescriptor_TypeDef deviceDesc __attribute__ ((aligned(4)) ...@@ -23,7 +23,7 @@ static const USB_DeviceDescriptor_TypeDef deviceDesc __attribute__ ((aligned(4))
.bLength = USB_DEVICE_DESCSIZE, .bLength = USB_DEVICE_DESCSIZE,
.bDescriptorType = USB_DEVICE_DESCRIPTOR, .bDescriptorType = USB_DEVICE_DESCRIPTOR,
.bcdUSB = 0x0200, .bcdUSB = 0x0200,
.bDeviceClass = USB_CLASS_CDC, .bDeviceClass = 0xFF,
.bDeviceSubClass = 0, .bDeviceSubClass = 0,
.bDeviceProtocol = 0, .bDeviceProtocol = 0,
.bMaxPacketSize0 = USB_EP0_SIZE, .bMaxPacketSize0 = USB_EP0_SIZE,
...@@ -32,116 +32,42 @@ static const USB_DeviceDescriptor_TypeDef deviceDesc __attribute__ ((aligned(4)) ...@@ -32,116 +32,42 @@ static const USB_DeviceDescriptor_TypeDef deviceDesc __attribute__ ((aligned(4))
.bcdDevice = 0x0000, .bcdDevice = 0x0000,
.iManufacturer = 1, .iManufacturer = 1,
.iProduct = 2, .iProduct = 2,
.iSerialNumber = 0, .iSerialNumber = 3,
.bNumConfigurations = 1 .bNumConfigurations = 1
}; };
#define CONFIG_DESCSIZE ( USB_CONFIG_DESCSIZE + \
(USB_INTERFACE_DESCSIZE * 2) + \
(USB_ENDPOINT_DESCSIZE * NUM_EP_USED) + \
USB_CDC_HEADER_FND_DESCSIZE + \
USB_CDC_CALLMNG_FND_DESCSIZE + \
USB_CDC_ACM_FND_DESCSIZE + \
5 )
EFM32_ALIGN(4) EFM32_ALIGN(4)
static const uint8_t configDesc[] __attribute__ ((aligned(4)))= static const uint8_t configDesc[] __attribute__ ((aligned(4)))=
{ {
/*** Configuration descriptor ***/ /*** Configuration descriptor ***/
USB_CONFIG_DESCSIZE, /* bLength */ USB_CONFIG_DESCSIZE, /* bLength */
USB_CONFIG_DESCRIPTOR, /* bDescriptorType */ USB_CONFIG_DESCRIPTOR, /* bDescriptorType */
CONFIG_DESCSIZE, /* wTotalLength (LSB) */
USB_CONFIG_DESCSIZE>>8, /* wTotalLength (MSB) */ USB_CONFIG_DESCSIZE + /* wTotalLength (LSB) */
2, /* bNumInterfaces */ USB_INTERFACE_DESCSIZE +
(USB_ENDPOINT_DESCSIZE * NUM_EP_USED),
(USB_CONFIG_DESCSIZE + /* wTotalLength (MSB) */
USB_INTERFACE_DESCSIZE +
(USB_ENDPOINT_DESCSIZE * NUM_EP_USED))>>8,
1, /* bNumInterfaces */
1, /* bConfigurationValue */ 1, /* bConfigurationValue */
0, /* iConfiguration */ 0, /* iConfiguration */
CONFIG_DESC_BM_RESERVED_D7 | /* bmAttrib: Self powered */ CONFIG_DESC_BM_RESERVED_D7 | /* bmAttrib: Self powered */
CONFIG_DESC_BM_SELFPOWERED, CONFIG_DESC_BM_SELFPOWERED,
CONFIG_DESC_MAXPOWER_mA( 100 ),/* bMaxPower: 100 mA */ CONFIG_DESC_MAXPOWER_mA( 100 ),/* bMaxPower: 100 mA */
/*** Communication Class Interface descriptor (interface no. 0) ***/ /*** Interface descriptor ***/
USB_INTERFACE_DESCSIZE, /* bLength */ USB_INTERFACE_DESCSIZE, /* bLength */
USB_INTERFACE_DESCRIPTOR,/* bDescriptorType */ USB_INTERFACE_DESCRIPTOR,/* bDescriptorType */
0, /* bInterfaceNumber */ 0, /* bInterfaceNumber */
0, /* bAlternateSetting */ 0, /* bAlternateSetting */
1, /* bNumEndpoints */ NUM_EP_USED, /* bNumEndpoints */
USB_CLASS_CDC, /* bInterfaceClass */ 0xFF, /* bInterfaceClass */
USB_CLASS_CDC_ACM, /* bInterfaceSubClass */
0, /* bInterfaceProtocol */
0, /* iInterface */
/*** CDC Header Functional descriptor ***/
USB_CDC_HEADER_FND_DESCSIZE, /* bFunctionLength */
USB_CS_INTERFACE_DESCRIPTOR, /* bDescriptorType */
USB_CLASS_CDC_HFN, /* bDescriptorSubtype */
0x20, /* bcdCDC spec.no LSB */
0x01, /* bcdCDC spec.no MSB */
/*** CDC Call Management Functional descriptor ***/
USB_CDC_CALLMNG_FND_DESCSIZE, /* bFunctionLength */
USB_CS_INTERFACE_DESCRIPTOR, /* bDescriptorType */
USB_CLASS_CDC_CMNGFN, /* bDescriptorSubtype */
0, /* bmCapabilities */
1, /* bDataInterface */
/*** CDC Abstract Control Management Functional descriptor ***/
USB_CDC_ACM_FND_DESCSIZE, /* bFunctionLength */
USB_CS_INTERFACE_DESCRIPTOR, /* bDescriptorType */
USB_CLASS_CDC_ACMFN, /* bDescriptorSubtype */
0x02, /* bmCapabilities */
/* The capabilities that this configuration supports: */
/* D7..D4: RESERVED (Reset to zero) */
/* D3: 1 - Device supports the notification Network_Connection. */
/* D2: 1 - Device supports the request Send_Break */
/* D1: 1 - Device supports the request combination of Set_Line_Coding, */
/* Set_Control_Line_State, Get_Line_Coding, and the */
/* notification Serial_State. */
/* D0: 1 - Device supports the request combination of Set_Comm_Feature, */
/* Clear_Comm_Feature, and Get_Comm_Feature. */
/*** CDC Union Functional descriptor ***/
5, /* bFunctionLength */
USB_CS_INTERFACE_DESCRIPTOR, /* bDescriptorType */
USB_CLASS_CDC_UNIONFN, /* bDescriptorSubtype */
0, /* bControlInterface, itf. no. 0 */
1, /* bSubordinateInterface0, itf. no. 1 */
/*** CDC Notification endpoint descriptor ***/
USB_ENDPOINT_DESCSIZE, /* bLength */
USB_ENDPOINT_DESCRIPTOR,/* bDescriptorType */
EP_NOTIFY, /* bEndpointAddress (IN) */
USB_EPTYPE_INTR, /* bmAttributes */
BULK_EP_SIZE, /* wMaxPacketSize (LSB) */
0, /* wMaxPacketSize (MSB) */
0xFF, /* bInterval */
/*** Data Class Interface descriptor (interface no. 1) ***/
USB_INTERFACE_DESCSIZE, /* bLength */
USB_INTERFACE_DESCRIPTOR,/* bDescriptorType */
1, /* bInterfaceNumber */
0, /* bAlternateSetting */
2, /* bNumEndpoints */
USB_CLASS_CDC_DATA, /* bInterfaceClass */
0, /* bInterfaceSubClass */ 0, /* bInterfaceSubClass */
0, /* bInterfaceProtocol */ 0, /* bInterfaceProtocol */
0, /* iInterface */ 0, /* iInterface */
/*** CDC Data interface endpoint descriptors ***/
USB_ENDPOINT_DESCSIZE, /* bLength */
USB_ENDPOINT_DESCRIPTOR,/* bDescriptorType */
EP_DATA_IN, /* bEndpointAddress (IN) */
USB_EPTYPE_BULK, /* bmAttributes */
BULK_EP_SIZE, /* wMaxPacketSize (LSB) */
0, /* wMaxPacketSize (MSB) */
0, /* bInterval */
USB_ENDPOINT_DESCSIZE, /* bLength */
USB_ENDPOINT_DESCRIPTOR,/* bDescriptorType */
EP_DATA_OUT, /* bEndpointAddress (OUT)*/
USB_EPTYPE_BULK, /* bmAttributes */
BULK_EP_SIZE, /* wMaxPacketSize (LSB) */
0, /* wMaxPacketSize (MSB) */
0 /* bInterval */
}; };
STATIC_CONST_STRING_DESC_LANGID( langID, 0x04, 0x09 ); STATIC_CONST_STRING_DESC_LANGID( langID, 0x04, 0x09 );
...@@ -149,26 +75,29 @@ STATIC_CONST_STRING_DESC( iManufacturer, 'S','i','l','i','c','o','n',' ','L', \ ...@@ -149,26 +75,29 @@ STATIC_CONST_STRING_DESC( iManufacturer, 'S','i','l','i','c','o','n',' ','L', \
'a','b','o','r','a','t','o','r','i', \ 'a','b','o','r','a','t','o','r','i', \
'e','s',' ','I','n','c','.' ); 'e','s',' ','I','n','c','.' );
STATIC_CONST_STRING_DESC( iProduct , 'E','F','M','3','2',' ','U','S','B', \ STATIC_CONST_STRING_DESC( iProduct , 'E','F','M','3','2',' ','U','S','B', \
' ','C','D','C',' ','s','e','r','i', \ ' ','V','e','n','d','o','r',' ', \
'a','l',' ','p','o','r','t',' ','d', \ 'U','n','i','q','u','e',' ', \
'e','v','i','c','e' ); 'L','E','D',' ', \
'D','e','v','i','c','e' );
STATIC_CONST_STRING_DESC( iSerialNumber, '0','0','0','0','0','0', \
'0','0','1','2','3','4' );
static const void * const strings[] = static const void * const strings[] =
{ {
&langID, &langID,
&iManufacturer, &iManufacturer,
&iProduct, &iProduct,
&iSerialNumber
}; };
/* Endpoint buffer sizes */ /* Endpoint buffer sizes */
/* 1 = single buffer, 2 = double buffering, 3 = triple buffering ... */ /* 1 = single buffer, 2 = double buffering, 3 = tripple buffering ... */
/* Use double buffering on the BULK endpoints. */ static const uint8_t bufferingMultiplier[ NUM_EP_USED + 1 ] = { 1 };
static const uint8_t bufferingMultiplier[ NUM_EP_USED + 1 ] = { 1, 1, 2, 2 };
static const USBD_Callbacks_TypeDef callbacks = static const USBD_Callbacks_TypeDef callbacks =
{ {
.usbReset = NULL, .usbReset = NULL,
.usbStateChange = NULL, //StateChange, .usbStateChange = NULL,
.setupCmd = SetupCmd, .setupCmd = SetupCmd,
.isSelfPowered = NULL, .isSelfPowered = NULL,
.sofInt = NULL .sofInt = NULL
......
...@@ -488,16 +488,17 @@ static void StateChange(USBD_State_TypeDef oldState, ...@@ -488,16 +488,17 @@ static void StateChange(USBD_State_TypeDef oldState,
*****************************************************************************/ *****************************************************************************/
static int SetupCmd(const USB_Setup_TypeDef *setup) static int SetupCmd(const USB_Setup_TypeDef *setup)
{ {
int retVal = USB_STATUS_REQ_UNHANDLED; int r = USB_STATUS_REQ_UNHANDLED;
if ((setup->Type == USB_SETUP_TYPE_CLASS) && if (setup->Type == USB_SETUP_TYPE_VENDOR) //&&
(setup->Recipient == USB_SETUP_RECIPIENT_INTERFACE)) //(setup->Recipient == USB_SETUP_RECIPIENT_DEVICE))
{ {
switch (setup->bRequest) switch (setup->bRequest)
{ {
case LED_SET: case LED_SET:
setup->wValue ? GPIO_PinOutSet(gpioPortE, setup->wIndex) : setup->wValue ? GPIO_PinOutSet(gpioPortE, setup->wIndex) :
GPIO_PinOutClear(gpioPortE, setup->wIndex); GPIO_PinOutClear(gpioPortE, setup->wIndex);
r = USB_STATUS_OK;
break; break;
// case USB_CDC_GETLINECODING: // case USB_CDC_GETLINECODING:
// /********************/ // /********************/
...@@ -508,7 +509,7 @@ static int SetupCmd(const USB_Setup_TypeDef *setup) ...@@ -508,7 +509,7 @@ static int SetupCmd(const USB_Setup_TypeDef *setup)
// { // {
// /* Send current settings to USB host. */ // /* Send current settings to USB host. */
// USBD_Write(0, (void*) &cdcLineCoding, 7, NULL); // USBD_Write(0, (void*) &cdcLineCoding, 7, NULL);
// retVal = USB_STATUS_OK; // r = USB_STATUS_OK;
// } // }
// break; // break;
// //
...@@ -521,7 +522,7 @@ static int SetupCmd(const USB_Setup_TypeDef *setup) ...@@ -521,7 +522,7 @@ static int SetupCmd(const USB_Setup_TypeDef *setup)
// { // {
// /* Get new settings from USB host. */ // /* Get new settings from USB host. */
// USBD_Read(0, (void*) &cdcLineCoding, 7, LineCodingReceived); // USBD_Read(0, (void*) &cdcLineCoding, 7, LineCodingReceived);
// retVal = USB_STATUS_OK; // r = USB_STATUS_OK;
// } // }
// break; // break;
// //
...@@ -531,13 +532,13 @@ static int SetupCmd(const USB_Setup_TypeDef *setup) ...@@ -531,13 +532,13 @@ static int SetupCmd(const USB_Setup_TypeDef *setup)
// (setup->wLength == 0)) /* No data */ // (setup->wLength == 0)) /* No data */
// { // {
// /* Do nothing ( Non compliant behaviour !! ) */ // /* Do nothing ( Non compliant behaviour !! ) */
// retVal = USB_STATUS_OK; // r = USB_STATUS_OK;
// } // }
// break; // break;
} }
} }
return retVal; return r;
} }
/**************************************************************************//** /**************************************************************************//**
......
...@@ -27,14 +27,13 @@ extern "C" { ...@@ -27,14 +27,13 @@ extern "C" {
** Specify number of endpoints used (in addition to EP0). ** ** Specify number of endpoints used (in addition to EP0). **
** ** ** **
*****************************************************************************/ *****************************************************************************/
#define NUM_EP_USED 3 #define NUM_EP_USED 0
/**************************************************************************** /****************************************************************************
** ** ** **
** Specify number of application timers you need. ** ** Configure serial port debug output. **
** ** ** **
*****************************************************************************/ *****************************************************************************/
#define NUM_APP_TIMERS 1
#ifdef __cplusplus #ifdef __cplusplus
} }
......
CC = gcc CC = gcc
CFLAGS = -Wall CFLAGS = -Wall
INCLUDE = INCLUDE = -I/usr/include/libusb-1.0
SOURCES = usbled.c SOURCES = usbled.c
LIB = LIB =
LDFLAGS = -lusb LDFLAGS = -lusb-1.0
all: all:
$(CC) -o usbled $(CFLAGS) $(INCLUDE) $(SOURCES) $(LIB) $(LDFLAGS) $(CC) -o usbled $(CFLAGS) $(INCLUDE) $(SOURCES) $(LIB) $(LDFLAGS)
......
#include <stdio.h> #include <stdio.h>
#include <usb.h> #include <unistd.h>
#include <libusb.h>
#define LED_SET 0x11 #define LED_SET 0x11
int main() int main()
{ {
static usb_dev_handle *pDevH = NULL; // USB device handle int i = 0;
static struct usb_device *pDev; int led = 12;
static struct usb_bus *pBus; int vals[4] = {0, 1, 1, 0};
int val;
int led = 12; libusb_device **devs, *dev;
int val = 0; libusb_device_handle *devh = NULL;
usb_init(); libusb_init(NULL);
usb_find_busses(); libusb_get_device_list(NULL, &devs);
usb_find_devices();
// Enumerate USB devices ... while ((dev = devs[i++]) != NULL) {
for ( pBus = usb_get_busses(); pBus; pBus = pBus->next) struct libusb_device_descriptor desc;
{ int r = libusb_get_device_descriptor(dev, &desc);
for ( pDev = pBus->devices; pDev; pDev = pDev->next) if (r < 0) {
{ fprintf(stderr, "failed to get device descriptor");
if ( ( pDev->descriptor.idVendor == 0x10C4 ) && break;
( pDev->descriptor.idProduct == 0x00ff ) ) }
{ if ((desc.idVendor == 0x10c4) && (desc.idProduct == 0x00ff)) {
pDevH = usb_open( pDev ); r = libusb_open(dev, &devh);
if ( pDevH ) if (r < 0) {
{ fprintf(stderr, "failed to open device: %d\n", r);
if ( usb_set_configuration( goto cleanup;
pDevH, pDev->config->bConfigurationValue ) != 0 ) }
{ printf("device open\n");
usb_close( pDevH ); r = libusb_claim_interface(devh, 0);
pDevH = NULL; if (r < 0) {
printf("FAIL: usb_set_configuration"); fprintf(stderr, "failed to claim interface: %d\n", r);
} goto cleanup;
}
if ( pDev->descriptor.idProduct == 0x00ff ) printf("interface claimed\n");
{ break;
if ( usb_claim_interface( pDevH, 0 ) != 0 ) }
{
usb_close( pDevH );
pDevH = NULL;
printf("FAIL: usb_claim_interface");
}
}
} }
}
}
}
while (1) { if (devh != NULL) {
for (i = 0; i < 10; i++) {
if (led == 11) led = 12;
else led = 11;
val = vals[i%4];
if (led == 11) led = 12; printf("%d:%d:%d\n", i, led, val);
if (led == 12) led = 11; libusb_control_transfer (
val ^= 1; devh, // Device handle
LIBUSB_ENDPOINT_OUT |
LIBUSB_REQUEST_TYPE_VENDOR |
LIBUSB_RECIPIENT_DEVICE, // bmRequestType
LED_SET, // bRequest
val, // wValue
led, // wIndex
NULL, // char *data
0, // wLength
1000 ); // timeout (ms)
sleep(1);
}
}
if ( pDevH ) cleanup:
{ printf("cleanup\n");
printf("led%d\n", led); if (devh != NULL) {
usb_control_msg( printf("release\n");
pDevH, // Device handle libusb_release_interface(devh, 0);
USB_ENDPOINT_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, libusb_close(devh);
// bmRequestType }
LED_SET, // bRequest printf("free\n");
val, // wValue libusb_free_device_list(devs, 1);
led, // wIndex printf("exit\n");
NULL, // char *pBuffer libusb_exit(NULL);
1, // wLength printf("done\n");
1000 ); // Timeout (ms)
sleep(1);
}
} return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment