Skip to content

Use EP0 buffer directly if it's large enough #3352

Merged
hathach merged 9 commits intomasterfrom
ep0_direct
Mar 11, 2026
Merged

Use EP0 buffer directly if it's large enough #3352
hathach merged 9 commits intomasterfrom
ep0_direct

Conversation

@HiFiPhile
Copy link
Copy Markdown
Collaborator

This pull request improves how control transfer buffers are managed for both the Audio and DFU device classes. The main enhancement is to allow the use of the endpoint 0 buffer for control transfers when it is large enough, falling back to a dedicated buffer only when necessary.

Copilot AI review requested due to automatic review settings November 20, 2025 23:52
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 optimizes control transfer buffer management in TinyUSB by allowing device classes to use the EP0 buffer directly when it's large enough, avoiding unnecessary dedicated buffer allocations and memory copies. This improves memory efficiency on resource-constrained devices while maintaining the same functional behavior.

Key Changes

  • Added usbd_control_get_buffer() API to expose the EP0 buffer to device class implementations
  • Optimized control transfer path to skip unnecessary memory copies when the buffer is already the EP0 buffer
  • Updated Audio and DFU device classes to conditionally use EP0 buffer instead of always allocating dedicated buffers

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/device/usbd_control.c Added function to expose EP0 buffer and optimized copy operations to skip when buffer pointer matches EP0 buffer
src/device/usbd.h Added declaration for usbd_control_get_buffer() function
src/class/dfu/dfu_device.c Conditionally allocates transfer buffer only when larger than EP0, otherwise uses EP0 buffer directly via helper function
src/class/audio/audio_device.c Conditionally allocates control buffer only when larger than EP0, uses EP0 buffer otherwise, and removes length cropping code that relied on dedicated buffer always existing

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

Comment thread src/class/audio/audio_device.c
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented Dec 2, 2025

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>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Dec 14, 2025

TinyUSB Average Code Size Metrics

