Skip to content

Add DWC2 deinit support#3378

Open
HiFiPhile wants to merge 10 commits intomasterfrom
dwc2_deinit
Open

Add DWC2 deinit support#3378
HiFiPhile wants to merge 10 commits intomasterfrom
dwc2_deinit

Conversation

@HiFiPhile
Copy link
Collaborator

@HiFiPhile HiFiPhile commented Nov 28, 2025

@roma-jam Hi, I've added deinit support to dwc2.

A dynamic switch example is added and tested on:

  • ESP32-S3-DevKitC
  • NUCLEO-H7S3L8
  • Nucleo-U5A5ZJ-Q
  • STM32F407-DISCO
  • STM32F723E-DISCO (FS/HS)
  • J-Link V9 (STM32F205 FS)

Copilot AI review requested due to automatic review settings November 28, 2025 14:44
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 adds deinitialization support for the Synopsys DWC2 USB controller, enabling dynamic start/stop of USB host and device modes. The implementation mirrors the initialization sequence in reverse, disabling interrupts, disconnecting from the bus, and resetting the core. This was tested on STM32H7S3 with device mode toggling via button press.

Key Changes

  • Implements hcd_deinit() and dcd_deinit() functions for DWC2 host and device controllers
  • Adds common dwc2_core_deinit() function shared between host and device modes
  • Updates error handling in tuh_deinit() and tud_deinit() to use TU_ASSERT consistently
  • Fixes button active state configuration for STM32H7S3 Nucleo board

Reviewed changes

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

Show a summary per file
File Description
src/portable/synopsys/dwc2/hcd_dwc2.c Adds host controller deinit implementation with interrupt disable
src/portable/synopsys/dwc2/dwc2_common.h Declares dwc2_core_deinit() for shared deinitialization logic
src/portable/synopsys/dwc2/dwc2_common.c Implements common core deinit with soft disconnect and core reset
src/portable/synopsys/dwc2/dcd_dwc2.c Adds device controller deinit with disconnect and cleanup
src/host/usbh.c Changes hcd_deinit() call to use TU_ASSERT for error checking
src/device/usbd.c Changes dcd_deinit() call to use TU_ASSERT for consistency
hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.h Corrects button active state from 1 to 0 (active-low with pullup)

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

@HiFiPhile HiFiPhile mentioned this pull request Nov 28, 2025
1 task
@HiFiPhile HiFiPhile force-pushed the dwc2_deinit branch 2 times, most recently from 9461753 to e214976 Compare November 29, 2025 12:31
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

Copilot reviewed 22 out of 22 changed files in this pull request and generated 9 comments.


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

@sonarqubecloud
Copy link

sonarqubecloud bot commented Dec 3, 2025

@github-actions
Copy link

github-actions bot commented Dec 13, 2025

Size Difference Report

Because TinyUSB code size varies by port and configuration, the metrics below represent the averaged totals across all example builds.

Note: If there is no change, only one value is shown.

Changes >1% in size

file .text .data .bss size % diff
tusb.c 418 ➙ 423 (+5) 359 ➙ 361 (+2) 3 420 ➙ 425 (+5) +1.2%
TOTAL 418 ➙ 423 (+5) 359 ➙ 361 (+2) 3 420 ➙ 425 (+5) +1.2%

Changes <1% in size

