rp2040: fix RP2350 hard fault in unaligned_memcpy to USB DPRAM#3585
Merged
rp2040: fix RP2350 hard fault in unaligned_memcpy to USB DPRAM#3585
Conversation
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
Contributor
There was a problem hiding this comment.
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
volatilebyte pointers inunaligned_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>
TinyUSB Average Code Size Metrics
Input files
|
|
| 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% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
volatilebyte accesses inunaligned_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)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. Addingvolatileforces the compiler to emit actual byte load/stores.Test plan
raspberry_pi_pico— verified locally🤖 Generated with Claude Code