Skip to content

dcd/stm32_fsdev: workaround CH32 EP0 premature OUT ACK#3648

Open
HiFiPhile wants to merge 1 commit into
masterfrom
ch32_fsdev
Open

dcd/stm32_fsdev: workaround CH32 EP0 premature OUT ACK#3648
HiFiPhile wants to merge 1 commit into
masterfrom
ch32_fsdev

Conversation

@HiFiPhile

@HiFiPhile HiFiPhile commented May 24, 2026

Copy link
Copy Markdown
Collaborator

CH32 EP0 blindly ACK all OUT packets, even not only ZLP.

Track EP0 control transfer state from SETUP (direction and wLength) and gate CH32 EP0 handshakes accordingly. Switch EP0 type away from control to bulk to block unsolicited OUT ACKs during control read/write gaps, then restore control type when EP0 transfer is explicitly armed.

Mainly fix DFU issue.

Copilot AI review requested due to automatic review settings May 24, 2026 13:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 a CH32-specific workaround in the STM32 FSDEV DCD to prevent EP0 from prematurely ACKing OUT packets during control transfers by tracking EP0 control direction/length and dynamically toggling EP0’s configured type (CONTROL ↔ BULK) to gate handshakes.

Changes:

  • Track EP0 control transfer direction and whether a data stage is expected (based on SETUP + wLength) for CH32 targets.
  • Add an EP0 helper to switch endpoint type and apply it during control read/write “gaps” to block unsolicited OUT ACKs.
  • Remove the post-status-stage edpt0_prepare_setup() call from dcd_edpt0_status_complete().

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

Comment thread src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c Outdated
@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

file .text .bss size % diff
dcd_stm32_fsdev.c 2558 ➙ 2568 (+10) 291 2849 ➙ 2859 (+10) +0.4%
TOTAL 2558 ➙ 2568 (+10) 291 2849 ➙ 2859 (+10) +0.4%
No changes
file .text .rodata .data .bss size % diff
audio_device.c 2896 0 1259 1625 4515 +0.0%
cdc_device.c 1239 16 1092 735 1972 +0.0%
cdc_host.c 6381 487 15 985 7579 +0.0%
dcd_ch32_usbfs.c 1582 0 0 2444 4026 +0.0%
dcd_ch32_usbhs.c 1889 0 0 481 2370 +0.0%
dcd_ci_fs.c 1924 0 0 1290 3214 +0.0%
dcd_ci_hs.c 1756 0 0 1344 2534 +0.0%
dcd_da146xx.c 3067 0 0 144 3211 +0.0%
dcd_dwc2.c 4263 19 0 265 4546 +0.0%
dcd_eptri.c 2273 0 0 259 2532 +0.0%
dcd_ft9xx.c 3284 0 0 172 3456 +0.0%
dcd_khci.c 1952 0 0 1290 3242 +0.0%
dcd_lpc17_40.c 1481 0 0 648 1805 +0.0%
dcd_lpc_ip3511.c 1463 0 0 264 1683 +0.0%
dcd_mm32f327x_otg.c 1477 0 0 1290 2767 +0.0%
dcd_msp430x5xx.c 1801 0 0 176 1977 +0.0%
dcd_musb.c 2225 0 0 171 2396 +0.0%
dcd_nrf5x.c 2939 0 0 292 3231 +0.0%
dcd_nuc120.c 1096 0 0 78 1174 +0.0%
dcd_nuc121.c 1170 0 0 101 1270 +0.0%
dcd_nuc505.c 0 0 1533 157 1690 +0.0%
dcd_rp2040.c 840 0 764 653 2257 +0.0%
dcd_rusb2.c 2918 0 0 156 3074 +0.0%
dcd_samd.c 1036 0 0 266 1302 +0.0%
dcd_samg.c 1322 0 0 72 1394 +0.0%
dfu_device.c 777 28 712 138 914 +0.0%
dfu_rt_device.c 157 0 134 0 157 +0.0%
dwc2_common.c 603 22 0 0 615 +0.0%
ecm_rndis_device.c 1059 0 1 2759 3818 +0.0%
ehci.c 2763 0 0 6274 7783 +0.0%
fsdev_common.c 180 0 0 0 180 +0.0%
hcd_ch32_usbfs.c 2489 0 0 502 2991 +0.0%
hcd_ci_hs.c 181 0 0 0 181 +0.0%
hcd_dwc2.c 5014 25 1 513 5553 +0.0%
hcd_khci.c 2443 0 0 454 2897 +0.0%
hcd_musb.c 3071 0 0 157 3228 +0.0%
hcd_pio_usb.c 262 0 240 0 502 +0.0%
hcd_rp2040.c 1996 17 4 321 2338 +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 3257 0 1 420 3678 +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%
midi2_device.c 2634 52 1175 566 3222 +0.0%
midi2_host.c 1802 0 0 5921 7723 +0.0%
midi_device.c 1151 0 1007 624 1773 +0.0%
midi_host.c 1341 7 7 3635 4979 +0.0%
msc_device.c 2517 108 2281 806 3323 +0.0%
msc_host.c 1638 0 0 394 2033 +0.0%
mtp_device.c 1706 22 739 588 2302 +0.0%
ncm_device.c 1757 28 815 4354 6124 +0.0%
ohci.c 1925 0 0 2503 4428 +0.0%
printer_device.c 830 0 706 566 1394 +0.0%
rp2040_usb.c 386 35 619 11 1051 +0.0%
rusb2_common.c 160 0 16 0 176 +0.0%
tusb.c 455 0 387 3 457 +0.0%
tusb_fifo.c 852 0 486 0 846 +0.0%
typec_stm32.c 820 8 2 12 842 +0.0%
usbc.c 420 2 20 166 608 +0.0%
usbd.c 3519 58 91 355 3936 +0.0%
usbh.c 4662 57 108 1022 5813 +0.0%
usbtmc_device.c 2196 24 68 316 2544 +0.0%
vendor_device.c 641 0 534 565 1204 +0.0%
video_device.c 4443 5 1235 479 4914 +0.0%
TOTAL 121273 1072 17057 51751 174609 +0.0%

