Skip to content

hcd: add stm32_fsdev driver#3364

Merged
hathach merged 25 commits intomasterfrom
hcd_fsdev
Dec 15, 2025
Merged

hcd: add stm32_fsdev driver#3364
hathach merged 25 commits intomasterfrom
hcd_fsdev

Conversation

@HiFiPhile
Copy link
Collaborator

@HiFiPhile HiFiPhile commented Nov 24, 2025

Describe the PR
Add stm32_fsdev HCD mode support, host mode works pretty like device mode with 8 bidirectional channels.

  • Static buffer allocation, 64*2 bytes per channel (IN+OUT) (no ISO support)
  • Dynamic channel allocation like dwc2
  • No interrupt transfer scheduler, transfer will be retried on each frame if NAKed
  • Add deinit to DCD/HCD

Tested on Nucleo-C071RB (put on both STLINK and USB power jumper):

  • Hub is tested with Keyboard + Disk
    • Hub plug-in result a strange hardfault (power ??) so it's plugged on startup
    • Sometimes device unplug result a assert in config_test_unit_ready_complete and hub stop working

Also tested on STM32H573I-DK

Example Result Note
device_info OK Keyboard/Disk
bare_api OK Keyboard/Disk
cdc_msc_hid OK Keyboard/Disk
cdc_msc_hid_freertos OK Keyboard/Disk
msc_file_explorer OK File copy at 100KB/s

Copilot AI review requested due to automatic review settings November 24, 2025 22:26
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 USB Host Controller Driver (HCD) support for STM32 FSDEV peripheral, enabling host mode functionality on STM32 C0, G0, H5, and U5 MCU families. The implementation refactors common code from the existing device controller driver (DCD) into shared files to support both device and host modes.

Key changes:

  • Implements a new HCD driver with static buffer allocation (64 bytes per channel), dynamic channel allocation, and NAK retry mechanism
  • Refactors packet memory access and buffer table functions into fsdev_common.c/h for reuse between DCD and HCD
  • Updates all STM32 BSP build files to include the new common source file

Reviewed changes

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