File .text .rodata .data .bss size %
cdc_host.c 6615 487 15 1498 8325 5.1%
ehci.c 2763 0 0 6043 7597 4.7%
ncm_device.c 1514 28 1408 5830 7358 4.5%
usbh.c 4649 55 99 961 5731 3.5%
hcd_dwc2.c 4994 33 1 513 5540 3.4%
midi_host.c 1351 7 7 3635 4989 3.1%
video_device.c 4394 5 1852 478 4865 3.0%
audio_device.c 2882 0 1260 1616 4493 2.8%
dcd_dwc2.c 4176 25 0 265 4465 2.7%
ohci.c 1940 0 0 2414 4353 2.7%
dcd_ch32_usbfs.c 1473 0 0 2444 3917 2.4%
ecm_rndis_device.c 1037 0 1 2858 3896 2.4%
hcd_stm32_fsdev.c 3287 0 1 420 3708 2.3%
usbd.c 3214 57 89 275 3554 2.2%
dcd_khci.c 1953 0 0 1290 3243 2.0%
hcd_musb.c 3073 0 0 157 3230 2.0%
dcd_ci_fs.c 1925 0 0 1290 3215 2.0%
dcd_da146xx.c 3067 0 0 144 3211 2.0%
dcd_nrf5x.c 2918 0 0 292 3210 2.0%
hcd_rusb2.c 2923 0 0 245 3168 1.9%
dcd_rusb2.c 2919 0 0 156 3075 1.9%
msc_device.c 2517 108 2286 546 3063 1.9%
hcd_ch32_usbfs.c 2484 0 0 498 2982 1.8%
hcd_khci.c 2442 0 0 449 2891 1.8%
dcd_stm32_fsdev.c 2557 0 0 291 2848 1.7%
dcd_mm32f327x_otg.c 1478 0 0 1290 2768 1.7%
dcd_musb.c 2445 0 0 160 2605 1.6%
hcd_samd.c 2220 0 0 324 2544 1.6%
dcd_ci_hs.c 1759 0 0 1344 2538 1.6%
dcd_eptri.c 2271 0 0 259 2530 1.5%
usbtmc_device.c 2176 24 69 294 2502 1.5%
hid_host.c 1206 0 0 1251 2457 1.5%
mtp_device.c 1689 22 1449 587 2283 1.4%
dcd_rp2040.c 858 20 604 655 2137 1.3%
dcd_ch32_usbhs.c 1649 0 0 448 2097 1.3%
msc_host.c 1587 0 0 394 1982 1.2%
dcd_msp430x5xx.c 1798 0 0 176 1974 1.2%
cdc_device.c 1306 16 19 630 1935 1.2%
hcd_rp2040.c 976 73 416 384 1849 1.1%
dcd_lpc17_40.c 1474 0 0 648 1798 1.1%
midi_device.c 1129 0 995 578 1705 1.0%
dcd_nuc505.c 0 0 1531 157 1688 1.0%
dcd_lpc_ip3511.c 1463 0 0 264 1683 1.0%
hub.c 1384 8 8 30 1418 0.9%
dcd_samg.c 1320 0 0 72 1392 0.9%
printer_device.c 813 0 694 519 1330 0.8%
dcd_samd.c 1034 0 0 266 1300 0.8%
dcd_nuc121.c 1168 0 0 101 1269 0.8%
hid_device.c 1118 44 997 119 1236 0.8%
dcd_nuc120.c 1094 0 0 78 1172 0.7%
vendor_device.c 624 0 530 453 1076 0.7%
rp2040_usb.c 172 75 717 4 968 0.6%
dfu_device.c 768 28 712 128 896 0.5%
tusb_fifo.c 848 0 480 0 843 0.5%
typec_stm32.c 820 8 2 12 842 0.5%
dwc2_common.c 602 30 0 0 618 0.4%
usbd_control.c 535 0 484 79 613 0.4%
usbc.c 420 2 20 166 608 0.4%
hcd_pio_usb.c 262 0 240 0 502 0.3%
tusb.c 452 0 384 3 453 0.3%
hcd_ci_hs.c 184 0 0 0 184 0.1%
fsdev_common.c 180 0 0 0 180 0.1%
rusb2_common.c 160 0 16 0 176 0.1%
dfu_rt_device.c 156 0 134 0 156 0.1%
TOTAL 114665 1155 17520 46481 163234 100.0%
Input files
  • cmake-build/cmake-build-adafruit_clue/metrics.json
  • cmake-build/cmake-build-apard32690/metrics.json
  • cmake-build/cmake-build-at32f403a_weact_blackpill/metrics.json
  • cmake-build/cmake-build-at_start_f402/metrics.json
  • cmake-build/cmake-build-at_start_f413/metrics.json
  • cmake-build/cmake-build-at_start_f415/metrics.json
  • cmake-build/cmake-build-at_start_f423/metrics.json
  • cmake-build/cmake-build-at_start_f425/metrics.json
  • cmake-build/cmake-build-at_start_f435/metrics.json
  • cmake-build/cmake-build-at_start_f455/metrics.json
  • cmake-build/cmake-build-b_g474e_dpow1/metrics.json
  • cmake-build/cmake-build-b_u585i_iot2a/metrics.json
  • cmake-build/cmake-build-ch32v103r_r1_1v0/metrics.json
  • cmake-build/cmake-build-ch32v203c_r0_1v0/metrics.json
  • cmake-build/cmake-build-ch32v307v_r1_1v0/metrics.json
  • cmake-build/cmake-build-cynthion_d11/metrics.json
  • cmake-build/cmake-build-da14695_dk_usb/metrics.json
  • cmake-build/cmake-build-double_m33_express/metrics.json
  • cmake-build/cmake-build-ea4088_quickstart/metrics.json
  • cmake-build/cmake-build-ea4357/metrics.json
  • cmake-build/cmake-build-ek_tm4c123gxl/metrics.json
  • cmake-build/cmake-build-feather_stm32f405/metrics.json
  • cmake-build/cmake-build-fomu/metrics.json
  • cmake-build/cmake-build-frdm_k32l2a4s/metrics.json
  • cmake-build/cmake-build-frdm_k64f/metrics.json
  • cmake-build/cmake-build-frdm_kl25z/metrics.json
  • cmake-build/cmake-build-frdm_mcxa153/metrics.json
  • cmake-build/cmake-build-frdm_rw612/metrics.json
  • cmake-build/cmake-build-hpm6750evk2/metrics.json
  • cmake-build/cmake-build-lpcxpresso11u37/metrics.json
  • cmake-build/cmake-build-lpcxpresso1347/metrics.json
  • cmake-build/cmake-build-lpcxpresso1549/metrics.json
  • cmake-build/cmake-build-lpcxpresso1769/metrics.json
  • cmake-build/cmake-build-lpcxpresso18s37/metrics.json
  • cmake-build/cmake-build-lpcxpresso51u68/metrics.json
  • cmake-build/cmake-build-lpcxpresso54114/metrics.json
  • cmake-build/cmake-build-metro_m0_express/metrics.json
  • cmake-build/cmake-build-metro_m4_express/metrics.json
  • cmake-build/cmake-build-metro_m7_1011/metrics.json
  • cmake-build/cmake-build-mm32f327x_mb39/metrics.json
  • cmake-build/cmake-build-msp_exp430f5529lp/metrics.json
  • cmake-build/cmake-build-msp_exp432e401y/metrics.json
  • cmake-build/cmake-build-nutiny_nuc126v/metrics.json
  • cmake-build/cmake-build-nutiny_sdk_nuc120/metrics.json
  • cmake-build/cmake-build-nutiny_sdk_nuc121/metrics.json
  • cmake-build/cmake-build-nutiny_sdk_nuc505/metrics.json
  • cmake-build/cmake-build-portenta_c33/metrics.json
  • cmake-build/cmake-build-raspberry_pi_pico/metrics.json
  • cmake-build/cmake-build-raspberrypi_cm4/metrics.json
  • cmake-build/cmake-build-raspberrypi_zero/metrics.json
  • cmake-build/cmake-build-samg55_xplained/metrics.json
  • cmake-build/cmake-build-sipeed_longan_nano/metrics.json
  • cmake-build/cmake-build-stlinkv3mini/metrics.json
  • cmake-build/cmake-build-stm32c071nucleo/metrics.json
  • cmake-build/cmake-build-stm32f070rbnucleo/metrics.json
  • cmake-build/cmake-build-stm32f103_bluepill/metrics.json
  • cmake-build/cmake-build-stm32f207nucleo/metrics.json
  • cmake-build/cmake-build-stm32f303disco/metrics.json
  • cmake-build/cmake-build-stm32g0b1nucleo/metrics.json
  • cmake-build/cmake-build-stm32h503nucleo/metrics.json
  • cmake-build/cmake-build-stm32h743eval/metrics.json
  • cmake-build/cmake-build-stm32h7s3nucleo/metrics.json
  • cmake-build/cmake-build-stm32l052dap52/metrics.json
  • cmake-build/cmake-build-stm32l412nucleo/metrics.json
  • cmake-build/cmake-build-stm32n6570dk/metrics.json
  • cmake-build/cmake-build-stm32u083cdk/metrics.json
  • cmake-build/cmake-build-stm32wb55nucleo/metrics.json
  • cmake-build/cmake-build-stm32wba_nucleo/metrics.json
  • cmake-build/cmake-build-xmc4500_relax/metrics.json

