Skip to content

Commit 28f8b0d

Browse files
committed
shared/tinyusb: Fix RHPORT mode selection for High-Speed USB.
The MICROPY_HW_TINYUSB_RHPORTx_MODE macros from mpconfigboard_common.h are not available when tusb_config.h is compiled, because TinyUSB's build system includes tusb_config.h before MicroPython's full configuration chain (mpconfigport.h). This caused CFG_TUSB_RHPORT0_MODE and CFG_TUSB_RHPORT1_MODE to be set incorrectly for boards using High-Speed USB with external ULPI PHY, resulting in USB descriptors with Full-Speed packet sizes (64 bytes) instead of High-Speed packet sizes (512 bytes). Replicate the RHPORT selection logic directly in tusb_config.h using board configuration macros that ARE available at this compilation stage (MICROPY_HW_USB_MAIN_DEV, MICROPY_HW_USB_HS_ULPI, STM32 family defines). This ensures proper RHPORT configuration for all STM32 USB configurations: - Full-Speed only: RHPORT0 enabled, RHPORT1 disabled - High-Speed with ULPI: RHPORT0 disabled, RHPORT1 High-Speed mode - High-Speed internal PHY: RHPORT0 disabled, RHPORT1 Full-Speed mode Tested on STM32H747I-DISCO with USB3320 ULPI PHY. Signed-off-by: Andrew Leech <[email protected]>
1 parent eabe052 commit 28f8b0d

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

shared/tinyusb/tusb_config.h

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,46 @@
5959
#define MICROPY_HW_USB_MSC_INQUIRY_REVISION_STRING "1.00"
6060
#endif
6161

62-
// #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_NONE)
63-
// #define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE)
62+
// Configure RHPORT modes based on board USB configuration
63+
// Note: MICROPY_HW_TINYUSB_RHPORTx_MODE from mpconfigboard_common.h is NOT available here
64+
// because tusb_config.h is included by TinyUSB's build before mpconfigport.h.
65+
// So we replicate the RHPORT selection logic using the board USB macros that ARE available.
6466

6567
#ifndef CFG_TUSB_RHPORT0_MODE
66-
#ifdef MICROPY_HW_TINYUSB_RHPORT0_MODE
67-
#define CFG_TUSB_RHPORT0_MODE MICROPY_HW_TINYUSB_RHPORT0_MODE
68+
#if defined(MICROPY_HW_USB_MAIN_DEV) && (MICROPY_HW_USB_MAIN_DEV == USB_PHY_HS_ID)
69+
// High-Speed controller selected - disable RHPORT0 (FS controller)
70+
#if defined(STM32F4) || defined(STM32F7) || defined(STM32H7)
71+
// These families have separate HS and FS controllers
72+
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_NONE)
73+
#else
74+
// Other families: HS PHY on same controller as FS
75+
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED)
76+
#endif
6877
#else
69-
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE)
78+
// Full-Speed controller selected (or auto-detect) - enable RHPORT0
79+
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED)
7080
#endif
7181
#endif
7282

7383
#ifndef CFG_TUSB_RHPORT1_MODE
74-
#ifdef MICROPY_HW_TINYUSB_RHPORT1_MODE
75-
#define CFG_TUSB_RHPORT1_MODE MICROPY_HW_TINYUSB_RHPORT1_MODE
84+
#if defined(MICROPY_HW_USB_MAIN_DEV) && (MICROPY_HW_USB_MAIN_DEV == USB_PHY_HS_ID)
85+
// High-Speed controller selected - configure RHPORT1 mode
86+
#if defined(STM32F4) || defined(STM32F7) || defined(STM32H7)
87+
// These families have separate HS and FS controllers
88+
#if defined(MICROPY_HW_USB_HS_ULPI)
89+
// External ULPI PHY - use High-Speed
90+
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | OPT_MODE_HIGH_SPEED)
91+
#else
92+
// Internal PHY or HS-in-FS mode - use Full-Speed
93+
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED)
94+
#endif
95+
#else
96+
// Other families: HS PHY on same controller as FS - disable RHPORT1
97+
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_NONE)
98+
#endif
7699
#else
77-
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_NONE)
100+
// Full-Speed controller selected (or auto-detect) - disable RHPORT1
101+
#define CFG_TUSB_RHPORT1_MODE (OPT_MODE_NONE)
78102
#endif
79103
#endif
80104

0 commit comments

Comments
 (0)