Skip to content

refresh dcd_samx7x#3519

Merged
hathach merged 11 commits intomasterfrom
samx7x_update
Mar 7, 2026
Merged

refresh dcd_samx7x#3519
hathach merged 11 commits intomasterfrom
samx7x_update

Conversation

@HiFiPhile
Copy link
Copy Markdown
Collaborator

  • Fix boards build
  • Add DMA switch CFG_TUD_SAMX7X_DMA_ENABLE and cache maintenance functions
  • Remove buggy FIFO transfer with DMA
  • Reformat code

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 Mar 3, 2026

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_device.c 1325 ➙ 1306 (-19) 16 19 661 ➙ 642 (-19) 1985 ➙ 1947 (-38) -1.9%
printer_device.c 0 ➙ 813 (+813) 0 0 ➙ 694 (+694) 0 ➙ 530 (+530) 0 ➙ 1341 (+1341) n/a
tusb.c 431 ➙ 451 (+20) 0 368 ➙ 384 (+16) 3 433 ➙ 453 (+20) +4.6%
TOTAL 1756 ➙ 2570 (+814) 16 387 ➙ 1097 (+710) 664 ➙ 1175 (+511) 2418 ➙ 3741 (+1323) +54.7%

Changes <1% in size

file .text .rodata .data .bss size % diff
dcd_ch32_usbfs.c 1472 ➙ 1473 (+1) 0 0 2444 3916 ➙ 3917 (+1) +0.0%
dcd_ch32_usbhs.c 1648 ➙ 1649 (+1) 0 0 448 2096 ➙ 2097 (+1) +0.0%
dcd_ci_hs.c 1762 ➙ 1760 (-2) 0 0 1344 2541 ➙ 2538 (-3) -0.1%
dcd_dwc2.c 4174 ➙ 4176 (+2) 25 0 265 4464 ➙ 4465 (+1) +0.0%
dcd_eptri.c 2270 ➙ 2271 (+1) 0 0 259 2529 ➙ 2530 (+1) +0.0%
dcd_lpc17_40.c 1470 ➙ 1474 (+4) 0 0 648 1794 ➙ 1798 (+4) +0.2%
dcd_msp430x5xx.c 1796 ➙ 1798 (+2) 0 0 176 1972 ➙ 1974 (+2) +0.1%
dcd_musb.c 2446 ➙ 2445 (-1) 0 0 160 2606 ➙ 2605 (-1) -0.0%
dcd_nrf5x.c 2919 ➙ 2918 (-1) 0 0 292 3211 ➙ 3210 (-1) -0.0%
dcd_nuc120.c 1093 ➙ 1094 (+1) 0 0 78 1171 ➙ 1172 (+1) +0.1%
dcd_nuc121.c 1167 ➙ 1168 (+1) 0 0 101 1268 ➙ 1269 (+1) +0.1%
dcd_nuc505.c 0 0 1529 ➙ 1531 (+2) 157 1686 ➙ 1688 (+2) +0.1%
dcd_rp2040.c 859 ➙ 858 (-1) 20 604 655 2138 ➙ 2137 (-1) -0.0%
dcd_rusb2.c 2917 ➙ 2919 (+2) 0 0 156 3073 ➙ 3075 (+2) +0.1%
dcd_samd.c 1032 ➙ 1034 (+2) 0 0 266 1298 ➙ 1300 (+2) +0.2%
dcd_samg.c 1319 ➙ 1320 (+1) 0 0 72 1391 ➙ 1392 (+1) +0.1%
rp2040_usb.c 172 75 718 ➙ 717 (-1) 4 969 ➙ 968 (-1) -0.1%
tusb_fifo.c 844 ➙ 848 (+4) 0 477 ➙ 480 (+3) 0 838 ➙ 842 (+4) +0.5%
usbd.c 3192 ➙ 3197 (+5) 57 89 276 ➙ 275 (-1) 3532 ➙ 3537 (+5) +0.1%
usbd_control.c 523 0 474 78 601 ➙ 600 (-1) -0.2%
TOTAL 33075 ➙ 33097 (+22) 177 3891 ➙ 3895 (+4) 7879 ➙ 7878 (-1) 43094 ➙ 43114 (+20) +0.0%
No changes
file .text .rodata .data .bss size % diff
audio_device.c 2850 0 1248 1673 4518 +0.0%
cdc_host.c 6610 487 15 1539 8371 +0.0%
dcd_ci_fs.c 1925 0 0 1290 3215 +0.0%
dcd_da146xx.c 3067 0 0 144 3211 +0.0%
dcd_khci.c 1953 0 0 1290 3243 +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_stm32_fsdev.c 2557 0 0 291 2848 +0.0%
dfu_device.c 744 28 712 183 926 +0.0%
dfu_rt_device.c 156 0 134 0 156 +0.0%
dwc2_common.c 602 30 0 0 618 +0.0%
ecm_rndis_device.c 1037 0 1 2272 3310 +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 1118 44 997 115 1233 +0.0%
hid_host.c 1206 0 0 1250 2456 +0.0%
hub.c 1235 8 8 29 1269 +0.0%
midi_device.c 1129 0 995 589 1716 +0.0%
midi_host.c 1353 7 7 3740 5097 +0.0%
msc_device.c 2518 108 2286 538 3056 +0.0%
msc_host.c 1589 0 0 394 1984 +0.0%
mtp_device.c 1689 22 1449 579 2275 +0.0%
ncm_device.c 1514 28 1408 5830 7358 +0.0%
ohci.c 1942 0 0 2414 4356 +0.0%
rusb2_common.c 160 0 16 0 176 +0.0%
typec_stm32.c 820 8 2 12 842 +0.0%
usbc.c 420 2 20 166 608 +0.0%
usbh.c 4563 55 99 964 5648 +0.0%
usbtmc_device.c 2176 24 69 291 2500 +0.0%
vendor_device.c 624 0 530 464 1087 +0.0%
video_device.c 4391 5 1851 472 4855 +0.0%
TOTAL 78677 962 12505 37116 115714 +0.0%

