Skip to content

Conversation

@piotrfila
Copy link
Contributor

This PR is my (yet another) shot at a better USB driver implementation.

Key changes:

  • Multi-function (composite) devices possible
  • Descriptors are automatically constructed at compile time by drivers
  • More type safety around descriptors
  • Supporting other USB controllers as well as multiple drivers per controller (eg. interrupted/polled) and writing new drivers should be much easier
  • Removed a lot of global variables

This is a succesor to #658, but this time I tried to change as little controller logic as possible to avoid bugs.
As last time, I'm open to suggestions.

There is still much work to be done in this area, but this PR is already big. Nice additions would include:

  • Better way of constructing HID descriptors
  • Using the new types in stm32 examples
  • More and better examples (eg. actual HID keyboard/mouse/gamepad, mass storage)

@arkadiuszwojcik
Copy link
Contributor

@piotrfila I will try take a look on this over the weekend. Going back to memcpy issue from previous thread, what is your plan for this? I think other usb libraries expose memcpy operation as part of driver interface. That would require change in FIFO to accept custom memcpy as dependency right?

@piotrfila
Copy link
Contributor Author

I plan to adress that in a separate PR, this one is already pretty big. I just wanted to get the descriptors out of the way first.

Despite the large diffs in the core/usb.zig and rp2xxx/hal/usb.zig the logic is largely unchanged there.

Do you mean the FIFO in the cdc driver? I think that could be retired entirely in favor of std reader/writer, but I have yet to write any code relating to that.

Copy link
Contributor

@arkadiuszwojcik arkadiuszwojcik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work! I put few comments and will have another look tomorrow. Did you test it on both RP2040 a RP2350?

@arkadiuszwojcik
Copy link
Contributor

I made some quick tests, and current behavior is not stable both on RP2040 and RP2350. Here is picture from my terminal and RP2040 test. It also freezes after some time (In my case after 33). For RP2350 output is even more messier. How does it look on your side?

usb_driver

@piotrfila
Copy link
Contributor Author

piotrfila commented Dec 15, 2025

Someone reported it in the discord as well. That bug is also present in the main branch (to be specific the main branch does not work at all - you have to go a few commits back). I'm not quite sure where the bug is, but I suspect the fifo implementation. I'd rather just rewrite the cdc driver and usb device code in a separate PR.

@arkadiuszwojcik
Copy link
Contributor

Sorry, I haven’t checked the code from main for a long time. Yes, it looks like the code doesn’t work at all.

@mattnite @Grazfather, what do you think about it? This code is not 100% correct yet, but it works better than the current code in master (some past PR had to break it). The solution is also much cleaner than the current USB driver code, so it’s a welcome improvement and a step in the right direction.

@mattnite
Copy link
Contributor

@arkadiuszwojcik @piotrfila that works for me, I don't know enough about USB yet to review this properly. I'm happy to merge improvements that are imperfect.

@mattnite mattnite merged commit ed8e54c into ZigEmbeddedGroup:main Dec 19, 2025
48 checks passed
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.

4 participants