twinkle_stick/source/usbcfg.c

344 lines
18 KiB
C

#include "hal.h"
#include "hal_usb.h"
/*
* USB Device Descriptor.
*/
static const uint8_t device_descriptor_data[18] = {
USB_DESC_DEVICE (0x0200, /* bcdUSB (2.0). */
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. */
0x00, /* 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 (0x09), /* bLength. */
USB_DESC_BYTE (0x21), /* bDescriptorType (HID). */
USB_DESC_BCD (0x111), /* bcdHID(1.1) */
USB_DESC_BYTE (0x00), /* bCountryCode(None) */
USB_DESC_BYTE (0x01), /* bNumDescriptors */
USB_DESC_BYTE (0x22), /* bDescriptorType */
USB_DESC_WORD (292), /* wDescriptorLength */
/* Endpoint Descriptors */
USB_DESC_ENDPOINT (0x81, /* bEndpointAddress */
0x03, /* bmAttributes */
0x0020, /* wMaxPacketSize. */
0xFF), /* bInterval. */
USB_DESC_ENDPOINT (0x01, /* bEndpointAddress */
0x03, /* bmAttributes */
0x0020, /* wMaxPacketSize. */
0xFF), /* bInterval. */
};
static const USBDescriptor configuration_descriptor = {
sizeof configuration_descriptor_data,
configuration_descriptor_data
};
static const uint8_t hid_report_descriptor_data[292] = {
0x05, 0x59, // UsagePage(Lighting And Illumination[0x0059])
0x09, 0x01, // UsageId(LampArray[0x0001])
0xA1, 0x01, // Collection(Application)
0x85, 0x01, // ReportId(1)
0x09, 0x02, // UsageId(LampArrayAttributesReport[0x0002])
0xA1, 0x02, // Collection(Logical)
0x09, 0x03, // UsageId(LampCount[0x0003])
0x15, 0x00, // LogicalMinimum(0)
0x27, 0xFF, 0xFF, 0x00, 0x00, // LogicalMaximum(65,535)
0x95, 0x01, // ReportCount(1)
0x75, 0x10, // ReportSize(16)
0xB1, 0x03, // Feature(Constant, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0x09, 0x04, // UsageId(BoundingBoxWidthInMicrometers[0x0004])
0x09, 0x05, // UsageId(BoundingBoxHeightInMicrometers[0x0005])
0x09, 0x06, // UsageId(BoundingBoxDepthInMicrometers[0x0006])
0x09, 0x07, // UsageId(LampArrayKind[0x0007])
0x09, 0x08, // UsageId(MinUpdateIntervalInMicroseconds[0x0008])
0x27, 0xFF, 0xFF, 0xFF, 0x7F, // LogicalMaximum(2,147,483,647)
0x95, 0x05, // ReportCount(5)
0x75, 0x20, // ReportSize(32)
0xB1, 0x03, // Feature(Constant, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0xC0, // EndCollection()
0x85, 0x02, // ReportId(2)
0x09, 0x20, // UsageId(LampAttributesRequestReport[0x0020])
0xA1, 0x02, // Collection(Logical)
0x09, 0x21, // UsageId(LampId[0x0021])
0x27, 0xFF, 0xFF, 0x00, 0x00, // LogicalMaximum(65,535)
0x95, 0x01, // ReportCount(1)
0x75, 0x10, // ReportSize(16)
0xB1, 0x02, // Feature(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0xC0, // EndCollection()
0x85, 0x03, // ReportId(3)
0x09, 0x22, // UsageId(LampAttributesResponseReport[0x0022])
0xA1, 0x02, // Collection(Logical)
0x09, 0x21, // UsageId(LampId[0x0021])
0xB1, 0x02, // Feature(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0x09, 0x23, // UsageId(PositionXInMicrometers[0x0023])
0x09, 0x24, // UsageId(PositionYInMicrometers[0x0024])
0x09, 0x25, // UsageId(PositionZInMicrometers[0x0025])
0x09, 0x27, // UsageId(UpdateLatencyInMicroseconds[0x0027])
0x09, 0x26, // UsageId(LampPurposes[0x0026])
0x27, 0xFF, 0xFF, 0xFF, 0x7F, // LogicalMaximum(2,147,483,647)
0x95, 0x05, // ReportCount(5)
0x75, 0x20, // ReportSize(32)
0xB1, 0x02, // Feature(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0x09, 0x28, // UsageId(RedLevelCount[0x0028])
0x09, 0x29, // UsageId(GreenLevelCount[0x0029])
0x09, 0x2A, // UsageId(BlueLevelCount[0x002A])
0x09, 0x2B, // UsageId(IntensityLevelCount[0x002B])
0x09, 0x2C, // UsageId(IsProgrammable[0x002C])
0x09, 0x2D, // UsageId(InputBinding[0x002D])
0x26, 0xFF, 0x00, // LogicalMaximum(255)
0x95, 0x06, // ReportCount(6)
0x75, 0x08, // ReportSize(8)
0xB1, 0x02, // Feature(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0xC0, // EndCollection()
0x85, 0x04, // ReportId(4)
0x09, 0x50, // UsageId(LampMultiUpdateReport[0x0050])
0xA1, 0x02, // Collection(Logical)
0x09, 0x03, // UsageId(LampCount[0x0003])
0x25, 0x08, // LogicalMaximum(8)
0x95, 0x01, // ReportCount(1)
0xB1, 0x02, // Feature(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0x09, 0x55, // UsageId(LampUpdateFlags[0x0055])
0x25, 0x01, // LogicalMaximum(1)
0xB1, 0x02, // Feature(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0x09, 0x21, // UsageId(LampId[0x0021])
0x27, 0xFF, 0xFF, 0x00, 0x00, // LogicalMaximum(65,535)
0x95, 0x08, // ReportCount(8)
0x75, 0x10, // ReportSize(16)
0xB1, 0x02, // Feature(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0x09, 0x51, // UsageId(RedUpdateChannel[0x0051])
0x09, 0x52, // UsageId(GreenUpdateChannel[0x0052])
0x09, 0x53, // UsageId(BlueUpdateChannel[0x0053])
0x09, 0x54, // UsageId(IntensityUpdateChannel[0x0054])
0x09, 0x51, // UsageId(RedUpdateChannel[0x0051])
0x09, 0x52, // UsageId(GreenUpdateChannel[0x0052])
0x09, 0x53, // UsageId(BlueUpdateChannel[0x0053])
0x09, 0x54, // UsageId(IntensityUpdateChannel[0x0054])
0x09, 0x51, // UsageId(RedUpdateChannel[0x0051])
0x09, 0x52, // UsageId(GreenUpdateChannel[0x0052])
0x09, 0x53, // UsageId(BlueUpdateChannel[0x0053])
0x09, 0x54, // UsageId(IntensityUpdateChannel[0x0054])
0x09, 0x51, // UsageId(RedUpdateChannel[0x0051])
0x09, 0x52, // UsageId(GreenUpdateChannel[0x0052])
0x09, 0x53, // UsageId(BlueUpdateChannel[0x0053])
0x09, 0x54, // UsageId(IntensityUpdateChannel[0x0054])
0x09, 0x51, // UsageId(RedUpdateChannel[0x0051])
0x09, 0x52, // UsageId(GreenUpdateChannel[0x0052])
0x09, 0x53, // UsageId(BlueUpdateChannel[0x0053])
0x09, 0x54, // UsageId(IntensityUpdateChannel[0x0054])
0x09, 0x51, // UsageId(RedUpdateChannel[0x0051])
0x09, 0x52, // UsageId(GreenUpdateChannel[0x0052])
0x09, 0x53, // UsageId(BlueUpdateChannel[0x0053])
0x09, 0x54, // UsageId(IntensityUpdateChannel[0x0054])
0x09, 0x51, // UsageId(RedUpdateChannel[0x0051])
0x09, 0x52, // UsageId(GreenUpdateChannel[0x0052])
0x09, 0x53, // UsageId(BlueUpdateChannel[0x0053])
0x09, 0x54, // UsageId(IntensityUpdateChannel[0x0054])
0x09, 0x51, // UsageId(RedUpdateChannel[0x0051])
0x09, 0x52, // UsageId(GreenUpdateChannel[0x0052])
0x09, 0x53, // UsageId(BlueUpdateChannel[0x0053])
0x09, 0x54, // UsageId(IntensityUpdateChannel[0x0054])
0x26, 0xFF, 0x00, // LogicalMaximum(255)
0x95, 0x20, // ReportCount(32)
0x75, 0x08, // ReportSize(8)
0xB1, 0x02, // Feature(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0xC0, // EndCollection()
0x85, 0x05, // ReportId(5)
0x09, 0x60, // UsageId(LampRangeUpdateReport[0x0060])
0xA1, 0x02, // Collection(Logical)
0x09, 0x55, // UsageId(LampUpdateFlags[0x0055])
0x25, 0x01, // LogicalMaximum(1)
0x95, 0x01, // ReportCount(1)
0xB1, 0x02, // Feature(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0x09, 0x61, // UsageId(LampIdStart[0x0061])
0x09, 0x62, // UsageId(LampIdEnd[0x0062])
0x27, 0xFF, 0xFF, 0x00, 0x00, // LogicalMaximum(65,535)
0x95, 0x02, // ReportCount(2)
0x75, 0x10, // ReportSize(16)
0xB1, 0x02, // Feature(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0x09, 0x51, // UsageId(RedUpdateChannel[0x0051])
0x09, 0x52, // UsageId(GreenUpdateChannel[0x0052])
0x09, 0x53, // UsageId(BlueUpdateChannel[0x0053])
0x09, 0x54, // UsageId(IntensityUpdateChannel[0x0054])
0x26, 0xFF, 0x00, // LogicalMaximum(255)
0x95, 0x04, // ReportCount(4)
0x75, 0x08, // ReportSize(8)
0xB1, 0x02, // Feature(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0xC0, // EndCollection()
0x85, 0x06, // ReportId(6)
0x09, 0x70, // UsageId(LampArrayControlReport[0x0070])
0xA1, 0x02, // Collection(Logical)
0x09, 0x71, // UsageId(AutonomousMode[0x0071])
0x25, 0x01, // LogicalMaximum(1)
0x95, 0x01, // ReportCount(1)
0xB1, 0x02, // Feature(Data, Variable, Absolute, NoWrap, Linear, PreferredState, NoNullPosition, NonVolatile, BitField)
0xC0, // EndCollection()
0xC0, // EndCollection()
};
static const USBDescriptor hid_report_descriptor = {
sizeof hid_report_descriptor_data,
hid_report_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];
case 0x22:
return &hid_report_descriptor;
}
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, 0x01, &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
};