Skip to content
systemcoding.dev
Embedded

Define a USB device descriptor in tud_descriptor_device_cb

Return a TinyUSB device descriptor from tud_descriptor_device_cb with a static tusb_desc_device_t, using the IAD class codes a composite CDC device needs.

1 min read
Summarize in AI

TinyUSB asks your firmware for the device descriptor when the host enumerates it, by calling tud_descriptor_device_cb. You return a pointer to a tusb_desc_device_t, and that memory has to outlive the call.

c
#include "tusb.h"

static tusb_desc_device_t const desc_device = {
    .bLength            = sizeof(tusb_desc_device_t),
    .bDescriptorType    = TUSB_DESC_DEVICE,
    .bcdUSB             = 0x0200,
    .bDeviceClass       = TUSB_CLASS_MISC,
    .bDeviceSubClass    = MISC_SUBCLASS_COMMON,
    .bDeviceProtocol    = MISC_PROTOCOL_IAD,
    .bMaxPacketSize0    = CFG_TUD_ENDPOINT0_SIZE,
    .idVendor           = 0xCafe,
    .idProduct          = 0x4001,
    .bcdDevice          = 0x0100,
    .iManufacturer      = 0x01,
    .iProduct           = 0x02,
    .iSerialNumber      = 0x03,
    .bNumConfigurations = 0x01,
};

uint8_t const *tud_descriptor_device_cb(void)
{
    return (uint8_t const *)&desc_device;
}

On Linux the device shows up in lsusb with the IDs from the descriptor:

Text
Bus 001 Device 012: ID cafe:4001 TinyUSB Example

The class triple 0xEF, 0x02, 0x01 (TUSB_CLASS_MISC, common subclass, IAD protocol) tells the host that interfaces are grouped by interface association descriptors. A CDC serial port is two interfaces, and without this triple Windows may load the wrong driver for it. A single-interface device can use class 0.

0xCafe is the vendor ID of the TinyUSB examples, not one you may ship with. Use your own vendor ID or a product ID from a free allocation such as pid.codes. The code follows TinyUSB 0.18.

Taggedc
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.