Skip to content

Conversation

@HiFiPhile
Copy link
Collaborator

@HiFiPhile HiFiPhile commented Jan 30, 2026

Describe the PR

  • Add VBus sensing option to tud_configure(), for STM32 GCCFG register config is now included in the stack, which reduces BSP compicity.
  • Also turn off device mode Vbus output in F4/F7's BSP to fix disconnection detection.
  • Add Vbus detection support to Nucleo-U5A5AZJ which uses ADC to read Vbus voltage.
  • Also fix N6 BSP to make both ports work

Other device families should not be infected as GOTGCFG default value stays the same.

Device and host modes are tested and functional:

Board Type Disconnection detection
F407-DISCO Internal FS OK
F723E-DISCO Internal FS / Internal ULPI HS Both ports OK 1
H747I-DISCO External ULPI HS OK
Nucleo-H7S3L8 Internal UTMI HS NO Vbus connection
Nucleo-U5A5AZJ Internal UTMI HS OK
N6570DK Internal UTMI HS NO Vbus connection

1: Needs 104cf33

Should fix #1476, #3426

Additional context
No more callback is needed for #3378

Copilot AI review requested due to automatic review settings January 30, 2026 20:30
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the DWC2 STM32 integration to support configurable VBUS sensing via tud_configure(), centralizes STM32 GCCFG/VBUS handling inside the DWC2 port, and wires board-level configurations (including ADC-based sensing) to fix missing DCD_EVENT_UNPLUGGED events on STM32 DWC2 targets.

Changes:

  • Extend the DWC2 device configuration (tud_configure_dwc2_t) with a vbus_sensing flag and propagate it into the DWC2 device/host drivers, including a new STM32-specific dwc2_stm32_gccfg_cfg() to manage GCCFG/VBUS bits per MCU family and role.
  • In the DWC2 device driver, integrate OTG SEDET-based unplug detection (GOTGINT_SEDET) and conditionally control GOTGCTL overrides based on vbus_sensing so real VBUS sensing can drive DCD_EVENT_UNPLUGGED.
  • Update STM32 F2/F4/F7/H7/H7RS/L4/U5/WBA/N6 BSPs to configure DWC2 via tud_configure() instead of manual register pokes, adjust board-level VBUS sense enable macros, and add ADC+TIM-based VBUS sensing for U5A5/U575 boards.

Reviewed changes

