diff options
| author | Andrew Leech <andrew.leech@planetinnovation.com.au> | 2025-10-23 09:48:00 +1100 | 
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2025-10-31 10:54:33 +1100 | 
| commit | 27b7bf36a33f7c51a976c41b8682d7acfb4201cb (patch) | |
| tree | 71c76ae17f6d364734a5c0ac6bb1afff0770b301 | |
| parent | faae8e73ef654339bd2adfab2b658e6e7f5ef368 (diff) | |
stm32/usb: Add VBUS sensing configuration for TinyUSB on F4/F7.HEADorigin/masterorigin/HEADmaster
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)
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
| -rw-r--r-- | ports/stm32/usbd_conf.c | 16 | 
1 files changed, 16 insertions, 0 deletions
| diff --git a/ports/stm32/usbd_conf.c b/ports/stm32/usbd_conf.c index 18f350d4a..76142789d 100644 --- a/ports/stm32/usbd_conf.c +++ b/ports/stm32/usbd_conf.c @@ -177,6 +177,22 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)          HAL_NVIC_EnableIRQ(OTG_FS_IRQn);          #endif +        #if MICROPY_HW_TINYUSB_STACK +        // Configure VBUS sensing for TinyUSB on STM32F4/F7 which have separate GCCFG register +        // The DWC2 PHY init only sets PWRDWN bit, but doesn't configure VBUS sensing +        #if defined(STM32F4) || defined(STM32F7) +        #if defined(MICROPY_HW_USB_VBUS_DETECT_PIN) +        // Enable VBUS sensing in "B device" mode (using PA9) +        USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS; +        USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBUSBSEN; +        #else +        // Force VBUS valid (no VBUS detect pin configured) +        USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS; +        USB_OTG_FS->GCCFG &= ~(USB_OTG_GCCFG_VBUSBSEN | USB_OTG_GCCFG_VBUSASEN); +        #endif +        #endif +        #endif +          #if MICROPY_HW_STM_USB_STACK          return;          #endif | 
