diff options
author | Brian Cully <bjc@kublai.com> | 2019-08-02 22:46:31 -0400 |
---|---|---|
committer | Brian Cully <bjc@kublai.com> | 2019-08-02 22:46:31 -0400 |
commit | 024fc1503cb73e538dcd05258a6a63059029a48f (patch) | |
tree | 55f02e523b0f59cc5bd7382df3f5473d3f2f8986 /usb-host/src/lib.rs | |
parent | f560a4e9ce76aca32765409f53b570bf0e3c6b4c (diff) | |
download | samd21-demo-024fc1503cb73e538dcd05258a6a63059029a48f.tar.gz samd21-demo-024fc1503cb73e538dcd05258a6a63059029a48f.zip |
Add traits for host controller and driver.
Diffstat (limited to 'usb-host/src/lib.rs')
-rw-r--r-- | usb-host/src/lib.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/usb-host/src/lib.rs b/usb-host/src/lib.rs new file mode 100644 index 0000000..831a67c --- /dev/null +++ b/usb-host/src/lib.rs @@ -0,0 +1,76 @@ +mod descriptor; +mod setup; + +pub use descriptor::*; +pub use setup::*; + +pub enum Error { + Retry(&'static str), + Permanent(&'static str), +} + +pub trait USBHost { + fn control_transfer( + &mut self, + ep: &mut dyn Endpoint, + bm_request_type: RequestType, + b_request: RequestCode, + w_value: WValue, + w_index: u16, + buf: Option<&mut [u8]>, + ) -> Result<usize, Error>; + + fn in_transfer(&mut self, ep: &mut dyn Endpoint, buf: &mut [u8]) -> Result<usize, Error>; + + fn out_transfer(&mut self, ep: &mut dyn Endpoint, buf: &[u8]) -> Result<usize, Error>; +} + +// cf ยง9.6.6 of USB 2.0 +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum TransferType { + Control = 0, + Isochronous = 1, + Bulk = 2, + Interrupt = 3, +} + +// ibid +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum Direction { + Out, + In, +} +pub trait Endpoint { + fn address(&self) -> u8; + + fn endpoint_num(&self) -> u8; + + fn transfer_type(&self) -> TransferType; + + fn direction(&self) -> Direction; + + fn max_packet_size(&self) -> u16; + + fn in_toggle(&self) -> bool; + + fn set_in_toggle(&mut self, toggle: bool); + + fn out_toggle(&self) -> bool; + + fn set_out_toggle(&mut self, toggle: bool); +} + +pub trait Driver { + fn want_device(&self, device: &DeviceDescriptor) -> bool; + + fn add_device(&mut self, device: DeviceDescriptor, address: u8) -> Result<(), Error>; + + fn remove_device(&mut self, address: u8); + + fn tick_device( + &mut self, + address: u8, + millis: usize, + usbhost: &mut dyn USBHost, + ) -> Result<(), Error>; +} |