Copilot reviewed 43 out of 43 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/portable/synopsys/dwc2/hcd_dwc2.c Calls dwc2_stm32_gccfg_cfg() for STM32 in host mode to centralize GCCFG/VBUS setup and keep BSPs simpler.
src/portable/synopsys/dwc2/dwc2_type.h Adds STM32 GCCFG bit definitions for VBUS sensing/override across F2/F4 and U5/femtoPHY families used by the new config helper.
src/portable/synopsys/dwc2/dwc2_stm32.h Introduces dwc2_stm32_gccfg_cfg() to configure STM32 GCCFG/VBUS behavior per family, role (host/device), and vbus_sensing flag.
src/portable/synopsys/dwc2/dcd_dwc2.c Extends DWC2 device config with vbus_sensing, adjusts GOTGCTL overrides accordingly, configures STM32 GCCFG via the new helper, and enables OTG SEDET-based unplug interrupt handling.
src/device/usbd.h Defines tud_configure_dwc2_t.vbus_sensing and the TUD_CFGID_DWC2/tud_configure_param_t plumbing so applications/BSPs can pass per-port VBUS-sense config.
hw/bsp/stm32wba/family.c Leaves GCCFG/VBUS configuration to the DWC2 STM32 helper while keeping HS PHY and power-up sequencing for WBA.
hw/bsp/stm32u5/stm32u5xx_hal_conf.h Enables HAL ADC and TIM modules needed by the ADC-triggered VBUS sensing implementation on U5 boards.
hw/bsp/stm32u5/family.mk Adds U5 HAL ADC/ADC_ex/TIM sources and includes the FSDEV HCD so host-mode can build on U5 FS devices.
hw/bsp/stm32u5/family.cmake Mirrors the Makefile updates in CMake for HAL ADC/ADC_ex/TIM sources on STM32U5.
hw/bsp/stm32u5/family.c Replaces manual GCCFG/GOTGCTL VBUS configuration with tud_configure() for FS/HS ports and invokes board_vbus_sense_init() for boards that implement custom (e.g., ADC-based) VBUS sensing.
hw/bsp/stm32u5/boards/stm32u5a5nucleo/board.h Adds VBUS_SENSE_EN, pulls in TIM LL header, and implements ADC+TIM-based VBUS monitoring that drives HS VBUS override bits; also defines board_vbus_sense_init() and ADC IRQ handler.
hw/bsp/stm32u5/boards/stm32u575nucleo/board.h Similar ADC+TIM-based VBUS sensing for the FS-only U575 Nucleo, with VBUS_SENSE_EN and a board_vbus_sense_init() plus ADC IRQ handler driving FS VBUS override (contains a HS/FS instance mismatch that needs fixing).
hw/bsp/stm32u5/boards/stm32u575eval/board.h Defines VBUS_SENSE_EN and an empty board_vbus_sense_init() stub so family.c can call it uniformly.
hw/bsp/stm32u5/boards/stm32u545nucleo/board.h Adds an empty board_vbus_sense_init() hook for future custom VBUS sensing on this board.
hw/bsp/stm32u5/boards/b_u585i_iot2a/board.h Adds VBUS_SENSE_EN and an empty board_vbus_sense_init() for this U585 board to participate in the new configuration flow.
hw/bsp/stm32n6/boards/stm32n657nucleo/board.mk Updates J-Link target name to stm32n657x0, aligning debug configuration with the specific N657 variant.
hw/bsp/stm32n6/boards/stm32n657nucleo/board.cmake Same J-Link device adjustment in CMake for the N657 Nucleo board.
hw/bsp/stm32n6/boards/stm32n6570dk/board.cmake Updates J-Link device name for the N6570 DK board to the more specific stm32n657x0.
hw/bsp/stm32l4/family.c For OTG_FS-based L4 boards, replaces direct VBUS-sense register programming with a tud_configure() call that passes board-defined VBUS_SENSE_EN.
hw/bsp/stm32l4/boards/stm32l4r5nucleo/board.h Adds VBUS_SENSE_EN = 1 and removes the inline board_vbus_sense_init(); VBUS sensing is now configured centrally via the DWC2 helper.
hw/bsp/stm32l4/boards/stm32l4p5nucleo/board.h Same L4 Nucleo pattern: defines VBUS_SENSE_EN = 1 and drops the per-board GCCFG VBUS init helper.
hw/bsp/stm32l4/boards/stm32l496nucleo/board.h Marks that this board uses hardware VBUS sensing (VBUS_SENSE_EN = 1) and removes the dedicated board_vbus_sense_init().
hw/bsp/stm32l4/boards/stm32l476disco/board.h Sets VBUS_SENSE_EN = 0 and removes custom VBUS-sense override logic, letting the core handle the always-on override via vbus_sensing = false.
hw/bsp/stm32h7rs/family.c Switches FS/HS VBUS sense configuration to tud_configure() per port, adjusts HS VBUS pin, and explicitly turns off device VBUS and on host VBUS via board_vbus_set.
hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.h Disables FS VBUS sensing (OTG_FS_VBUS_SENSE = 0) for this board to match its wiring, leaving HS VBUS sensing disabled as well.
hw/bsp/stm32h7/family.c Replaces hand-coded GCCFG/GOTGCTL manipulation for FS/HS with tud_configure() calls that use board-level FS/HS VBUS sense macros, and centralizes device/host VBUS power control via board_vbus_set.
hw/bsp/stm32h7/boards/stm32h747disco/board.h Enables HS VBUS sensing (OTG_HS_VBUS_SENSE = 1) to reflect its ULPI HS port wiring.
hw/bsp/stm32h7/boards/stm32h743eval/board.h Likewise enables HS VBUS sensing for this H743 Eval board.
hw/bsp/stm32f7/family.c Uses tud_configure() to drive FS/HS VBUS sensing based on board macros, removes direct HS GCCFG VBDEN programming, and centralizes device/host VBUS power toggling via board_vbus_set.
hw/bsp/stm32f7/boards/stm32f746disco/board.h Enables both FS and HS VBUS sensing via OTG_FS_VBUS_SENSE = 1 and OTG_HS_VBUS_SENSE = 1 to align with hardware.
hw/bsp/stm32f7/boards/stm32f723disco/board.h Enables HS VBUS sensing for the F723 Disco board (OTG_HS_VBUS_SENSE = 1) so unplug detection can work on the HS port.
hw/bsp/stm32f4/family.c Replaces per-board FS VBUS-sense init with tud_configure() using VBUS_SENSE_EN, and ensures device VBUS is initially turned off while host VBUS is turned on using the correct BOARD_TUH_RHPORT.
hw/bsp/stm32f4/boards/stm32f439nucleo/board.h Introduces VBUS_SENSE_EN = 1 and removes the old board_vbus_sense_init(rhport) that wrote GCCFG directly.
hw/bsp/stm32f4/boards/stm32f412nucleo/board.h Same as above for the F412 Nucleo board: define VBUS sense macro and drop the inlined GCCFG VBUS init.
hw/bsp/stm32f4/boards/stm32f412disco/board.h Adds VBUS_SENSE_EN = 1 and removes its custom board_vbus_sense_init() in favor of core-managed GCCFG config.
hw/bsp/stm32f4/boards/stm32f411disco/board.h Sets VBUS_SENSE_EN = 1 and removes direct NOVBUSSENS/VBUSBSEN tweaking; DWC2 STM32 helper now configures GCCFG.
hw/bsp/stm32f4/boards/stm32f411blackpill/board.h Marks this board as not using VBUS sense (VBUS_SENSE_EN = 0) and drops the explicit NOVBUSSENS/VBUSBSEN/ASEN programming.
hw/bsp/stm32f4/boards/stm32f407disco/board.h Enables VBUS sensing (VBUS_SENSE_EN = 1) and removes the inline GCCFG init; unplug detection routes through the new config pipeline.
hw/bsp/stm32f4/boards/stm32f407blackvet/board.h Sets VBUS_SENSE_EN = 0 and deletes its direct NOVBUSSENS/VBUSBSEN/ASEN configuration so the core treats it as “no VBUS sense”.
hw/bsp/stm32f4/boards/stm32f401blackpill/board.h Also sets VBUS_SENSE_EN = 0 and removes explicit GCCFG VBUS-sense configuration for this Blackpill board.
hw/bsp/stm32f4/boards/pyboardv11/board.h Enables VBUS sensing (VBUS_SENSE_EN = 1) and drops redundant GCCFG manipulation, relying on the shared helper.
hw/bsp/stm32f4/boards/feather_stm32f405/board.h Adds VBUS_SENSE_EN = 1 and eliminates the board-specific GCCFG VBUS init function.
hw/bsp/stm32f2/family.c Switches from hard-coded NOVBUSSENS/VBUSBSEN writes to configuring DWC2 via tud_configure() with vbus_sensing = true for the FS port.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@HiFiPhile HiFiPhile force-pushed the dwc2_vbus branch 4 times, most recently from d1158d8 to 3a27026 Compare January 30, 2026 21:04
@github-actions
Copy link

