Skip to content

Declare interrupt handler functions for ESP32 DWC2#3330

Closed
me-no-dev wants to merge 1 commit intomasterfrom
me-no-dev-patch-1
Closed

Declare interrupt handler functions for ESP32 DWC2#3330
me-no-dev wants to merge 1 commit intomasterfrom
me-no-dev-patch-1

Conversation

@me-no-dev
Copy link
Collaborator

@me-no-dev me-no-dev commented Nov 4, 2025

When both host and device are enabled at the same time, this function is compiled twice, once for host and once for device, but the device dcd_int_handler is not declared when compiling host and the opposite for when compiling device. This PR only adds the declarations so that the function can compile in both cases.

Device dcd_dwc2.c

tinyusb/src/portable/synopsys/dwc2/dcd_dwc2.c
In file included from tinyusb/src/portable/synopsys/dwc2/dwc2_common.h:43,
                 from tinyusb/src/portable/synopsys/dwc2/dcd_dwc2.c:43:
tinyusb/src/portable/synopsys/dwc2/dwc2_esp32.h: In function 'dwc2_int_handler_wrap':
tinyusb/src/portable/synopsys/dwc2/dwc2_esp32.h:95:5: error: implicit declaration of function 'hcd_int_handler'; did you mean 'dcd_int_handler'? [-Wimplicit-function-declaration]
   95 |     hcd_int_handler(rhport, true);
      |     ^~~~~~~~~~~~~~~
      |     dcd_int_handler

Host hcd_dwc2.c

tinyusb/src/portable/synopsys/dwc2/hcd_dwc2.c
In file included from tinyusb/src/portable/synopsys/dwc2/dwc2_common.h:43,
                 from tinyusb/src/portable/synopsys/dwc2/hcd_dwc2.c:40:
tinyusb/src/portable/synopsys/dwc2/dwc2_esp32.h: In function 'dwc2_int_handler_wrap':
tinyusb/src/portable/synopsys/dwc2/dwc2_esp32.h:90:5: error: implicit declaration of function 'dcd_int_handler'; did you mean 'hcd_int_handler'? [-Wimplicit-function-declaration]
   90 |     dcd_int_handler(rhport);
      |     ^~~~~~~~~~~~~~~
      |     hcd_int_handler

@me-no-dev me-no-dev requested review from Copilot and hathach and removed request for Copilot November 4, 2025 08:51
@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 4, 2025

@HiFiPhile
Copy link
Collaborator

dwc2_esp32.h is included by dwc2_common.h, which is included by dwc2_common.c, dwc2_common.c also includes the prototype headers, so it should be fine:

dwc2_common.c
|--device/dcd.h   -> void dcd_int_handler(uint8_t rhport);
|--host/hcd.h     -> void hcd_int_handler(uint8_t rhport, bool in_isr);
|--dwc2_common.h
    |--dwc2_esp32.h

I think the real issue is prevent dwc2_int_handler_wrap to be compiled 2 times ?

@me-no-dev
Copy link
Collaborator Author

me-no-dev commented Nov 4, 2025

@HiFiPhile it was hard to track this down, but in Arduino we now have both Host and Device enabled at the same time and that produced build errors for undefined dcd_int_handler when hcd is compiled and hcd_int_handler when dcd is compiled. I guess my colleagues at Espressif never tested both options enabled at the same time.

@me-no-dev
Copy link
Collaborator Author

me-no-dev commented Nov 4, 2025

@HiFiPhile

  • dcd_dwc2.c includes dcd.h which defines dcd_int_handler and also pull dwc2_esp32.h
  • hcd_dwc2.c includes hcd.h which defines hcd_int_handler and also pull dwc2_esp32.h

So they both compile the function dwc2_int_handler_wrap in dwc2_esp32.h and see that both Host and Device are enabled, but depending on which usbh or usbd is compiled, it only sees hcd_int_handler or dcd_int_handler declared, but never both. Compilation fails

@HiFiPhile
Copy link
Collaborator

@me-no-dev

device/usbd.c includes dcd.h which defines dcd_int_handler and also pull dwc2_esp32.h
host/usbh.c includes hcd.h which defines hcd_int_handler and also pull dwc2_esp32.h

I can't find how dwc2_esp32.h get pulled by usbd.c or usbh.c, the only dependency chain I found are:
ddc_dwc2.c -> dwc2_common.h ->dwc2_esp32.h
ddc_common.c -> dwc2_common.h ->dwc2_esp32.h
hdc_dwc2.c -> dwc2_common.h ->dwc2_esp32.h

@me-no-dev
Copy link
Collaborator Author

@HiFiPhile let me try to compile and give you the exact errors

@me-no-dev
Copy link
Collaborator Author

@HiFiPhile
Device dcd_dwc2.c

/Users/ficeto/Desktop/ESP32/ESP-IDF-5/esp32-arduino-lib-builder/components/arduino_tinyusb/tinyusb/src/portable/synopsys/dwc2/dcd_dwc2.c
In file included from /Users/ficeto/Desktop/ESP32/ESP-IDF-5/esp32-arduino-lib-builder/components/arduino_tinyusb/tinyusb/src/portable/synopsys/dwc2/dwc2_common.h:43,
                 from /Users/ficeto/Desktop/ESP32/ESP-IDF-5/esp32-arduino-lib-builder/components/arduino_tinyusb/tinyusb/src/portable/synopsys/dwc2/dcd_dwc2.c:43:
