Skip to content

ci_hs: add deinit support#3394

Merged
hathach merged 2 commits intomasterfrom
ci_deinit
Mar 6, 2026
Merged

ci_hs: add deinit support#3394
hathach merged 2 commits intomasterfrom
ci_deinit

Conversation

@HiFiPhile
Copy link
Copy Markdown
Collaborator

Describe the PR
Tested on LPC4322 with new dynamic_switch example.

Rebase after #3378

Copilot AI review requested due to automatic review settings December 8, 2025 08:46
Copy link
Copy Markdown
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 comprehensive deinitialization support to TinyUSB, enabling dynamic switching between USB device and host modes at runtime. The changes introduce new pre-init and post-deinit callback hooks, implement deinit functions across multiple USB controller drivers, and include a new example demonstrating the dual-role switching capability.

Key Changes:

  • Added tusb_pre_init_cb() and tusb_post_deinit_cb() callback hooks with weak default implementations for MCU-specific initialization/cleanup
  • Implemented deinit support across multiple USB drivers: dwc2 (DCD/HCD), EHCI, and chipidea ci_hs (DCD/HCD)
  • Created a new dynamic_switch example demonstrating runtime switching between device and host modes via button press

Reviewed changes

Copilot reviewed 27 out of 27 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/tusb.h Declares new pre-init and post-deinit callback hooks
src/tusb.c Provides weak default implementations for the new callback hooks
src/host/usbh.c Integrates pre-init/post-deinit callbacks into host stack init/deinit flow, changes to TU_ASSERT
src/device/usbd.c Integrates pre-init/post-deinit callbacks into device stack init/deinit flow, changes to TU_ASSERT
src/portable/synopsys/dwc2/dwc2_common.h Declares dwc2_core_deinit function
src/portable/synopsys/dwc2/dwc2_common.c Implements core deinit with soft disconnect and reset
src/portable/synopsys/dwc2/hcd_dwc2.c Adds HCD deinit, removes CONIDSTSCHNG interrupt handling, fixes DATATOGGLE_ERR handling
src/portable/synopsys/dwc2/dcd_dwc2.c Adds DCD deinit implementation
src/portable/ehci/ehci_api.h Declares ehci_deinit function
src/portable/ehci/ehci.c Implements EHCI deinit with interrupt/schedule disable
src/portable/chipidea/ci_hs/hcd_ci_hs.c Adds HCD deinit, fixes comment from "device" to "host", fixes full-speed logic
src/portable/chipidea/ci_hs/dcd_ci_hs.c Implements DCD deinit with interrupt disable and endpoint flushing
hw/bsp/stm32u5/family.c Implements tusb_pre_init_cb for role-specific VBUS/OTG configuration, fixes interrupt handler naming
hw/bsp/stm32h7rs/family.c Implements tusb_pre_init_cb with rhport-aware VBUS/OTG configuration
hw/bsp/stm32h7rs/boards/stm32h7s3nucleo/board.h Fixes button active_state from 1 to 0 (active low)
hw/bsp/lpc43/family.c Adds GPIO configuration for VIF_OUT, conditionally includes EA4357 board-specific code
examples/dual/dynamic_switch/* New example demonstrating dynamic device/host mode switching with CDC echo and host enumeration

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

Comment thread examples/dual/dynamic_switch/src/main.c
Comment thread hw/bsp/stm32u5/family.c Outdated
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Dec 8, 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 size % diff
hcd_ci_hs.c 190 ➙ 184 (-6) 190 ➙ 184 (-6) -3.2%
TOTAL 190 ➙ 184 (-6) 190 ➙ 184 (-6) -3.2%

Changes <1% in size

file .text .rodata .data .bss size % diff
dcd_ci_hs.c 1762 0 0 1280 ➙ 1344 (+64) 2530 ➙ 2541 (+11) +0.4%
ehci.c 2761 ➙ 2763 (+2) 0 0 5970 ➙ 6043 (+73) 7537 ➙ 7597 (+60) +0.8%
tusb.c 430 ➙ 431 (+1) 0 368 3 432 ➙ 433 (+1) +0.2%
usbd_control.c 523 0 474 78 600 ➙ 601 (+1) +0.2%
usbh.c 4562 ➙ 4563 (+1) 55 100 ➙ 99 (-1) 964 5647 ➙ 5648 (+1) +0.0%
TOTAL 10038 ➙ 10042 (+4) 55 942 ➙ 941 (-1) 8295 ➙ 8432 (+137) 16746 ➙ 16820 (+74) +0.4%
No changes
file .text .rodata .data .bss size % diff
audio_device.c 2849 0 1248 1673 4518 +0.0%
cdc_device.c 1326 ➙ 1325 (-1) 16 19 660 ➙ 661 (+1) 1985 +0.0%
cdc_host.c 6610 487 15 1539 8371 +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_da146xx.c 3067 0 0 144 3211 +0.0%
dcd_dwc2.c 4174 25 0 265 4464 +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 2446 0 0 160 2606 +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 859 20 604 655 2138 +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 2557 0 0 291 2848 +0.0%
dfu_device.c 744 28 712 183 926 +0.0%
dfu_rt_device.c 156 0 134 0 156 +0.0%
dwc2_common.c 602 30 0 0 618 +0.0%
ecm_rndis_device.c 1037 0 1 2272 3310 +0.0%
fsdev_common.c 180 0 0 0 180 +0.0%
hcd_ch32_usbfs.c 2484 0 0 498 2982 +0.0%
hcd_dwc2.c 4994 33 1 513 5540 +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 976 73 416 384 1849 +0.0%
hcd_rusb2.c 2923 0 0 245 3168 +0.0%
hcd_samd.c 2220 0 0 324 2544 +0.0%
hcd_stm32_fsdev.c 3287 0 1 420 3708 +0.0%
hid_device.c 1118 44 997 115 1233 +0.0%
hid_host.c 1206 0 0 1250 2456 +0.0%
hub.c 1235 8 8 29 1269 +0.0%
midi_device.c 1127 0 991 589 1714 +0.0%
midi_host.c 1353 7 7 3740 5097 +0.0%
msc_device.c 2518 108 2286 538 3056 +0.0%
msc_host.c 1589 0 0 394 1984 +0.0%
mtp_device.c 1689 22 1449 579 2275 +0.0%
ncm_device.c 1514 28 1408 5830 7358 +0.0%
ohci.c 1942 0 0 2414 4356 +0.0%
rp2040_usb.c 172 75 718 4 969 +0.0%
rusb2_common.c 160 0 16 0 176 +0.0%
tusb_fifo.c 844 0 477 0 838 +0.0%
typec_stm32.c 820 8 2 12 842 +0.0%
usbc.c 420 2 20 166 608 +0.0%
usbd.c 3191 ➙ 3192 (+1) 57 89 276 3532 +0.0%
usbtmc_device.c 2176 24 69 291 2500 +0.0%
vendor_device.c 624 0 530 464 1087 +0.0%
video_device.c 4391 5 1851 472 4855 +0.0%
TOTAL 103279 1100 15838 37226 ➙ 37227 (+1) 144220 +0.0%

@HiFiPhile HiFiPhile force-pushed the ci_deinit branch 3 times, most recently from 1893a2a to 8ae01ea Compare January 6, 2026 22:29
@hathach
Copy link
Copy Markdown
Owner

hathach commented Mar 5, 2026

@HiFiPhile we may need to re-base this as well

Signed-off-by: Zixun LI <admin@hifiphile.com>
Copy link
Copy Markdown
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 6 out of 6 changed files in this pull request and generated 5 comments.


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

Comment thread src/portable/ehci/ehci.c
Comment thread src/portable/chipidea/ci_hs/hcd_ci_hs.c Outdated
Comment thread src/portable/chipidea/ci_hs/dcd_ci_hs.c
Comment thread src/portable/chipidea/ci_hs/dcd_ci_hs.c
Comment thread src/portable/ehci/ehci.c
Signed-off-by: Zixun LI <admin@hifiphile.com>
@HiFiPhile
Copy link
Copy Markdown
Collaborator Author

@hathach Rebase done

@hathach
Copy link
Copy Markdown
Owner

hathach commented Mar 5, 2026

@claude review this

Copy link
Copy Markdown
Owner

@hathach hathach left a comment

Choose a reason for hiding this comment

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

perfect, this look good. THank you

@hathach hathach merged commit 694d921 into master Mar 6, 2026
403 of 407 checks passed
@hathach hathach deleted the ci_deinit branch March 6, 2026 04:51
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.

3 participants