258 lines
6.0 KiB
Rust
Raw Normal View History

2021-02-18 21:57:35 -03:00
use core::cell::RefCell;
use core::marker::PhantomData;
use core::pin::Pin;
use usb_device::bus::UsbBus;
use usb_device::class::UsbClass;
use usb_device::device::UsbDevice;
2021-03-18 21:30:35 -03:00
mod cdc_acm;
pub mod usb_serial;
2021-02-18 21:57:35 -03:00
use crate::peripheral::{PeripheralMutex, PeripheralStateUnchecked};
2021-03-18 21:30:35 -03:00
use embassy::interrupt::Interrupt;
use usb_serial::{ReadInterface, UsbSerial, WriteInterface};
/// Marker trait to mark an interrupt to be used with the [`Usb`] abstraction.
pub unsafe trait USBInterrupt: Interrupt + Send {}
2021-03-18 21:30:35 -03:00
pub(crate) struct State<'bus, B, T, I>
where
B: UsbBus,
T: ClassSet<B>,
2021-03-18 21:30:35 -03:00
I: USBInterrupt,
{
2021-02-18 21:57:35 -03:00
device: UsbDevice<'bus, B>,
pub(crate) classes: T,
2021-03-18 21:30:35 -03:00
_interrupt: PhantomData<I>,
2021-02-18 21:57:35 -03:00
}
2021-03-18 21:30:35 -03:00
pub struct Usb<'bus, B, T, I>
where
B: UsbBus,
T: ClassSet<B>,
2021-03-18 21:30:35 -03:00
I: USBInterrupt,
{
2021-02-18 21:57:35 -03:00
// Don't you dare moving out `PeripheralMutex`
2021-03-18 21:30:35 -03:00
inner: RefCell<PeripheralMutex<State<'bus, B, T, I>>>,
2021-02-18 21:57:35 -03:00
}
2021-03-18 21:30:35 -03:00
impl<'bus, B, T, I> Usb<'bus, B, T, I>
2021-02-18 21:57:35 -03:00
where
B: UsbBus,
T: ClassSet<B>,
2021-03-18 21:30:35 -03:00
I: USBInterrupt,
2021-02-18 21:57:35 -03:00
{
2021-03-18 21:30:35 -03:00
pub fn new<S: IntoClassSet<B, T>>(device: UsbDevice<'bus, B>, class_set: S, irq: I) -> Self {
2021-02-18 21:57:35 -03:00
let state = State {
device,
classes: class_set.into_class_set(),
2021-03-18 21:30:35 -03:00
_interrupt: PhantomData,
2021-02-18 21:57:35 -03:00
};
let mutex = PeripheralMutex::new(state, irq);
Self {
inner: RefCell::new(mutex),
}
}
/// # Safety
/// The `UsbDevice` passed to `Self::new` must not be dropped without calling `Drop` on this `Usb` first.
pub unsafe fn start(self: Pin<&mut Self>) {
let this = self.get_unchecked_mut();
2021-02-18 21:57:35 -03:00
let mut mutex = this.inner.borrow_mut();
let mutex = Pin::new_unchecked(&mut *mutex);
2021-02-18 21:57:35 -03:00
// Use inner to register the irq
2021-03-01 21:30:03 -03:00
mutex.register_interrupt();
2021-02-18 21:57:35 -03:00
}
}
2021-03-18 21:30:35 -03:00
impl<'bus, 'c, B, T, I> Usb<'bus, B, T, I>
2021-02-18 21:57:35 -03:00
where
B: UsbBus,
T: ClassSet<B> + SerialState<'bus, 'c, B, Index0>,
2021-03-18 21:30:35 -03:00
I: USBInterrupt,
2021-02-18 21:57:35 -03:00
{
2021-03-18 21:30:35 -03:00
/// Take a serial class that was passed as the first class in a tuple
pub fn take_serial_0<'a>(
2021-02-18 21:57:35 -03:00
self: Pin<&'a Self>,
) -> (
2021-03-18 21:30:35 -03:00
ReadInterface<'a, 'bus, 'c, Index0, B, T, I>,
WriteInterface<'a, 'bus, 'c, Index0, B, T, I>,
2021-02-18 21:57:35 -03:00
) {
let this = self.get_ref();
let r = ReadInterface {
inner: &this.inner,
_buf_lifetime: PhantomData,
_index: PhantomData,
2021-02-18 21:57:35 -03:00
};
let w = WriteInterface {
inner: &this.inner,
_buf_lifetime: PhantomData,
_index: PhantomData,
};
(r, w)
}
}
2021-03-18 21:30:35 -03:00
impl<'bus, 'c, B, T, I> Usb<'bus, B, T, I>
where
B: UsbBus,
T: ClassSet<B> + SerialState<'bus, 'c, B, Index1>,
2021-03-18 21:30:35 -03:00
I: USBInterrupt,
{
2021-03-18 21:30:35 -03:00
/// Take a serial class that was passed as the second class in a tuple
pub fn take_serial_1<'a>(
self: Pin<&'a Self>,
) -> (
2021-03-18 21:30:35 -03:00
ReadInterface<'a, 'bus, 'c, Index1, B, T, I>,
WriteInterface<'a, 'bus, 'c, Index1, B, T, I>,
) {
let this = self.get_ref();
let r = ReadInterface {
inner: &this.inner,
_buf_lifetime: PhantomData,
_index: PhantomData,
};
let w = WriteInterface {
inner: &this.inner,
_buf_lifetime: PhantomData,
_index: PhantomData,
2021-02-18 21:57:35 -03:00
};
(r, w)
}
}
// SAFETY: The safety contract of `PeripheralStateUnchecked` is forwarded to `Usb::start`.
unsafe impl<'bus, B, T, I> PeripheralStateUnchecked for State<'bus, B, T, I>
2021-02-18 21:57:35 -03:00
where
B: UsbBus,
T: ClassSet<B>,
2021-03-18 21:30:35 -03:00
I: USBInterrupt,
2021-02-18 21:57:35 -03:00
{
2021-03-18 21:30:35 -03:00
type Interrupt = I;
2021-02-18 21:57:35 -03:00
fn on_interrupt(&mut self) {
self.classes.poll_all(&mut self.device);
}
}
pub trait ClassSet<B: UsbBus>: Send {
2021-02-18 21:57:35 -03:00
fn poll_all(&mut self, device: &mut UsbDevice<'_, B>) -> bool;
}
pub trait IntoClassSet<B: UsbBus, C: ClassSet<B>> {
fn into_class_set(self) -> C;
}
pub struct ClassSet1<B, C1>
where
B: UsbBus,
C1: UsbClass<B>,
{
class: C1,
2021-02-18 21:57:35 -03:00
_bus: PhantomData<B>,
}
pub struct ClassSet2<B, C1, C2>
2021-02-18 21:57:35 -03:00
where
B: UsbBus,
C1: UsbClass<B>,
C2: UsbClass<B>,
{
class1: C1,
class2: C2,
_bus: PhantomData<B>,
}
2021-03-18 21:30:35 -03:00
/// The first class into a [`ClassSet`]
pub struct Index0;
2021-03-18 21:30:35 -03:00
/// The second class into a [`ClassSet`]
pub struct Index1;
impl<B, C1> ClassSet<B> for ClassSet1<B, C1>
where
B: UsbBus + Send,
C1: UsbClass<B> + Send,
2021-02-18 21:57:35 -03:00
{
fn poll_all(&mut self, device: &mut UsbDevice<'_, B>) -> bool {
device.poll(&mut [&mut self.class])
}
}
impl<B, C1, C2> ClassSet<B> for ClassSet2<B, C1, C2>
where
B: UsbBus + Send,
C1: UsbClass<B> + Send,
C2: UsbClass<B> + Send,
{
fn poll_all(&mut self, device: &mut UsbDevice<'_, B>) -> bool {
device.poll(&mut [&mut self.class1, &mut self.class2])
}
}
impl<B, C1> IntoClassSet<B, ClassSet1<B, C1>> for C1
where
B: UsbBus + Send,
C1: UsbClass<B> + Send,
{
fn into_class_set(self) -> ClassSet1<B, C1> {
2021-02-18 21:57:35 -03:00
ClassSet1 {
class: self,
_bus: PhantomData,
}
}
}
impl<B, C1, C2> IntoClassSet<B, ClassSet2<B, C1, C2>> for (C1, C2)
where
B: UsbBus + Send,
C1: UsbClass<B> + Send,
C2: UsbClass<B> + Send,
{
fn into_class_set(self) -> ClassSet2<B, C1, C2> {
ClassSet2 {
class1: self.0,
class2: self.1,
_bus: PhantomData,
}
}
}
2021-03-18 21:30:35 -03:00
/// Trait for a USB State that has a serial class inside
pub trait SerialState<'bus, 'a, B: UsbBus, I> {
2021-02-18 21:57:35 -03:00
fn get_serial(&mut self) -> &mut UsbSerial<'bus, 'a, B>;
}
impl<'bus, 'a, B: UsbBus> SerialState<'bus, 'a, B, Index0>
for ClassSet1<B, UsbSerial<'bus, 'a, B>>
{
2021-02-18 21:57:35 -03:00
fn get_serial(&mut self) -> &mut UsbSerial<'bus, 'a, B> {
&mut self.class
}
}
impl<'bus, 'a, B, C2> SerialState<'bus, 'a, B, Index0> for ClassSet2<B, UsbSerial<'bus, 'a, B>, C2>
where
B: UsbBus,
C2: UsbClass<B>,
{
fn get_serial(&mut self) -> &mut UsbSerial<'bus, 'a, B> {
&mut self.class1
}
}
impl<'bus, 'a, B, C1> SerialState<'bus, 'a, B, Index1> for ClassSet2<B, C1, UsbSerial<'bus, 'a, B>>
where
B: UsbBus,
C1: UsbClass<B>,
{
fn get_serial(&mut self) -> &mut UsbSerial<'bus, 'a, B> {
&mut self.class2
}
}