/Users/ficeto/Desktop/ESP32/ESP-IDF-5/esp32-arduino-lib-builder/components/arduino_tinyusb/tinyusb/src/portable/synopsys/dwc2/dwc2_esp32.h: In function 'dwc2_int_handler_wrap':
/Users/ficeto/Desktop/ESP32/ESP-IDF-5/esp32-arduino-lib-builder/components/arduino_tinyusb/tinyusb/src/portable/synopsys/dwc2/dwc2_esp32.h:95:5: error: implicit declaration of function 'hcd_int_handler'; did you mean 'dcd_int_handler'? [-Wimplicit-function-declaration]
   95 |     hcd_int_handler(rhport, true);
      |     ^~~~~~~~~~~~~~~
      |     dcd_int_handler

Host hcd_dwc2.c

/Users/ficeto/Desktop/ESP32/ESP-IDF-5/esp32-arduino-lib-builder/components/arduino_tinyusb/tinyusb/src/portable/synopsys/dwc2/hcd_dwc2.c
In file included from /Users/ficeto/Desktop/ESP32/ESP-IDF-5/esp32-arduino-lib-builder/components/arduino_tinyusb/tinyusb/src/portable/synopsys/dwc2/dwc2_common.h:43,
                 from /Users/ficeto/Desktop/ESP32/ESP-IDF-5/esp32-arduino-lib-builder/components/arduino_tinyusb/tinyusb/src/portable/synopsys/dwc2/hcd_dwc2.c:40:
/Users/ficeto/Desktop/ESP32/ESP-IDF-5/esp32-arduino-lib-builder/components/arduino_tinyusb/tinyusb/src/portable/synopsys/dwc2/dwc2_esp32.h: In function 'dwc2_int_handler_wrap':
/Users/ficeto/Desktop/ESP32/ESP-IDF-5/esp32-arduino-lib-builder/components/arduino_tinyusb/tinyusb/src/portable/synopsys/dwc2/dwc2_esp32.h:90:5: error: implicit declaration of function 'dcd_int_handler'; did you mean 'hcd_int_handler'? [-Wimplicit-function-declaration]
   90 |     dcd_int_handler(rhport);
      |     ^~~~~~~~~~~~~~~
      |     hcd_int_handler

I guess my mistake, not usbd and usbh but dcd_dwc2 and hcd_dwc2

@me-no-dev
Copy link
Collaborator Author

@HiFiPhile @hathach CI failures are unrelated to this PR

@HiFiPhile
Copy link
Collaborator

@me-no-dev Thank you for the log.
Adding a declaration could resulting error dcd_dwc2.c:1040:6: error: redundant redeclaration of 'dcd_int_handler' [-Werror=redundant-decls], even though for esp-idf this check is not enabled ATM.

Another way is to fix once for all (future) ports, but it will leak dcd api into hcd or vice versa:

diff --git a/src/portable/synopsys/dwc2/dwc2_common.c b/src/portable/synopsys/dwc2/dwc2_common.c
index 5ff18ab94..ee79ddc46 100644
--- a/src/portable/synopsys/dwc2/dwc2_common.c
+++ b/src/portable/synopsys/dwc2/dwc2_common.c
@@ -30,15 +30,6 @@

 #if defined(TUP_USBIP_DWC2) && (CFG_TUH_ENABLED || CFG_TUD_ENABLED)

-#if CFG_TUD_ENABLED
-#include "device/dcd.h"
-#endif
-
-#if CFG_TUH_ENABLED
-#include "host/hcd.h"
-#include "host/usbh.h"
-#endif
-
 #include "dwc2_common.h"

 //--------------------------------------------------------------------
diff --git a/src/portable/synopsys/dwc2/dwc2_common.h b/src/portable/synopsys/dwc2/dwc2_common.h
index 0166b0261..dc204f578 100644
--- a/src/portable/synopsys/dwc2/dwc2_common.h
+++ b/src/portable/synopsys/dwc2/dwc2_common.h
@@ -30,6 +30,14 @@
 #include "common/tusb_common.h"
 #include "dwc2_type.h"

+#if CFG_TUD_ENABLED
+#include "device/dcd.h"
+#endif
+
+#if CFG_TUH_ENABLED
+#include "host/hcd.h"
+#endif
+
 // Following symbols must be defined by port header
 // - _dwc2_controller[]: array of controllers
 // - DWC2_EP_MAX: largest EP counts of all controllers

Let's see how @hathach thinks.

@me-no-dev
Copy link
Collaborator Author

@HiFiPhile I'm OK with whatever you guys decide, as long as it's working 😄

@me-no-dev
Copy link
Collaborator Author

@HiFiPhile maybe the includes can be added to dwc2_esp32.h, so that APIs would be mixed only for ESP32? Could this problem exist for other targets?

@HiFiPhile
Copy link
Collaborator

@HiFiPhile maybe the includes can be added to dwc2_esp32.h, so that APIs would be mixed only for ESP32? Could this problem exist for other targets?

I'm good with it, other port headers are simpler and doesn't need dcd/hcd api for now.

@hathach
Copy link
Owner

hathach commented Nov 5, 2025

@me-no-dev let me check, I think I got is the old version of esp32p4 devkit that does not wire the usbfs to usbc connector. And I didn't test with dual examples.

@me-no-dev
Copy link
Collaborator Author

@hathach yes. P4 EV board has the USB-OTG port only for host, but there are other boards and there are also S2 and S3 boards that we tested with. Both host and device are working fine with the changes (only one is initialize at a time), but since Arduino uses pre-built TinyUSB, we need it to be compiled for both at the same time.

@hathach
Copy link
Owner

hathach commented Nov 6, 2025

@HiFiPhile suggestion is better, I have made an PR for it, also update example to compile/test with #3333, but haven't tested dual mode on 2 otg (fs & hs) just yet as I don't have time to wiring up the board atm, will test it out later.

@hathach hathach closed this in #3333 Nov 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants