Merge pull request #2994 from hathach/enhance-hub

Enhance hub driver
This commit is contained in:
Ha Thach
2025-02-21 10:36:52 +07:00
committed by GitHub
21 changed files with 801 additions and 419 deletions

View File

@@ -476,6 +476,10 @@ function(family_flash_openocd TARGET)
set(OPENOCD_OPTION2 "") set(OPENOCD_OPTION2 "")
endif () endif ()
if (DEFINED OPENOCD_SERIAL)
set(OPENOCD_OPTION "-c \"adapter serial ${OPENOCD_SERIAL}\" ${OPENOCD_OPTION}")
endif ()
separate_arguments(OPTION_LIST UNIX_COMMAND ${OPENOCD_OPTION}) separate_arguments(OPTION_LIST UNIX_COMMAND ${OPENOCD_OPTION})
separate_arguments(OPTION_LIST2 UNIX_COMMAND ${OPENOCD_OPTION2}) separate_arguments(OPTION_LIST2 UNIX_COMMAND ${OPENOCD_OPTION2})

View File

@@ -0,0 +1,2 @@
set(PICO_PLATFORM rp2040)
set(PICO_BOARD adafruit_feather_rp2040_usb_host)

View File

@@ -0,0 +1,64 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2025 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.
*/
#ifndef TUSB_BOARD_H
#define TUSB_BOARD_H
#ifdef __cplusplus
extern "C" {
#endif
// UART and LED are already defined in pico-sdk board
//--------------------------------------------------------------------+
// PIO_USB
//--------------------------------------------------------------------+
#define PICO_DEFAULT_PIO_USB_DP_PIN 16
#define PICO_DEFAULT_PIO_USB_VBUSEN_PIN 18
#define PICO_DEFAULT_PIO_USB_VBUSEN_STATE 1
//--------------------------------------------------------------------
// USB Host MAX3421E
//--------------------------------------------------------------------
#ifdef PICO_DEFAULT_SPI
#define MAX3421_SPI PICO_DEFAULT_SPI // sdk v2
#else
#define MAX3421_SPI PICO_DEFAULT_SPI_INSTANCE // sdk v1
#endif
#define MAX3421_SCK_PIN PICO_DEFAULT_SPI_SCK_PIN
#define MAX3421_MOSI_PIN PICO_DEFAULT_SPI_TX_PIN
#define MAX3421_MISO_PIN PICO_DEFAULT_SPI_RX_PIN
#define MAX3421_CS_PIN 10
#define MAX3421_INTR_PIN 9
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,113 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2025 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.
*/
#ifndef BOARDS_ADAFRUIT_FRUIT_JAM_H
#define BOARDS_ADAFRUIT_FRUIT_JAM_H
// required for board that is not part of pico-sdk
// -----------------------------------------------------
// NOTE: THIS HEADER IS ALSO INCLUDED BY ASSEMBLER SO
// SHOULD ONLY CONSIST OF PREPROCESSOR DIRECTIVES
// -----------------------------------------------------
// pico_cmake_set PICO_PLATFORM=rp2350
// On some samples, the xosc can take longer to stabilize than is usual
#ifndef PICO_XOSC_STARTUP_DELAY_MULTIPLIER
#define PICO_XOSC_STARTUP_DELAY_MULTIPLIER 64
#endif
// For board detection
#define ADAFRUIT_FRUIT_JAM
// --- RP2350 VARIANT ---
#define PICO_RP2350A 0
// --- UART ---
#ifndef PICO_DEFAULT_UART
#define PICO_DEFAULT_UART 1
#endif
#ifndef PICO_DEFAULT_UART_TX_PIN
#define PICO_DEFAULT_UART_TX_PIN 8
#endif
#ifndef PICO_DEFAULT_UART_RX_PIN
#define PICO_DEFAULT_UART_RX_PIN 9
#endif
// --- LED ---
#ifndef PICO_DEFAULT_LED_PIN
#define PICO_DEFAULT_LED_PIN 29
#endif
#ifndef PICO_DEFAULT_WS2812_PIN
#define PICO_DEFAULT_WS2812_PIN 32
#endif
// --- I2C ---
#ifndef PICO_DEFAULT_I2C
#define PICO_DEFAULT_I2C 0
#endif
#ifndef PICO_DEFAULT_I2C_SDA_PIN
#define PICO_DEFAULT_I2C_SDA_PIN 20
#endif
#ifndef PICO_DEFAULT_I2C_SCL_PIN
#define PICO_DEFAULT_I2C_SCL_PIN 21
#endif
// --- SPI ---
#ifndef PICO_DEFAULT_SPI
#define PICO_DEFAULT_SPI 1
#endif
#ifndef PICO_DEFAULT_SPI_SCK_PIN
#define PICO_DEFAULT_SPI_SCK_PIN 30
#endif
#ifndef PICO_DEFAULT_SPI_TX_PIN
#define PICO_DEFAULT_SPI_TX_PIN 31
#endif
#ifndef PICO_DEFAULT_SPI_RX_PIN
#define PICO_DEFAULT_SPI_RX_PIN 28
#endif
// --- FLASH ---
// FruitJam use w25q128 but sdk does not have .s for it, use q080 instead
#define PICO_BOOT_STAGE2_CHOOSE_W25Q080 1
#ifndef PICO_FLASH_SPI_CLKDIV
#define PICO_FLASH_SPI_CLKDIV 2
#endif
// pico_cmake_set_default PICO_FLASH_SIZE_BYTES = (8 * 1024 * 1024)
#ifndef PICO_FLASH_SIZE_BYTES
#define PICO_FLASH_SIZE_BYTES (8 * 1024 * 1024)
#endif
// pico_cmake_set_default PICO_RP2350_A2_SUPPORTED = 1
#ifndef PICO_RP2350_A2_SUPPORTED
#define PICO_RP2350_A2_SUPPORTED 1
#endif
#endif

View File

@@ -0,0 +1,4 @@
set(PICO_PLATFORM rp2350-arm-s)
set(PICO_BOARD adafruit_fruit_jam)
set(PICO_BOARD_HEADER_DIRS ${CMAKE_CURRENT_LIST_DIR})
#set(OPENOCD_SERIAL E6614103E78E8324)

View File

@@ -0,0 +1,52 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2025 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.
*/
#ifndef TUSB_BOARD_H
#define TUSB_BOARD_H
#ifdef __cplusplus
extern "C" {
#endif
// UART and LED are already defined in pico-sdk board
//--------------------------------------------------------------------+
// PIO_USB
//--------------------------------------------------------------------+
// default to pico brain tester
#define PICO_DEFAULT_PIO_USB_DP_PIN 1
#define PICO_DEFAULT_PIO_USB_VBUSEN_PIN 11
#define PICO_DEFAULT_PIO_USB_VBUSEN_STATE 1
//--------------------------------------------------------------------
// USB Host MAX3421E
//--------------------------------------------------------------------
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,64 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2025 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.
*/
#ifndef TUSB_BOARD_H
#define TUSB_BOARD_H
#ifdef __cplusplus
extern "C" {
#endif
// UART and LED are already defined in pico-sdk board
//--------------------------------------------------------------------+
// PIO_USB
//--------------------------------------------------------------------+
#define PICO_DEFAULT_PIO_USB_DP_PIN 16
#define PICO_DEFAULT_PIO_USB_VBUSEN_PIN 18
#define PICO_DEFAULT_PIO_USB_VBUSEN_STATE 1
//--------------------------------------------------------------------
// USB Host MAX3421E
//--------------------------------------------------------------------
#ifdef PICO_DEFAULT_SPI
#define MAX3421_SPI PICO_DEFAULT_SPI // sdk v2
#else
#define MAX3421_SPI PICO_DEFAULT_SPI_INSTANCE // sdk v1
#endif
#define MAX3421_SCK_PIN PICO_DEFAULT_SPI_SCK_PIN
#define MAX3421_MOSI_PIN PICO_DEFAULT_SPI_TX_PIN
#define MAX3421_MISO_PIN PICO_DEFAULT_SPI_RX_PIN
#define MAX3421_CS_PIN 10
#define MAX3421_INTR_PIN 9
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,2 +1,3 @@
set(PICO_PLATFORM rp2040) set(PICO_PLATFORM rp2040)
set(PICO_BOARD pico) set(PICO_BOARD pico)
#set(OPENOCD_SERIAL E6614103E719612F)

View File

@@ -1,7 +1,7 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2021, Ha Thach (tinyusb.org) * Copyright (c) 2025 Ha Thach (tinyusb.org)
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@@ -29,55 +29,22 @@
url: https://www.raspberrypi.org/products/raspberry-pi-pico/ url: https://www.raspberrypi.org/products/raspberry-pi-pico/
*/ */
#ifndef BOARD_H_ #ifndef TUSB_BOARD_H
#define BOARD_H_ #define TUSB_BOARD_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
// LED // UART and LED are already defined in pico-sdk board
#ifdef PICO_DEFAULT_LED_PIN
#define LED_PIN PICO_DEFAULT_LED_PIN
#define LED_STATE_ON (!(PICO_DEFAULT_LED_PIN_INVERTED))
#endif
// Button pin is BOOTSEL which is flash CS pin
#define BUTTON_BOOTSEL
#define BUTTON_STATE_ACTIVE 0
// UART
#if defined(PICO_DEFAULT_UART_TX_PIN) && defined(PICO_DEFAULT_UART_RX_PIN) && \
defined(PICO_DEFAULT_UART) && defined(LIB_PICO_STDIO_UART)
#define UART_DEV PICO_DEFAULT_UART
#define UART_TX_PIN PICO_DEFAULT_UART_TX_PIN
#define UART_RX_PIN PICO_DEFAULT_UART_RX_PIN
#endif
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// PIO_USB // PIO_USB
// default to pin on Adafruit Feather rp2040 USB Host or Tester if defined
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// default to pico brain tester
// #define USE_ADAFRUIT_FEATHER_RP2040_USBHOST
#ifdef USE_ADAFRUIT_FEATHER_RP2040_USBHOST
#define PICO_DEFAULT_PIO_USB_DP_PIN 16
#define PICO_DEFAULT_PIO_USB_VBUSEN_PIN 18
#endif
#ifndef PICO_DEFAULT_PIO_USB_DP_PIN
#define PICO_DEFAULT_PIO_USB_DP_PIN 20 #define PICO_DEFAULT_PIO_USB_DP_PIN 20
#endif
// VBUS enable pin and its active state
#ifndef PICO_DEFAULT_PIO_USB_VBUSEN_PIN
#define PICO_DEFAULT_PIO_USB_VBUSEN_PIN 22 #define PICO_DEFAULT_PIO_USB_VBUSEN_PIN 22
#endif
// VBUS enable state
#ifndef PICO_DEFAULT_PIO_USB_VBUSEN_STATE
#define PICO_DEFAULT_PIO_USB_VBUSEN_STATE 1 #define PICO_DEFAULT_PIO_USB_VBUSEN_STATE 1
#endif
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// USB Host MAX3421E // USB Host MAX3421E
@@ -99,4 +66,4 @@
} }
#endif #endif
#endif /* BOARD_H_ */ #endif

View File

@@ -1,2 +1,3 @@
set(PICO_PLATFORM rp2350-arm-s) set(PICO_PLATFORM rp2350-arm-s)
set(PICO_BOARD pico2) set(PICO_BOARD pico2)
#set(OPENOCD_SERIAL E6614103E77C5A24)

View File

@@ -0,0 +1,69 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2025 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.
*/
/* metadata:
name: Pico2
url: https://www.raspberrypi.org/products/raspberry-pi-pico/
*/
#ifndef TUSB_BOARD_H
#define TUSB_BOARD_H
#ifdef __cplusplus
extern "C" {
#endif
// UART and LED are already defined in pico-sdk board
//--------------------------------------------------------------------+
// PIO_USB
//--------------------------------------------------------------------+
// default to pico brain tester
#define PICO_DEFAULT_PIO_USB_DP_PIN 20
#define PICO_DEFAULT_PIO_USB_VBUSEN_PIN 22
#define PICO_DEFAULT_PIO_USB_VBUSEN_STATE 1
//--------------------------------------------------------------------
// USB Host MAX3421E
//--------------------------------------------------------------------
#ifdef PICO_DEFAULT_SPI
#define MAX3421_SPI PICO_DEFAULT_SPI // sdk v2
#else
#define MAX3421_SPI PICO_DEFAULT_SPI_INSTANCE // sdk v1
#endif
#define MAX3421_SCK_PIN PICO_DEFAULT_SPI_SCK_PIN
#define MAX3421_MOSI_PIN PICO_DEFAULT_SPI_TX_PIN
#define MAX3421_MISO_PIN PICO_DEFAULT_SPI_RX_PIN
#define MAX3421_CS_PIN 10
#define MAX3421_INTR_PIN 9
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -42,10 +42,6 @@
#include "bsp/board_api.h" #include "bsp/board_api.h"
#include "board.h" #include "board.h"
#ifdef UART_DEV
static uart_inst_t *uart_inst;
#endif
#if (CFG_TUH_ENABLED && CFG_TUH_RPI_PIO_USB) || (CFG_TUD_ENABLED && CFG_TUD_RPI_PIO_USB) #if (CFG_TUH_ENABLED && CFG_TUH_RPI_PIO_USB) || (CFG_TUD_ENABLED && CFG_TUD_RPI_PIO_USB)
#include "pio_usb.h" #include "pio_usb.h"
#endif #endif
@@ -55,6 +51,36 @@ static uart_inst_t *uart_inst;
static void max3421_init(void); static void max3421_init(void);
#endif #endif
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
// LED
#if !defined(LED_PIN) && defined(PICO_DEFAULT_LED_PIN)
#define LED_PIN PICO_DEFAULT_LED_PIN
#define LED_STATE_ON (!(PICO_DEFAULT_LED_PIN_INVERTED))
#endif
// Button, if not defined use BOOTSEL button
#ifndef BUTTON_PIN
#define BUTTON_BOOTSEL
#define BUTTON_STATE_ACTIVE 0
#endif
// UART
#if !defined(UART_DEV) && defined(PICO_DEFAULT_UART) && defined(LIB_PICO_STDIO_UART) && \
defined(PICO_DEFAULT_UART_TX_PIN) && defined(PICO_DEFAULT_UART_RX_PIN)
#define UART_DEV PICO_DEFAULT_UART
#define UART_TX_PIN PICO_DEFAULT_UART_TX_PIN
#define UART_RX_PIN PICO_DEFAULT_UART_RX_PIN
#endif
#ifdef UART_DEV
static uart_inst_t *uart_inst;
#endif
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
#ifdef BUTTON_BOOTSEL #ifdef BUTTON_BOOTSEL
// This example blinks the Picoboard LED when the BOOTSEL button is pressed. // This example blinks the Picoboard LED when the BOOTSEL button is pressed.
// //
@@ -79,7 +105,7 @@ bool __no_inline_not_in_flash_func(get_bootsel_button)(void) {
IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS); IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS);
// Note we can't call into any sleep functions in flash right now // Note we can't call into any sleep functions in flash right now
for (volatile int i = 0; i < 1000; ++i); for (volatile int i = 0; i < 1000; ++i) {}
// The HI GPIO registers in SIO can observe and control the 6 QSPI pins. // The HI GPIO registers in SIO can observe and control the 6 QSPI pins.
// Note the button pulls the pin *low* when pressed. // Note the button pulls the pin *low* when pressed.
@@ -133,12 +159,15 @@ void stdio_rtt_init(void) {
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// //
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void board_init(void) void board_init(void)
{ {
#if (CFG_TUH_ENABLED && CFG_TUH_RPI_PIO_USB) || (CFG_TUD_ENABLED && CFG_TUD_RPI_PIO_USB) #if (CFG_TUH_ENABLED && CFG_TUH_RPI_PIO_USB) || (CFG_TUD_ENABLED && CFG_TUD_RPI_PIO_USB)
// Set the system clock to a multiple of 120mhz for bitbanging USB with pico-usb // Set the system clock to a multiple of 12mhz for bit-banging USB with pico-usb
set_sys_clock_khz(120000, true); set_sys_clock_khz(120000, true);
// set_sys_clock_khz(180000, true);
// set_sys_clock_khz(192000, true);
// set_sys_clock_khz(240000, true);
// set_sys_clock_khz(264000, true);
#ifdef PICO_DEFAULT_PIO_USB_VBUSEN_PIN #ifdef PICO_DEFAULT_PIO_USB_VBUSEN_PIN
gpio_init(PICO_DEFAULT_PIO_USB_VBUSEN_PIN); gpio_init(PICO_DEFAULT_PIO_USB_VBUSEN_PIN);
@@ -193,7 +222,6 @@ void board_init(void)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Board porting API // Board porting API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
void board_led_write(bool state) { void board_led_write(bool state) {
(void) state; (void) state;

View File

@@ -31,7 +31,9 @@ elseif (PICO_PLATFORM STREQUAL "rp2350-riscv")
set(OPENOCD_TARGET rp2350-riscv) set(OPENOCD_TARGET rp2350-riscv)
endif() endif()
if (NOT OPENOCD_OPTION)
set(OPENOCD_OPTION "-f interface/cmsis-dap.cfg -f target/${OPENOCD_TARGET}.cfg -c \"adapter speed 5000\"") set(OPENOCD_OPTION "-f interface/cmsis-dap.cfg -f target/${OPENOCD_TARGET}.cfg -c \"adapter speed 5000\"")
endif()
if (NOT PICO_TINYUSB_PATH) if (NOT PICO_TINYUSB_PATH)
set(PICO_TINYUSB_PATH ${TOP}) set(PICO_TINYUSB_PATH ${TOP})
@@ -143,6 +145,7 @@ target_sources(tinyusb_bsp INTERFACE
) )
target_include_directories(tinyusb_bsp INTERFACE target_include_directories(tinyusb_bsp INTERFACE
${TOP}/hw ${TOP}/hw
${CMAKE_CURRENT_LIST_DIR}/boards/${BOARD}
) )
target_link_libraries(tinyusb_bsp INTERFACE target_link_libraries(tinyusb_bsp INTERFACE
pico_unique_id pico_unique_id

View File

@@ -0,0 +1,3 @@
source [find interface/cmsis-dap.cfg]
adapter speed 5000
source [find target/rp2350.cfg]

View File

@@ -302,9 +302,9 @@ typedef enum {
enum { enum {
CONTROL_STAGE_IDLE = 0, CONTROL_STAGE_IDLE = 0,
CONTROL_STAGE_SETUP, CONTROL_STAGE_SETUP, // 1
CONTROL_STAGE_DATA, CONTROL_STAGE_DATA, // 2
CONTROL_STAGE_ACK CONTROL_STAGE_ACK // 3
}; };
enum { enum {

View File

@@ -40,29 +40,36 @@
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF // MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
typedef struct typedef struct {
{
uint8_t itf_num; uint8_t itf_num;
uint8_t ep_in; uint8_t ep_in;
uint8_t port_count;
CFG_TUH_MEM_ALIGN uint8_t status_change; // from hub descriptor
CFG_TUH_MEM_ALIGN hub_port_status_response_t port_status; uint8_t bNbrPorts;
CFG_TUH_MEM_ALIGN hub_status_response_t hub_status; uint8_t bPwrOn2PwrGood; // port power on to good, in 2ms unit
// uint16_t wHubCharacteristics;
hub_port_status_response_t port_status;
} hub_interface_t; } hub_interface_t;
CFG_TUH_MEM_SECTION static hub_interface_t hub_data[CFG_TUH_HUB]; typedef struct {
CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN static uint8_t _hub_buffer[sizeof(descriptor_hub_desc_t)]; TUH_EPBUF_DEF(status_change, 4); // interrupt endpoint
TUH_EPBUF_DEF(ctrl_buf, CFG_TUH_HUB_BUFSIZE);
} hub_epbuf_t;
TU_ATTR_ALWAYS_INLINE static hub_interface_t hub_itfs[CFG_TUH_HUB];
static inline hub_interface_t* get_itf(uint8_t dev_addr) CFG_TUH_MEM_SECTION static hub_epbuf_t hub_epbufs[CFG_TUH_HUB];
{
return &hub_data[dev_addr-1-CFG_TUH_DEVICE_MAX]; TU_ATTR_ALWAYS_INLINE static inline hub_interface_t* get_hub_itf(uint8_t daddr) {
return &hub_itfs[daddr-1-CFG_TUH_DEVICE_MAX];
} }
#if CFG_TUSB_DEBUG >= 2 TU_ATTR_ALWAYS_INLINE static inline hub_epbuf_t* get_hub_epbuf(uint8_t daddr) {
static char const* const _hub_feature_str[] = return &hub_epbufs[daddr-1-CFG_TUH_DEVICE_MAX];
{ }
#if CFG_TUSB_DEBUG >= HUB_DEBUG
static char const* const _hub_feature_str[] = {
[HUB_FEATURE_PORT_CONNECTION ] = "PORT_CONNECTION", [HUB_FEATURE_PORT_CONNECTION ] = "PORT_CONNECTION",
[HUB_FEATURE_PORT_ENABLE ] = "PORT_ENABLE", [HUB_FEATURE_PORT_ENABLE ] = "PORT_ENABLE",
[HUB_FEATURE_PORT_SUSPEND ] = "PORT_SUSPEND", [HUB_FEATURE_PORT_SUSPEND ] = "PORT_SUSPEND",
@@ -84,12 +91,9 @@ static char const* const _hub_feature_str[] =
// HUB // HUB
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature, bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
tuh_xfer_cb_t complete_cb, uintptr_t user_data) tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
{ tusb_control_request_t const request = {
tusb_control_request_t const request = .bmRequestType_bit = {
{
.bmRequestType_bit =
{
.recipient = (hub_port == 0) ? TUSB_REQ_RCPT_DEVICE : TUSB_REQ_RCPT_OTHER, .recipient = (hub_port == 0) ? TUSB_REQ_RCPT_DEVICE : TUSB_REQ_RCPT_OTHER,
.type = TUSB_REQ_TYPE_CLASS, .type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_OUT .direction = TUSB_DIR_OUT
@@ -100,8 +104,7 @@ bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
.wLength = 0 .wLength = 0
}; };
tuh_xfer_t xfer = tuh_xfer_t xfer = {
{
.daddr = hub_addr, .daddr = hub_addr,
.ep_addr = 0, .ep_addr = 0,
.setup = &request, .setup = &request,
@@ -110,18 +113,15 @@ bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
.user_data = user_data .user_data = user_data
}; };
TU_LOG2("HUB Clear Feature: %s, addr = %u port = %u\r\n", _hub_feature_str[feature], hub_addr, hub_port); TU_LOG_DRV("HUB Clear Feature: %s, addr = %u port = %u\r\n", _hub_feature_str[feature], hub_addr, hub_port);
TU_ASSERT(tuh_control_xfer(&xfer)); TU_ASSERT(tuh_control_xfer(&xfer));
return true; return true;
} }
bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature, bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
tuh_xfer_cb_t complete_cb, uintptr_t user_data) tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
{ tusb_control_request_t const request = {
tusb_control_request_t const request = .bmRequestType_bit = {
{
.bmRequestType_bit =
{
.recipient = (hub_port == 0) ? TUSB_REQ_RCPT_DEVICE : TUSB_REQ_RCPT_OTHER, .recipient = (hub_port == 0) ? TUSB_REQ_RCPT_DEVICE : TUSB_REQ_RCPT_OTHER,
.type = TUSB_REQ_TYPE_CLASS, .type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_OUT .direction = TUSB_DIR_OUT
@@ -132,8 +132,7 @@ bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
.wLength = 0 .wLength = 0
}; };
tuh_xfer_t xfer = tuh_xfer_t xfer = {
{
.daddr = hub_addr, .daddr = hub_addr,
.ep_addr = 0, .ep_addr = 0,
.setup = &request, .setup = &request,
@@ -142,18 +141,15 @@ bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
.user_data = user_data .user_data = user_data
}; };
TU_LOG2("HUB Set Feature: %s, addr = %u port = %u\r\n", _hub_feature_str[feature], hub_addr, hub_port); TU_LOG_DRV("HUB Set Feature: %s, addr = %u port = %u\r\n", _hub_feature_str[feature], hub_addr, hub_port);
TU_ASSERT( tuh_control_xfer(&xfer) ); TU_ASSERT( tuh_control_xfer(&xfer) );
return true; return true;
} }
bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp, bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp,
tuh_xfer_cb_t complete_cb, uintptr_t user_data) tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
{ tusb_control_request_t const request = {
tusb_control_request_t const request = .bmRequestType_bit = {
{
.bmRequestType_bit =
{
.recipient = (hub_port == 0) ? TUSB_REQ_RCPT_DEVICE : TUSB_REQ_RCPT_OTHER, .recipient = (hub_port == 0) ? TUSB_REQ_RCPT_DEVICE : TUSB_REQ_RCPT_OTHER,
.type = TUSB_REQ_TYPE_CLASS, .type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_IN .direction = TUSB_DIR_IN
@@ -161,11 +157,10 @@ bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp,
.bRequest = HUB_REQUEST_GET_STATUS, .bRequest = HUB_REQUEST_GET_STATUS,
.wValue = 0, .wValue = 0,
.wIndex = hub_port, .wIndex = hub_port,
.wLength = 4 .wLength = tu_htole16(4)
}; };
tuh_xfer_t xfer = tuh_xfer_t xfer = {
{
.daddr = hub_addr, .daddr = hub_addr,
.ep_addr = 0, .ep_addr = 0,
.setup = &request, .setup = &request,
@@ -174,7 +169,7 @@ bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp,
.user_data = user_data .user_data = user_data
}; };
TU_LOG2("HUB Get Port Status: addr = %u port = %u\r\n", hub_addr, hub_port); TU_LOG_DRV("HUB Get Port Status: addr = %u port = %u\r\n", hub_addr, hub_port);
TU_VERIFY( tuh_control_xfer(&xfer) ); TU_VERIFY( tuh_control_xfer(&xfer) );
return true; return true;
} }
@@ -183,7 +178,7 @@ bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp,
// CLASS-USBH API (don't require to verify parameters) // CLASS-USBH API (don't require to verify parameters)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
bool hub_init(void) { bool hub_init(void) {
tu_memclr(hub_data, sizeof(hub_data)); tu_memclr(hub_itfs, sizeof(hub_itfs));
return true; return true;
} }
@@ -191,40 +186,32 @@ bool hub_deinit(void) {
return true; return true;
} }
bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len) bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len) {
{
(void) rhport; (void) rhport;
TU_VERIFY(TUSB_CLASS_HUB == itf_desc->bInterfaceClass && TU_VERIFY(TUSB_CLASS_HUB == itf_desc->bInterfaceClass &&
0 == itf_desc->bInterfaceSubClass); 0 == itf_desc->bInterfaceSubClass);
TU_VERIFY(itf_desc->bInterfaceProtocol <= 1); // not support multiple TT yet
// hub driver does not support multiple TT yet
TU_VERIFY(itf_desc->bInterfaceProtocol <= 1);
// msc driver length is fixed
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_desc_endpoint_t); uint16_t const drv_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_desc_endpoint_t);
TU_ASSERT(drv_len <= max_len); TU_ASSERT(drv_len <= max_len);
//------------- Interrupt Status endpoint -------------// // Interrupt Status endpoint
tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc); tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc);
TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType &&
TUSB_XFER_INTERRUPT == desc_ep->bmAttributes.xfer, 0); TUSB_XFER_INTERRUPT == desc_ep->bmAttributes.xfer, 0);
TU_ASSERT(tuh_edpt_open(dev_addr, desc_ep)); TU_ASSERT(tuh_edpt_open(dev_addr, desc_ep));
hub_interface_t* p_hub = get_itf(dev_addr); hub_interface_t* p_hub = get_hub_itf(dev_addr);
p_hub->itf_num = itf_desc->bInterfaceNumber; p_hub->itf_num = itf_desc->bInterfaceNumber;
p_hub->ep_in = desc_ep->bEndpointAddress; p_hub->ep_in = desc_ep->bEndpointAddress;
return true; return true;
} }
void hub_close(uint8_t dev_addr) void hub_close(uint8_t dev_addr) {
{
TU_VERIFY(dev_addr > CFG_TUH_DEVICE_MAX, ); TU_VERIFY(dev_addr > CFG_TUH_DEVICE_MAX, );
hub_interface_t* p_hub = get_itf(dev_addr); hub_interface_t* p_hub = get_hub_itf(dev_addr);
if (p_hub->ep_in) { if (p_hub->ep_in) {
TU_LOG_DRV(" HUB close addr = %d\r\n", dev_addr); TU_LOG_DRV(" HUB close addr = %d\r\n", dev_addr);
@@ -232,30 +219,33 @@ void hub_close(uint8_t dev_addr)
} }
} }
bool hub_edpt_status_xfer(uint8_t dev_addr) bool hub_edpt_status_xfer(uint8_t daddr) {
{ hub_interface_t* p_hub = get_hub_itf(daddr);
hub_interface_t* hub_itf = get_itf(dev_addr); hub_epbuf_t* p_epbuf = get_hub_epbuf(daddr);
return usbh_edpt_xfer(dev_addr, hub_itf->ep_in, &hub_itf->status_change, 1);
TU_VERIFY(usbh_edpt_claim(daddr, p_hub->ep_in));
if (!usbh_edpt_xfer(daddr, p_hub->ep_in, p_epbuf->status_change, 1)) {
usbh_edpt_release(daddr, p_hub->ep_in);
return false;
} }
return true;
}
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Set Configure // Set Configure
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static void config_set_port_power (tuh_xfer_t* xfer); static void config_set_port_power (tuh_xfer_t* xfer);
static void config_port_power_complete (tuh_xfer_t* xfer); static void config_port_power_complete (tuh_xfer_t* xfer);
bool hub_set_config(uint8_t dev_addr, uint8_t itf_num) bool hub_set_config(uint8_t dev_addr, uint8_t itf_num) {
{ hub_interface_t* p_hub = get_hub_itf(dev_addr);
hub_interface_t* p_hub = get_itf(dev_addr);
TU_ASSERT(itf_num == p_hub->itf_num); TU_ASSERT(itf_num == p_hub->itf_num);
hub_epbuf_t* p_epbuf = get_hub_epbuf(dev_addr);
// Get Hub Descriptor // Get Hub Descriptor
tusb_control_request_t const request = tusb_control_request_t const request = {
{ .bmRequestType_bit = {
.bmRequestType_bit =
{
.recipient = TUSB_REQ_RCPT_DEVICE, .recipient = TUSB_REQ_RCPT_DEVICE,
.type = TUSB_REQ_TYPE_CLASS, .type = TUSB_REQ_TYPE_CLASS,
.direction = TUSB_DIR_IN .direction = TUSB_DIR_IN
@@ -263,34 +253,33 @@ bool hub_set_config(uint8_t dev_addr, uint8_t itf_num)
.bRequest = HUB_REQUEST_GET_DESCRIPTOR, .bRequest = HUB_REQUEST_GET_DESCRIPTOR,
.wValue = 0, .wValue = 0,
.wIndex = 0, .wIndex = 0,
.wLength = sizeof(descriptor_hub_desc_t) .wLength = sizeof(hub_desc_cs_t)
}; };
tuh_xfer_t xfer = tuh_xfer_t xfer = {
{
.daddr = dev_addr, .daddr = dev_addr,
.ep_addr = 0, .ep_addr = 0,
.setup = &request, .setup = &request,
.buffer = _hub_buffer, .buffer = p_epbuf->ctrl_buf,
.complete_cb = config_set_port_power, .complete_cb = config_set_port_power,
.user_data = 0 .user_data = 0
}; };
TU_ASSERT(tuh_control_xfer(&xfer)); TU_ASSERT(tuh_control_xfer(&xfer));
return true; return true;
} }
static void config_set_port_power (tuh_xfer_t* xfer) static void config_set_port_power (tuh_xfer_t* xfer) {
{
TU_ASSERT(XFER_RESULT_SUCCESS == xfer->result, ); TU_ASSERT(XFER_RESULT_SUCCESS == xfer->result, );
uint8_t const daddr = xfer->daddr; uint8_t const daddr = xfer->daddr;
hub_interface_t* p_hub = get_itf(daddr); hub_interface_t* p_hub = get_hub_itf(daddr);
hub_epbuf_t* p_epbuf = get_hub_epbuf(daddr);
// only use number of ports in hub descriptor // only use number of ports in hub descriptor
descriptor_hub_desc_t const* desc_hub = (descriptor_hub_desc_t const*) _hub_buffer; hub_desc_cs_t const* desc_hub = (hub_desc_cs_t const*) p_epbuf->ctrl_buf;
p_hub->port_count = desc_hub->bNbrPorts; p_hub->bNbrPorts = desc_hub->bNbrPorts;
p_hub->bPwrOn2PwrGood = desc_hub->bPwrOn2PwrGood;
// May need to GET_STATUS // May need to GET_STATUS
@@ -299,22 +288,22 @@ static void config_set_port_power (tuh_xfer_t* xfer)
hub_port_set_feature(daddr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete, 0); hub_port_set_feature(daddr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete, 0);
} }
static void config_port_power_complete (tuh_xfer_t* xfer) static void config_port_power_complete (tuh_xfer_t* xfer) {
{
TU_ASSERT(XFER_RESULT_SUCCESS == xfer->result, ); TU_ASSERT(XFER_RESULT_SUCCESS == xfer->result, );
uint8_t const daddr = xfer->daddr; uint8_t const daddr = xfer->daddr;
hub_interface_t* p_hub = get_itf(daddr); hub_interface_t* p_hub = get_hub_itf(daddr);
if (xfer->setup->wIndex == p_hub->port_count) if (xfer->setup->wIndex == p_hub->bNbrPorts) {
{
// All ports are power -> queue notification status endpoint and // All ports are power -> queue notification status endpoint and
// complete the SET CONFIGURATION // complete the SET CONFIGURATION
TU_ASSERT( usbh_edpt_xfer(daddr, p_hub->ep_in, &p_hub->status_change, 1), ); if (!hub_edpt_status_xfer(daddr)) {
TU_MESS_FAILED();
TU_BREAKPOINT();
}
// delay bPwrOn2PwrGood * 2 ms before set configuration complete
usbh_driver_set_config_complete(daddr, p_hub->itf_num); usbh_driver_set_config_complete(daddr, p_hub->itf_num);
}else } else {
{
// power next port // power next port
uint8_t const hub_port = (uint8_t) (xfer->setup->wIndex + 1); uint8_t const hub_port = (uint8_t) (xfer->setup->wIndex + 1);
hub_port_set_feature(daddr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete, 0); hub_port_set_feature(daddr, hub_port, HUB_FEATURE_PORT_POWER, config_port_power_complete, 0);
@@ -324,180 +313,162 @@ static void config_port_power_complete (tuh_xfer_t* xfer)
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Connection Changes // Connection Changes
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
static void get_status_complete (tuh_xfer_t* xfer);
static void hub_port_get_status_complete (tuh_xfer_t* xfer); static void port_get_status_complete (tuh_xfer_t* xfer);
static void hub_get_status_complete (tuh_xfer_t* xfer); static void port_clear_feature_complete_stub(tuh_xfer_t* xfer);
static void connection_clear_conn_change_complete (tuh_xfer_t* xfer); static void connection_clear_conn_change_complete (tuh_xfer_t* xfer);
static void connection_port_reset_complete (tuh_xfer_t* xfer); static void connection_port_reset_complete (tuh_xfer_t* xfer);
// callback as response of interrupt endpoint polling // callback as response of interrupt endpoint polling
bool hub_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { bool hub_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) {
(void) xferred_bytes; // TODO can be more than 1 for hub with lots of ports (void) xferred_bytes;
(void) ep_addr; (void) ep_addr;
TU_VERIFY(result == XFER_RESULT_SUCCESS);
hub_interface_t* p_hub = get_itf(dev_addr); bool processed = false; // true if new status is processed
uint8_t const status_change = p_hub->status_change; if (result == XFER_RESULT_SUCCESS) {
TU_LOG2(" Hub Status Change = 0x%02X\r\n", status_change); hub_interface_t* p_hub = get_hub_itf(daddr);
hub_epbuf_t *p_epbuf = get_hub_epbuf(daddr);
const uint8_t status_change = p_epbuf->status_change[0];
TU_LOG_DRV(" Hub Status Change = 0x%02X\r\n", status_change);
if (status_change == 0) { if (status_change == 0) {
// The status change event was neither for the hub, nor for any of its ports. // The status change event was neither for the hub, nor for any of its ports.
// This shouldn't happen, but it does with some devices. // This shouldn't happen, but it does with some devices. Re-Initiate the interrupt poll.
// Initiate the next interrupt poll here. processed = false;
return hub_edpt_status_xfer(dev_addr); } else if (tu_bit_test(status_change, 0)) {
}
if (tu_bit_test(status_change, 0)) {
// Hub bit 0 is for the hub device events // Hub bit 0 is for the hub device events
if (hub_port_get_status(dev_addr, 0, &p_hub->hub_status, hub_get_status_complete, 0) == false) { processed = hub_get_status(daddr, p_epbuf->ctrl_buf, get_status_complete, 0);
//Hub status control transfer failed, retry } else {
hub_edpt_status_xfer(dev_addr);
}
}
else {
// Hub bits 1 to n are hub port events // Hub bits 1 to n are hub port events
for (uint8_t port=1; port <= p_hub->port_count; port++) { for (uint8_t port=1; port <= p_hub->bNbrPorts; port++) {
if (tu_bit_test(status_change, port)) { if (tu_bit_test(status_change, port)) {
if (hub_port_get_status(dev_addr, port, &p_hub->port_status, hub_port_get_status_complete, 0) == false) { processed = hub_port_get_status(daddr, port, p_epbuf->ctrl_buf, port_get_status_complete, 0);
//Hub status control transfer failed, retry break; // after completely processed one port, we will re-queue the status poll and handle next one
hub_edpt_status_xfer(dev_addr);
} }
break;
} }
} }
} }
// NOTE: next status transfer is queued by usbh.c after handling this request // If new status event is processed: next status pool is queued by usbh.c after handled this request
// Otherwise re-queue the status poll here
if (!processed) {
TU_ASSERT(hub_edpt_status_xfer(daddr));
}
return true; return true;
} }
static void hub_clear_feature_complete_stub(tuh_xfer_t* xfer) static void port_clear_feature_complete_stub(tuh_xfer_t* xfer) {
{
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
hub_edpt_status_xfer(xfer->daddr); hub_edpt_status_xfer(xfer->daddr);
} }
static void hub_get_status_complete (tuh_xfer_t* xfer) static void get_status_complete(tuh_xfer_t *xfer) {
{ const uint8_t daddr = xfer->daddr;
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
uint8_t const daddr = xfer->daddr; bool processed = false; // true if new status is processed
hub_interface_t* p_hub = get_itf(daddr); if (xfer->result == XFER_RESULT_SUCCESS) {
uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex); hub_status_response_t hub_status = *((const hub_status_response_t *) (uintptr_t) xfer->buffer);
TU_ASSERT(port_num == 0 , );
TU_LOG2("HUB Got hub status, addr = %u, status = %04x\r\n", daddr, p_hub->hub_status.change.value); TU_LOG_DRV("HUB Got hub status, addr = %u, status = %04x\r\n", daddr, hub_status.change.value);
if (p_hub->hub_status.change.local_power_source) if (hub_status.change.local_power_source) {
{ TU_LOG_DRV(" Local Power Change\r\n");
TU_LOG2("HUB Local Power Change, addr = %u\r\n", daddr); processed = hub_clear_feature(daddr, HUB_FEATURE_HUB_LOCAL_POWER_CHANGE, port_clear_feature_complete_stub, 0);
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_HUB_LOCAL_POWER_CHANGE, hub_clear_feature_complete_stub, 0); } else if (hub_status.change.over_current) {
} TU_LOG_DRV(" Over Current\r\n");
else if (p_hub->hub_status.change.over_current) processed = hub_clear_feature(daddr, HUB_FEATURE_HUB_OVER_CURRENT_CHANGE, port_clear_feature_complete_stub, 0);
{
TU_LOG1("HUB Over Current, addr = %u\r\n", daddr);
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_HUB_OVER_CURRENT_CHANGE, hub_clear_feature_complete_stub, 0);
} }
} }
static void hub_port_get_status_complete (tuh_xfer_t* xfer) if (!processed) {
{ TU_ASSERT(hub_edpt_status_xfer(daddr), );
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, ); }
}
uint8_t const daddr = xfer->daddr; static void port_get_status_complete(tuh_xfer_t *xfer) {
hub_interface_t* p_hub = get_itf(daddr); const uint8_t daddr = xfer->daddr;
uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex); bool processed = false; // true if new status is processed
if (xfer->result == XFER_RESULT_SUCCESS) {
const uint8_t port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
hub_interface_t *p_hub = get_hub_itf(daddr);
p_hub->port_status = *((const hub_port_status_response_t *) (uintptr_t) xfer->buffer);
// Clear port status change interrupts
if (p_hub->port_status.change.connection) {
// Connection change // Connection change
if (p_hub->port_status.change.connection)
{
// Port is powered and enabled // Port is powered and enabled
//TU_VERIFY(port_status.status_current.port_power && port_status.status_current.port_enable, ); //TU_VERIFY(port_status.status_current.port_power && port_status.status_current.port_enable, );
// Acknowledge Port Connection Change // Acknowledge Port Connection Change
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_CONNECTION_CHANGE, connection_clear_conn_change_complete, 0); processed = hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_CONNECTION_CHANGE, connection_clear_conn_change_complete, 0);
}else } else if (p_hub->port_status.change.port_enable) {
{ processed = hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_ENABLE_CHANGE, port_clear_feature_complete_stub, 0);
// Clear other port status change interrupts. TODO Not currently handled - just cleared. } else if (p_hub->port_status.change.suspend) {
if (p_hub->port_status.change.port_enable) processed = hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_SUSPEND_CHANGE, port_clear_feature_complete_stub, 0);
{ } else if (p_hub->port_status.change.over_current) {
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_ENABLE_CHANGE, hub_clear_feature_complete_stub, 0); processed = hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_OVER_CURRENT_CHANGE, port_clear_feature_complete_stub, 0);
} } else if (p_hub->port_status.change.reset) {
else if (p_hub->port_status.change.suspend) processed = hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_RESET_CHANGE, port_clear_feature_complete_stub, 0);
{
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_SUSPEND_CHANGE, hub_clear_feature_complete_stub, 0);
}
else if (p_hub->port_status.change.over_current)
{
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_OVER_CURRENT_CHANGE, hub_clear_feature_complete_stub, 0);
}
else if (p_hub->port_status.change.reset)
{
hub_port_clear_feature(daddr, port_num, HUB_FEATURE_PORT_RESET_CHANGE, hub_clear_feature_complete_stub, 0);
}
// Other changes are: L1 state
// TODO clear change
else
{
// prepare for next hub status
// TODO continue with status_change, or maybe we can do it again with status
hub_edpt_status_xfer(daddr);
}
} }
} }
static void connection_clear_conn_change_complete (tuh_xfer_t* xfer) if (!processed) {
{ TU_ASSERT(hub_edpt_status_xfer(daddr), );
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, ); }
}
uint8_t const daddr = xfer->daddr; static void connection_clear_conn_change_complete (tuh_xfer_t* xfer) {
hub_interface_t* p_hub = get_itf(daddr); const uint8_t daddr = xfer->daddr;
uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
if ( p_hub->port_status.status.connection ) if (xfer->result != XFER_RESULT_SUCCESS) {
{ TU_ASSERT(hub_edpt_status_xfer(daddr), );
return;
}
hub_interface_t *p_hub = get_hub_itf(daddr);
const uint8_t port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
if (p_hub->port_status.status.connection) {
// Reset port if attach event // Reset port if attach event
hub_port_reset(daddr, port_num, connection_port_reset_complete, 0); hub_port_reset(daddr, port_num, connection_port_reset_complete, 0);
}else } else {
{
// submit detach event // submit detach event
hcd_event_t event = const hcd_event_t event = {
{
.rhport = usbh_get_rhport(daddr), .rhport = usbh_get_rhport(daddr),
.event_id = HCD_EVENT_DEVICE_REMOVE, .event_id = HCD_EVENT_DEVICE_REMOVE,
.connection = .connection = {
{
.hub_addr = daddr, .hub_addr = daddr,
.hub_port = port_num .hub_port = port_num
} }
}; };
hcd_event_handler(&event, false); hcd_event_handler(&event, false);
} }
} }
static void connection_port_reset_complete (tuh_xfer_t* xfer) static void connection_port_reset_complete (tuh_xfer_t* xfer) {
{ const uint8_t daddr = xfer->daddr;
TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, );
uint8_t const daddr = xfer->daddr; if (xfer->result != XFER_RESULT_SUCCESS) {
// hub_interface_t* p_hub = get_itf(daddr); // retry port reset if failed
uint8_t const port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex); if (!tuh_control_xfer(xfer)) {
TU_ASSERT(hub_edpt_status_xfer(daddr), ); // back to status poll if failed to queue request
}
return;
}
const uint8_t port_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
// submit attach event // submit attach event
hcd_event_t event = hcd_event_t event = {
{
.rhport = usbh_get_rhport(daddr), .rhport = usbh_get_rhport(daddr),
.event_id = HCD_EVENT_DEVICE_ATTACH, .event_id = HCD_EVENT_DEVICE_ATTACH,
.connection = .connection = {
{
.hub_addr = daddr, .hub_addr = daddr,
.hub_port = port_num .hub_port = port_num
} }
}; };
hcd_event_handler(&event, false); hcd_event_handler(&event, false);
} }

View File

@@ -24,17 +24,8 @@
* This file is part of the TinyUSB stack. * This file is part of the TinyUSB stack.
*/ */
/** \ingroup group_class #ifndef TUSB_HUB_H_
* \defgroup ClassDriver_Hub Hub (Host only) #define TUSB_HUB_H_
* \details Like most PC's OS, Hub support is completely hidden from Application. In fact, application cannot determine whether
* a device is mounted directly via roothub or via a hub's port. All Hub-related procedures are performed and managed
* by tinyusb stack. Unless you are trying to develop the stack itself, there are nothing else can be used by Application.
* \note Due to my laziness, only 1-level of Hub is supported. In other way, the stack cannot mount a hub via another hub.
* @{
*/
#ifndef _TUSB_HUB_H_
#define _TUSB_HUB_H_
#include "common/tusb_common.h" #include "common/tusb_common.h"
@@ -42,63 +33,23 @@
extern "C" { extern "C" {
#endif #endif
//D1...D0: Logical Power Switching Mode //--------------------------------------------------------------------+
//00: Ganged power switching (all portspower at // Configuration
//once) //--------------------------------------------------------------------+
//01: Individual port power switching
//1X: Reserved. Used only on 1.0 compliant hubs #ifndef CFG_TUH_HUB_BUFSIZE
//that implement no power switching #define CFG_TUH_HUB_BUFSIZE 12
//D2: Identifies a Compound Device #endif
//0: Hub is not part of a compound device.
//1: Hub is part of a compound device. //--------------------------------------------------------------------+
//D4...D3: Over-current Protection Mode
//00: Global Over-current Protection. The hub
//reports over-current as a summation of all
//portscurrent draw, without a breakdown of
//individual port over-current status.
//01: Individual Port Over-current Protection. The
//hub reports over-current on a per-port basis.
//Each port has an over-current status.
//1X: No Over-current Protection. This option is
//allowed only for bus-powered hubs that do not
//implement over-current protection.
// //
//D6...D5: TT Think TIme //--------------------------------------------------------------------+
//00: TT requires at most 8 FS bit times of inter
//transaction gap on a full-/low-speed
//downstream bus.
//01: TT requires at most 16 FS bit times.
//10: TT requires at most 24 FS bit times.
//11: TT requires at most 32 FS bit times.
//D7: Port Indicators Supported
//0: Port Indicators are not supported on its
//downstream facing ports and the
//PORT_INDICATOR request has no effect.
//1: Port Indicators are supported on its
//downstream facing ports and the
//PORT_INDICATOR request controls the
//indicators. See Section 11.5.3.
//D15...D8: Reserved
typedef struct TU_ATTR_PACKED{
uint8_t bLength ; ///< Size of descriptor
uint8_t bDescriptorType ; ///< Other_speed_Configuration Type
uint8_t bNbrPorts;
uint16_t wHubCharacteristics;
uint8_t bPwrOn2PwrGood;
uint8_t bHubContrCurrent;
uint8_t DeviceRemovable; // bitmap each bit for a port (from bit1)
uint8_t PortPwrCtrlMask; // just for compatibility, should be 0xff
} descriptor_hub_desc_t;
TU_VERIFY_STATIC( sizeof(descriptor_hub_desc_t) == 9, "size is not correct");
enum { enum {
HUB_REQUEST_GET_STATUS = 0 , HUB_REQUEST_GET_STATUS = 0 ,
HUB_REQUEST_CLEAR_FEATURE = 1 , HUB_REQUEST_CLEAR_FEATURE = 1 ,
// 2 is reserved
HUB_REQUEST_SET_FEATURE = 3 , HUB_REQUEST_SET_FEATURE = 3 ,
// 4-5 are reserved
HUB_REQUEST_GET_DESCRIPTOR = 6 , HUB_REQUEST_GET_DESCRIPTOR = 6 ,
HUB_REQUEST_SET_DESCRIPTOR = 7 , HUB_REQUEST_SET_DESCRIPTOR = 7 ,
HUB_REQUEST_CLEAR_TT_BUFFER = 8 , HUB_REQUEST_CLEAR_TT_BUFFER = 8 ,
@@ -118,10 +69,10 @@ enum{
HUB_FEATURE_PORT_SUSPEND = 2, HUB_FEATURE_PORT_SUSPEND = 2,
HUB_FEATURE_PORT_OVER_CURRENT = 3, HUB_FEATURE_PORT_OVER_CURRENT = 3,
HUB_FEATURE_PORT_RESET = 4, HUB_FEATURE_PORT_RESET = 4,
// 5-7 are reserved
HUB_FEATURE_PORT_POWER = 8, HUB_FEATURE_PORT_POWER = 8,
HUB_FEATURE_PORT_LOW_SPEED = 9, HUB_FEATURE_PORT_LOW_SPEED = 9,
// 10-15 are reserved
HUB_FEATURE_PORT_CONNECTION_CHANGE = 16, HUB_FEATURE_PORT_CONNECTION_CHANGE = 16,
HUB_FEATURE_PORT_ENABLE_CHANGE = 17, HUB_FEATURE_PORT_ENABLE_CHANGE = 17,
HUB_FEATURE_PORT_SUSPEND_CHANGE = 18, HUB_FEATURE_PORT_SUSPEND_CHANGE = 18,
@@ -131,6 +82,41 @@ enum{
HUB_FEATURE_PORT_INDICATOR = 22 HUB_FEATURE_PORT_INDICATOR = 22
}; };
enum {
HUB_CHARS_POWER_GANGED_SWITCHING = 0,
HUB_CHARS_POWER_INDIVIDUAL_SWITCHING = 1,
};
enum {
HUB_CHARS_OVER_CURRENT_GLOBAL = 0,
HUB_CHARS_OVER_CURRENT_INDIVIDUAL = 1,
};
typedef struct TU_ATTR_PACKED{
uint8_t bLength ; ///< Size of descriptor
uint8_t bDescriptorType ; ///< Other_speed_Configuration Type
uint8_t bNbrPorts;
uint16_t wHubCharacteristics;
uint8_t bPwrOn2PwrGood;
uint8_t bHubContrCurrent;
uint8_t DeviceRemovable; // bitmap each bit for a port (from bit1)
uint8_t PortPwrCtrlMask; // just for compatibility, should be 0xff
} hub_desc_cs_t;
TU_VERIFY_STATIC(sizeof(hub_desc_cs_t) == 9, "size is not correct");
TU_VERIFY_STATIC(CFG_TUH_HUB_BUFSIZE >= sizeof(hub_desc_cs_t), "buffer is not big enough");
typedef struct TU_ATTR_PACKED {
struct TU_ATTR_PACKED {
uint8_t logical_power_switching_mode : 2; // [0..1] gannged or individual power switching
uint8_t compound_device : 1; // [2] hub is part of compound device
uint8_t over_current_protect_mode : 2; // [3..4] global or individual port over-current protection
uint8_t tt_think_time : 2; // [5..6] TT think time
uint8_t port_indicator_supported : 1; // [7] port indicator supported
};
uint8_t rsv1;
} hub_characteristics_t;
TU_VERIFY_STATIC(sizeof(hub_characteristics_t) == 2, "size is not correct");
// data in response of HUB_REQUEST_GET_STATUS, wIndex = 0 (hub) // data in response of HUB_REQUEST_GET_STATUS, wIndex = 0 (hub)
typedef struct { typedef struct {
union{ union{
@@ -143,39 +129,43 @@ typedef struct {
uint16_t value; uint16_t value;
} status, change; } status, change;
} hub_status_response_t; } hub_status_response_t;
TU_VERIFY_STATIC( sizeof(hub_status_response_t) == 4, "size is not correct"); TU_VERIFY_STATIC( sizeof(hub_status_response_t) == 4, "size is not correct");
// data in response of HUB_REQUEST_GET_STATUS, wIndex = Port num // data in response of HUB_REQUEST_GET_STATUS, wIndex = Port num
typedef struct { typedef struct {
union { union TU_ATTR_PACKED {
struct TU_ATTR_PACKED { struct TU_ATTR_PACKED {
uint16_t connection : 1; // Bit 0-4 are for change & status
uint16_t port_enable : 1; uint16_t connection : 1; // [0] 0 = no device, 1 = device connected
uint16_t suspend : 1; uint16_t port_enable : 1; // [1] port is enabled
uint16_t over_current : 1; uint16_t suspend : 1; // [2]
uint16_t reset : 1; uint16_t over_current : 1; // [3] over-current exists
uint16_t reset : 1; // [4] 0 = no reset, 1 = resetting
uint16_t : 3; // From Bit 5 are for status only
uint16_t port_power : 1; uint16_t rsv5_7 : 3; // [5..7] reserved
uint16_t low_speed : 1; uint16_t port_power : 1; // [8] 0 = port is off, 1 = port is on
uint16_t high_speed : 1; uint16_t low_speed : 1; // [9] low speed device attached
uint16_t port_test_mode : 1; uint16_t high_speed : 1; // [10] high speed device attached
uint16_t port_indicator_control : 1; uint16_t port_test_mode : 1; // [11] port in test mode
uint16_t TU_RESERVED : 3; uint16_t port_indicator_control : 1; // [12] 0: default color, 1: indicator is software controlled
uint16_t TU_RESERVED : 3; // [13..15] reserved
}; };
uint16_t value; uint16_t value;
} status, change; } status, change;
} hub_port_status_response_t; } hub_port_status_response_t;
TU_VERIFY_STATIC( sizeof(hub_port_status_response_t) == 4, "size is not correct"); TU_VERIFY_STATIC( sizeof(hub_port_status_response_t) == 4, "size is not correct");
// Clear feature //--------------------------------------------------------------------+
// HUB API
//--------------------------------------------------------------------+
// Clear port feature
bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature, bool hub_port_clear_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
tuh_xfer_cb_t complete_cb, uintptr_t user_data); tuh_xfer_cb_t complete_cb, uintptr_t user_data);
// Set feature // Set port feature
bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature, bool hub_port_set_feature(uint8_t hub_addr, uint8_t hub_port, uint8_t feature,
tuh_xfer_cb_t complete_cb, uintptr_t user_data); tuh_xfer_cb_t complete_cb, uintptr_t user_data);
@@ -184,7 +174,7 @@ bool hub_port_get_status (uint8_t hub_addr, uint8_t hub_port, void* resp,
tuh_xfer_cb_t complete_cb, uintptr_t user_data); tuh_xfer_cb_t complete_cb, uintptr_t user_data);
// Get status from Interrupt endpoint // Get status from Interrupt endpoint
bool hub_edpt_status_xfer(uint8_t dev_addr); bool hub_edpt_status_xfer(uint8_t daddr);
// Reset a port // Reset a port
TU_ATTR_ALWAYS_INLINE static inline TU_ATTR_ALWAYS_INLINE static inline
@@ -192,13 +182,23 @@ bool hub_port_reset(uint8_t hub_addr, uint8_t hub_port, tuh_xfer_cb_t complete_c
return hub_port_set_feature(hub_addr, hub_port, HUB_FEATURE_PORT_RESET, complete_cb, user_data); return hub_port_set_feature(hub_addr, hub_port, HUB_FEATURE_PORT_RESET, complete_cb, user_data);
} }
// Clear Reset Change // Clear Port Reset Change
TU_ATTR_ALWAYS_INLINE static inline TU_ATTR_ALWAYS_INLINE static inline
bool hub_port_clear_reset_change(uint8_t hub_addr, uint8_t hub_port, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { bool hub_port_clear_reset_change(uint8_t hub_addr, uint8_t hub_port, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
return hub_port_clear_feature(hub_addr, hub_port, HUB_FEATURE_PORT_RESET_CHANGE, complete_cb, user_data); return hub_port_clear_feature(hub_addr, hub_port, HUB_FEATURE_PORT_RESET_CHANGE, complete_cb, user_data);
} }
// Get Hub status
TU_ATTR_ALWAYS_INLINE static inline
bool hub_get_status(uint8_t hub_addr, void* resp, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
return hub_port_get_status(hub_addr, 0, resp, complete_cb, user_data);
}
// Clear Hub feature
TU_ATTR_ALWAYS_INLINE static inline
bool hub_clear_feature(uint8_t hub_addr, uint8_t feature, tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
return hub_port_clear_feature(hub_addr, 0, feature, complete_cb, user_data);
}
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Internal Class Driver API // Internal Class Driver API
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@@ -206,13 +206,11 @@ bool hub_init (void);
bool hub_deinit (void); bool hub_deinit (void);
bool hub_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len); bool hub_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len);
bool hub_set_config (uint8_t dev_addr, uint8_t itf_num); bool hub_set_config (uint8_t dev_addr, uint8_t itf_num);
bool hub_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); bool hub_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes);
void hub_close (uint8_t dev_addr); void hub_close (uint8_t dev_addr);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* _TUSB_HUB_H_ */ #endif
/** @} */

