mirror of
https://github.com/justinian/edmfd_firmware.git
synced 2025-12-09 16:24:31 -08:00
Initial version, HID and vendor echo working
This commit is contained in:
154
src/usb_descriptors.cc
Normal file
154
src/usb_descriptors.cc
Normal file
@@ -0,0 +1,154 @@
|
||||
#include "bsp/board_api.h"
|
||||
#include "tusb.h"
|
||||
#include "usb_descriptors.hh"
|
||||
|
||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
|
||||
*
|
||||
* Auto ProductID layout's Bitmap:
|
||||
* [MSB] HID | MSC | CDC [LSB]
|
||||
*/
|
||||
#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
|
||||
#define USB_PID (0x0000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
|
||||
_PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
|
||||
|
||||
#define USB_VID 0x4a33 // J3 in ascii
|
||||
#define USB_BCD 0x0200
|
||||
|
||||
extern "C" {
|
||||
// TinyUSB callbacks
|
||||
uint8_t const * tud_descriptor_device_cb();
|
||||
uint8_t const * tud_hid_descriptor_report_cb(uint8_t);
|
||||
uint8_t const * tud_descriptor_configuration_cb(uint8_t);
|
||||
uint16_t const * tud_descriptor_string_cb(uint8_t, uint16_t);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Device Descriptors
|
||||
//--------------------------------------------------------------------+
|
||||
tusb_desc_device_t const desc_device = {
|
||||
.bLength = sizeof(tusb_desc_device_t),
|
||||
.bDescriptorType = TUSB_DESC_DEVICE,
|
||||
.bcdUSB = USB_BCD,
|
||||
.bDeviceClass = TUSB_CLASS_MISC,
|
||||
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
|
||||
.bDeviceProtocol = MISC_PROTOCOL_IAD,
|
||||
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
|
||||
|
||||
.idVendor = USB_VID,
|
||||
.idProduct = USB_PID,
|
||||
.bcdDevice = 0x0100,
|
||||
|
||||
.iManufacturer = 0x01,
|
||||
.iProduct = 0x02,
|
||||
.iSerialNumber = 0x03,
|
||||
|
||||
.bNumConfigurations = 0x01
|
||||
};
|
||||
|
||||
// Invoked when received GET DEVICE DESCRIPTOR
|
||||
// Application return pointer to descriptor
|
||||
uint8_t const * tud_descriptor_device_cb(void) {
|
||||
return (uint8_t const *) &desc_device;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// HID Report Descriptor
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
uint8_t const desc_hid_report[] = {
|
||||
TUD_HID_REPORT_DESC_GAMEPAD ( HID_REPORT_ID(static_cast<uint8_t>(descriptor::gamepad)) )
|
||||
};
|
||||
|
||||
// Invoked when received GET HID REPORT DESCRIPTOR
|
||||
// Application return pointer to descriptor
|
||||
// Descriptor contents must exist long enough for transfer to complete
|
||||
uint8_t const * tud_hid_descriptor_report_cb(uint8_t) {
|
||||
return desc_hid_report;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// String Descriptors
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
enum class strings : uint8_t {
|
||||
language_id,
|
||||
manufacturer,
|
||||
product,
|
||||
serial,
|
||||
interface,
|
||||
_count
|
||||
};
|
||||
|
||||
// array of pointer to string descriptors
|
||||
char16_t const *string_desc_arr[size_t(strings::_count)] = {
|
||||
u"\u0409", // 0: is supported language is English (0x0409)
|
||||
u"j3gaming", // 1: Manufacturer
|
||||
u"EDMFD", // 2: Product
|
||||
nullptr, // 3: Serials will use unique ID if possible
|
||||
u"screen control", // 4: screen interface
|
||||
};
|
||||
|
||||
static constexpr size_t desc_len = 32;
|
||||
static uint16_t desc_str[desc_len + 1];
|
||||
|
||||
// Invoked when received GET STRING DESCRIPTOR request
|
||||
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
|
||||
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||
(void) langid;
|
||||
size_t chr_count = 0;
|
||||
strings string_id = static_cast<strings>(index);
|
||||
|
||||
switch (string_id) {
|
||||
case strings::serial:
|
||||
chr_count = board_usb_get_serial(desc_str + 1, desc_len);
|
||||
break;
|
||||
|
||||
default:
|
||||
if (string_id >= strings::_count) return nullptr;
|
||||
uint16_t const *src = reinterpret_cast<uint16_t const*>(string_desc_arr[index]);
|
||||
uint16_t *dest = desc_str + 1;
|
||||
while (*src) {
|
||||
*dest++ = *src++;
|
||||
++chr_count;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// first byte is length (including header), second byte is string type
|
||||
desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||
return desc_str;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Configuration Descriptor
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
static constexpr size_t config_total_len = TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN + TUD_VENDOR_DESC_LEN;
|
||||
enum class interface : uint8_t { HID, vendor, _count };
|
||||
enum class endpoint : uint8_t { HID = 0x81, vendor_out = 0x02, vendor_in = 0x82 };
|
||||
|
||||
uint8_t const desc_configuration[config_total_len] = {
|
||||
// Config number, interface count, string index, total length, attribute, power in mA
|
||||
TUD_CONFIG_DESCRIPTOR(1, uint8_t(interface::_count), 0, config_total_len, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
|
||||
|
||||
// Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval
|
||||
TUD_HID_DESCRIPTOR(uint8_t(interface::HID), 0, HID_ITF_PROTOCOL_NONE, sizeof(desc_hid_report), uint8_t(endpoint::HID), CFG_TUD_HID_EP_BUFSIZE, 5),
|
||||
|
||||
// Interface number, string index, EP Out & IN address, EP size
|
||||
TUD_VENDOR_DESCRIPTOR(uint8_t(interface::vendor), uint8_t(strings::interface), uint8_t(endpoint::vendor_out), uint8_t(endpoint::vendor_in), 64)
|
||||
|
||||
// CDC: Interface number, string index, EP notification address and size, EP data address (out, in) and size.
|
||||
//TUD_CDC_DESCRIPTOR(uint8_t(interface::CDC), 4, uint8_t(endpoint::CDCnotify), 8, uint8_t(endpoint::CDCout), uint8_t(endpoint::CDCin), 64)
|
||||
};
|
||||
|
||||
|
||||
// Invoked when received GET CONFIGURATION DESCRIPTOR
|
||||
// Application return pointer to descriptor
|
||||
// Descriptor contents must exist long enough for transfer to complete
|
||||
uint8_t const * tud_descriptor_configuration_cb(uint8_t index) {
|
||||
(void) index; // for multiple configurations
|
||||
|
||||
// This example use the same configuration for both high and full speed mode
|
||||
return desc_configuration;
|
||||
}
|
||||
Reference in New Issue
Block a user