Skip to content

Commit 64767cc

Browse files
authored
Compile support for TinyUSB Host (#314)
1 parent fc0e6bb commit 64767cc

File tree

4 files changed

+84
-7
lines changed

4 files changed

+84
-7
lines changed

components/arduino_tinyusb/CMakeLists.txt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ if(CONFIG_TINYUSB_ENABLED)
2525

2626
set(srcs
2727
# espressif:
28-
"${COMPONENT_DIR}/src/dcd_dwc2.c"
28+
#"${COMPONENT_DIR}/src/dcd_dwc2.c"
2929
# tusb:
30-
#"${COMPONENT_DIR}/tinyusb/src/portable/synopsys/dwc2/dcd_dwc2.c"
30+
"${COMPONENT_DIR}/tinyusb/src/portable/synopsys/dwc2/dcd_dwc2.c"
3131
"${COMPONENT_DIR}/tinyusb/src/portable/synopsys/dwc2/dwc2_common.c"
32+
"${COMPONENT_DIR}/tinyusb/src/portable/synopsys/dwc2/hcd_dwc2.c"
33+
# common
34+
"${COMPONENT_DIR}/tinyusb/src/tusb.c"
35+
"${COMPONENT_DIR}/tinyusb/src/common/tusb_fifo.c"
36+
# device
37+
"${COMPONENT_DIR}/tinyusb/src/device/usbd_control.c"
38+
"${COMPONENT_DIR}/tinyusb/src/device/usbd.c"
3239
"${COMPONENT_DIR}/tinyusb/src/class/cdc/cdc_device.c"
3340
"${COMPONENT_DIR}/tinyusb/src/class/hid/hid_device.c"
3441
"${COMPONENT_DIR}/tinyusb/src/class/midi/midi_device.c"
@@ -38,16 +45,25 @@ if(CONFIG_TINYUSB_ENABLED)
3845
"${COMPONENT_DIR}/tinyusb/src/class/dfu/dfu_device.c"
3946
"${COMPONENT_DIR}/tinyusb/src/class/vendor/vendor_device.c"
4047
"${COMPONENT_DIR}/tinyusb/src/class/net/ncm_device.c"
41-
"${COMPONENT_DIR}/tinyusb/src/common/tusb_fifo.c"
42-
"${COMPONENT_DIR}/tinyusb/src/device/usbd_control.c"
43-
"${COMPONENT_DIR}/tinyusb/src/device/usbd.c"
44-
"${COMPONENT_DIR}/tinyusb/src/tusb.c")
48+
"${COMPONENT_DIR}/tinyusb/src/class/net/ecm_rndis_device.c"
49+
"${COMPONENT_DIR}/tinyusb/src/class/usbtmc/usbtmc_device.c"
50+
# host
51+
"${COMPONENT_DIR}/tinyusb/src/host/usbh.c"
52+
"${COMPONENT_DIR}/tinyusb/src/host/hub.c"
53+
"${COMPONENT_DIR}/tinyusb/src/class/cdc/cdc_host.c"
54+
"${COMPONENT_DIR}/tinyusb/src/class/hid/hid_host.c"
55+
"${COMPONENT_DIR}/tinyusb/src/class/midi/midi_host.c"
56+
"${COMPONENT_DIR}/tinyusb/src/class/msc/msc_host.c"
57+
"${COMPONENT_DIR}/tinyusb/src/class/vendor/vendor_host.c"
58+
)
4559

4660
set(includes_private
4761
# tusb:
62+
#"${COMPONENT_DIR}/tinyusb/lib/networking/"
4863
"${COMPONENT_DIR}/tinyusb/hw/bsp/"
4964
"${COMPONENT_DIR}/tinyusb/src/"
5065
"${COMPONENT_DIR}/tinyusb/src/device"
66+
"${COMPONENT_DIR}/tinyusb/src/host"
5167
"${COMPONENT_DIR}/tinyusb/src/portable/synopsys/dwc2"
5268
)
5369

