Skip to content

Commit 403634c

Browse files
committed
ports/stm32: Add VBUS sensing configuration for TinyUSB on F4/F7.
TinyUSB's dwc2_phy_init() only sets the PWRDWN bit in the GCCFG register but doesn't configure VBUS sensing, which is required for the DWC2 USB controller to detect host connection on STM32F4 and STM32F7 series. Without VBUS sensing configured, USB devices fail to enumerate on these platforms when using the TinyUSB stack, while the legacy STM32 USB stack works because it includes this configuration. This commit adds VBUS sensing configuration in pyb_usbd_init() for TinyUSB mode on STM32F4/F7: - When VBUS detect pin is configured (typically PA9): Enable "B device" VBUS sensing (USB_OTG_GCCFG_VBUSBSEN) - When no VBUS pin: Force VBUS valid (USB_OTG_GCCFG_NOVBUSSENS) Tested on: - NUCLEO-F429ZI: USB now enumerates successfully (CDC + MSC) - NUCLEO-H563ZI: No regression (STM32H5 uses different register layout) Fixes USB enumeration failure on STM32F4/F7 with TinyUSB stack. Signed-off-by: Andrew Leech <[email protected]>
1 parent dba1a1a commit 403634c

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

ports/stm32/usbd_conf.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,22 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
177177
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
178178
#endif
179179

180+
#if MICROPY_HW_TINYUSB_STACK
181+
// Configure VBUS sensing for TinyUSB on STM32F4/F7 which have separate GCCFG register
182+
// The DWC2 PHY init only sets PWRDWN bit, but doesn't configure VBUS sensing
183+
#if defined(STM32F4) || defined(STM32F7)
184+
#if defined(MICROPY_HW_USB_VBUS_DETECT_PIN)
185+
// Enable VBUS sensing in "B device" mode (using PA9)
186+
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS;
187+
USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBUSBSEN;
188+
#else
189+
// Force VBUS valid (no VBUS detect pin configured)
190+
USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
191+
USB_OTG_FS->GCCFG &= ~(USB_OTG_GCCFG_VBUSBSEN | USB_OTG_GCCFG_VBUSASEN);
192+
#endif
193+
#endif
194+
#endif
195+
180196
#if !MICROPY_HW_TINYUSB_STACK
181197
return;
182198
#endif

0 commit comments

Comments
 (0)