Skip to content

Conversation

@rnd-ash
Copy link
Contributor

@rnd-ash rnd-ash commented Nov 20, 2025

Summary

Our current USB Subsystem has a few issues with buffer allocation. This PR attempts to fix them.

Issues

  1. All endpoints share a fixed 2048 byte buffer which cannot be adjusted
  2. Max packet size is tied to allocation size, which means we never utilize SAMx's multi-packet transmit feature, and allows the user to setup invalid packet sizes for the FS USB when simply just trying to have a larger USB endpoint buffer.
  3. Buffer allocation does not handle overflow correctly, resulting in a panic if we attempt to allocate more than 2048 bytes in total.

Addressing the issues

To address (1), I have now added in some feature flags for the HAL, which allows the user to set the global USB endpoint allocation buffer to either 1KB, 2KB, 4KB or 8KB. 2KB remains the default to maintain compatibility (The 2KB feature flag is not required to compile the crate)

To address 2 and 3, and to simplify the USB buffer allocation method, This global buffer is now divided equally by 16 (Number of endpoints on our chips), rather than attempting to be split dynamically, it is also stored in a structure that is 4 byte aligned, to simplify the allocation process.

This PR also makes the USB connection more stable under high loads, since we now utilize the multi-packet RX/TX feature properly, which allows a user to send large bulk data, and the hardware will send it over smaller USB packets, before generating an interrupt once sending is complete. Before, we were limited by the USB packet size to the endpoint buffer size, meaning realistically, an interrupt was generated every 64 bytes being sent or received.

Old implementation

Global USB buffer size 2048
Buffer size per endpoint Dynamic
Number of packets before Tx/Rx interrupt fired 1

New implementation

Feature flag Global USB buffer size Buffer size per endpoint Number of 64 byte packets Tx/Rx before interrupt fires
usb-buffer-1k 1024 bytes 64 bytes 1
usb-buffer-2k (Default) 2048 bytes 128 bytes 2
usb-buffer-4k 4096 bytes 256 bytes 4
usb-buffer-8k 8192 bytes 512 bytes 8

@rnd-ash rnd-ash changed the title Add feature flags for more USB allocator buffer sizes USB allocation rework and simplification Nov 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant