Skip to content

Commit 5cd4063

Browse files
authored
Merge pull request #372 from adafruit/rework-esp-config-desc-builder
Rework ESP32 configuration descriptor builder
2 parents 185692b + 1bae99a commit 5cd4063

33 files changed

+3895
-426
lines changed

examples/CDC/no_serial/no_serial.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ int led = LED_BUILTIN;
2323

2424
void setup()
2525
{
26-
Serial.end();
26+
// clear configuration will remove all USB interfaces including CDC (Serial)
27+
TinyUSBDevice.clearConfiguration();
28+
2729
pinMode(led, OUTPUT);
2830
}
2931

examples/Composite/mouse_external_flash/mouse_external_flash.ino

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,12 @@ Adafruit_USBD_MSC usb_msc;
7272

7373
// HID report descriptor using TinyUSB's template
7474
// Single Report (no ID) descriptor
75-
uint8_t const desc_hid_report[] =
76-
{
77-
TUD_HID_REPORT_DESC_MOUSE()
75+
uint8_t const desc_hid_report[] = {
76+
TUD_HID_REPORT_DESC_MOUSE()
7877
};
7978

80-
// USB HID object. For ESP32 these values cannot be changed after this declaration
81-
// desc report, desc len, protocol, interval, use out endpoint
82-
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, false);
79+
// USB HID object
80+
Adafruit_USBD_HID usb_hid;
8381

8482
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
8583
const int pin = 4; // Left Button
@@ -93,6 +91,10 @@ Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROT
9391
const int pin = PIN_BUTTON1;
9492
bool activeState = false;
9593

94+
#elif defined(ARDUINO_ARCH_ESP32)
95+
const int pin = 0;
96+
bool activeState = false;
97+
9698
#else
9799
const int pin = 12;
98100
bool activeState = false;
@@ -102,6 +104,11 @@ Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROT
102104
// the setup function runs once when you press reset or power the board
103105
void setup()
104106
{
107+
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
108+
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
109+
TinyUSB_Device_Init(0);
110+
#endif
111+
105112
flash.begin();
106113

107114
pinMode(LED_BUILTIN, OUTPUT);
@@ -123,9 +130,10 @@ void setup()
123130
// Set up button
124131
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
125132

126-
// Notes: following commented-out functions has no affect on ESP32
127-
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
128-
133+
// Set up HID
134+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
135+
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_NONE);
136+
usb_hid.setPollInterval(2);
129137
usb_hid.begin();
130138

131139
Serial.begin(115200);

examples/Composite/mouse_ramdisk/mouse_ramdisk.ino

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ Adafruit_USBD_MSC usb_msc;
3333

3434
// HID report descriptor using TinyUSB's template
3535
// Single Report (no ID) descriptor
36-
uint8_t const desc_hid_report[] =
37-
{
38-
TUD_HID_REPORT_DESC_MOUSE()
36+
uint8_t const desc_hid_report[] = {
37+
TUD_HID_REPORT_DESC_MOUSE()
3938
};
4039

41-
// USB HID object. For ESP32 these values cannot be changed after this declaration
42-
// desc report, desc len, protocol, interval, use out endpoint
43-
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, false);
40+
// USB HID object
41+
Adafruit_USBD_HID usb_hid;
4442

4543
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
4644
const int pin = 4; // Left Button
@@ -54,6 +52,10 @@ Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROT
5452
const int pin = PIN_BUTTON1;
5553
bool activeState = false;
5654

55+
#elif defined(ARDUINO_ARCH_ESP32)
56+
const int pin = 0;
57+
bool activeState = false;
58+
5759
#else
5860
const int pin = 12;
5961
bool activeState = false;
@@ -84,9 +86,10 @@ void setup()
8486
// Set up button
8587
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
8688

87-
// Notes: following commented-out functions has no affect on ESP32
88-
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
89-
89+
// Set up HID
90+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
91+
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_NONE);
92+
usb_hid.setPollInterval(2);
9093
usb_hid.begin();
9194

9295
Serial.begin(115200);

examples/HID/hid_boot_keyboard/hid_boot_keyboard.ino

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020

2121
// HID report descriptor using TinyUSB's template
2222
// Single Report (no ID) descriptor
23-
uint8_t const desc_hid_report[] =
24-
{
23+
uint8_t const desc_hid_report[] = {
2524
TUD_HID_REPORT_DESC_KEYBOARD()
2625
};
2726

2827
// USB HID object. For ESP32 these values cannot be changed after this declaration
2928
// desc report, desc len, protocol, interval, use out endpoint
30-
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_KEYBOARD, 2, false);
29+
Adafruit_USBD_HID usb_hid;
3130

3231
//------------- Input Pins -------------//
3332
// Array of pins and its keycode.
@@ -73,11 +72,11 @@ void setup()
7372
TinyUSB_Device_Init(0);
7473
#endif
7574