Track EP0 control transfer state from SETUP (direction and wLength) and gate CH32 EP0 handshakes accordingly. Switch EP0 type away from control to bulk to block unsolicited OUT ACKs during control read/write gaps, then restore control type when EP0 transfer is explicitly armed.

Signed-off-by: HiFiPhile <admin@hifiphile.com>
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report

Top 10 targets by memory change (%) (out of 2181 targets) View Project Dashboard →

target .text .rodata .data .bss total % diff
ch32v203c_r0_1v0/hid_generic_inout 11,548 → 11,792 (+244) 932 → 936 (+4) 11,852 → 12,096 (+244) +2.1%
ch32v203c_r0_1v0/hid_composite 12,616 → 12,864 (+248) 796 → 800 (+4) 12,920 → 13,168 (+248) +1.9%
ch32v203c_r0_1v0/dfu_runtime 10,376 → 10,620 (+244) 728 → 732 (+4) 13,504 → 13,756 (+252) +1.9%
ch32v203c_r0_1v0/audio_test 14,036 → 14,288 (+252) 1,308 → 1,312 (+4) 14,356 → 14,608 (+252) +1.8%
ch32v203c_r0_1v0/audio_test_multi_rate 14,332 → 14,580 (+248) 2,664 → 2,668 (+4) 14,656 → 14,904 (+248) +1.7%
ch32v203c_r0_1v0/webusb_serial 14,396 → 14,644 (+248) 1,100 → 1,104 (+4) 14,748 → 14,996 (+248) +1.7%
ch32v203c_r0_1v0/usbtmc 14,788 → 15,036 (+248) 1,124 → 1,128 (+4) 15,212 → 15,460 (+248) +1.6%
ch32v203c_r0_1v0/hid_multiple_interface 12,348 → 12,592 (+244) 808 → 812 (+4) 15,556 → 15,808 (+252) +1.6%
ch32v203c_r0_1v0/hid_boot_interface 12,372 → 12,616 (+244) 808 → 812 (+4) 15,580 → 15,832 (+252) +1.6%
ch32v203c_r0_1v0/midi_test 12,828 → 13,072 (+244) 928 → 932 (+4) 16,156 → 16,408 (+252) +1.6%

Devin-goBILDA added a commit to Devin-goBILDA/CH32V203-TinyUSB-DFU-Issue that referenced this pull request May 26, 2026
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