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 2.0 HS #73

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ license = "MIT"
authors = ["Matti Virkkunen <[email protected]>"]
repository = "https://github.com/mvirkkunen/usb-device"

[dependencies]
embedded-time = "0.12.0"

[dev-dependencies]
rusb = "0.8.0"
rand = "0.6.1"
Expand Down
28 changes: 20 additions & 8 deletions src/bus.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use embedded_time::duration::{Extensions, Generic};

use crate::endpoint::{Endpoint, EndpointAddress, EndpointDirection, EndpointType};
use crate::{Result, UsbDirection, UsbError};
use core::cell::RefCell;
Expand Down Expand Up @@ -41,7 +43,7 @@ pub trait UsbBus: Sync + Sized {
ep_addr: Option<EndpointAddress>,
ep_type: EndpointType,
max_packet_size: u16,
interval: u8,
interval: Generic<u32>,
) -> Result<EndpointAddress>;

/// Enables and initializes the USB peripheral. Soon after enabling the device will be reset, so
Expand Down Expand Up @@ -211,12 +213,12 @@ impl<B: UsbBus> UsbBusAllocator<B> {
///
/// This directly delegates to [`UsbBus::alloc_ep`], so see that method for details. In most
/// cases classes should call the endpoint type specific methods instead.
pub fn alloc<'a, D: EndpointDirection>(
pub fn alloc<D: EndpointDirection>(
&self,
ep_addr: Option<EndpointAddress>,
ep_type: EndpointType,
max_packet_size: u16,
interval: u8,
interval: Generic<u32>,
) -> Result<Endpoint<'_, B, D>> {
self.bus
.borrow_mut()
Expand All @@ -240,8 +242,13 @@ impl<B: UsbBus> UsbBusAllocator<B> {
/// feasibly recoverable.
#[inline]
pub fn control<D: EndpointDirection>(&self, max_packet_size: u16) -> Endpoint<'_, B, D> {
self.alloc(None, EndpointType::Control, max_packet_size, 0)
.expect("alloc_ep failed")
self.alloc(
None,
EndpointType::Control,
max_packet_size,
0u32.milliseconds().into(),
)
.expect("alloc_ep failed")
}

/// Allocates a bulk endpoint.
Expand All @@ -256,8 +263,13 @@ impl<B: UsbBus> UsbBusAllocator<B> {
/// feasibly recoverable.
#[inline]
pub fn bulk<D: EndpointDirection>(&self, max_packet_size: u16) -> Endpoint<'_, B, D> {
self.alloc(None, EndpointType::Bulk, max_packet_size, 0)
.expect("alloc_ep failed")
self.alloc(
None,
EndpointType::Bulk,
max_packet_size,
0u32.milliseconds().into(),
)
.expect("alloc_ep failed")
}

/// Allocates an interrupt endpoint.
Expand All @@ -272,7 +284,7 @@ impl<B: UsbBus> UsbBusAllocator<B> {
pub fn interrupt<D: EndpointDirection>(
&self,
max_packet_size: u16,
interval: u8,
interval: Generic<u32>,
) -> Endpoint<'_, B, D> {
self.alloc(None, EndpointType::Interrupt, max_packet_size, interval)
.expect("alloc_ep failed")
Expand Down
10 changes: 5 additions & 5 deletions src/control_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
self.state = ControlState::Idle;
}

pub fn handle_setup<'p>(&'p mut self) -> Option<Request> {
pub fn handle_setup(&mut self) -> Option<Request> {
let count = match self.ep_out.read(&mut self.buf[..]) {
Ok(count) => count,
Err(UsbError::WouldBlock) => return None,
Expand Down Expand Up @@ -122,10 +122,10 @@ impl<B: UsbBus> ControlPipe<'_, B> {
return Some(req);
}

return None;
None
}

pub fn handle_out<'p>(&'p mut self) -> Option<Request> {
pub fn handle_out(&mut self) -> Option<Request> {
match self.state {
ControlState::DataOut(req) => {
let i = self.i;
Expand Down Expand Up @@ -160,7 +160,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
}
}

return None;
None
}

pub fn handle_in_complete(&mut self) -> bool {
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<B: UsbBus> ControlPipe<'_, B> {
}
};

return false;
false
}

fn write_in_chunk(&mut self) {
Expand Down
13 changes: 10 additions & 3 deletions src/descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use core::convert::TryFrom;

use embedded_time::duration::Milliseconds;
use embedded_time::fixed_point::FixedPoint;

use crate::bus::{InterfaceNumber, StringIndex, UsbBus};
use crate::device;
use crate::endpoint::{Endpoint, EndpointDirection};
Expand Down Expand Up @@ -278,8 +283,10 @@ impl DescriptorWriter<'_> {
endpoint.address().into(), // bEndpointAddress
endpoint.ep_type() as u8, // bmAttributes
mps as u8,
(mps >> 8) as u8, // wMaxPacketSize
endpoint.interval(), // bInterval
(mps >> 8) as u8, // wMaxPacketSize
Milliseconds::<u32>::try_from(endpoint.interval())
.unwrap()
.integer() as u8, // bInterval
],
)?;