76-
// Notes: following commented-out functions has no affect on ESP32
77-
// usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
78-
// usb_hid.setPollInterval(2);
79-
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
80-
// usb_hid.setStringDescriptor("TinyUSB Keyboard");
75+
// Setup HID
76+
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
77+
usb_hid.setPollInterval(2);
78+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
79+
usb_hid.setStringDescriptor("TinyUSB Keyboard");
8180

8281
// Set up output report (on control endpoint) for Capslock indicator
8382
usb_hid.setReportCallback(NULL, hid_report_callback);

examples/HID/hid_boot_mouse/hid_boot_mouse.ino

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
const int pin = PIN_BUTTON1;
3131
bool activeState = false;
3232

33+
#elif defined(ARDUINO_ARCH_ESP32)
34+
const int pin = 0;
35+
bool activeState = false;
36+
3337
#else
3438
const int pin = 12;
3539
bool activeState = false;
@@ -38,14 +42,12 @@
3842

3943
// HID report descriptor using TinyUSB's template
4044
// Single Report (no ID) descriptor
41-
uint8_t const desc_hid_report[] =
42-
{
45+
uint8_t const desc_hid_report[] = {
4346
TUD_HID_REPORT_DESC_MOUSE()
4447
};
4548

46-
// USB HID object. For ESP32 these values cannot be changed after this declaration
47-
// desc report, desc len, protocol, interval, use out endpoint
48-
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_MOUSE, 2, false);
49+
// USB HID object
50+
Adafruit_USBD_HID usb_hid;
4951

5052
// the setup function runs once when you press reset or power the board
5153
void setup()
@@ -58,11 +60,11 @@ void setup()
5860
// Set up button, pullup opposite to active state
5961
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
6062

61-
// Notes: following commented-out functions has no affect on ESP32
62-
// usb_hid.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
63-
// usb_hid.setPollInterval(2);
64-
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
65-
// usb_hid.setStringDescriptor("TinyUSB Mouse");
63+
// Set up HID
64+
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
65+
usb_hid.setPollInterval(2);
66+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
67+
usb_hid.setStringDescriptor("TinyUSB Mouse");
6668

6769
usb_hid.begin();
6870

examples/HID/hid_composite/hid_composite.ino

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
const int pin = PIN_BUTTON1;
3232
bool activeState = false;
3333

34+
#elif defined(ARDUINO_ARCH_ESP32)
35+
const int pin = 0;
36+
bool activeState = false;
37+
3438
#else
3539
const int pin = 12;
3640
bool activeState = false;
@@ -46,24 +50,22 @@ enum
4650
};
4751

4852
// HID report descriptor using TinyUSB's template
49-
uint8_t const desc_hid_report[] =
50-
{
53+
uint8_t const desc_hid_report[] = {
5154
TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(RID_KEYBOARD) ),
5255
TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(RID_MOUSE) ),
5356
TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(RID_CONSUMER_CONTROL) )
5457
};
5558

56-
// USB HID object. For ESP32 these values cannot be changed after this declaration
57-
// desc report, desc len, protocol, interval, use out endpoint
58-
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, false);
59+
// USB HID object.
60+
Adafruit_USBD_HID usb_hid;
5961

