twinkle_stick/source/usbcfg.c

198 lines
6.6 KiB
C

#include "hal.h"
/*
* USB Device Descriptor.
*/
static const uint8_t device_descriptor_data[18] = {
USB_DESC_DEVICE (0x0110, /* bcdUSB (1.1). */
0x00, /* bDeviceClass (Unknown). */
0x00, /* bDeviceSubClass. */
0x00, /* bDeviceProtocol. */
0x40, /* bMaxPacketSize. */
0x0483, /* idVendor (ST). */
0x5740, /* idProduct. */
0x0200, /* bcdDevice. */
1, /* iManufacturer. */
2, /* iProduct. */
3, /* iSerialNumber. */
1) /* bNumConfigurations. */
};
/*
* Device Descriptor wrapper.
*/
static const USBDescriptor device_descriptor = {
sizeof device_descriptor_data,
device_descriptor_data
};
/* Configuration Descriptor tree for an HID Lighting Array.*/
static const uint8_t configuration_descriptor_data[41] = {
/* Configuration Descriptor.*/
USB_DESC_CONFIGURATION(41, /* wTotalLength. */
0x01, /* bNumInterfaces. */
0x01, /* bConfigurationValue. */
0, /* iConfiguration. */
0xC0, /* bmAttributes (self powered). */
50), /* bMaxPower (100mA). */
/* Interface Descriptor.*/
USB_DESC_INTERFACE (0x00, /* bInterfaceNumber. */
0x00, /* bAlternateSetting. */
0x02, /* bNumEndpoints. */
0x03, /* bInterfaceClass (HID). */
0x00, /* bInterfaceSubClass */
0x00, /* bInterfaceProtocol */
0), /* iInterface. */
/* HID Descriptor (HID Section 6.2.1).*/
USB_DESC_BYTE (6), /* bLength. */
USB_DESC_BYTE (0x21), /* bDescriptorType (HID). */
USB_DESC_BCD (0x110), /* bcdHID(1.1) */
USB_DESC_BYTE (0x00), /* bCountryCode(None) */
USB_DESC_BYTE (0x01), /* bNumDescriptors */
USB_DESC_BYTE (0x34), /* bDescriptorType */
USB_DESC_WORD (0x00), /* wDescriptorLength */
/* HID Report Descriptors */
/* Endpoint Descriptors */
USB_DESC_ENDPOINT (0x81, /* bEndpointAddress */
0x03, /* bmAttributes */
0x0020, /* wMaxPacketSize. */
0xFF), /* bInterval. */
USB_DESC_ENDPOINT (0x01, /* bEndpointAddress */
0x03, /* bmAttributes */
0x0020, /* wMaxPacketSize. */
0xFF), /* bInterval. */
};
/*
* Configuration Descriptor wrapper.
*/
static const USBDescriptor configuration_descriptor = {
sizeof configuration_descriptor_data,
configuration_descriptor_data
};
/*
* U.S. English language identifier.
*/
static const uint8_t usb_string0[] = {
USB_DESC_BYTE(4), /* bLength. */
USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
USB_DESC_WORD(0x0409) /* wLANGID (U.S. English). */
};
/*
* Vendor string.
*/
static const uint8_t usb_string1[] = {
USB_DESC_BYTE(16), /* bLength. */
USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
'M', 0, 'e', 0, 't', 0, 'z', 0, 'N', 0, 'e', 0, 't', 0
};
/*
* Device Description string.
*/
static const uint8_t usb_string2[] = {
USB_DESC_BYTE(30), /* bLength. */
USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
'T', 0, 'w', 0, 'i', 0, 'n', 0, 'k', 0, 'l', 0, 'e', 0,
'S', 0, 't', 0, 'i', 0, 'c', 0, 'k', 0, 'V', 0, '2', 0
};
/*
* Serial Number string.
*/
static const uint8_t usb_string3[] = {
USB_DESC_BYTE(8), /* bLength. */
USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
'0' + CH_KERNEL_MAJOR, 0,
'0' + CH_KERNEL_MINOR, 0,
'0' + CH_KERNEL_PATCH, 0
};
/*
* Strings wrappers array.
*/
static const USBDescriptor usb_strings[] = {
{sizeof usb_string0, usb_string0},
{sizeof usb_string1, usb_string1},
{sizeof usb_string2, usb_string2},
{sizeof usb_string3, usb_string3}
};
static const USBDescriptor *get_descriptor(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t lang) {
(void)usbp;
(void)lang;
switch (dtype) {
case USB_DESCRIPTOR_DEVICE:
return &device_descriptor;
case USB_DESCRIPTOR_CONFIGURATION:
return &configuration_descriptor;
case USB_DESCRIPTOR_STRING:
if (dindex < 4)
return &usb_strings[dindex];
}
return NULL;
}
static USBInEndpointState ep1instate;
static USBOutEndpointState ep1outstate;
static const USBEndpointConfig ep1config = {
USB_EP_MODE_TYPE_INTR,
NULL,
NULL,
NULL,
0x0020,
0x0020,
&ep1instate,
&ep1outstate,
1,
NULL
};
static bool usb_request(USBDriver *usbp) {
(void)usbp;
return false;
}
static void usb_event(USBDriver *usbp, usbevent_t event) {
switch (event) {
case USB_EVENT_ADDRESS:
return;
case USB_EVENT_CONFIGURED:
chSysLockFromISR();
usbInitEndpointI(usbp, 0x02, &ep1config);
chSysUnlockFromISR();
return;
case USB_EVENT_RESET:
/* Falls into.*/
case USB_EVENT_UNCONFIGURED:
/* Falls into.*/
case USB_EVENT_SUSPEND:
return;
case USB_EVENT_WAKEUP:
return;
case USB_EVENT_STALLED:
return;
}
return;
}
static void sof_handler(USBDriver *usbp) {
(void)usbp;
}
const USBConfig usbcfg = {
usb_event,
get_descriptor,
usb_request,
sof_handler
};