microzig icon indicating copy to clipboard operation
microzig copied to clipboard

RP2xxx USB driver - memcpy issue

Open arkadiuszwojcik opened this issue 10 months ago • 1 comments

There is strange issue in USB driver usb_start_tx function when @memcpy is used.

For Zig 0.14:

  • RP2350 crashes when @memcpy is used but RP2040 is fine
  • RP2040 crashes when rom.memcpy code is used even before zig 0.14 update this was working fine
  • std.mem.copyForwards seems to work for both - but we probably should use it as just temporary solution

related PR: #432

arkadiuszwojcik avatar Apr 17 '25 15:04 arkadiuszwojcik

It looks like this might be an issue with memory alignment. Please take a look here: https://github.com/raspberrypi/pico-sdk/issues/2216#issuecomment-2721525484 and also this comment:

"# beware commpiler memcpy cannot be used for unaligned copies in peripheral space"

in link

The issue might occur when copying memory between CPU space and peripheral space. TinyUSB had to implement a custom byte-by-byte memcpy implementation for the RP2040. The RP2350 should support unaligned memory access, but there might still be some edge cases, so it’s better to use a byte-by-byte implementation as well.

There are two scenarios where we are affected:

  • When we use @memcpy directly in the USB driver (it was replaced by std.mem.copyForwards in some recent commits).
  • When we feed the endpoint FIFO (internally, @memcpy is called for the provided slice)

arkadiuszwojcik avatar May 29 '25 20:05 arkadiuszwojcik