Skip to content

Refactor STM32 FSDEV PMA errata delay into shared static inline helper and apply to DCD/HCD with overridable timing#3590

Open
Copilot wants to merge 13 commits intomasterfrom
copilot/extract-hcd-pma-buffer-errata-workaround
Open

Refactor STM32 FSDEV PMA errata delay into shared static inline helper and apply to DCD/HCD with overridable timing#3590
Copilot wants to merge 13 commits intomasterfrom
copilot/extract-hcd-pma-buffer-errata-workaround

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 9, 2026

This change extracts the STM32 FSDEV PMA buffer description table errata workaround from HCD-local logic into shared FSDEV common code, and applies the same workaround path to DCD. It also makes delay timing configurable via macros instead of fixed literals.

  • Errata workaround moved to shared FSDEV common layer

    • Added fsdev_btable_workaround_delay(bool low_speed) as a static inline helper in fsdev_common.h.
    • Centralizes the PMA BTable post-CTR delay used before reading PMA count/data.
  • DCD now uses the same workaround path

    • Replaced the inline CTR_RX delay loop in dcd_stm32_fsdev.c with the shared helper.
    • Keeps behavior aligned between host/device paths on 32-bit STM32 FSDEV variants.
  • HCD duplication removed

    • Replaced HCD inline PMA delay logic in channel_get_rx_count() with the shared helper.
    • Kept DCON stabilization wait separate from errata handling; only errata logic was refactored.
  • Delay count is now overridable

    • Added configurable delay macros:
      • CFG_TUSB_FSDEV_BTABLE_FS_DELAY_COUNT
      • CFG_TUSB_FSDEV_BTABLE_LS_DELAY_COUNT
    • Defaults are derived from FSDEV_STM32_CPU_MHZ when not explicitly configured.
  • STM32 CPU MHz mapping consolidated

    • Added FSDEV_STM32_CPU_MHZ defaults in fsdev_stm32.h for STM32 FSDEV DRD families.
    • Includes STM32U0 default and compile-time guards for unsupported 32-bit STM32 FSDEV targets unless delays are explicitly overridden.
// Shared errata workaround usage (HCD path)
const bool is_low_speed = (FSDEV_REG->ISTR & U_ISTR_LS_DCONN) || (ch_reg & U_EP_LSEP);
fsdev_btable_workaround_delay(is_low_speed);
return btable_get_count(ch_id, BTABLE_BUF_RX);

Copilot AI and others added 8 commits April 9, 2026 10:35
Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Agent-Logs-Url: https://github.com/hathach/tinyusb/sessions/3ec0d5b6-cb8e-48ff-8606-3371beb1efcb

Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Agent-Logs-Url: https://github.com/hathach/tinyusb/sessions/3ec0d5b6-cb8e-48ff-8606-3371beb1efcb

Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Agent-Logs-Url: https://github.com/hathach/tinyusb/sessions/3ec0d5b6-cb8e-48ff-8606-3371beb1efcb

Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 9, 2026

TinyUSB Average Code Size Metrics

File .text .rodata .data .bss size %
ehci.c 2763 0 0 6043 7597 4.5%
cdc_host.c 6381 487 15 985 7579 4.5%
ncm_device.c 1538 28 718 5843 7395 4.4%
usbh.c 4652 55 99 1034 5807 3.5%
hcd_dwc2.c 5007 25 1 513 5545 3.3%
midi_host.c 1341 7 7 3635 4979 3.0%
video_device.c 4443 5 1235 479 4914 2.9%
dcd_dwc2.c 4237 19 0 265 4521 2.7%
audio_device.c 2896 0 1259 1625 4515 2.7%
ohci.c 1940 0 0 2414 4353 2.6%
dcd_ch32_usbfs.c 1473 0 0 2444 3917 2.3%
ecm_rndis_device.c 1037 0 1 2858 3896 2.3%
hcd_stm32_fsdev.c 3259 0 1 420 3680 2.2%
usbd.c 3224 57 88 275 3564 2.1%
dcd_ft9xx.c 3276 0 0 172 3448 2.1%
dcd_khci.c 1953 0 0 1290 3243 1.9%
hcd_musb.c 3073 0 0 157 3230 1.9%
dcd_ci_fs.c 1925 0 0 1290 3215 1.9%
dcd_da146xx.c 3067 0 0 144 3211 1.9%
dcd_nrf5x.c 2918 0 0 292 3210 1.9%
hcd_rusb2.c 2923 0 0 245 3168 1.9%
dcd_rusb2.c 2919 0 0 156 3075 1.8%
msc_device.c 2525 108 2286 547 3071 1.8%
hcd_ch32_usbfs.c 2485 0 0 498 2983 1.8%
hcd_khci.c 2442 0 0 449 2891 1.7%
dcd_stm32_fsdev.c 2558 0 0 291 2849 1.7%
dcd_mm32f327x_otg.c 1478 0 0 1290 2768 1.7%
dcd_musb.c 2431 0 0 160 2591 1.6%
usbtmc_device.c 2196 24 68 316 2544 1.5%
hcd_samd.c 2220 0 0 324 2544 1.5%
dcd_ci_hs.c 1759 0 0 1344 2538 1.5%
dcd_eptri.c 2271 0 0 259 2530 1.5%
hid_host.c 1240 0 0 1251 2491 1.5%
hcd_rp2040.c 2000 17 4 321 2342 1.4%
mtp_device.c 1696 22 735 588 2292 1.4%
dcd_rp2040.c 842 0 764 653 2259 1.4%
msc_host.c 1587 0 0 394 1982 1.2%
dcd_msp430x5xx.c 1798 0 0 176 1974 1.2%
cdc_device.c 1253 16 1106 684 1935 1.2%
dcd_ch32_usbhs.c 1469 0 0 448 1917 1.1%
dcd_lpc17_40.c 1474 0 0 648 1798 1.1%
midi_device.c 1151 0 1007 624 1773 1.1%
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.8%
printer_device.c 830 0 706 566 1394 0.8%
dcd_samg.c 1320 0 0 72 1392 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 1125 44 997 119 1244 0.7%
vendor_device.c 641 0 534 565 1204 0.7%
dcd_nuc120.c 1094 0 0 78 1172 0.7%
rp2040_usb.c 369 35 644 11 1059 0.6%
dfu_device.c 777 28 712 140 916 0.5%
typec_stm32.c 820 8 2 12 842 0.5%
tusb_fifo.c 846 0 480 0 841 0.5%
usbd_control.c 538 0 484 79 616 0.4%
dwc2_common.c 603 22 0 0 615 0.4%
usbc.c 420 2 20 166 608 0.4%
hcd_pio_usb.c 262 0 240 0 502 0.3%
tusb.c 451 0 383 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 157 0 134 0 157 0.1%
TOTAL 118946 1017 16285 46473 167047 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-ch32f205r-r0/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-mm900evxb/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

