Преглед изворни кода

start to add pio usb (host) support

run as proof of concept
hathach пре 4 година
родитељ
комит
1d29817139

+ 1 - 1
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})
+family_configure_host_example(${PROJECT})

+ 6 - 1
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
 

+ 3 - 0
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);

+ 18 - 2
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

+ 1 - 0
lib/Pico-PIO-USB

@@ -0,0 +1 @@
+Subproject commit 7d672990011695892da523cb0ac29a2c7243a4c9

+ 12 - 1
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);
 
 //--------------------------------------------------------------------+

+ 9 - 3
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();
   }

+ 8 - 1
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;
 }

+ 237 - 0
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

+ 5 - 4
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