Skip to content

Fix bugs in FSDEV HCD/DCD and MIDI host stream write#3559

Merged
hathach merged 1 commit intomasterfrom
fix-fsdev-midi-host-bugs
Mar 18, 2026
Merged

Fix bugs in FSDEV HCD/DCD and MIDI host stream write#3559
hathach merged 1 commit intomasterfrom
fix-fsdev-midi-host-bugs

Conversation

@hathach
Copy link
Copy Markdown
Owner

@hathach hathach commented Mar 18, 2026

Summary

  • hcd_edpt_clear_stall: fix wrong endpoint lookup — was always finding EP0 instead of the target endpoint, breaking stall recovery on non-control endpoints
  • hcd compile guard: add OPT_MCU_STM32U3 to enable USB host mode on STM32U3 (all infrastructure already in place)
  • dcd_edpt_close_all: use FSDEV_EP_COUNT instead of CFG_TUD_ENDPPOINT_MAX for PMA btable offset, matching handle_bus_reset
  • midi host tuh_midi_stream_write: add missing cable number to system messages, SysEx, and real-time MIDI packets; add & 0xF mask for SysEx CIN checks — aligns with device-side tud_midi_n_stream_write

Test plan

  • Build stm32f072disco (16-bit FSDEV, 1KB PMA) — OK
  • Build stm32g0b1nucleo (32-bit FSDEV, 2KB PMA) — OK
  • Build stm32g491nucleo (16-bit FSDEV, 1KB PMA) — OK
  • Build stm32l476disco (16-bit FSDEV, 1KB PMA) — OK
  • Build stm32f723disco (DWC2, MIDI host compiled) — OK
  • Build stm32h743nucleo (DWC2, DMA) — OK
  • Pre-commit checks (yaml, whitespace, eof, codespell) — all pass
  • HIL tests: 0 failures across stm32f072disco, stm32g0b1nucleo, stm32f723disco, stm32h743nucleo, max32666fthr

🤖 Generated with Claude Code

@github-actions
Copy link
Copy Markdown

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

No entries.

Changes <1% in size

No entries.

No changes
file .text .rodata .data .bss size % diff
audio_device.c 2896 0 1260 1623 4514 +0.0%
cdc_device.c 1252 16 1106 682 1932 +0.0%
cdc_host.c 6617 487 15 1498 8327 +0.0%
dcd_ch32_usbfs.c 1473 0 0 2444 3917 +0.0%
dcd_ch32_usbhs.c 1469 0 0 448 1917 +0.0%
dcd_ci_fs.c 1925 0 0 1290 3215 +0.0%
dcd_ci_hs.c 1759 0 0 1344 2538 +0.0%
dcd_da146xx.c 3067 0 0 144 3211 +0.0%
dcd_dwc2.c 4176 25 0 265 4465 +0.0%
dcd_eptri.c 2271 0 0 259 2530 +0.0%
dcd_ft9xx.c 3276 0 0 172 3448 +0.0%
dcd_khci.c 1953 0 0 1290 3243 +0.0%
dcd_lpc17_40.c 1474 0 0 648 1798 +0.0%
dcd_lpc_ip3511.c 1463 0 0 264 1683 +0.0%
dcd_mm32f327x_otg.c 1478 0 0 1290 2768 +0.0%
dcd_msp430x5xx.c 1798 0 0 176 1974 +0.0%
dcd_musb.c 2445 0 0 160 2605 +0.0%
dcd_nrf5x.c 2918 0 0 292 3210 +0.0%
dcd_nuc120.c 1094 0 0 78 1172 +0.0%
dcd_nuc121.c 1168 0 0 101 1269 +0.0%
dcd_nuc505.c 0 0 1531 157 1688 +0.0%
dcd_rp2040.c 858 20 604 655 2137 +0.0%
dcd_rusb2.c 2919 0 0 156 3075 +0.0%
dcd_samd.c 1034 0 0 266 1300 +0.0%
dcd_samg.c 1320 0 0 72 1392 +0.0%
dcd_stm32_fsdev.c 2558 0 0 291 2849 +0.0%
dfu_device.c 777 28 712 140 916 +0.0%
dfu_rt_device.c 157 0 134 0 157 +0.0%
dwc2_common.c 602 30 0 0 618 +0.0%
ecm_rndis_device.c 1037 0 1 2858 3896 +0.0%
ehci.c 2763 0 0 6043 7597 +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 184 0 0 0 184 +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 1125 44 997 119 1244 +0.0%
hid_host.c 1240 0 0 1251 2491 +0.0%
hub.c 1384 8 8 30 1418 +0.0%
midi_device.c 1150 0 1007 621 1770 +0.0%
midi_host.c 1341 7 7 3635 4979 +0.0%
msc_device.c 2525 108 2286 547 3071 +0.0%
msc_host.c 1587 0 0 394 1982 +0.0%
mtp_device.c 1696 22 735 588 2292 +0.0%
ncm_device.c 1538 28 718 5843 7395 +0.0%
ohci.c 1940 0 0 2414 4353 +0.0%
printer_device.c 830 0 706 564 1392 +0.0%
rp2040_usb.c 172 75 717 4 968 +0.0%
rusb2_common.c 160 0 16 0 176 +0.0%
tusb.c 451 0 383 3 453 +0.0%
tusb_fifo.c 842 0 480 0 837 +0.0%
typec_stm32.c 820 8 2 12 842 +0.0%
usbc.c 420 2 20 166 608 +0.0%
usbd.c 3225 57 88 275 3565 +0.0%
usbd_control.c 538 0 484 79 616 +0.0%
usbh.c 4649 55 99 961 5731 +0.0%
usbtmc_device.c 2196 24 68 316 2544 +0.0%
vendor_device.c 641 0 534 563 1202 +0.0%
video_device.c 4443 5 1235 479 4914 +0.0%
TOTAL 117935 1155 16611 46960 166982 +0.0%

