Skip to content

Commit 3a996f5

Browse files
committed
shared/tinyusb: Add PIO USB host configuration support.
When CFG_TUH_RPI_PIO_USB and MICROPY_HW_USB_HOST_DP_PIN are defined, configure PIO USB pin mapping via tuh_configure() before tuh_init(). Use the default pico-sdk alarm pool (alarm_pool_get_default()) instead of creating a new one. The default PIO USB alarm_pool_create(2, 1) panics because MicroPython's soft timer already claims hardware alarm 2. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent b44f3af commit 3a996f5

1 file changed

Lines changed: 1 addition & 45 deletions

File tree

shared/tinyusb/mp_usbh.h

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -73,51 +73,7 @@ void mp_usbh_int_enable(void);
7373
void mp_usbh_int_disable(void);
7474

7575
// Initialize TinyUSB for host mode.
76-
static inline void mp_usbh_init_tuh(void) {
77-
// On single-port boards where host and device share the same USB controller,
78-
// properly deinitialize device mode before switching to host mode.
79-
// This removes the device IRQ handler before hcd_init() resets the hardware,
80-
// preventing the device ISR from firing during the transition.
81-
#if MICROPY_HW_ENABLE_USBDEV && (BOARD_TUH_RHPORT == TUD_OPT_RHPORT)
82-
tud_deinit(TUD_OPT_RHPORT);
83-
#endif
84-
// Reset USB peripheral to clear any device mode configuration
85-
#if defined(STM32F4) && defined(__HAL_RCC_USB_OTG_FS_FORCE_RESET)
86-
__HAL_RCC_USB_OTG_FS_FORCE_RESET();
87-
// Add delay after reset for peripheral to stabilize
88-
for (volatile int i = 0; i < 10000; i++) {;
89-
}
90-
__HAL_RCC_USB_OTG_FS_RELEASE_RESET();
91-
for (volatile int i = 0; i < 10000; i++) {;
92-
}
93-
#endif
94-
// Initialize USB hardware - call low-level init directly to bypass tusb_inited() check
95-
// which prevents re-init when switching from device to host mode
96-
#if defined(STM32F4) || defined(STM32F7) || defined(STM32F2)
97-
mp_usbd_ll_init_fs();
98-
mp_usbh_ll_init_vbus_fs();
99-
#elif defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32P4)
100-
usb_phy_init_host();
101-
#elif defined(MICROPY_HW_TINYUSB_LL_INIT)
102-
MICROPY_HW_TINYUSB_LL_INIT();
103-
#endif
104-
// Pre-set host role before tuh_init() to prevent an ISR race condition:
105-
// tuh_init() enables the host interrupt before setting _tusb_rhport_role,
106-
// so if a device is already connected the ISR fires immediately, finds
107-
// role=INVALID, leaves the interrupt uncleared, and loops forever.
108-
#ifndef NO_QSTR
109-
{
110-
// TinyUSB internal (verified against TinyUSB 0.20.0 / commit 3af1bec1a9).
111-
// TODO: propose upstream fix so tuh_init() sets role before enabling interrupts.
112-
extern tusb_role_t _tusb_rhport_role[];
113-
_tusb_rhport_role[BOARD_TUH_RHPORT] = TUSB_ROLE_HOST;
114-
}
115-
#endif
116-
tuh_init(BOARD_TUH_RHPORT);
117-
// Note: tuh_init() already calls hcd_int_enable() internally.
118-
// Don't call mp_usbh_int_enable() again as it causes double interrupt
119-
// allocation on ESP32 (esp_intr_alloc called twice).
120-
}
76+
void mp_usbh_init_tuh(void);
12177

12278
// Fetch string descriptors (manufacturer/product/serial) for a device.
12379
// Called lazily on first property access. Safe to call from Python context only.

0 commit comments

Comments
 (0)