github-actions bot commented Jan 30, 2026

TinyUSB Average Code Size Metrics

File .text .rodata .data .bss size %
cdc_host.c 6596 487 15 1539 8357 5.2%
ehci.c 2761 0 0 5970 7537 4.7%
ncm_device.c 1514 28 1408 5830 7358 4.6%
hcd_dwc2.c 4924 32 0 512 5468 3.4%
usbh.c 4374 60 63 954 5415 3.4%
midi_host.c 1353 7 7 3740 5097 3.2%
video_device.c 4391 5 1851 472 4855 3.0%
audio_device.c 2849 0 1248 1673 4518 2.8%
dcd_dwc2.c 4159 25 0 265 4449 2.8%
ohci.c 1942 0 0 2414 4356 2.7%
dcd_ch32_usbfs.c 1472 0 0 2444 3916 2.4%
hcd_stm32_fsdev.c 3282 0 1 420 3703 2.3%
usbd.c 3178 57 89 276 3519 2.2%
ecm_rndis_device.c 1037 0 1 2272 3310 2.1%
dcd_khci.c 1953 0 0 1290 3243 2.0%
hcd_musb.c 3073 0 0 157 3230 2.0%
dcd_ci_fs.c 1925 0 0 1290 3215 2.0%
dcd_nrf5x.c 2919 0 0 292 3211 2.0%
dcd_da146xx.c 3067 0 0 144 3211 2.0%
hcd_rusb2.c 2923 0 0 245 3168 2.0%
dcd_rusb2.c 2917 0 0 156 3073 1.9%
msc_device.c 2503 108 2274 538 3041 1.9%
hcd_ch32_usbfs.c 2484 0 0 498 2982 1.9%
hcd_khci.c 2442 0 0 449 2891 1.8%
dcd_stm32_fsdev.c 2557 0 0 291 2848 1.8%
dcd_mm32f327x_otg.c 1478 0 0 1290 2768 1.7%
dcd_musb.c 2431 0 0 160 2591 1.6%
hcd_samd.c 2220 0 0 324 2544 1.6%
dcd_ci_hs.c 1762 0 0 1280 2530 1.6%
dcd_eptri.c 2270 0 0 259 2529 1.6%
usbtmc_device.c 2176 24 69 291 2500 1.6%
hid_host.c 1206 0 0 1250 2456 1.5%
mtp_device.c 1689 22 1449 579 2275 1.4%
dcd_rp2040.c 859 20 604 655 2138 1.3%
dcd_ch32_usbhs.c 1648 0 0 448 2096 1.3%
cdc_device.c 1328 16 19 661 1988 1.2%
msc_host.c 1589 0 0 394 1984 1.2%
dcd_msp430x5xx.c 1796 0 0 176 1972 1.2%
hcd_rp2040.c 976 73 416 384 1849 1.2%
dcd_lpc17_40.c 1470 0 0 648 1794 1.1%
midi_device.c 1127 0 991 589 1714 1.1%
dcd_nuc505.c 0 0 1529 157 1686 1.0%
dcd_lpc_ip3511.c 1463 0 0 264 1639 1.0%
dcd_samg.c 1319 0 0 72 1391 0.9%
dcd_samd.c 1032 0 0 266 1298 0.8%
hub.c 1235 8 8 29 1268 0.8%
dcd_nuc121.c 1167 0 0 101 1268 0.8%
hid_device.c 1118 44 997 115 1233 0.8%
dcd_nuc120.c 1093 0 0 78 1171 0.7%
vendor_device.c 624 0 530 464 1087 0.7%
rp2040_usb.c 172 75 718 4 969 0.6%
dfu_device.c 744 28 712 183 926 0.6%
typec_stm32.c 820 8 2 12 842 0.5%
tusb_fifo.c 843 0 477 0 838 0.5%
usbc.c 420 2 20 166 608 0.4%
usbd_control.c 523 0 474 78 600 0.4%
dwc2_common.c 582 30 0 0 598 0.4%
hcd_pio_usb.c 262 0 240 0 502 0.3%
tusb.c 429 0 368 3 430 0.3%
hcd_ci_hs.c 190 0 0 0 190 0.1%
fsdev_common.c 180 0 0 0 180 0.1%
rusb2_common.c 160 0 16 0 176 0.1%
dfu_rt_device.c 156 0 134 0 156 0.1%
TOTAL 113152 1159 16730 45511 160755 100.0%
Input files
  • cmake-build/cmake-build-adafruit_clue/metrics.json
  • cmake-build/cmake-build-apard32690/metrics.json
  • cmake-build/cmake-build-at32f403a_weact_blackpill/metrics.json
  • cmake-build/cmake-build-at_start_f402/metrics.json
  • cmake-build/cmake-build-at_start_f413/metrics.json
  • cmake-build/cmake-build-at_start_f415/metrics.json
  • cmake-build/cmake-build-at_start_f423/metrics.json
  • cmake-build/cmake-build-at_start_f425/metrics.json
  • cmake-build/cmake-build-at_start_f435/metrics.json
  • cmake-build/cmake-build-at_start_f455/metrics.json
  • cmake-build/cmake-build-b_g474e_dpow1/metrics.json
  • cmake-build/cmake-build-b_u585i_iot2a/metrics.json
  • cmake-build/cmake-build-ch32v103r_r1_1v0/metrics.json
  • cmake-build/cmake-build-ch32v203c_r0_1v0/metrics.json
  • cmake-build/cmake-build-ch32v307v_r1_1v0/metrics.json
  • cmake-build/cmake-build-cynthion_d11/metrics.json
  • cmake-build/cmake-build-da14695_dk_usb/metrics.json
  • cmake-build/cmake-build-double_m33_express/metrics.json
  • cmake-build/cmake-build-ea4088_quickstart/metrics.json
  • cmake-build/cmake-build-ea4357/metrics.json
  • cmake-build/cmake-build-ek_tm4c123gxl/metrics.json
  • cmake-build/cmake-build-feather_stm32f405/metrics.json
  • cmake-build/cmake-build-fomu/metrics.json
  • cmake-build/cmake-build-frdm_k32l2a4s/metrics.json
  • cmake-build/cmake-build-frdm_k64f/metrics.json
  • cmake-build/cmake-build-frdm_kl25z/metrics.json
  • cmake-build/cmake-build-frdm_mcxa153/metrics.json
  • cmake-build/cmake-build-frdm_rw612/metrics.json
  • cmake-build/cmake-build-hpm6750evk2/metrics.json
  • cmake-build/cmake-build-lpcxpresso11u37/metrics.json
  • cmake-build/cmake-build-lpcxpresso1347/metrics.json
  • cmake-build/cmake-build-lpcxpresso1549/metrics.json
  • cmake-build/cmake-build-lpcxpresso1769/metrics.json
  • cmake-build/cmake-build-lpcxpresso18s37/metrics.json
  • cmake-build/cmake-build-lpcxpresso51u68/metrics.json
  • cmake-build/cmake-build-lpcxpresso54114/metrics.json
  • cmake-build/cmake-build-metro_m0_express/metrics.json
  • cmake-build/cmake-build-metro_m4_express/metrics.json
  • cmake-build/cmake-build-metro_m7_1011/metrics.json
  • cmake-build/cmake-build-mm32f327x_mb39/metrics.json
  • cmake-build/cmake-build-msp_exp430f5529lp/metrics.json
  • cmake-build/cmake-build-msp_exp432e401y/metrics.json
  • cmake-build/cmake-build-nutiny_nuc126v/metrics.json
  • cmake-build/cmake-build-nutiny_sdk_nuc120/metrics.json
  • cmake-build/cmake-build-nutiny_sdk_nuc121/metrics.json
  • cmake-build/cmake-build-nutiny_sdk_nuc505/metrics.json
  • cmake-build/cmake-build-portenta_c33/metrics.json
  • cmake-build/cmake-build-raspberry_pi_pico/metrics.json
  • cmake-build/cmake-build-raspberrypi_cm4/metrics.json
  • cmake-build/cmake-build-raspberrypi_zero/metrics.json
  • cmake-build/cmake-build-samg55_xplained/metrics.json
  • cmake-build/cmake-build-sipeed_longan_nano/metrics.json
  • cmake-build/cmake-build-stlinkv3mini/metrics.json
  • cmake-build/cmake-build-stm32c071nucleo/metrics.json
  • cmake-build/cmake-build-stm32f070rbnucleo/metrics.json
  • cmake-build/cmake-build-stm32f103_bluepill/metrics.json
  • cmake-build/cmake-build-stm32f207nucleo/metrics.json
  • cmake-build/cmake-build-stm32f303disco/metrics.json
  • cmake-build/cmake-build-stm32g0b1nucleo/metrics.json
  • cmake-build/cmake-build-stm32h503nucleo/metrics.json
  • cmake-build/cmake-build-stm32h743eval/metrics.json
  • cmake-build/cmake-build-stm32h7s3nucleo/metrics.json
  • cmake-build/cmake-build-stm32l052dap52/metrics.json
  • cmake-build/cmake-build-stm32l412nucleo/metrics.json
  • cmake-build/cmake-build-stm32n6570dk/metrics.json
  • cmake-build/cmake-build-stm32u083cdk/metrics.json
  • cmake-build/cmake-build-stm32wb55nucleo/metrics.json
  • cmake-build/cmake-build-stm32wba_nucleo/metrics.json
  • cmake-build/cmake-build-xmc4500_relax/metrics.json