@HiFiPhile
Copy link
Copy Markdown
Collaborator Author

Or just define a shared EP0 buffer large enough for all classes.

# Conflicts:
#	hw/bsp/same7x/boards/same70_qmtech/board.cmake
#	hw/bsp/same7x/boards/same70_xplained/board.cmake
#	hw/bsp/same7x/family.cmake
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 11, 2026

MemBrowse Memory Report

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

target .text .rodata .data .bss total % diff
xmc4500_relax/audio_test 1,312 → 1,248 (-64) 1,388 → 1,324 (-64) -4.6%
nutiny_nuc126v/audio_test 1,716 → 1,652 (-64) 1,792 → 1,728 (-64) -3.6%
nutiny_sdk_nuc121/audio_test 1,716 → 1,652 (-64) 1,792 → 1,728 (-64) -3.6%
nutiny_sdk_nuc120/audio_test 1,688 → 1,624 (-64) 1,796 → 1,732 (-64) -3.6%
msp_exp432e401y/audio_test 1,644 → 1,580 (-64) 1,828 → 1,764 (-64) -3.5%
mm32f327x_mb39/dfu 10,868 → 10,912 (+44) 2,648 → 2,136 (-512) 16,184 → 15,716 (-468) -2.9%
frdm_k64f/dfu 13,664 → 13,708 (+44) 2,688 → 2,176 (-512) 16,500 → 16,032 (-468) -2.8%
frdm_kl25z/dfu 12,992 → 13,044 (+52) 2,668 → 2,156 (-512) 17,852 → 17,392 (-460) -2.6%
xmc4500_relax/dfu 1,128 → 1,064 (-64) 2,572 → 2,508 (-64) -2.5%
frdm_k32l2a4s/dfu 13,988 → 14,040 (+52) 2,680 → 2,168 (-512) 18,848 → 18,388 (-460) -2.4%

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.

I change extern to usbd_get_ctrl_buf(), it is better to keep isolation between usbd and driver.

@hathach hathach merged commit f245078 into master Mar 11, 2026
400 of 405 checks passed
@hathach hathach deleted the ep0_direct branch March 11, 2026 08:46
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