summaryrefslogtreecommitdiff
path: root/embassy-hal-common/src/usb/mod.rs
blob: 1fb501d7fb82a6998dc7a4ee8b406a92af4c3c07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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;

mod cdc_acm;
pub mod usb_serial;

use crate::peripheral::{PeripheralMutex, PeripheralState};
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 {}

pub(crate) struct State<'bus, B, T, I>
where
    B: UsbBus,
    T: ClassSet<B>,
    I: USBInterrupt,
{
    device: UsbDevice<'bus, B>,
    pub(crate) classes: T,
    _interrupt: PhantomData<I>,
}

pub struct Usb<'bus, B, T, I>
where
    B: UsbBus,
    T: ClassSet<B>,
    I: USBInterrupt,
{
    // Don't you dare moving out `PeripheralMutex`
    inner: RefCell<PeripheralMutex<State<'bus, B, T, I>>>,
}

impl<'bus, B, T, I> Usb<'bus, B, T, I>
where
    B: UsbBus,
    T: ClassSet<B>,
    I: USBInterrupt,
{
    pub fn new<S: IntoClassSet<B, T>>(device: UsbDevice<'bus, B>, class_set: S, irq: I) -> Self {
        let state = State {
            device,
            classes: class_set.into_class_set(),
            _interrupt: PhantomData,
        };
        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();
        let mut mutex = this.inner.borrow_mut();
        let mutex = Pin::new_unchecked(&mut *mutex);

        // Use inner to register the irq
        // SAFETY: the safety contract of this function makes sure the `UsbDevice` won't be invalidated
        // without the `PeripheralMutex` being dropped.
        mutex.register_interrupt_unchecked();
    }
}

impl<'bus, 'c, B, T, I> Usb<'bus, B, T, I>
where
    B: UsbBus,
    T: ClassSet<B> + SerialState<'bus, 'c, B, Index0>,
    I: USBInterrupt,
{
    /// Take a serial class that was passed as the first class in a tuple
    pub fn take_serial_0<'a>(
        self: Pin<&'a Self>,
    ) -> (
        ReadInterface<'a, 'bus, 'c, Index0, B, T, I>,
        WriteInterface<'a, 'bus, 'c, Index0, 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,
        };
        (r, w)
    }
}

impl<'bus, 'c, B, T, I> Usb<'bus, B, T, I>
where
    B: UsbBus,
    T: ClassSet<B> + SerialState<'bus, 'c, B, Index1>,
    I: USBInterrupt,
{
    /// Take a serial class that was passed as the second class in a tuple
    pub fn take_serial_1<'a>(
        self: Pin<&'a Self>,
    ) -> (
        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,
        };
        (r, w)
    }
}

impl<'bus, B, T, I> PeripheralState for State<'bus, B, T, I>
where
    B: UsbBus,
    T: ClassSet<B>,
    I: USBInterrupt,
{
    type Interrupt = I;
    fn on_interrupt(&mut self) {
        self.classes.poll_all(&mut self.device);
    }
}

pub trait ClassSet<B: UsbBus>: Send {
    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,
    _bus: PhantomData<B>,
}

pub struct ClassSet2<B, C1, C2>
where
    B: UsbBus,
    C1: UsbClass<B>,
    C2: UsbClass<B>,
{
    class1: C1,
    class2: C2,
    _bus: PhantomData<B>,
}

/// The first class into a [`ClassSet`]
pub struct Index0;

/// 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,
{
    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> {
        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,
        }
    }
}

/// Trait for a USB State that has a serial class inside
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, 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
    }
}