Skip to content

Update dwc2 and TinyUSB component #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions components/arduino_tinyusb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ if(CONFIG_TINYUSB_ENABLED)
"-DCFG_TUSB_DEBUG=${CONFIG_TINYUSB_DEBUG_LEVEL}"
"-Wno-type-limits" # needed for the vanila tinyusb with turned off classes
)
elseif(IDF_TARGET STREQUAL "esp32p4")
set(compile_options
"-DCFG_TUSB_MCU=OPT_MCU_ESP32P4"
"-DCFG_TUSB_DEBUG=${CONFIG_TINYUSB_DEBUG_LEVEL}"
"-Wno-type-limits" # needed for the vanila tinyusb with turned off classes
)
endif()

set(srcs
# espressif:
#"${COMPONENT_DIR}/src/dcd_esp32sx.c"
"${COMPONENT_DIR}/src/dcd_dwc2.c"
# tusb:
#"${COMPONENT_DIR}/tinyusb/src/portable/espressif/esp32sx/dcd_esp32sx.c"
#"{COMPONENT_DIR}/tinyusb/src/portable/synopsys/dwc2/dcd_dwc2.c"
"${COMPONENT_DIR}/tinyusb/src/class/cdc/cdc_device.c"
"${COMPONENT_DIR}/tinyusb/src/class/hid/hid_device.c"
Expand All @@ -32,6 +36,7 @@ if(CONFIG_TINYUSB_ENABLED)
"${COMPONENT_DIR}/tinyusb/src/class/dfu/dfu_rt_device.c"
"${COMPONENT_DIR}/tinyusb/src/class/dfu/dfu_device.c"
"${COMPONENT_DIR}/tinyusb/src/class/vendor/vendor_device.c"
"${COMPONENT_DIR}/tinyusb/src/class/net/ncm_device.c"
"${COMPONENT_DIR}/tinyusb/src/common/tusb_fifo.c"
"${COMPONENT_DIR}/tinyusb/src/device/usbd_control.c"
"${COMPONENT_DIR}/tinyusb/src/device/usbd.c"
Expand All @@ -56,16 +61,15 @@ if(CONFIG_TINYUSB_ENABLED)

set(requires esp_rom freertos soc)
set(priv_requires arduino main)
### tinyusb lib ###
###################
idf_component_register(INCLUDE_DIRS ${includes_public} PRIV_INCLUDE_DIRS ${includes_private} SRCS ${srcs} REQUIRES ${requires} PRIV_REQUIRES ${priv_requires})
# add_library(${COMPONENT_TARGET} STATIC ${srcs})
# target_include_directories(
# ${COMPONENT_TARGET}
# PUBLIC ${includes_public}
# PRIVATE ${includes_private})

idf_component_register(
INCLUDE_DIRS ${includes_public}
PRIV_INCLUDE_DIRS ${includes_private}
SRCS ${srcs}
REQUIRES ${requires}
PRIV_REQUIRES ${priv_requires}
)
target_compile_options(${COMPONENT_TARGET} PRIVATE ${compile_options})
#target_link_libraries(${COMPONENT_TARGET} INTERFACE ${COMPONENT_TARGET})

else()

Expand Down
11 changes: 11 additions & 0 deletions components/arduino_tinyusb/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,17 @@ menu "Arduino TinyUSB"

endmenu

menu "NCM driver"
depends on TINYUSB_ENABLED

config TINYUSB_NCM_ENABLED
bool "Enable USB NCM TinyUSB driver"
default y
help
Enable USB NCM TinyUSB driver.

endmenu

config TINYUSB_DEBUG_LEVEL
int "TinyUSB log level (0-3)"
default 0
Expand Down
5 changes: 5 additions & 0 deletions components/arduino_tinyusb/include/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ extern "C" {
# define CONFIG_TINYUSB_VENDOR_ENABLED 0
#endif

#ifndef CONFIG_TINYUSB_NCM_ENABLED
# define CONFIG_TINYUSB_NCM_ENABLED 0
#endif

/* */
/* COMMON CONFIGURATION */
/* */
Expand Down Expand Up @@ -113,6 +117,7 @@ extern "C" {
#define CFG_TUD_DFU_RUNTIME CONFIG_TINYUSB_DFU_RT_ENABLED
#define CFG_TUD_DFU CONFIG_TINYUSB_DFU_ENABLED
#define CFG_TUD_VENDOR CONFIG_TINYUSB_VENDOR_ENABLED
#define CFG_TUD_NCM CONFIG_TINYUSB_NCM_ENABLED

// CDC FIFO size of TX and RX
#define CFG_TUD_CDC_RX_BUFSIZE CONFIG_TINYUSB_CDC_RX_BUFSIZE
Expand Down
Original file line number Diff line number Diff line change
@@ -1,83 +1,81 @@
--- a/components/arduino_tinyusb/tinyusb/src/portable/synopsys/dwc2/dcd_dwc2.c 2024-06-10 22:10:55.000000000 +0300
+++ b/components/arduino_tinyusb/tinyusb/src/portable/synopsys/dwc2/dcd_dwc2.c 2024-06-10 22:20:01.000000000 +0300
@@ -186,6 +186,18 @@
return true;
}

+#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
--- a/components/arduino_tinyusb/src/dcd_dwc2.c 2024-10-02 12:17:40.000000000 +0300
+++ b/components/arduino_tinyusb/src/dcd_dwc2.c 2024-10-02 12:19:48.000000000 +0300
@@ -316,6 +316,16 @@
//--------------------------------------------------------------------
// Endpoint
//--------------------------------------------------------------------
+#if defined(TUP_USBIP_DWC2_ESP32)
+// Keep count of how many FIFOs are in use
+static uint8_t _allocated_fifos = 1; //FIFO0 is always in use
+
+// Will either return an unused FIFO number, or 0 if all are used.
+static uint8_t get_free_fifo(void)
+{
+static uint8_t get_free_fifo(void) {
+ if (_allocated_fifos < 5) return _allocated_fifos++;
+ return 0;
+}
+#endif
+
static void edpt_activate(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc) {
dwc2_regs_t* dwc2 = DWC2_REG(rhport);
uint8_t const epnum = tu_edpt_number(p_endpoint_desc->bEndpointAddress);
@@ -205,7 +217,18 @@
@@ -336,7 +346,18 @@
dwc2->epout[epnum].doepctl = dxepctl;
dwc2->daintmsk |= TU_BIT(DAINTMSK_OEPM_Pos + epnum);
} else {
- dwc2->epin[epnum].diepctl = dxepctl | (epnum << DIEPCTL_TXFNUM_Pos);
+ uint8_t fifo_num = epnum;
+#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
+#if defined(TUP_USBIP_DWC2_ESP32)
+ // Special Case for EP5, which is used by CDC but not actually called by the driver
+ // we can give it a fake FIFO
+ if (epnum == 5) {
+ fifo_num = epnum;
+ } else {
+ fifo_num = get_free_fifo();
+ }
+ TU_ASSERT(fifo_num != 0);
+ //TU_ASSERT(fifo_num != 0);
+#endif
+ dwc2->epin[epnum].diepctl = dxepctl | (fifo_num << DIEPCTL_TXFNUM_Pos);
dwc2->daintmsk |= (1 << (DAINTMSK_IEPM_Pos + epnum));
dwc2->daintmsk |= TU_BIT(DAINTMSK_IEPM_Pos + epnum);
}
}
@@ -728,6 +751,10 @@
// reset allocated fifo IN
_allocated_fifo_words_tx = 16;
@@ -850,6 +871,10 @@
xfer_status[n][TUSB_DIR_IN].max_size = 0;
}

+#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
+#if defined(TUP_USBIP_DWC2_ESP32)
+ _allocated_fifos = 1;
+#endif
+
fifo_flush_tx(dwc2, 0x10); // all tx fifo
fifo_flush_rx(dwc2);
}
@@ -1096,6 +1123,9 @@
dfifo_flush_tx(dwc2, 0x10); // all tx fifo
dfifo_flush_rx(dwc2);