@HiFiPhile
Copy link
Collaborator Author

@brentmellor Could you check if Vbus detection works on Nucleo-U575 ?

@github-actions
Copy link

github-actions bot commented Jan 31, 2026

MemBrowse Memory Report

at32f402_405-at_start_f402-cdc_msc

  • FLASH: .text +28 B (+0.2%, 18,224 B / 262,144 B, total: 7% used)

at32f415-at_start_f415-cdc_msc

  • FLASH: .text +28 B (+0.1%, 19,664 B / 262,144 B, total: 8% used)

at32f423-at_start_f423-cdc_msc

  • FLASH: .text +28 B (+0.1%, 20,048 B / 262,144 B, total: 8% used)

at32f425-at_start_f425-cdc_msc

  • FLASH: .text +28 B (+0.1%, 27,696 B / 65,536 B, total: 42% used)

at32f435_437-at_start_f435-cdc_msc

  • FLASH: .text +28 B (+0.2%, 17,676 B / 4,128,768 B, total: 0% used)

gd32vf103-sipeed_longan_nano-cdc_msc

  • flash: .text +64 B (+0.3%, 20,346 B / 131,072 B, total: 16% used)

stm32f2-stm32f207nucleo-cdc_msc

  • FLASH: .text +104 B (+0.6%, 17,976 B / 1,048,576 B, total: 2% used)