file .text .rodata .data .bss size % diff
cdc_device.c 1331 ➙ 1329 (-2) 16 20 670 ➙ 668 (-2) 2000 ➙ 1996 (-4) -0.2%
hcd_dwc2.c 4893 ➙ 4925 (+32) 30 0 512 5434 ➙ 5467 (+33) +0.6%
hcd_stm32_fsdev.c 3282 ➙ 3287 (+5) 0 1 420 3703 ➙ 3708 (+5) +0.1%
hub.c 1235 8 8 29 1268 ➙ 1269 (+1) +0.1%
usbd.c 3179 ➙ 3188 (+9) 57 90 276 3519 ➙ 3528 (+9) +0.3%
usbh.c 4374 ➙ 4385 (+11) 60 ➙ 54 (-6) 63 ➙ 62 (-1) 954 ➙ 951 (-3) 5415 ➙ 5422 (+7) +0.1%
TOTAL 18294 ➙ 18349 (+55) 171 ➙ 165 (-6) 182 ➙ 181 (-1) 2861 ➙ 2856 (-5) 21339 ➙ 21390 (+51) +0.2%
No changes
file .text .rodata .data .bss size % diff
audio_device.c 2855 0 1248 1679 4529 +0.0%
cdc_host.c 6596 487 15 1539 8357 +0.0%
dcd_ch32_usbfs.c 1472 0 0 2444 3916 +0.0%
dcd_ch32_usbhs.c 1648 0 0 448 2096 +0.0%
dcd_ci_fs.c 1925 0 0 1290 3215 +0.0%
dcd_ci_hs.c 1762 0 0 1280 2530 +0.0%
dcd_da146xx.c 3067 0 0 144 3211 +0.0%
dcd_dwc2.c 4160 ➙ 4161 (+1) 25 0 263 4447 +0.0%
dcd_eptri.c 2270 0 0 259 2529 +0.0%
dcd_khci.c 1953 0 0 1290 3243 +0.0%
dcd_lpc17_40.c 1470 0 0 648 1794 +0.0%
dcd_lpc_ip3511.c 1463 0 0 264 1639 +0.0%
dcd_mm32f327x_otg.c 1478 0 0 1290 2768 +0.0%
dcd_msp430x5xx.c 1796 0 0 176 1972 +0.0%
dcd_musb.c 2079 0 0 160 2239 +0.0%
dcd_nrf5x.c 2919 0 0 292 3211 +0.0%
dcd_nuc120.c 1093 0 0 78 1171 +0.0%
dcd_nuc121.c 1167 0 0 101 1268 +0.0%
dcd_nuc505.c 0 0 1529 157 1686 +0.0%
dcd_rp2040.c 736 20 580 1029 2365 +0.0%
dcd_rusb2.c 2917 0 0 156 3073 +0.0%
dcd_samd.c 1032 0 0 266 1298 +0.0%
dcd_samg.c 1319 0 0 72 1391 +0.0%
dcd_stm32_fsdev.c 2556 0 0 291 2847 +0.0%
dfu_device.c 744 28 712 184 929 +0.0%
dfu_rt_device.c 157 0 134 0 156 +0.0%
dwc2_common.c 594 29 0 0 610 +0.0%
ecm_rndis_device.c 1037 0 1 2272 3310 +0.0%
ehci.c 2761 0 0 5970 7537 +0.0%
fsdev_common.c 180 0 0 0 180 +0.0%
hcd_ch32_usbfs.c 2484 0 0 498 2982 +0.0%
hcd_ci_hs.c 190 0 0 0 190 +0.0%
hcd_khci.c 2442 0 0 449 2891 +0.0%
hcd_musb.c 3073 0 0 157 3230 +0.0%
hcd_pio_usb.c 262 0 240 0 502 +0.0%
hcd_rp2040.c 936 73 420 512 1941 +0.0%
hcd_rusb2.c 2923 0 0 245 3168 +0.0%
hcd_samd.c 2220 0 0 324 2544 +0.0%
hid_device.c 1120 44 997 115 1235 +0.0%
hid_host.c 1206 0 0 1250 2456 +0.0%
midi_device.c 1129 0 991 596 1723 +0.0%
midi_host.c 1353 7 7 3740 5097 +0.0%
msc_device.c 2506 108 2274 538 3044 +0.0%
msc_host.c 1589 0 0 394 1984 +0.0%
mtp_device.c 1622 22 1385 578 2208 +0.0%
ncm_device.c 1517 28 1408 5824 7354 +0.0%
ohci.c 1942 0 0 2414 4356 +0.0%
rp2040_usb.c 124 70 574 4 772 +0.0%
rusb2_common.c 160 0 16 0 176 +0.0%
tusb_fifo.c 844 ➙ 845 (+1) 0 477 0 839 +0.0%
typec_stm32.c 820 8 2 12 842 +0.0%
usbc.c 420 2 20 166 608 +0.0%
usbd_control.c 524 0 474 78 601 +0.0%
usbtmc_device.c 2178 24 70 294 2504 +0.0%
vendor_device.c 636 0 538 470 1105 +0.0%
video_device.c 4402 6 1851 471 4865 +0.0%
TOTAL 93828 ➙ 93830 (+2) 981 15963 43171 138734 +0.0%

return phys_addr;
}

TU_ATTR_WEAK void tusb_pre_init_cb(uint8_t rhport, tusb_role_t role) {
Copy link
Owner

Choose a reason for hiding this comment

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

do we really need this, both init() and de-init() is called by application, they should call them before/after those accordingly ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@hathach It's for host/device specific init, like STM32's GCCFG and GOTGCTL register, which need to be set to different value on host/device. Their meaning is also different across families, I haven't found a easy way to include them in the driver.

void tusb_pre_init_cb(uint8_t rhport, tusb_role_t role) {

Copy link
Owner

Choose a reason for hiding this comment

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

Give me a bit of time, I will try to review and see if we could avoid using these callbacks

@HiFiPhile HiFiPhile force-pushed the dwc2_deinit branch 3 times, most recently from 7c4233b to f7d82a1 Compare December 15, 2025 22:01
Signed-off-by: HiFiPhile <admin@hifiphile.com>
Signed-off-by: HiFiPhile <admin@hifiphile.com>
Signed-off-by: HiFiPhile <admin@hifiphile.com>
Signed-off-by: HiFiPhile <admin@hifiphile.com>
Signed-off-by: HiFiPhile <admin@hifiphile.com>
Signed-off-by: HiFiPhile <admin@hifiphile.com>
Signed-off-by: HiFiPhile <admin@hifiphile.com>
Signed-off-by: HiFiPhile <admin@hifiphile.com>
Signed-off-by: Zixun LI <admin@hifiphile.com>
Signed-off-by: HiFiPhile <admin@hifiphile.com>
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.

2 participants