Skip to content

Commit 8c4b142

Browse files
authored
Merge pull request hathach#1498 from hathach/add-tuh_config-port-specific-setup
add tuh_configure() for port/dynamic host behavior config
2 parents 896c707 + 8cbc34d commit 8c4b142

File tree

6 files changed

+67
-23
lines changed

6 files changed

+67
-23
lines changed

hw/bsp/rp2040/family.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
#include "bsp/board.h"
3636
#include "board.h"
3737

38+
#if CFG_TUH_RPI_PIO_USB || CFG_TUD_RPI_PIO_USB
39+
#include "pio_usb.h"
40+
#endif
41+
42+
// PIO_USB_DP_PIN_DEFAULT is 0, which conflict with UART, change to 2
43+
#define PICO_PIO_USB_PIN_DP 2
44+
3845
#ifdef BUTTON_BOOTSEL
3946
// This example blinks the Picoboard LED when the BOOTSEL button is pressed.
4047
//
@@ -130,6 +137,12 @@ void board_init(void)
130137
#if CFG_TUH_RPI_PIO_USB || CFG_TUD_RPI_PIO_USB
131138
// Set the system clock to a multiple of 120mhz for bitbanging USB with pico-usb
132139
set_sys_clock_khz(120000, true);
140+
141+
// rp2040 use pico-pio-usb for host tuh_configure() can be used to passed pio configuration to the host stack
142+
// Note: tuh_configure() must be called before tuh_init()
143+
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
144+
pio_cfg.pin_dp = PICO_PIO_USB_PIN_DP;
145+
tuh_configure(BOARD_TUH_RHPORT, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_cfg);
133146
#endif
134147

135148
#if defined(UART_DEV) && defined(LIB_PICO_STDIO_UART)
@@ -142,9 +155,8 @@ void board_init(void)
142155
stdio_rtt_init();
143156
#endif
144157

145-
// todo probably set up device mode?
146158
#if CFG_TUD_ENABLED
147-
159+
// TODO probably set up device mode?
148160
#endif
149161

150162
#if CFG_TUH_ENABLED

src/device/usbd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ bool tud_init (uint8_t rhport)
392392
// skip if already initialized
393393
if ( tud_inited() ) return true;
394394

395-
TU_LOG2("USBD init\r\n");
395+
TU_LOG2("USBD init rhport %u\r\n", rhport);
396396
TU_LOG2_INT(sizeof(usbd_device_t));
397397

398398
tu_varclr(&_usbd_dev);

src/host/hcd.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
// #endif
4949
#endif
5050

51-
//--------------------------------------------------------------------+
51+
//--------------------------------------------------------------------+
5252
// MACRO CONSTANT TYPEDEF
5353
//--------------------------------------------------------------------+
5454
typedef enum
@@ -106,6 +106,9 @@ typedef struct
106106
// Controller API
107107
//--------------------------------------------------------------------+
108108

109+
// optional hcd configuration, called by tuh_config()
110+
bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param) TU_ATTR_WEAK;
111+
109112
// Initialize controller to host mode
110113
bool hcd_init(uint8_t rhport);
111114

src/host/usbh.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,32 @@ static void process_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t h
274274
static bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size);
275275
static bool usbh_control_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes);
276276

277+
#if CFG_TUSB_OS == OPT_OS_NONE
278+
// TODO rework time-related function later
279+
void osal_task_delay(uint32_t msec)
280+
{
281+
(void) msec;
282+
283+
const uint32_t start = hcd_frame_number(TUH_OPT_RHPORT);
284+
while ( ( hcd_frame_number(TUH_OPT_RHPORT) - start ) < msec ) {}
285+
}
286+
#endif
287+
277288
//--------------------------------------------------------------------+
278289
// PUBLIC API (Parameter Verification is required)
279290
//--------------------------------------------------------------------+
291+
292+
bool tuh_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param)
293+
{
294+
if (hcd_configure)
295+
{
296+
return hcd_configure(rhport, cfg_id, cfg_param);
297+
}else
298+
{
299+
return false;
300+
}
301+
}
302+
280303
bool tuh_mounted(uint8_t dev_addr)
281304
{
282305
usbh_device_t* dev = get_device(dev_addr);
@@ -303,20 +326,6 @@ tusb_speed_t tuh_speed_get (uint8_t dev_addr)
303326
return (tusb_speed_t) (dev ? get_device(dev_addr)->speed : _dev0.speed);
304327
}
305328

