ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
usb_descriptors.c
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include "usb_descriptors.h"
8#include "sdkconfig.h"
9
10/*
11 * A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
12 * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
13 *
14 * Auto ProductID layout's Bitmap:
15 * [MSB] HID | MSC | CDC [LSB]
16 */
17#define USB_TUSB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
18 _PID_MAP(MIDI, 3) ) //| _PID_MAP(AUDIO, 4) | _PID_MAP(VENDOR, 5) )
19
20/**** TinyUSB default ****/
21tusb_desc_device_t descriptor_tinyusb = {
22 .bLength = sizeof(descriptor_tinyusb),
23 .bDescriptorType = TUSB_DESC_DEVICE,
24 .bcdUSB = 0x0200,
25
27 // Use Interface Association Descriptor (IAD) for CDC
28 // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
29 .bDeviceClass = TUSB_CLASS_MISC,
30 .bDeviceSubClass = MISC_SUBCLASS_COMMON,
31 .bDeviceProtocol = MISC_PROTOCOL_IAD,
32#else
33 .bDeviceClass = 0x00,
34 .bDeviceSubClass = 0x00,
35 .bDeviceProtocol = 0x00,
36#endif
37
38 .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
39
40 .idVendor = 0xCafe,
41 .idProduct = USB_TUSB_PID,
42 .bcdDevice = 0x0100,
43
44 .iManufacturer = 0x01,
45 .iProduct = 0x02,
46 .iSerialNumber = 0x03,
47
48 .bNumConfigurations = 0x01
49};
50
52 // array of pointer to string descriptors
53 (char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
54 "TinyUSB", // 1: Manufacturer
55 "TinyUSB Device", // 2: Product
56 "123456", // 3: Serials, should use chip ID
57};
58/* End of TinyUSB default */
59
60/**** Kconfig driven Descriptor ****/
61const tusb_desc_device_t descriptor_dev_kconfig = {
62 .bLength = sizeof(descriptor_dev_kconfig),
63 .bDescriptorType = TUSB_DESC_DEVICE,
64 .bcdUSB = 0x0200,
65
67 // Use Interface Association Descriptor (IAD) for CDC
68 // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
69 .bDeviceClass = TUSB_CLASS_MISC,
70 .bDeviceSubClass = MISC_SUBCLASS_COMMON,
71 .bDeviceProtocol = MISC_PROTOCOL_IAD,
72#else
73 .bDeviceClass = 0x00,
74 .bDeviceSubClass = 0x00,
75 .bDeviceProtocol = 0x00,
76#endif
77
78 .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
79
80#if CONFIG_TINYUSB_DESC_USE_ESPRESSIF_VID
81 .idVendor = USB_ESPRESSIF_VID,
82#else
83 .idVendor = CONFIG_TINYUSB_DESC_CUSTOM_VID,
84#endif
85
86#if CONFIG_TINYUSB_DESC_USE_DEFAULT_PID
87 .idProduct = USB_TUSB_PID,
88#else
89 .idProduct = CONFIG_TINYUSB_DESC_CUSTOM_PID,
90#endif
91
92 .bcdDevice = CONFIG_TINYUSB_DESC_BCD_DEVICE,
93
94 .iManufacturer = 0x01,
95 .iProduct = 0x02,
96 .iSerialNumber = 0x03,
97
98 .bNumConfigurations = 0x01
99};
100
102 // array of pointer to string descriptors
103 (char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
104 CONFIG_TINYUSB_DESC_MANUFACTURER_STRING, // 1: Manufacturer
105 CONFIG_TINYUSB_DESC_PRODUCT_STRING, // 2: Product
106 CONFIG_TINYUSB_DESC_SERIAL_STRING, // 3: Serials, should use chip ID
107
108#if CONFIG_TINYUSB_CDC_ENABLED
109 CONFIG_TINYUSB_DESC_CDC_STRING, // 4: CDC Interface
110#else
111 "",
112#endif
113
114#if CONFIG_TINYUSB_MSC_ENABLED
115 CONFIG_TINYUSB_DESC_MSC_STRING, // 5: MSC Interface
116#else
117 "",
118#endif
119
120};
121
122//------------- Configuration Descriptor -------------//
123enum {
124#if CFG_TUD_CDC
125 ITF_NUM_CDC = 0,
126 ITF_NUM_CDC_DATA,
127#endif
128
129#if CFG_TUD_CDC > 1
130 ITF_NUM_CDC1,
131 ITF_NUM_CDC1_DATA,
132#endif
133
134#if CFG_TUD_MSC
135 ITF_NUM_MSC,
136#endif
137
139};
140
141enum {
142 TUSB_DESC_TOTAL_LEN = TUD_CONFIG_DESC_LEN +
143 CFG_TUD_CDC * TUD_CDC_DESC_LEN +
144 CFG_TUD_MSC * TUD_MSC_DESC_LEN
145};
146
147//------------- USB Endpoint numbers -------------//
148enum {
149 // Available USB Endpoints: 5 IN/OUT EPs and 1 IN EP
151#if CFG_TUD_CDC
152 EPNUM_0_CDC_NOTIF,
153 EPNUM_0_CDC,
154#endif
155
156#if CFG_TUD_CDC > 1
157 EPNUM_1_CDC_NOTIF,
158 EPNUM_1_CDC,
159#endif
160
161#if CFG_TUD_MSC
162 EPNUM_MSC,
163#endif
164};
165
166uint8_t const descriptor_cfg_kconfig[] = {
167 // Configuration number, interface count, string index, total length, attribute, power in mA
168 TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, TUSB_DESC_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
169
170#if CFG_TUD_CDC
171 // Interface number, string index, EP notification address and size, EP data address (out, in) and size.
172 TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, 0x80 | EPNUM_0_CDC_NOTIF, 8, EPNUM_0_CDC, 0x80 | EPNUM_0_CDC, CFG_TUD_CDC_EP_BUFSIZE),
173#endif
174
175#if CFG_TUD_CDC > 1
176 // Interface number, string index, EP notification address and size, EP data address (out, in) and size.
177 TUD_CDC_DESCRIPTOR(ITF_NUM_CDC1, 4, 0x80 | EPNUM_1_CDC_NOTIF, 8, EPNUM_1_CDC, 0x80 | EPNUM_1_CDC, CFG_TUD_CDC_EP_BUFSIZE),
178#endif
179
180#if CFG_TUD_MSC
181 // Interface number, string index, EP Out & EP In address, EP size
182 TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC, 0x80 | EPNUM_MSC, 64), // highspeed 512
183#endif
184};
185
186/* End of Kconfig driven Descriptor */
const char * tusb_desc_strarray_device_t[8]
#define USB_ESPRESSIF_VID
#define CFG_TUD_MSC
#define CFG_TUD_CDC
#define CFG_TUD_ENDPOINT0_SIZE
Definition tusb_config.h:83
@ ITF_NUM_TOTAL
@ TUSB_DESC_TOTAL_LEN
@ EP_EMPTY
#define USB_TUSB_PID
tusb_desc_strarray_device_t descriptor_str_tinyusb
tusb_desc_strarray_device_t descriptor_str_kconfig
const tusb_desc_device_t descriptor_dev_kconfig
const uint8_t descriptor_cfg_kconfig[]
tusb_desc_device_t descriptor_tinyusb