Skip to content
Closed
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
1 change: 1 addition & 0 deletions ports/raspberrypi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
18 changes: 17 additions & 1 deletion ports/raspberrypi/common-hal/usb_host/Port.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 12 additions & 2 deletions ports/raspberrypi/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions supervisor/shared/usb/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions supervisor/shared/usb/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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
Expand Down