123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- #include "bsp/board_api.h"
- #include "tusb.h"
- #include "usb_descriptors.h"
- #define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
- #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
- _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
- #define USB_VID 0xCafe
- #define USB_BCD 0x0200
- tusb_desc_device_t const desc_device =
- {
- .bLength = sizeof(tusb_desc_device_t),
- .bDescriptorType = TUSB_DESC_DEVICE,
- .bcdUSB = USB_BCD,
- .bDeviceClass = 0x00,
- .bDeviceSubClass = 0x00,
- .bDeviceProtocol = 0x00,
- .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
- .idVendor = USB_VID,
- .idProduct = USB_PID,
- .bcdDevice = 0x0100,
- .iManufacturer = 0x01,
- .iProduct = 0x02,
- .iSerialNumber = 0x03,
- .bNumConfigurations = 0x01
- };
- uint8_t const * tud_descriptor_device_cb(void)
- {
- return (uint8_t const *) &desc_device;
- }
- uint8_t const desc_hid_report[] =
- {
- TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD )),
- };
- uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance)
- {
- (void) instance;
- return desc_hid_report;
- }
- enum
- {
- ITF_NUM_HID,
- ITF_NUM_TOTAL
- };
- #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN)
- #define EPNUM_HID 0x81
- uint8_t const desc_configuration[] =
- {
-
- TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
-
- TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 5)
- };
- #if TUD_OPT_HIGH_SPEED
- uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN];
- tusb_desc_device_qualifier_t const desc_device_qualifier =
- {
- .bLength = sizeof(tusb_desc_device_qualifier_t),
- .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
- .bcdUSB = USB_BCD,
- .bDeviceClass = 0x00,
- .bDeviceSubClass = 0x00,
- .bDeviceProtocol = 0x00,
- .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
- .bNumConfigurations = 0x01,
- .bReserved = 0x00
- };
- uint8_t const* tud_descriptor_device_qualifier_cb(void)
- {
- return (uint8_t const*) &desc_device_qualifier;
- }
- uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index)
- {
- (void) index;
-
- memcpy(desc_other_speed_config, desc_configuration, CONFIG_TOTAL_LEN);
- desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
-
- return desc_other_speed_config;
- }
- #endif
- uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
- {
- (void) index;
-
- return desc_configuration;
- }
- enum {
- STRID_LANGID = 0,
- STRID_MANUFACTURER,
- STRID_PRODUCT,
- STRID_SERIAL,
- };
- char const *string_desc_arr[] =
- {
- (const char[]) { 0x09, 0x04 },
- "TinyUSB",
- "TinyUSB Device",
- NULL,
- };
- static uint16_t _desc_str[32 + 1];
- uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
- (void) langid;
- size_t chr_count;
- switch ( index ) {
- case STRID_LANGID:
- memcpy(&_desc_str[1], string_desc_arr[0], 2);
- chr_count = 1;
- break;
- case STRID_SERIAL:
- chr_count = board_usb_get_serial(_desc_str + 1, 32);
- break;
- default:
-
-
- if ( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
- const char *str = string_desc_arr[index];
-
- chr_count = strlen(str);
- size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1;
- if ( chr_count > max_count ) chr_count = max_count;
-
- for ( size_t i = 0; i < chr_count; i++ ) {
- _desc_str[1 + i] = str[i];
- }
- break;
- }
-
- _desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
- return _desc_str;
- }
|