Skip to content

Commit

Permalink
force re-enumerate for all examples. For some fast chip such as esp32…
Browse files Browse the repository at this point in the history
…-s3, by the time we initialize class driver enumeration process is already done.
  • Loading branch information
hathach committed Sep 5, 2024
1 parent 2df6d29 commit 59a651c
Show file tree
Hide file tree
Showing 24 changed files with 234 additions and 108 deletions.
7 changes: 7 additions & 0 deletions examples/CDC/cdc_multi/cdc_multi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ void setup() {
// initialize 2nd CDC interface
USBSer1.begin(115200);

// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}

while (!Serial || !USBSer1) {
if (Serial) {
Serial.println("Waiting for other USB ports");
Expand Down
7 changes: 7 additions & 0 deletions examples/CDC/no_serial/no_serial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ void setup()
// clear configuration will remove all USB interfaces including CDC (Serial)
TinyUSBDevice.clearConfiguration();

// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}

pinMode(led, OUTPUT);
}

Expand Down
10 changes: 9 additions & 1 deletion examples/Composite/mouse_ramdisk/mouse_ramdisk.ino
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ void setup() {
TinyUSBDevice.begin(0);
}

Serial.begin(115200);

// Set disk vendor id, product id and revision with string up to 8, 16, 4 characters respectively
usb_msc.setID("Adafruit", "Mass Storage", "1.0");

Expand All @@ -97,7 +99,13 @@ void setup() {
usb_hid.setPollInterval(2);
usb_hid.begin();

Serial.begin(115200);
// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}

Serial.println("Adafruit TinyUSB Mouse + Mass Storage (ramdisk) example");
}

Expand Down
7 changes: 7 additions & 0 deletions examples/HID/hid_boot_keyboard/hid_boot_keyboard.ino
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ void setup() {

usb_hid.begin();

// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}

// led pin
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Expand Down
11 changes: 9 additions & 2 deletions examples/HID/hid_boot_mouse/hid_boot_mouse.ino
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ void setup() {
TinyUSBDevice.begin(0);
}

Serial.begin(115200);

// Set up button, pullup opposite to active state
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);

Expand All @@ -67,10 +69,15 @@ void setup() {
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.setStringDescriptor("TinyUSB Mouse");

usb_hid.begin();

Serial.begin(115200);
// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}

Serial.println("Adafruit TinyUSB HID Mouse example");
}

Expand Down
11 changes: 9 additions & 2 deletions examples/HID/hid_composite/hid_composite.ino
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,24 @@ void setup() {
TinyUSBDevice.begin(0);
}

Serial.begin(115200);

// Set up HID
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.setStringDescriptor("TinyUSB HID Composite");

usb_hid.begin();

// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}

// Set up button, pullup opposite to active state
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);

Serial.begin(115200);
Serial.println("Adafruit TinyUSB HID Composite example");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,20 @@ void setup() {
if (!TinyUSBDevice.isInitialized()) {
TinyUSBDevice.begin(0);
}
Serial.begin(115200);

// Notes: following commented-out functions has no affect on ESP32
// usb_hid.setPollInterval(2);
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.begin();

Serial.begin(115200);
// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}

Serial.println("Adafruit TinyUSB HID Mouse with Joy FeatherWing example");

if (!ss.begin(0x49)) {
Expand Down
10 changes: 9 additions & 1 deletion examples/HID/hid_dual_interfaces/hid_dual_interfaces.ino
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ void setup() {
TinyUSBDevice.begin(0);
}

Serial.begin(115200);

// HID Keyboard
usb_keyboard.setPollInterval(2);
usb_keyboard.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
Expand All @@ -81,10 +83,16 @@ void setup() {
usb_mouse.setStringDescriptor("TinyUSB HID Keyboard");
usb_mouse.begin();

// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}

// Set up button, pullup opposite to active state
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);

Serial.begin(115200);
Serial.println("Adafruit TinyUSB HID Composite example");
}

Expand Down
7 changes: 7 additions & 0 deletions examples/HID/hid_gamepad/hid_gamepad.ino
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ void setup() {
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.begin();

// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}

Serial.println("Adafruit TinyUSB HID Gamepad example");
}

Expand Down
11 changes: 9 additions & 2 deletions examples/HID/hid_generic_inout/hid_generic_inout.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,23 @@ void setup() {
TinyUSBDevice.begin(0);
}

Serial.begin(115200);

// Notes: following commented-out functions has no affect on ESP32
usb_hid.enableOutEndpoint(true);
usb_hid.setPollInterval(2);
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
usb_hid.setStringDescriptor("TinyUSB HID Generic");

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

Serial.begin(115200);
// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}

Serial.println("Adafruit TinyUSB HID Generic In Out example");
}

Expand Down
14 changes: 9 additions & 5 deletions examples/MIDI/midi_multi_ports/midi_multi_ports.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
// USB MIDI object with 3 ports
Adafruit_USBD_MIDI usb_midi(3);

void setup()
{
void setup() {
pinMode(LED_BUILTIN, OUTPUT);

// Manual begin() is required on core without built-in support e.g. mbed rp2040
Expand All @@ -32,12 +31,17 @@ void setup()
usb_midi.setCableName(1, "Keyboard");
usb_midi.setCableName(2, "Drum Pads");
usb_midi.setCableName(3, "Lights");

usb_midi.begin();

// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}
}

void loop()
{
void loop() {
#ifdef TINYUSB_NEED_POLLING_TASK
// Manual call tud_task since it isn't called by Core's background
TinyUSBDevice.task();
Expand Down
11 changes: 9 additions & 2 deletions examples/MIDI/midi_test/midi_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ void setup() {
TinyUSBDevice.begin(0);
}

Serial.begin(115200);

pinMode(LED_BUILTIN, OUTPUT);

usb_midi.setStringDescriptor("TinyUSB MIDI");
Expand All @@ -50,14 +52,19 @@ void setup() {
// This will also call usb_midi's begin()
MIDI.begin(MIDI_CHANNEL_OMNI);

// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}

// Attach the handleNoteOn function to the MIDI Library. It will
// be called whenever the Bluefruit receives MIDI Note On messages.
MIDI.setHandleNoteOn(handleNoteOn);

// Do the same for MIDI Note Off messages.
MIDI.setHandleNoteOff(handleNoteOff);

Serial.begin(115200);
}

void loop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,19 @@ void setupMassStorage(void)
// MSC is ready for read/write
fs_changed = false;
usb_msc.setReadyCallback(0, msc_ready_callback);

usb_msc.begin();

// If already enumerated, additional class driverr begin() e.g msc, hid, midi won't take effect until re-enumeration
if (TinyUSBDevice.mounted()) {
TinyUSBDevice.detach();
delay(10);
TinyUSBDevice.attach();
}

// Init file system on the flash
fs_formatted = fatfs.begin(&flash);

if ( !fs_formatted )
{
if ( !fs_formatted ) {
DBG_SERIAL.println("Failed to init files system, flash may not be formatted");
}
}
Expand Down
Loading

0 comments on commit 59a651c

Please sign in to comment.