Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USB support #295

Merged
merged 39 commits into from
Jul 7, 2021
Merged

USB support #295

merged 39 commits into from
Jul 7, 2021

Conversation

Disasm
Copy link
Contributor

@Disasm Disasm commented Feb 17, 2021

This PR is based on #144 and previous attempts.

@Disasm
Copy link
Contributor Author

Disasm commented Feb 17, 2021

Current problems:

  • IN packets sometimes get lost. This happens because there is no status tracking implemented for IN endpoints and write never returns WouldBlock.
  • full IN SETUP transactions with size == 0 (mod ep0_size) are not handled correctly. The size of the transaction should be tracked and status stage should be signaled when the transaction finishes (instead of sending a ZLP).
  • backing buffers are no longer used and should be removed.

Copy link
Member

@wez wez left a comment

Choose a reason for hiding this comment

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

Looks like you're making great progress! I got about 60% of the way that you did; I'm pleased to see some similarities with the stuff in my local hack branch, and happy that spending the time on that means that I can more easily reason about your PR!

nrf-hal-common/src/usbd/mod.rs Outdated Show resolved Hide resolved
nrf-hal-common/src/usbd/mod.rs Outdated Show resolved Hide resolved
return true;
}

if ep0_state.direction == UsbDirection::Out {
Copy link
Member

Choose a reason for hiding this comment

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

makes sense!

nrf-hal-common/src/usbd/mod.rs Outdated Show resolved Hide resolved
nrf-hal-common/src/usbd/mod.rs Outdated Show resolved Hide resolved
nrf-hal-common/src/usbd/mod.rs Outdated Show resolved Hide resolved
nrf-hal-common/src/usbd/mod.rs Outdated Show resolved Hide resolved
nrf-hal-common/src/usbd/mod.rs Outdated Show resolved Hide resolved
@Disasm
Copy link
Contributor Author

Disasm commented Feb 26, 2021

Current status: serial stress test works better but still fails. One of the IN packets is not followed by EPDATASTATUS change, so "busy" flag is not erased and driver ends up in a locked state.

This helps in a situation when `poll()` is not called periodically, for
example, when main loop is busy writing data to an endpoint. This
situation happens in the usb_serial example.
@Disasm
Copy link
Contributor Author

Disasm commented Jun 4, 2021

There was a problem in the serial example: it doesn't call pool() while trying to send the whole buffer. Since pool() was the only place where IN endpoints are unblocked, this resulted in an endpoint that was blocked forever. The problem was resolved by checking for the "IN complete" event inside write().

@Disasm Disasm marked this pull request as ready for review June 4, 2021 21:27
@Disasm
Copy link
Contributor Author

Disasm commented Jun 5, 2021

I tested this version of the driver:

  • usb-device tests passed
  • serial test passed
  • almost all USB compliance tests passed: two problems relate to usb-device (alt.settings and LPM are not supported)

Copy link
Contributor

@jonas-schievink jonas-schievink left a comment

Choose a reason for hiding this comment

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

Did a first pass, this looks pretty good so far!

examples/usb/README.md Outdated Show resolved Hide resolved
examples/usb/src/main.rs Outdated Show resolved Hide resolved
nrf-hal-common/Cargo.toml Outdated Show resolved Hide resolved
nrf-hal-common/src/lib.rs Outdated Show resolved Hide resolved
nrf52840-hal/Cargo.toml Outdated Show resolved Hide resolved
nrf-hal-common/src/usbd/mod.rs Outdated Show resolved Hide resolved
regs.events_endepout[i].reset();
dma_end();

// TODO: ISO
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the current behavior when trying to use an ISO endpoint?

nrf-hal-common/src/usbd/mod.rs Outdated Show resolved Hide resolved
Copy link
Member

@Dirbaio Dirbaio left a comment

Choose a reason for hiding this comment

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

Looks great! Excited to finally have USB support in :)

Commented a few minor nits


pub use nrf_usbd::Usbd;

#[allow(dead_code)] // fields are unused and only hold ownership
Copy link
Member

Choose a reason for hiding this comment

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

minor nit: I think if you name the field _clocks the warning goes away

#[allow(dead_code)] // fields are unused and only hold ownership
pub struct UsbPeripheral<'a> {
usbd: USBD,
clocks: &'a (),
Copy link
Member

Choose a reason for hiding this comment

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

&() is not ZST even if () is. Maybe use PhantomData<&'a ()>?

Copy link
Member

@Dirbaio Dirbaio left a comment

Choose a reason for hiding this comment

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

🚀

bors r+

@bors
Copy link
Contributor

bors bot commented Jul 7, 2021

Build succeeded:

@bors bors bot merged commit 5099e8f into nrf-rs:master Jul 7, 2021
@jonas-schievink jonas-schievink mentioned this pull request Jul 7, 2021
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.

5 participants