Skip to content

Commit 8f6e841

Browse files
committed
ports/rp2: Configure Waveshare RP2350-USB-A for PIO USB host.
Reconfigure for dual-mode USB: native USB controller in device mode (USB-C, REPL) on rhport 0, PIO USB in host mode (USB-A receptacle, GPIO12/13) on rhport 1. Set system clock to 120MHz (multiple of 12MHz required by PIO USB). RP2350 default 150MHz doesn't divide evenly. The original board config assumed native dual-controller host mode which doesn't work as the RP2350 has only one USB controller. PIO USB provides the second port via PIO state machines. Tested on Waveshare RP2350-USB-A with ESP32-S3 native USB CDC device connected to the USB-A receptacle. Host enumeration, CDC read/write all verified. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent 3a996f5 commit 8f6e841

3 files changed

Lines changed: 48 additions & 16 deletions

File tree

ports/rp2/boards/WAVESHARE_RP2350_USB_A/mpconfigboard.cmake

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,15 @@ set(MICROPY_PY_BTREE 1)
66
# Set the board variant for the Pico SDK
77
set(PICO_BOARD "waveshare_rp2350_one")
88

9-
# Enable USB Host support
10-
set(MICROPY_HW_USB_HOST 1)
9+
# Enable USB Host support via PIO USB
10+
set(MICROPY_HW_USB_HOST 1)
11+
12+
# PIO USB requires sys clock to be a multiple of 12MHz.
13+
# RP2350 default is 150MHz which doesn't divide evenly.
14+
# 120MHz = 1440MHz VCO / (6 * 2)
15+
add_compile_definitions(
16+
SYS_CLK_KHZ=120000
17+
PLL_SYS_VCO_FREQ_HZ=1440000000
18+
PLL_SYS_POSTDIV1=6
19+
PLL_SYS_POSTDIV2=2
20+
)

ports/rp2/boards/WAVESHARE_RP2350_USB_A/mpconfigboard.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,30 @@
44
#define MICROPY_HW_BOARD_NAME "Waveshare RP2350-USB-A"
55
#define MICROPY_HW_FLASH_STORAGE_BYTES (PICO_FLASH_SIZE_BYTES - 1024 * 1024)
66

7-
// Enable USB Host functionality
7+
// USB Host via PIO USB on USB-A connector (GPIO12=D+, GPIO13=D-)
8+
// Native USB controller remains in device mode for CDC REPL on USB-C
89
#define MICROPY_HW_USB_HOST (1)
910

11+
// PIO USB D+ pin (D- is implicitly D+ + 1 = GPIO13)
12+
#define MICROPY_HW_USB_HOST_DP_PIN (12)
13+
14+
// Dual-mode USB: native device on rhport 0, PIO USB host on rhport 1
15+
#define BOARD_TUD_RHPORT 0
16+
#define BOARD_TUH_RHPORT 1
17+
#define CFG_TUH_RPI_PIO_USB 1
18+
19+
// Tell TinyUSB which rhport is device vs host mode.
20+
// Using raw values (0x0001=DEVICE, 0x0002=HOST, 0x0200=FULL_SPEED) because
21+
// OPT_MODE_* may not be defined when mpconfigboard.h is included outside
22+
// TinyUSB context. tusb_option.h checks these before selecting HCD/DCD.
23+
#define CFG_TUSB_RHPORT0_MODE (0x0201) // OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED
24+
#define CFG_TUSB_RHPORT1_MODE (0x0202) // OPT_MODE_HOST | OPT_MODE_FULL_SPEED
25+
26+
// PIO USB requires sys clock to be a multiple of 12MHz.
27+
// RP2350 default is 150MHz which doesn't divide evenly.
28+
// SYS_CLK_KHZ=120000 is set in mpconfigboard.cmake so pico-sdk
29+
// sees it early enough during clock init.
30+
1031
// UART config
1132
#define MICROPY_HW_UART0_TX (0)
1233
#define MICROPY_HW_UART0_RX (1)
@@ -32,6 +53,8 @@
3253

3354
#define MICROPY_HW_SPI1_SCK (10)
3455
#define MICROPY_HW_SPI1_MOSI (11)
56+
// Note: GPIO12 is shared with PIO USB host D+ pin. Do not use SPI1 MISO
57+
// when USB host is active.
3558
#define MICROPY_HW_SPI1_MISO (12)
3659

3760
// LEDs

ports/rp2/boards/WAVESHARE_RP2350_USB_A/tusb_config.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,18 @@
2525
#ifndef MICROPY_INCLUDED_RP2_TUSB_CONFIG_H
2626
#define MICROPY_INCLUDED_RP2_TUSB_CONFIG_H
2727

28-
#include "py/mpconfig.h"
29-
#include "mpconfigport.h"
30-
31-
// Use the default TinyUSB config
32-
#include "../../tusb_config.h"
33-
34-
// Use native USB controllers for both host and device mode on RP2350
35-
#if CFG_TUH_ENABLED
36-
#define CFG_TUH_RPI_PIO_USB 0 // Disable PIO USB, use native controllers
37-
#define BOARD_TUH_RHPORT 1 // Use USB1 for host mode
38-
39-
// Use native USB for device mode
40-
#define BOARD_TUD_RHPORT 0 // Use USB0 for device mode
28+
// Dual-mode USB: native device (USB-C) + PIO USB host (USB-A)
29+
// These are defined in mpconfigboard.h for early visibility but
30+
// repeated here with guards for TinyUSB source files that include
31+
// tusb_config.h without going through mpconfigboard.h first.
32+
#ifndef BOARD_TUD_RHPORT
33+
#define BOARD_TUD_RHPORT 0
34+
#endif
35+
#ifndef BOARD_TUH_RHPORT
36+
#define BOARD_TUH_RHPORT 1
37+
#endif
38+
#ifndef CFG_TUH_RPI_PIO_USB
39+
#define CFG_TUH_RPI_PIO_USB 1
4140
#endif
4241

4342
#endif // MICROPY_INCLUDED_RP2_TUSB_CONFIG_H

0 commit comments

Comments
 (0)