From 1d29817139c473971ce239bbc2cb5924de4a644e Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 1 Mar 2022 23:55:53 +0700 Subject: [PATCH 01/62] start to add pio usb (host) support run as proof of concept --- examples/host/cdc_msc_hid/CMakeLists.txt | 2 +- examples/host/cdc_msc_hid/src/tusb_config.h | 7 +- hw/bsp/rp2040/family.c | 3 + hw/bsp/rp2040/family.cmake | 20 +- lib/Pico-PIO-USB | 1 + src/host/hcd.h | 13 +- src/host/usbh.c | 12 +- src/host/usbh_control.c | 9 +- src/portable/raspberrypi/pio/hcd_pio.c | 237 +++++++++++++++++++ src/portable/raspberrypi/rp2040/hcd_rp2040.c | 9 +- 10 files changed, 300 insertions(+), 13 deletions(-) create mode 160000 lib/Pico-PIO-USB create mode 100644 src/portable/raspberrypi/pio/hcd_pio.c diff --git a/examples/host/cdc_msc_hid/CMakeLists.txt b/examples/host/cdc_msc_hid/CMakeLists.txt index 0a99bc3a9..95c07339e 100644 --- a/examples/host/cdc_msc_hid/CMakeLists.txt +++ b/examples/host/cdc_msc_hid/CMakeLists.txt @@ -26,4 +26,4 @@ target_include_directories(${PROJECT} PUBLIC # Configure compilation flags and libraries for the example... see the corresponding function # in hw/bsp/FAMILY/family.cmake for details. -family_configure_host_example(${PROJECT}) \ No newline at end of file +family_configure_host_example(${PROJECT}) diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index bc6c68e5b..abbe474c2 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -42,7 +42,9 @@ #if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED) #else - #define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST + //#define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST + // rp2040: port0 is native, port 1 for PIO-USB + #define CFG_TUSB_RHPORT1_MODE OPT_MODE_HOST #endif #ifndef CFG_TUSB_OS @@ -71,6 +73,9 @@ // CONFIGURATION //-------------------------------------------------------------------- +#define CFG_TUH_RPI_PIO 1 +//#define CFG_TUSB_RPI_PIO_INC_PATH 1 + // Size of buffer to hold descriptors and other data used for enumeration #define CFG_TUH_ENUMERATION_BUFSIZE 256 diff --git a/hw/bsp/rp2040/family.c b/hw/bsp/rp2040/family.c index fa63dadf5..dcf38b6ec 100644 --- a/hw/bsp/rp2040/family.c +++ b/hw/bsp/rp2040/family.c @@ -127,6 +127,9 @@ void board_init(void) #ifndef BUTTON_BOOTSEL #endif + // Set the system clock to a multiple of 120mhz for bitbanging USB with pico-usb + set_sys_clock_khz(120000, true); + #if defined(UART_DEV) && defined(LIB_PICO_STDIO_UART) bi_decl(bi_2pins_with_func(UART_TX_PIN, UART_TX_PIN, GPIO_FUNC_UART)); uart_inst = uart_get_instance(UART_DEV); diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index 1aa180ef8..ddb34bf7d 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -89,13 +89,27 @@ if (NOT TARGET _rp2040_family_inclusion_marker) ${TOP}/src/class/hid/hid_host.c ${TOP}/src/class/msc/msc_host.c ${TOP}/src/class/vendor/vendor_host.c + + ${TOP}/src/portable/raspberrypi/pio/hcd_pio.c + ${TOP}/lib/Pico-PIO-USB/pio_usb.c + ${TOP}/lib/Pico-PIO-USB/usb_crc.c ) - # Sometimes have to do host specific actions in mostly - # common functions + # Sometimes have to do host specific actions in mostly common functions target_compile_definitions(tinyusb_host_base INTERFACE RP2040_USB_HOST_MODE=1 ) + + # config for host pio + target_link_libraries(tinyusb_host_base INTERFACE + hardware_dma + hardware_pio + pico_multicore + ) + + target_include_directories(tinyusb_host_base INTERFACE + ${TOP}/lib/Pico-PIO-USB + ) add_library(tinyusb_bsp INTERFACE) target_sources(tinyusb_bsp INTERFACE @@ -151,6 +165,8 @@ if (NOT TARGET _rp2040_family_inclusion_marker) _family_initialize_project(${PROJECT} ${DIR}) enable_language(C CXX ASM) pico_sdk_init() + pico_generate_pio_header(tinyusb_host_base ${TOP}/lib/Pico-PIO-USB/usb_tx.pio) + pico_generate_pio_header(tinyusb_host_base ${TOP}/lib/Pico-PIO-USB/usb_rx.pio) endfunction() # This method must be called from the project scope to suppress known warnings in TinyUSB source files diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB new file mode 160000 index 000000000..7d6729900 --- /dev/null +++ b/lib/Pico-PIO-USB @@ -0,0 +1 @@ +Subproject commit 7d672990011695892da523cb0ac29a2c7243a4c9 diff --git a/src/host/hcd.h b/src/host/hcd.h index 80500f048..9d10104f3 100644 --- a/src/host/hcd.h +++ b/src/host/hcd.h @@ -143,9 +143,20 @@ void hcd_device_close(uint8_t rhport, uint8_t dev_addr); // Endpoints API //--------------------------------------------------------------------+ -bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]); +// Open an endpoint bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc); + +// Submit a transfer, when complete hcd_event_xfer_complete() must be invoked bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen); + +// Submit a special transfer to send 8-byte Setup Packet, when complete hcd_event_xfer_complete() must be invoked +bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]); + +// Optional: some controllers are capable of carry out the whole Control Transfer (setup, data, status) +// without help of USBH. +bool hcd_edpt_control_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8], uint8_t* data) TU_ATTR_WEAK; + +// clear stall, data toggle is also reset to DATA0 bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr); //--------------------------------------------------------------------+ diff --git a/src/host/usbh.c b/src/host/usbh.c index 130aba40e..ea0a0809d 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -358,12 +358,12 @@ void tuh_task(void) case HCD_EVENT_DEVICE_ATTACH: // TODO due to the shared _usbh_ctrl_buf, we must complete enumerating // one device before enumerating another one. - TU_LOG2("USBH DEVICE ATTACH\r\n"); + TU_LOG2("[rh%d] USBH DEVICE ATTACH\r\n", event.rhport); enum_new_device(&event); break; case HCD_EVENT_DEVICE_REMOVE: - TU_LOG2("USBH DEVICE REMOVED\r\n"); + TU_LOG2("[rh%d] USBH DEVICE REMOVED\r\n", event.rhport); process_device_unplugged(event.rhport, event.connection.hub_addr, event.connection.hub_port); #if CFG_TUH_HUB @@ -703,7 +703,9 @@ static bool enum_new_device(hcd_event_t* event) if (_dev0.hub_addr == 0) { // wait until device is stable TODO non blocking + hcd_port_reset(_dev0.rhport); osal_task_delay(RESET_DELAY); + hcd_port_reset_end( _dev0.rhport); // device unplugged while delaying if ( !hcd_port_connect_status(_dev0.rhport) ) return true; @@ -772,13 +774,17 @@ static bool enum_get_addr0_device_desc_complete(uint8_t dev_addr, tusb_control_r TU_ASSERT( tu_desc_type(desc_device) == TUSB_DESC_DEVICE ); // Reset device again before Set Address + // reset port after 8 byte descriptor TU_LOG2("Port reset \r\n"); if (_dev0.hub_addr == 0) { +#if !CFG_TUH_RPI_PIO // skip this reset for pio-usb // connected directly to roothub - hcd_port_reset( _dev0.rhport ); // reset port after 8 byte descriptor + hcd_port_reset(_dev0.rhport); osal_task_delay(RESET_DELAY); + hcd_port_reset_end(_dev0.rhport); +#endif enum_request_set_addr(); } diff --git a/src/host/usbh_control.c b/src/host/usbh_control.c index d034eec7f..1c73d6626 100644 --- a/src/host/usbh_control.c +++ b/src/host/usbh_control.c @@ -71,7 +71,14 @@ bool tuh_control_xfer (uint8_t dev_addr, tusb_control_request_t const* request, TU_LOG2("\r\n"); // Send setup packet - TU_ASSERT( hcd_setup_send(rhport, dev_addr, (uint8_t const*) &_ctrl_xfer.request) ); + if ( hcd_edpt_control_xfer ) + { + _ctrl_xfer.stage = STAGE_ACK; + TU_ASSERT( hcd_edpt_control_xfer(rhport, dev_addr, (uint8_t const*) &_ctrl_xfer.request, buffer) ); + }else + { + TU_ASSERT( hcd_setup_send(rhport, dev_addr, (uint8_t const*) &_ctrl_xfer.request) ); + } return true; } diff --git a/src/portable/raspberrypi/pio/hcd_pio.c b/src/portable/raspberrypi/pio/hcd_pio.c new file mode 100644 index 000000000..74df49e0d --- /dev/null +++ b/src/portable/raspberrypi/pio/hcd_pio.c @@ -0,0 +1,237 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2021 Ha Thach (tinyusb.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * This file is part of the TinyUSB stack. + */ + +#include "tusb_option.h" + +#if CFG_TUH_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && CFG_TUH_RPI_PIO + +#include "pico.h" +#include "hardware/pio.h" +#include "pio_usb.h" + +//--------------------------------------------------------------------+ +// INCLUDE +//--------------------------------------------------------------------+ +#include "osal/osal.h" + +#include "host/hcd.h" +#include "host/usbh.h" + +#define RHPORT_OFFSET 1 +#define RHPORT_PIO(_x) ((_x)-RHPORT_OFFSET) + + +static usb_device_t *_test_usb_device = NULL; +static pio_usb_configuration_t pio_host_config = PIO_USB_DEFAULT_CONFIG; + +extern root_port_t root_port[PIO_USB_ROOT_PORT_CNT]; +extern usb_device_t usb_device[PIO_USB_DEVICE_CNT]; +extern pio_port_t pio_port[1]; +extern root_port_t root_port[PIO_USB_ROOT_PORT_CNT]; +extern endpoint_t ep_pool[PIO_USB_EP_POOL_CNT]; + + +extern port_pin_status_t get_port_pin_status( root_port_t *port); +extern void configure_fullspeed_host( pio_port_t *pp, const pio_usb_configuration_t *c, root_port_t *port); +extern void configure_lowspeed_host( pio_port_t *pp, const pio_usb_configuration_t *c, root_port_t *port); + +extern void control_setup_transfer( const pio_port_t *pp, uint8_t device_address, uint8_t *tx_data_address, uint8_t tx_data_len); +extern int control_in_protocol( usb_device_t *device, uint8_t *tx_data, uint16_t tx_length, uint8_t *rx_buffer, uint16_t request_length); +extern int control_out_protocol( usb_device_t *device, uint8_t *setup_data, uint16_t setup_length, uint8_t *out_data, uint16_t out_length); + +extern void update_packet_crc16(usb_setup_packet_t * packet); + +//--------------------------------------------------------------------+ +// HCD API +//--------------------------------------------------------------------+ +bool hcd_init(uint8_t rhport) +{ + // To run USB SOF interrupt in core1, create alarm pool in core1. + pio_host_config.alarm_pool = (void*)alarm_pool_create(2, 1); + _test_usb_device = pio_usb_host_init(&pio_host_config); + + return true; +} + +void hcd_port_reset(uint8_t rhport) +{ + rhport = RHPORT_PIO(rhport); + + pio_port_t *pp = &pio_port[0]; + root_port_t *root = &root_port[rhport]; + + pio_sm_set_pins_with_mask(pp->pio_usb_tx, pp->sm_tx, (0b00 << root->pin_dp), + (0b11u << root->pin_dp)); + pio_sm_set_pindirs_with_mask(pp->pio_usb_tx, pp->sm_tx, (0b11u << root->pin_dp), + (0b11u << root->pin_dp)); +} + +void hcd_port_reset_end(uint8_t rhport) +{ + rhport = RHPORT_PIO(rhport); + + pio_port_t *pp = &pio_port[0]; + root_port_t *root = &root_port[rhport]; + + pio_sm_set_pindirs_with_mask(pp->pio_usb_tx, pp->sm_tx, (0b00u << root->pin_dp), + (0b11u << root->pin_dp)); + + busy_wait_us(100); + + // TODO slow speed + bool fullspeed_flag = true; + + if (fullspeed_flag && get_port_pin_status(root) == PORT_PIN_FS_IDLE) { + root->root_device = &usb_device[0]; + if (!root->root_device->connected) { + configure_fullspeed_host(pp, &pio_host_config, root); + root->root_device->is_fullspeed = true; + root->root_device->is_root = true; + root->root_device->connected = true; + root->root_device->root = root; + root->root_device->event = EVENT_CONNECT; + } + } else if (!fullspeed_flag && get_port_pin_status(root) == PORT_PIN_LS_IDLE) { + root->root_device = &usb_device[0]; + if (!root->root_device->connected) { + configure_lowspeed_host(pp, &pio_host_config, root); + root->root_device->is_fullspeed = false; + root->root_device->is_root = true; + root->root_device->connected = true; + root->root_device->root = root; + root->root_device->event = EVENT_CONNECT; + } + } +} + +bool hcd_port_connect_status(uint8_t rhport) +{ + root_port_t* port = &root_port[0]; + bool dp = gpio_get(port->pin_dp); + bool dm = gpio_get(port->pin_dm); + return dp || dm; +} + +tusb_speed_t hcd_port_speed_get(uint8_t rhport) +{ + // TODO determine link speed + return TUSB_SPEED_FULL; +} + +// Close all opened endpoint belong to this device +void hcd_device_close(uint8_t rhport, uint8_t dev_addr) +{ +} + +uint32_t hcd_frame_number(uint8_t rhport) +{ + return 0; +} + +void hcd_int_enable(uint8_t rhport) +{ +} + +void hcd_int_disable(uint8_t rhport) +{ +} + +//--------------------------------------------------------------------+ +// Endpoint API +//--------------------------------------------------------------------+ + +bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc) +{ + rhport = RHPORT_PIO(rhport); + + usb_device[0].event = EVENT_NONE; + usb_device[0].address = dev_addr; + + return true; +} + +bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen) +{ + (void) rhport; + + return true; +} + +bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]) +{ + (void) rhport; + return false; +} + +bool hcd_edpt_control_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8], uint8_t* data) +{ + int ret; + rhport = RHPORT_PIO(rhport); + + uint16_t const len = ((tusb_control_request_t const*) setup_packet)->wLength; + + usb_setup_packet_t pio_setup = { USB_SYNC, USB_PID_DATA0 }; + memcpy(&pio_setup.request_type, setup_packet, 8); + update_packet_crc16(&pio_setup); + + if (setup_packet[0] & TUSB_DIR_IN_MASK) + { + ret = control_in_protocol(&usb_device[0], (uint8_t*) &pio_setup, sizeof(pio_setup), data, len); + }else + { + ret = control_out_protocol(&usb_device[0], (uint8_t*) &pio_setup, sizeof(pio_setup), data, len); + } + + // TODO current pio is blocking + hcd_event_xfer_complete(dev_addr, 0, 0, ret ? XFER_RESULT_FAILED : XFER_RESULT_SUCCESS, false); + + return ret == 0; +} + + +//bool hcd_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) +//{ +// // EPX is shared, so multiple device addresses and endpoint addresses share that +// // so if any transfer is active on epx, we are busy. Interrupt endpoints have their own +// // EPX so ep->active will only be busy if there is a pending transfer on that interrupt endpoint +// // on that device +// pico_trace("hcd_edpt_busy dev addr %d ep_addr 0x%x\n", dev_addr, ep_addr); +// struct hw_endpoint *ep = get_dev_ep(dev_addr, ep_addr); +// assert(ep); +// bool busy = ep->active; +// pico_trace("busy == %d\n", busy); +// return busy; +//} + +bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr) +{ + (void) dev_addr; + (void) ep_addr; + + return true; +} + +#endif diff --git a/src/portable/raspberrypi/rp2040/hcd_rp2040.c b/src/portable/raspberrypi/rp2040/hcd_rp2040.c index b6fbdb200..75acefb65 100644 --- a/src/portable/raspberrypi/rp2040/hcd_rp2040.c +++ b/src/portable/raspberrypi/rp2040/hcd_rp2040.c @@ -27,7 +27,7 @@ #include "tusb_option.h" -#if CFG_TUH_ENABLED && CFG_TUSB_MCU == OPT_MCU_RP2040 +#if CFG_TUH_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && !CFG_TUH_RPI_PIO #include "pico.h" #include "rp2040_usb.h" @@ -40,7 +40,8 @@ #include "host/hcd.h" #include "host/usbh.h" -#define ROOT_PORT 0 +// port 0 is native USB port, other is counted as software PIO +#define RHPORT_NATIVE 0 //--------------------------------------------------------------------+ // Low level rp2040 controller functions @@ -185,11 +186,11 @@ static void hcd_rp2040_irq(void) if (dev_speed()) { - hcd_event_device_attach(ROOT_PORT, true); + hcd_event_device_attach(RHPORT_NATIVE, true); } else { - hcd_event_device_remove(ROOT_PORT, true); + hcd_event_device_remove(RHPORT_NATIVE, true); } // Clear speed change interrupt From df4c614597157b540e0936a4555c6a1ba3ac487c Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Mar 2022 00:00:11 +0700 Subject: [PATCH 02/62] update pio usb submodule --- .gitmodules | 3 +++ lib/Pico-PIO-USB | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitmodules b/.gitmodules index fb551060a..9a5d89d00 100644 --- a/.gitmodules +++ b/.gitmodules @@ -140,3 +140,6 @@ [submodule "hw/mcu/allwinner"] path = hw/mcu/allwinner url = https://github.com/hathach/allwinner_driver.git +[submodule "lib/Pico-PIO-USB"] + path = lib/Pico-PIO-USB + url = https://github.com/hathach/Pico-PIO-USB.git diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 7d6729900..ee20881ac 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 7d672990011695892da523cb0ac29a2c7243a4c9 +Subproject commit ee20881ac8abc515df5e0cfe8851c7c21c47a0ef From d3ed76902eaf52a61f19d88c941045ea1fc5b808 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Mar 2022 00:06:13 +0700 Subject: [PATCH 03/62] clean up --- examples/host/cdc_msc_hid/src/tusb_config.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index abbe474c2..e957b2cde 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -39,12 +39,17 @@ #error CFG_TUSB_MCU must be defined #endif +#define CFG_TUH_RPI_PIO 1 +//#define CFG_TUSB_RPI_PIO_INC_PATH 1 + + #if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED) +#elif CFG_TUH_RPI_PIO + // rp2040: port0 is native, port 1 for PIO-USB + #define CFG_TUSB_RHPORT1_MODE OPT_MODE_HOST #else - //#define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST - // rp2040: port0 is native, port 1 for PIO-USB - #define CFG_TUSB_RHPORT1_MODE OPT_MODE_HOST + #define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST #endif #ifndef CFG_TUSB_OS @@ -73,9 +78,6 @@ // CONFIGURATION //-------------------------------------------------------------------- -#define CFG_TUH_RPI_PIO 1 -//#define CFG_TUSB_RPI_PIO_INC_PATH 1 - // Size of buffer to hold descriptors and other data used for enumeration #define CFG_TUH_ENUMERATION_BUFSIZE 256 From 918f3e9caec1e5566feac9214b7142b950a3d27c Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Mar 2022 12:22:20 +0700 Subject: [PATCH 04/62] minor rename --- examples/host/cdc_msc_hid/src/tusb_config.h | 4 ++-- src/host/usbh.c | 2 +- src/portable/raspberrypi/pio/hcd_pio.c | 2 +- src/portable/raspberrypi/rp2040/hcd_rp2040.c | 2 +- src/tusb_option.h | 11 ++++++++--- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index e957b2cde..80ee8cb01 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -39,13 +39,13 @@ #error CFG_TUSB_MCU must be defined #endif -#define CFG_TUH_RPI_PIO 1 +#define CFG_TUH_RPI_PIO_USB 1 //#define CFG_TUSB_RPI_PIO_INC_PATH 1 #if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED) -#elif CFG_TUH_RPI_PIO +#elif CFG_TUH_RPI_PIO_USB // rp2040: port0 is native, port 1 for PIO-USB #define CFG_TUSB_RHPORT1_MODE OPT_MODE_HOST #else diff --git a/src/host/usbh.c b/src/host/usbh.c index ea0a0809d..1f2a19527 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -779,7 +779,7 @@ static bool enum_get_addr0_device_desc_complete(uint8_t dev_addr, tusb_control_r if (_dev0.hub_addr == 0) { -#if !CFG_TUH_RPI_PIO // skip this reset for pio-usb +#if !CFG_TUH_RPI_PIO_USB // skip this reset for pio-usb // connected directly to roothub hcd_port_reset(_dev0.rhport); osal_task_delay(RESET_DELAY); diff --git a/src/portable/raspberrypi/pio/hcd_pio.c b/src/portable/raspberrypi/pio/hcd_pio.c index 74df49e0d..71645bd45 100644 --- a/src/portable/raspberrypi/pio/hcd_pio.c +++ b/src/portable/raspberrypi/pio/hcd_pio.c @@ -26,7 +26,7 @@ #include "tusb_option.h" -#if CFG_TUH_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && CFG_TUH_RPI_PIO +#if CFG_TUH_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && CFG_TUH_RPI_PIO_USB #include "pico.h" #include "hardware/pio.h" diff --git a/src/portable/raspberrypi/rp2040/hcd_rp2040.c b/src/portable/raspberrypi/rp2040/hcd_rp2040.c index 75acefb65..53b372fcb 100644 --- a/src/portable/raspberrypi/rp2040/hcd_rp2040.c +++ b/src/portable/raspberrypi/rp2040/hcd_rp2040.c @@ -27,7 +27,7 @@ #include "tusb_option.h" -#if CFG_TUH_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && !CFG_TUH_RPI_PIO +#if CFG_TUH_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && !CFG_TUH_RPI_PIO_USB #include "pico.h" #include "rp2040_usb.h" diff --git a/src/tusb_option.h b/src/tusb_option.h index 25e536787..44cdaa3a6 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -252,7 +252,7 @@ //--------------------------------------------------------------------+ -// COMMON OPTIONS +// Common Options (Default) //--------------------------------------------------------------------+ // Debug enable to print out error message @@ -280,7 +280,7 @@ #endif //-------------------------------------------------------------------- -// DEVICE OPTIONS +// Device Options (Default) //-------------------------------------------------------------------- #ifndef CFG_TUD_ENDPOINT0_SIZE @@ -345,7 +345,7 @@ #endif //-------------------------------------------------------------------- -// HOST OPTIONS +// Host Options (Default) //-------------------------------------------------------------------- #if CFG_TUH_ENABLED #ifndef CFG_TUH_DEVICE_MAX @@ -383,6 +383,11 @@ #define CFG_TUH_VENDOR 0 #endif +// Enable PIO-USB software host controller +#ifndef CFG_TUH_RPI_PIO_USB +#define CFG_TUH_RPI_PIO_USB 0 +#endif + //------------------------------------------------------------------ // Configuration Validation //------------------------------------------------------------------ From 635fb9dcdd08c736a4b0161381068b51ef4c140e Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Mar 2022 12:33:47 +0700 Subject: [PATCH 05/62] try to fix ci --- src/portable/ehci/ehci.c | 4 ++-- src/portable/mentor/musb/hcd_musb.c | 5 +++++ src/portable/ohci/ohci.c | 5 +++++ src/portable/raspberrypi/rp2040/hcd_rp2040.c | 5 +++++ src/portable/renesas/usba/hcd_usba.c | 5 +++++ 5 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/portable/ehci/ehci.c b/src/portable/ehci/ehci.c index e2d51dc4b..31e348360 100644 --- a/src/portable/ehci/ehci.c +++ b/src/portable/ehci/ehci.c @@ -160,15 +160,15 @@ void hcd_port_reset(uint8_t rhport) regs->portsc = portsc; } -#if 0 void hcd_port_reset_end(uint8_t rhport) { (void) rhport; +#if 0 ehci_registers_t* regs = ehci_data.regs; regs->portsc_bm.port_reset = 0; -} #endif +} bool hcd_port_connect_status(uint8_t rhport) { diff --git a/src/portable/mentor/musb/hcd_musb.c b/src/portable/mentor/musb/hcd_musb.c index 3bfd65b45..85e18e3aa 100644 --- a/src/portable/mentor/musb/hcd_musb.c +++ b/src/portable/mentor/musb/hcd_musb.c @@ -617,6 +617,11 @@ void hcd_port_reset(uint8_t rhport) _hcd.need_reset = false; } +void hcd_port_reset_end(uint8_t rhport) +{ + (void) rhport; +} + tusb_speed_t hcd_port_speed_get(uint8_t rhport) { (void)rhport; diff --git a/src/portable/ohci/ohci.c b/src/portable/ohci/ohci.c index cb4e6dc4a..db087716f 100644 --- a/src/portable/ohci/ohci.c +++ b/src/portable/ohci/ohci.c @@ -216,6 +216,11 @@ void hcd_port_reset(uint8_t hostid) OHCI_REG->rhport_status[0] = RHPORT_PORT_RESET_STATUS_MASK; } +void hcd_port_reset_end(uint8_t rhport) +{ + (void) rhport; +} + bool hcd_port_connect_status(uint8_t hostid) { (void) hostid; diff --git a/src/portable/raspberrypi/rp2040/hcd_rp2040.c b/src/portable/raspberrypi/rp2040/hcd_rp2040.c index 53b372fcb..7bbf00efe 100644 --- a/src/portable/raspberrypi/rp2040/hcd_rp2040.c +++ b/src/portable/raspberrypi/rp2040/hcd_rp2040.c @@ -387,6 +387,11 @@ void hcd_port_reset(uint8_t rhport) // TODO: Nothing to do here yet. Perhaps need to reset some state? } +void hcd_port_reset_end(uint8_t rhport) +{ + (void) rhport; +} + bool hcd_port_connect_status(uint8_t rhport) { pico_trace("hcd_port_connect_status\n"); diff --git a/src/portable/renesas/usba/hcd_usba.c b/src/portable/renesas/usba/hcd_usba.c index ebb682342..5246ecb94 100644 --- a/src/portable/renesas/usba/hcd_usba.c +++ b/src/portable/renesas/usba/hcd_usba.c @@ -620,6 +620,11 @@ void hcd_port_reset(uint8_t rhport) _hcd.need_reset = false; } +void hcd_port_reset_end(uint8_t rhport) +{ + (void) rhport; +} + tusb_speed_t hcd_port_speed_get(uint8_t rhport) { (void)rhport; From d4d7d35afbfe831b20ba83c2689c678361bfb709 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Mar 2022 13:24:51 +0700 Subject: [PATCH 06/62] checkout pio-usb --- .github/workflows/build_arm.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build_arm.yml b/.github/workflows/build_arm.yml index 177f1076e..085274eb5 100644 --- a/.github/workflows/build_arm.yml +++ b/.github/workflows/build_arm.yml @@ -82,6 +82,7 @@ jobs: run: | git clone --depth 1 -b develop https://github.com/raspberrypi/pico-sdk ~/pico-sdk echo >> $GITHUB_ENV PICO_SDK_PATH=~/pico-sdk + git submodule update --init lib/Pico-PIO-USB - name: Set Toolchain URL run: echo >> $GITHUB_ENV TOOLCHAIN_URL=https://github.com/xpack-dev-tools/arm-none-eabi-gcc-xpack/releases/download/v10.2.1-1.1/xpack-arm-none-eabi-gcc-10.2.1-1.1-linux-x64.tar.gz From fa62d5abc9b1cede44a8e0630d8a28c90cdcdd00 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 3 Mar 2022 21:57:29 +0700 Subject: [PATCH 07/62] got interrupt endpoint working --- lib/Pico-PIO-USB | 2 +- src/portable/raspberrypi/pio/hcd_pio.c | 41 ++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index ee20881ac..92268187f 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit ee20881ac8abc515df5e0cfe8851c7c21c47a0ef +Subproject commit 92268187f3b0490a43d139ed2cb598b06258011f diff --git a/src/portable/raspberrypi/pio/hcd_pio.c b/src/portable/raspberrypi/pio/hcd_pio.c index 71645bd45..8d1903f85 100644 --- a/src/portable/raspberrypi/pio/hcd_pio.c +++ b/src/portable/raspberrypi/pio/hcd_pio.c @@ -167,8 +167,45 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const { rhport = RHPORT_PIO(rhport); - usb_device[0].event = EVENT_NONE; - usb_device[0].address = dev_addr; + usb_device_t *device = &usb_device[0]; + + static uint8_t ep_id_idx; // TODO remove later + + if (ep_desc->bEndpointAddress == 0) + { + device->event = EVENT_NONE; + device->address = dev_addr; + + ep_id_idx = 0; + }else if (ep_desc->bmAttributes.xfer == TUSB_XFER_INTERRUPT) // only support interrupt endpoint + { + endpoint_t *ep = NULL; + for ( int ep_pool_idx = 0; ep_pool_idx < PIO_USB_EP_POOL_CNT; ep_pool_idx++ ) + { + if ( ep_pool[ep_pool_idx].ep_num == 0 ) + { + ep = &ep_pool[ep_pool_idx]; + device->endpoint_id[ep_id_idx] = ep_pool_idx + 1; + ep_id_idx++; + + ep->data_id = 0; + ep->ep_num = ep_desc->bEndpointAddress; + ep->interval = ep_desc->bInterval; + ep->interval_counter = 0; + ep->size = (uint8_t) tu_edpt_packet_size(ep_desc); + ep->attr = EP_ATTR_INTERRUPT; + + break; + } + } + +// TU_LOG1_INT(device->connected); +// TU_LOG1_INT(device->root); +// TU_LOG1_INT(device->is_root); +// TU_LOG1_INT(device->is_fullspeed); +// +// TU_LOG1_INT(ep_id_idx); + } return true; } From c0195fc6270394a489faf29fe8fc42dff48bcff1 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 4 Mar 2022 14:48:29 +0700 Subject: [PATCH 08/62] skip pyocd reset after flash --- examples/rules.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/rules.mk b/examples/rules.mk index 97f6956fa..538dffbcf 100644 --- a/examples/rules.mk +++ b/examples/rules.mk @@ -199,7 +199,7 @@ flash-xfel: $(BUILD)/$(PROJECT)-sunxi.bin PYOCD_OPTION ?= flash-pyocd: $(BUILD)/$(PROJECT).hex pyocd flash -t $(PYOCD_TARGET) $(PYOCD_OPTION) $< - pyocd reset -t $(PYOCD_TARGET) + #pyocd reset -t $(PYOCD_TARGET) # Flash using openocd OPENOCD_OPTION ?= From 735300b53cf519b58576b3746ec48ae163df327f Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 28 Mar 2022 12:47:32 +0700 Subject: [PATCH 09/62] remove cxd56 __pycache-_ folder --- .../tools/__pycache__/xmodem.cpython-36.pyc | Bin 15257 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 hw/mcu/sony/cxd56/tools/__pycache__/xmodem.cpython-36.pyc diff --git a/hw/mcu/sony/cxd56/tools/__pycache__/xmodem.cpython-36.pyc b/hw/mcu/sony/cxd56/tools/__pycache__/xmodem.cpython-36.pyc deleted file mode 100644 index cfa917f71d3dbe945ac710d5b292db2daa36426e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 15257 zcmeHO2Y4LSwVtxOT8&o4w%iR2V6Ya{ZNMl-*p_85*s>#uwv_c~&q!Kni!?K9Nt;zd zA`?s!6Q_rS^g>AQJ*0>9O4_6Y2~m1NDoODB&z;#K*~agSpWpYr?|UoHKX+zsJ@>!& zo^$T?#@bq~Wgxi!b9*J}A*tdQKzJ)I+XavmlSq!qq>wtI#1sm|R0_rd6r!qBFcuW| zP%I?wRk14E!>Mq(I#w-9;++=L%-~oJRnN(>h*?Y8h>TDjLNy{(k5EK}8W5@#p(O~_ ziO^Dn>P2W7LJcC+h|m%dT7HE@OKI6biI$lw=G53qv)WuqjkNr*5?e(pXyrjEw%S}n ztHu@UJ7(3`T66WBG$*5GRRdCVwJ~2ON!n#k{L}R7_w3!#yGI{RrcB+jjEp^OTKa^Q zb+UZHlF)>eJ}E?Tc?gQH1X$3H_)B^}c;QhG8ykuuX}#xa~^Hlt^U{jypIuOEyz z`wtygi$~qakj7Qn)6?JMN3e4f6IsjAhqIP`gI7z$(qmo_MNOC)N@hk{i(l>0Y%WFG zkf{$FNh>v@n}$83JK1tgbOUvCylRdbiE$nEakG zZRO{R&z?joBY1C0QHmleM=8G1v>hl-A5LYbbaUEZ`RmC{B9&t?>852tl9rjs9x$z$ zlXYzcZ#3;n(?U+%xwe<9FEDPLD6Sb&5}ko48(W4_S;!W`u#FM3RoexLi&{WBEM{ja ztEaQJqbIWIGG%!w_U-8H=g`T2{k=WCyRPl+_fnobz87OlON-vO?P{;Iy;th(9s15*ojdnNRQ6__ z+qXEcmX>Z2fARYhJ#!flX}OF}edmtTUB*UzXV2*_W0Ssf`{^#@B7NsJzl`>F^vAwE zy?b}wxS(rJr9XQ24xXsn7QFvY%kY`s(lPFHzFEiU!Js7b1nlP_@xMI+dOU6pUcZ3< z?VXz-aMYHsir;@K3(jJI%c$W0X)B|G|Lv8;sc6!vwBxk%W>0b&Gjj*z!z;F4nmM=g zjDMn;vpAjpKYvc8ZwGiCsNj#7Y>piU`YD%DF@IK4l$V`iai>zoY3Hb~D{eQO#Hc>V zDs4Jx+pK~PKqKLMQ-@I4qE9f_6**hHpCWfF=Bbkgd13C+n^-x*80)fzqUANZUEjGA zif6Vpq4!<08_K}OieB+@K9#;XY2Ew?qKf5OE!IwoG^xn$RB9!dkJ)*F=5RX7!00M! z4*%DfU(^;)_votV(vt=ku|#m!V{PSJQkl(v$`Vb>pL<<$EW>Nko-C1RVd&^v(nj9g zIx*vnwr5(i)<}1LeNT2`#!8NiIv8J3y|cZeT|XvW{_KUZIxgMZq4$jDVAAZ&TK14> zjl|SFy9V>Y_STK9o%8S`NPSV+t(q__CuyYI&@?Mj>3JC+<`o8ZI?RU{8W@%{tYYW^ zByg2~NVFACb^!@Z39bnVZV4G~3AiXIK*A+KVX7vLYA8Z*B~U#z&=PhPuycSN1GI`( z(;8Y!>*x$Rlg=WY&ZcwdTv|`((fPE2nkY&a(1p}YE!0YF)J`4LNgHVsT|^htCA66? zr7n6BZK2Dkn=YrVw2ih?5AC2{+DTW?m9&elqN`~)?V&!}OZ(`_)K3F6NY~J{bRAt! zH&Bdjq?_nwx`pDjp9~tJ1fgeWm_}%nk~Bu+l%h0cC`%JGNfz1UP>v4J6iw3%<>@I@ zpjmn`{?8J3Hl^`iat%Bq0iFi=zjVYA>3Evs>M}@=PKOS;97!fDV{mrT3pL;HR8D%_jS0I<641d4fn`^)N}@z z=!QHK*|-I46!X1rGVk*pMEWR(T*|bX;cu8Q6ENX1?y^H;2(shFG7Zv)t!!FoiXeiMTiGeoM(aV?rxpTVK(j)O?EPGHtS9@ZDw=MWgXEj5v3Jw6#iMJle03yxxyBI zUp8YFzc0vYB8RNlOL5YyxV#f)CC0OUneQNK;tDqFoOe|7s@{dMZ=^-dyY!v8OhUN4 zgajPROdcpYk33oEHeMQOQPVDGX2SFnCX7_d7)p6f@w4Po7G^2DbZNM;Jbx4lfm>#A zhF-GcWyo#m@>^1J^m(QqOV+E87YVt-$>LShYv6=IUHU$QghMGYibf~k?BlLZ8{_Co zcv&XkS2IC`w9voGZ*-P$MUzEVoP{Sri)YK^*_a=b+jT&q9dHm+Ve$?80H50rcw_<) zemWrfJit}20gS&3@SINq-u4Z^SAPch{T~4xO92hwZPM-p^q&KGUq9f5+W~*G0N)t` zT>1jQsyV<@-UEoe4)C#W0q*`Z;OL`(A3p>bS`X;i1kf>^C*?-KF9>kI19<%oz-@zo zu{Qv&ejlLeHbC`@0KfY=;A?*Yy!Eqy!`}uBTnw zoq!uZ0Lb41Sp5>f=Gy^3`5WNzM*y$*F5sj013q&l;OEx^o;eD*XBx0I3^1_LB$Xz> zxtjrB{sQ2)-v``@r7QW4e*mt$1CYE6pxq0&@I!#V!~hRm1^7T7@ZvGRK4HM z2w>Yfz}tTdc<#RezxfK_D?b8U@HRlr#{i>;0lQuSc*#=%@6Q0feGA~veSl@%fX-IH zlg|W9zyqFq)ltBGzX$y2CxCx_4Y2m(fUb7{Zg>@7`niBZOsRXBTwi2L{hG-YVM?_y zx%!w=Sti%5py)h4O36<#MIU0a2AHDfF+{j zrqnG=u6HSbBTUvu&jEas$$HUtKqHfNY6x&Gll50jsV_0P-o%u;gUOX)O6_HGH8Z7Z znOqMtML)eB@M@;$(*uBErsx#}SkDv8NvuDfmre2~c%e+2MUCf6DF1GX@^ zzQq*%6O;9MOwo5TS+8S?9$>OAXNqoOvhHR|rI}oHOsO^|*XNm1^GvR1F{R$ZnE6^KVz~!gDLttChIIy>Lw=FoRWZ>brNHU zw~|U*Io{4r&$hneUI54CpICA@RHg&sBJSX(3=g%L!AJQ$40p_%NhfsJ5O@)dSe} zoUP6@IyGYv(wy3YHYX3uE5x>A0o$XONV8Ris)8~ew6>$R#*?8f`$&I-;;#98jFs9h!T)@$({ z=ZM%`+I#&p^;CEHf;i5qLOs=PExpS$6k}H8gdQlUs5$z;SuJYzc%eqLtj1YW2s&%W z*3EH!|5XSUB88el)to9?FM8{UVht8$TM=8aE~t#{Nga7yjPgrzhb+&adTs^xz!7=! z;>@7vy9UuRzsDBjdh35P*Cmy?E-EuZON(C%wG>*3E$kK2Z0+R3*GbdY3i+HV$`{;R zAQ{E_J9yb#Y_H!c#d=^-31?7av4kVa zoLSxn>=z~s?{jL`;F`2^iG*nn=ThEge9?0$b~SezDVrlEGkU_XVXX|8T8a(t zRL;U)Yw3+|K6v%$(g&fL`^_qwXIxXyhGM&iHh!~ck=TtFTe6sxd6zzAQmNFUJiWVTM(9H(78y4cwL&XB0hlmQcp&lVW4CT++b67@ z=?sH(aYahS9j7XjxuofI>AQyY_GUhPAh?2a2$^AsB#T~wyUK!DiwGSe0%akuOxBNy z)oVsXUR!;y8ymynvDn(8l|q50^~TQNw`|FE-{+*lvg^g);@* zb~`zeF<^U}sBrPfN6t2xu#n}6b0}`u+c*u9v%z&3rB3TFg%s!9zm)!XwU9Fu}yut35^m zpQZ2~Dh7iXHpLveL|!Fxo(q`26>EEvHJYd*^gTE1HN0ayziwLwy?nq(B}vR9`V=~a zkHDmoCJC*?Vm%@s!2u3-D3H!diR=b=SnkZ9E5>qffB)Wo-xY%xa0<6!&!$-)0h!LT zqq4P@v#8;?+?j9|&qINg=2eg*@;FlQ6Ic6{S zf~#F?q;g(Z=(nzlnn7pl@3B^+U2aeeI9Ii2Y&YbMHdjq$N8F&OsjFr!vRJ!z1MIJI z13WU_U}`LfnU6JOH;g__#1mG+t@T3wK@wj(#QubZ9S4Wp z8XToc!6k;hC)*A8_+Fk^)s$gn&=0N(8AKaA{Sq}~-w^aqo>N7Kxj`}1MK#2@c56Id z#@VIf)>M!YajZmK=>4Gtmz{D0>|=@5E+BBMb^*QJa8WUaoZ&2URphc*p@jjFp>IMQ z_I&{9EJfzOkQ|Zg-6X!OG zab@ukNK|pl@Z(-*YorI4{QyiblA%+}(5YqU)E|XbqC&%kr8p)P0;C8HR~>+1#+n** z-oRRETtZk_Cv&pxIO$#LuY@JAFf9DK4|M7B-Ds)0K_Pzgr4NJM1%J2Y2j!QYvHgIygdKR zUy#?sct*a^C~+9F^hOhJ&J-=H5$(~??y!SW5p#0^d0CyMk{>R>8*TSH5emar2+zq* z-B>+UABJ{YtdlRxhgp_$m=QQF>xq6ntC3E~fwl5k9I&i>UM0a6s4YZLwqo5~s4In# z0{V4uC3*xGOR&DcaaJKUc1u>nUZI+9Njxp#ru`Cx`wSkYQP-4d#YB~e?b8&>_AGZzH|Cm)1#A?4_D z1IJj)+H$|QuwXUzHNO8Lg?Z{^3Nu!;1IJaEJ2!6XENiqyg_&K-WsS~rf|nM>k_v5m zCjoh9;3O(Dugv@ti>$v!_6cW%H#3}tNkA;xc_@mB1Nx#x7PEgF=5MjcBK6sbZJon# zE+D^@&rCF(*S=|5-_ot~J(@2cL~JS^NkoiJKSt+t)LPF;{dHVuUBx4)=V5sii#fHO zNAr&#J$iH=*HP;#F7WCiL|2IQ%yaZa@zxEng6anClxa>_O&m{nf-d3^TzisODJ}NB zSa85e7QAMX)y8lk13X`n)dFyXWDeyh382uX4W=-4WAg0lrfS25}f~v%7G4ZG7FOj$DE-nI@yx;?J4Iv%mrW1sW2;fps9>s;W$R}6Q!BTS}B5g&2h>ab+NQjbGqr+FB{sYHzqceCkQcY;JQakLAC|qpPA_566^M<|6{#L0b-{>m{3z}ByChj8k*@Jd<0c)zUU;R+)qc58u?S+>rsJ_QW08Gm}Uq6ENZkHV?G9 z85~2v5$2QhYpEX>^{;PqpL>X$BkX;Mhjb`+<&1OBVWH#SHaV?oi=dtr~Xlv%)O+;MpQ$jn7)i;ed4phvAl&e1~wbtR;#l-Xrpd-@UkSh^HEMdGr4S zh{s(m&Z>AW#oP@rdhKUe+mo0V@ zAHrp0<4Fpy48Xg5Dt~L#hEPLQ!_y?aVY;DKE!DlU6F+6&*b zl#*t>yL<=pULp2Y%d_y(k5!ylIUImKBaa2YZF>)hzT`ys1InE1(fpJJrxV!vzH%?v;mC_RWsf6sq~`%wc5~p|J?-qnVgr z0&AJF)h+T~^8|UXJwe{J$L1}zZO-Tu{K>bS2lIGDzi~@;LKuqOH|ty2JSp%3(Jfq& zOrBBzwtq0k|G^@!w50Q)mKL6ig;yberZ;(ceuO5^#n9~gSUSd|7*SR5br`vn;|4HU zPt0?F=9d)R4~8|8HasEn_K3Y3;F01EpA@r-14>;#@)-1SSP{WYfTY40o#?c?+e8eFPI$G=%*$*xsIQBTu=q0ZhpPqs$k{m}+=xgibf63Wh6v7?A9kf?pXbx&zl7 zm>1la#s6<}1N(OE>$S#_kCg)O(De@nW0N;IVWRn>RVA^y!8pIal&db5KGrtn#Z~4g zF~!d3!!0e|{2*38SD6~}=X_zlAj~R!OtJDVc#V!MO$O0AH;kDI$AWBAR3R4GD<-ae z;!RA$A)&Z8mARUT700|{d(^@9x8y3xtQ+7)dh3nYkmjWiTdb)pGUcM85zFCMA+H>_ z%AXwFfXHkN->gZ70}R5ZVK#Xt2+wLPSoSyz-W7!!XgRFXg_oW;)GcO}g;NYttQu3oZbXIC zj}0>G5{8QyxE*B^E~f6--Rqfp>;<)=05_BsNArZhV_|QV=olBxu&(0>0dqR(ROrTK rT7|cGs!TW2P{=F*%ZD9+u#SXV&vWdtowVRjg<8RXazqX92+RKk`88&w From e2f0aef93b246368e1f6bdf101604e73d252801a Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 30 Mar 2022 16:41:55 +0700 Subject: [PATCH 10/62] make use pio_usb_port_reset_start/end --- hw/bsp/rp2040/family.cmake | 1 + src/portable/raspberrypi/pio/hcd_pio.c | 10 ++-------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index 23ea893b7..b56ee6a6d 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -91,6 +91,7 @@ if (NOT TARGET _rp2040_family_inclusion_marker) ${TOP}/src/portable/raspberrypi/pio/hcd_pio.c ${TOP}/lib/Pico-PIO-USB/pio_usb.c + ${TOP}/lib/Pico-PIO-USB/pio_usb_port.c ${TOP}/lib/Pico-PIO-USB/usb_crc.c ) diff --git a/src/portable/raspberrypi/pio/hcd_pio.c b/src/portable/raspberrypi/pio/hcd_pio.c index 8d1903f85..e4a4e2450 100644 --- a/src/portable/raspberrypi/pio/hcd_pio.c +++ b/src/portable/raspberrypi/pio/hcd_pio.c @@ -53,8 +53,6 @@ extern pio_port_t pio_port[1]; extern root_port_t root_port[PIO_USB_ROOT_PORT_CNT]; extern endpoint_t ep_pool[PIO_USB_EP_POOL_CNT]; - -extern port_pin_status_t get_port_pin_status( root_port_t *port); extern void configure_fullspeed_host( pio_port_t *pp, const pio_usb_configuration_t *c, root_port_t *port); extern void configure_lowspeed_host( pio_port_t *pp, const pio_usb_configuration_t *c, root_port_t *port); @@ -83,10 +81,7 @@ void hcd_port_reset(uint8_t rhport) pio_port_t *pp = &pio_port[0]; root_port_t *root = &root_port[rhport]; - pio_sm_set_pins_with_mask(pp->pio_usb_tx, pp->sm_tx, (0b00 << root->pin_dp), - (0b11u << root->pin_dp)); - pio_sm_set_pindirs_with_mask(pp->pio_usb_tx, pp->sm_tx, (0b11u << root->pin_dp), - (0b11u << root->pin_dp)); + pio_usb_port_reset_start(root, pp); } void hcd_port_reset_end(uint8_t rhport) @@ -96,8 +91,7 @@ void hcd_port_reset_end(uint8_t rhport) pio_port_t *pp = &pio_port[0]; root_port_t *root = &root_port[rhport]; - pio_sm_set_pindirs_with_mask(pp->pio_usb_tx, pp->sm_tx, (0b00u << root->pin_dp), - (0b11u << root->pin_dp)); + pio_usb_port_reset_end(root, pp); busy_wait_us(100); From 95801bf5cc4afa27f9b54c33e74aa74689074a97 Mon Sep 17 00:00:00 2001 From: Rafael Silva Date: Wed, 30 Mar 2022 14:15:34 +0100 Subject: [PATCH 11/62] Update requirements.txt --- docs/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/requirements.txt b/docs/requirements.txt index 8ae9ae73b..15022e147 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,3 +1,4 @@ sphinx~=3.0 furo>=2020.12.30.b24 sphinx-autodoc-typehints>=1.10 +jinja2==3.0.3 From e94d11a5b39cae08f39722eaa5f6ac4d527a329b Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 31 Mar 2022 12:56:53 +0700 Subject: [PATCH 12/62] implement pio_usb_irq_handler --- lib/Pico-PIO-USB | 2 +- src/portable/raspberrypi/pio/hcd_pio.c | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 92268187f..4220c05dc 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 92268187f3b0490a43d139ed2cb598b06258011f +Subproject commit 4220c05dc6b7ab7b4db33e5e05bb66be7b6c7156 diff --git a/src/portable/raspberrypi/pio/hcd_pio.c b/src/portable/raspberrypi/pio/hcd_pio.c index e4a4e2450..254acf1d2 100644 --- a/src/portable/raspberrypi/pio/hcd_pio.c +++ b/src/portable/raspberrypi/pio/hcd_pio.c @@ -101,7 +101,7 @@ void hcd_port_reset_end(uint8_t rhport) if (fullspeed_flag && get_port_pin_status(root) == PORT_PIN_FS_IDLE) { root->root_device = &usb_device[0]; if (!root->root_device->connected) { - configure_fullspeed_host(pp, &pio_host_config, root); +// configure_fullspeed_host(pp, &pio_host_config, root); root->root_device->is_fullspeed = true; root->root_device->is_root = true; root->root_device->connected = true; @@ -111,7 +111,7 @@ void hcd_port_reset_end(uint8_t rhport) } else if (!fullspeed_flag && get_port_pin_status(root) == PORT_PIN_LS_IDLE) { root->root_device = &usb_device[0]; if (!root->root_device->connected) { - configure_lowspeed_host(pp, &pio_host_config, root); +// configure_lowspeed_host(pp, &pio_host_config, root); root->root_device->is_fullspeed = false; root->root_device->is_root = true; root->root_device->connected = true; @@ -217,6 +217,7 @@ bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet return false; } + bool hcd_edpt_control_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8], uint8_t* data) { int ret; @@ -242,7 +243,6 @@ bool hcd_edpt_control_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t const setup return ret == 0; } - //bool hcd_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) //{ // // EPX is shared, so multiple device addresses and endpoint addresses share that @@ -265,4 +265,24 @@ bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr) return true; } +// IRQ Handler +void pio_usb_irq_handler(uint8_t root_id) +{ + root_port_t* port = PIO_USB(root_id); + + if ( port->ints & PIO_USB_INTS_CONNECT_BITS ) + { + hcd_event_device_attach(root_id+1, true); + + port->ints &= ~PIO_USB_INTS_CONNECT_BITS; + } + + if ( port->ints & PIO_USB_INTS_DISCONNECT_BITS ) + { + hcd_event_device_remove(root_id+1, true); + + port->ints &= ~PIO_USB_INTS_DISCONNECT_BITS; + } +} + #endif From 75bca96bc630b98723c9f4bbb4958dac22ac594d Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 1 Apr 2022 16:01:24 +0700 Subject: [PATCH 13/62] pio usb control work well including stall response --- src/host/hcd.h | 4 - src/host/usbh.c | 16 ++-- src/portable/raspberrypi/pio/hcd_pio.c | 118 +++++++++++++++---------- 3 files changed, 78 insertions(+), 60 deletions(-) diff --git a/src/host/hcd.h b/src/host/hcd.h index 0e18351fc..9b41f2447 100644 --- a/src/host/hcd.h +++ b/src/host/hcd.h @@ -153,10 +153,6 @@ bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * // Submit a special transfer to send 8-byte Setup Packet, when complete hcd_event_xfer_complete() must be invoked bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]); -// Optional: some controllers are capable of carry out the whole Control Transfer (setup, data, status) -// without help of USBH. -bool hcd_edpt_control_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8], uint8_t* data) TU_ATTR_WEAK; - // clear stall, data toggle is also reset to DATA0 bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr); diff --git a/src/host/usbh.c b/src/host/usbh.c index b388a4c08..520ef4c7d 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -543,19 +543,12 @@ bool tuh_control_xfer (tuh_xfer_t* xfer) const uint8_t rhport = usbh_get_rhport(daddr); TU_LOG2("[%u:%u] %s: ", rhport, daddr, xfer->setup->bRequest <= TUSB_REQ_SYNCH_FRAME ? tu_str_std_request[xfer->setup->bRequest] : "Unknown Request"); - TU_LOG2_VAR(&xfer->setup); + TU_LOG2_VAR(xfer->setup); TU_LOG2("\r\n"); if (xfer->complete_cb) { - if ( hcd_edpt_control_xfer ) - { - _ctrl_xfer.stage = CONTROL_STAGE_ACK; - TU_ASSERT( hcd_edpt_control_xfer(rhport, daddr, (uint8_t const*) &_ctrl_xfer.request, _ctrl_xfer.buffer) ); - }else - { - TU_ASSERT( hcd_setup_send(rhport, daddr, (uint8_t const*) &_ctrl_xfer.request) ); - } + TU_ASSERT( hcd_setup_send(rhport, daddr, (uint8_t const*) &_ctrl_xfer.request) ); }else { // blocking if complete callback is not provided @@ -643,7 +636,8 @@ static bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result { // DATA stage: initial data toggle is always 1 _set_control_xfer_stage(CONTROL_STAGE_DATA); - return hcd_edpt_xfer(rhport, dev_addr, tu_edpt_addr(0, request->bmRequestType_bit.direction), _ctrl_xfer.buffer, request->wLength); + TU_ASSERT( hcd_edpt_xfer(rhport, dev_addr, tu_edpt_addr(0, request->bmRequestType_bit.direction), _ctrl_xfer.buffer, request->wLength) ); + return true; } __attribute__((fallthrough)); @@ -658,7 +652,7 @@ static bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result // ACK stage: toggle is always 1 _set_control_xfer_stage(CONTROL_STAGE_ACK); - hcd_edpt_xfer(rhport, dev_addr, tu_edpt_addr(0, 1-request->bmRequestType_bit.direction), NULL, 0); + TU_ASSERT( hcd_edpt_xfer(rhport, dev_addr, tu_edpt_addr(0, 1-request->bmRequestType_bit.direction), NULL, 0) ); break; case CONTROL_STAGE_ACK: diff --git a/src/portable/raspberrypi/pio/hcd_pio.c b/src/portable/raspberrypi/pio/hcd_pio.c index 254acf1d2..4eff490c8 100644 --- a/src/portable/raspberrypi/pio/hcd_pio.c +++ b/src/portable/raspberrypi/pio/hcd_pio.c @@ -53,15 +53,6 @@ extern pio_port_t pio_port[1]; extern root_port_t root_port[PIO_USB_ROOT_PORT_CNT]; extern endpoint_t ep_pool[PIO_USB_EP_POOL_CNT]; -extern void configure_fullspeed_host( pio_port_t *pp, const pio_usb_configuration_t *c, root_port_t *port); -extern void configure_lowspeed_host( pio_port_t *pp, const pio_usb_configuration_t *c, root_port_t *port); - -extern void control_setup_transfer( const pio_port_t *pp, uint8_t device_address, uint8_t *tx_data_address, uint8_t tx_data_len); -extern int control_in_protocol( usb_device_t *device, uint8_t *tx_data, uint16_t tx_length, uint8_t *rx_buffer, uint16_t request_length); -extern int control_out_protocol( usb_device_t *device, uint8_t *setup_data, uint16_t setup_length, uint8_t *out_data, uint16_t out_length); - -extern void update_packet_crc16(usb_setup_packet_t * packet); - //--------------------------------------------------------------------+ // HCD API //--------------------------------------------------------------------+ @@ -157,12 +148,15 @@ void hcd_int_disable(uint8_t rhport) // Endpoint API //--------------------------------------------------------------------+ -bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc) +bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep) { rhport = RHPORT_PIO(rhport); usb_device_t *device = &usb_device[0]; + return pio_usb_endpoint_open(rhport, dev_addr, (uint8_t const*) desc_ep); + +#if 0 static uint8_t ep_id_idx; // TODO remove later if (ep_desc->bEndpointAddress == 0) @@ -202,45 +196,19 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const } return true; +#endif } bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen) { - (void) rhport; - - return true; + rhport = RHPORT_PIO(rhport); + return pio_usb_endpoint_transfer(rhport, dev_addr, ep_addr, buffer, buflen); } bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]) { - (void) rhport; - return false; -} - - -bool hcd_edpt_control_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8], uint8_t* data) -{ - int ret; rhport = RHPORT_PIO(rhport); - - uint16_t const len = ((tusb_control_request_t const*) setup_packet)->wLength; - - usb_setup_packet_t pio_setup = { USB_SYNC, USB_PID_DATA0 }; - memcpy(&pio_setup.request_type, setup_packet, 8); - update_packet_crc16(&pio_setup); - - if (setup_packet[0] & TUSB_DIR_IN_MASK) - { - ret = control_in_protocol(&usb_device[0], (uint8_t*) &pio_setup, sizeof(pio_setup), data, len); - }else - { - ret = control_out_protocol(&usb_device[0], (uint8_t*) &pio_setup, sizeof(pio_setup), data, len); - } - - // TODO current pio is blocking - hcd_event_xfer_complete(dev_addr, 0, 0, ret ? XFER_RESULT_FAILED : XFER_RESULT_SUCCESS, false); - - return ret == 0; + return pio_usb_endpoint_send_setup(rhport, dev_addr, setup_packet); } //bool hcd_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) @@ -265,23 +233,83 @@ bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr) return true; } +void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* port, uint32_t flag) +{ + volatile uint32_t* ep_reg; + xfer_result_t result; + + if ( flag == PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) + { + ep_reg = &port->ep_complete; + result = XFER_RESULT_SUCCESS; + } + else if ( flag == PIO_USB_INTS_ENDPOINT_ERROR_BITS ) + { + ep_reg = &port->ep_error; + result = XFER_RESULT_FAILED; + } + else if ( flag == PIO_USB_INTS_ENDPOINT_STALLED_BITS ) + { + ep_reg = &port->ep_stalled; + result = XFER_RESULT_STALLED; + } + else + { + // something wrong + return; + } + + const uint32_t ep_all = *ep_reg; + + for(uint8_t ep_idx = 0; ep_idx < PIO_USB_EP_POOL_CNT; ep_idx++) + { + uint32_t const mask = (1u << ep_idx); + + if (ep_all & mask) + { + endpoint_t* ep = PIO_USB_EP(ep_idx); + hcd_event_xfer_complete(ep->dev_addr, ep->ep_num, ep->actual_len, result, true); + } + } + + // clear all + (*ep_reg) &= ~ep_all; +} + // IRQ Handler -void pio_usb_irq_handler(uint8_t root_id) +void __no_inline_not_in_flash_func(pio_usb_host_irq_handler)(uint8_t root_id) { root_port_t* port = PIO_USB(root_id); if ( port->ints & PIO_USB_INTS_CONNECT_BITS ) { - hcd_event_device_attach(root_id+1, true); - port->ints &= ~PIO_USB_INTS_CONNECT_BITS; + + hcd_event_device_attach(root_id+1, true); } if ( port->ints & PIO_USB_INTS_DISCONNECT_BITS ) { - hcd_event_device_remove(root_id+1, true); - port->ints &= ~PIO_USB_INTS_DISCONNECT_BITS; + hcd_event_device_remove(root_id+1, true); + } + + if ( port->ints & PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) + { + port->ints &= ~PIO_USB_INTS_ENDPOINT_COMPLETE_BITS; + handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_COMPLETE_BITS); + } + + if ( port->ints & PIO_USB_INTS_ENDPOINT_ERROR_BITS ) + { + port->ints &= ~PIO_USB_INTS_ENDPOINT_ERROR_BITS; + handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_ERROR_BITS); + } + + if ( port->ints & PIO_USB_INTS_ENDPOINT_STALLED_BITS ) + { + port->ints &= ~PIO_USB_INTS_ENDPOINT_STALLED_BITS; + handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_STALLED_BITS); } } From e6e3dfedc815007d07ea455df04569604d87a856 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 1 Apr 2022 16:34:36 +0700 Subject: [PATCH 14/62] hid example work well --- lib/Pico-PIO-USB | 2 +- src/class/hid/hid_host.c | 2 +- src/host/usbh.c | 2 +- src/portable/raspberrypi/pio/hcd_pio.c | 42 -------------------------- 4 files changed, 3 insertions(+), 45 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 4220c05dc..650130ab3 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 4220c05dc6b7ab7b4db33e5e05bb66be7b6c7156 +Subproject commit 650130ab312e0dc9337dc52cccf34d35875abdf7 diff --git a/src/class/hid/hid_host.c b/src/class/hid/hid_host.c index ce3d1598c..2573f5a6b 100644 --- a/src/class/hid/hid_host.c +++ b/src/class/hid/hid_host.c @@ -329,7 +329,7 @@ bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *de TU_VERIFY(TUSB_CLASS_HID == desc_itf->bInterfaceClass); - TU_LOG2("HID opening Interface %u (addr = %u)\r\n", desc_itf->bInterfaceNumber, dev_addr); + TU_LOG2("[%u] HID opening Interface %u\r\n", dev_addr, desc_itf->bInterfaceNumber); // len = interface + hid + n*endpoints uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t); diff --git a/src/host/usbh.c b/src/host/usbh.c index 520ef4c7d..c127ef310 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -792,7 +792,7 @@ bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * b static bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size) { - TU_LOG2("Open EP0 with Size = %u (addr = %u)\r\n", max_packet_size, dev_addr); + TU_LOG2("[%u] Open EP0 with Size = %u\r\n", dev_addr, max_packet_size); tusb_desc_endpoint_t ep0_desc = { diff --git a/src/portable/raspberrypi/pio/hcd_pio.c b/src/portable/raspberrypi/pio/hcd_pio.c index 4eff490c8..037ebc132 100644 --- a/src/portable/raspberrypi/pio/hcd_pio.c +++ b/src/portable/raspberrypi/pio/hcd_pio.c @@ -155,48 +155,6 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const usb_device_t *device = &usb_device[0]; return pio_usb_endpoint_open(rhport, dev_addr, (uint8_t const*) desc_ep); - -#if 0 - static uint8_t ep_id_idx; // TODO remove later - - if (ep_desc->bEndpointAddress == 0) - { - device->event = EVENT_NONE; - device->address = dev_addr; - - ep_id_idx = 0; - }else if (ep_desc->bmAttributes.xfer == TUSB_XFER_INTERRUPT) // only support interrupt endpoint - { - endpoint_t *ep = NULL; - for ( int ep_pool_idx = 0; ep_pool_idx < PIO_USB_EP_POOL_CNT; ep_pool_idx++ ) - { - if ( ep_pool[ep_pool_idx].ep_num == 0 ) - { - ep = &ep_pool[ep_pool_idx]; - device->endpoint_id[ep_id_idx] = ep_pool_idx + 1; - ep_id_idx++; - - ep->data_id = 0; - ep->ep_num = ep_desc->bEndpointAddress; - ep->interval = ep_desc->bInterval; - ep->interval_counter = 0; - ep->size = (uint8_t) tu_edpt_packet_size(ep_desc); - ep->attr = EP_ATTR_INTERRUPT; - - break; - } - } - -// TU_LOG1_INT(device->connected); -// TU_LOG1_INT(device->root); -// TU_LOG1_INT(device->is_root); -// TU_LOG1_INT(device->is_fullspeed); -// -// TU_LOG1_INT(ep_id_idx); - } - - return true; -#endif } bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen) From db6cba6a7bdf82f8c131fa2c521b86b10b4d053e Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 1 Apr 2022 16:38:41 +0700 Subject: [PATCH 15/62] rename pio to pio_usb --- hw/bsp/rp2040/family.cmake | 2 +- .../raspberrypi/{pio/hcd_pio.c => pio_usb/hcd_pio_usb.c} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/portable/raspberrypi/{pio/hcd_pio.c => pio_usb/hcd_pio_usb.c} (100%) diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index b56ee6a6d..4cec645a2 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -89,7 +89,7 @@ if (NOT TARGET _rp2040_family_inclusion_marker) ${TOP}/src/class/msc/msc_host.c ${TOP}/src/class/vendor/vendor_host.c - ${TOP}/src/portable/raspberrypi/pio/hcd_pio.c + ${TOP}/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c ${TOP}/lib/Pico-PIO-USB/pio_usb.c ${TOP}/lib/Pico-PIO-USB/pio_usb_port.c ${TOP}/lib/Pico-PIO-USB/usb_crc.c diff --git a/src/portable/raspberrypi/pio/hcd_pio.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c similarity index 100% rename from src/portable/raspberrypi/pio/hcd_pio.c rename to src/portable/raspberrypi/pio_usb/hcd_pio_usb.c From c25f835aea838f935893fdef6352fab938e11d4b Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 1 Apr 2022 23:31:43 +0700 Subject: [PATCH 16/62] update to use pio hw endpoint --- hw/bsp/rp2040/family.cmake | 3 ++- lib/Pico-PIO-USB | 2 +- src/portable/raspberrypi/pio_usb/hcd_pio_usb.c | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index 4cec645a2..275ec9e6b 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -91,7 +91,8 @@ if (NOT TARGET _rp2040_family_inclusion_marker) ${TOP}/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c ${TOP}/lib/Pico-PIO-USB/pio_usb.c - ${TOP}/lib/Pico-PIO-USB/pio_usb_port.c + ${TOP}/lib/Pico-PIO-USB/pio_usb_host.c + ${TOP}/lib/Pico-PIO-USB/pio_usb_hw.c ${TOP}/lib/Pico-PIO-USB/usb_crc.c ) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 650130ab3..72f91ac9c 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 650130ab312e0dc9337dc52cccf34d35875abdf7 +Subproject commit 72f91ac9cdaf532eacfbe9534420341b13480441 diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index 037ebc132..09407851d 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -225,7 +225,7 @@ void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* port, uint3 if (ep_all & mask) { - endpoint_t* ep = PIO_USB_EP(ep_idx); + pio_hw_endpoint_t* ep = PIO_USB_HW_EP(ep_idx); hcd_event_xfer_complete(ep->dev_addr, ep->ep_num, ep->actual_len, result, true); } } From f881e77e66ea92506fefb69a24324f6c2274aab9 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 2 Apr 2022 19:19:00 +0700 Subject: [PATCH 17/62] extracting hw root port --- lib/Pico-PIO-USB | 2 +- src/portable/raspberrypi/pio_usb/hcd_pio_usb.c | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 72f91ac9c..ea48fc073 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 72f91ac9cdaf532eacfbe9534420341b13480441 +Subproject commit ea48fc073ce28e3870a71e8019b08a02bde8ebf9 diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index 09407851d..1fe7a0d6e 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -50,8 +50,6 @@ static pio_usb_configuration_t pio_host_config = PIO_USB_DEFAULT_CONFIG; extern root_port_t root_port[PIO_USB_ROOT_PORT_CNT]; extern usb_device_t usb_device[PIO_USB_DEVICE_CNT]; extern pio_port_t pio_port[1]; -extern root_port_t root_port[PIO_USB_ROOT_PORT_CNT]; -extern endpoint_t ep_pool[PIO_USB_EP_POOL_CNT]; //--------------------------------------------------------------------+ // HCD API @@ -191,7 +189,7 @@ bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr) return true; } -void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* port, uint32_t flag) +void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_t* port, uint32_t flag) { volatile uint32_t* ep_reg; xfer_result_t result; @@ -237,7 +235,7 @@ void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* port, uint3 // IRQ Handler void __no_inline_not_in_flash_func(pio_usb_host_irq_handler)(uint8_t root_id) { - root_port_t* port = PIO_USB(root_id); + pio_hw_root_port_t* port = PIO_USB_HW_RPORT(root_id); if ( port->ints & PIO_USB_INTS_CONNECT_BITS ) { From f6851cf18764a615f83bb782973bb3ea25e9089c Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 3 Apr 2022 00:17:58 +0700 Subject: [PATCH 18/62] clean up hcd pio --- lib/Pico-PIO-USB | 2 +- .../raspberrypi/pio_usb/hcd_pio_usb.c | 60 +++---------------- 2 files changed, 10 insertions(+), 52 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index ea48fc073..7d8df447c 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit ea48fc073ce28e3870a71e8019b08a02bde8ebf9 +Subproject commit 7d8df447c4c5d0515f2ab9f10b28ac92d7cc78df diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index 1fe7a0d6e..dc52870e9 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -43,14 +43,8 @@ #define RHPORT_OFFSET 1 #define RHPORT_PIO(_x) ((_x)-RHPORT_OFFSET) - -static usb_device_t *_test_usb_device = NULL; static pio_usb_configuration_t pio_host_config = PIO_USB_DEFAULT_CONFIG; -extern root_port_t root_port[PIO_USB_ROOT_PORT_CNT]; -extern usb_device_t usb_device[PIO_USB_DEVICE_CNT]; -extern pio_port_t pio_port[1]; - //--------------------------------------------------------------------+ // HCD API //--------------------------------------------------------------------+ @@ -58,7 +52,7 @@ bool hcd_init(uint8_t rhport) { // To run USB SOF interrupt in core1, create alarm pool in core1. pio_host_config.alarm_pool = (void*)alarm_pool_create(2, 1); - _test_usb_device = pio_usb_host_init(&pio_host_config); + (void) pio_usb_host_init(&pio_host_config); return true; } @@ -66,56 +60,23 @@ bool hcd_init(uint8_t rhport) void hcd_port_reset(uint8_t rhport) { rhport = RHPORT_PIO(rhport); - - pio_port_t *pp = &pio_port[0]; - root_port_t *root = &root_port[rhport]; - - pio_usb_port_reset_start(root, pp); + pio_usb_hw_port_reset_start(rhport); } void hcd_port_reset_end(uint8_t rhport) { rhport = RHPORT_PIO(rhport); - - pio_port_t *pp = &pio_port[0]; - root_port_t *root = &root_port[rhport]; - - pio_usb_port_reset_end(root, pp); - - busy_wait_us(100); - - // TODO slow speed - bool fullspeed_flag = true; - - if (fullspeed_flag && get_port_pin_status(root) == PORT_PIN_FS_IDLE) { - root->root_device = &usb_device[0]; - if (!root->root_device->connected) { -// configure_fullspeed_host(pp, &pio_host_config, root); - root->root_device->is_fullspeed = true; - root->root_device->is_root = true; - root->root_device->connected = true; - root->root_device->root = root; - root->root_device->event = EVENT_CONNECT; - } - } else if (!fullspeed_flag && get_port_pin_status(root) == PORT_PIN_LS_IDLE) { - root->root_device = &usb_device[0]; - if (!root->root_device->connected) { -// configure_lowspeed_host(pp, &pio_host_config, root); - root->root_device->is_fullspeed = false; - root->root_device->is_root = true; - root->root_device->connected = true; - root->root_device->root = root; - root->root_device->event = EVENT_CONNECT; - } - } + pio_usb_hw_port_reset_end(rhport); } bool hcd_port_connect_status(uint8_t rhport) { - root_port_t* port = &root_port[0]; - bool dp = gpio_get(port->pin_dp); - bool dm = gpio_get(port->pin_dm); - return dp || dm; + rhport = RHPORT_PIO(rhport); + + pio_hw_root_port_t *root = PIO_USB_HW_RPORT(rhport); + port_pin_status_t line_state = pio_hw_get_line_state(root); + + return line_state != PORT_PIN_SE0; } tusb_speed_t hcd_port_speed_get(uint8_t rhport) @@ -149,9 +110,6 @@ void hcd_int_disable(uint8_t rhport) bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep) { rhport = RHPORT_PIO(rhport); - - usb_device_t *device = &usb_device[0]; - return pio_usb_endpoint_open(rhport, dev_addr, (uint8_t const*) desc_ep); } From 7ea75d3c09d1afd4db5374c89f715d8ff6c1d414 Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 3 Apr 2022 20:44:16 +0700 Subject: [PATCH 19/62] use pio usb host init --- lib/Pico-PIO-USB | 2 +- src/portable/raspberrypi/pio_usb/hcd_pio_usb.c | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 7d8df447c..b507f95de 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 7d8df447c4c5d0515f2ab9f10b28ac92d7cc78df +Subproject commit b507f95de457d5dc14fb15fd6481ef748c6e3342 diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index dc52870e9..c2ac2c706 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -50,9 +50,8 @@ static pio_usb_configuration_t pio_host_config = PIO_USB_DEFAULT_CONFIG; //--------------------------------------------------------------------+ bool hcd_init(uint8_t rhport) { - // To run USB SOF interrupt in core1, create alarm pool in core1. - pio_host_config.alarm_pool = (void*)alarm_pool_create(2, 1); - (void) pio_usb_host_init(&pio_host_config); + // To run USB SOF interrupt in core1, call this init in core1 + pio_usb_host_controller_init(&pio_host_config); return true; } From d7d7e61f3437076a38ec031f2dc4cef38c8c831b Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 3 Apr 2022 22:48:08 +0700 Subject: [PATCH 20/62] correct pio usb speed, and close device --- lib/Pico-PIO-USB | 2 +- src/portable/raspberrypi/pio_usb/hcd_pio_usb.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index b507f95de..650ef75f0 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit b507f95de457d5dc14fb15fd6481ef748c6e3342 +Subproject commit 650ef75f0d8a4020a7ddc2ccee1ee0f3237ab760 diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index c2ac2c706..839087957 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -81,12 +81,15 @@ bool hcd_port_connect_status(uint8_t rhport) tusb_speed_t hcd_port_speed_get(uint8_t rhport) { // TODO determine link speed - return TUSB_SPEED_FULL; + rhport = RHPORT_PIO(rhport); + return PIO_USB_HW_RPORT(rhport)->is_fullspeed ? TUSB_SPEED_FULL : TUSB_SPEED_LOW; } // Close all opened endpoint belong to this device void hcd_device_close(uint8_t rhport, uint8_t dev_addr) { + rhport = RHPORT_PIO(rhport); + pio_usb_host_close_device(rhport, dev_addr); } uint32_t hcd_frame_number(uint8_t rhport) From d97c1546957059cfb3063b95d5287e2729f7f79a Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 5 Apr 2022 00:04:47 +0700 Subject: [PATCH 21/62] add need_pre for pio usb, but not work out well --- lib/Pico-PIO-USB | 2 +- src/host/usbh.c | 4 ++-- src/portable/raspberrypi/pio_usb/hcd_pio_usb.c | 6 +++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 650ef75f0..4993f4a1d 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 650ef75f0d8a4020a7ddc2ccee1ee0f3237ab760 +Subproject commit 4993f4a1df03c8af13c8091052a6274e8c333bc0 diff --git a/src/host/usbh.c b/src/host/usbh.c index c127ef310..47c462012 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -642,7 +642,7 @@ static bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result __attribute__((fallthrough)); case CONTROL_STAGE_DATA: - if (xferred_bytes) + if (request->wLength) { TU_LOG2("[%u:%u] Control data:\r\n", rhport, dev_addr); TU_LOG2_MEM(_ctrl_xfer.buffer, xferred_bytes, 2); @@ -1301,7 +1301,7 @@ static void process_enumeration(tuh_xfer_t* xfer) TU_ASSERT(new_dev, ); new_dev->addressed = 1; - // TODO close device 0, may not be needed + // Close device 0 hcd_device_close(_dev0.rhport, 0); // open control pipe for new address diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index 839087957..c2ef54b9e 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -111,8 +111,12 @@ void hcd_int_disable(uint8_t rhport) bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep) { + hcd_devtree_info_t dev_tree; + hcd_devtree_get_info(dev_addr, &dev_tree); + bool const need_pre = (dev_tree.hub_addr && dev_tree.speed == TUSB_SPEED_LOW); + rhport = RHPORT_PIO(rhport); - return pio_usb_endpoint_open(rhport, dev_addr, (uint8_t const*) desc_ep); + return pio_usb_endpoint_open(rhport, dev_addr, (uint8_t const*) desc_ep, need_pre); } bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen) From d5d4909c20f581ef2046932191f487c001d59ca3 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 8 Apr 2022 13:36:05 +0700 Subject: [PATCH 22/62] proof of concpet that device pio-usb work with host pio-usb --- examples/device/hid_boot_interface/src/main.c | 4 +++ .../hid_boot_interface/src/tusb_config.h | 3 ++ examples/host/cdc_msc_hid/src/tusb_config.h | 1 + hw/bsp/rp2040/family.cmake | 32 ++++++++----------- src/portable/raspberrypi/rp2040/dcd_rp2040.c | 2 +- src/tusb_option.h | 6 +++- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/examples/device/hid_boot_interface/src/main.c b/examples/device/hid_boot_interface/src/main.c index e5e2f6856..ed7f235b7 100644 --- a/examples/device/hid_boot_interface/src/main.c +++ b/examples/device/hid_boot_interface/src/main.c @@ -31,6 +31,8 @@ #include "tusb.h" #include "usb_descriptors.h" +#include "pio_usb.h" + //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF PROTYPES //--------------------------------------------------------------------+ @@ -59,6 +61,8 @@ int main(void) while (1) { + pio_usb_device_task(); + tud_task(); // tinyusb device task led_blinking_task(); diff --git a/examples/device/hid_boot_interface/src/tusb_config.h b/examples/device/hid_boot_interface/src/tusb_config.h index 1c8fa40b2..d4f67e7ba 100644 --- a/examples/device/hid_boot_interface/src/tusb_config.h +++ b/examples/device/hid_boot_interface/src/tusb_config.h @@ -39,6 +39,9 @@ #error CFG_TUSB_MCU must be defined #endif +// Use raspberry pio-usb for device +#define CFG_TUD_RPI_PIO_USB 1 + // RHPort number used for device can be defined by board.mk, default to port 0 #ifndef BOARD_DEVICE_RHPORT_NUM #define BOARD_DEVICE_RHPORT_NUM 0 diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index 80ee8cb01..9d083c771 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -39,6 +39,7 @@ #error CFG_TUSB_MCU must be defined #endif +// Use raspberry pio-usb for host #define CFG_TUH_RPI_PIO_USB 1 //#define CFG_TUSB_RPI_PIO_INC_PATH 1 diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index 275ec9e6b..b186fc78e 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -29,12 +29,17 @@ if (NOT TARGET _rp2040_family_inclusion_marker) target_sources(tinyusb_common_base INTERFACE ${TOP}/src/tusb.c ${TOP}/src/common/tusb_fifo.c + ${TOP}/lib/Pico-PIO-USB/pio_usb.c + ${TOP}/lib/Pico-PIO-USB/pio_usb_host.c + ${TOP}/lib/Pico-PIO-USB/pio_usb_hw.c + ${TOP}/lib/Pico-PIO-USB/usb_crc.c ) target_include_directories(tinyusb_common_base INTERFACE ${TOP}/src ${TOP}/src/common ${TOP}/hw + ${TOP}/lib/Pico-PIO-USB ) target_link_libraries(tinyusb_common_base INTERFACE @@ -42,6 +47,10 @@ if (NOT TARGET _rp2040_family_inclusion_marker) hardware_irq hardware_resets pico_sync + # for usb-pio + hardware_dma + hardware_pio + pico_multicore ) set(TINYUSB_DEBUG_LEVEL 0) @@ -60,6 +69,7 @@ if (NOT TARGET _rp2040_family_inclusion_marker) add_library(tinyusb_device_base INTERFACE) target_sources(tinyusb_device_base INTERFACE ${TOP}/src/portable/raspberrypi/rp2040/dcd_rp2040.c + ${TOP}/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c ${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c ${TOP}/src/device/usbd.c ${TOP}/src/device/usbd_control.c @@ -88,12 +98,7 @@ if (NOT TARGET _rp2040_family_inclusion_marker) ${TOP}/src/class/hid/hid_host.c ${TOP}/src/class/msc/msc_host.c ${TOP}/src/class/vendor/vendor_host.c - - ${TOP}/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c - ${TOP}/lib/Pico-PIO-USB/pio_usb.c - ${TOP}/lib/Pico-PIO-USB/pio_usb_host.c - ${TOP}/lib/Pico-PIO-USB/pio_usb_hw.c - ${TOP}/lib/Pico-PIO-USB/usb_crc.c + ${TOP}/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c ) # Sometimes have to do host specific actions in mostly common functions @@ -101,17 +106,6 @@ if (NOT TARGET _rp2040_family_inclusion_marker) RP2040_USB_HOST_MODE=1 ) - # config for host pio - target_link_libraries(tinyusb_host_base INTERFACE - hardware_dma - hardware_pio - pico_multicore - ) - - target_include_directories(tinyusb_host_base INTERFACE - ${TOP}/lib/Pico-PIO-USB - ) - add_library(tinyusb_bsp INTERFACE) target_sources(tinyusb_bsp INTERFACE ${TOP}/hw/bsp/rp2040/family.c @@ -166,8 +160,8 @@ if (NOT TARGET _rp2040_family_inclusion_marker) _family_initialize_project(${PROJECT} ${DIR}) enable_language(C CXX ASM) pico_sdk_init() - pico_generate_pio_header(tinyusb_host_base ${TOP}/lib/Pico-PIO-USB/usb_tx.pio) - pico_generate_pio_header(tinyusb_host_base ${TOP}/lib/Pico-PIO-USB/usb_rx.pio) + pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/usb_tx.pio) + pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/usb_rx.pio) endfunction() # This method must be called from the project scope to suppress known warnings in TinyUSB source files diff --git a/src/portable/raspberrypi/rp2040/dcd_rp2040.c b/src/portable/raspberrypi/rp2040/dcd_rp2040.c index 915822798..2d77ad4f9 100644 --- a/src/portable/raspberrypi/rp2040/dcd_rp2040.c +++ b/src/portable/raspberrypi/rp2040/dcd_rp2040.c @@ -26,7 +26,7 @@ #include "tusb_option.h" -#if CFG_TUD_ENABLED && CFG_TUSB_MCU == OPT_MCU_RP2040 +#if CFG_TUD_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && !CFG_TUD_RPI_PIO_USB #include "pico.h" #include "rp2040_usb.h" diff --git a/src/tusb_option.h b/src/tusb_option.h index 61cc283c4..5636b34fe 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -239,7 +239,7 @@ #define TUH_OPT_RHPORT -1 #endif -#define CFG_TUH_ENABLED ( TUH_RHPORT_MODE & OPT_MODE_HOST ) +#define CFG_TUH_ENABLED (TUH_RHPORT_MODE & OPT_MODE_HOST) // For backward compatible #define TUSB_OPT_DEVICE_ENABLED CFG_TUD_ENABLED @@ -398,6 +398,10 @@ #define CFG_TUH_RPI_PIO_USB 0 #endif +#ifndef CFG_TUD_RPI_PIO_USB +#define CFG_TUD_RPI_PIO_USB 0 +#endif + //------------------------------------------------------------------ // Configuration Validation From cc5a4720e5c1295eacf02c7b81fc4454f29cfaa4 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Apr 2022 21:51:49 +0700 Subject: [PATCH 23/62] catch up with pio lib --- .../hid_boot_interface/src/tusb_config.h | 3 + hw/bsp/rp2040/family.cmake | 5 +- lib/Pico-PIO-USB | 2 +- .../raspberrypi/pio_usb/dcd_pio_usb.c | 234 ++++++++++++++++++ .../raspberrypi/pio_usb/hcd_pio_usb.c | 28 +-- 5 files changed, 254 insertions(+), 18 deletions(-) create mode 100644 src/portable/raspberrypi/pio_usb/dcd_pio_usb.c diff --git a/examples/device/hid_boot_interface/src/tusb_config.h b/examples/device/hid_boot_interface/src/tusb_config.h index d4f67e7ba..c8bb8a807 100644 --- a/examples/device/hid_boot_interface/src/tusb_config.h +++ b/examples/device/hid_boot_interface/src/tusb_config.h @@ -41,6 +41,9 @@ // Use raspberry pio-usb for device #define CFG_TUD_RPI_PIO_USB 1 +#if CFG_TUD_RPI_PIO_USB +#define BOARD_DEVICE_RHPORT_NUM 1 +#endif // RHPort number used for device can be defined by board.mk, default to port 0 #ifndef BOARD_DEVICE_RHPORT_NUM diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index b186fc78e..d872c35ea 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -31,6 +31,7 @@ if (NOT TARGET _rp2040_family_inclusion_marker) ${TOP}/src/common/tusb_fifo.c ${TOP}/lib/Pico-PIO-USB/pio_usb.c ${TOP}/lib/Pico-PIO-USB/pio_usb_host.c + ${TOP}/lib/Pico-PIO-USB/pio_usb_device.c ${TOP}/lib/Pico-PIO-USB/pio_usb_hw.c ${TOP}/lib/Pico-PIO-USB/usb_crc.c ) @@ -69,8 +70,8 @@ if (NOT TARGET _rp2040_family_inclusion_marker) add_library(tinyusb_device_base INTERFACE) target_sources(tinyusb_device_base INTERFACE ${TOP}/src/portable/raspberrypi/rp2040/dcd_rp2040.c - ${TOP}/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c ${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c + ${TOP}/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c ${TOP}/src/device/usbd.c ${TOP}/src/device/usbd_control.c ${TOP}/src/class/audio/audio_device.c @@ -92,13 +93,13 @@ if (NOT TARGET _rp2040_family_inclusion_marker) target_sources(tinyusb_host_base INTERFACE ${TOP}/src/portable/raspberrypi/rp2040/hcd_rp2040.c ${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c + ${TOP}/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c ${TOP}/src/host/usbh.c ${TOP}/src/host/hub.c ${TOP}/src/class/cdc/cdc_host.c ${TOP}/src/class/hid/hid_host.c ${TOP}/src/class/msc/msc_host.c ${TOP}/src/class/vendor/vendor_host.c - ${TOP}/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c ) # Sometimes have to do host specific actions in mostly common functions diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 4993f4a1d..84a00b976 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 4993f4a1df03c8af13c8091052a6274e8c333bc0 +Subproject commit 84a00b976041d8c34cf660fda6fa88198c9721dc diff --git a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c new file mode 100644 index 000000000..08d8b6086 --- /dev/null +++ b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c @@ -0,0 +1,234 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018, hathach (tinyusb.org) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * This file is part of the TinyUSB stack. + */ + +#include "tusb_option.h" + +#if CFG_TUD_ENABLED && (CFG_TUSB_MCU == OPT_MCU_RP2040) && CFG_TUD_RPI_PIO_USB + +#include "pico.h" +#include "hardware/pio.h" +#include "pio_usb.h" + +#include "device/dcd.h" + +//--------------------------------------------------------------------+ +// MACRO TYPEDEF CONSTANT ENUM DECLARATION +//--------------------------------------------------------------------+ + +#define RHPORT_OFFSET 1 +#define RHPORT_PIO(_x) ((_x)-RHPORT_OFFSET) + +static uint8_t new_addr = 0; + +//------------- -------------// +static usb_device_t *usb_device = NULL; +static usb_descriptor_buffers_t desc; + +/*------------------------------------------------------------------*/ +/* Device API + *------------------------------------------------------------------*/ + +// Initialize controller to device mode +void dcd_init (uint8_t rhport) +{ + (void) rhport; + + static pio_usb_configuration_t config = PIO_USB_DEFAULT_CONFIG; + usb_device = pio_usb_device_init(&config, &desc); +} + +// Enable device interrupt +void dcd_int_enable (uint8_t rhport) +{ + (void) rhport; +} + +// Disable device interrupt +void dcd_int_disable (uint8_t rhport) +{ + (void) rhport; +} + +// Receive Set Address request, mcu port must also include status IN response +void dcd_set_address (uint8_t rhport, uint8_t dev_addr) +{ + // store addr, will update crc5 lut when status is complete + new_addr = dev_addr; + + dcd_edpt_xfer(rhport, 0x80, NULL, 0); +} + +// Wake up host +void dcd_remote_wakeup (uint8_t rhport) +{ + (void) rhport; +} + +// Connect by enabling internal pull-up resistor on D+/D- +void dcd_connect(uint8_t rhport) +{ + (void) rhport; +} + +// Disconnect by disabling internal pull-up resistor on D+/D- +void dcd_disconnect(uint8_t rhport) +{ + (void) rhport; +} + +//--------------------------------------------------------------------+ +// Endpoint API +//--------------------------------------------------------------------+ + +// Configure endpoint's registers according to descriptor +bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * ep_desc) +{ + (void) rhport; + (void) ep_desc; + return false; +} + +void dcd_edpt_close_all (uint8_t rhport) +{ + (void) rhport; +} + +// Submit a transfer, When complete dcd_event_xfer_complete() is invoked to notify the stack +bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) +{ + uint8_t pio_rhport = RHPORT_PIO(rhport); + return pio_usb_device_endpoint_transfer(pio_rhport, ep_addr, buffer, total_bytes); +} + +// Submit a transfer where is managed by FIFO, When complete dcd_event_xfer_complete() is invoked to notify the stack - optional, however, must be listed in usbd.c +bool dcd_edpt_xfer_fifo (uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16_t total_bytes) +{ + (void) rhport; + (void) ep_addr; + (void) ff; + (void) total_bytes; + return false; +} + +// Stall endpoint +void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) +{ + (void) rhport; + (void) ep_addr; +} + +// clear stall, data toggle is also reset to DATA0 +void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) +{ + (void) rhport; + (void) ep_addr; +} + +//--------------------------------------------------------------------+ +// +//--------------------------------------------------------------------+ +extern void update_ep0_crc5_lut(uint8_t addr); + +static void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_t* port, uint32_t flag) +{ + volatile uint32_t* ep_reg; + xfer_result_t result; + + if ( flag == PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) + { + ep_reg = &port->ep_complete; + result = XFER_RESULT_SUCCESS; + } + else if ( flag == PIO_USB_INTS_ENDPOINT_ERROR_BITS ) + { + ep_reg = &port->ep_error; + result = XFER_RESULT_FAILED; + } + else if ( flag == PIO_USB_INTS_ENDPOINT_STALLED_BITS ) + { + ep_reg = &port->ep_stalled; + result = XFER_RESULT_STALLED; + } + else + { + // something wrong + return; + } + + const uint32_t ep_all = *ep_reg; + + for(uint8_t ep_idx = 0; ep_idx < PIO_USB_EP_POOL_CNT; ep_idx++) + { + uint32_t const mask = (1u << ep_idx); + + if (ep_all & mask) + { + pio_hw_endpoint_t* ep = PIO_USB_HW_EP(ep_idx); + uint8_t const tu_rhport = port - PIO_USB_HW_RPORT(0) + 1; + + // address is changed, update crc5 lut + if (new_addr && ep->ep_num == 0x80 && ep->actual_len == 0) + { + update_ep0_crc5_lut(new_addr); + new_addr = 0; + } + + dcd_event_xfer_complete(tu_rhport, ep->ep_num, ep->actual_len, result, true); + } + } + + // clear all + (*ep_reg) &= ~ep_all; +} + +// IRQ Handler +void __no_inline_not_in_flash_func(pio_usb_device_irq_handler)(uint8_t root_id) +{ + uint8_t const tu_rhport = root_id + 1; + pio_hw_root_port_t* port = PIO_USB_HW_RPORT(root_id); + uint32_t const ints = port->ints; + + if (ints & PIO_USB_INTS_RESET_END_BITS) + { + new_addr = 0; + dcd_event_bus_reset(tu_rhport, TUSB_SPEED_FULL, true); + } + + if (ints & PIO_USB_INTS_SETUP_REQ_BITS) + { + dcd_event_setup_received(tu_rhport, port->setup_packet, true); + } + + if ( ints & PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) + { + handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_COMPLETE_BITS); + } + + // clear all + port->ints &= ~ints; +} + +#endif diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index c2ef54b9e..4bd318448 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -116,19 +116,19 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const bool const need_pre = (dev_tree.hub_addr && dev_tree.speed == TUSB_SPEED_LOW); rhport = RHPORT_PIO(rhport); - return pio_usb_endpoint_open(rhport, dev_addr, (uint8_t const*) desc_ep, need_pre); + return pio_usb_host_endpoint_open(rhport, dev_addr, (uint8_t const*) desc_ep, need_pre); } bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen) { rhport = RHPORT_PIO(rhport); - return pio_usb_endpoint_transfer(rhport, dev_addr, ep_addr, buffer, buflen); + return pio_usb_host_endpoint_transfer(rhport, dev_addr, ep_addr, buffer, buflen); } bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]) { rhport = RHPORT_PIO(rhport); - return pio_usb_endpoint_send_setup(rhport, dev_addr, setup_packet); + return pio_usb_host_send_setup(rhport, dev_addr, setup_packet); } //bool hcd_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) @@ -153,7 +153,7 @@ bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr) return true; } -void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_t* port, uint32_t flag) +static void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_t* port, uint32_t flag) { volatile uint32_t* ep_reg; xfer_result_t result; @@ -200,37 +200,35 @@ void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_t* port void __no_inline_not_in_flash_func(pio_usb_host_irq_handler)(uint8_t root_id) { pio_hw_root_port_t* port = PIO_USB_HW_RPORT(root_id); + uint32_t const ints = port->ints; - if ( port->ints & PIO_USB_INTS_CONNECT_BITS ) + if ( ints & PIO_USB_INTS_CONNECT_BITS ) { - port->ints &= ~PIO_USB_INTS_CONNECT_BITS; - hcd_event_device_attach(root_id+1, true); } - if ( port->ints & PIO_USB_INTS_DISCONNECT_BITS ) + if ( ints & PIO_USB_INTS_DISCONNECT_BITS ) { - port->ints &= ~PIO_USB_INTS_DISCONNECT_BITS; hcd_event_device_remove(root_id+1, true); } - if ( port->ints & PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) + if ( ints & PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) { - port->ints &= ~PIO_USB_INTS_ENDPOINT_COMPLETE_BITS; handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_COMPLETE_BITS); } - if ( port->ints & PIO_USB_INTS_ENDPOINT_ERROR_BITS ) + if ( ints & PIO_USB_INTS_ENDPOINT_ERROR_BITS ) { - port->ints &= ~PIO_USB_INTS_ENDPOINT_ERROR_BITS; handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_ERROR_BITS); } - if ( port->ints & PIO_USB_INTS_ENDPOINT_STALLED_BITS ) + if ( ints & PIO_USB_INTS_ENDPOINT_STALLED_BITS ) { - port->ints &= ~PIO_USB_INTS_ENDPOINT_STALLED_BITS; handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_STALLED_BITS); } + + // clear all + port->ints &= ~ints; } #endif From 05bfc1a9eba9fc901c35bdd1844cf962c5732310 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 15 Apr 2022 01:04:12 +0700 Subject: [PATCH 24/62] pio device works well --- examples/device/hid_boot_interface/src/main.c | 2 - lib/Pico-PIO-USB | 2 +- .../raspberrypi/pio_usb/dcd_pio_usb.c | 45 +++++++------------ 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/examples/device/hid_boot_interface/src/main.c b/examples/device/hid_boot_interface/src/main.c index ed7f235b7..031b17a2e 100644 --- a/examples/device/hid_boot_interface/src/main.c +++ b/examples/device/hid_boot_interface/src/main.c @@ -61,8 +61,6 @@ int main(void) while (1) { - pio_usb_device_task(); - tud_task(); // tinyusb device task led_blinking_task(); diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 84a00b976..d2b37b42b 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 84a00b976041d8c34cf660fda6fa88198c9721dc +Subproject commit d2b37b42befb6c761433b3c164c59a049721762f diff --git a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c index 08d8b6086..dc9cbb45b 100644 --- a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c @@ -41,8 +41,6 @@ #define RHPORT_OFFSET 1 #define RHPORT_PIO(_x) ((_x)-RHPORT_OFFSET) -static uint8_t new_addr = 0; - //------------- -------------// static usb_device_t *usb_device = NULL; static usb_descriptor_buffers_t desc; @@ -75,10 +73,12 @@ void dcd_int_disable (uint8_t rhport) // Receive Set Address request, mcu port must also include status IN response void dcd_set_address (uint8_t rhport, uint8_t dev_addr) { - // store addr, will update crc5 lut when status is complete - new_addr = dev_addr; + uint8_t const pio_rhport = RHPORT_PIO(rhport); - dcd_edpt_xfer(rhport, 0x80, NULL, 0); + // must be called before queuing status + pio_usb_device_set_address(pio_rhport, dev_addr); + + pio_usb_device_endpoint_transfer(pio_rhport, 0x80, NULL, 0); } // Wake up host @@ -104,11 +104,10 @@ void dcd_disconnect(uint8_t rhport) //--------------------------------------------------------------------+ // Configure endpoint's registers according to descriptor -bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * ep_desc) +bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_ep) { - (void) rhport; - (void) ep_desc; - return false; + uint8_t const pio_rhport = RHPORT_PIO(rhport); + return pio_usb_device_endpoint_open(pio_rhport, (uint8_t const*) desc_ep); } void dcd_edpt_close_all (uint8_t rhport) @@ -119,19 +118,19 @@ void dcd_edpt_close_all (uint8_t rhport) // Submit a transfer, When complete dcd_event_xfer_complete() is invoked to notify the stack bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) { - uint8_t pio_rhport = RHPORT_PIO(rhport); + uint8_t const pio_rhport = RHPORT_PIO(rhport); return pio_usb_device_endpoint_transfer(pio_rhport, ep_addr, buffer, total_bytes); } // Submit a transfer where is managed by FIFO, When complete dcd_event_xfer_complete() is invoked to notify the stack - optional, however, must be listed in usbd.c -bool dcd_edpt_xfer_fifo (uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16_t total_bytes) -{ - (void) rhport; - (void) ep_addr; - (void) ff; - (void) total_bytes; - return false; -} +//bool dcd_edpt_xfer_fifo (uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16_t total_bytes) +//{ +// (void) rhport; +// (void) ep_addr; +// (void) ff; +// (void) total_bytes; +// return false; +//} // Stall endpoint void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) @@ -150,7 +149,6 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) //--------------------------------------------------------------------+ // //--------------------------------------------------------------------+ -extern void update_ep0_crc5_lut(uint8_t addr); static void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_t* port, uint32_t flag) { @@ -188,14 +186,6 @@ static void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_ { pio_hw_endpoint_t* ep = PIO_USB_HW_EP(ep_idx); uint8_t const tu_rhport = port - PIO_USB_HW_RPORT(0) + 1; - - // address is changed, update crc5 lut - if (new_addr && ep->ep_num == 0x80 && ep->actual_len == 0) - { - update_ep0_crc5_lut(new_addr); - new_addr = 0; - } - dcd_event_xfer_complete(tu_rhport, ep->ep_num, ep->actual_len, result, true); } } @@ -213,7 +203,6 @@ void __no_inline_not_in_flash_func(pio_usb_device_irq_handler)(uint8_t root_id) if (ints & PIO_USB_INTS_RESET_END_BITS) { - new_addr = 0; dcd_event_bus_reset(tu_rhport, TUSB_SPEED_FULL, true); } From 00a0e3f21ba588f892022159e435938b48305b99 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 15 Apr 2022 20:14:47 +0700 Subject: [PATCH 25/62] update lib pio --- lib/Pico-PIO-USB | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index d2b37b42b..ed84b4de8 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit d2b37b42befb6c761433b3c164c59a049721762f +Subproject commit ed84b4de88e7c22bb56cb53bf85f1a2c5dd8b8fc From b5a9537eea81bd6ffd8d575dbd2b5823c2fc1f0e Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 16 Apr 2022 01:19:51 +0700 Subject: [PATCH 26/62] support pio dcd endpiont stall --- lib/Pico-PIO-USB | 2 +- src/portable/raspberrypi/pio_usb/dcd_pio_usb.c | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index ed84b4de8..d6712cad5 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit ed84b4de88e7c22bb56cb53bf85f1a2c5dd8b8fc +Subproject commit d6712cad5bfbddb25647974f0d583596e703c06f diff --git a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c index dc9cbb45b..7744a11cd 100644 --- a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c @@ -136,14 +136,17 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; - (void) ep_addr; + pio_hw_endpoint_t *ep = pio_usb_device_get_ep(ep_addr); + ep->stalled = true; } // clear stall, data toggle is also reset to DATA0 void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; - (void) ep_addr; + pio_hw_endpoint_t *ep = pio_usb_device_get_ep(ep_addr); + ep->data_id = 0; + ep->stalled = false; } //--------------------------------------------------------------------+ From a32cb1bb93ce986af9ed83f5d378c26bb4df2b1e Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 18 Apr 2022 17:49:21 +0700 Subject: [PATCH 27/62] clean up pio driver --- lib/Pico-PIO-USB | 2 +- src/device/dcd.h | 32 ++++++++++++++--- src/device/usbd.c | 36 ++----------------- src/device/usbd.h | 2 +- .../raspberrypi/pio_usb/dcd_pio_usb.c | 7 ++-- .../raspberrypi/pio_usb/hcd_pio_usb.c | 6 ++-- 6 files changed, 39 insertions(+), 46 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index d6712cad5..98e3feefc 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit d6712cad5bfbddb25647974f0d583596e703c06f +Subproject commit 98e3feefcd1c7218bced0ea2ea35530fd1b550e4 diff --git a/src/device/dcd.h b/src/device/dcd.h index 8efbc90ef..bc68c9be6 100644 --- a/src/device/dcd.h +++ b/src/device/dcd.h @@ -174,16 +174,40 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr); extern void dcd_event_handler(dcd_event_t const * event, bool in_isr); // helper to send bus signal event -extern void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr); +TU_ATTR_ALWAYS_INLINE static inline void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr) +{ + dcd_event_t event = { .rhport = rhport, .event_id = eid }; + dcd_event_handler(&event, in_isr); +} // helper to send bus reset event -extern void dcd_event_bus_reset (uint8_t rhport, tusb_speed_t speed, bool in_isr); +TU_ATTR_ALWAYS_INLINE static inline void dcd_event_bus_reset (uint8_t rhport, tusb_speed_t speed, bool in_isr) +{ + dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_BUS_RESET }; + event.bus_reset.speed = speed; + dcd_event_handler(&event, in_isr); +} // helper to send setup received -extern void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr); +TU_ATTR_ALWAYS_INLINE static inline void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr) +{ + dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SETUP_RECEIVED }; + memcpy(&event.setup_received, setup, 8); + + dcd_event_handler(&event, in_isr); +} // helper to send transfer complete event -extern void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr); +TU_ATTR_ALWAYS_INLINE static inline void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr) +{ + dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_XFER_COMPLETE }; + + event.xfer_complete.ep_addr = ep_addr; + event.xfer_complete.len = xferred_bytes; + event.xfer_complete.result = result; + + dcd_event_handler(&event, in_isr); +} #ifdef __cplusplus } diff --git a/src/device/usbd.c b/src/device/usbd.c index 7ab2660f4..8d5d54b86 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -265,7 +265,7 @@ static inline usbd_class_driver_t const * get_driver(uint8_t drvid) //--------------------------------------------------------------------+ enum { RHPORT_INVALID = 0xFFu }; -static uint8_t _usbd_rhport = RHPORT_INVALID; +static volatile uint8_t _usbd_rhport = RHPORT_INVALID; // Event queue // usbd_int_set() is used as mutex in OS NONE config @@ -1065,7 +1065,7 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const //--------------------------------------------------------------------+ // DCD Event Handler //--------------------------------------------------------------------+ -void dcd_event_handler(dcd_event_t const * event, bool in_isr) +void __no_inline_not_in_flash_func(dcd_event_handler)(dcd_event_t const * event, bool in_isr) { switch (event->event_id) { @@ -1115,38 +1115,6 @@ void dcd_event_handler(dcd_event_t const * event, bool in_isr) } } -void dcd_event_bus_signal (uint8_t rhport, dcd_eventid_t eid, bool in_isr) -{ - dcd_event_t event = { .rhport = rhport, .event_id = eid }; - dcd_event_handler(&event, in_isr); -} - -void dcd_event_bus_reset (uint8_t rhport, tusb_speed_t speed, bool in_isr) -{ - dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_BUS_RESET }; - event.bus_reset.speed = speed; - dcd_event_handler(&event, in_isr); -} - -void dcd_event_setup_received(uint8_t rhport, uint8_t const * setup, bool in_isr) -{ - dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SETUP_RECEIVED }; - memcpy(&event.setup_received, setup, 8); - - dcd_event_handler(&event, in_isr); -} - -void dcd_event_xfer_complete (uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes, uint8_t result, bool in_isr) -{ - dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_XFER_COMPLETE }; - - event.xfer_complete.ep_addr = ep_addr; - event.xfer_complete.len = xferred_bytes; - event.xfer_complete.result = result; - - dcd_event_handler(&event, in_isr); -} - //--------------------------------------------------------------------+ // USBD API For Class Driver //--------------------------------------------------------------------+ diff --git a/src/device/usbd.h b/src/device/usbd.h index b2bf8ba9d..32c82e75a 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -46,7 +46,7 @@ bool tud_inited(void); // Task function should be called in main/rtos loop void tud_task (void); -// Check if there is pending events need proccessing by tud_task() +// Check if there is pending events need processing by tud_task() bool tud_task_event_ready(void); // Interrupt handler, name alias to DCD diff --git a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c index 7744a11cd..38867ea1c 100644 --- a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c @@ -78,7 +78,7 @@ void dcd_set_address (uint8_t rhport, uint8_t dev_addr) // must be called before queuing status pio_usb_device_set_address(pio_rhport, dev_addr); - pio_usb_device_endpoint_transfer(pio_rhport, 0x80, NULL, 0); + dcd_edpt_xfer(rhport, 0x80, NULL, 0); } // Wake up host @@ -118,8 +118,9 @@ void dcd_edpt_close_all (uint8_t rhport) // Submit a transfer, When complete dcd_event_xfer_complete() is invoked to notify the stack bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) { - uint8_t const pio_rhport = RHPORT_PIO(rhport); - return pio_usb_device_endpoint_transfer(pio_rhport, ep_addr, buffer, total_bytes); + (void) rhport; + pio_hw_endpoint_t *ep = pio_usb_device_get_ep(ep_addr); + return pio_usb_endpoint_transfer(ep, buffer, total_bytes); } // Submit a transfer where is managed by FIFO, When complete dcd_event_xfer_complete() is invoked to notify the stack - optional, however, must be listed in usbd.c diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index 4bd318448..e311f16d0 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -51,7 +51,7 @@ static pio_usb_configuration_t pio_host_config = PIO_USB_DEFAULT_CONFIG; bool hcd_init(uint8_t rhport) { // To run USB SOF interrupt in core1, call this init in core1 - pio_usb_host_controller_init(&pio_host_config); + pio_usb_host_init(&pio_host_config); return true; } @@ -59,13 +59,13 @@ bool hcd_init(uint8_t rhport) void hcd_port_reset(uint8_t rhport) { rhport = RHPORT_PIO(rhport); - pio_usb_hw_port_reset_start(rhport); + pio_usb_host_port_reset_start(rhport); } void hcd_port_reset_end(uint8_t rhport) { rhport = RHPORT_PIO(rhport); - pio_usb_hw_port_reset_end(rhport); + pio_usb_host_port_reset_end(rhport); } bool hcd_port_connect_status(uint8_t rhport) From a90839688c11338451729838e45ae3aaa58864fe Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 Apr 2022 13:25:38 +0700 Subject: [PATCH 28/62] add msec timeout to osal_queue_receive(), tud_task() and tuh_task() both pending for event queue with timeout of 1 ms --- src/device/usbd.c | 3 +- src/host/usbh.c | 2 +- src/osal/osal.h | 22 +++++++-------- src/osal/osal_freertos.h | 29 ++++++++++--------- src/osal/osal_mynewt.h | 22 ++++++++------- src/osal/osal_none.h | 28 +++++++++--------- src/osal/osal_pico.h | 44 +++++++++++++++-------------- src/osal/osal_rtthread.h | 28 +++++++++--------- src/osal/osal_rtx4.h | 28 +++++++++--------- src/portable/sony/cxd56/dcd_cxd56.c | 2 +- src/tusb_option.h | 2 +- 11 files changed, 109 insertions(+), 101 deletions(-) diff --git a/src/device/usbd.c b/src/device/usbd.c index 7ab2660f4..36b9c7cff 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -475,8 +475,7 @@ void tud_task (void) while (1) { dcd_event_t event; - - if ( !osal_queue_receive(_usbd_q, &event) ) return; + if ( !osal_queue_receive(_usbd_q, &event, 1) ) return; #if CFG_TUSB_DEBUG >= 2 if (event.event_id == DCD_EVENT_SETUP_RECEIVED) TU_LOG2("\r\n"); // extra line for setup diff --git a/src/host/usbh.c b/src/host/usbh.c index f534070de..ad90a1b1b 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -401,7 +401,7 @@ void tuh_task(void) while (1) { hcd_event_t event; - if ( !osal_queue_receive(_usbh_q, &event) ) return; + if ( !osal_queue_receive(_usbh_q, &event, 1) ) return; switch (event.event_id) { diff --git a/src/osal/osal.h b/src/osal/osal.h index 7111bbdb2..9d11866df 100644 --- a/src/osal/osal.h +++ b/src/osal/osal.h @@ -66,19 +66,19 @@ typedef void (*osal_task_func_t)( void * ); // OSAL Porting API // Should be implemented as static inline function in osal_port.h header /* - static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef); - static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr); - static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec); - static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl); // TODO removed + osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef); + bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr); + bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec); + void osal_semaphore_reset(osal_semaphore_t sem_hdl); // TODO removed - static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef); - static inline bool osal_mutex_lock (osal_mutex_t sem_hdl, uint32_t msec); - static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl); + osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef); + bool osal_mutex_lock (osal_mutex_t sem_hdl, uint32_t msec); + bool osal_mutex_unlock(osal_mutex_t mutex_hdl); - static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef); - static inline bool osal_queue_receive(osal_queue_t qhdl, void* data); - static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr); - static inline bool osal_queue_empty(osal_queue_t qhdl); + osal_queue_t osal_queue_create(osal_queue_def_t* qdef); + bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec); + bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr); + bool osal_queue_empty(osal_queue_t qhdl); */ //--------------------------------------------------------------------+ diff --git a/src/osal/osal_freertos.h b/src/osal/osal_freertos.h index 69a026df5..c3a0756e1 100644 --- a/src/osal/osal_freertos.h +++ b/src/osal/osal_freertos.h @@ -40,7 +40,7 @@ extern "C" { //--------------------------------------------------------------------+ // TASK API //--------------------------------------------------------------------+ -static inline void osal_task_delay(uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) { vTaskDelay( pdMS_TO_TICKS(msec) ); } @@ -51,12 +51,12 @@ static inline void osal_task_delay(uint32_t msec) typedef StaticSemaphore_t osal_semaphore_def_t; typedef SemaphoreHandle_t osal_semaphore_t; -static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) +TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) { return xSemaphoreCreateBinaryStatic(semdef); } -static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { if ( !in_isr ) { @@ -78,13 +78,13 @@ static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) } } -static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) { uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec); return xSemaphoreTake(sem_hdl, ticks); } -static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) +TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { xQueueReset(sem_hdl); } @@ -95,17 +95,17 @@ static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) typedef StaticSemaphore_t osal_mutex_def_t; typedef SemaphoreHandle_t osal_mutex_t; -static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) +TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) { return xSemaphoreCreateMutexStatic(mdef); } -static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) { return osal_semaphore_wait(mutex_hdl, msec); } -static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { return xSemaphoreGive(mutex_hdl); } @@ -114,7 +114,7 @@ static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) // QUEUE API //--------------------------------------------------------------------+ -// role device/host is used by OS NONE for mutex (disable usb isr) only +// _int_set is not used with an RTOS #define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \ static _type _name##_##buf[_depth];\ osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf }; @@ -130,17 +130,18 @@ typedef struct typedef QueueHandle_t osal_queue_t; -static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) +TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) { return xQueueCreateStatic(qdef->depth, qdef->item_sz, (uint8_t*) qdef->buf, &qdef->sq); } -static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) { - return xQueueReceive(qhdl, data, portMAX_DELAY); + uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec); + return xQueueReceive(qhdl, data, ticks); } -static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) { if ( !in_isr ) { @@ -162,7 +163,7 @@ static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in } } -static inline bool osal_queue_empty(osal_queue_t qhdl) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) { return uxQueueMessagesWaiting(qhdl) == 0; } diff --git a/src/osal/osal_mynewt.h b/src/osal/osal_mynewt.h index 78a257cd6..b8ea2087c 100644 --- a/src/osal/osal_mynewt.h +++ b/src/osal/osal_mynewt.h @@ -36,7 +36,7 @@ //--------------------------------------------------------------------+ // TASK API //--------------------------------------------------------------------+ -static inline void osal_task_delay(uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) { os_time_delay( os_time_ms_to_ticks32(msec) ); } @@ -47,18 +47,18 @@ static inline void osal_task_delay(uint32_t msec) typedef struct os_sem osal_semaphore_def_t; typedef struct os_sem* osal_semaphore_t; -static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) +TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) { return (os_sem_init(semdef, 0) == OS_OK) ? (osal_semaphore_t) semdef : NULL; } -static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { (void) in_isr; return os_sem_release(sem_hdl) == OS_OK; } -static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) { uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec); return os_sem_pend(sem_hdl, ticks) == OS_OK; @@ -75,18 +75,18 @@ static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) typedef struct os_mutex osal_mutex_def_t; typedef struct os_mutex* osal_mutex_t; -static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) +TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) { return (os_mutex_init(mdef) == OS_OK) ? (osal_mutex_t) mdef : NULL; } -static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) { uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec); return os_mutex_pend(mutex_hdl, ticks) == OS_OK; } -static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { return os_mutex_release(mutex_hdl) == OS_OK; } @@ -116,7 +116,7 @@ typedef struct typedef osal_queue_def_t* osal_queue_t; -static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) +TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) { if ( OS_OK != os_mempool_init(&qdef->mpool, qdef->depth, qdef->item_sz, qdef->buf, "usbd queue") ) return NULL; if ( OS_OK != os_mempool_init(&qdef->epool, qdef->depth, sizeof(struct os_event), qdef->evbuf, "usbd evqueue") ) return NULL; @@ -125,8 +125,10 @@ static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) return (osal_queue_t) qdef; } -static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) { + (void) msec; // os_eventq_get() does not take timeout, always behave as msec = WAIT_FOREVER + struct os_event* ev; ev = os_eventq_get(&qhdl->evq); @@ -161,7 +163,7 @@ static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in return true; } -static inline bool osal_queue_empty(osal_queue_t qhdl) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) { return STAILQ_EMPTY(&qhdl->evq.evq_list); } diff --git a/src/osal/osal_none.h b/src/osal/osal_none.h index 4217f0422..9c80e4548 100644 --- a/src/osal/osal_none.h +++ b/src/osal/osal_none.h @@ -46,13 +46,13 @@ typedef struct typedef osal_semaphore_def_t* osal_semaphore_t; -static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) +TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) { semdef->count = 0; return semdef; } -static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { (void) in_isr; sem_hdl->count++; @@ -60,7 +60,7 @@ static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) } // TODO blocking for now -static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) { (void) msec; @@ -70,7 +70,7 @@ static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) return true; } -static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) +TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) { sem_hdl->count = 0; } @@ -82,18 +82,18 @@ static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) typedef osal_semaphore_def_t osal_mutex_def_t; typedef osal_semaphore_t osal_mutex_t; -static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) +TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) { mdef->count = 1; return mdef; } -static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) { return osal_semaphore_wait(mutex_hdl, msec); } -static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { return osal_semaphore_post(mutex_hdl, false); } @@ -120,27 +120,29 @@ typedef osal_queue_def_t* osal_queue_t; } // lock queue by disable USB interrupt -static inline void _osal_q_lock(osal_queue_t qhdl) +TU_ATTR_ALWAYS_INLINE static inline void _osal_q_lock(osal_queue_t qhdl) { // disable dcd/hcd interrupt qhdl->interrupt_set(false); } // unlock queue -static inline void _osal_q_unlock(osal_queue_t qhdl) +TU_ATTR_ALWAYS_INLINE static inline void _osal_q_unlock(osal_queue_t qhdl) { // enable dcd/hcd interrupt qhdl->interrupt_set(true); } -static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) +TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) { tu_fifo_clear(&qdef->ff); return (osal_queue_t) qdef; } -static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) { + (void) msec; // not used, always behave as msec = 0 + _osal_q_lock(qhdl); bool success = tu_fifo_read(&qhdl->ff, data); _osal_q_unlock(qhdl); @@ -148,7 +150,7 @@ static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) return success; } -static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) { if (!in_isr) { _osal_q_lock(qhdl); @@ -165,7 +167,7 @@ static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in return success; } -static inline bool osal_queue_empty(osal_queue_t qhdl) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) { // Skip queue lock/unlock since this function is primarily called // with interrupt disabled before going into low power mode diff --git a/src/osal/osal_pico.h b/src/osal/osal_pico.h index 70432d22b..abef286b4 100644 --- a/src/osal/osal_pico.h +++ b/src/osal/osal_pico.h @@ -39,7 +39,7 @@ //--------------------------------------------------------------------+ // TASK API //--------------------------------------------------------------------+ -static inline void osal_task_delay(uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) { sleep_ms(msec); } @@ -49,25 +49,25 @@ static inline void osal_task_delay(uint32_t msec) //--------------------------------------------------------------------+ typedef struct semaphore osal_semaphore_def_t, *osal_semaphore_t; -static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) +TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) { sem_init(semdef, 0, 255); return semdef; } -static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { (void) in_isr; sem_release(sem_hdl); return true; } -static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) { return sem_acquire_timeout_ms(sem_hdl, msec); } -static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) +TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) { sem_reset(sem_hdl, 0); } @@ -78,21 +78,21 @@ static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) //--------------------------------------------------------------------+ typedef struct mutex osal_mutex_def_t, *osal_mutex_t; -static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) +TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) { - mutex_init(mdef); - return mdef; + mutex_init(mdef); + return mdef; } -static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) { - return mutex_enter_timeout_ms(mutex_hdl, msec); + return mutex_enter_timeout_ms(mutex_hdl, msec); } -static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { - mutex_exit(mutex_hdl); - return true; + mutex_exit(mutex_hdl); + return true; } //--------------------------------------------------------------------+ @@ -121,26 +121,28 @@ typedef osal_queue_def_t* osal_queue_t; } // lock queue by disable USB interrupt -static inline void _osal_q_lock(osal_queue_t qhdl) +TU_ATTR_ALWAYS_INLINE static inline void _osal_q_lock(osal_queue_t qhdl) { - critical_section_enter_blocking(&qhdl->critsec); + critical_section_enter_blocking(&qhdl->critsec); } // unlock queue -static inline void _osal_q_unlock(osal_queue_t qhdl) +TU_ATTR_ALWAYS_INLINE static inline void _osal_q_unlock(osal_queue_t qhdl) { - critical_section_exit(&qhdl->critsec); + critical_section_exit(&qhdl->critsec); } -static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) +TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) { critical_section_init(&qdef->critsec); tu_fifo_clear(&qdef->ff); return (osal_queue_t) qdef; } -static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) { + (void) msec; // not used, always behave as msec = 0 + // TODO: revisit... docs say that mutexes are never used from IRQ context, // however osal_queue_recieve may be. therefore my assumption is that // the fifo mutex is not populated for queues used from an IRQ context @@ -153,7 +155,7 @@ static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) return success; } -static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) { // TODO: revisit... docs say that mutexes are never used from IRQ context, // however osal_queue_recieve may be. therefore my assumption is that @@ -170,7 +172,7 @@ static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in return success; } -static inline bool osal_queue_empty(osal_queue_t qhdl) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) { // TODO: revisit; whether this is true or not currently, tu_fifo_empty is a single // volatile read. diff --git a/src/osal/osal_rtthread.h b/src/osal/osal_rtthread.h index 0845175b8..a790a5e27 100644 --- a/src/osal/osal_rtthread.h +++ b/src/osal/osal_rtthread.h @@ -37,7 +37,7 @@ extern "C" { //--------------------------------------------------------------------+ // TASK API //--------------------------------------------------------------------+ -static inline void osal_task_delay(uint32_t msec) { +TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) { rt_thread_mdelay(msec); } @@ -47,22 +47,22 @@ static inline void osal_task_delay(uint32_t msec) { typedef struct rt_semaphore osal_semaphore_def_t; typedef rt_sem_t osal_semaphore_t; -static inline osal_semaphore_t +TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t *semdef) { rt_sem_init(semdef, "tusb", 0, RT_IPC_FLAG_FIFO); return semdef; } -static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { (void) in_isr; return rt_sem_release(sem_hdl) == RT_EOK; } -static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) { +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) { return rt_sem_take(sem_hdl, rt_tick_from_millisecond(msec)) == RT_EOK; } -static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { +TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { // TODO: implement } @@ -72,16 +72,16 @@ static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { typedef struct rt_mutex osal_mutex_def_t; typedef rt_mutex_t osal_mutex_t; -static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef) { +TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef) { rt_mutex_init(mdef, "tusb", RT_IPC_FLAG_FIFO); return mdef; } -static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) { +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) { return rt_mutex_take(mutex_hdl, rt_tick_from_millisecond(msec)) == RT_EOK; } -static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { return rt_mutex_release(mutex_hdl) == RT_EOK; } @@ -104,22 +104,24 @@ typedef struct { typedef rt_mq_t osal_queue_t; -static inline osal_queue_t osal_queue_create(osal_queue_def_t *qdef) { +TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t *qdef) { rt_mq_init(&(qdef->sq), "tusb", qdef->buf, qdef->item_sz, qdef->item_sz * qdef->depth, RT_IPC_FLAG_FIFO); return &(qdef->sq); } -static inline bool osal_queue_receive(osal_queue_t qhdl, void *data) { - return rt_mq_recv(qhdl, data, qhdl->msg_size, RT_WAITING_FOREVER) == RT_EOK; +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void *data, uint32_t msec) { + + rt_tick_t tick = rt_tick_from_millisecond((rt_int32_t) msec)); + return rt_mq_recv(qhdl, data, qhdl->msg_size, tick) == RT_EOK; } -static inline bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr) { +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr) { (void) in_isr; return rt_mq_send(qhdl, (void *)data, qhdl->msg_size) == RT_EOK; } -static inline bool osal_queue_empty(osal_queue_t qhdl) { +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) { return (qhdl->entry) == 0; } diff --git a/src/osal/osal_rtx4.h b/src/osal/osal_rtx4.h index 1856a5d9a..dea1c12c8 100644 --- a/src/osal/osal_rtx4.h +++ b/src/osal/osal_rtx4.h @@ -37,7 +37,7 @@ extern "C" { //--------------------------------------------------------------------+ // TASK API //--------------------------------------------------------------------+ -static inline void osal_task_delay(uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) { uint16_t hi = msec >> 16; uint16_t lo = msec; @@ -47,7 +47,7 @@ static inline void osal_task_delay(uint32_t msec) os_dly_wait(lo); } -static inline uint16_t msec2wait(uint32_t msec) { +TU_ATTR_ALWAYS_INLINE static inline uint16_t msec2wait(uint32_t msec) { if (msec == OSAL_TIMEOUT_WAIT_FOREVER) return 0xFFFF; else if (msec >= 0xFFFE) @@ -62,12 +62,12 @@ static inline uint16_t msec2wait(uint32_t msec) { typedef OS_SEM osal_semaphore_def_t; typedef OS_ID osal_semaphore_t; -static inline OS_ID osal_semaphore_create(osal_semaphore_def_t* semdef) { +TU_ATTR_ALWAYS_INLINE static inline OS_ID osal_semaphore_create(osal_semaphore_def_t* semdef) { os_sem_init(semdef, 0); return semdef; } -static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { if ( !in_isr ) { os_sem_send(sem_hdl); } else { @@ -76,11 +76,11 @@ static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { return true; } -static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) { +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) { return os_sem_wait(sem_hdl, msec2wait(msec)) != OS_R_TMO; } -static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { +TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { // TODO: implement } @@ -90,18 +90,18 @@ static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { typedef OS_MUT osal_mutex_def_t; typedef OS_ID osal_mutex_t; -static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) +TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) { os_mut_init(mdef); return mdef; } -static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) { return os_mut_wait(mutex_hdl, msec2wait(msec)) != OS_R_TMO; } -static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { return os_mut_release(mutex_hdl) == OS_R_OK; } @@ -127,23 +127,23 @@ typedef struct typedef osal_queue_def_t* osal_queue_t; -static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) +TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) { os_mbx_init(qdef->mbox, (qdef->depth + 4) * 4); _init_box(qdef->pool, ((qdef->item_sz+3)/4)*(qdef->depth) + 3, qdef->item_sz); return qdef; } -static inline bool osal_queue_receive(osal_queue_t qhdl, void* data) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) { void* buf; - os_mbx_wait(qhdl->mbox, &buf, 0xFFFF); + os_mbx_wait(qhdl->mbox, &buf, msec2wait(msec)); memcpy(data, buf, qhdl->item_sz); _free_box(qhdl->pool, buf); return true; } -static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) { void* buf = _alloc_box(qhdl->pool); memcpy(buf, data, qhdl->item_sz); @@ -158,7 +158,7 @@ static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in return true; } -static inline bool osal_queue_empty(osal_queue_t qhdl) +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) { return os_mbx_check(qhdl->mbox) == qhdl->depth; } diff --git a/src/portable/sony/cxd56/dcd_cxd56.c b/src/portable/sony/cxd56/dcd_cxd56.c index fbea03b1d..d3154e58f 100644 --- a/src/portable/sony/cxd56/dcd_cxd56.c +++ b/src/portable/sony/cxd56/dcd_cxd56.c @@ -363,7 +363,7 @@ bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer, uint16_t to if (usbdcd_driver.setup_processed) { - if (osal_queue_receive(usbdcd_driver.setup_queue, &ctrl)) + if (osal_queue_receive(usbdcd_driver.setup_queue, &ctrl, 100)) { usbdcd_driver.setup_processed = false; dcd_event_setup_received(0, (uint8_t *)&ctrl, false); diff --git a/src/tusb_option.h b/src/tusb_option.h index bd87a953c..9cacedd4c 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -286,7 +286,7 @@ #endif // mutex is only needed for RTOS TODO also required with multiple core MCUs -#define TUSB_OPT_MUTEX (CFG_TUSB_OS != OPT_OS_NONE) +#define TUSB_OPT_MUTEX (CFG_TUSB_OS != OPT_OS_NONE) //-------------------------------------------------------------------- // DEVICE OPTIONS From 669e36d674d4b67b56127d8b41928a6dd3bdca7b Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 Apr 2022 14:37:42 +0700 Subject: [PATCH 29/62] fix freertos issue when 1 tick > 1 ms --- examples/device/cdc_msc_freertos/src/main.c | 6 ++---- src/osal/osal_freertos.h | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/examples/device/cdc_msc_freertos/src/main.c b/examples/device/cdc_msc_freertos/src/main.c index 0a1c964ae..c0fc722f4 100644 --- a/examples/device/cdc_msc_freertos/src/main.c +++ b/examples/device/cdc_msc_freertos/src/main.c @@ -132,6 +132,8 @@ void usb_device_task(void* param) { // tinyusb device task tud_task(); + + tud_cdc_write_flush(); } } @@ -194,12 +196,8 @@ void cdc_task(void* params) // for throughput test e.g // $ dd if=/dev/zero of=/dev/ttyACM0 count=10000 tud_cdc_write(buf, count); - tud_cdc_write_flush(); } } - - // For ESP32-S2 this delay is essential to allow idle how to run and reset wdt - vTaskDelay(pdMS_TO_TICKS(10)); } } diff --git a/src/osal/osal_freertos.h b/src/osal/osal_freertos.h index c3a0756e1..52db336f5 100644 --- a/src/osal/osal_freertos.h +++ b/src/osal/osal_freertos.h @@ -37,6 +37,20 @@ extern "C" { #endif +TU_ATTR_ALWAYS_INLINE static inline uint32_t _osal_ms2tick(uint32_t msec) +{ + if (msec == OSAL_TIMEOUT_WAIT_FOREVER) return portMAX_DELAY; + if (msec == 0) return 0; + + uint32_t ticks = pdMS_TO_TICKS(msec); + + // configTICK_RATE_HZ is less than 1000 and 1 tick > 1 ms + // we still need to delay at least 1 tick + if (ticks == 0) ticks =1 ; + + return ticks; +} + //--------------------------------------------------------------------+ // TASK API //--------------------------------------------------------------------+ @@ -80,8 +94,7 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t se TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) { - uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec); - return xSemaphoreTake(sem_hdl, ticks); + return xSemaphoreTake(sem_hdl, _osal_ms2tick(msec)); } TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) @@ -137,8 +150,7 @@ TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_de TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) { - uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? portMAX_DELAY : pdMS_TO_TICKS(msec); - return xQueueReceive(qhdl, data, ticks); + return xQueueReceive(qhdl, data, _osal_ms2tick(msec)); } TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) From ccafb42c8261d8304fce0af46c0ddc3adf6da56f Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 Apr 2022 14:40:55 +0700 Subject: [PATCH 30/62] disable blank issues, add online docs link --- .github/ISSUE_TEMPLATE/config.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index 735a5ef22..28fd27467 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -1,4 +1,8 @@ +blank_issues_enabled: false contact_links: - name: TinyUSB Discussion url: https://github.com/hathach/tinyusb/discussions about: If you have other questions or need help, post it here. + - name: TinyUSB Docs + url: https://docs.tinyusb.org/ + about: Online documentation From b034c18077ef1350f2678f245ed1b885e9be79fc Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 Apr 2022 18:29:41 +0700 Subject: [PATCH 31/62] add tud_task_ext(), tuh_task_ext() as exteneded version that take timeout and in_isr also allow exit tud_task,tuh_task after processing all events for running other background task for user --- examples/device/cdc_msc_freertos/src/main.c | 3 +++ src/common/tusb_verify.h | 10 +++++----- src/device/usbd.c | 11 +++++++++-- src/device/usbd.h | 11 ++++++++++- src/host/usbh.c | 11 +++++++++-- src/host/usbh.h | 19 ++++++++++++++----- 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/examples/device/cdc_msc_freertos/src/main.c b/examples/device/cdc_msc_freertos/src/main.c index c0fc722f4..0d40782ef 100644 --- a/examples/device/cdc_msc_freertos/src/main.c +++ b/examples/device/cdc_msc_freertos/src/main.c @@ -198,6 +198,9 @@ void cdc_task(void* params) tud_cdc_write(buf, count); } } + + // For ESP32-Sx this delay is essential to allow idle how to run and reset watchdog + vTaskDelay(1); } } diff --git a/src/common/tusb_verify.h b/src/common/tusb_verify.h index f4a08ce2f..568bac8cc 100644 --- a/src/common/tusb_verify.h +++ b/src/common/tusb_verify.h @@ -99,8 +99,8 @@ *------------------------------------------------------------------*/ // Helper to implement optional parameter for TU_VERIFY Macro family -#define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 -#define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4 +#define _GET_3RD_ARG(arg1, arg2, arg3, ...) arg3 +#define _GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4 /*------------- Generator for TU_VERIFY and TU_VERIFY_HDLR -------------*/ #define TU_VERIFY_DEFINE(_cond, _handler, _ret) do \ @@ -116,7 +116,7 @@ #define TU_VERIFY_1ARGS(_cond) TU_VERIFY_DEFINE(_cond, , false) #define TU_VERIFY_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, , _ret) -#define TU_VERIFY(...) GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, UNUSED)(__VA_ARGS__) +#define TU_VERIFY(...) _GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, UNUSED)(__VA_ARGS__) /*------------------------------------------------------------------*/ @@ -127,7 +127,7 @@ #define TU_VERIFY_HDLR_2ARGS(_cond, _handler) TU_VERIFY_DEFINE(_cond, _handler, false) #define TU_VERIFY_HDLR_3ARGS(_cond, _handler, _ret) TU_VERIFY_DEFINE(_cond, _handler, _ret) -#define TU_VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, TU_VERIFY_HDLR_3ARGS, TU_VERIFY_HDLR_2ARGS,UNUSED)(__VA_ARGS__) +#define TU_VERIFY_HDLR(...) _GET_4TH_ARG(__VA_ARGS__, TU_VERIFY_HDLR_3ARGS, TU_VERIFY_HDLR_2ARGS,UNUSED)(__VA_ARGS__) /*------------------------------------------------------------------*/ /* ASSERT @@ -139,7 +139,7 @@ #define ASSERT_2ARGS(_cond, _ret) TU_VERIFY_DEFINE(_cond, _MESS_FAILED(); TU_BREAKPOINT(), _ret) #ifndef TU_ASSERT -#define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS,UNUSED)(__VA_ARGS__) +#define TU_ASSERT(...) _GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS,UNUSED)(__VA_ARGS__) #endif /*------------------------------------------------------------------*/ diff --git a/src/device/usbd.c b/src/device/usbd.c index 36b9c7cff..167a053e2 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -466,8 +466,10 @@ bool tud_task_event_ready(void) } @endcode */ -void tud_task (void) +void tud_task_ext(uint32_t timeout_ms, bool in_isr) { + (void) in_isr; // not implemented yet + // Skip if stack is not initialized if ( !tusb_inited() ) return; @@ -475,7 +477,7 @@ void tud_task (void) while (1) { dcd_event_t event; - if ( !osal_queue_receive(_usbd_q, &event, 1) ) return; + if ( !osal_queue_receive(_usbd_q, &event, timeout_ms) ) return; #if CFG_TUSB_DEBUG >= 2 if (event.event_id == DCD_EVENT_SETUP_RECEIVED) TU_LOG2("\r\n"); // extra line for setup @@ -592,6 +594,11 @@ void tud_task (void) TU_BREAKPOINT(); break; } + +#if CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO + // return if there is no more events, for application to run other background + if (osal_queue_empty(_usbd_q)) return; +#endif } } diff --git a/src/device/usbd.h b/src/device/usbd.h index b2bf8ba9d..30373abb9 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -43,8 +43,17 @@ bool tud_init (uint8_t rhport); // Check if device stack is already initialized bool tud_inited(void); +// Task function should be called in main/rtos loop, extended version of tud_task() +// - timeout_ms: millisecond to wait, zero = no wait, 0xFFFFFFFF = wait forever +// - in_isr: if function is called in ISR +void tud_task_ext(uint32_t timeout_ms, bool in_isr); + // Task function should be called in main/rtos loop -void tud_task (void); +TU_ATTR_ALWAYS_INLINE static inline +void tud_task (void) +{ + tud_task_ext(OSAL_TIMEOUT_WAIT_FOREVER, false); +} // Check if there is pending events need proccessing by tud_task() bool tud_task_event_ready(void); diff --git a/src/host/usbh.c b/src/host/usbh.c index ad90a1b1b..80d176142 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -392,8 +392,10 @@ bool tuh_init(uint8_t rhport) } @endcode */ -void tuh_task(void) +void tuh_task_ext(uint32_t timeout_ms, bool in_isr) { + (void) in_isr; // not implemented yet + // Skip if stack is not initialized if ( !tusb_inited() ) return; @@ -401,7 +403,7 @@ void tuh_task(void) while (1) { hcd_event_t event; - if ( !osal_queue_receive(_usbh_q, &event, 1) ) return; + if ( !osal_queue_receive(_usbh_q, &event, timeout_ms) ) return; switch (event.event_id) { @@ -497,6 +499,11 @@ void tuh_task(void) default: break; } + +#if CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO + // return if there is no more events, for application to run other background + if (osal_queue_empty(_usbh_q)) return; +#endif } } diff --git a/src/host/usbh.h b/src/host/usbh.h index e883ac90b..1580717fd 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -91,8 +91,17 @@ bool tuh_init(uint8_t rhport); // Check if host stack is already initialized bool tuh_inited(void); +// Task function should be called in main/rtos loop, extended version of tuh_task() +// - timeout_ms: millisecond to wait, zero = no wait, 0xFFFFFFFF = wait forever +// - in_isr: if function is called in ISR +void tuh_task_ext(uint32_t timeout_ms, bool in_isr); + // Task function should be called in main/rtos loop -void tuh_task(void); +TU_ATTR_ALWAYS_INLINE static inline +void tuh_task(void) +{ + tuh_task_ext(OSAL_TIMEOUT_WAIT_FOREVER, false); +} // Interrupt handler, name alias to HCD extern void hcd_int_handler(uint8_t rhport); @@ -106,8 +115,8 @@ tusb_speed_t tuh_speed_get(uint8_t daddr); bool tuh_mounted(uint8_t daddr); // Check if device is suspended -TU_ATTR_ALWAYS_INLINE -static inline bool tuh_suspended(uint8_t daddr) +TU_ATTR_ALWAYS_INLINE static inline +bool tuh_suspended(uint8_t daddr) { // TODO implement suspend & resume on host (void) daddr; @@ -115,8 +124,8 @@ static inline bool tuh_suspended(uint8_t daddr) } // Check if device is ready to communicate with -TU_ATTR_ALWAYS_INLINE -static inline bool tuh_ready(uint8_t daddr) +TU_ATTR_ALWAYS_INLINE static inline +bool tuh_ready(uint8_t daddr) { return tuh_mounted(daddr) && !tuh_suspended(daddr); } From 6a022c73dbf1551a441fafbea3a27c1a9951816f Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 Apr 2022 18:35:38 +0700 Subject: [PATCH 32/62] add note for tud_task() behavior in freertos example --- examples/device/cdc_msc_freertos/src/main.c | 5 +++-- examples/device/hid_composite_freertos/src/main.c | 4 +++- src/device/usbd.h | 2 +- src/host/usbh.h | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/device/cdc_msc_freertos/src/main.c b/examples/device/cdc_msc_freertos/src/main.c index 0d40782ef..0492f2724 100644 --- a/examples/device/cdc_msc_freertos/src/main.c +++ b/examples/device/cdc_msc_freertos/src/main.c @@ -130,10 +130,10 @@ void usb_device_task(void* param) // RTOS forever loop while (1) { - // tinyusb device task + // put this thread to waiting state until there is new events tud_task(); - tud_cdc_write_flush(); + // following code only run if tud_task() process at least 1 event } } @@ -196,6 +196,7 @@ void cdc_task(void* params) // for throughput test e.g // $ dd if=/dev/zero of=/dev/ttyACM0 count=10000 tud_cdc_write(buf, count); + tud_cdc_write_flush(); } } diff --git a/examples/device/hid_composite_freertos/src/main.c b/examples/device/hid_composite_freertos/src/main.c index 1695f968c..317b64611 100644 --- a/examples/device/hid_composite_freertos/src/main.c +++ b/examples/device/hid_composite_freertos/src/main.c @@ -132,8 +132,10 @@ void usb_device_task(void* param) // RTOS forever loop while (1) { - // tinyusb device task + // put this thread to waiting state until there is new events tud_task(); + + // following code only run if tud_task() process at least 1 event } } diff --git a/src/device/usbd.h b/src/device/usbd.h index 30373abb9..583962611 100644 --- a/src/device/usbd.h +++ b/src/device/usbd.h @@ -52,7 +52,7 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr); TU_ATTR_ALWAYS_INLINE static inline void tud_task (void) { - tud_task_ext(OSAL_TIMEOUT_WAIT_FOREVER, false); + tud_task_ext(UINT32_MAX, false); } // Check if there is pending events need proccessing by tud_task() diff --git a/src/host/usbh.h b/src/host/usbh.h index 1580717fd..c6d36fb7f 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -100,7 +100,7 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr); TU_ATTR_ALWAYS_INLINE static inline void tuh_task(void) { - tuh_task_ext(OSAL_TIMEOUT_WAIT_FOREVER, false); + tuh_task_ext(UINT32_MAX, false); } // Interrupt handler, name alias to HCD From 87572871d586856242c1fa44b1532a09e624162d Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 20 Apr 2022 18:52:15 +0700 Subject: [PATCH 33/62] update example for better cdc echo --- examples/device/cdc_msc_freertos/src/main.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/device/cdc_msc_freertos/src/main.c b/examples/device/cdc_msc_freertos/src/main.c index 0492f2724..a5e907199 100644 --- a/examples/device/cdc_msc_freertos/src/main.c +++ b/examples/device/cdc_msc_freertos/src/main.c @@ -134,6 +134,7 @@ void usb_device_task(void* param) tud_task(); // following code only run if tud_task() process at least 1 event + tud_cdc_write_flush(); } } @@ -183,7 +184,7 @@ void cdc_task(void* params) // if ( tud_cdc_connected() ) { // There are data available - if ( tud_cdc_available() ) + while ( tud_cdc_available() ) { uint8_t buf[64]; @@ -196,8 +197,9 @@ void cdc_task(void* params) // for throughput test e.g // $ dd if=/dev/zero of=/dev/ttyACM0 count=10000 tud_cdc_write(buf, count); - tud_cdc_write_flush(); } + + tud_cdc_write_flush(); } // For ESP32-Sx this delay is essential to allow idle how to run and reset watchdog From 328039058ef1c654246304a3a0acbee724278b85 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 21 Apr 2022 01:45:03 +0700 Subject: [PATCH 34/62] update lib pio --- lib/Pico-PIO-USB | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 98e3feefc..d15fffd95 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 98e3feefcd1c7218bced0ea2ea35530fd1b550e4 +Subproject commit d15fffd950018c60cba5c98d676a4fafaeea2648 From 12debd7763bfc6f55566e5f4f8c982a55f206815 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 22 Apr 2022 01:06:17 +0700 Subject: [PATCH 35/62] keep up with pio usb --- hw/bsp/rp2040/family.cmake | 1 - lib/Pico-PIO-USB | 2 +- src/common/tusb_debug.h | 4 ++++ src/portable/raspberrypi/pio_usb/dcd_pio_usb.c | 10 +++++----- src/portable/raspberrypi/pio_usb/hcd_pio_usb.c | 4 ++-- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index d872c35ea..70e18293f 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -32,7 +32,6 @@ if (NOT TARGET _rp2040_family_inclusion_marker) ${TOP}/lib/Pico-PIO-USB/pio_usb.c ${TOP}/lib/Pico-PIO-USB/pio_usb_host.c ${TOP}/lib/Pico-PIO-USB/pio_usb_device.c - ${TOP}/lib/Pico-PIO-USB/pio_usb_hw.c ${TOP}/lib/Pico-PIO-USB/usb_crc.c ) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index d15fffd95..f656d703f 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit d15fffd950018c60cba5c98d676a4fafaeea2648 +Subproject commit f656d703f3cee1e589eebb6606e204f59afb5611 diff --git a/src/common/tusb_debug.h b/src/common/tusb_debug.h index 647e8a8db..1ea2c19f5 100644 --- a/src/common/tusb_debug.h +++ b/src/common/tusb_debug.h @@ -65,6 +65,7 @@ static inline void tu_print_var(uint8_t const* buf, uint32_t bufsize) // Log with Level #define TU_LOG(n, ...) TU_XSTRCAT(TU_LOG, n)(__VA_ARGS__) #define TU_LOG_MEM(n, ...) TU_XSTRCAT3(TU_LOG, n, _MEM)(__VA_ARGS__) +#define TU_LOG_ARR(n, ...) TU_XSTRCAT3(TU_LOG, n, _ARR)(__VA_ARGS__) #define TU_LOG_VAR(n, ...) TU_XSTRCAT3(TU_LOG, n, _VAR)(__VA_ARGS__) #define TU_LOG_INT(n, ...) TU_XSTRCAT3(TU_LOG, n, _INT)(__VA_ARGS__) #define TU_LOG_HEX(n, ...) TU_XSTRCAT3(TU_LOG, n, _HEX)(__VA_ARGS__) @@ -74,6 +75,7 @@ static inline void tu_print_var(uint8_t const* buf, uint32_t bufsize) // Log Level 1: Error #define TU_LOG1 tu_printf #define TU_LOG1_MEM tu_print_mem +#define TU_LOG1_ARR(_x, _n) tu_print_var((uint8_t const*)(_x), _n) #define TU_LOG1_VAR(_x) tu_print_var((uint8_t const*)(_x), sizeof(*(_x))) #define TU_LOG1_INT(_x) tu_printf(#_x " = %ld\r\n", (unsigned long) (_x) ) #define TU_LOG1_HEX(_x) tu_printf(#_x " = %lX\r\n", (unsigned long) (_x) ) @@ -82,6 +84,7 @@ static inline void tu_print_var(uint8_t const* buf, uint32_t bufsize) #if CFG_TUSB_DEBUG >= 2 #define TU_LOG2 TU_LOG1 #define TU_LOG2_MEM TU_LOG1_MEM + #define TU_LOG2_ARR TU_LOG1_ARR #define TU_LOG2_VAR TU_LOG1_VAR #define TU_LOG2_INT TU_LOG1_INT #define TU_LOG2_HEX TU_LOG1_HEX @@ -91,6 +94,7 @@ static inline void tu_print_var(uint8_t const* buf, uint32_t bufsize) #if CFG_TUSB_DEBUG >= 3 #define TU_LOG3 TU_LOG1 #define TU_LOG3_MEM TU_LOG1_MEM + #define TU_LOG3_ARR TU_LOG1_ARR #define TU_LOG3_VAR TU_LOG1_VAR #define TU_LOG3_INT TU_LOG1_INT #define TU_LOG3_HEX TU_LOG1_HEX diff --git a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c index 38867ea1c..1cb850654 100644 --- a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c @@ -119,8 +119,8 @@ void dcd_edpt_close_all (uint8_t rhport) bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) { (void) rhport; - pio_hw_endpoint_t *ep = pio_usb_device_get_ep(ep_addr); - return pio_usb_endpoint_transfer(ep, buffer, total_bytes); + endpoint_t *ep = pio_usb_device_get_ep(ep_addr); + return pio_usb_ll_endpoint_transfer(ep, buffer, total_bytes); } // Submit a transfer where is managed by FIFO, When complete dcd_event_xfer_complete() is invoked to notify the stack - optional, however, must be listed in usbd.c @@ -137,7 +137,7 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; - pio_hw_endpoint_t *ep = pio_usb_device_get_ep(ep_addr); + endpoint_t *ep = pio_usb_device_get_ep(ep_addr); ep->stalled = true; } @@ -145,7 +145,7 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; - pio_hw_endpoint_t *ep = pio_usb_device_get_ep(ep_addr); + endpoint_t *ep = pio_usb_device_get_ep(ep_addr); ep->data_id = 0; ep->stalled = false; } @@ -188,7 +188,7 @@ static void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_ if (ep_all & mask) { - pio_hw_endpoint_t* ep = PIO_USB_HW_EP(ep_idx); + endpoint_t* ep = PIO_USB_HW_EP(ep_idx); uint8_t const tu_rhport = port - PIO_USB_HW_RPORT(0) + 1; dcd_event_xfer_complete(tu_rhport, ep->ep_num, ep->actual_len, result, true); } diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index e311f16d0..2615b2356 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -73,7 +73,7 @@ bool hcd_port_connect_status(uint8_t rhport) rhport = RHPORT_PIO(rhport); pio_hw_root_port_t *root = PIO_USB_HW_RPORT(rhport); - port_pin_status_t line_state = pio_hw_get_line_state(root); + port_pin_status_t line_state = pio_usb_ll_get_line_state(root); return line_state != PORT_PIN_SE0; } @@ -187,7 +187,7 @@ static void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_ if (ep_all & mask) { - pio_hw_endpoint_t* ep = PIO_USB_HW_EP(ep_idx); + endpoint_t* ep = PIO_USB_HW_EP(ep_idx); hcd_event_xfer_complete(ep->dev_addr, ep->ep_num, ep->actual_len, result, true); } } From 2f9b9a31be9d511d0bbb99671a2f5e7818cf8a55 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 22 Apr 2022 18:23:38 +0700 Subject: [PATCH 36/62] more with pio usb --- lib/Pico-PIO-USB | 2 +- .../raspberrypi/pio_usb/dcd_pio_usb.c | 29 ++++++++++--------- .../raspberrypi/pio_usb/hcd_pio_usb.c | 13 +++++---- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index f656d703f..496454021 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit f656d703f3cee1e589eebb6606e204f59afb5611 +Subproject commit 496454021deab00e1c425a6eb70009666fa036b3 diff --git a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c index 1cb850654..d73419fe3 100644 --- a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c @@ -31,6 +31,7 @@ #include "pico.h" #include "hardware/pio.h" #include "pio_usb.h" +#include "pio_usb_ll.h" #include "device/dcd.h" @@ -119,7 +120,7 @@ void dcd_edpt_close_all (uint8_t rhport) bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) { (void) rhport; - endpoint_t *ep = pio_usb_device_get_ep(ep_addr); + endpoint_t *ep = pio_usb_device_get_endpoint_by_address(ep_addr); return pio_usb_ll_endpoint_transfer(ep, buffer, total_bytes); } @@ -137,7 +138,7 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; - endpoint_t *ep = pio_usb_device_get_ep(ep_addr); + endpoint_t *ep = pio_usb_device_get_endpoint_by_address(ep_addr); ep->stalled = true; } @@ -145,7 +146,7 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; - endpoint_t *ep = pio_usb_device_get_ep(ep_addr); + endpoint_t *ep = pio_usb_device_get_endpoint_by_address(ep_addr); ep->data_id = 0; ep->stalled = false; } @@ -154,24 +155,24 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) // //--------------------------------------------------------------------+ -static void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_t* port, uint32_t flag) +static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* rport, uint32_t flag) { volatile uint32_t* ep_reg; xfer_result_t result; if ( flag == PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) { - ep_reg = &port->ep_complete; + ep_reg = &rport->ep_complete; result = XFER_RESULT_SUCCESS; } else if ( flag == PIO_USB_INTS_ENDPOINT_ERROR_BITS ) { - ep_reg = &port->ep_error; + ep_reg = &rport->ep_error; result = XFER_RESULT_FAILED; } else if ( flag == PIO_USB_INTS_ENDPOINT_STALLED_BITS ) { - ep_reg = &port->ep_stalled; + ep_reg = &rport->ep_stalled; result = XFER_RESULT_STALLED; } else @@ -188,8 +189,8 @@ static void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_ if (ep_all & mask) { - endpoint_t* ep = PIO_USB_HW_EP(ep_idx); - uint8_t const tu_rhport = port - PIO_USB_HW_RPORT(0) + 1; + endpoint_t* ep = PIO_USB_ENDPOINT(ep_idx); + uint8_t const tu_rhport = rport - PIO_USB_ROOT_PORT(0) + 1; dcd_event_xfer_complete(tu_rhport, ep->ep_num, ep->actual_len, result, true); } } @@ -202,8 +203,8 @@ static void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_ void __no_inline_not_in_flash_func(pio_usb_device_irq_handler)(uint8_t root_id) { uint8_t const tu_rhport = root_id + 1; - pio_hw_root_port_t* port = PIO_USB_HW_RPORT(root_id); - uint32_t const ints = port->ints; + root_port_t* rport = PIO_USB_ROOT_PORT(root_id); + uint32_t const ints = rport->ints; if (ints & PIO_USB_INTS_RESET_END_BITS) { @@ -212,16 +213,16 @@ void __no_inline_not_in_flash_func(pio_usb_device_irq_handler)(uint8_t root_id) if (ints & PIO_USB_INTS_SETUP_REQ_BITS) { - dcd_event_setup_received(tu_rhport, port->setup_packet, true); + dcd_event_setup_received(tu_rhport, rport->setup_packet, true); } if ( ints & PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) { - handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_COMPLETE_BITS); + handle_endpoint_irq(rport, PIO_USB_INTS_ENDPOINT_COMPLETE_BITS); } // clear all - port->ints &= ~ints; + rport->ints &= ~ints; } #endif diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index 2615b2356..f881716ec 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -31,6 +31,7 @@ #include "pico.h" #include "hardware/pio.h" #include "pio_usb.h" +#include "pio_usb_ll.h" //--------------------------------------------------------------------+ // INCLUDE @@ -72,8 +73,8 @@ bool hcd_port_connect_status(uint8_t rhport) { rhport = RHPORT_PIO(rhport); - pio_hw_root_port_t *root = PIO_USB_HW_RPORT(rhport); - port_pin_status_t line_state = pio_usb_ll_get_line_state(root); + root_port_t *root = PIO_USB_ROOT_PORT(rhport); + port_pin_status_t line_state = pio_usb_bus_get_line_state(root); return line_state != PORT_PIN_SE0; } @@ -82,7 +83,7 @@ tusb_speed_t hcd_port_speed_get(uint8_t rhport) { // TODO determine link speed rhport = RHPORT_PIO(rhport); - return PIO_USB_HW_RPORT(rhport)->is_fullspeed ? TUSB_SPEED_FULL : TUSB_SPEED_LOW; + return PIO_USB_ROOT_PORT(rhport)->is_fullspeed ? TUSB_SPEED_FULL : TUSB_SPEED_LOW; } // Close all opened endpoint belong to this device @@ -153,7 +154,7 @@ bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr) return true; } -static void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_t* port, uint32_t flag) +static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* port, uint32_t flag) { volatile uint32_t* ep_reg; xfer_result_t result; @@ -187,7 +188,7 @@ static void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_ if (ep_all & mask) { - endpoint_t* ep = PIO_USB_HW_EP(ep_idx); + endpoint_t* ep = PIO_USB_ENDPOINT(ep_idx); hcd_event_xfer_complete(ep->dev_addr, ep->ep_num, ep->actual_len, result, true); } } @@ -199,7 +200,7 @@ static void __no_inline_not_in_flash_func(handle_endpoint_irq)(pio_hw_root_port_ // IRQ Handler void __no_inline_not_in_flash_func(pio_usb_host_irq_handler)(uint8_t root_id) { - pio_hw_root_port_t* port = PIO_USB_HW_RPORT(root_id); + root_port_t* port = PIO_USB_ROOT_PORT(root_id); uint32_t const ints = port->ints; if ( ints & PIO_USB_INTS_CONNECT_BITS ) From 3a1c7339cd54cc03233c2337e95403260992738f Mon Sep 17 00:00:00 2001 From: Grant Slater Date: Sun, 24 Apr 2022 16:48:49 +0100 Subject: [PATCH 37/62] Replace actions/setup-ruby with replacement ruby/setup-ruby --- .github/workflows/build_arm.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_arm.yml b/.github/workflows/build_arm.yml index 177f1076e..fc5eec1df 100644 --- a/.github/workflows/build_arm.yml +++ b/.github/workflows/build_arm.yml @@ -15,7 +15,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Setup Ruby - uses: actions/setup-ruby@v1 + uses: ruby/setup-ruby@v1 with: ruby-version: '2.7' From 35d1a71ed7d3b3ea42b4d38c7cdc85233b815348 Mon Sep 17 00:00:00 2001 From: Grant Slater Date: Sun, 24 Apr 2022 16:49:29 +0100 Subject: [PATCH 38/62] GH Update to actions/checkout@v3 --- .github/workflows/build_aarch64.yml | 4 ++-- .github/workflows/build_arm.yml | 8 ++++---- .github/workflows/build_esp.yml | 4 ++-- .github/workflows/build_msp430.yml | 4 ++-- .github/workflows/build_renesas.yml | 4 ++-- .github/workflows/build_riscv.yml | 4 ++-- .github/workflows/trigger.yml | 2 +- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/workflows/build_aarch64.yml b/.github/workflows/build_aarch64.yml index 8cf7852b9..1720ba592 100644 --- a/.github/workflows/build_aarch64.yml +++ b/.github/workflows/build_aarch64.yml @@ -24,13 +24,13 @@ jobs: uses: actions/setup-python@v2 - name: Checkout TinyUSB - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Checkout common submodules in lib run: git submodule update --init lib/FreeRTOS-Kernel lib/lwip lib/sct_neopixel - name: Checkout hathach/linkermap - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: repository: hathach/linkermap path: linkermap diff --git a/.github/workflows/build_arm.yml b/.github/workflows/build_arm.yml index fc5eec1df..133671f82 100644 --- a/.github/workflows/build_arm.yml +++ b/.github/workflows/build_arm.yml @@ -20,7 +20,7 @@ jobs: ruby-version: '2.7' - name: Checkout TinyUSB - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Unit Tests run: | @@ -66,13 +66,13 @@ jobs: uses: actions/setup-python@v2 - name: Checkout TinyUSB - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Checkout common submodules in lib run: git submodule update --init lib/FreeRTOS-Kernel lib/lwip lib/sct_neopixel - name: Checkout hathach/linkermap - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: repository: hathach/linkermap path: linkermap @@ -135,7 +135,7 @@ jobs: uses: actions/setup-python@v2 - name: Checkout TinyUSB - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Checkout common submodules in lib run: git submodule update --init lib/FreeRTOS-Kernel lib/lwip diff --git a/.github/workflows/build_esp.yml b/.github/workflows/build_esp.yml index 1ed76ef4d..6a46773b2 100644 --- a/.github/workflows/build_esp.yml +++ b/.github/workflows/build_esp.yml @@ -29,10 +29,10 @@ jobs: run: docker pull espressif/idf:latest - name: Checkout TinyUSB - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Checkout hathach/linkermap - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: repository: hathach/linkermap path: linkermap diff --git a/.github/workflows/build_msp430.yml b/.github/workflows/build_msp430.yml index 98dc36cd9..6a468ab04 100644 --- a/.github/workflows/build_msp430.yml +++ b/.github/workflows/build_msp430.yml @@ -21,13 +21,13 @@ jobs: uses: actions/setup-python@v2 - name: Checkout TinyUSB - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Checkout common submodules in lib run: git submodule update --init lib/FreeRTOS-Kernel lib/lwip - name: Checkout hathach/linkermap - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: repository: hathach/linkermap path: linkermap diff --git a/.github/workflows/build_renesas.yml b/.github/workflows/build_renesas.yml index 6eb042d72..50618ff6b 100644 --- a/.github/workflows/build_renesas.yml +++ b/.github/workflows/build_renesas.yml @@ -21,13 +21,13 @@ jobs: uses: actions/setup-python@v2 - name: Checkout TinyUSB - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Checkout common submodules in lib run: git submodule update --init lib/FreeRTOS-Kernel lib/lwip - name: Checkout hathach/linkermap - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: repository: hathach/linkermap path: linkermap diff --git a/.github/workflows/build_riscv.yml b/.github/workflows/build_riscv.yml index 0c7b2d154..2d670138d 100644 --- a/.github/workflows/build_riscv.yml +++ b/.github/workflows/build_riscv.yml @@ -22,13 +22,13 @@ jobs: uses: actions/setup-python@v2 - name: Checkout TinyUSB - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Checkout common submodules in lib run: git submodule update --init lib/FreeRTOS-Kernel lib/lwip - name: Checkout hathach/linkermap - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: repository: hathach/linkermap path: linkermap diff --git a/.github/workflows/trigger.yml b/.github/workflows/trigger.yml index 40750a2f9..780d6f4f3 100644 --- a/.github/workflows/trigger.yml +++ b/.github/workflows/trigger.yml @@ -22,7 +22,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Push to tinyusb_src run: | From 26a25279bc7196d93ca074203ac35260298df472 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 26 Apr 2022 00:35:16 +0700 Subject: [PATCH 39/62] follow pio usb changes --- lib/Pico-PIO-USB | 2 +- .../raspberrypi/pio_usb/dcd_pio_usb.c | 11 ++--- .../raspberrypi/pio_usb/hcd_pio_usb.c | 40 ++++++++++--------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 496454021..d2c70ae7b 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 496454021deab00e1c425a6eb70009666fa036b3 +Subproject commit d2c70ae7b6a122027b7c5c9aa4769fab19e8e886 diff --git a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c index d73419fe3..09be69afa 100644 --- a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c @@ -74,11 +74,8 @@ void dcd_int_disable (uint8_t rhport) // Receive Set Address request, mcu port must also include status IN response void dcd_set_address (uint8_t rhport, uint8_t dev_addr) { - uint8_t const pio_rhport = RHPORT_PIO(rhport); - // must be called before queuing status - pio_usb_device_set_address(pio_rhport, dev_addr); - + pio_usb_device_set_address(dev_addr); dcd_edpt_xfer(rhport, 0x80, NULL, 0); } @@ -107,8 +104,8 @@ void dcd_disconnect(uint8_t rhport) // Configure endpoint's registers according to descriptor bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * desc_ep) { - uint8_t const pio_rhport = RHPORT_PIO(rhport); - return pio_usb_device_endpoint_open(pio_rhport, (uint8_t const*) desc_ep); + (void) rhport; + return pio_usb_device_endpoint_open((uint8_t const*) desc_ep); } void dcd_edpt_close_all (uint8_t rhport) @@ -121,7 +118,7 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t { (void) rhport; endpoint_t *ep = pio_usb_device_get_endpoint_by_address(ep_addr); - return pio_usb_ll_endpoint_transfer(ep, buffer, total_bytes); + return pio_usb_ll_transfer_start(ep, buffer, total_bytes); } // Submit a transfer where is managed by FIFO, When complete dcd_event_xfer_complete() is invoked to notify the stack - optional, however, must be listed in usbd.c diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index f881716ec..2b3d72df8 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -59,21 +59,21 @@ bool hcd_init(uint8_t rhport) void hcd_port_reset(uint8_t rhport) { - rhport = RHPORT_PIO(rhport); - pio_usb_host_port_reset_start(rhport); + uint8_t pio_rhport = RHPORT_PIO(rhport); + pio_usb_host_port_reset_start(pio_rhport); } void hcd_port_reset_end(uint8_t rhport) { - rhport = RHPORT_PIO(rhport); - pio_usb_host_port_reset_end(rhport); + uint8_t pio_rhport = RHPORT_PIO(rhport); + pio_usb_host_port_reset_end(pio_rhport); } bool hcd_port_connect_status(uint8_t rhport) { - rhport = RHPORT_PIO(rhport); + uint8_t pio_rhport = RHPORT_PIO(rhport); - root_port_t *root = PIO_USB_ROOT_PORT(rhport); + root_port_t *root = PIO_USB_ROOT_PORT(pio_rhport); port_pin_status_t line_state = pio_usb_bus_get_line_state(root); return line_state != PORT_PIN_SE0; @@ -82,28 +82,31 @@ bool hcd_port_connect_status(uint8_t rhport) tusb_speed_t hcd_port_speed_get(uint8_t rhport) { // TODO determine link speed - rhport = RHPORT_PIO(rhport); - return PIO_USB_ROOT_PORT(rhport)->is_fullspeed ? TUSB_SPEED_FULL : TUSB_SPEED_LOW; + uint8_t pio_rhport = RHPORT_PIO(rhport); + return PIO_USB_ROOT_PORT(pio_rhport)->is_fullspeed ? TUSB_SPEED_FULL : TUSB_SPEED_LOW; } // Close all opened endpoint belong to this device void hcd_device_close(uint8_t rhport, uint8_t dev_addr) { - rhport = RHPORT_PIO(rhport); - pio_usb_host_close_device(rhport, dev_addr); + uint8_t pio_rhport = RHPORT_PIO(rhport); + pio_usb_host_close_device(pio_rhport, dev_addr); } uint32_t hcd_frame_number(uint8_t rhport) { + (void) rhport; return 0; } void hcd_int_enable(uint8_t rhport) { + (void) rhport; } void hcd_int_disable(uint8_t rhport) { + (void) rhport; } //--------------------------------------------------------------------+ @@ -116,20 +119,20 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const hcd_devtree_get_info(dev_addr, &dev_tree); bool const need_pre = (dev_tree.hub_addr && dev_tree.speed == TUSB_SPEED_LOW); - rhport = RHPORT_PIO(rhport); - return pio_usb_host_endpoint_open(rhport, dev_addr, (uint8_t const*) desc_ep, need_pre); + uint8_t pio_rhport = RHPORT_PIO(rhport); + return pio_usb_host_endpoint_open(pio_rhport, dev_addr, (uint8_t const*) desc_ep, need_pre); } bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen) { - rhport = RHPORT_PIO(rhport); - return pio_usb_host_endpoint_transfer(rhport, dev_addr, ep_addr, buffer, buflen); + uint8_t pio_rhport = RHPORT_PIO(rhport); + return pio_usb_host_endpoint_transfer(pio_rhport, dev_addr, ep_addr, buffer, buflen); } bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]) { - rhport = RHPORT_PIO(rhport); - return pio_usb_host_send_setup(rhport, dev_addr, setup_packet); + uint8_t pio_rhport = RHPORT_PIO(rhport); + return pio_usb_host_send_setup(pio_rhport, dev_addr, setup_packet); } //bool hcd_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) @@ -200,17 +203,18 @@ static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* port // IRQ Handler void __no_inline_not_in_flash_func(pio_usb_host_irq_handler)(uint8_t root_id) { + uint8_t const tu_rhport = root_id + 1; root_port_t* port = PIO_USB_ROOT_PORT(root_id); uint32_t const ints = port->ints; if ( ints & PIO_USB_INTS_CONNECT_BITS ) { - hcd_event_device_attach(root_id+1, true); + hcd_event_device_attach(tu_rhport, true); } if ( ints & PIO_USB_INTS_DISCONNECT_BITS ) { - hcd_event_device_remove(root_id+1, true); + hcd_event_device_remove(tu_rhport, true); } if ( ints & PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) From 4d11c658fffa810f271fdafb6453eff1f2b77b3f Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 26 Apr 2022 13:08:03 +0700 Subject: [PATCH 40/62] improve pio usb endpoint handler --- .../raspberrypi/pio_usb/dcd_pio_usb.c | 38 +++++---------- .../raspberrypi/pio_usb/hcd_pio_usb.c | 46 +++++-------------- 2 files changed, 23 insertions(+), 61 deletions(-) diff --git a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c index 09be69afa..77f34238a 100644 --- a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c @@ -152,32 +152,8 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) // //--------------------------------------------------------------------+ -static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* rport, uint32_t flag) +static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* rport, xfer_result_t result, volatile uint32_t* ep_reg) { - volatile uint32_t* ep_reg; - xfer_result_t result; - - if ( flag == PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) - { - ep_reg = &rport->ep_complete; - result = XFER_RESULT_SUCCESS; - } - else if ( flag == PIO_USB_INTS_ENDPOINT_ERROR_BITS ) - { - ep_reg = &rport->ep_error; - result = XFER_RESULT_FAILED; - } - else if ( flag == PIO_USB_INTS_ENDPOINT_STALLED_BITS ) - { - ep_reg = &rport->ep_stalled; - result = XFER_RESULT_STALLED; - } - else - { - // something wrong - return; - } - const uint32_t ep_all = *ep_reg; for(uint8_t ep_idx = 0; ep_idx < PIO_USB_EP_POOL_CNT; ep_idx++) @@ -215,7 +191,17 @@ void __no_inline_not_in_flash_func(pio_usb_device_irq_handler)(uint8_t root_id) if ( ints & PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) { - handle_endpoint_irq(rport, PIO_USB_INTS_ENDPOINT_COMPLETE_BITS); + handle_endpoint_irq(rport, XFER_RESULT_SUCCESS, &rport->ep_complete); + } + + if ( ints & PIO_USB_INTS_ENDPOINT_STALLED_BITS ) + { + handle_endpoint_irq(rport, XFER_RESULT_STALLED, &rport->ep_stalled); + } + + if ( ints & PIO_USB_INTS_ENDPOINT_ERROR_BITS ) + { + handle_endpoint_irq(rport, XFER_RESULT_FAILED, &rport->ep_error); } // clear all diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index 2b3d72df8..8101fb2d3 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -157,32 +157,8 @@ bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr) return true; } -static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* port, uint32_t flag) +static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* port, xfer_result_t result, volatile uint32_t* ep_reg) { - volatile uint32_t* ep_reg; - xfer_result_t result; - - if ( flag == PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) - { - ep_reg = &port->ep_complete; - result = XFER_RESULT_SUCCESS; - } - else if ( flag == PIO_USB_INTS_ENDPOINT_ERROR_BITS ) - { - ep_reg = &port->ep_error; - result = XFER_RESULT_FAILED; - } - else if ( flag == PIO_USB_INTS_ENDPOINT_STALLED_BITS ) - { - ep_reg = &port->ep_stalled; - result = XFER_RESULT_STALLED; - } - else - { - // something wrong - return; - } - const uint32_t ep_all = *ep_reg; for(uint8_t ep_idx = 0; ep_idx < PIO_USB_EP_POOL_CNT; ep_idx++) @@ -204,8 +180,8 @@ static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* port void __no_inline_not_in_flash_func(pio_usb_host_irq_handler)(uint8_t root_id) { uint8_t const tu_rhport = root_id + 1; - root_port_t* port = PIO_USB_ROOT_PORT(root_id); - uint32_t const ints = port->ints; + root_port_t* rport = PIO_USB_ROOT_PORT(root_id); + uint32_t const ints = rport->ints; if ( ints & PIO_USB_INTS_CONNECT_BITS ) { @@ -219,21 +195,21 @@ void __no_inline_not_in_flash_func(pio_usb_host_irq_handler)(uint8_t root_id) if ( ints & PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) { - handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_COMPLETE_BITS); - } - - if ( ints & PIO_USB_INTS_ENDPOINT_ERROR_BITS ) - { - handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_ERROR_BITS); + handle_endpoint_irq(rport, XFER_RESULT_SUCCESS, &rport->ep_complete); } if ( ints & PIO_USB_INTS_ENDPOINT_STALLED_BITS ) { - handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_STALLED_BITS); + handle_endpoint_irq(rport, XFER_RESULT_STALLED, &rport->ep_stalled); + } + + if ( ints & PIO_USB_INTS_ENDPOINT_ERROR_BITS ) + { + handle_endpoint_irq(rport, XFER_RESULT_FAILED, &rport->ep_error); } // clear all - port->ints &= ~ints; + rport->ints &= ~ints; } #endif From 455bddddcd051acd6d62bca97783be04fc508e96 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 26 Apr 2022 18:47:09 +0700 Subject: [PATCH 41/62] minor clean up --- lib/Pico-PIO-USB | 2 +- src/portable/raspberrypi/pio_usb/dcd_pio_usb.c | 9 ++++----- src/portable/raspberrypi/pio_usb/hcd_pio_usb.c | 3 ++- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index d2c70ae7b..125753a97 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit d2c70ae7b6a122027b7c5c9aa4769fab19e8e886 +Subproject commit 125753a9783bc1724a185770b04f83c4ebd4b6c5 diff --git a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c index 77f34238a..3daa39a8b 100644 --- a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c @@ -152,7 +152,7 @@ void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr) // //--------------------------------------------------------------------+ -static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* rport, xfer_result_t result, volatile uint32_t* ep_reg) +static void __no_inline_not_in_flash_func(handle_endpoint_irq)(uint8_t tu_rhport, xfer_result_t result, volatile uint32_t* ep_reg) { const uint32_t ep_all = *ep_reg; @@ -163,7 +163,6 @@ static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* rpor if (ep_all & mask) { endpoint_t* ep = PIO_USB_ENDPOINT(ep_idx); - uint8_t const tu_rhport = rport - PIO_USB_ROOT_PORT(0) + 1; dcd_event_xfer_complete(tu_rhport, ep->ep_num, ep->actual_len, result, true); } } @@ -191,17 +190,17 @@ void __no_inline_not_in_flash_func(pio_usb_device_irq_handler)(uint8_t root_id) if ( ints & PIO_USB_INTS_ENDPOINT_COMPLETE_BITS ) { - handle_endpoint_irq(rport, XFER_RESULT_SUCCESS, &rport->ep_complete); + handle_endpoint_irq(tu_rhport, XFER_RESULT_SUCCESS, &rport->ep_complete); } if ( ints & PIO_USB_INTS_ENDPOINT_STALLED_BITS ) { - handle_endpoint_irq(rport, XFER_RESULT_STALLED, &rport->ep_stalled); + handle_endpoint_irq(tu_rhport, XFER_RESULT_STALLED, &rport->ep_stalled); } if ( ints & PIO_USB_INTS_ENDPOINT_ERROR_BITS ) { - handle_endpoint_irq(rport, XFER_RESULT_FAILED, &rport->ep_error); + handle_endpoint_irq(tu_rhport, XFER_RESULT_FAILED, &rport->ep_error); } // clear all diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index 8101fb2d3..f6caabb54 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -157,8 +157,9 @@ bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr) return true; } -static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* port, xfer_result_t result, volatile uint32_t* ep_reg) +static void __no_inline_not_in_flash_func(handle_endpoint_irq)(root_port_t* rport, xfer_result_t result, volatile uint32_t* ep_reg) { + (void) rport; const uint32_t ep_all = *ep_reg; for(uint8_t ep_idx = 0; ep_idx < PIO_USB_EP_POOL_CNT; ep_idx++) From fd827a80a9f3b1e482cc8b1609c055cbad252b37 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 27 Apr 2022 01:09:30 +0700 Subject: [PATCH 42/62] everything work great --- lib/Pico-PIO-USB | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 125753a97..aecced02e 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 125753a9783bc1724a185770b04f83c4ebd4b6c5 +Subproject commit aecced02e756f3a35805cacdd9be25976b6dd978 From 4a661dead0899cb154325a8257d19281f2536af3 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 27 Apr 2022 17:57:52 +0700 Subject: [PATCH 43/62] add TU_ATTR_FAST_FUNC for rp2040 __not_in_flash() section --- examples/device/hid_boot_interface/src/main.c | 2 -- hw/bsp/rp2040/family.cmake | 2 +- src/common/tusb_mcu.h | 7 +++++++ src/device/usbd.c | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/examples/device/hid_boot_interface/src/main.c b/examples/device/hid_boot_interface/src/main.c index 031b17a2e..e5e2f6856 100644 --- a/examples/device/hid_boot_interface/src/main.c +++ b/examples/device/hid_boot_interface/src/main.c @@ -31,8 +31,6 @@ #include "tusb.h" #include "usb_descriptors.h" -#include "pio_usb.h" - //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF PROTYPES //--------------------------------------------------------------------+ diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index 70e18293f..bb146d085 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -47,7 +47,7 @@ if (NOT TARGET _rp2040_family_inclusion_marker) hardware_irq hardware_resets pico_sync - # for usb-pio + # for usb-pio hardware_dma hardware_pio pico_multicore diff --git a/src/common/tusb_mcu.h b/src/common/tusb_mcu.h index 8eb4ad475..c1cf2a810 100644 --- a/src/common/tusb_mcu.h +++ b/src/common/tusb_mcu.h @@ -228,6 +228,8 @@ #elif TU_CHECK_MCU(OPT_MCU_RP2040) #define TUP_DCD_ENDPOINT_MAX 16 + #define TU_ATTR_FAST_FUNC __attribute__((section(".time_critical.tinyusb"))) + //------------- Silabs -------------// #elif TU_CHECK_MCU(OPT_MCU_EFM32GG) #define TUP_USBIP_DWC2 @@ -282,4 +284,9 @@ #define TUP_RHPORT_HIGHSPEED 0x00 #endif +// fast function, normally mean placing function in SRAM +#ifndef TU_ATTR_FAST_FUNC + #define TU_ATTR_FAST_FUNC +#endif + #endif diff --git a/src/device/usbd.c b/src/device/usbd.c index 12b4071af..4c2cd3abd 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -1071,7 +1071,8 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const //--------------------------------------------------------------------+ // DCD Event Handler //--------------------------------------------------------------------+ -void __no_inline_not_in_flash_func(dcd_event_handler)(dcd_event_t const * event, bool in_isr) +TU_ATTR_FAST_FUNC +void dcd_event_handler(dcd_event_t const * event, bool in_isr) { switch (event->event_id) { From ae5490e5a5cd8650a45578b4ac70dbe89b002fb1 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 27 Apr 2022 20:52:56 +0700 Subject: [PATCH 44/62] clean up --- examples/device/hid_boot_interface/src/tusb_config.h | 6 ++---- examples/host/cdc_msc_hid/src/tusb_config.h | 7 +++---- src/common/tusb_debug.h | 6 +++--- src/device/usbd.c | 2 +- src/host/usbh.c | 6 +++--- src/portable/raspberrypi/pio_usb/dcd_pio_usb.c | 1 - src/portable/raspberrypi/pio_usb/hcd_pio_usb.c | 1 - 7 files changed, 12 insertions(+), 17 deletions(-) diff --git a/examples/device/hid_boot_interface/src/tusb_config.h b/examples/device/hid_boot_interface/src/tusb_config.h index c8bb8a807..8bc85b722 100644 --- a/examples/device/hid_boot_interface/src/tusb_config.h +++ b/examples/device/hid_boot_interface/src/tusb_config.h @@ -40,10 +40,8 @@ #endif // Use raspberry pio-usb for device -#define CFG_TUD_RPI_PIO_USB 1 -#if CFG_TUD_RPI_PIO_USB -#define BOARD_DEVICE_RHPORT_NUM 1 -#endif +// #define CFG_TUD_RPI_PIO_USB 1 +// #define BOARD_DEVICE_RHPORT_NUM 1 // RHPort number used for device can be defined by board.mk, default to port 0 #ifndef BOARD_DEVICE_RHPORT_NUM diff --git a/examples/host/cdc_msc_hid/src/tusb_config.h b/examples/host/cdc_msc_hid/src/tusb_config.h index 9d083c771..e9e68cf5a 100644 --- a/examples/host/cdc_msc_hid/src/tusb_config.h +++ b/examples/host/cdc_msc_hid/src/tusb_config.h @@ -40,13 +40,12 @@ #endif // Use raspberry pio-usb for host -#define CFG_TUH_RPI_PIO_USB 1 -//#define CFG_TUSB_RPI_PIO_INC_PATH 1 - +// #define CFG_TUH_RPI_PIO_USB 1 +// #define CFG_TUH_RPI_PIO_USB 1 #if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED) -#elif CFG_TUH_RPI_PIO_USB +#elif defined(CFG_TUH_RPI_PIO_USB) && CFG_TUH_RPI_PIO_USB // rp2040: port0 is native, port 1 for PIO-USB #define CFG_TUSB_RHPORT1_MODE OPT_MODE_HOST #else diff --git a/src/common/tusb_debug.h b/src/common/tusb_debug.h index 1ea2c19f5..ac5bee6ec 100644 --- a/src/common/tusb_debug.h +++ b/src/common/tusb_debug.h @@ -57,7 +57,7 @@ void tu_print_mem(void const *buf, uint32_t count, uint8_t indent); #define tu_printf printf #endif -static inline void tu_print_var(uint8_t const* buf, uint32_t bufsize) +static inline void tu_print_arr(uint8_t const* buf, uint32_t bufsize) { for(uint32_t i=0; i Date: Wed, 27 Apr 2022 20:54:21 +0700 Subject: [PATCH 45/62] make hcd_event_handler() fastfunc, and force inline other helper --- src/host/hcd.h | 42 +++++++++++++++++++++++++++++++++++++++--- src/host/usbh.c | 48 +----------------------------------------------- 2 files changed, 40 insertions(+), 50 deletions(-) diff --git a/src/host/hcd.h b/src/host/hcd.h index 9b41f2447..036394c72 100644 --- a/src/host/hcd.h +++ b/src/host/hcd.h @@ -171,13 +171,49 @@ extern void hcd_devtree_get_info(uint8_t dev_addr, hcd_devtree_info_t* devtree_i extern void hcd_event_handler(hcd_event_t const* event, bool in_isr); // Helper to send device attach event -extern void hcd_event_device_attach(uint8_t rhport, bool in_isr); +TU_ATTR_ALWAYS_INLINE static inline +void hcd_event_device_attach(uint8_t rhport, bool in_isr) +{ + hcd_event_t event; + event.rhport = rhport; + event.event_id = HCD_EVENT_DEVICE_ATTACH; + event.connection.hub_addr = 0; + event.connection.hub_port = 0; + hcd_event_handler(&event, in_isr); +} // Helper to send device removal event -extern void hcd_event_device_remove(uint8_t rhport, bool in_isr); +TU_ATTR_ALWAYS_INLINE static inline +void hcd_event_device_remove(uint8_t rhport, bool in_isr) +{ + hcd_event_t event; + event.rhport = rhport; + event.event_id = HCD_EVENT_DEVICE_REMOVE; + event.connection.hub_addr = 0; + event.connection.hub_port = 0; + + hcd_event_handler(&event, in_isr); +} // Helper to send USB transfer event -extern void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, uint32_t xferred_bytes, xfer_result_t result, bool in_isr); +TU_ATTR_ALWAYS_INLINE static inline +void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, uint32_t xferred_bytes, xfer_result_t result, bool in_isr) +{ + hcd_event_t event = + { + .rhport = 0, // TODO correct rhport + .event_id = HCD_EVENT_XFER_COMPLETE, + .dev_addr = dev_addr, + .xfer_complete = + { + .ep_addr = ep_addr, + .result = result, + .len = xferred_bytes + } + }; + + hcd_event_handler(&event, in_isr); +} #ifdef __cplusplus } diff --git a/src/host/usbh.c b/src/host/usbh.c index a0d9d83d3..99ba964d2 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -855,7 +855,7 @@ void hcd_devtree_get_info(uint8_t dev_addr, hcd_devtree_info_t* devtree_info) } } -void hcd_event_handler(hcd_event_t const* event, bool in_isr) +TU_ATTR_FAST_FUNC void hcd_event_handler(hcd_event_t const* event, bool in_isr) { switch (event->event_id) { @@ -865,52 +865,6 @@ void hcd_event_handler(hcd_event_t const* event, bool in_isr) } } -void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, uint32_t xferred_bytes, xfer_result_t result, bool in_isr) -{ - hcd_event_t event = - { - .rhport = 0, // TODO correct rhport - .event_id = HCD_EVENT_XFER_COMPLETE, - .dev_addr = dev_addr, - .xfer_complete = - { - .ep_addr = ep_addr, - .result = result, - .len = xferred_bytes - } - }; - - hcd_event_handler(&event, in_isr); -} - -void hcd_event_device_attach(uint8_t rhport, bool in_isr) -{ - hcd_event_t event = - { - .rhport = rhport, - .event_id = HCD_EVENT_DEVICE_ATTACH - }; - - event.connection.hub_addr = 0; - event.connection.hub_port = 0; - - hcd_event_handler(&event, in_isr); -} - -void hcd_event_device_remove(uint8_t hostid, bool in_isr) -{ - hcd_event_t event = - { - .rhport = hostid, - .event_id = HCD_EVENT_DEVICE_REMOVE - }; - - event.connection.hub_addr = 0; - event.connection.hub_port = 0; - - hcd_event_handler(&event, in_isr); -} - //--------------------------------------------------------------------+ // Descriptors Async //--------------------------------------------------------------------+ From 85dcb737749576d1c2946f3993b964c59974f38c Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 28 Apr 2022 18:00:30 +0700 Subject: [PATCH 46/62] more clean up --- lib/Pico-PIO-USB | 2 +- src/device/usbd.c | 3 +-- src/host/usbh.c | 9 +++++---- src/portable/raspberrypi/pio_usb/dcd_pio_usb.c | 1 + 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index aecced02e..6f76de3c0 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit aecced02e756f3a35805cacdd9be25976b6dd978 +Subproject commit 6f76de3c0fb4679afc9f953cd4fec0637ebd2f15 diff --git a/src/device/usbd.c b/src/device/usbd.c index e545e8056..1dd1a352e 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -1071,8 +1071,7 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const //--------------------------------------------------------------------+ // DCD Event Handler //--------------------------------------------------------------------+ -TU_ATTR_FAST_FUNC -void dcd_event_handler(dcd_event_t const * event, bool in_isr) +TU_ATTR_FAST_FUNC void dcd_event_handler(dcd_event_t const * event, bool in_isr) { switch (event->event_id) { diff --git a/src/host/usbh.c b/src/host/usbh.c index 99ba964d2..74ffb5c38 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -1139,7 +1139,7 @@ enum { //ENUM_HUB_GET_STATUS_1, ENUM_HUB_CLEAR_RESET_1, ENUM_ADDR0_DEVICE_DESC, - ENUM_RESET_2, // 2nd reset before set address + ENUM_RESET_2, // 2nd reset before set address (not used) ENUM_HUB_GET_STATUS_2, ENUM_HUB_CLEAR_RESET_2, ENUM_SET_ADDR, @@ -1227,17 +1227,17 @@ static void process_enumeration(tuh_xfer_t* xfer) } break; +#if 0 case ENUM_RESET_2: + // XXX note used by now, but may be needed for some devices !? // Reset device again before Set Address - TU_LOG2("Port reset \r\n"); + TU_LOG2("Port reset2 \r\n"); if (_dev0.hub_addr == 0) { // connected directly to roothub -#if !CFG_TUH_RPI_PIO_USB // FIXME skip this reset for pio-usb hcd_port_reset( _dev0.rhport ); osal_task_delay(RESET_DELAY); hcd_port_reset_end(_dev0.rhport); -#endif // TODO: fall through to SET ADDRESS, refactor later } #if CFG_TUH_HUB @@ -1249,6 +1249,7 @@ static void process_enumeration(tuh_xfer_t* xfer) } #endif __attribute__((fallthrough)); +#endif case ENUM_SET_ADDR: enum_request_set_addr(); diff --git a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c index fb10087b5..1bc5594d8 100644 --- a/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c @@ -135,6 +135,7 @@ void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr) { (void) rhport; endpoint_t *ep = pio_usb_device_get_endpoint_by_address(ep_addr); + ep->has_transfer = false; ep->stalled = true; } From 608577e05c9f2e2e8831d8819090ed376d08284b Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 29 Apr 2022 18:05:50 +0700 Subject: [PATCH 47/62] update pio --- lib/Pico-PIO-USB | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 6f76de3c0..7e147ad44 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 6f76de3c0fb4679afc9f953cd4fec0637ebd2f15 +Subproject commit 7e147ad44dbd5038590449418b26fb867024db6c From 0b30a10ab3c2f1fdcf4fae8f530c80bf5cffaf96 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 29 Apr 2022 22:24:36 +0700 Subject: [PATCH 48/62] configure hid_to_cdc example to build with rp2040 with pio usb as host --- examples/host/hid_to_cdc/CMakeLists.txt | 3 +- examples/host/hid_to_cdc/only.txt | 1 + examples/host/hid_to_cdc/src/main.c | 213 ++++++++++----------- examples/host/hid_to_cdc/src/tusb_config.h | 12 +- hw/bsp/rp2040/family.cmake | 56 ++++-- lib/Pico-PIO-USB | 2 +- src/host/usbh.c | 2 +- 7 files changed, 148 insertions(+), 141 deletions(-) diff --git a/examples/host/hid_to_cdc/CMakeLists.txt b/examples/host/hid_to_cdc/CMakeLists.txt index abc4d91da..ff9f58c4c 100644 --- a/examples/host/hid_to_cdc/CMakeLists.txt +++ b/examples/host/hid_to_cdc/CMakeLists.txt @@ -25,4 +25,5 @@ target_include_directories(${PROJECT} PUBLIC # Configure compilation flags and libraries for the example... see the corresponding function # in hw/bsp/FAMILY/family.cmake for details. -family_configure_device_example(${PROJECT}) \ No newline at end of file +family_configure_device_example(${PROJECT}) +family_configure_host_example(${PROJECT}) diff --git a/examples/host/hid_to_cdc/only.txt b/examples/host/hid_to_cdc/only.txt index 78d94e3ce..6ee8e3fde 100644 --- a/examples/host/hid_to_cdc/only.txt +++ b/examples/host/hid_to_cdc/only.txt @@ -1,2 +1,3 @@ board:mimxrt1060_evk board:mimxrt1064_evk +mcu:RP2040 diff --git a/examples/host/hid_to_cdc/src/main.c b/examples/host/hid_to_cdc/src/main.c index cdf5d264e..f0e78887b 100644 --- a/examples/host/hid_to_cdc/src/main.c +++ b/examples/host/hid_to_cdc/src/main.c @@ -73,12 +73,14 @@ enum { static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; void led_blinking_task(void); -void cdc_task(void); /*------------- MAIN -------------*/ int main(void) { board_init(); + + printf("TinyUSB Host HID <-> Device CDC Example\r\n"); + tusb_init(); while (1) @@ -86,15 +88,13 @@ int main(void) tud_task(); // tinyusb device task tuh_task(); // tinyusb host task led_blinking_task(); - - cdc_task(); } return 0; } //--------------------------------------------------------------------+ -// Device callbacks +// Device CDC //--------------------------------------------------------------------+ // Invoked when device is mounted @@ -124,8 +124,20 @@ void tud_resume_cb(void) blink_interval_ms = BLINK_MOUNTED; } +// Invoked when CDC interface received data from host +void tud_cdc_rx_cb(uint8_t itf) +{ + (void) itf; + + char buf[64]; + uint32_t count = tud_cdc_read(buf, sizeof(buf)); + + // TODO control LED on keyboard of host stack + (void) count; +} + //--------------------------------------------------------------------+ -// Host callbacks +// Host HID //--------------------------------------------------------------------+ // Invoked when device with hid interface is mounted @@ -137,169 +149,138 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re { (void)desc_report; (void)desc_len; + + // Interface protocol (hid_interface_protocol_enum_t) + const char* protocol_str[] = { "None", "Keyboard", "Mouse" }; + uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance); + uint16_t vid, pid; tuh_vid_pid_get(dev_addr, &vid, &pid); - printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance); - printf("VID = %04x, PID = %04x\r\n", vid, pid); + printf("[%04x:%04x][%u] HID Interface instance = %d, Protocol = %s\r\n", vid, pid, dev_addr, instance, protocol_str[itf_protocol]); - // Receive any report and treat it like a keyboard. + // Receive report from boot keyboard & mouse only // tuh_hid_report_received_cb() will be invoked when report is available - if ( !tuh_hid_receive_report(dev_addr, instance) ) + if (itf_protocol == HID_ITF_PROTOCOL_KEYBOARD || itf_protocol == HID_ITF_PROTOCOL_MOUSE) { - printf("Error: cannot request to receive report\r\n"); + if ( !tuh_hid_receive_report(dev_addr, instance) ) + { + printf("Error: cannot request report\r\n"); + } } } // Invoked when device with hid interface is un-mounted void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) { - printf("HID device address = %d, instance = %d is unmounted\r\n", dev_addr, instance); + printf("[%u] HID Interface instance = %d is unmounted\r\n", dev_addr, instance); } // keycodes from last report to check if key is holding or newly pressed uint8_t last_keycodes[6] = {0}; // look up new key in previous keys -static inline bool key_in_last_report(const uint8_t key_arr[6], uint8_t keycode) +static inline bool find_key_in_report(hid_keyboard_report_t const *report, uint8_t keycode) { for(uint8_t i=0; i<6; i++) { - if (key_arr[i] == keycode) return true; + if (report->keycode[i] == keycode) return true; } return false; } -// Invoked when received report from device via interrupt endpoint -void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) + +// convert hid keycode to ascii and print via usb device CDC (ignore non-printable) +static void process_kbd_report(hid_keyboard_report_t const *report) { - if (len != 8) - { - char ch_num; - - tud_cdc_write_str("incorrect report len: "); - - if ( len > 10 ) - { - ch_num = '0' + (len / 10); - tud_cdc_write(&ch_num, 1); - len = len % 10; - } - - ch_num = '0' + len; - tud_cdc_write(&ch_num, 1); - - tud_cdc_write_str("\r\n"); - tud_cdc_write_flush(); - - // Don't request a new report for a wrong sized endpoint. - return; - } - - uint8_t const modifiers = report[0]; + static hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released bool flush = false; - for (int i = 2; i < 8; i++) + for(uint8_t i=0; i<6; i++) { - uint8_t keycode = report[i]; - - if (keycode) + uint8_t keycode = report->keycode[i]; + if ( keycode ) { - if ( key_in_last_report(last_keycodes, keycode) ) + if ( find_key_in_report(&prev_report, keycode) ) { // exist in previous report means the current key is holding - // do nothing }else { // not existed in previous report means the current key is pressed - // Only print keycodes 0 - 128. - if (keycode < 128) - { - // remap the key code for Colemak layout so @tannewt can type. - #ifdef KEYBOARD_COLEMAK - uint8_t colemak_key_code = colemak[keycode]; - if (colemak_key_code != 0) keycode = colemak_key_code; - #endif - bool const is_shift = modifiers & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT); - char c = keycode2ascii[keycode][is_shift ? 1 : 0]; - if (c) - { - if (c == '\n') tud_cdc_write("\r", 1); - tud_cdc_write(&c, 1); - flush = true; - } + // remap the key code for Colemak layout + #ifdef KEYBOARD_COLEMAK + uint8_t colemak_key_code = colemak[keycode]; + if (colemak_key_code != 0) keycode = colemak_key_code; + #endif + + bool const is_shift = report->modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT); + uint8_t ch = keycode2ascii[keycode][is_shift ? 1 : 0]; + + if (ch) + { + if (ch == '\n') tud_cdc_write("\r", 1); + tud_cdc_write(&ch, 1); + flush = true; } } } + // TODO example skips key released } if (flush) tud_cdc_write_flush(); - // save current report - memcpy(last_keycodes, report+2, 6); + prev_report = *report; +} + +// send mouse report to usb device CDC +static void process_mouse_report(hid_mouse_report_t const * report) +{ + static hid_mouse_report_t prev_report = { 0 }; + + char tempbuf[32]; + int count; + + //------------- button state -------------// + //uint8_t button_changed_mask = report->buttons ^ prev_report.buttons; + char l = report->buttons & MOUSE_BUTTON_LEFT ? 'L' : '-'; + char m = report->buttons & MOUSE_BUTTON_MIDDLE ? 'M' : '-'; + char r = report->buttons & MOUSE_BUTTON_RIGHT ? 'R' : '-'; + + count = sprintf(tempbuf, " %c%c%c %d %d %d\r\n", l, m, r, report->x, report->y, report->wheel); + + tud_cdc_write(tempbuf, count); + tud_cdc_write_flush(); +} + +// Invoked when received report from device via interrupt endpoint +void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) +{ + uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance); + + switch(itf_protocol) + { + case HID_ITF_PROTOCOL_KEYBOARD: + process_kbd_report( (hid_keyboard_report_t const*) report ); + break; + + case HID_ITF_PROTOCOL_MOUSE: + process_mouse_report( (hid_mouse_report_t const*) report ); + break; + + default: break; + } // continue to request to receive report if ( !tuh_hid_receive_report(dev_addr, instance) ) { - printf("Error: cannot request to receive report\r\n"); + printf("Error: cannot request report\r\n"); } } - - //--------------------------------------------------------------------+ -// USB CDC -//--------------------------------------------------------------------+ -void cdc_task(void) -{ - // connected() check for DTR bit - // Most but not all terminal client set this when making connection - // if ( tud_cdc_connected() ) - { - // connected and there are data available - if ( tud_cdc_available() ) - { - // read datas - char buf[64]; - uint32_t count = tud_cdc_read(buf, sizeof(buf)); - (void) count; - - // Echo back - // Note: Skip echo by commenting out write() and write_flush() - // for throughput test e.g - // $ dd if=/dev/zero of=/dev/ttyACM0 count=10000 - tud_cdc_write(buf, count); - tud_cdc_write_flush(); - } - } -} - -// Invoked when cdc when line state changed e.g connected/disconnected -void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) -{ - (void) itf; - (void) rts; - - // TODO set some indicator - if ( dtr ) - { - // Terminal connected - }else - { - // Terminal disconnected - } -} - -// Invoked when CDC interface received data from host -void tud_cdc_rx_cb(uint8_t itf) -{ - (void) itf; -} - -//--------------------------------------------------------------------+ -// BLINKING TASK +// Blinking Task //--------------------------------------------------------------------+ void led_blinking_task(void) { diff --git a/examples/host/hid_to_cdc/src/tusb_config.h b/examples/host/hid_to_cdc/src/tusb_config.h index 9a35ffc30..0fa4899a9 100644 --- a/examples/host/hid_to_cdc/src/tusb_config.h +++ b/examples/host/hid_to_cdc/src/tusb_config.h @@ -49,6 +49,9 @@ #define BOARD_HOST_RHPORT_NUM 1 #endif +// Use raspberry pio-usb for host +#define CFG_TUH_RPI_PIO_USB 1 + // RHPort max operational speed can defined by board.mk // Default to Highspeed for MCU with internal HighSpeed PHY (can be port specific), otherwise FullSpeed #ifndef BOARD_DEVICE_RHPORT_SPEED @@ -124,10 +127,6 @@ //------------- CLASS -------------// #define CFG_TUD_CDC 1 -#define CFG_TUD_MSC 0 -#define CFG_TUD_HID 0 -#define CFG_TUD_MIDI 0 -#define CFG_TUD_VENDOR 0 // CDC FIFO size of TX and RX #define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) @@ -144,14 +143,9 @@ #define CFG_TUH_ENUMERATION_BUFSIZE 256 #define CFG_TUH_HUB 1 -#define CFG_TUH_CDC 0 -#define CFG_TUH_MSC 0 -#define CFG_TUH_VENDOR 0 - // max device support (excluding hub device) #define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports -//------------- HID -------------// #define CFG_TUH_HID 4 #define CFG_TUH_HID_EPIN_BUFSIZE 64 #define CFG_TUH_HID_EPOUT_BUFSIZE 64 diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index bb146d085..c3ea077bb 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -23,23 +23,20 @@ if (NOT TARGET _rp2040_family_inclusion_marker) set(PICO_TINYUSB_PATH ${TOP}) endif() + #------------------------------------ # Base config for both device and host; wrapped by SDK's tinyusb_common + #------------------------------------ add_library(tinyusb_common_base INTERFACE) - + target_sources(tinyusb_common_base INTERFACE ${TOP}/src/tusb.c ${TOP}/src/common/tusb_fifo.c - ${TOP}/lib/Pico-PIO-USB/pio_usb.c - ${TOP}/lib/Pico-PIO-USB/pio_usb_host.c - ${TOP}/lib/Pico-PIO-USB/pio_usb_device.c - ${TOP}/lib/Pico-PIO-USB/usb_crc.c ) target_include_directories(tinyusb_common_base INTERFACE ${TOP}/src ${TOP}/src/common ${TOP}/hw - ${TOP}/lib/Pico-PIO-USB ) target_link_libraries(tinyusb_common_base INTERFACE @@ -47,10 +44,6 @@ if (NOT TARGET _rp2040_family_inclusion_marker) hardware_irq hardware_resets pico_sync - # for usb-pio - hardware_dma - hardware_pio - pico_multicore ) set(TINYUSB_DEBUG_LEVEL 0) @@ -65,7 +58,35 @@ if (NOT TARGET _rp2040_family_inclusion_marker) CFG_TUSB_DEBUG=${TINYUSB_DEBUG_LEVEL} ) + #------------------------------------ + # PIO USB for both host and device + #------------------------------------ + add_library(tinyusb_pio_usb_base INTERFACE) + + target_sources(tinyusb_pio_usb_base INTERFACE + ${TOP}/lib/Pico-PIO-USB/pio_usb.c + ${TOP}/lib/Pico-PIO-USB/pio_usb_host.c + ${TOP}/lib/Pico-PIO-USB/pio_usb_device.c + ${TOP}/lib/Pico-PIO-USB/usb_crc.c + ) + + target_include_directories(tinyusb_pio_usb_base INTERFACE + ${TOP}/lib/Pico-PIO-USB + ) + + target_link_libraries(tinyusb_pio_usb_base INTERFACE + hardware_dma + hardware_pio + pico_multicore + ) + + target_compile_definitions(tinyusb_pio_usb_base INTERFACE + PIO_USB_USE_TINYUSB + ) + + #------------------------------------ # Base config for device mode; wrapped by SDK's tinyusb_device + #------------------------------------ add_library(tinyusb_device_base INTERFACE) target_sources(tinyusb_device_base INTERFACE ${TOP}/src/portable/raspberrypi/rp2040/dcd_rp2040.c @@ -87,7 +108,9 @@ if (NOT TARGET _rp2040_family_inclusion_marker) ${TOP}/src/class/video/video_device.c ) + #------------------------------------ # Base config for host mode; wrapped by SDK's tinyusb_host + #------------------------------------ add_library(tinyusb_host_base INTERFACE) target_sources(tinyusb_host_base INTERFACE ${TOP}/src/portable/raspberrypi/rp2040/hcd_rp2040.c @@ -105,7 +128,10 @@ if (NOT TARGET _rp2040_family_inclusion_marker) target_compile_definitions(tinyusb_host_base INTERFACE RP2040_USB_HOST_MODE=1 ) - + + #------------------------------------ + # BSP & Additions + #------------------------------------ add_library(tinyusb_bsp INTERFACE) target_sources(tinyusb_bsp INTERFACE ${TOP}/hw/bsp/rp2040/family.c @@ -139,6 +165,10 @@ if (NOT TARGET _rp2040_family_inclusion_marker) ) endif() + #------------------------------------ + # Functions + #------------------------------------ + function(family_configure_target TARGET) pico_add_extra_outputs(${TARGET}) pico_enable_stdio_uart(${TARGET} 1) @@ -147,12 +177,12 @@ if (NOT TARGET _rp2040_family_inclusion_marker) function(family_configure_device_example TARGET) family_configure_target(${TARGET}) - target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device) + target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device tinyusb_pio_usb_base) endfunction() function(family_configure_host_example TARGET) family_configure_target(${TARGET}) - target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_host) + target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_host tinyusb_pio_usb_base) endfunction() function(family_initialize_project PROJECT DIR) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 7e147ad44..1ab409f13 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 7e147ad44dbd5038590449418b26fb867024db6c +Subproject commit 1ab409f13bd888b8f1f7c9bf3bd7269d1ccc1c79 diff --git a/src/host/usbh.c b/src/host/usbh.c index 74ffb5c38..e0e41a8d1 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -630,7 +630,7 @@ static bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result if (XFER_RESULT_SUCCESS != result) { - TU_LOG2("[%u:%u] Control %s\r\n", rhport, dev_addr, result == XFER_RESULT_STALLED ? "STALLED" : "FAILED"); + TU_LOG1("[%u:%u] Control %s\r\n", rhport, dev_addr, result == XFER_RESULT_STALLED ? "STALLED" : "FAILED"); // terminate transfer if any stage failed _xfer_complete(dev_addr, result); From c289438b75af9b256a954c6a9293a21825c53e04 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 29 Apr 2022 23:03:52 +0700 Subject: [PATCH 49/62] fix ci build --- examples/host/hid_to_cdc/src/main.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/host/hid_to_cdc/src/main.c b/examples/host/hid_to_cdc/src/main.c index f0e78887b..cc811cd71 100644 --- a/examples/host/hid_to_cdc/src/main.c +++ b/examples/host/hid_to_cdc/src/main.c @@ -237,8 +237,6 @@ static void process_kbd_report(hid_keyboard_report_t const *report) // send mouse report to usb device CDC static void process_mouse_report(hid_mouse_report_t const * report) { - static hid_mouse_report_t prev_report = { 0 }; - char tempbuf[32]; int count; From f19d913b5d67ecf1b4f4d84c9d9a5880376f5a21 Mon Sep 17 00:00:00 2001 From: caleb crome Date: Thu, 5 May 2022 14:36:02 -0700 Subject: [PATCH 50/62] set irq priority for freertos --- hw/bsp/imxrt/family.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/hw/bsp/imxrt/family.c b/hw/bsp/imxrt/family.c index a811a7e40..8d34d4b9f 100644 --- a/hw/bsp/imxrt/family.c +++ b/hw/bsp/imxrt/family.c @@ -51,7 +51,10 @@ void board_init(void) SysTick_Config(SystemCoreClock / 1000); #elif CFG_TUSB_OS == OPT_OS_FREERTOS // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher ) -// NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); + NVIC_SetPriority(USB_OTG1_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); +#ifdef USB_OTG2_IRQn + NVIC_SetPriority(USB_OTG2_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY ); +#endif #endif // LED From 79b5ab822ec1a17d6bfe88f6135613ba50acedbd Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 6 May 2022 17:22:52 +0700 Subject: [PATCH 51/62] fix incorrect total number of interface --- examples/host/hid_to_cdc/src/usb_descriptors.c | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/host/hid_to_cdc/src/usb_descriptors.c b/examples/host/hid_to_cdc/src/usb_descriptors.c index 7dd73937c..e55899f65 100644 --- a/examples/host/hid_to_cdc/src/usb_descriptors.c +++ b/examples/host/hid_to_cdc/src/usb_descriptors.c @@ -81,7 +81,6 @@ enum { ITF_NUM_CDC = 0, ITF_NUM_CDC_DATA, - ITF_NUM_MSC, ITF_NUM_TOTAL }; From 26ee62222290abf016ff4eb801d1289e05e595a1 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 May 2022 12:51:48 +0700 Subject: [PATCH 52/62] move pio usb files to src --- hw/bsp/rp2040/family.cmake | 14 +++++++------- lib/Pico-PIO-USB | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index c3ea077bb..f51976d39 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -64,14 +64,14 @@ if (NOT TARGET _rp2040_family_inclusion_marker) add_library(tinyusb_pio_usb_base INTERFACE) target_sources(tinyusb_pio_usb_base INTERFACE - ${TOP}/lib/Pico-PIO-USB/pio_usb.c - ${TOP}/lib/Pico-PIO-USB/pio_usb_host.c - ${TOP}/lib/Pico-PIO-USB/pio_usb_device.c - ${TOP}/lib/Pico-PIO-USB/usb_crc.c + ${TOP}/lib/Pico-PIO-USB/src/pio_usb.c + ${TOP}/lib/Pico-PIO-USB/src/pio_usb_host.c + ${TOP}/lib/Pico-PIO-USB/src/pio_usb_device.c + ${TOP}/lib/Pico-PIO-USB/src/usb_crc.c ) target_include_directories(tinyusb_pio_usb_base INTERFACE - ${TOP}/lib/Pico-PIO-USB + ${TOP}/lib/Pico-PIO-USB/src ) target_link_libraries(tinyusb_pio_usb_base INTERFACE @@ -190,8 +190,8 @@ if (NOT TARGET _rp2040_family_inclusion_marker) _family_initialize_project(${PROJECT} ${DIR}) enable_language(C CXX ASM) pico_sdk_init() - pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/usb_tx.pio) - pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/usb_rx.pio) + pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/src/usb_tx.pio) + pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/src/usb_rx.pio) endfunction() # This method must be called from the project scope to suppress known warnings in TinyUSB source files diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 1ab409f13..d2c6c4592 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 1ab409f13bd888b8f1f7c9bf3bd7269d1ccc1c79 +Subproject commit d2c6c459277816466cc087a5e27f41d2ce257f46 From e3b57cea05b5e023bba7472bf8c583d4adc29e3e Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 May 2022 13:01:28 +0700 Subject: [PATCH 53/62] rename host/hid_to_cdc to dual/host_hid_to_device_cdc --- .../hid_to_cdc => dual/host_hid_to_device_cdc}/CMakeLists.txt | 0 .../{host/hid_to_cdc => dual/host_hid_to_device_cdc}/Makefile | 0 .../{host/hid_to_cdc => dual/host_hid_to_device_cdc}/only.txt | 0 .../{host/hid_to_cdc => dual/host_hid_to_device_cdc}/src/main.c | 0 .../hid_to_cdc => dual/host_hid_to_device_cdc}/src/tusb_config.h | 0 .../host_hid_to_device_cdc}/src/usb_descriptors.c | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename examples/{host/hid_to_cdc => dual/host_hid_to_device_cdc}/CMakeLists.txt (100%) rename examples/{host/hid_to_cdc => dual/host_hid_to_device_cdc}/Makefile (100%) rename examples/{host/hid_to_cdc => dual/host_hid_to_device_cdc}/only.txt (100%) rename examples/{host/hid_to_cdc => dual/host_hid_to_device_cdc}/src/main.c (100%) rename examples/{host/hid_to_cdc => dual/host_hid_to_device_cdc}/src/tusb_config.h (100%) rename examples/{host/hid_to_cdc => dual/host_hid_to_device_cdc}/src/usb_descriptors.c (100%) diff --git a/examples/host/hid_to_cdc/CMakeLists.txt b/examples/dual/host_hid_to_device_cdc/CMakeLists.txt similarity index 100% rename from examples/host/hid_to_cdc/CMakeLists.txt rename to examples/dual/host_hid_to_device_cdc/CMakeLists.txt diff --git a/examples/host/hid_to_cdc/Makefile b/examples/dual/host_hid_to_device_cdc/Makefile similarity index 100% rename from examples/host/hid_to_cdc/Makefile rename to examples/dual/host_hid_to_device_cdc/Makefile diff --git a/examples/host/hid_to_cdc/only.txt b/examples/dual/host_hid_to_device_cdc/only.txt similarity index 100% rename from examples/host/hid_to_cdc/only.txt rename to examples/dual/host_hid_to_device_cdc/only.txt diff --git a/examples/host/hid_to_cdc/src/main.c b/examples/dual/host_hid_to_device_cdc/src/main.c similarity index 100% rename from examples/host/hid_to_cdc/src/main.c rename to examples/dual/host_hid_to_device_cdc/src/main.c diff --git a/examples/host/hid_to_cdc/src/tusb_config.h b/examples/dual/host_hid_to_device_cdc/src/tusb_config.h similarity index 100% rename from examples/host/hid_to_cdc/src/tusb_config.h rename to examples/dual/host_hid_to_device_cdc/src/tusb_config.h diff --git a/examples/host/hid_to_cdc/src/usb_descriptors.c b/examples/dual/host_hid_to_device_cdc/src/usb_descriptors.c similarity index 100% rename from examples/host/hid_to_cdc/src/usb_descriptors.c rename to examples/dual/host_hid_to_device_cdc/src/usb_descriptors.c From 993f7b6a2c0b325683ebb4f7dc5b0039d92c1eab Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 May 2022 13:15:07 +0700 Subject: [PATCH 54/62] include dual examples to ci --- tools/build_board.py | 11 +++++------ tools/build_family.py | 11 +++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/tools/build_board.py b/tools/build_board.py index b2a80c680..4d895329a 100644 --- a/tools/build_board.py +++ b/tools/build_board.py @@ -28,12 +28,11 @@ def filter_with_input(mylist): # If examples are not specified in arguments, build all all_examples = [] -for entry in os.scandir("examples/device"): - if entry.is_dir(): - all_examples.append("device/" + entry.name) -for entry in os.scandir("examples/host"): - if entry.is_dir(): - all_examples.append("host/" + entry.name) +for dir1 in os.scandir("examples"): + if dir1.is_dir(): + for entry in os.scandir(dir1.path): + if entry.is_dir(): + all_examples.append(dir1.name + '/' + entry.name) filter_with_input(all_examples) all_examples.sort() diff --git a/tools/build_family.py b/tools/build_family.py index 4094d07db..095ca4ff7 100644 --- a/tools/build_family.py +++ b/tools/build_family.py @@ -28,12 +28,11 @@ def filter_with_input(mylist): # If examples are not specified in arguments, build all all_examples = [] -for entry in os.scandir("examples/device"): - if entry.is_dir(): - all_examples.append("device/" + entry.name) -for entry in os.scandir("examples/host"): - if entry.is_dir(): - all_examples.append("host/" + entry.name) +for dir1 in os.scandir("examples"): + if dir1.is_dir(): + for entry in os.scandir(dir1.path): + if entry.is_dir(): + all_examples.append(dir1.name + '/' + entry.name) filter_with_input(all_examples) all_examples.sort() From 717a47412548923123141503749f721184222cf0 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 May 2022 21:56:47 +0700 Subject: [PATCH 55/62] move pio-usb init to family_configure_pio_usb_example() to stay compatible --- examples/dual/host_hid_to_device_cdc/CMakeLists.txt | 1 + examples/host/bare_api/CMakeLists.txt | 5 ++++- examples/host/cdc_msc_hid/CMakeLists.txt | 3 +++ examples/host/hid_controller/CMakeLists.txt | 5 ++++- hw/bsp/rp2040/family.cmake | 13 +++++++++---- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/examples/dual/host_hid_to_device_cdc/CMakeLists.txt b/examples/dual/host_hid_to_device_cdc/CMakeLists.txt index ff9f58c4c..bf3e042d1 100644 --- a/examples/dual/host_hid_to_device_cdc/CMakeLists.txt +++ b/examples/dual/host_hid_to_device_cdc/CMakeLists.txt @@ -27,3 +27,4 @@ target_include_directories(${PROJECT} PUBLIC # in hw/bsp/FAMILY/family.cmake for details. family_configure_device_example(${PROJECT}) family_configure_host_example(${PROJECT}) +family_configure_pio_usb_example(${PROJECT}) diff --git a/examples/host/bare_api/CMakeLists.txt b/examples/host/bare_api/CMakeLists.txt index bff281a8c..e2e8d4fd5 100644 --- a/examples/host/bare_api/CMakeLists.txt +++ b/examples/host/bare_api/CMakeLists.txt @@ -24,4 +24,7 @@ target_include_directories(${PROJECT} PUBLIC # Configure compilation flags and libraries for the example... see the corresponding function # in hw/bsp/FAMILY/family.cmake for details. -family_configure_host_example(${PROJECT}) \ No newline at end of file +family_configure_host_example(${PROJECT}) + +# For rp2040, un-comment to enable pico-pio-usb +# family_configure_pio_usb_example(${PROJECT}) diff --git a/examples/host/cdc_msc_hid/CMakeLists.txt b/examples/host/cdc_msc_hid/CMakeLists.txt index 95c07339e..ca0f8e48a 100644 --- a/examples/host/cdc_msc_hid/CMakeLists.txt +++ b/examples/host/cdc_msc_hid/CMakeLists.txt @@ -27,3 +27,6 @@ target_include_directories(${PROJECT} PUBLIC # Configure compilation flags and libraries for the example... see the corresponding function # in hw/bsp/FAMILY/family.cmake for details. family_configure_host_example(${PROJECT}) + +# For rp2040, un-comment to enable pico-pio-usb +# family_configure_pio_usb_example(${PROJECT}) diff --git a/examples/host/hid_controller/CMakeLists.txt b/examples/host/hid_controller/CMakeLists.txt index aaf8bc34f..60678fc1c 100644 --- a/examples/host/hid_controller/CMakeLists.txt +++ b/examples/host/hid_controller/CMakeLists.txt @@ -25,4 +25,7 @@ target_include_directories(${PROJECT} PUBLIC # Configure compilation flags and libraries for the example... see the corresponding function # in hw/bsp/FAMILY/family.cmake for details. -family_configure_host_example(${PROJECT}) \ No newline at end of file +family_configure_host_example(${PROJECT}) + +# For rp2040, un-comment to enable pico-pio-usb +# family_configure_pio_usb_example(${PROJECT}) diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index f51976d39..06ca74988 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -177,12 +177,19 @@ if (NOT TARGET _rp2040_family_inclusion_marker) function(family_configure_device_example TARGET) family_configure_target(${TARGET}) - target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device tinyusb_pio_usb_base) + target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_device) endfunction() function(family_configure_host_example TARGET) family_configure_target(${TARGET}) - target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_host tinyusb_pio_usb_base) + target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_host) + endfunction() + + function(family_configure_pio_usb_example TARGET) + family_configure_target(${TARGET}) + target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_pio_usb_base) + pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/src/usb_tx.pio) + pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/src/usb_rx.pio) endfunction() function(family_initialize_project PROJECT DIR) @@ -190,8 +197,6 @@ if (NOT TARGET _rp2040_family_inclusion_marker) _family_initialize_project(${PROJECT} ${DIR}) enable_language(C CXX ASM) pico_sdk_init() - pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/src/usb_tx.pio) - pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/src/usb_rx.pio) endfunction() # This method must be called from the project scope to suppress known warnings in TinyUSB source files From 35b77a4a81b62da0558630a7148fb3934faa4391 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 May 2022 22:14:00 +0700 Subject: [PATCH 56/62] cmake lib rename --- .../dual/host_hid_to_device_cdc/CMakeLists.txt | 2 +- examples/host/bare_api/CMakeLists.txt | 2 +- examples/host/cdc_msc_hid/CMakeLists.txt | 2 +- examples/host/hid_controller/CMakeLists.txt | 2 +- hw/bsp/rp2040/family.cmake | 14 +++++++------- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/dual/host_hid_to_device_cdc/CMakeLists.txt b/examples/dual/host_hid_to_device_cdc/CMakeLists.txt index bf3e042d1..24ebfdec3 100644 --- a/examples/dual/host_hid_to_device_cdc/CMakeLists.txt +++ b/examples/dual/host_hid_to_device_cdc/CMakeLists.txt @@ -27,4 +27,4 @@ target_include_directories(${PROJECT} PUBLIC # in hw/bsp/FAMILY/family.cmake for details. family_configure_device_example(${PROJECT}) family_configure_host_example(${PROJECT}) -family_configure_pio_usb_example(${PROJECT}) +family_configure_pico_pio_usb_example(${PROJECT}) diff --git a/examples/host/bare_api/CMakeLists.txt b/examples/host/bare_api/CMakeLists.txt index e2e8d4fd5..607ef22eb 100644 --- a/examples/host/bare_api/CMakeLists.txt +++ b/examples/host/bare_api/CMakeLists.txt @@ -27,4 +27,4 @@ target_include_directories(${PROJECT} PUBLIC family_configure_host_example(${PROJECT}) # For rp2040, un-comment to enable pico-pio-usb -# family_configure_pio_usb_example(${PROJECT}) +# family_configure_pico_pio_usb_example(${PROJECT}) diff --git a/examples/host/cdc_msc_hid/CMakeLists.txt b/examples/host/cdc_msc_hid/CMakeLists.txt index ca0f8e48a..214e9f2e7 100644 --- a/examples/host/cdc_msc_hid/CMakeLists.txt +++ b/examples/host/cdc_msc_hid/CMakeLists.txt @@ -29,4 +29,4 @@ target_include_directories(${PROJECT} PUBLIC family_configure_host_example(${PROJECT}) # For rp2040, un-comment to enable pico-pio-usb -# family_configure_pio_usb_example(${PROJECT}) +# family_configure_pico_pio_usb_example(${PROJECT}) diff --git a/examples/host/hid_controller/CMakeLists.txt b/examples/host/hid_controller/CMakeLists.txt index 60678fc1c..6227e0790 100644 --- a/examples/host/hid_controller/CMakeLists.txt +++ b/examples/host/hid_controller/CMakeLists.txt @@ -28,4 +28,4 @@ target_include_directories(${PROJECT} PUBLIC family_configure_host_example(${PROJECT}) # For rp2040, un-comment to enable pico-pio-usb -# family_configure_pio_usb_example(${PROJECT}) +# family_configure_pico_pio_usb_example(${PROJECT}) diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index 06ca74988..f52274c15 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -61,26 +61,26 @@ if (NOT TARGET _rp2040_family_inclusion_marker) #------------------------------------ # PIO USB for both host and device #------------------------------------ - add_library(tinyusb_pio_usb_base INTERFACE) + add_library(pico_pio_usb INTERFACE) - target_sources(tinyusb_pio_usb_base INTERFACE + target_sources(pico_pio_usb INTERFACE ${TOP}/lib/Pico-PIO-USB/src/pio_usb.c ${TOP}/lib/Pico-PIO-USB/src/pio_usb_host.c ${TOP}/lib/Pico-PIO-USB/src/pio_usb_device.c ${TOP}/lib/Pico-PIO-USB/src/usb_crc.c ) - target_include_directories(tinyusb_pio_usb_base INTERFACE + target_include_directories(pico_pio_usb INTERFACE ${TOP}/lib/Pico-PIO-USB/src ) - target_link_libraries(tinyusb_pio_usb_base INTERFACE + target_link_libraries(pico_pio_usb INTERFACE hardware_dma hardware_pio pico_multicore ) - target_compile_definitions(tinyusb_pio_usb_base INTERFACE + target_compile_definitions(pico_pio_usb INTERFACE PIO_USB_USE_TINYUSB ) @@ -185,9 +185,9 @@ if (NOT TARGET _rp2040_family_inclusion_marker) target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_host) endfunction() - function(family_configure_pio_usb_example TARGET) + function(family_configure_pico_pio_usb_example TARGET) family_configure_target(${TARGET}) - target_link_libraries(${TARGET} PUBLIC pico_stdlib tinyusb_pio_usb_base) + target_link_libraries(${TARGET} PUBLIC pico_stdlib pico_pio_usb) pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/src/usb_tx.pio) pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/src/usb_rx.pio) endfunction() From 8473ca16fb74edcb32a050ef087d5f30f50c599b Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 May 2022 22:56:06 +0700 Subject: [PATCH 57/62] clean up --- .../dual/host_hid_to_device_cdc/src/main.c | 3 --- src/portable/raspberrypi/pio_usb/hcd_pio_usb.c | 18 ++++++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/examples/dual/host_hid_to_device_cdc/src/main.c b/examples/dual/host_hid_to_device_cdc/src/main.c index cc811cd71..c05d65c9e 100644 --- a/examples/dual/host_hid_to_device_cdc/src/main.c +++ b/examples/dual/host_hid_to_device_cdc/src/main.c @@ -176,9 +176,6 @@ void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) printf("[%u] HID Interface instance = %d is unmounted\r\n", dev_addr, instance); } -// keycodes from last report to check if key is holding or newly pressed -uint8_t last_keycodes[6] = {0}; - // look up new key in previous keys static inline bool find_key_in_report(hid_keyboard_report_t const *report, uint8_t keycode) { diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c index 3c7378561..1b2d29331 100644 --- a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -50,6 +50,8 @@ static pio_usb_configuration_t pio_host_config = PIO_USB_DEFAULT_CONFIG; //--------------------------------------------------------------------+ bool hcd_init(uint8_t rhport) { + (void) rhport; + // To run USB SOF interrupt in core1, call this init in core1 pio_usb_host_init(&pio_host_config); @@ -58,19 +60,19 @@ bool hcd_init(uint8_t rhport) void hcd_port_reset(uint8_t rhport) { - uint8_t pio_rhport = RHPORT_PIO(rhport); + uint8_t const pio_rhport = RHPORT_PIO(rhport); pio_usb_host_port_reset_start(pio_rhport); } void hcd_port_reset_end(uint8_t rhport) { - uint8_t pio_rhport = RHPORT_PIO(rhport); + uint8_t const pio_rhport = RHPORT_PIO(rhport); pio_usb_host_port_reset_end(pio_rhport); } bool hcd_port_connect_status(uint8_t rhport) { - uint8_t pio_rhport = RHPORT_PIO(rhport); + uint8_t const pio_rhport = RHPORT_PIO(rhport); root_port_t *root = PIO_USB_ROOT_PORT(pio_rhport); port_pin_status_t line_state = pio_usb_bus_get_line_state(root); @@ -81,14 +83,14 @@ bool hcd_port_connect_status(uint8_t rhport) tusb_speed_t hcd_port_speed_get(uint8_t rhport) { // TODO determine link speed - uint8_t pio_rhport = RHPORT_PIO(rhport); + uint8_t const pio_rhport = RHPORT_PIO(rhport); return PIO_USB_ROOT_PORT(pio_rhport)->is_fullspeed ? TUSB_SPEED_FULL : TUSB_SPEED_LOW; } // Close all opened endpoint belong to this device void hcd_device_close(uint8_t rhport, uint8_t dev_addr) { - uint8_t pio_rhport = RHPORT_PIO(rhport); + uint8_t const pio_rhport = RHPORT_PIO(rhport); pio_usb_host_close_device(pio_rhport, dev_addr); } @@ -118,19 +120,19 @@ bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const hcd_devtree_get_info(dev_addr, &dev_tree); bool const need_pre = (dev_tree.hub_addr && dev_tree.speed == TUSB_SPEED_LOW); - uint8_t pio_rhport = RHPORT_PIO(rhport); + uint8_t const pio_rhport = RHPORT_PIO(rhport); return pio_usb_host_endpoint_open(pio_rhport, dev_addr, (uint8_t const*) desc_ep, need_pre); } bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen) { - uint8_t pio_rhport = RHPORT_PIO(rhport); + uint8_t const pio_rhport = RHPORT_PIO(rhport); return pio_usb_host_endpoint_transfer(pio_rhport, dev_addr, ep_addr, buffer, buflen); } bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8]) { - uint8_t pio_rhport = RHPORT_PIO(rhport); + uint8_t const pio_rhport = RHPORT_PIO(rhport); return pio_usb_host_send_setup(pio_rhport, dev_addr, setup_packet); } From 8bd923f5f17fd124f9b479df031ecb82cd6cc2d9 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 11 May 2022 00:37:45 +0700 Subject: [PATCH 58/62] sync with pio usb --- lib/Pico-PIO-USB | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index d2c6c4592..9d2b7dae4 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit d2c6c459277816466cc087a5e27f41d2ce257f46 +Subproject commit 9d2b7dae4261e0f6a336a125972e16aab65be722 From f09df55ab986deea6d2df1168b1ad6f8eddd2ff1 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 11 May 2022 15:40:02 +0700 Subject: [PATCH 59/62] clean up example --- .../dual/host_hid_to_device_cdc/src/main.c | 28 +++++++++++-------- lib/Pico-PIO-USB | 2 +- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/examples/dual/host_hid_to_device_cdc/src/main.c b/examples/dual/host_hid_to_device_cdc/src/main.c index c05d65c9e..3e5724451 100644 --- a/examples/dual/host_hid_to_device_cdc/src/main.c +++ b/examples/dual/host_hid_to_device_cdc/src/main.c @@ -23,10 +23,8 @@ * */ -// This example runs both host and device concurrently. The USB host looks for -// any HID device with reports that are 8 bytes long and then assumes they are -// keyboard reports. It translates the keypresses of the reports to ASCII and -// transmits it over CDC to the device's host. +// This example runs both host and device concurrently. The USB host receive +// reports from HID device and print it out over USB Device CDC interface. #include #include @@ -157,7 +155,11 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re uint16_t vid, pid; tuh_vid_pid_get(dev_addr, &vid, &pid); - printf("[%04x:%04x][%u] HID Interface instance = %d, Protocol = %s\r\n", vid, pid, dev_addr, instance, protocol_str[itf_protocol]); + char tempbuf[256]; + int count = sprintf(tempbuf, "[%04x:%04x][%u] HID Interface instance = %d, Protocol = %s\r\n", vid, pid, dev_addr, instance, protocol_str[itf_protocol]); + + tud_cdc_write(tempbuf, count); + tud_cdc_write_flush(); // Receive report from boot keyboard & mouse only // tuh_hid_report_received_cb() will be invoked when report is available @@ -165,7 +167,7 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re { if ( !tuh_hid_receive_report(dev_addr, instance) ) { - printf("Error: cannot request report\r\n"); + tud_cdc_write_str("Error: cannot request report\r\n"); } } } @@ -173,7 +175,10 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re // Invoked when device with hid interface is un-mounted void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) { - printf("[%u] HID Interface instance = %d is unmounted\r\n", dev_addr, instance); + char tempbuf[256]; + int count = sprintf(tempbuf, "[%u] HID Interface instance = %d is unmounted\r\n", dev_addr, instance); + tud_cdc_write(tempbuf, count); + tud_cdc_write_flush(); } // look up new key in previous keys @@ -234,16 +239,14 @@ static void process_kbd_report(hid_keyboard_report_t const *report) // send mouse report to usb device CDC static void process_mouse_report(hid_mouse_report_t const * report) { - char tempbuf[32]; - int count; - //------------- button state -------------// //uint8_t button_changed_mask = report->buttons ^ prev_report.buttons; char l = report->buttons & MOUSE_BUTTON_LEFT ? 'L' : '-'; char m = report->buttons & MOUSE_BUTTON_MIDDLE ? 'M' : '-'; char r = report->buttons & MOUSE_BUTTON_RIGHT ? 'R' : '-'; - count = sprintf(tempbuf, " %c%c%c %d %d %d\r\n", l, m, r, report->x, report->y, report->wheel); + char tempbuf[32]; + int count = sprintf(tempbuf, "%c%c%c %d %d %d\r\n", l, m, r, report->x, report->y, report->wheel); tud_cdc_write(tempbuf, count); tud_cdc_write_flush(); @@ -252,6 +255,7 @@ static void process_mouse_report(hid_mouse_report_t const * report) // Invoked when received report from device via interrupt endpoint void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len) { + (void) len; uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance); switch(itf_protocol) @@ -270,7 +274,7 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons // continue to request to receive report if ( !tuh_hid_receive_report(dev_addr, instance) ) { - printf("Error: cannot request report\r\n"); + tud_cdc_write_str("Error: cannot request report\r\n"); } } diff --git a/lib/Pico-PIO-USB b/lib/Pico-PIO-USB index 9d2b7dae4..53ec09168 160000 --- a/lib/Pico-PIO-USB +++ b/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 9d2b7dae4261e0f6a336a125972e16aab65be722 +Subproject commit 53ec09168c5a3cd30d8dcb577fa97dc328d769d4 From be2a51316cd708a99899732acf43920537b97526 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 16 May 2022 13:29:12 +0700 Subject: [PATCH 60/62] remove pico-usb fork --- .gitmodules | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitmodules b/.gitmodules index e1489d303..21bbf08c0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -146,6 +146,3 @@ [submodule "hw/mcu/allwinner"] path = hw/mcu/allwinner url = https://github.com/hathach/allwinner_driver.git -[submodule "lib/Pico-PIO-USB"] - path = lib/Pico-PIO-USB - url = https://github.com/hathach/Pico-PIO-USB.git From 4586d8f119f779d1ef5f937e5c79e0ea2c84579c Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 16 May 2022 13:44:06 +0700 Subject: [PATCH 61/62] add upstream Pico-PIO-USB --- .github/workflows/build_arm.yml | 2 +- .gitmodules | 3 +++ hw/bsp/rp2040/family.cmake | 18 +++++++++++------- hw/mcu/raspberry_pi/Pico-PIO-USB | 1 + 4 files changed, 16 insertions(+), 8 deletions(-) create mode 160000 hw/mcu/raspberry_pi/Pico-PIO-USB diff --git a/.github/workflows/build_arm.yml b/.github/workflows/build_arm.yml index 085274eb5..bec879a93 100644 --- a/.github/workflows/build_arm.yml +++ b/.github/workflows/build_arm.yml @@ -82,7 +82,7 @@ jobs: run: | git clone --depth 1 -b develop https://github.com/raspberrypi/pico-sdk ~/pico-sdk echo >> $GITHUB_ENV PICO_SDK_PATH=~/pico-sdk - git submodule update --init lib/Pico-PIO-USB + git submodule update --init hw/mcu/raspberry_pi/Pico-PIO-USB - name: Set Toolchain URL run: echo >> $GITHUB_ENV TOOLCHAIN_URL=https://github.com/xpack-dev-tools/arm-none-eabi-gcc-xpack/releases/download/v10.2.1-1.1/xpack-arm-none-eabi-gcc-10.2.1-1.1-linux-x64.tar.gz diff --git a/.gitmodules b/.gitmodules index 21bbf08c0..044ac24ec 100644 --- a/.gitmodules +++ b/.gitmodules @@ -146,3 +146,6 @@ [submodule "hw/mcu/allwinner"] path = hw/mcu/allwinner url = https://github.com/hathach/allwinner_driver.git +[submodule "hw/mcu/raspberry_pi/Pico-PIO-USB"] + path = hw/mcu/raspberry_pi/Pico-PIO-USB + url = https://github.com/sekigon-gonnoc/Pico-PIO-USB.git diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index f52274c15..17ae3de9b 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -63,15 +63,19 @@ if (NOT TARGET _rp2040_family_inclusion_marker) #------------------------------------ add_library(pico_pio_usb INTERFACE) + if (NOT DEFINED PICO_PIO_USB_PATH) + set(PICO_PIO_USB_PATH "${TOP}/hw/mcu/raspberry_pi/Pico-PIO-USB") + endif() + target_sources(pico_pio_usb INTERFACE - ${TOP}/lib/Pico-PIO-USB/src/pio_usb.c - ${TOP}/lib/Pico-PIO-USB/src/pio_usb_host.c - ${TOP}/lib/Pico-PIO-USB/src/pio_usb_device.c - ${TOP}/lib/Pico-PIO-USB/src/usb_crc.c + ${PICO_PIO_USB_PATH}/src/pio_usb.c + ${PICO_PIO_USB_PATH}/src/pio_usb_host.c + ${PICO_PIO_USB_PATH}/src/pio_usb_device.c + ${PICO_PIO_USB_PATH}/src/usb_crc.c ) target_include_directories(pico_pio_usb INTERFACE - ${TOP}/lib/Pico-PIO-USB/src + ${PICO_PIO_USB_PATH}/src ) target_link_libraries(pico_pio_usb INTERFACE @@ -188,8 +192,8 @@ if (NOT TARGET _rp2040_family_inclusion_marker) function(family_configure_pico_pio_usb_example TARGET) family_configure_target(${TARGET}) target_link_libraries(${TARGET} PUBLIC pico_stdlib pico_pio_usb) - pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/src/usb_tx.pio) - pico_generate_pio_header(tinyusb_common_base ${TOP}/lib/Pico-PIO-USB/src/usb_rx.pio) + pico_generate_pio_header(tinyusb_common_base ${PICO_PIO_USB_PATH}/src/usb_tx.pio) + pico_generate_pio_header(tinyusb_common_base ${PICO_PIO_USB_PATH}/src/usb_rx.pio) endfunction() function(family_initialize_project PROJECT DIR) diff --git a/hw/mcu/raspberry_pi/Pico-PIO-USB b/hw/mcu/raspberry_pi/Pico-PIO-USB new file mode 160000 index 000000000..2a9fefd6c --- /dev/null +++ b/hw/mcu/raspberry_pi/Pico-PIO-USB @@ -0,0 +1 @@ +Subproject commit 2a9fefd6ccf42e5d8570ae83fdc54c9c875ebdd1 From e0e9426c2a912c2c405da602b1bf8d75d16f92e7 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 16 May 2022 16:00:15 +0700 Subject: [PATCH 62/62] print addres of mouse input --- examples/dual/host_hid_to_device_cdc/src/main.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/examples/dual/host_hid_to_device_cdc/src/main.c b/examples/dual/host_hid_to_device_cdc/src/main.c index 3e5724451..62ecb2c3f 100644 --- a/examples/dual/host_hid_to_device_cdc/src/main.c +++ b/examples/dual/host_hid_to_device_cdc/src/main.c @@ -156,7 +156,7 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re tuh_vid_pid_get(dev_addr, &vid, &pid); char tempbuf[256]; - int count = sprintf(tempbuf, "[%04x:%04x][%u] HID Interface instance = %d, Protocol = %s\r\n", vid, pid, dev_addr, instance, protocol_str[itf_protocol]); + int count = sprintf(tempbuf, "[%04x:%04x][%u] HID Interface%u, Protocol = %s\r\n", vid, pid, dev_addr, instance, protocol_str[itf_protocol]); tud_cdc_write(tempbuf, count); tud_cdc_write_flush(); @@ -176,7 +176,7 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_re void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) { char tempbuf[256]; - int count = sprintf(tempbuf, "[%u] HID Interface instance = %d is unmounted\r\n", dev_addr, instance); + int count = sprintf(tempbuf, "[%u] HID Interface%u is unmounted\r\n", dev_addr, instance); tud_cdc_write(tempbuf, count); tud_cdc_write_flush(); } @@ -194,8 +194,9 @@ static inline bool find_key_in_report(hid_keyboard_report_t const *report, uint8 // convert hid keycode to ascii and print via usb device CDC (ignore non-printable) -static void process_kbd_report(hid_keyboard_report_t const *report) +static void process_kbd_report(uint8_t dev_addr, hid_keyboard_report_t const *report) { + (void) dev_addr; static hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released bool flush = false; @@ -237,7 +238,7 @@ static void process_kbd_report(hid_keyboard_report_t const *report) } // send mouse report to usb device CDC -static void process_mouse_report(hid_mouse_report_t const * report) +static void process_mouse_report(uint8_t dev_addr, hid_mouse_report_t const * report) { //------------- button state -------------// //uint8_t button_changed_mask = report->buttons ^ prev_report.buttons; @@ -246,7 +247,7 @@ static void process_mouse_report(hid_mouse_report_t const * report) char r = report->buttons & MOUSE_BUTTON_RIGHT ? 'R' : '-'; char tempbuf[32]; - int count = sprintf(tempbuf, "%c%c%c %d %d %d\r\n", l, m, r, report->x, report->y, report->wheel); + int count = sprintf(tempbuf, "[%u] %c%c%c %d %d %d\r\n", dev_addr, l, m, r, report->x, report->y, report->wheel); tud_cdc_write(tempbuf, count); tud_cdc_write_flush(); @@ -261,11 +262,11 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons switch(itf_protocol) { case HID_ITF_PROTOCOL_KEYBOARD: - process_kbd_report( (hid_keyboard_report_t const*) report ); + process_kbd_report(dev_addr, (hid_keyboard_report_t const*) report ); break; case HID_ITF_PROTOCOL_MOUSE: - process_mouse_report( (hid_mouse_report_t const*) report ); + process_mouse_report(dev_addr, (hid_mouse_report_t const*) report ); break; default: break;