@copilot make it static inline

Copilot AI and others added 2 commits April 10, 2026 10:53
Agent-Logs-Url: https://github.com/hathach/tinyusb/sessions/8588a09c-d4e5-4284-8944-47ae4e2a2a1f

Co-authored-by: HiFiPhile <4375114+HiFiPhile@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 10, 2026

@copilot make it static inline

Updated: fsdev_btable_workaround_delay() is now static inline in fsdev_common.h, and the out-of-line definition in fsdev_common.c was removed. Commit: 54e1973.

Copilot AI changed the title Refactor STM32 FSDEV PMA errata delay into shared helper and apply to DCD/HCD with overridable timing Refactor STM32 FSDEV PMA errata delay into shared static inline helper and apply to DCD/HCD with overridable timing Apr 10, 2026
Copilot AI requested a review from HiFiPhile April 10, 2026 10:55
@HiFiPhile HiFiPhile marked this pull request as ready for review April 10, 2026 11:00
Copilot AI review requested due to automatic review settings April 10, 2026 11:00
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 refactors the STM32 FSDEV 32-bit PMA Buffer Description Table (BTable) errata delay workaround into shared FSDEV common code, reusing it from both host (HCD) and device (DCD) paths while making the delay timing configurable via macros.

Changes:

  • Introduced a shared fsdev_btable_workaround_delay(bool low_speed) static-inline helper and default delay-count macros in fsdev_common.h.
  • Updated STM32 FSDEV HCD/DCD code paths to call the shared helper instead of duplicating delay loops.
  • Consolidated STM32 max-CPU-MHz defaults into fsdev_stm32.h and switched HCD’s stabilization delay to use FSDEV_STM32_CPU_MHZ.

Reviewed changes

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

File Description
src/portable/st/stm32_fsdev/hcd_stm32_fsdev.c Removes local CPU MHz + delay loop duplication; uses shared BTable errata delay helper and shared CPU MHz macro.
src/portable/st/stm32_fsdev/fsdev_stm32.h Adds FSDEV_STM32_CPU_MHZ defaults for several STM32 FSDEV DRD families (incl. STM32U0).
src/portable/st/stm32_fsdev/fsdev_common.h Adds overridable delay-count macros and the shared static inline errata delay helper.
src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c Replaces the device-side CTR_RX inline delay loop with the shared helper.

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

Comment thread src/portable/st/stm32_fsdev/fsdev_stm32.h Outdated
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 10, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Signed-off-by: HiFiPhile <admin@hifiphile.com>
@HiFiPhile HiFiPhile force-pushed the copilot/extract-hcd-pma-buffer-errata-workaround branch from 27d91e5 to 35045e0 Compare April 10, 2026 11:36
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 10, 2026

MemBrowse Memory Report

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

Comment thread src/portable/st/stm32_fsdev/hcd_stm32_fsdev.c
@hathach hathach force-pushed the copilot/extract-hcd-pma-buffer-errata-workaround branch from a83f801 to 5540b2a Compare April 17, 2026 12:17
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, thank you

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.

4 participants