Skip to content

rp2040: fix RP2350 hard fault in unaligned_memcpy to USB DPRAM#3585

Merged
hathach merged 2 commits intomasterfrom
fix/rp2350-unaligned-memcpy
Apr 3, 2026
Merged

rp2040: fix RP2350 hard fault in unaligned_memcpy to USB DPRAM#3585
hathach merged 2 commits intomasterfrom
fix/rp2350-unaligned-memcpy

Conversation

@hathach
Copy link
Copy Markdown
Owner

@hathach hathach commented Apr 2, 2026

Summary

  • Use volatile byte accesses in unaligned_memcpy() to prevent the compiler from widening the byte-by-byte loop into 16/32-bit accesses, which cause a hard fault on RP2350 when targeting USB DPRAM (device memory)
  • Simpler alternative to Fix RP2350 TinyUSB USB DPRAM memcpy hard fault #3554 — same root cause fix with minimal code change and zero code size impact

Closes #3554

Detail

On RP2350, the compiler optimizes the plain *dst++ = *src++ loop into wider 16/32-bit accesses for performance. When the destination is USB DPRAM, unaligned wide stores trigger a hard fault. Adding volatile forces the compiler to emit actual byte load/stores.

Test plan

  • Build for raspberry_pi_pico — verified locally
  • CI
  • @ccrome to verify on RP2350 hardware with audio IN transfers

🤖 Generated with Claude Code

Use volatile byte accesses to prevent the compiler from widening
the byte-by-byte copy loop into 16/32-bit accesses, which cause
a hard fault on RP2350 when targeting USB DPRAM (device memory).

Closes #3554
Copilot AI review requested due to automatic review settings April 2, 2026 16:50
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 updates the RP2040 USB port’s unaligned_memcpy() helper to force true byte-sized loads/stores, preventing the compiler from generating unaligned 16/32-bit accesses that can hard-fault on RP2350 when copying to/from USB DPRAM (device memory).

Changes:

  • Add volatile byte pointers in unaligned_memcpy() to inhibit access-width widening optimizations.
  • Clarify the rationale in comments (RP2350 USB DPRAM hard-fault scenario).

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

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

github-actions bot commented Apr 2, 2026

TinyUSB Average Code Size Metrics

File .text .rodata .data .bss size %
cdc_host.c 6617 487 15 1498 8327 5.0%
ehci.c 2763 0 0 6043 7597 4.5%
ncm_device.c 1538 28 718 5843 7395 4.4%
usbh.c 4652 55 99 961 5734 3.4%
hcd_dwc2.c 4994 33 1 513 5540 3.3%
midi_host.c 1341 7 7 3635 4979 3.0%
video_device.c 4443 5 1235 479 4914 2.9%
audio_device.c 2897 0 1260 1627 4518 2.7%
dcd_dwc2.c 4210 25 0 265 4500 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 3287 0 1 420 3708 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 2445 0 0 160 2605 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 838 0 764 655 2257 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 1252 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 623 1772 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 382 35 625 11 1053 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 841 0 480 0 836 0.5%
dwc2_common.c 602 30 0 0 618 0.4%
usbd_control.c 538 0 484 79 616 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 119187 1039 16267 46916 167730 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

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

MemBrowse Memory Report

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

target .text .rodata .data .bss total % diff
raspberry_pi_pico/dfu_runtime 3,132 → 3,148 (+16) 8,200 → 8,216 (+16) +0.2%
raspberry_pi_pico/hid_composite 3,244 → 3,260 (+16) 8,380 → 8,396 (+16) +0.2%
raspberry_pi_pico/hid_generic_inout 3,148 → 3,164 (+16) 8,420 → 8,436 (+16) +0.2%
raspberry_pi_pico/midi_test 3,132 → 3,148 (+16) 8,560 → 8,576 (+16) +0.2%
raspberry_pi_pico/audio_test 3,164 → 3,180 (+16) 8,920 → 8,936 (+16) +0.2%
raspberry_pi_pico/usbtmc 3,524 → 3,540 (+16) 9,300 → 9,316 (+16) +0.2%
raspberry_pi_pico/audio_test_multi_rate 3,164 → 3,180 (+16) 10,372 → 10,388 (+16) +0.2%
raspberry_pi_pico/audio_4_channel_mic 3,132 → 3,148 (+16) 10,648 → 10,664 (+16) +0.2%
raspberry_pi_pico/cdc_uac2 3,164 → 3,180 (+16) 11,412 → 11,428 (+16) +0.1%
raspberry_pi_pico/uac2_headset 3,228 → 3,244 (+16) 14,100 → 14,116 (+16) +0.1%

@hathach hathach merged commit 6252448 into master Apr 3, 2026
310 of 311 checks passed
@hathach hathach deleted the fix/rp2350-unaligned-memcpy branch April 3, 2026 09:54
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