@@ -1204,6 +1229,9 @@
if (int_status & GINTSTS_USBRST) {
// USBRST is start of reset.
dwc2->gintsts = GINTSTS_USBRST;
+#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
+#if defined(TUP_USBIP_DWC2_ESP32)
+ _allocated_fifos = 1;
+#endif
bus_reset(rhport);
}

@@ -1127,7 +1157,11 @@
@@ -1235,7 +1263,11 @@

if (int_status & GINTSTS_USBSUSP) {
dwc2->gintsts = GINTSTS_USBSUSP;
- dcd_event_bus_signal(rhport, DCD_EVENT_SUSPEND, true);
+ //dcd_event_bus_signal(rhport, DCD_EVENT_SUSPEND, true);
+ dcd_event_bus_signal(rhport, DCD_EVENT_UNPLUGGED, true);
+#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
+#if defined(TUP_USBIP_DWC2_ESP32)
+ _allocated_fifos = 1;
+#endif
}

if (int_status & GINTSTS_WKUINT) {
@@ -1144,6 +1178,9 @@
@@ -1252,6 +1284,9 @@

if (otg_int & GOTGINT_SEDET) {
dcd_event_bus_signal(rhport, DCD_EVENT_UNPLUGGED, true);
+#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
+#if defined(TUP_USBIP_DWC2_ESP32)
+ _allocated_fifos = 1;
+#endif
}
Expand Down
Loading