@hathach
Copy link
Copy Markdown
Owner

hathach commented Mar 5, 2026

@claude review this

@claude
Copy link
Copy Markdown

claude bot commented Mar 5, 2026

Claude finished @hathach's task in 2m 35s —— View job


PR Review: refresh dcd_samx7x

  • Read changed files and git diff
  • Review dcd_samx7x.c changes
  • Review cmake/header changes
  • Post final review

Overall this is a solid cleanup PR. The changes are well-structured. A few issues worth addressing:

Issues Found

1. Dead code / potential out-of-bounds in dcd_edpt_xfer_fifo (dcd_samx7x.c:573-575)

const uint8_t epnum = tu_edpt_number(ep_addr);
// ...
if (epnum == 0x80) {         // ← NEVER true: tu_edpt_number() already strips dir bit
  xfer = &xfer_status[EP_MAX]; // ← out-of-bounds if somehow reached (array is [0..EP_MAX-1])
}

tu_edpt_number() masks off the direction bit, so epnum will always be 0–9 and can never equal 0x80. This block should be removed. Fix this →


2. Wrong register name in dcd_edpt_clear_stall (dcd_samx7x.c:606)

USB_REG->DEVEPTIER[epnum] = HSTPIPIER_RSTDTS;  // ← uses Host pipe name in Device code

Should be DEVEPTIER_RSTDTS. While both likely share the same bit value, using a host register macro in device code is misleading and fragile. Fix this →


3. Cache invalidation may discard adjacent data if address is unaligned (samx7x_common.h)

