-
Notifications
You must be signed in to change notification settings - Fork 1.4k
DWC2 Vbus sensing enhancement #3476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: HiFiPhile <[email protected]>
There was a problem hiding this 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 avbus_sensingflag and propagate it into the DWC2 device/host drivers, including a new STM32-specificdwc2_stm32_gccfg_cfg()to manageGCCFG/VBUS bits per MCU family and role. - In the DWC2 device driver, integrate OTG SEDET-based unplug detection (
GOTGINT_SEDET) and conditionally controlGOTGCTLoverrides based onvbus_sensingso real VBUS sensing can driveDCD_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.
d1158d8 to
3a27026
Compare
TinyUSB Average Code Size Metrics
Input files
|
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
|
@brentmellor Could you check if Vbus detection works on Nucleo-U575 ? |
Signed-off-by: HiFiPhile <[email protected]>
Signed-off-by: HiFiPhile <[email protected]>
Describe the PR
tud_configure(), for STM32GCCFGregister config is now included in the stack, which reduces BSP compicity.Other device families should not be infected as
GOTGCFGdefault value stays the same.Device and host modes are tested and functional:
1: Needs 104cf33
Should fix #1476, #3426
Additional context
No more callback is needed for #3378