@hathach hathach force-pushed the fix-fsdev-midi-host-bugs branch from f738d9f to cd3b98c Compare March 18, 2026 12:02
- hcd_edpt_clear_stall: use ep_addr instead of hardcoded 0 (control endpoint)
- hcd: add TUP_USBIP_FSDEV_DRD define for MCUs with host support (C0, G0, H5,
  U3, U5) and use it in hcd compile guard instead of enumerating MCUs
- dcd_edpt_close_all: use FSDEV_EP_COUNT instead of CFG_TUD_ENDPPOINT_MAX for
  PMA btable offset to match handle_bus_reset
- midi host tuh_midi_stream_write: add missing cable_num to system messages,
  SysEx, and real-time MIDI packets. Add 0xF mask for SysEx CIN checks.
  Aligns with midi_device.c tud_midi_n_stream_write implementation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@hathach hathach force-pushed the fix-fsdev-midi-host-bugs branch from cd3b98c to 8819605 Compare March 18, 2026 12:04
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 18, 2026

MemBrowse Memory Report

No memory changes detected across 2156 targets. View Project Dashboard →

@hathach hathach marked this pull request as ready for review March 18, 2026 14:00
Copilot AI review requested due to automatic review settings March 18, 2026 14:01
@hathach hathach merged commit 418501e into master Mar 18, 2026
415 of 416 checks passed
@hathach hathach deleted the fix-fsdev-midi-host-bugs branch March 18, 2026 14:01
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 fixes several correctness issues across the STM32 FSDEV USB host/device drivers and the MIDI host stream writer to improve endpoint handling, PMA allocation consistency, and USB-MIDI packet formatting.

Changes:

  • Fix FSDEV HCD stall-clear endpoint lookup and broaden host compile gating to DRD-capable STM32 FSDEV MCUs (including STM32U3).
  • Correct FSDEV DCD PMA allocation reset logic to use the hardware endpoint count for BTABLE sizing.
  • Fix MIDI host stream write to consistently include the cable number and properly detect SysEx state via CIN masking.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/portable/st/stm32_fsdev/hcd_stm32_fsdev.c Fixes hcd_edpt_clear_stall() to clear stalls on the intended endpoint; updates compile guard to TUP_USBIP_FSDEV_DRD.
src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c Resets PMA allocation using FSDEV_EP_COUNT to match hardware BTABLE sizing and bus-reset behavior.
src/common/tusb_mcu.h Defines TUP_USBIP_FSDEV_DRD for additional STM32 FSDEV families to enable host builds where supported.
src/class/midi/midi_host.c Updates tuh_midi_stream_write() to include cable number for more message types and masks CIN for SysEx checks.
Comments suppressed due to low confidence (1)

src/class/midi/midi_host.c:468

  • streamrt is stack-allocated and only buffer[0]/buffer[1] are initialized, but the code writes 4 bytes to the endpoint stream. This can send uninitialized garbage bytes in buffer[2]/buffer[3] (and potentially leak stack data) for real-time MIDI messages. Initialize the whole struct (e.g., zero it) or explicitly set the remaining bytes to 0 before calling tu_edpt_stream_write.
      midi_driver_stream_t streamrt;
      streamrt.buffer[0] = (uint8_t)((cable_num << 4) | MIDI_CIN_SYSEX_END_1BYTE);
      streamrt.buffer[1] = data;
      streamrt.index = 2;
      streamrt.total = 2;
      const uint32_t count = tu_edpt_stream_write(&p_midi->ep_stream.tx, streamrt.buffer, 4);

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

You can also share your feedback on Copilot code review. Take the survey.

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