From 75ddc8f360075ad558b6c3b8948ce18fdef854d2 Mon Sep 17 00:00:00 2001 From: Mike Date: Fri, 13 Dec 2024 08:47:25 +0000 Subject: [PATCH] Add USB host support for esp32s2(s3), support OTG dual-mode (#2922) This PR updates the USB library to enable Host support for esp32s2 and s3 (untested). This is by switching ot the synopsis DWC2 driver. The basic CDC, HID and MSC interfaces work OK but isochronous are transfers not yet supported. (I'd like this for attaching a USB DAC.) In addition, the OTG hardware (for rp2040 and esp32s2) supports dual-mode operation and this PR allows the operation mode to be selected at run time. The application still needs to decide which mode is required: this can be as simple as connecting a GPIO input to the ID pin. Mode switching between two OTG hosts is more complex and involves session negotiation support which hasn't been investigated yet. **Disconnection events** The esp32s2 doesn't report when devices are disconnected in host mode. There's an open issue for this in tinyusb https://github.com/hathach/tinyusb/issues/564. There's a reference to the IDF regarding [self-powered devices](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/peripherals/usb_device.html#self-powered-device) which states that a separate GPIO is required to sense VBUS for disconnect events. A quick look at the IDF usb code suggests that it handles this using polling and a debounce timer. However, there is a separate disconnect interrupt which requires only a few lines of code to enable and seems to work reliably. Doubtless there will be further host improvements in the future but this is pretty functional as-is. --- .../Arch/Esp32/Components/driver/include/driver/hw_timer.h | 3 +++ Sming/Arch/Esp32/Components/esp32/component.mk | 7 ++++++- Sming/Libraries/USB | 2 +- samples/Basic_IFS/app/application.cpp | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Sming/Arch/Esp32/Components/driver/include/driver/hw_timer.h b/Sming/Arch/Esp32/Components/driver/include/driver/hw_timer.h index 1979b45072..623bc5de29 100644 --- a/Sming/Arch/Esp32/Components/driver/include/driver/hw_timer.h +++ b/Sming/Arch/Esp32/Components/driver/include/driver/hw_timer.h @@ -15,11 +15,14 @@ #include #include +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" #if CONFIG_ESP_TIMER_IMPL_TG0_LAC #include #else #include #endif +#pragma GCC diagnostic pop #ifdef __cplusplus extern "C" { diff --git a/Sming/Arch/Esp32/Components/esp32/component.mk b/Sming/Arch/Esp32/Components/esp32/component.mk index eaf4804398..66ed206cba 100644 --- a/Sming/Arch/Esp32/Components/esp32/component.mk +++ b/Sming/Arch/Esp32/Components/esp32/component.mk @@ -117,7 +117,8 @@ SDK_INCDIRS := \ esp_netif/include \ esp_eth/include \ esp_wifi/include \ - lwip/include/apps/sntp + lwip/include/apps/sntp \ + usb/include ifdef IDF_VERSION_4x SDK_INCDIRS += \ @@ -262,6 +263,10 @@ SDK_COMPONENTS := \ soc \ spi_flash +ifneq (,$(filter esp32s2 esp32s3,$(SMING_SOC))) +SDK_COMPONENTS += usb +endif + ifdef IDF_VERSION_43 SDK_COMPONENTS += $(ESP_VARIANT) else diff --git a/Sming/Libraries/USB b/Sming/Libraries/USB index 0717652a30..6af107fbe2 160000 --- a/Sming/Libraries/USB +++ b/Sming/Libraries/USB @@ -1 +1 @@ -Subproject commit 0717652a3095a3091d5bb56ed683474b2d72f0bf +Subproject commit 6af107fbe25f7a8cb154ccea6ce9fb69e34dc60b diff --git a/samples/Basic_IFS/app/application.cpp b/samples/Basic_IFS/app/application.cpp index 8602249330..acaf936a8f 100644 --- a/samples/Basic_IFS/app/application.cpp +++ b/samples/Basic_IFS/app/application.cpp @@ -246,7 +246,7 @@ bool initFileSystem() #endif #ifdef ENABLE_USB_STORAGE - USB::begin(); + USB::begin(true); USB::MSC::onMount([](auto inst) { usbStorage.begin(inst); usbStorage.enumerate([](auto& unit, const USB::MSC::Inquiry& inquiry) {