Change ClassSet indexing and tune up example
Example doesn't work with F401 because it doesn't have enough usb endpoints
This commit is contained in:
@ -10,12 +10,20 @@ use crate::interrupt;
|
||||
use crate::usb_serial::{ReadInterface, UsbSerial, WriteInterface};
|
||||
use crate::util::peripheral::{PeripheralMutex, PeripheralState};
|
||||
|
||||
pub struct State<'bus, B: UsbBus, T: ClassSet<B>> {
|
||||
pub struct State<'bus, B, T>
|
||||
where
|
||||
B: UsbBus,
|
||||
T: ClassSet<B>,
|
||||
{
|
||||
device: UsbDevice<'bus, B>,
|
||||
pub(crate) classes: T,
|
||||
}
|
||||
|
||||
pub struct Usb<'bus, B: UsbBus, T: ClassSet<B>> {
|
||||
pub struct Usb<'bus, B, T>
|
||||
where
|
||||
B: UsbBus,
|
||||
T: ClassSet<B>,
|
||||
{
|
||||
// Don't you dare moving out `PeripheralMutex`
|
||||
inner: RefCell<PeripheralMutex<State<'bus, B, T>>>,
|
||||
}
|
||||
@ -53,24 +61,54 @@ where
|
||||
impl<'bus, 'c, B, T> Usb<'bus, B, T>
|
||||
where
|
||||
B: UsbBus,
|
||||
T: ClassSet<B> + SerialState<'bus, 'c, B>,
|
||||
T: ClassSet<B> + SerialState<'bus, 'c, B, Index0>,
|
||||
{
|
||||
pub fn take_serial<'a>(
|
||||
pub fn take_serial_0<'a>(
|
||||
self: Pin<&'a Self>,
|
||||
) -> (
|
||||
ReadInterface<'a, 'bus, 'c, B, T>,
|
||||
WriteInterface<'a, 'bus, 'c, B, T>,
|
||||
ReadInterface<'a, 'bus, 'c, Index0, B, T>,
|
||||
WriteInterface<'a, 'bus, 'c, Index0, B, T>,
|
||||
) {
|
||||
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,
|
||||
};
|
||||
(r, w)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'bus, 'c, B, T> Usb<'bus, B, T>
|
||||
where
|
||||
B: UsbBus,
|
||||
T: ClassSet<B> + SerialState<'bus, 'c, B, Index1>,
|
||||
{
|
||||
pub fn take_serial_1<'a>(
|
||||
self: Pin<&'a Self>,
|
||||
) -> (
|
||||
ReadInterface<'a, 'bus, 'c, Index1, B, T>,
|
||||
WriteInterface<'a, 'bus, 'c, Index1, B, T>,
|
||||
) {
|
||||
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,
|
||||
};
|
||||
(r, w)
|
||||
}
|
||||
@ -95,23 +133,56 @@ pub trait IntoClassSet<B: UsbBus, C: ClassSet<B>> {
|
||||
fn into_class_set(self) -> C;
|
||||
}
|
||||
|
||||
pub struct ClassSet1<B: UsbBus, T: UsbClass<B>> {
|
||||
class: T,
|
||||
pub struct ClassSet1<B, C1>
|
||||
where
|
||||
B: UsbBus,
|
||||
C1: UsbClass<B>,
|
||||
{
|
||||
class: C1,
|
||||
_bus: PhantomData<B>,
|
||||
}
|
||||
|
||||
impl<B, T> ClassSet<B> for ClassSet1<B, T>
|
||||
pub struct ClassSet2<B, C1, C2>
|
||||
where
|
||||
B: UsbBus,
|
||||
T: UsbClass<B>,
|
||||
C1: UsbClass<B>,
|
||||
C2: UsbClass<B>,
|
||||
{
|
||||
class1: C1,
|
||||
class2: C2,
|
||||
_bus: PhantomData<B>,
|
||||
}
|
||||
|
||||
pub struct Index0;
|
||||
pub struct Index1;
|
||||
|
||||
impl<B, C1> ClassSet<B> for ClassSet1<B, C1>
|
||||
where
|
||||
B: UsbBus,
|
||||
C1: UsbClass<B>,
|
||||
{
|
||||
fn poll_all(&mut self, device: &mut UsbDevice<'_, B>) -> bool {
|
||||
device.poll(&mut [&mut self.class])
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: UsbBus, T: UsbClass<B>> IntoClassSet<B, ClassSet1<B, T>> for T {
|
||||
fn into_class_set(self) -> ClassSet1<B, T> {
|
||||
impl<B, C1, C2> ClassSet<B> for ClassSet2<B, C1, C2>
|
||||
where
|
||||
B: UsbBus,
|
||||
C1: UsbClass<B>,
|
||||
C2: UsbClass<B>,
|
||||
{
|
||||
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,
|
||||
C1: UsbClass<B>,
|
||||
{
|
||||
fn into_class_set(self) -> ClassSet1<B, C1> {
|
||||
ClassSet1 {
|
||||
class: self,
|
||||
_bus: PhantomData,
|
||||
@ -119,12 +190,49 @@ impl<B: UsbBus, T: UsbClass<B>> IntoClassSet<B, ClassSet1<B, T>> for T {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SerialState<'bus, 'a, B: UsbBus> {
|
||||
impl<B, C1, C2> IntoClassSet<B, ClassSet2<B, C1, C2>> for (C1, C2)
|
||||
where
|
||||
B: UsbBus,
|
||||
C1: UsbClass<B>,
|
||||
C2: UsbClass<B>,
|
||||
{
|
||||
fn into_class_set(self) -> ClassSet2<B, C1, C2> {
|
||||
ClassSet2 {
|
||||
class1: self.0,
|
||||
class2: self.1,
|
||||
_bus: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SerialState<'bus, 'a, B: UsbBus, I> {
|
||||
fn get_serial(&mut self) -> &mut UsbSerial<'bus, 'a, B>;
|
||||
}
|
||||
|
||||
impl<'bus, 'a, B: UsbBus> SerialState<'bus, 'a, B> for ClassSet1<B, UsbSerial<'bus, 'a, B>> {
|
||||
impl<'bus, 'a, B: UsbBus> SerialState<'bus, 'a, B, Index0>
|
||||
for ClassSet1<B, UsbSerial<'bus, 'a, B>>
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
use core::cell::RefCell;
|
||||
use core::marker::{PhantomData, PhantomPinned};
|
||||
use core::marker::{PhantomData, Unpin};
|
||||
use core::pin::Pin;
|
||||
use core::task::{Context, Poll};
|
||||
|
||||
@ -14,26 +14,39 @@ use crate::usb::{ClassSet, SerialState, State};
|
||||
use crate::util::peripheral::PeripheralMutex;
|
||||
use crate::util::ring_buffer::RingBuffer;
|
||||
|
||||
pub struct ReadInterface<'a, 'bus, 'c, B: UsbBus, T: SerialState<'bus, 'c, B> + ClassSet<B>> {
|
||||
pub struct ReadInterface<'a, 'bus, 'c, I, B, T>
|
||||
where
|
||||
I: Unpin,
|
||||
B: UsbBus,
|
||||
T: SerialState<'bus, 'c, B, I> + ClassSet<B>,
|
||||
{
|
||||
// Don't you dare moving out `PeripheralMutex`
|
||||
pub(crate) inner: &'a RefCell<PeripheralMutex<State<'bus, B, T>>>,
|
||||
pub(crate) _buf_lifetime: PhantomData<&'c T>,
|
||||
pub(crate) _index: PhantomData<I>,
|
||||
}
|
||||
|
||||
/// Write interface for USB CDC_ACM
|
||||
///
|
||||
/// This interface is buffered, meaning that after the write returns the bytes might not be fully
|
||||
/// on the wire just yet
|
||||
pub struct WriteInterface<'a, 'bus, 'c, B: UsbBus, T: SerialState<'bus, 'c, B> + ClassSet<B>> {
|
||||
pub struct WriteInterface<'a, 'bus, 'c, I, B, T>
|
||||
where
|
||||
I: Unpin,
|
||||
B: UsbBus,
|
||||
T: SerialState<'bus, 'c, B, I> + ClassSet<B>,
|
||||
{
|
||||
// Don't you dare moving out `PeripheralMutex`
|
||||
pub(crate) inner: &'a RefCell<PeripheralMutex<State<'bus, B, T>>>,
|
||||
pub(crate) _buf_lifetime: PhantomData<&'c T>,
|
||||
pub(crate) _index: PhantomData<I>,
|
||||
}
|
||||
|
||||
impl<'a, 'bus, 'c, B, T> AsyncBufRead for ReadInterface<'a, 'bus, 'c, B, T>
|
||||
impl<'a, 'bus, 'c, I, B, T> AsyncBufRead for ReadInterface<'a, 'bus, 'c, I, B, T>
|
||||
where
|
||||
I: Unpin,
|
||||
B: UsbBus,
|
||||
T: SerialState<'bus, 'c, B> + ClassSet<B>,
|
||||
T: SerialState<'bus, 'c, B, I> + ClassSet<B>,
|
||||
{
|
||||
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
|
||||
let this = self.get_mut();
|
||||
@ -68,10 +81,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'bus, 'c, B, T> AsyncWrite for WriteInterface<'a, 'bus, 'c, B, T>
|
||||
impl<'a, 'bus, 'c, I, B, T> AsyncWrite for WriteInterface<'a, 'bus, 'c, I, B, T>
|
||||
where
|
||||
I: Unpin,
|
||||
B: UsbBus,
|
||||
T: SerialState<'bus, 'c, B> + ClassSet<B>,
|
||||
T: SerialState<'bus, 'c, B, I> + ClassSet<B>,
|
||||
{
|
||||
fn poll_write(
|
||||
self: Pin<&mut Self>,
|
||||
|
Reference in New Issue
Block a user