stm32f4-feather_stm32f405-cdc_msc

  • FLASH: .text +100 B (+0.5%, 18,728 B / 1,048,576 B, total: 2% used)

stm32f7-stlinkv3mini-cdc_msc

  • FLASH: .text +48 B (+0.2%, 20,148 B / 524,288 B, total: 4% used)

stm32h7-stm32h743eval-cdc_msc

  • FLASH: .text +52 B (+0.2%, 30,032 B / 2,097,152 B, total: 1% used)

stm32h7rs-stm32h7s3nucleo-cdc_msc

  • FLASH: .text +156 B (+0.6%, 27,508 B / 65,536 B, total: 42% used)

stm32n6-stm32n6570dk-cdc_msc

  • ROM: .rodata +48 B, .text +748 B (+2.0%, 41,136 B / 261,120 B, total: 16% used)
  • RAM: .data +28 B (+0.2%, 13,840 B / 262,144 B, total: 5% used)

stm32u5-b_u585i_iot2a-cdc_msc

  • FLASH: .text +72 B (+0.3%, 28,536 B / 4,194,304 B, total: 1% used)

stm32wba-stm32wba_nucleo-cdc_msc

  • FLASH: .text +28 B (+0.1%, 21,068 B / 1,048,576 B, total: 2% used)

xmc4000-xmc4500_relax-cdc_msc

  • FLASH_1_cached: .text +64 B (+0.4%, 17,688 B / 1,048,576 B, total: 2% used)

No memory changes detected for:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No DCD_EVENT_UNPLUGGED event for STM32 dwc2

2 participants