6062
// the setup function runs once when you press reset or power the board
6163
void setup()
6264
{
63-
// Notes: following commented-out functions has no affect on ESP32
64-
// usb_hid.setPollInterval(2);
65-
// usb_hid.setReportDescriptor();
66-
// usb_hid.setStringDescriptor("TinyUSB HID Composite");
65+
// Set up HID
66+
usb_hid.setPollInterval(2);
67+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
68+
usb_hid.setStringDescriptor("TinyUSB HID Composite");
6769

6870
usb_hid.begin();
6971

examples/HID/hid_dual_interfaces/hid_dual_interfaces.ino

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,43 @@
3434
const int pin = PIN_BUTTON;
3535
bool activeState = false;
3636

37+
#elif defined(ARDUINO_ARCH_ESP32)
38+
const int pin = 0;
39+
bool activeState = false;
40+
3741
#else
3842
const int pin = 12;
3943
bool activeState = false;
4044
#endif
4145

4246
// HID report descriptor using TinyUSB's template
43-
uint8_t const desc_keyboard_report[] =
44-
{
47+
uint8_t const desc_keyboard_report[] = {
4548
TUD_HID_REPORT_DESC_KEYBOARD()
4649
};
4750

48-
uint8_t const desc_mouse_report[] =
49-
{
51+
uint8_t const desc_mouse_report[] = {
5052
TUD_HID_REPORT_DESC_MOUSE()
5153
};
5254

53-
// USB HID object. For ESP32 these values cannot be changed after this declaration
54-
// desc report, desc len, protocol, interval, use out endpoint
55-
Adafruit_USBD_HID usb_keyboard(desc_keyboard_report, sizeof(desc_keyboard_report), HID_ITF_PROTOCOL_KEYBOARD, 2, false);
56-
Adafruit_USBD_HID usb_mouse(desc_mouse_report, sizeof(desc_mouse_report), HID_ITF_PROTOCOL_MOUSE, 2, false);
55+
// USB HID objects
56+
Adafruit_USBD_HID usb_keyboard;
57+
Adafruit_USBD_HID usb_mouse;
5758

5859
// the setup function runs once when you press reset or power the board
5960
void setup()
6061
{
61-
// Notes: following commented-out functions has no affect on ESP32
62-
// usb_keyboard.setPollInterval(2);
63-
// usb_keyboard.setReportDescriptor();
64-
// usb_keyboard.setStringDescriptor("TinyUSB HID Composite");
65-
62+
// HID Keyboard
63+
usb_keyboard.setPollInterval(2);
64+
usb_keyboard.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
65+
usb_keyboard.setReportDescriptor(desc_keyboard_report, sizeof(desc_keyboard_report));
66+
usb_keyboard.setStringDescriptor("TinyUSB HID Keyboard");
6667
usb_keyboard.begin();
68+
69+
// HID Mouse
70+
usb_mouse.setPollInterval(2);
71+
usb_mouse.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
72+
usb_mouse.setReportDescriptor(desc_mouse_report, sizeof(desc_mouse_report));
73+
usb_mouse.setStringDescriptor("TinyUSB HID Keyboard");
6774
usb_mouse.begin();
6875

6976
// Set up button, pullup opposite to active state

examples/HID/hid_gamepad/hid_gamepad.ino

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222

2323
// HID report descriptor using TinyUSB's template
2424
// Single Report (no ID) descriptor
25-
uint8_t const desc_hid_report[] =
26-
{
25+
uint8_t const desc_hid_report[] = {
2726
TUD_HID_REPORT_DESC_GAMEPAD()
2827
};
2928

30-
// USB HID object. For ESP32 these values cannot be changed after this declaration
31-
// desc report, desc len, protocol, interval, use out endpoint
32-
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, false);
29+
// USB HID object
30+
Adafruit_USBD_HID usb_hid;
3331

3432
// Report payload defined in src/class/hid/hid.h
3533
// - For Gamepad Button Bit Mask see hid_gamepad_button_bm_t
@@ -45,9 +43,9 @@ void setup()
4543

4644
Serial.begin(115200);
4745

48-
// Notes: following commented-out functions has no affect on ESP32
49-
// usb_hid.setPollInterval(2);
50-
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
46+
// Setup HID
47+
usb_hid.setPollInterval(2);
48+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
5149

5250
usb_hid.begin();
5351

examples/HID/hid_generic_inout/hid_generic_inout.ino

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,12 @@
3939

4040
// HID report descriptor using TinyUSB's template
4141
// Generic In Out with 64 bytes report (max)
42-
uint8_t const desc_hid_report[] =
43-
{
42+
uint8_t const desc_hid_report[] = {
4443
TUD_HID_REPORT_DESC_GENERIC_INOUT(64)
4544
};
4645

47-
// USB HID object. For ESP32 these values cannot be changed after this declaration
48-
// desc report, desc len, protocol, interval, use out endpoint
49-
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, true);
46+
// USB HID object
47+
Adafruit_USBD_HID usb_hid;
5048

5149
// the setup function runs once when you press reset or power the board
5250
void setup()
@@ -57,10 +55,10 @@ void setup()
5755
#endif
5856

5957
// Notes: following commented-out functions has no affect on ESP32
60-
// usb_hid.enableOutEndpoint(true);
61-
// usb_hid.setPollInterval(2);
62-
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
63-
// usb_hid.setStringDescriptor("TinyUSB HID Generic");
58+
usb_hid.enableOutEndpoint(true);
59+
usb_hid.setPollInterval(2);
60+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
61+
usb_hid.setStringDescriptor("TinyUSB HID Generic");
6462

6563
usb_hid.setReportCallback(get_report_callback, set_report_callback);
6664
usb_hid.begin();

examples/MIDI/midi_test/midi_test.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void setup()
4545

4646
pinMode(LED_BUILTIN, OUTPUT);
4747

48-
//usb_midi.setStringDescriptor("TinyUSB MIDI");
48+
usb_midi.setStringDescriptor("TinyUSB MIDI");
4949

5050
// Initialize MIDI, and listen to all MIDI channels
5151
// This will also call usb_midi's begin()

examples/Video/video_capture/video_capture.ino

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ void loop() {
156156
tud_video_n_frame_xfer(0, 0, (void*) frame_buffer, FRAME_WIDTH * FRAME_HEIGHT * 16 / 8);
157157
}
158158

159+
//--------------------------------------------------------------------+
160+
// TinyUSB Video Callbacks
161+
//--------------------------------------------------------------------+
162+
extern "C" {
163+
159164
void tud_video_frame_xfer_complete_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx) {
160165
(void) ctl_idx;
161166
(void) stm_idx;
@@ -173,6 +178,8 @@ int tud_video_commit_cb(uint_fast8_t ctl_idx, uint_fast8_t stm_idx,
173178
return VIDEO_ERROR_NONE;
174179
}
175180

181+
} // extern C
182+
176183
//------------- Helper -------------//
177184
static void fill_color_bar(uint8_t* buffer, unsigned start_position) {
178185
/* EBU color bars

0 commit comments

Comments
 (0)