Expand Down Expand Up @@ -325,7 +332,7 @@ pub struct BosWriter<'w, 'a: 'w> {
impl<'w, 'a: 'w> BosWriter<'w, 'a> {
pub(crate) fn new(writer: &'w mut DescriptorWriter<'a>) -> Self {
Self {
writer: writer,
writer,
num_caps_mark: None,
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/device.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use embedded_time::duration::*;

use crate::bus::{PollResult, StringIndex, UsbBus, UsbBusAllocator};
use crate::class::{ControlIn, ControlOut, UsbClass};
use crate::control;
Expand Down Expand Up @@ -75,7 +77,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
Some(0x00.into()),
EndpointType::Control,
config.max_packet_size_0 as u16,
0,
0.milliseconds().into(),
)
.expect("failed to alloc control endpoint");

Expand All @@ -84,7 +86,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
Some(0x80.into()),
EndpointType::Control,
config.max_packet_size_0 as u16,
0,
0.milliseconds().into(),
)
.expect("failed to alloc control endpoint");

Expand Down Expand Up @@ -193,14 +195,13 @@ impl<B: UsbBus> UsbDevice<'_, B> {
if (ep_in_complete & 1) != 0 {
let completed = self.control.handle_in_complete();

if !B::QUIRK_SET_ADDRESS_BEFORE_STATUS {
if completed && self.pending_address != 0 {
self.bus.set_device_address(self.pending_address);
self.pending_address = 0;
if !B::QUIRK_SET_ADDRESS_BEFORE_STATUS && ( completed && self.pending_address != 0 ) {
self.bus.set_device_address(self.pending_address);
self.pending_address = 0;

self.device_state = UsbDeviceState::Addressed;
}
self.device_state = UsbDeviceState::Addressed;
}

}

let req = if (ep_setup & 1) != 0 {
Expand Down Expand Up @@ -275,7 +276,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
}
}

return false;
false
}

fn control_in(&mut self, classes: &mut ClassList<'_, B>, req: control::Request) {
Expand Down Expand Up @@ -508,7 +509,7 @@ impl<B: UsbBus> UsbDevice<'_, B> {
classes
.iter()
.filter_map(|cls| cls.get_string(index, lang_id))
.nth(0)
.next()
}
};

Expand Down
12 changes: 7 additions & 5 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use embedded_time::duration::Generic;

use crate::bus::UsbBus;
use crate::{Result, UsbDirection};
use core::marker::PhantomData;
Expand Down Expand Up @@ -53,17 +55,17 @@ pub struct Endpoint<'a, B: UsbBus, D: EndpointDirection> {
address: EndpointAddress,
ep_type: EndpointType,
max_packet_size: u16,
interval: u8,
interval: Generic<u32>,
_marker: PhantomData<D>,
}

impl<B: UsbBus, D: EndpointDirection> Endpoint<'_, B, D> {
pub(crate) fn new<'a>(
bus_ptr: &'a AtomicPtr<B>,
pub(crate) fn new(
bus_ptr: &AtomicPtr<B>,
address: EndpointAddress,
ep_type: EndpointType,
max_packet_size: u16,
interval: u8,
interval: Generic<u32>,
) -> Endpoint<'_, B, D> {
Endpoint {
bus_ptr,
Expand Down Expand Up @@ -100,7 +102,7 @@ impl<B: UsbBus, D: EndpointDirection> Endpoint<'_, B, D> {
}

/// Gets the poll interval for interrupt endpoints.
pub fn interval(&self) -> u8 {
pub fn interval(&self) -> Generic<u32> {
self.interval
}

Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#![no_std]
#![warn(missing_docs)]

use embedded_time::duration::Generic;

/// A USB stack error.
#[derive(Debug)]
pub enum UsbError {
Expand Down Expand Up @@ -207,7 +209,7 @@ fn _ensure_sync() {
_ep_addr: Option<EndpointAddress>,
_ep_type: EndpointType,
_max_packet_size: u16,
_interval: u8,
_interval: Generic<u32>,
) -> Result<EndpointAddress> {
Err(UsbError::EndpointOverflow)
}
Expand Down
9 changes: 6 additions & 3 deletions src/test_class.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(missing_docs)]

use embedded_time::duration::Extensions;

use crate::class_prelude::*;
use crate::descriptor;
use crate::device::{UsbDevice, UsbDeviceBuilder, UsbVidPid};
Expand Down Expand Up @@ -70,8 +72,9 @@ impl<B: UsbBus> TestClass<'_, B> {
iface: alloc.interface(),
ep_bulk_in: alloc.bulk(sizes::BULK_ENDPOINT),
ep_bulk_out: alloc.bulk(sizes::BULK_ENDPOINT),
ep_interrupt_in: alloc.interrupt(sizes::INTERRUPT_ENDPOINT, 1),
ep_interrupt_out: alloc.interrupt(sizes::INTERRUPT_ENDPOINT, 1),
ep_interrupt_in: alloc.interrupt(sizes::INTERRUPT_ENDPOINT, 1u32.milliseconds().into()),
ep_interrupt_out: alloc
.interrupt(sizes::INTERRUPT_ENDPOINT, 1u32.milliseconds().into()),
control_buf: [0; sizes::BUFFER],
bulk_buf: [0; sizes::BUFFER],
interrupt_buf: [0; sizes::BUFFER],
Expand Down Expand Up @@ -105,7 +108,7 @@ impl<B: UsbBus> TestClass<'_, B> {
&'a self,
usb_bus: &'b UsbBusAllocator<B>,
) -> UsbDeviceBuilder<'b, B> {
UsbDeviceBuilder::new(&usb_bus, UsbVidPid(VID, PID))
UsbDeviceBuilder::new(usb_bus, UsbVidPid(VID, PID))
.manufacturer(MANUFACTURER)
.product(PRODUCT)
.serial_number(SERIAL_NUMBER)
Expand Down