use crate::endpoint::{EndpointAddress, EndpointType};
use crate::host::error::HostResult;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Speed {
Low,
Full,
High,
}
impl Speed {
#[inline]
pub const fn max_bulk_packet(self) -> u16 {
match self {
Self::High => 512,
_ => 64,
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct DeviceAddress(u8);
impl DeviceAddress {
pub const ZERO: Self = Self(0);
pub const FIRST: Self = Self(1);
pub const fn new(value: u8) -> Self {
assert!(value < 0x80, "a USB device address is seven bits wide");
Self(value)
}
#[inline]
pub const fn raw(self) -> u8 {
self.0
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PortStatus {
pub connected: bool,
pub enabled: bool,
pub speed: Speed,
pub overcurrent: bool,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Translator {
pub hub: DeviceAddress,
pub port: u8,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct HubRoute {
pub hub: DeviceAddress,
pub port: u8,
pub translator: Option<Translator>,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Channel(u8);
impl Channel {
pub const fn new(index: u8) -> Self {
assert!(index < 16, "the core implements at most 16 host channels");
Self(index)
}
#[inline]
pub const fn index(self) -> usize {
self.0 as usize
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Toggle {
Data0,
Data1,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct EndpointConfig {
pub address: EndpointAddress,
pub transfer: EndpointType,
pub max_packet: u16,
pub interval: u8,
pub speed: Speed,
pub route: Option<HubRoute>,
}
pub trait HostController {
fn port_status(&self) -> PortStatus;
fn reset_port(&self) -> HostResult<PortStatus>;
fn alloc_channel(&self) -> HostResult<Channel>;
fn free_channel(&self, channel: Channel);
fn configure(
&self,
channel: Channel,
device: DeviceAddress,
endpoint: &EndpointConfig,
) -> HostResult<()>;
fn setup(&self, channel: Channel, packet: &[u8]) -> HostResult<()>;
fn transfer_in(&self, channel: Channel, toggle: Toggle, buffer: &mut [u8])
-> HostResult<usize>;
fn transfer_out(&self, channel: Channel, toggle: Toggle, data: &[u8]) -> HostResult<()>;
fn wait_millis(&self, millis: u32);
}