Show a summary per file
File Description
src/portable/st/stm32_fsdev/hcd_stm32_fsdev.c New HCD driver implementation for STM32 FSDEV peripheral
src/portable/st/stm32_fsdev/fsdev_common.c Extracted common PMA access and buffer table functions
src/portable/st/stm32_fsdev/fsdev_common.h Common header with helper functions for both DCD and HCD
src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c Updated to use refactored common functions
src/portable/st/stm32_fsdev/fsdev_stm32.h Converted DCD-specific functions to generic inline helpers
src/portable/st/stm32_fsdev/fsdev_ch32.h Updated function names and added volatile qualifiers
src/portable/st/stm32_fsdev/fsdev_at32.h Updated function names and improved volatile pointer usage
hw/bsp/stm32c0/family.c Updated interrupt handler to support both device and host modes
hw/bsp/stm32c0/boards/stm32c071nucleo/board.h Changed clock source from HSI48 to external HSE
hw/bsp/stm32*/family.mk, family.cmake Added fsdev_common.c and hcd_stm32_fsdev.c to build
tools/iar_template.ipcf Added new HCD and common files to IAR template
examples/host/*/only.txt Enabled host examples for STM32C0
README.rst Updated support matrix to indicate host mode support on C0, G0, H5

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

@HiFiPhile HiFiPhile force-pushed the hcd_fsdev branch 2 times, most recently from 26f130e to 5963b1d Compare November 24, 2025 22:36
@hathach
Copy link
Owner

hathach commented Nov 25, 2025

brilliant as usual, unfortunately, I got an sore throat and running nose. I will test this out later on when getting better.

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: Zixun LI <admin@hifiphile.com>
Signed-off-by: Zixun LI <admin@hifiphile.com>
Signed-off-by: Zixun LI <admin@hifiphile.com>
Signed-off-by: Zixun LI <admin@hifiphile.com>
Signed-off-by: Zixun LI <admin@hifiphile.com>
Signed-off-by: Zixun LI <admin@hifiphile.com>
Signed-off-by: Zixun LI <admin@hifiphile.com>
Signed-off-by: HiFiPhile <admin@hifiphile.com>
Signed-off-by: HiFiPhile <admin@hifiphile.com>
@HiFiPhile HiFiPhile force-pushed the hcd_fsdev branch 2 times, most recently from 31a987f to cbab765 Compare November 27, 2025 22:18
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>
@sonarqubecloud
Copy link

sonarqubecloud bot commented Dec 2, 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 .rodata .data .bss size % diff
cdc_host.c 6614 ➙ 6511 (-103) 487 15 1624 ➙ 1418 (-206) 8422 ➙ 8173 (-249) -3.0%
dcd_stm32_fsdev.c 3149 ➙ 2494 (-655) 3 ➙ 0 (-3) 0 291 3442 ➙ 2785 (-657) -19.1%
fsdev_common.c 0 ➙ 622 (+622) 0 0 0 0 ➙ 622 (+622) n/a
hcd_stm32_fsdev.c 0 ➙ 3258 (+3258) 0 0 ➙ 1 (+1) 0 ➙ 420 (+420) 0 ➙ 3679 (+3679) n/a
midi_host.c 1340 ➙ 1335 (-5) 7 7 4010 ➙ 3471 (-539) 5353 ➙ 4810 (-543) -10.1%
msc_host.c 1590 ➙ 1571 (-19) 0 0 404 ➙ 403 (-1) 1994 ➙ 1974 (-20) -1.0%
TOTAL 12693 ➙ 15791 (+3098) 497 ➙ 494 (-3) 22 ➙ 23 (+1) 6329 ➙ 6003 (-326) 19211 ➙ 22043 (+2832) +14.7%

Changes <1% in size

file .text .rodata .data .bss size % diff
hcd_dwc2.c 4898 ➙ 4880 (-18) 32 ➙ 30 (-2) 0 512 5442 ➙ 5422 (-20) -0.4%
hid_host.c 1194 ➙ 1190 (-4) 0 0 1286 ➙ 1306 (+20) 2480 ➙ 2495 (+15) +0.6%
hub.c 1239 ➙ 1227 (-12) 8 8 31 ➙ 30 (-1) 1274 ➙ 1262 (-12) -0.9%
tusb.c 416 ➙ 419 (+3) 0 361 3 418 ➙ 421 (+3) +0.7%
tusb_fifo.c 902 ➙ 898 (-4) 0 525 0 896 ➙ 892 (-4) -0.4%
usbh.c 4314 ➙ 4286 (-28) 57 ➙ 60 (+3) 61 ➙ 60 (-1) 962 ➙ 966 (+4) 5356 ➙ 5339 (-17) -0.3%
TOTAL 12963 ➙ 12900 (-63) 97 ➙ 98 (+1) 955 ➙ 954 (-1) 2794 ➙ 2817 (+23) 15866 ➙ 15831 (-35) -0.2%
No changes
file .text .rodata .data .bss size % diff
audio_device.c 2859 0 1250 1687 4540 +0.0%
cdc_device.c 1332 16 20 656 1986 +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 1725 0 0 1152 2493 +0.0%
dcd_da146xx.c 3067 0 0 144 3211 +0.0%
dcd_dwc2.c 4188 25 0 263 4474 +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 2228 0 0 160 2388 +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 3076 0 0 156 3232 +0.0%
dcd_samd.c 1032 0 0 266 1298 +0.0%
dcd_samg.c 1319 0 0 72 1391 +0.0%
dfu_device.c 741 28 712 174 915 +0.0%
dfu_rt_device.c 157 0 134 0 156 +0.0%
dwc2_common.c 781 29 0 0 797 +0.0%
ecm_rndis_device.c 1037 0 1 2272 3310 +0.0%
ehci.c 2651 0 0 5970 8622 +0.0%
hcd_ch32_usbfs.c 2484 0 0 498 2982 +0.0%
hcd_ci_hs.c 229 0 0 0 229 +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 2965 0 0 246 3211 +0.0%
hcd_samd.c 2220 0 0 324 2544 +0.0%
hid_device.c 1119 44 997 117 1235 +0.0%
midi_device.c 1129 0 991 584 1711 +0.0%
msc_device.c 2508 108 2274 544 3052 +0.0%
mtp_device.c 1622 22 1385 585 2215 +0.0%
ncm_device.c 1503 28 1408 5961 7478 +0.0%
ohci.c 1942 0 0 2414 4356 +0.0%
rp2040_usb.c 124 70 574 4 772 +0.0%
rusb2_common.c 0 0 16 0 16 +0.0%
typec_stm32.c 820 8 2 12 842 +0.0%
usbc.c 408 2 16 166 592 +0.0%
usbd.c 3167 57 88 276 3505 +0.0%
usbd_control.c 523 0 474 79 601 +0.0%
usbtmc_device.c 2169 24 71 287 2488 +0.0%
vendor_device.c 609 0 514 456 1064 +0.0%
video_device.c 4399 6 1851 476 4866 +0.0%
TOTAL 84236 560 15547 36885 124009 +0.0%

Copy link
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.

tested on my h563 nucleo and it works perfectly. Thank you very much for this brilliant work

@hathach hathach merged commit 9a22543 into master Dec 15, 2025
156 checks passed
@hathach hathach deleted the hcd_fsdev branch December 15, 2025 09:01
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