diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile index b75d542325166..728a517480eb2 100644 --- a/ports/raspberrypi/Makefile +++ b/ports/raspberrypi/Makefile @@ -549,6 +549,7 @@ SRC_C += \ ifeq ($(CIRCUITPY_USB_HOST), 1) SRC_C += \ lib/tinyusb/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c \ + lib/tinyusb/src/portable/raspberrypi/rp2040/hcd_rp2040.c \ lib/Pico-PIO-USB/src/pio_usb.c \ lib/Pico-PIO-USB/src/pio_usb_host.c \ lib/Pico-PIO-USB/src/usb_crc.c \ diff --git a/ports/raspberrypi/common-hal/usb_host/Port.c b/ports/raspberrypi/common-hal/usb_host/Port.c index b8a49725030ea..53bfeef8314db 100644 --- a/ports/raspberrypi/common-hal/usb_host/Port.c +++ b/ports/raspberrypi/common-hal/usb_host/Port.c @@ -32,6 +32,7 @@ usb_host_port_obj_t usb_host_instance; +#if CIRCUITPY_USB_DEVICE volatile bool _core1_ready = false; static void __not_in_flash_func(core1_main)(void) { @@ -109,16 +110,19 @@ static size_t get_usb_pio(void) { } mp_raise_RuntimeError(MP_ERROR_TEXT("All state machines in use")); } - +#endif usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm) { + #if CIRCUITPY_USB_DEVICE if ((dp->number + 1 != dm->number) && (dp->number - 1 != dm->number)) { raise_ValueError_invalid_pins(); } + #endif usb_host_port_obj_t *self = &usb_host_instance; // Return the singleton if given the same pins. + #if CIRCUITPY_USB_DEVICE if (self->dp != NULL) { if (self->dp != dp || self->dm != dm) { mp_raise_msg_varg(&mp_type_RuntimeError, MP_ERROR_TEXT("%q in use"), MP_QSTR_usb_host); @@ -170,6 +174,18 @@ usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp, tuh_configure(TUH_OPT_RHPORT, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_cfg); tuh_init(TUH_OPT_RHPORT); + #else + #if CIRCUITPY_USB_HOST + // init host stack on configured roothub port + tusb_rhport_init_t host_init = { + .role = TUSB_ROLE_HOST, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(0, &host_init); + + // tuh_init(0); + #endif + #endif return self; } diff --git a/ports/raspberrypi/supervisor/port.c b/ports/raspberrypi/supervisor/port.c index 66e63248c4810..48dcea6688791 100644 --- a/ports/raspberrypi/supervisor/port.c +++ b/ports/raspberrypi/supervisor/port.c @@ -552,10 +552,15 @@ void port_interrupt_after_ticks(uint32_t ticks) { void port_idle_until_interrupt(void) { #ifdef PICO_RP2040 common_hal_mcu_disable_interrupts(); - #if CIRCUITPY_USB_HOST + #if CIRCUITPY_USB_HOST && CIRCUITPY_USB_DEVICE if (!background_callback_pending() && !tud_task_event_ready() && !tuh_task_event_ready() && !_woken_up) { #else + #if CIRCUITPY_USB_HOST + if (!background_callback_pending() && !tuh_task_event_ready() && !_woken_up) { + #endif + #if CIRCUITPY_USB_DEVICE if (!background_callback_pending() && !tud_task_event_ready() && !_woken_up) { + #endif #endif __DSB(); __WFI(); @@ -571,10 +576,15 @@ void port_idle_until_interrupt(void) { uint32_t oldBasePri = __get_BASEPRI(); __set_BASEPRI(0); __isb(); - #if CIRCUITPY_USB_HOST + #if CIRCUITPY_USB_HOST && CIRCUITPY_USB_DEVICE if (!background_callback_pending() && !tud_task_event_ready() && !tuh_task_event_ready() && !_woken_up) { #else + #if CIRCUITPY_USB_HOST + if (!background_callback_pending() && !tuh_task_event_ready() && !_woken_up) { + #endif + #if CIRCUITPY_USB_DEVICE if (!background_callback_pending() && !tud_task_event_ready() && !_woken_up) { + #endif #endif __DSB(); __WFI(); diff --git a/supervisor/shared/usb/tusb_config.h b/supervisor/shared/usb/tusb_config.h index ac2fd9f4f258c..babd9c87ac3e9 100644 --- a/supervisor/shared/usb/tusb_config.h +++ b/supervisor/shared/usb/tusb_config.h @@ -150,9 +150,9 @@ extern "C" { #if CIRCUITPY_USB_HOST || CIRCUITPY_MAX3421E #define CFG_TUH_ENABLED 1 -// Always use PIO to do host on RP2. -#if !CIRCUITPY_MAX3421E -#define CFG_TUH_RPI_PIO_USB 1 +// Don't use RP2 PIO to do host on MAX3421E or when USB device is disabled +#if CIRCUITPY_MAX3421E || (CIRCUITPY_USB_HOST && !CIRCUITPY_USB_DEVICE) +#define CFG_TUH_RPI_PIO_USB 0 #else #define CFG_TUH_RPI_PIO_USB 1 #endif diff --git a/supervisor/shared/usb/usb.c b/supervisor/shared/usb/usb.c index e67c15d022cc9..b56a650180e01 100644 --- a/supervisor/shared/usb/usb.c +++ b/supervisor/shared/usb/usb.c @@ -117,9 +117,11 @@ void usb_init(void) { usb_hid_build_report_descriptor(); #endif + #if CIRCUITPY_USB_DEVICE // Only init device. Host gets inited by the `usb_host` module common-hal. tud_init(TUD_OPT_RHPORT); #endif + #endif post_usb_init(); @@ -181,7 +183,9 @@ void usb_setup_with_vm(void) { void usb_background(void) { if (usb_enabled()) { #if CFG_TUSB_OS == OPT_OS_NONE || CFG_TUSB_OS == OPT_OS_PICO + #if CIRCUITPY_USB_DEVICE tud_task(); + #endif #if CIRCUITPY_USB_HOST || CIRCUITPY_MAX3421E tuh_task(); #endif