306-
#if CFG_TUSB_OS == OPT_OS_NONE
307-
void osal_task_delay(uint32_t msec)
308-
{
309-
(void) msec;
310-
311-
const uint32_t start = hcd_frame_number(TUH_OPT_RHPORT);
312-
while ( ( hcd_frame_number(TUH_OPT_RHPORT) - start ) < msec ) {}
313-
}
314-
#endif
315-
316-
//--------------------------------------------------------------------+
317-
// CLASS-USBD API (don't require to verify parameters)
318-
//--------------------------------------------------------------------+
319-
320329
static void clear_device(usbh_device_t* dev)
321330
{
322331
tu_memclr(dev, sizeof(usbh_device_t));
@@ -334,7 +343,7 @@ bool tuh_init(uint8_t rhport)
334343
// skip if already initialized
335344
if (_usbh_initialized) return _usbh_initialized;
336345

337-
TU_LOG2("USBH init\r\n");
346+
TU_LOG2("USBH init rhport %u\r\n", rhport);
338347
TU_LOG2_INT(sizeof(usbh_device_t));
339348
TU_LOG2_INT(sizeof(hcd_event_t));
340349
TU_LOG2_INT(sizeof(_ctrl_xfer));

src/host/usbh.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ typedef void (*tuh_xfer_cb_t)(tuh_xfer_t* xfer);
4646

4747
// Note1: layout and order of this will be changed in near future
4848
// it is advised to initialize it using member name
49-
// Note2: not all field is available/meaningful in callback, some info is not saved by
50-
// usbh to save SRAM
49+
// Note2: not all field is available/meaningful in callback,
50+
// some info is not saved by usbh to save SRAM
5151
struct tuh_xfer_s
5252
{
5353
uint8_t daddr;
@@ -69,6 +69,12 @@ struct tuh_xfer_s
6969
// uint32_t timeout_ms; // place holder, not supported yet
7070
};
7171

72+
// ConfigID for tuh_config()
73+
enum
74+
{
75+
TUH_CFGID_RPI_PIO_USB_CONFIGURATION = OPT_MCU_RP2040 // cfg_param: pio_usb_configuration_t
76+
};
77+
7278
//--------------------------------------------------------------------+
7379
// APPLICATION CALLBACK
7480
//--------------------------------------------------------------------+
@@ -85,6 +91,12 @@ TU_ATTR_WEAK void tuh_umount_cb(uint8_t daddr);
8591
// APPLICATION API
8692
//--------------------------------------------------------------------+
8793

94+
// Configure host stack behavior with dynamic or port-specific parameters.
95+
// Should be called before tuh_init()
96+
// - cfg_id : configure ID (TBD)
97+
// - cfg_param: configure data, structure depends on the ID
98+
bool tuh_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param);
99+
88100
// Init host stack
89101
bool tuh_init(uint8_t rhport);
90102

src/portable/raspberrypi/pio_usb/hcd_pio_usb.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,25 @@
4343
#define RHPORT_OFFSET 1
4444
#define RHPORT_PIO(_x) ((_x)-RHPORT_OFFSET)
4545

46-
static pio_usb_configuration_t pio_host_config = PIO_USB_DEFAULT_CONFIG;
46+
static pio_usb_configuration_t pio_host_cfg = PIO_USB_DEFAULT_CONFIG;
4747

4848
//--------------------------------------------------------------------+
4949
// HCD API
5050
//--------------------------------------------------------------------+
51+
bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param)
52+
{
53+
(void) rhport;
54+
TU_VERIFY(cfg_id == TUH_CFGID_RPI_PIO_USB_CONFIGURATION);
55+
memcpy(&pio_host_cfg, cfg_param, sizeof(pio_usb_configuration_t));
56+
return true;
57+
}
58+
5159
bool hcd_init(uint8_t rhport)
5260
{
5361
(void) rhport;
5462

5563
// To run USB SOF interrupt in core1, call this init in core1
56-
pio_usb_host_init(&pio_host_config);
64+
pio_usb_host_init(&pio_host_cfg);
5765

5866
return true;
5967
}

0 commit comments

Comments
 (0)