use core::marker::PhantomData;
use crate::bus::UsbBus;
use crate::error::UsbResult;
pub trait EndpointDir: Sized + 'static {
const DIRECTION: Direction;
}
pub struct In;
impl EndpointDir for In {
const DIRECTION: Direction = Direction::In;
}
pub struct Out;
impl EndpointDir for Out {
const DIRECTION: Direction = Direction::Out;
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Direction {
Out = 0x00,
In = 0x80,
}
impl From<u8> for Direction {
#[inline]
fn from(v: u8) -> Self {
if v & 0x80 != 0 {
Direction::In
} else {
Direction::Out
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum EndpointType {
Control,
Isochronous {
synchronization: IsoSynchronization,
usage: IsoUsage,
},
Bulk,
Interrupt,
}
impl EndpointType {
pub fn bm_attributes(self) -> u8 {
match self {
EndpointType::Control => 0b00,
EndpointType::Bulk => 0b10,
EndpointType::Interrupt => 0b11,
EndpointType::Isochronous {
synchronization,
usage,
} => {
let sync = match synchronization {
IsoSynchronization::None => 0b00,
IsoSynchronization::Asynchronous => 0b01,
IsoSynchronization::Adaptive => 0b10,
IsoSynchronization::Synchronous => 0b11,
};
let usg = match usage {
IsoUsage::Data => 0b00,
IsoUsage::Feedback => 0b01,
IsoUsage::ImplicitFeedbackData => 0b10,
};
0b01 | (sync << 2) | (usg << 4)
}
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum IsoSynchronization {
None,
Asynchronous,
Adaptive,
Synchronous,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum IsoUsage {
Data,
Feedback,
ImplicitFeedbackData,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct EndpointAddress(u8);
impl EndpointAddress {
pub const EP0_OUT: EndpointAddress = EndpointAddress(0x00);
pub const EP0_IN: EndpointAddress = EndpointAddress(0x80);
#[inline]
pub const fn new(number: u8, direction: Direction) -> Self {
Self((number & 0x0f) | direction as u8)
}
#[inline]
pub const fn out(number: u8) -> Self {
Self(number & 0x0f)
}
#[inline]
pub const fn in_(number: u8) -> Self {
Self((number & 0x0f) | 0x80)
}
#[inline]
pub const fn raw(self) -> u8 {
self.0
}
#[inline]
pub const fn number(self) -> u8 {
self.0 & 0x0f
}
#[inline]
pub fn direction(self) -> Direction {
Direction::from(self.0)
}
#[inline]
pub const fn is_in(self) -> bool {
self.0 & 0x80 != 0
}
#[inline]
pub const fn is_out(self) -> bool {
!self.is_in()
}
}
impl From<u8> for EndpointAddress {
#[inline]
fn from(v: u8) -> Self {
Self(v)
}
}
impl From<EndpointAddress> for u8 {
#[inline]
fn from(a: EndpointAddress) -> Self {
a.0
}
}
impl core::fmt::Debug for EndpointAddress {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"EP{} {}",
self.number(),
if self.is_in() { "IN" } else { "OUT" }
)
}
}
#[cfg(feature = "defmt")]
impl defmt::Format for EndpointAddress {
fn format(&self, f: defmt::Formatter) {
defmt::write!(
f,
"EP{} {}",
self.number(),
if self.is_in() { "IN" } else { "OUT" }
);
}
}
pub struct Endpoint<'a, B: UsbBus, D: EndpointDir> {
bus: &'a B,
address: EndpointAddress,
ep_type: EndpointType,
max_packet_size: u16,
interval: u8,
_dir: PhantomData<fn() -> D>,
}
impl<'a, B: UsbBus, D: EndpointDir> Endpoint<'a, B, D> {
pub(crate) fn new(
bus: &'a B,
address: EndpointAddress,
ep_type: EndpointType,
max_packet_size: u16,
interval: u8,
) -> Self {
Self {
bus,
address,
ep_type,
max_packet_size,
interval,
_dir: PhantomData,
}
}
#[inline]
pub fn address(&self) -> EndpointAddress {
self.address
}
#[inline]
pub fn ep_type(&self) -> EndpointType {
self.ep_type
}
#[inline]
pub fn max_packet_size(&self) -> u16 {
self.max_packet_size
}
#[inline]
pub fn interval(&self) -> u8 {
self.interval
}
pub fn stall(&self) {
self.bus.set_stalled(self.address, true);
}
pub fn unstall(&self) {
self.bus.set_stalled(self.address, false);
}
}
pub type EndpointIn<'a, B> = Endpoint<'a, B, In>;
pub type EndpointOut<'a, B> = Endpoint<'a, B, Out>;
impl<B: UsbBus> EndpointIn<'_, B> {
pub fn write(&self, data: &[u8]) -> UsbResult<usize> {
self.bus.write(self.address, data)
}
}
impl<B: UsbBus> EndpointOut<'_, B> {
pub fn read(&self, buf: &mut [u8]) -> UsbResult<usize> {
self.bus.read(self.address, buf)
}
}
unsafe impl<B: UsbBus + Sync, D: EndpointDir> Sync for Endpoint<'_, B, D> {}
unsafe impl<B: UsbBus + Send, D: EndpointDir> Send for Endpoint<'_, B, D> {}