View File

@@ -44,6 +44,10 @@
#define CFG_TUH_INTERFACE_MAX 8 #define CFG_TUH_INTERFACE_MAX 8
#endif #endif
enum {
USBH_CONTROL_RETRY_MAX = 3,
};
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Weak stubs: invoked if no strong implementation is available // Weak stubs: invoked if no strong implementation is available
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@@ -272,9 +276,10 @@ static struct {
tuh_xfer_cb_t complete_cb; tuh_xfer_cb_t complete_cb;
uintptr_t user_data; uintptr_t user_data;
uint8_t daddr;
volatile uint8_t stage; volatile uint8_t stage;
uint8_t daddr;
volatile uint16_t actual_len; volatile uint16_t actual_len;
uint8_t failed_count;
} _ctrl_xfer; } _ctrl_xfer;
typedef struct { typedef struct {
@@ -285,7 +290,6 @@ typedef struct {
CFG_TUH_MEM_SECTION static usbh_epbuf_t _usbh_epbuf; CFG_TUH_MEM_SECTION static usbh_epbuf_t _usbh_epbuf;
//------------- Helper Function -------------// //------------- Helper Function -------------//
TU_ATTR_ALWAYS_INLINE static inline usbh_device_t* get_device(uint8_t dev_addr) { TU_ATTR_ALWAYS_INLINE static inline usbh_device_t* get_device(uint8_t dev_addr) {
TU_VERIFY(dev_addr > 0 && dev_addr <= TOTAL_DEVICES, NULL); TU_VERIFY(dev_addr > 0 && dev_addr <= TOTAL_DEVICES, NULL);
return &_usbh_devices[dev_addr-1]; return &_usbh_devices[dev_addr-1];
@@ -312,6 +316,15 @@ bool tuh_mounted(uint8_t dev_addr) {
return dev->configured; return dev->configured;
} }
bool tuh_connected(uint8_t daddr) {
if (daddr == 0) {
return _dev0.enumerating; // dev0 is connected if still enumerating
} else {
const usbh_device_t* dev = get_device(daddr);
return dev && dev->connected;
}
}
bool tuh_vid_pid_get(uint8_t dev_addr, uint16_t *vid, uint16_t *pid) { bool tuh_vid_pid_get(uint8_t dev_addr, uint16_t *vid, uint16_t *pid) {
*vid = *pid = 0; *vid = *pid = 0;
@@ -421,7 +434,9 @@ bool tuh_rhport_init(uint8_t rhport, const tusb_rhport_init_t* rh_init) {
} }
bool tuh_deinit(uint8_t rhport) { bool tuh_deinit(uint8_t rhport) {
if (!tuh_rhport_is_active(rhport)) return true; if (!tuh_rhport_is_active(rhport)) {
return true;
}
// deinit host controller // deinit host controller
hcd_int_disable(rhport); hcd_int_disable(rhport);
@@ -467,13 +482,11 @@ bool tuh_task_event_ready(void) {
* This should be called periodically within the mainloop or rtos thread. * This should be called periodically within the mainloop or rtos thread.
* *
@code @code
int main(void) int main(void) {
{
application_init(); application_init();
tusb_init(0, TUSB_ROLE_HOST); tusb_init(0, TUSB_ROLE_HOST);
while(1) // the mainloop while(1) { // the mainloop
{
application_code(); application_code();
tuh_task(); // tinyusb host task tuh_task(); // tinyusb host task
} }
@@ -484,7 +497,9 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
(void) in_isr; // not implemented yet (void) in_isr; // not implemented yet
// Skip if stack is not initialized // Skip if stack is not initialized
if (!tuh_inited()) return; if (!tuh_inited()) {
return;
}
// Loop until there is no more events in the queue // Loop until there is no more events in the queue
while (1) { while (1) {
@@ -618,17 +633,9 @@ static void _control_blocking_complete_cb(tuh_xfer_t* xfer) {
// TODO timeout_ms is not supported yet // TODO timeout_ms is not supported yet
bool tuh_control_xfer (tuh_xfer_t* xfer) { bool tuh_control_xfer (tuh_xfer_t* xfer) {
// EP0 with setup packet TU_VERIFY(xfer->ep_addr == 0 && xfer->setup); // EP0 with setup packet
TU_VERIFY(xfer->ep_addr == 0 && xfer->setup);
// Check if device is still connected (enumerating for dev0)
const uint8_t daddr = xfer->daddr; const uint8_t daddr = xfer->daddr;
if (daddr == 0) { TU_VERIFY(tuh_connected(daddr)); // Check if device is still connected (enumerating for dev0)
TU_VERIFY(_dev0.enumerating);
} else {
const usbh_device_t* dev = get_device(daddr);
TU_VERIFY(dev && dev->connected);
}
// pre-check to help reducing mutex lock // pre-check to help reducing mutex lock
TU_VERIFY(_ctrl_xfer.stage == CONTROL_STAGE_IDLE); TU_VERIFY(_ctrl_xfer.stage == CONTROL_STAGE_IDLE);
@@ -639,6 +646,7 @@ bool tuh_control_xfer (tuh_xfer_t* xfer) {
_ctrl_xfer.stage = CONTROL_STAGE_SETUP; _ctrl_xfer.stage = CONTROL_STAGE_SETUP;
_ctrl_xfer.daddr = daddr; _ctrl_xfer.daddr = daddr;
_ctrl_xfer.actual_len = 0; _ctrl_xfer.actual_len = 0;
_ctrl_xfer.failed_count = 0;
_ctrl_xfer.buffer = xfer->buffer; _ctrl_xfer.buffer = xfer->buffer;
_ctrl_xfer.complete_cb = xfer->complete_cb; _ctrl_xfer.complete_cb = xfer->complete_cb;
@@ -724,13 +732,31 @@ static bool usbh_control_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t
const uint8_t rhport = usbh_get_rhport(daddr); const uint8_t rhport = usbh_get_rhport(daddr);
tusb_control_request_t const * request = &_usbh_epbuf.request; tusb_control_request_t const * request = &_usbh_epbuf.request;
if (XFER_RESULT_SUCCESS != result) { switch (result) {
TU_LOG_USBH("[%u:%u] Control %s, xferred_bytes = %" PRIu32 "\r\n", rhport, daddr, result == XFER_RESULT_STALLED ? "STALLED" : "FAILED", xferred_bytes); case XFER_RESULT_STALLED:
TU_LOG_USBH("[%u:%u] Control STALLED, xferred_bytes = %" PRIu32 "\r\n", rhport, daddr, xferred_bytes);
TU_LOG_BUF_USBH(request, 8); TU_LOG_BUF_USBH(request, 8);
// terminate transfer if any stage failed
_control_xfer_complete(daddr, result); _control_xfer_complete(daddr, result);
break;
case XFER_RESULT_FAILED:
if (tuh_connected(daddr) && _ctrl_xfer.failed_count < USBH_CONTROL_RETRY_MAX) {
TU_LOG_USBH("[%u:%u] Control FAILED %u/%u, retrying\r\n", rhport, daddr, _ctrl_xfer.failed_count+1, USBH_CONTROL_RETRY_MAX);
(void) osal_mutex_lock(_usbh_mutex, OSAL_TIMEOUT_WAIT_FOREVER);
_ctrl_xfer.stage = CONTROL_STAGE_SETUP;
_ctrl_xfer.failed_count++;
_ctrl_xfer.actual_len = 0; // reset actual_len
(void) osal_mutex_unlock(_usbh_mutex);
TU_ASSERT(hcd_setup_send(rhport, daddr, (uint8_t const *) request));
} else { } else {
TU_LOG_USBH("[%u:%u] Control FAILED, xferred_bytes = %" PRIu32 "\r\n", rhport, daddr, xferred_bytes);
TU_LOG_BUF_USBH(request, 8);
_control_xfer_complete(daddr, result);
}
break;
case XFER_RESULT_SUCCESS:
switch(_ctrl_xfer.stage) { switch(_ctrl_xfer.stage) {
case CONTROL_STAGE_SETUP: case CONTROL_STAGE_SETUP:
if (request->wLength) { if (request->wLength) {
@@ -769,8 +795,11 @@ static bool usbh_control_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t
break; break;
} }
default: return false; default: return false; // unsupported stage
} }
break;
default: return false; // unsupported result
} }
return true; return true;
@@ -891,7 +920,6 @@ bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr) {
} }
// Submit an transfer // Submit an transfer
// TODO call usbh_edpt_release if failed
bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes, bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes,
tuh_xfer_cb_t complete_cb, uintptr_t user_data) { tuh_xfer_cb_t complete_cb, uintptr_t user_data) {
(void) complete_cb; (void) complete_cb;
@@ -1657,7 +1685,7 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
if ( 0 == tu_desc_len(p_desc) ) { if ( 0 == tu_desc_len(p_desc) ) {
// A zero length descriptor indicates that the device is off spec (e.g. wrong wTotalLength). // A zero length descriptor indicates that the device is off spec (e.g. wrong wTotalLength).
// Parsed interfaces should still be usable // Parsed interfaces should still be usable
TU_LOG_USBH("Encountered a zero-length descriptor after %u bytes\r\n", (uint32_t)p_desc - (uint32_t)desc_cfg); TU_LOG_USBH("Encountered a zero-length descriptor after %" PRIu32 "bytes\r\n", (uint32_t)p_desc - (uint32_t)desc_cfg);
break; break;
} }
@@ -1771,8 +1799,9 @@ static void enum_full_complete(void) {
_dev0.enumerating = 0; _dev0.enumerating = 0;
#if CFG_TUH_HUB #if CFG_TUH_HUB
// get next hub status if (_dev0.hub_addr) {
if (_dev0.hub_addr) hub_edpt_status_xfer(_dev0.hub_addr); hub_edpt_status_xfer(_dev0.hub_addr); // get next hub status
}
#endif #endif
} }

View File

@@ -183,6 +183,10 @@ tusb_speed_t tuh_speed_get(uint8_t daddr);
// Check if device is connected and configured // Check if device is connected and configured
bool tuh_mounted(uint8_t daddr); bool tuh_mounted(uint8_t daddr);
// Check if device is connected which mean device has at least 1 successful transfer
// Note: It may not be addressed/configured/mounted yet
bool tuh_connected(uint8_t daddr);
// Check if device is suspended // Check if device is suspended
TU_ATTR_ALWAYS_INLINE static inline TU_ATTR_ALWAYS_INLINE static inline
bool tuh_suspended(uint8_t daddr) { bool tuh_suspended(uint8_t daddr) {

View File

@@ -83,8 +83,8 @@ void usbh_defer_func(osal_task_func_t func, void *param, bool in_isr);
bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes, bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes,
tuh_xfer_cb_t complete_cb, uintptr_t user_data); tuh_xfer_cb_t complete_cb, uintptr_t user_data);
TU_ATTR_ALWAYS_INLINE TU_ATTR_ALWAYS_INLINE static inline
static inline bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) { bool usbh_edpt_xfer(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) {
return usbh_edpt_xfer_with_callback(dev_addr, ep_addr, buffer, total_bytes, NULL, 0); return usbh_edpt_xfer_with_callback(dev_addr, ep_addr, buffer, total_bytes, NULL, 0);
} }

View File

@@ -58,8 +58,8 @@ deps_optional = {
'hw/mcu/nxp/mcux-sdk': ['https://github.com/hathach/mcux-sdk.git', 'hw/mcu/nxp/mcux-sdk': ['https://github.com/hathach/mcux-sdk.git',
'144f1eb7ea8c06512e12f12b27383601c0272410', '144f1eb7ea8c06512e12f12b27383601c0272410',
'kinetis_k kinetis_k32l2 kinetis_kl lpc51 lpc54 lpc55 mcx imxrt'], 'kinetis_k kinetis_k32l2 kinetis_kl lpc51 lpc54 lpc55 mcx imxrt'],
'hw/mcu/raspberry_pi/Pico-PIO-USB': ['https://github.com/sekigon-gonnoc/Pico-PIO-USB.git', 'hw/mcu/raspberry_pi/Pico-PIO-USB': ['https://github.com/hathach/Pico-PIO-USB.git',
'fe9133fc513b82cc3dc62c67cb51f2339cf29ef7', '9c8df3083b62c0a678f3bd3d8a7e773932622d4b',
'rp2040'], 'rp2040'],
'hw/mcu/renesas/fsp': ['https://github.com/renesas/fsp.git', 'hw/mcu/renesas/fsp': ['https://github.com/renesas/fsp.git',
'edcc97d684b6f716728a60d7a6fea049d9870bd6', 'edcc97d684b6f716728a60d7a6fea049d9870bd6',
@@ -217,7 +217,12 @@ TOP = Path(__file__).parent.parent.resolve()
def run_cmd(cmd): def run_cmd(cmd):
return subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) r = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
title = f'Command Error: {cmd}'
if r.returncode != 0:
print(title)
print(r.stdout.decode("utf-8"))
return r
def get_a_dep(d): def get_a_dep(d):