From 1d29817139c473971ce239bbc2cb5924de4a644e Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 1 Mar 2022 23:55:53 +0700 Subject: 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 -- cgit v1.3.1 From df4c614597157b540e0936a4555c6a1ba3ac487c Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Mar 2022 00:00:11 +0700 Subject: 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 -- cgit v1.3.1 From d3ed76902eaf52a61f19d88c941045ea1fc5b808 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Mar 2022 00:06:13 +0700 Subject: 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 -- cgit v1.3.1 From 918f3e9caec1e5566feac9214b7142b950a3d27c Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Mar 2022 12:22:20 +0700 Subject: 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 //------------------------------------------------------------------ -- cgit v1.3.1 From 635fb9dcdd08c736a4b0161381068b51ef4c140e Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Mar 2022 12:33:47 +0700 Subject: 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; -- cgit v1.3.1 From d4d7d35afbfe831b20ba83c2689c678361bfb709 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 2 Mar 2022 13:24:51 +0700 Subject: 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 -- cgit v1.3.1 From fa62d5abc9b1cede44a8e0630d8a28c90cdcdd00 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 3 Mar 2022 21:57:29 +0700 Subject: 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; } -- cgit v1.3.1 From c0195fc6270394a489faf29fe8fc42dff48bcff1 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 4 Mar 2022 14:48:29 +0700 Subject: 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 ?= -- cgit v1.3.1 From e2f0aef93b246368e1f6bdf101604e73d252801a Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 30 Mar 2022 16:41:55 +0700 Subject: 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); -- cgit v1.3.1 From e94d11a5b39cae08f39722eaa5f6ac4d527a329b Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 31 Mar 2022 12:56:53 +0700 Subject: 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 -- cgit v1.3.1 From 75bca96bc630b98723c9f4bbb4958dac22ac594d Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 1 Apr 2022 16:01:24 +0700 Subject: 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 | 116 ++++++++++++++++++++------------- 3 files changed, 77 insertions(+), 59 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 ) { + port->ints &= ~PIO_USB_INTS_DISCONNECT_BITS; hcd_event_device_remove(root_id+1, true); + } - port->ints &= ~PIO_USB_INTS_DISCONNECT_BITS; + 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); } } -- cgit v1.3.1 From e6e3dfedc815007d07ea455df04569604d87a856 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 1 Apr 2022 16:34:36 +0700 Subject: 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) -- cgit v1.3.1 From db6cba6a7bdf82f8c131fa2c521b86b10b4d053e Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 1 Apr 2022 16:38:41 +0700 Subject: rename pio to pio_usb --- hw/bsp/rp2040/family.cmake | 2 +- src/portable/raspberrypi/pio/hcd_pio.c | 274 ------------------------- src/portable/raspberrypi/pio_usb/hcd_pio_usb.c | 274 +++++++++++++++++++++++++ 3 files changed, 275 insertions(+), 275 deletions(-) delete mode 100644 src/portable/raspberrypi/pio/hcd_pio.c create mode 100644 src/portable/raspberrypi/pio_usb/hcd_pio_usb.c 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/hcd_pio.c deleted file mode 100644 index 037ebc132..000000000 --- a/src/portable/raspberrypi/pio/hcd_pio.c +++ /dev/null @@ -1,274 +0,0 @@ -/* - * 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_USB - -#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]; - -//--------------------------------------------------------------------+ -// 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_usb_port_reset_start(root, pp); -} - -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; - } - } -} - -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 * 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); -} - -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); -} - -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); -} - -//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; -} - -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 __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 ) - { - port->ints &= ~PIO_USB_INTS_CONNECT_BITS; - - hcd_event_device_attach(root_id+1, true); - } - - if ( port->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 ) - { - 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); - } -} - -#endif diff --git a/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c new file mode 100644 index 000000000..037ebc132 --- /dev/null +++ b/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c @@ -0,0 +1,274 @@ +/* + * 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_USB + +#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]; + +//--------------------------------------------------------------------+ +// 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_usb_port_reset_start(root, pp); +} + +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; + } + } +} + +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 * 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); +} + +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); +} + +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); +} + +//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; +} + +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 __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 ) + { + port->ints &= ~PIO_USB_INTS_CONNECT_BITS; + + hcd_event_device_attach(root_id+1, true); + } + + if ( port->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 ) + { + 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); + } +} + +#endif -- cgit v1.3.1 From c25f835aea838f935893fdef6352fab938e11d4b Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 1 Apr 2022 23:31:43 +0700 Subject: 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); } } -- cgit v1.3.1 From f881e77e66ea92506fefb69a24324f6c2274aab9 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 2 Apr 2022 19:19:00 +0700 Subject: 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 ) { -- cgit v1.3.1 From f6851cf18764a615f83bb782973bb3ea25e9089c Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 3 Apr 2022 00:17:58 +0700 Subject: clean up hcd pio --- lib/Pico-PIO-USB | 2 +- src/portable/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); } -- cgit v1.3.1 From 7ea75d3c09d1afd4db5374c89f715d8ff6c1d414 Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 3 Apr 2022 20:44:16 +0700 Subject: 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; } -- cgit v1.3.1 From d7d7e61f3437076a38ec031f2dc4cef38c8c831b Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 3 Apr 2022 22:48:08 +0700 Subject: 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) -- cgit v1.3.1 From d97c1546957059cfb3063b95d5287e2729f7f79a Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 5 Apr 2022 00:04:47 +0700 Subject: 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) -- cgit v1.3.1 From d5d4909c20f581ef2046932191f487c001d59ca3 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 8 Apr 2022 13:36:05 +0700 Subject: proof of concpet that device pio-usb work with host pio-usb --- examples/device/hid_boot_interface/src/main.c | 4 +++ .../device/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 -- cgit v1.3.1 From cc5a4720e5c1295eacf02c7b81fc4454f29cfaa4 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 14 Apr 2022 21:51:49 +0700 Subject: catch up with pio lib --- .../device/hid_boot_interface/src/tusb_config.h | 3 + hw/bsp/rp2040/family.cmake | 5 +- lib/Pico-PIO-USB | 2 +- src/portable/raspberrypi/pio_usb/dcd_pio_usb.c | 234 +++++++++++++++++++++ src/portable/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 -- cgit v1.3.1 From 05bfc1a9eba9fc901c35bdd1844cf962c5732310 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 15 Apr 2022 01:04:12 +0700 Subject: pio device works well --- examples/device/hid_boot_interface/src/main.c | 2 -- lib/Pico-PIO-USB | 2 +- src/portable/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); + + // must be called before queuing status + pio_usb_device_set_address(pio_rhport, dev_addr); - dcd_edpt_xfer(rhport, 0x80, NULL, 0); + 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); } -- cgit v1.3.1 From 00a0e3f21ba588f892022159e435938b48305b99 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 15 Apr 2022 20:14:47 +0700 Subject: 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 -- cgit v1.3.1 From b5a9537eea81bd6ffd8d575dbd2b5823c2fc1f0e Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 16 Apr 2022 01:19:51 +0700 Subject: 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; } //--------------------------------------------------------------------+ -- cgit v1.3.1 From a32cb1bb93ce986af9ed83f5d378c26bb4df2b1e Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 18 Apr 2022 17:49:21 +0700 Subject: clean up pio driver --- lib/Pico-PIO-USB | 2 +- src/device/dcd.h | 32 ++++++++++++++++++++--- src/device/usbd.c | 36 ++------------------------ src/device/usbd.h | 2 +- src/portable/raspberrypi/pio_usb/dcd_pio_usb.c | 7 ++--- src/portable/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) -- cgit v1.3.1 From 328039058ef1c654246304a3a0acbee724278b85 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 21 Apr 2022 01:45:03 +0700 Subject: 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 -- cgit v1.3.1 From 12debd7763bfc6f55566e5f4f8c982a55f206815 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 22 Apr 2022 01:06:17 +0700 Subject: 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); } } -- cgit v1.3.1 From 2f9b9a31be9d511d0bbb99671a2f5e7818cf8a55 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 22 Apr 2022 18:23:38 +0700 Subject: more with pio usb --- lib/Pico-PIO-USB | 2 +- src/portable/raspberrypi/pio_usb/dcd_pio_usb.c | 29 +++++++++++++------------- src/portable/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 ) -- cgit v1.3.1 From 26a25279bc7196d93ca074203ac35260298df472 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 26 Apr 2022 00:35:16 +0700 Subject: follow pio usb changes --- lib/Pico-PIO-USB | 2 +- src/portable/raspberrypi/pio_usb/dcd_pio_usb.c | 11 +++---- src/portable/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 ) -- cgit v1.3.1 From 4d11c658fffa810f271fdafb6453eff1f2b77b3f Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 26 Apr 2022 13:08:03 +0700 Subject: improve pio usb endpoint handler --- src/portable/raspberrypi/pio_usb/dcd_pio_usb.c | 38 ++++++++--------------- src/portable/raspberrypi/pio_usb/hcd_pio_usb.c | 42 ++++++-------------------- 2 files changed, 21 insertions(+), 59 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); + handle_endpoint_irq(rport, XFER_RESULT_SUCCESS, &rport->ep_complete); } - if ( ints & PIO_USB_INTS_ENDPOINT_ERROR_BITS ) + if ( ints & PIO_USB_INTS_ENDPOINT_STALLED_BITS ) { - handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_ERROR_BITS); + handle_endpoint_irq(rport, XFER_RESULT_STALLED, &rport->ep_stalled); } - if ( ints & PIO_USB_INTS_ENDPOINT_STALLED_BITS ) + if ( ints & PIO_USB_INTS_ENDPOINT_ERROR_BITS ) { - handle_endpoint_irq(port, PIO_USB_INTS_ENDPOINT_STALLED_BITS); + handle_endpoint_irq(rport, XFER_RESULT_FAILED, &rport->ep_error); } // clear all - port->ints &= ~ints; + rport->ints &= ~ints; } #endif -- cgit v1.3.1 From 455bddddcd051acd6d62bca97783be04fc508e96 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 26 Apr 2022 18:47:09 +0700 Subject: 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++) -- cgit v1.3.1 From fd827a80a9f3b1e482cc8b1609c055cbad252b37 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 27 Apr 2022 01:09:30 +0700 Subject: 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 -- cgit v1.3.1 From 4a661dead0899cb154325a8257d19281f2536af3 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 27 Apr 2022 17:57:52 +0700 Subject: 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) { -- cgit v1.3.1 From ae5490e5a5cd8650a45578b4ac70dbe89b002fb1 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 27 Apr 2022 20:52:56 +0700 Subject: 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: 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 //--------------------------------------------------------------------+ -- cgit v1.3.1 From 85dcb737749576d1c2946f3993b964c59974f38c Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 28 Apr 2022 18:00:30 +0700 Subject: 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; } -- cgit v1.3.1 From 608577e05c9f2e2e8831d8819090ed376d08284b Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 29 Apr 2022 18:05:50 +0700 Subject: 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 -- cgit v1.3.1 From 0b30a10ab3c2f1fdcf4fae8f530c80bf5cffaf96 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 29 Apr 2022 22:24:36 +0700 Subject: 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 | 201 +++++++++++++---------------- 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, 142 insertions(+), 135 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) -{ - 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]; +// 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 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 + #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) { - // 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; - } + 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); - - // continue to request to receive report - if ( !tuh_hid_receive_report(dev_addr, instance) ) - { - printf("Error: cannot request to receive report\r\n"); - } + 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' : '-'; -//--------------------------------------------------------------------+ -// 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(); - } - } + 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 cdc when line state changed e.g connected/disconnected -void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) +// 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) itf; - (void) rts; + uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance); - // TODO set some indicator - if ( dtr ) + switch(itf_protocol) { - // Terminal connected - }else - { - // Terminal disconnected + 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; } -} -// Invoked when CDC interface received data from host -void tud_cdc_rx_cb(uint8_t itf) -{ - (void) itf; + // continue to request to receive report + if ( !tuh_hid_receive_report(dev_addr, instance) ) + { + printf("Error: cannot request report\r\n"); + } } //--------------------------------------------------------------------+ -// 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); -- cgit v1.3.1 From c289438b75af9b256a954c6a9293a21825c53e04 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 29 Apr 2022 23:03:52 +0700 Subject: 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; -- cgit v1.3.1 From 79b5ab822ec1a17d6bfe88f6135613ba50acedbd Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 6 May 2022 17:22:52 +0700 Subject: 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 }; -- cgit v1.3.1 From 26ee62222290abf016ff4eb801d1289e05e595a1 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 May 2022 12:51:48 +0700 Subject: 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 -- cgit v1.3.1 From e3b57cea05b5e023bba7472bf8c583d4adc29e3e Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 May 2022 13:01:28 +0700 Subject: rename host/hid_to_cdc to dual/host_hid_to_device_cdc --- .../dual/host_hid_to_device_cdc/CMakeLists.txt | 29 ++ examples/dual/host_hid_to_device_cdc/Makefile | 19 ++ examples/dual/host_hid_to_device_cdc/only.txt | 3 + examples/dual/host_hid_to_device_cdc/src/main.c | 294 +++++++++++++++++++++ .../dual/host_hid_to_device_cdc/src/tusb_config.h | 157 +++++++++++ .../host_hid_to_device_cdc/src/usb_descriptors.c | 265 +++++++++++++++++++ examples/host/hid_to_cdc/CMakeLists.txt | 29 -- examples/host/hid_to_cdc/Makefile | 19 -- examples/host/hid_to_cdc/only.txt | 3 - examples/host/hid_to_cdc/src/main.c | 294 --------------------- examples/host/hid_to_cdc/src/tusb_config.h | 157 ----------- examples/host/hid_to_cdc/src/usb_descriptors.c | 265 ------------------- 12 files changed, 767 insertions(+), 767 deletions(-) create mode 100644 examples/dual/host_hid_to_device_cdc/CMakeLists.txt create mode 100644 examples/dual/host_hid_to_device_cdc/Makefile create mode 100644 examples/dual/host_hid_to_device_cdc/only.txt create mode 100644 examples/dual/host_hid_to_device_cdc/src/main.c create mode 100644 examples/dual/host_hid_to_device_cdc/src/tusb_config.h create mode 100644 examples/dual/host_hid_to_device_cdc/src/usb_descriptors.c delete mode 100644 examples/host/hid_to_cdc/CMakeLists.txt delete mode 100644 examples/host/hid_to_cdc/Makefile delete mode 100644 examples/host/hid_to_cdc/only.txt delete mode 100644 examples/host/hid_to_cdc/src/main.c delete mode 100644 examples/host/hid_to_cdc/src/tusb_config.h delete mode 100644 examples/host/hid_to_cdc/src/usb_descriptors.c diff --git a/examples/dual/host_hid_to_device_cdc/CMakeLists.txt b/examples/dual/host_hid_to_device_cdc/CMakeLists.txt new file mode 100644 index 000000000..ff9f58c4c --- /dev/null +++ b/examples/dual/host_hid_to_device_cdc/CMakeLists.txt @@ -0,0 +1,29 @@ +cmake_minimum_required(VERSION 3.5) + +include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) + +# gets PROJECT name for the example (e.g. -) +family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR}) + +project(${PROJECT}) + +# Checks this example is valid for the family and initializes the project +family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR}) + +add_executable(${PROJECT}) + +# Example source +target_sources(${PROJECT} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c + ${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c + ) + +# Example include +target_include_directories(${PROJECT} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src + ) + +# 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}) +family_configure_host_example(${PROJECT}) diff --git a/examples/dual/host_hid_to_device_cdc/Makefile b/examples/dual/host_hid_to_device_cdc/Makefile new file mode 100644 index 000000000..3fe9b0888 --- /dev/null +++ b/examples/dual/host_hid_to_device_cdc/Makefile @@ -0,0 +1,19 @@ +include ../../../tools/top.mk +include ../../make.mk + +INC += \ + src \ + $(TOP)/hw \ + +# Example source +EXAMPLE_SOURCE += $(wildcard src/*.c) +SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) + +CFLAGS += -Wno-error=cast-align -Wno-error=null-dereference + +SRC_C += \ + src/class/hid/hid_host.c \ + src/host/hub.c \ + src/host/usbh.c + +include ../../rules.mk diff --git a/examples/dual/host_hid_to_device_cdc/only.txt b/examples/dual/host_hid_to_device_cdc/only.txt new file mode 100644 index 000000000..6ee8e3fde --- /dev/null +++ b/examples/dual/host_hid_to_device_cdc/only.txt @@ -0,0 +1,3 @@ +board:mimxrt1060_evk +board:mimxrt1064_evk +mcu:RP2040 diff --git a/examples/dual/host_hid_to_device_cdc/src/main.c b/examples/dual/host_hid_to_device_cdc/src/main.c new file mode 100644 index 000000000..cc811cd71 --- /dev/null +++ b/examples/dual/host_hid_to_device_cdc/src/main.c @@ -0,0 +1,294 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 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 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. + +#include +#include +#include + +#include "bsp/board.h" +#include "tusb.h" + +//--------------------------------------------------------------------+ +// MACRO CONSTANT TYPEDEF PROTYPES +//--------------------------------------------------------------------+ + +// uncomment if you are using colemak layout +// #define KEYBOARD_COLEMAK + +#ifdef KEYBOARD_COLEMAK +const uint8_t colemak[128] = { + 0 , 0, 0, 0, 0, 0, 0, 22, + 9 , 23, 7, 0, 24, 17, 8, 12, + 0 , 14, 28, 51, 0, 19, 21, 10, + 15 , 0, 0, 0, 13, 0, 0, 0, + 0 , 0, 0, 0, 0, 0, 0, 0, + 0 , 0, 0, 0, 0, 0, 0, 0, + 0 , 0, 0, 18, 0, 0, 0, 0, + 0 , 0, 0, 0, 0, 0, 0, 0, + 0 , 0, 0, 0, 0, 0, 0, 0, + 0 , 0, 0, 0, 0, 0, 0, 0 +}; +#endif + +static uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII }; + +/* Blink pattern + * - 250 ms : device not mounted + * - 1000 ms : device mounted + * - 2500 ms : device is suspended + */ +enum { + BLINK_NOT_MOUNTED = 250, + BLINK_MOUNTED = 1000, + BLINK_SUSPENDED = 2500, +}; + +static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; + +void led_blinking_task(void); + +/*------------- MAIN -------------*/ +int main(void) +{ + board_init(); + + printf("TinyUSB Host HID <-> Device CDC Example\r\n"); + + tusb_init(); + + while (1) + { + tud_task(); // tinyusb device task + tuh_task(); // tinyusb host task + led_blinking_task(); + } + + return 0; +} + +//--------------------------------------------------------------------+ +// Device CDC +//--------------------------------------------------------------------+ + +// Invoked when device is mounted +void tud_mount_cb(void) +{ + blink_interval_ms = BLINK_MOUNTED; +} + +// Invoked when device is unmounted +void tud_umount_cb(void) +{ + blink_interval_ms = BLINK_NOT_MOUNTED; +} + +// Invoked when usb bus is suspended +// remote_wakeup_en : if host allow us to perform remote wakeup +// Within 7ms, device must draw an average of current less than 2.5 mA from bus +void tud_suspend_cb(bool remote_wakeup_en) +{ + (void) remote_wakeup_en; + blink_interval_ms = BLINK_SUSPENDED; +} + +// Invoked when usb bus is resumed +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 HID +//--------------------------------------------------------------------+ + +// Invoked when device with hid interface is mounted +// Report descriptor is also available for use. tuh_hid_parse_report_descriptor() +// can be used to parse common/simple enough descriptor. +// Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped +// therefore report_desc = NULL, desc_len = 0 +void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) +{ + (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("[%04x:%04x][%u] HID Interface instance = %d, Protocol = %s\r\n", vid, pid, dev_addr, instance, protocol_str[itf_protocol]); + + // Receive report from boot keyboard & mouse only + // tuh_hid_report_received_cb() will be invoked when report is available + if (itf_protocol == HID_ITF_PROTOCOL_KEYBOARD || itf_protocol == HID_ITF_PROTOCOL_MOUSE) + { + 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("[%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) +{ + for(uint8_t i=0; i<6; i++) + { + if (report->keycode[i] == keycode) return true; + } + + return false; +} + + +// 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 hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released + bool flush = false; + + for(uint8_t i=0; i<6; i++) + { + uint8_t keycode = report->keycode[i]; + if ( keycode ) + { + if ( find_key_in_report(&prev_report, keycode) ) + { + // exist in previous report means the current key is holding + }else + { + // not existed in previous report means the current key is pressed + + // 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(); + + prev_report = *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); + + 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 report\r\n"); + } +} + +//--------------------------------------------------------------------+ +// Blinking Task +//--------------------------------------------------------------------+ +void led_blinking_task(void) +{ + static uint32_t start_ms = 0; + static bool led_state = false; + + // Blink every interval ms + if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time + start_ms += blink_interval_ms; + + board_led_write(led_state); + led_state = 1 - led_state; // toggle +} diff --git a/examples/dual/host_hid_to_device_cdc/src/tusb_config.h b/examples/dual/host_hid_to_device_cdc/src/tusb_config.h new file mode 100644 index 000000000..0fa4899a9 --- /dev/null +++ b/examples/dual/host_hid_to_device_cdc/src/tusb_config.h @@ -0,0 +1,157 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 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. + * + */ + +#ifndef _TUSB_CONFIG_H_ +#define _TUSB_CONFIG_H_ + +#ifdef __cplusplus + extern "C" { +#endif + +//-------------------------------------------------------------------- +// COMMON CONFIGURATION +//-------------------------------------------------------------------- + +// defined by board.mk +#ifndef CFG_TUSB_MCU + #error CFG_TUSB_MCU must be defined +#endif + +// 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 +#endif + +// RHPort number used for device can be defined by board.mk, default to port 1 +#ifndef BOARD_HOST_RHPORT_NUM + #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 + #if TU_CHECK_MCU(OPT_MCU_LPC18XX, OPT_MCU_LPC43XX, OPT_MCU_MIMXRT10XX, OPT_MCU_NUC505) ||\ + TU_CHECK_MCU(OPT_MCU_CXD56, OPT_MCU_SAMX7X, OPT_MCU_BCM2711) ||\ + TU_CHECK_MCU(OPT_MCU_FT90X, OPT_MCU_FT93X) + #define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_HIGH_SPEED + #else + #define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED + #endif +#endif + +// 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_HOST_RHPORT_SPEED + #if TU_CHECK_MCU(OPT_MCU_LPC18XX, OPT_MCU_LPC43XX, OPT_MCU_MIMXRT10XX, OPT_MCU_NUC505) ||\ + TU_CHECK_MCU(OPT_MCU_CXD56, OPT_MCU_SAMX7X, OPT_MCU_BCM2711) ||\ + TU_CHECK_MCU(OPT_MCU_FT90X, OPT_MCU_FT93X) + #define BOARD_HOST_RHPORT_SPEED OPT_MODE_HIGH_SPEED + #else + #define BOARD_HOST_RHPORT_SPEED OPT_MODE_FULL_SPEED + #endif +#endif + +// Device mode with rhport and speed defined by board.mk +#if BOARD_DEVICE_RHPORT_NUM == 0 + #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED) +#elif BOARD_DEVICE_RHPORT_NUM == 1 + #define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED) +#else + #error "Incorrect RHPort configuration" +#endif + +// Device mode with rhport and speed defined by board.mk +#if BOARD_HOST_RHPORT_NUM == 0 + #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | BOARD_HOST_RHPORT_SPEED) +#elif BOARD_HOST_RHPORT_NUM == 1 + #define CFG_TUSB_RHPORT1_MODE (OPT_MODE_HOST | BOARD_HOST_RHPORT_SPEED) +#else + #error "Incorrect RHPort configuration" +#endif + +// This example doesn't use an RTOS +#ifndef CFG_TUSB_OS +#define CFG_TUSB_OS OPT_OS_NONE +#endif + +// CFG_TUSB_DEBUG is defined by compiler in DEBUG build +// #define CFG_TUSB_DEBUG 0 + +/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. + * Tinyusb use follows macros to declare transferring memory so that they can be put + * into those specific section. + * e.g + * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) + * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) + */ +#ifndef CFG_TUSB_MEM_SECTION +#define CFG_TUSB_MEM_SECTION +#endif + +#ifndef CFG_TUSB_MEM_ALIGN +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) +#endif + +//-------------------------------------------------------------------- +// DEVICE CONFIGURATION +//-------------------------------------------------------------------- + +#ifndef CFG_TUD_ENDPOINT0_SIZE +#define CFG_TUD_ENDPOINT0_SIZE 64 +#endif + +//------------- CLASS -------------// +#define CFG_TUD_CDC 1 + +// CDC FIFO size of TX and RX +#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) +#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) + +// CDC Endpoint transfer buffer size, more is faster +#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) + +//-------------------------------------------------------------------- +// HOST CONFIGURATION +//-------------------------------------------------------------------- + +// Size of buffer to hold descriptors and other data used for enumeration +#define CFG_TUH_ENUMERATION_BUFSIZE 256 + +#define CFG_TUH_HUB 1 +// max device support (excluding hub device) +#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports + +#define CFG_TUH_HID 4 +#define CFG_TUH_HID_EPIN_BUFSIZE 64 +#define CFG_TUH_HID_EPOUT_BUFSIZE 64 + +#ifdef __cplusplus + } +#endif + +#endif /* _TUSB_CONFIG_H_ */ diff --git a/examples/dual/host_hid_to_device_cdc/src/usb_descriptors.c b/examples/dual/host_hid_to_device_cdc/src/usb_descriptors.c new file mode 100644 index 000000000..e55899f65 --- /dev/null +++ b/examples/dual/host_hid_to_device_cdc/src/usb_descriptors.c @@ -0,0 +1,265 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2019 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. + * + */ + +#include "tusb.h" + +/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug. + * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC. + * + * Auto ProductID layout's Bitmap: + * [MSB] HID | MSC | CDC [LSB] + */ +#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) +#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ + _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) + +#define USB_VID 0xCafe +#define USB_BCD 0x0200 + +//--------------------------------------------------------------------+ +// Device Descriptors +//--------------------------------------------------------------------+ +tusb_desc_device_t const desc_device = +{ + .bLength = sizeof(tusb_desc_device_t), + .bDescriptorType = TUSB_DESC_DEVICE, + .bcdUSB = USB_BCD, + + // Use Interface Association Descriptor (IAD) for CDC + // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1) + .bDeviceClass = TUSB_CLASS_MISC, + .bDeviceSubClass = MISC_SUBCLASS_COMMON, + .bDeviceProtocol = MISC_PROTOCOL_IAD, + + .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, + + .idVendor = USB_VID, + .idProduct = USB_PID, + .bcdDevice = 0x0100, + + .iManufacturer = 0x01, + .iProduct = 0x02, + .iSerialNumber = 0x03, + + .bNumConfigurations = 0x01 +}; + +// Invoked when received GET DEVICE DESCRIPTOR +// Application return pointer to descriptor +uint8_t const * tud_descriptor_device_cb(void) +{ + return (uint8_t const *) &desc_device; +} + +//--------------------------------------------------------------------+ +// Configuration Descriptor +//--------------------------------------------------------------------+ + +enum +{ + ITF_NUM_CDC = 0, + ITF_NUM_CDC_DATA, + ITF_NUM_TOTAL +}; + +#if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX + // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number + // 0 control, 1 In, 2 Bulk, 3 Iso, 4 In, 5 Bulk etc ... + #define EPNUM_CDC_NOTIF 0x81 + #define EPNUM_CDC_OUT 0x02 + #define EPNUM_CDC_IN 0x82 + +#elif CFG_TUSB_MCU == OPT_MCU_SAMG || CFG_TUSB_MCU == OPT_MCU_SAMX7X + // SAMG & SAME70 don't support a same endpoint number with different direction IN and OUT + // e.g EP1 OUT & EP1 IN cannot exist together + #define EPNUM_CDC_NOTIF 0x81 + #define EPNUM_CDC_OUT 0x02 + #define EPNUM_CDC_IN 0x83 + +#elif CFG_TUSB_MCU == OPT_MCU_CXD56 + // CXD56 doesn't support a same endpoint number with different direction IN and OUT + // e.g EP1 OUT & EP1 IN cannot exist together + // CXD56 USB driver has fixed endpoint type (bulk/interrupt/iso) and direction (IN/OUT) by its number + // 0 control (IN/OUT), 1 Bulk (IN), 2 Bulk (OUT), 3 In (IN), 4 Bulk (IN), 5 Bulk (OUT), 6 In (IN) + #define EPNUM_CDC_NOTIF 0x83 + #define EPNUM_CDC_OUT 0x02 + #define EPNUM_CDC_IN 0x81 + +#elif CFG_TUSB_MCU == OPT_MCU_FT90X || CFG_TUSB_MCU == OPT_MCU_FT93X + // FT9XX doesn't support a same endpoint number with different direction IN and OUT + // e.g EP1 OUT & EP1 IN cannot exist together + #define EPNUM_CDC_NOTIF 0x81 + #define EPNUM_CDC_OUT 0x02 + #define EPNUM_CDC_IN 0x83 + +#else + #define EPNUM_CDC_NOTIF 0x81 + #define EPNUM_CDC_OUT 0x02 + #define EPNUM_CDC_IN 0x82 + +#endif + +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN) + +// full speed configuration +uint8_t const desc_fs_configuration[] = +{ + // Config number, interface count, string index, total length, attribute, power in mA + TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100), + + // Interface number, string index, EP notification address and size, EP data address (out, in) and size. + TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64), +}; + +#if TUD_OPT_HIGH_SPEED +// Per USB specs: high speed capable device must report device_qualifier and other_speed_configuration + +// high speed configuration +uint8_t const desc_hs_configuration[] = +{ + // Config number, interface count, string index, total length, attribute, power in mA + TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100), + + // Interface number, string index, EP notification address and size, EP data address (out, in) and size. + TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512), +}; + +// other speed configuration +uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN]; + +// device qualifier is mostly similar to device descriptor since we don't change configuration based on speed +tusb_desc_device_qualifier_t const desc_device_qualifier = +{ + .bLength = sizeof(tusb_desc_device_qualifier_t), + .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER, + .bcdUSB = USB_BCD, + + .bDeviceClass = TUSB_CLASS_MISC, + .bDeviceSubClass = MISC_SUBCLASS_COMMON, + .bDeviceProtocol = MISC_PROTOCOL_IAD, + + .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, + .bNumConfigurations = 0x01, + .bReserved = 0x00 +}; + +// Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete. +// device_qualifier descriptor describes information about a high-speed capable device that would +// change if the device were operating at the other speed. If not highspeed capable stall this request. +uint8_t const* tud_descriptor_device_qualifier_cb(void) +{ + return (uint8_t const*) &desc_device_qualifier; +} + +// Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete +// Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa +uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index) +{ + (void) index; // for multiple configurations + + // if link speed is high return fullspeed config, and vice versa + // Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG + memcpy(desc_other_speed_config, + (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_fs_configuration : desc_hs_configuration, + CONFIG_TOTAL_LEN); + + desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG; + + return desc_other_speed_config; +} + +#endif // highspeed + + +// Invoked when received GET CONFIGURATION DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_descriptor_configuration_cb(uint8_t index) +{ + (void) index; // for multiple configurations + +#if TUD_OPT_HIGH_SPEED + // Although we are highspeed, host may be fullspeed. + return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration : desc_fs_configuration; +#else + return desc_fs_configuration; +#endif +} + +//--------------------------------------------------------------------+ +// String Descriptors +//--------------------------------------------------------------------+ + +// array of pointer to string descriptors +char const* string_desc_arr [] = +{ + (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) + "TinyUSB", // 1: Manufacturer + "TinyUSB Device", // 2: Product + "123456789012", // 3: Serials, should use chip ID + "TinyUSB CDC", // 4: CDC Interface +}; + +static uint16_t _desc_str[32]; + +// Invoked when received GET STRING DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete +uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) +{ + (void) langid; + + uint8_t chr_count; + + if ( index == 0) + { + memcpy(&_desc_str[1], string_desc_arr[0], 2); + chr_count = 1; + }else + { + // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors. + // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors + + if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; + + const char* str = string_desc_arr[index]; + + // Cap at max char + chr_count = (uint8_t) strlen(str); + if ( chr_count > 31 ) chr_count = 31; + + // Convert ASCII string into UTF-16 + for(uint8_t i=0; i-) -family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR}) - -project(${PROJECT}) - -# Checks this example is valid for the family and initializes the project -family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR}) - -add_executable(${PROJECT}) - -# Example source -target_sources(${PROJECT} PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c - ${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c - ) - -# Example include -target_include_directories(${PROJECT} PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/src - ) - -# 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}) -family_configure_host_example(${PROJECT}) diff --git a/examples/host/hid_to_cdc/Makefile b/examples/host/hid_to_cdc/Makefile deleted file mode 100644 index 3fe9b0888..000000000 --- a/examples/host/hid_to_cdc/Makefile +++ /dev/null @@ -1,19 +0,0 @@ -include ../../../tools/top.mk -include ../../make.mk - -INC += \ - src \ - $(TOP)/hw \ - -# Example source -EXAMPLE_SOURCE += $(wildcard src/*.c) -SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) - -CFLAGS += -Wno-error=cast-align -Wno-error=null-dereference - -SRC_C += \ - src/class/hid/hid_host.c \ - src/host/hub.c \ - src/host/usbh.c - -include ../../rules.mk diff --git a/examples/host/hid_to_cdc/only.txt b/examples/host/hid_to_cdc/only.txt deleted file mode 100644 index 6ee8e3fde..000000000 --- a/examples/host/hid_to_cdc/only.txt +++ /dev/null @@ -1,3 +0,0 @@ -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 deleted file mode 100644 index cc811cd71..000000000 --- a/examples/host/hid_to_cdc/src/main.c +++ /dev/null @@ -1,294 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 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 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. - -#include -#include -#include - -#include "bsp/board.h" -#include "tusb.h" - -//--------------------------------------------------------------------+ -// MACRO CONSTANT TYPEDEF PROTYPES -//--------------------------------------------------------------------+ - -// uncomment if you are using colemak layout -// #define KEYBOARD_COLEMAK - -#ifdef KEYBOARD_COLEMAK -const uint8_t colemak[128] = { - 0 , 0, 0, 0, 0, 0, 0, 22, - 9 , 23, 7, 0, 24, 17, 8, 12, - 0 , 14, 28, 51, 0, 19, 21, 10, - 15 , 0, 0, 0, 13, 0, 0, 0, - 0 , 0, 0, 0, 0, 0, 0, 0, - 0 , 0, 0, 0, 0, 0, 0, 0, - 0 , 0, 0, 18, 0, 0, 0, 0, - 0 , 0, 0, 0, 0, 0, 0, 0, - 0 , 0, 0, 0, 0, 0, 0, 0, - 0 , 0, 0, 0, 0, 0, 0, 0 -}; -#endif - -static uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII }; - -/* Blink pattern - * - 250 ms : device not mounted - * - 1000 ms : device mounted - * - 2500 ms : device is suspended - */ -enum { - BLINK_NOT_MOUNTED = 250, - BLINK_MOUNTED = 1000, - BLINK_SUSPENDED = 2500, -}; - -static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; - -void led_blinking_task(void); - -/*------------- MAIN -------------*/ -int main(void) -{ - board_init(); - - printf("TinyUSB Host HID <-> Device CDC Example\r\n"); - - tusb_init(); - - while (1) - { - tud_task(); // tinyusb device task - tuh_task(); // tinyusb host task - led_blinking_task(); - } - - return 0; -} - -//--------------------------------------------------------------------+ -// Device CDC -//--------------------------------------------------------------------+ - -// Invoked when device is mounted -void tud_mount_cb(void) -{ - blink_interval_ms = BLINK_MOUNTED; -} - -// Invoked when device is unmounted -void tud_umount_cb(void) -{ - blink_interval_ms = BLINK_NOT_MOUNTED; -} - -// Invoked when usb bus is suspended -// remote_wakeup_en : if host allow us to perform remote wakeup -// Within 7ms, device must draw an average of current less than 2.5 mA from bus -void tud_suspend_cb(bool remote_wakeup_en) -{ - (void) remote_wakeup_en; - blink_interval_ms = BLINK_SUSPENDED; -} - -// Invoked when usb bus is resumed -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 HID -//--------------------------------------------------------------------+ - -// Invoked when device with hid interface is mounted -// Report descriptor is also available for use. tuh_hid_parse_report_descriptor() -// can be used to parse common/simple enough descriptor. -// Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped -// therefore report_desc = NULL, desc_len = 0 -void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len) -{ - (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("[%04x:%04x][%u] HID Interface instance = %d, Protocol = %s\r\n", vid, pid, dev_addr, instance, protocol_str[itf_protocol]); - - // Receive report from boot keyboard & mouse only - // tuh_hid_report_received_cb() will be invoked when report is available - if (itf_protocol == HID_ITF_PROTOCOL_KEYBOARD || itf_protocol == HID_ITF_PROTOCOL_MOUSE) - { - 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("[%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) -{ - for(uint8_t i=0; i<6; i++) - { - if (report->keycode[i] == keycode) return true; - } - - return false; -} - - -// 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 hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released - bool flush = false; - - for(uint8_t i=0; i<6; i++) - { - uint8_t keycode = report->keycode[i]; - if ( keycode ) - { - if ( find_key_in_report(&prev_report, keycode) ) - { - // exist in previous report means the current key is holding - }else - { - // not existed in previous report means the current key is pressed - - // 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(); - - prev_report = *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); - - 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 report\r\n"); - } -} - -//--------------------------------------------------------------------+ -// Blinking Task -//--------------------------------------------------------------------+ -void led_blinking_task(void) -{ - static uint32_t start_ms = 0; - static bool led_state = false; - - // Blink every interval ms - if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time - start_ms += blink_interval_ms; - - board_led_write(led_state); - led_state = 1 - led_state; // toggle -} diff --git a/examples/host/hid_to_cdc/src/tusb_config.h b/examples/host/hid_to_cdc/src/tusb_config.h deleted file mode 100644 index 0fa4899a9..000000000 --- a/examples/host/hid_to_cdc/src/tusb_config.h +++ /dev/null @@ -1,157 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 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. - * - */ - -#ifndef _TUSB_CONFIG_H_ -#define _TUSB_CONFIG_H_ - -#ifdef __cplusplus - extern "C" { -#endif - -//-------------------------------------------------------------------- -// COMMON CONFIGURATION -//-------------------------------------------------------------------- - -// defined by board.mk -#ifndef CFG_TUSB_MCU - #error CFG_TUSB_MCU must be defined -#endif - -// 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 -#endif - -// RHPort number used for device can be defined by board.mk, default to port 1 -#ifndef BOARD_HOST_RHPORT_NUM - #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 - #if TU_CHECK_MCU(OPT_MCU_LPC18XX, OPT_MCU_LPC43XX, OPT_MCU_MIMXRT10XX, OPT_MCU_NUC505) ||\ - TU_CHECK_MCU(OPT_MCU_CXD56, OPT_MCU_SAMX7X, OPT_MCU_BCM2711) ||\ - TU_CHECK_MCU(OPT_MCU_FT90X, OPT_MCU_FT93X) - #define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_HIGH_SPEED - #else - #define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED - #endif -#endif - -// 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_HOST_RHPORT_SPEED - #if TU_CHECK_MCU(OPT_MCU_LPC18XX, OPT_MCU_LPC43XX, OPT_MCU_MIMXRT10XX, OPT_MCU_NUC505) ||\ - TU_CHECK_MCU(OPT_MCU_CXD56, OPT_MCU_SAMX7X, OPT_MCU_BCM2711) ||\ - TU_CHECK_MCU(OPT_MCU_FT90X, OPT_MCU_FT93X) - #define BOARD_HOST_RHPORT_SPEED OPT_MODE_HIGH_SPEED - #else - #define BOARD_HOST_RHPORT_SPEED OPT_MODE_FULL_SPEED - #endif -#endif - -// Device mode with rhport and speed defined by board.mk -#if BOARD_DEVICE_RHPORT_NUM == 0 - #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED) -#elif BOARD_DEVICE_RHPORT_NUM == 1 - #define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED) -#else - #error "Incorrect RHPort configuration" -#endif - -// Device mode with rhport and speed defined by board.mk -#if BOARD_HOST_RHPORT_NUM == 0 - #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | BOARD_HOST_RHPORT_SPEED) -#elif BOARD_HOST_RHPORT_NUM == 1 - #define CFG_TUSB_RHPORT1_MODE (OPT_MODE_HOST | BOARD_HOST_RHPORT_SPEED) -#else - #error "Incorrect RHPort configuration" -#endif - -// This example doesn't use an RTOS -#ifndef CFG_TUSB_OS -#define CFG_TUSB_OS OPT_OS_NONE -#endif - -// CFG_TUSB_DEBUG is defined by compiler in DEBUG build -// #define CFG_TUSB_DEBUG 0 - -/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. - * Tinyusb use follows macros to declare transferring memory so that they can be put - * into those specific section. - * e.g - * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) - * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) - */ -#ifndef CFG_TUSB_MEM_SECTION -#define CFG_TUSB_MEM_SECTION -#endif - -#ifndef CFG_TUSB_MEM_ALIGN -#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) -#endif - -//-------------------------------------------------------------------- -// DEVICE CONFIGURATION -//-------------------------------------------------------------------- - -#ifndef CFG_TUD_ENDPOINT0_SIZE -#define CFG_TUD_ENDPOINT0_SIZE 64 -#endif - -//------------- CLASS -------------// -#define CFG_TUD_CDC 1 - -// CDC FIFO size of TX and RX -#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) -#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) - -// CDC Endpoint transfer buffer size, more is faster -#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) - -//-------------------------------------------------------------------- -// HOST CONFIGURATION -//-------------------------------------------------------------------- - -// Size of buffer to hold descriptors and other data used for enumeration -#define CFG_TUH_ENUMERATION_BUFSIZE 256 - -#define CFG_TUH_HUB 1 -// max device support (excluding hub device) -#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports - -#define CFG_TUH_HID 4 -#define CFG_TUH_HID_EPIN_BUFSIZE 64 -#define CFG_TUH_HID_EPOUT_BUFSIZE 64 - -#ifdef __cplusplus - } -#endif - -#endif /* _TUSB_CONFIG_H_ */ diff --git a/examples/host/hid_to_cdc/src/usb_descriptors.c b/examples/host/hid_to_cdc/src/usb_descriptors.c deleted file mode 100644 index e55899f65..000000000 --- a/examples/host/hid_to_cdc/src/usb_descriptors.c +++ /dev/null @@ -1,265 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 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. - * - */ - -#include "tusb.h" - -/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug. - * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC. - * - * Auto ProductID layout's Bitmap: - * [MSB] HID | MSC | CDC [LSB] - */ -#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) -#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ - _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) - -#define USB_VID 0xCafe -#define USB_BCD 0x0200 - -//--------------------------------------------------------------------+ -// Device Descriptors -//--------------------------------------------------------------------+ -tusb_desc_device_t const desc_device = -{ - .bLength = sizeof(tusb_desc_device_t), - .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = USB_BCD, - - // Use Interface Association Descriptor (IAD) for CDC - // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1) - .bDeviceClass = TUSB_CLASS_MISC, - .bDeviceSubClass = MISC_SUBCLASS_COMMON, - .bDeviceProtocol = MISC_PROTOCOL_IAD, - - .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, - - .idVendor = USB_VID, - .idProduct = USB_PID, - .bcdDevice = 0x0100, - - .iManufacturer = 0x01, - .iProduct = 0x02, - .iSerialNumber = 0x03, - - .bNumConfigurations = 0x01 -}; - -// Invoked when received GET DEVICE DESCRIPTOR -// Application return pointer to descriptor -uint8_t const * tud_descriptor_device_cb(void) -{ - return (uint8_t const *) &desc_device; -} - -//--------------------------------------------------------------------+ -// Configuration Descriptor -//--------------------------------------------------------------------+ - -enum -{ - ITF_NUM_CDC = 0, - ITF_NUM_CDC_DATA, - ITF_NUM_TOTAL -}; - -#if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX - // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number - // 0 control, 1 In, 2 Bulk, 3 Iso, 4 In, 5 Bulk etc ... - #define EPNUM_CDC_NOTIF 0x81 - #define EPNUM_CDC_OUT 0x02 - #define EPNUM_CDC_IN 0x82 - -#elif CFG_TUSB_MCU == OPT_MCU_SAMG || CFG_TUSB_MCU == OPT_MCU_SAMX7X - // SAMG & SAME70 don't support a same endpoint number with different direction IN and OUT - // e.g EP1 OUT & EP1 IN cannot exist together - #define EPNUM_CDC_NOTIF 0x81 - #define EPNUM_CDC_OUT 0x02 - #define EPNUM_CDC_IN 0x83 - -#elif CFG_TUSB_MCU == OPT_MCU_CXD56 - // CXD56 doesn't support a same endpoint number with different direction IN and OUT - // e.g EP1 OUT & EP1 IN cannot exist together - // CXD56 USB driver has fixed endpoint type (bulk/interrupt/iso) and direction (IN/OUT) by its number - // 0 control (IN/OUT), 1 Bulk (IN), 2 Bulk (OUT), 3 In (IN), 4 Bulk (IN), 5 Bulk (OUT), 6 In (IN) - #define EPNUM_CDC_NOTIF 0x83 - #define EPNUM_CDC_OUT 0x02 - #define EPNUM_CDC_IN 0x81 - -#elif CFG_TUSB_MCU == OPT_MCU_FT90X || CFG_TUSB_MCU == OPT_MCU_FT93X - // FT9XX doesn't support a same endpoint number with different direction IN and OUT - // e.g EP1 OUT & EP1 IN cannot exist together - #define EPNUM_CDC_NOTIF 0x81 - #define EPNUM_CDC_OUT 0x02 - #define EPNUM_CDC_IN 0x83 - -#else - #define EPNUM_CDC_NOTIF 0x81 - #define EPNUM_CDC_OUT 0x02 - #define EPNUM_CDC_IN 0x82 - -#endif - -#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN) - -// full speed configuration -uint8_t const desc_fs_configuration[] = -{ - // Config number, interface count, string index, total length, attribute, power in mA - TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100), - - // Interface number, string index, EP notification address and size, EP data address (out, in) and size. - TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64), -}; - -#if TUD_OPT_HIGH_SPEED -// Per USB specs: high speed capable device must report device_qualifier and other_speed_configuration - -// high speed configuration -uint8_t const desc_hs_configuration[] = -{ - // Config number, interface count, string index, total length, attribute, power in mA - TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100), - - // Interface number, string index, EP notification address and size, EP data address (out, in) and size. - TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512), -}; - -// other speed configuration -uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN]; - -// device qualifier is mostly similar to device descriptor since we don't change configuration based on speed -tusb_desc_device_qualifier_t const desc_device_qualifier = -{ - .bLength = sizeof(tusb_desc_device_qualifier_t), - .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER, - .bcdUSB = USB_BCD, - - .bDeviceClass = TUSB_CLASS_MISC, - .bDeviceSubClass = MISC_SUBCLASS_COMMON, - .bDeviceProtocol = MISC_PROTOCOL_IAD, - - .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, - .bNumConfigurations = 0x01, - .bReserved = 0x00 -}; - -// Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request -// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete. -// device_qualifier descriptor describes information about a high-speed capable device that would -// change if the device were operating at the other speed. If not highspeed capable stall this request. -uint8_t const* tud_descriptor_device_qualifier_cb(void) -{ - return (uint8_t const*) &desc_device_qualifier; -} - -// Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request -// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete -// Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa -uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index) -{ - (void) index; // for multiple configurations - - // if link speed is high return fullspeed config, and vice versa - // Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG - memcpy(desc_other_speed_config, - (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_fs_configuration : desc_hs_configuration, - CONFIG_TOTAL_LEN); - - desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG; - - return desc_other_speed_config; -} - -#endif // highspeed - - -// Invoked when received GET CONFIGURATION DESCRIPTOR -// Application return pointer to descriptor -// Descriptor contents must exist long enough for transfer to complete -uint8_t const * tud_descriptor_configuration_cb(uint8_t index) -{ - (void) index; // for multiple configurations - -#if TUD_OPT_HIGH_SPEED - // Although we are highspeed, host may be fullspeed. - return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration : desc_fs_configuration; -#else - return desc_fs_configuration; -#endif -} - -//--------------------------------------------------------------------+ -// String Descriptors -//--------------------------------------------------------------------+ - -// array of pointer to string descriptors -char const* string_desc_arr [] = -{ - (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) - "TinyUSB", // 1: Manufacturer - "TinyUSB Device", // 2: Product - "123456789012", // 3: Serials, should use chip ID - "TinyUSB CDC", // 4: CDC Interface -}; - -static uint16_t _desc_str[32]; - -// Invoked when received GET STRING DESCRIPTOR request -// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete -uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) -{ - (void) langid; - - uint8_t chr_count; - - if ( index == 0) - { - memcpy(&_desc_str[1], string_desc_arr[0], 2); - chr_count = 1; - }else - { - // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors. - // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors - - if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; - - const char* str = string_desc_arr[index]; - - // Cap at max char - chr_count = (uint8_t) strlen(str); - if ( chr_count > 31 ) chr_count = 31; - - // Convert ASCII string into UTF-16 - for(uint8_t i=0; i Date: Tue, 10 May 2022 13:15:07 +0700 Subject: 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() -- cgit v1.3.1 From 717a47412548923123141503749f721184222cf0 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 May 2022 21:56:47 +0700 Subject: 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 -- cgit v1.3.1 From 35b77a4a81b62da0558630a7148fb3934faa4391 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 May 2022 22:14:00 +0700 Subject: cmake lib rename --- examples/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() -- cgit v1.3.1 From 8473ca16fb74edcb32a050ef087d5f30f50c599b Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 May 2022 22:56:06 +0700 Subject: clean up --- examples/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); } -- cgit v1.3.1 From 8bd923f5f17fd124f9b479df031ecb82cd6cc2d9 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 11 May 2022 00:37:45 +0700 Subject: 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 -- cgit v1.3.1 From f09df55ab986deea6d2df1168b1ad6f8eddd2ff1 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 11 May 2022 15:40:02 +0700 Subject: clean up example --- examples/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 -- cgit v1.3.1 From be2a51316cd708a99899732acf43920537b97526 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 16 May 2022 13:29:12 +0700 Subject: 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 -- cgit v1.3.1 From 4586d8f119f779d1ef5f937e5c79e0ea2c84579c Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 16 May 2022 13:44:06 +0700 Subject: 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 -- cgit v1.3.1 From e0e9426c2a912c2c405da602b1bf8d75d16f92e7 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 16 May 2022 16:00:15 +0700 Subject: 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; -- cgit v1.3.1