TU_ATTR_ALWAYS_INLINE static inline bool samx7x_dcache_invalidate(void const* addr, uint32_t data_size) {
  const uintptr_t addr32 = (uintptr_t) addr;
  if (is_cache_mem(addr32)) {
    data_size = round_up_to_cache_line_size(data_size);
    SCB_InvalidateDCache_by_Addr((void*) addr32, (int32_t) data_size);  // addr may be unaligned
  }

SCB_InvalidateDCache_by_Addr rounds the start address down to the nearest cache line boundary internally. If addr is not cache-line aligned, this can inadvertently invalidate data from an adjacent buffer (the partial cache line before addr). The fix is to also round down the start address and increase data_size accordingly, or better, document that DMA buffers must be cache-line aligned (32 bytes for SAME70). The clean functions have the same issue but it's less dangerous there.


Minor Notes

  • dcd_edpt_iso_alloc / dcd_edpt_iso_activate both return false (lines 487-498) — a // TODO comment noting they're not yet implemented would be helpful.
  • #if CFG_TUD_MEM_DCACHE_ENABLE guards around the dcd_dcache_* implementations (lines 81-96) rely on the fact that dcd_dcache_* have weak stubs in the core when disabled. The call at dcd_dma_handler:324 (dcd_dcache_invalidate(...)) has no guard but is safe because of those stubs. Consider adding a comment noting the dependency.

Positive Aspects

  • Defaulting CFG_TUD_SAMX7X_DMA_ENABLE to 0 is correct and avoids breaking existing users.
  • Tying CFG_TUD_MEM_DCACHE_ENABLE_DEFAULT to CFG_TUD_SAMX7X_DMA_ENABLE in tusb_mcu.h:176 is a clean pattern consistent with other drivers (e.g. DWC2).
  • EP_DMA_SUPPORT correctly excludes EP0 and the errata-affected EP7 (epnum >= 1 && epnum <= 6).
  • The uncached_regions mechanism for DTCM is a nice way to avoid unnecessary cache ops on TCM memory.
  • Removing the FIFO+DMA path (buggy) is the right call.

@HiFiPhile HiFiPhile marked this pull request as ready for review March 6, 2026 11:37
Copilot AI review requested due to automatic review settings March 6, 2026 11:37
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

Refreshes the SAMX7X device controller driver and SAME7X BSP configuration to restore board builds and make DMA usage optional (with corresponding cache maintenance helpers), while also removing the prior DMA+FIFO transfer path.

Changes:

  • Add CFG_TUD_SAMX7X_DMA_ENABLE (default off) and gate endpoint DMA support on it.
  • Introduce SAMX7X D-cache maintenance helpers/regions and default-enable D-cache when SAMX7X DMA is enabled.
  • Update SAME7X CMake board configuration (startup/linker file selection and warning handling) and reformat/refactor the DCD.

Reviewed changes

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

Show a summary per file
File Description
src/tusb_option.h Adds SAMX7X-specific DMA config option and FIFO/HWFIFO configuration defaults.
src/portable/microchip/samx7x/samx7x_common.h Gates EP DMA support on new config and adds D-cache maintenance helpers + uncached region list.
src/portable/microchip/samx7x/dcd_samx7x.c Refactors SAMX7X DCD, adds DMA cache maintenance hooks, removes DMA FIFO xfer path, reformats.
src/common/tusb_mcu.h Sets SAMX7X D-cache defaults/line size tied to DMA enable.
hw/bsp/same7x/family.cmake Adjusts warning suppression and startup-file handling for GNU/Clang, uses board-provided startup paths.
hw/bsp/same7x/boards/same70_xplained/board.cmake Defines startup and linker scripts (GNU + IAR) for the Xplained board.
hw/bsp/same7x/boards/same70_qmtech/board.cmake Fixes linker script selection for N19B and defines startup/linker scripts (GNU + IAR).
Comments suppressed due to low confidence (1)

src/portable/microchip/samx7x/samx7x_common.h:2120

  • uncached_regions is never modified, but it is declared non-const. That forces it into RAM (BSS/data) for every translation unit that includes this header. Make the array static const so it can live in flash and reduce RAM usage.

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

HiFiPhile and others added 5 commits March 6, 2026 12:47
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: HiFiPhile <admin@hifiphile.com>
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. I add support for uinique ID and also clean up BSP and add freertos build for same7x

@hathach hathach merged commit d65964e into master Mar 7, 2026
306 of 307 checks passed
@hathach hathach deleted the samx7x_update branch March 7, 2026 08:29
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