components/arduino_tinyusb/include/tusb_config.h

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ extern "C" {
7676
# define CONFIG_TINYUSB_NCM_ENABLED 0
7777
#endif
7878

79+
#if CONFIG_TINYUSB_ENABLED
80+
# define CFG_TUD_ENABLED 1
81+
#endif
82+
7983
/* */
8084
/* COMMON CONFIGURATION */
8185
/* */
@@ -84,6 +88,7 @@ extern "C" {
8488
#endif
8589
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
8690
#define CFG_TUSB_OS OPT_OS_FREERTOS
91+
#define BOARD_TUD_RHPORT 0
8792

8893
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
8994
* Tinyusb use follows macros to declare transferring memory so that they can be put
@@ -97,17 +102,23 @@ extern "C" {
97102
#endif
98103

99104
#ifndef CFG_TUSB_MEM_ALIGN
105+
#if CONFIG_IDF_TARGET_ESP32P4
106+
# define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(64)
107+
#else
100108
# define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4)
101109
#endif
110+
#endif
102111

103112
#if CONFIG_IDF_TARGET_ESP32P4
104113
#define CFG_TUD_MAX_SPEED OPT_MODE_HIGH_SPEED
105114
#else
106115
#define CFG_TUD_MAX_SPEED OPT_MODE_FULL_SPEED
107116
#endif
108117

118+
#define BOARD_TUD_MAX_SPEED CFG_TUD_MAX_SPEED
119+
109120
/* */
110-
/* DRIVER CONFIGURATION */
121+
/* DEVICE CONFIGURATION */
111122
/* */
112123

113124
#define CFG_TUD_MAINTASK_SIZE 4096
@@ -155,6 +166,35 @@ extern "C" {
155166
#define CFG_TUD_VENDOR_RX_BUFSIZE CONFIG_TINYUSB_VENDOR_RX_BUFSIZE
156167
#define CFG_TUD_VENDOR_TX_BUFSIZE CONFIG_TINYUSB_VENDOR_TX_BUFSIZE
157168

169+
/* */
170+
/* HOST CONFIGURATION */
171+
/* */
172+
173+
#define CFG_TUH_ENABLED CFG_TUD_ENABLED
174+
#define CFG_TUSB_RHPORT1_MODE OPT_MODE_HOST
175+
#define BOARD_TUH_RHPORT 1
176+
#define BOARD_TUH_MAX_SPEED CFG_TUD_MAX_SPEED
177+
#define CFG_TUH_ENUMERATION_BUFSIZE 256
178+
179+
#define CFG_TUH_HUB 2 // number of supported hubs
180+
#define CFG_TUH_CDC 1 // CDC ACM
181+
#define CFG_TUH_CDC_FTDI 1 // FTDI Serial. FTDI is not part of CDC class, only to re-use CDC driver API
182+
#define CFG_TUH_CDC_CP210X 1 // CP210x Serial. CP210X is not part of CDC class, only to re-use CDC driver API
183+
#define CFG_TUH_CDC_CH34X 1 // CH340 or CH341 Serial. CH34X is not part of CDC class, only to re-use CDC driver API
184+
#define CFG_TUH_HID 1 // typical keyboard + mouse device can have 3-4 HID interfaces
185+
#define CFG_TUH_MSC 1
186+
//#define CFG_TUH_VENDOR 3
187+
188+
#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1)
189+
190+
//------------- HID -------------//
191+
#define CFG_TUH_HID_EPIN_BUFSIZE 64
192+
#define CFG_TUH_HID_EPOUT_BUFSIZE 64
193+
194+
//------------- CDC -------------//
195+
#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0x03
196+
#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CODING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
197+
158198
#ifdef __cplusplus
159199
}
160200
#endif

patches/dwc2_esp32_h.diff

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
diff --git a/src/portable/synopsys/dwc2/dwc2_esp32.h b/src/portable/synopsys/dwc2/dwc2_esp32.h
2+
index 49b8c54cb..8b5e99741 100644
3+
--- a/src/portable/synopsys/dwc2/dwc2_esp32.h
4+
+++ b/src/portable/synopsys/dwc2/dwc2_esp32.h
5+
@@ -70,11 +70,13 @@ static void dwc2_int_handler_wrap(void* arg) {
6+
const tusb_role_t role = (tusb_role_t) tu_u16_high((uint16_t)(uintptr_t)arg);
7+
#if CFG_TUD_ENABLED
8+
if (role == TUSB_ROLE_DEVICE) {
9+
+ void dcd_int_handler(uint8_t rhport);
10+
dcd_int_handler(rhport);
11+
}
12+
#endif
13+
#if CFG_TUH_ENABLED && !CFG_TUH_MAX3421
14+
if (role == TUSB_ROLE_HOST) {
15+
+ void hcd_int_handler(uint8_t rhport, bool in_isr);
16+
hcd_int_handler(rhport, true);
17+
}
18+
#endif

tools/update-components.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ else
1414
git -C "$TINYUSB_REPO_DIR" fetch && \
1515
git -C "$TINYUSB_REPO_DIR" pull --ff-only
1616
fi
17+
cd $TINYUSB_REPO_DIR
18+
patch -p1 -N -i $AR_PATCHES/dwc2_esp32_h.diff
19+
cd -
1720
if [ $? -ne 0 ]; then exit 1; fi

0 commit comments

Comments
 (0)