aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-12-26 04:31:49 +0100
committerDario Nieuwenhuis <[email protected]>2022-12-26 04:49:08 +0100
commit007246b16036c3aa874e78d0665567a27ab35fa9 (patch)
tree451d75b390e751e2ef6401b0b60c137bcaf92b29
parent1f033d509afb4e590a81896de66af683fda4e706 (diff)
net: split channel-based driver impl from usb cdc-ncm into a separate crate.
-rw-r--r--embassy-net-driver-channel/Cargo.toml12
-rw-r--r--embassy-net-driver-channel/src/fmt.rs225
-rw-r--r--embassy-net-driver-channel/src/lib.rs525
-rw-r--r--embassy-usb/Cargo.toml2
-rw-r--r--embassy-usb/src/class/cdc_ncm/embassy_net.rs408
5 files changed, 787 insertions, 385 deletions
diff --git a/embassy-net-driver-channel/Cargo.toml b/embassy-net-driver-channel/Cargo.toml
new file mode 100644
index 000000000..700a4e8a0
--- /dev/null
+++ b/embassy-net-driver-channel/Cargo.toml
@@ -0,0 +1,12 @@
1[package]
2name = "embassy-net-driver-channel"
3version = "0.1.0"
4edition = "2021"
5
6[dependencies]
7defmt = { version = "0.3", optional = true }
8log = { version = "0.4.14", optional = true }
9
10embassy-sync = { version = "0.1.0", path = "../embassy-sync" }
11embassy-futures = { version = "0.1.0", path = "../embassy-futures" }
12embassy-net-driver = { version = "0.1.0", path = "../embassy-net-driver" }
diff --git a/embassy-net-driver-channel/src/fmt.rs b/embassy-net-driver-channel/src/fmt.rs
new file mode 100644
index 000000000..066970813
--- /dev/null
+++ b/embassy-net-driver-channel/src/fmt.rs
@@ -0,0 +1,225 @@
1#![macro_use]
2#![allow(unused_macros)]
3
4#[cfg(all(feature = "defmt", feature = "log"))]
5compile_error!("You may not enable both `defmt` and `log` features.");
6
7macro_rules! assert {
8 ($($x:tt)*) => {
9 {
10 #[cfg(not(feature = "defmt"))]
11 ::core::assert!($($x)*);
12 #[cfg(feature = "defmt")]
13 ::defmt::assert!($($x)*);
14 }
15 };
16}
17
18macro_rules! assert_eq {
19 ($($x:tt)*) => {
20 {
21 #[cfg(not(feature = "defmt"))]
22 ::core::assert_eq!($($x)*);
23 #[cfg(feature = "defmt")]
24 ::defmt::assert_eq!($($x)*);
25 }
26 };
27}
28
29macro_rules! assert_ne {
30 ($($x:tt)*) => {
31 {
32 #[cfg(not(feature = "defmt"))]
33 ::core::assert_ne!($($x)*);
34 #[cfg(feature = "defmt")]
35 ::defmt::assert_ne!($($x)*);
36 }
37 };
38}
39
40macro_rules! debug_assert {
41 ($($x:tt)*) => {
42 {
43 #[cfg(not(feature = "defmt"))]
44 ::core::debug_assert!($($x)*);
45 #[cfg(feature = "defmt")]
46 ::defmt::debug_assert!($($x)*);
47 }
48 };
49}
50
51macro_rules! debug_assert_eq {
52 ($($x:tt)*) => {
53 {
54 #[cfg(not(feature = "defmt"))]
55 ::core::debug_assert_eq!($($x)*);
56 #[cfg(feature = "defmt")]
57 ::defmt::debug_assert_eq!($($x)*);
58 }
59 };
60}
61
62macro_rules! debug_assert_ne {
63 ($($x:tt)*) => {
64 {
65 #[cfg(not(feature = "defmt"))]
66 ::core::debug_assert_ne!($($x)*);
67 #[cfg(feature = "defmt")]
68 ::defmt::debug_assert_ne!($($x)*);
69 }
70 };
71}
72
73macro_rules! todo {
74 ($($x:tt)*) => {
75 {
76 #[cfg(not(feature = "defmt"))]
77 ::core::todo!($($x)*);
78 #[cfg(feature = "defmt")]
79 ::defmt::todo!($($x)*);
80 }
81 };
82}
83
84macro_rules! unreachable {
85 ($($x:tt)*) => {
86 {
87 #[cfg(not(feature = "defmt"))]
88 ::core::unreachable!($($x)*);
89 #[cfg(feature = "defmt")]
90 ::defmt::unreachable!($($x)*);
91 }
92 };
93}
94
95macro_rules! panic {
96 ($($x:tt)*) => {
97 {
98 #[cfg(not(feature = "defmt"))]
99 ::core::panic!($($x)*);
100 #[cfg(feature = "defmt")]
101 ::defmt::panic!($($x)*);
102 }
103 };
104}
105
106macro_rules! trace {
107 ($s:literal $(, $x:expr)* $(,)?) => {
108 {
109 #[cfg(feature = "log")]
110 ::log::trace!($s $(, $x)*);
111 #[cfg(feature = "defmt")]
112 ::defmt::trace!($s $(, $x)*);
113 #[cfg(not(any(feature = "log", feature="defmt")))]
114 let _ = ($( & $x ),*);
115 }
116 };
117}
118
119macro_rules! debug {
120 ($s:literal $(, $x:expr)* $(,)?) => {
121 {
122 #[cfg(feature = "log")]
123 ::log::debug!($s $(, $x)*);
124 #[cfg(feature = "defmt")]
125 ::defmt::debug!($s $(, $x)*);
126 #[cfg(not(any(feature = "log", feature="defmt")))]
127 let _ = ($( & $x ),*);
128 }
129 };
130}
131
132macro_rules! info {
133 ($s:literal $(, $x:expr)* $(,)?) => {
134 {
135 #[cfg(feature = "log")]
136 ::log::info!($s $(, $x)*);
137 #[cfg(feature = "defmt")]
138 ::defmt::info!($s $(, $x)*);
139 #[cfg(not(any(feature = "log", feature="defmt")))]
140 let _ = ($( & $x ),*);
141 }
142 };
143}
144
145macro_rules! warn {
146 ($s:literal $(, $x:expr)* $(,)?) => {
147 {
148 #[cfg(feature = "log")]
149 ::log::warn!($s $(, $x)*);
150 #[cfg(feature = "defmt")]
151 ::defmt::warn!($s $(, $x)*);
152 #[cfg(not(any(feature = "log", feature="defmt")))]
153 let _ = ($( & $x ),*);
154 }
155 };
156}
157
158macro_rules! error {
159 ($s:literal $(, $x:expr)* $(,)?) => {
160 {
161 #[cfg(feature = "log")]
162 ::log::error!($s $(, $x)*);
163 #[cfg(feature = "defmt")]
164 ::defmt::error!($s $(, $x)*);
165 #[cfg(not(any(feature = "log", feature="defmt")))]
166 let _ = ($( & $x ),*);
167 }
168 };
169}
170
171#[cfg(feature = "defmt")]
172macro_rules! unwrap {
173 ($($x:tt)*) => {
174 ::defmt::unwrap!($($x)*)
175 };
176}
177
178#[cfg(not(feature = "defmt"))]
179macro_rules! unwrap {
180 ($arg:expr) => {
181 match $crate::fmt::Try::into_result($arg) {
182 ::core::result::Result::Ok(t) => t,
183 ::core::result::Result::Err(e) => {
184 ::core::panic!("unwrap of `{}` failed: {:?}", ::core::stringify!($arg), e);
185 }
186 }
187 };
188 ($arg:expr, $($msg:expr),+ $(,)? ) => {
189 match $crate::fmt::Try::into_result($arg) {
190 ::core::result::Result::Ok(t) => t,
191 ::core::result::Result::Err(e) => {
192 ::core::panic!("unwrap of `{}` failed: {}: {:?}", ::core::stringify!($arg), ::core::format_args!($($msg,)*), e);
193 }
194 }
195 }
196}
197
198#[derive(Debug, Copy, Clone, Eq, PartialEq)]
199pub struct NoneError;
200
201pub trait Try {
202 type Ok;
203 type Error;
204 fn into_result(self) -> Result<Self::Ok, Self::Error>;
205}
206
207impl<T> Try for Option<T> {
208 type Ok = T;
209 type Error = NoneError;
210
211 #[inline]
212 fn into_result(self) -> Result<T, NoneError> {
213 self.ok_or(NoneError)
214 }
215}
216
217impl<T, E> Try for Result<T, E> {
218 type Ok = T;
219 type Error = E;
220
221 #[inline]
222 fn into_result(self) -> Self {
223 self
224 }
225}
diff --git a/embassy-net-driver-channel/src/lib.rs b/embassy-net-driver-channel/src/lib.rs
new file mode 100644
index 000000000..369dc5a9d
--- /dev/null
+++ b/embassy-net-driver-channel/src/lib.rs
@@ -0,0 +1,525 @@
1#![no_std]
2
3// must go first!
4mod fmt;
5
6use core::cell::RefCell;
7use core::mem::MaybeUninit;
8use core::task::{Context, Poll};
9
10pub use embassy_net_driver as driver;
11use embassy_net_driver::{Capabilities, LinkState, Medium};
12use embassy_sync::blocking_mutex::raw::NoopRawMutex;
13use embassy_sync::blocking_mutex::Mutex;
14use embassy_sync::waitqueue::WakerRegistration;
15
16pub struct State<const MTU: usize, const N_RX: usize, const N_TX: usize> {
17 rx: [PacketBuf<MTU>; N_RX],
18 tx: [PacketBuf<MTU>; N_TX],
19 inner: MaybeUninit<StateInner<'static, MTU>>,
20}
21
22impl<const MTU: usize, const N_RX: usize, const N_TX: usize> State<MTU, N_RX, N_TX> {
23 const NEW_PACKET: PacketBuf<MTU> = PacketBuf::new();
24
25 pub const fn new() -> Self {
26 Self {
27 rx: [Self::NEW_PACKET; N_RX],
28 tx: [Self::NEW_PACKET; N_TX],
29 inner: MaybeUninit::uninit(),
30 }
31 }
32}
33
34struct StateInner<'d, const MTU: usize> {
35 rx: zerocopy_channel::Channel<'d, NoopRawMutex, PacketBuf<MTU>>,
36 tx: zerocopy_channel::Channel<'d, NoopRawMutex, PacketBuf<MTU>>,
37 link_state: Mutex<NoopRawMutex, RefCell<LinkStateState>>,
38}
39
40/// State of the LinkState
41struct LinkStateState {
42 state: LinkState,
43 waker: WakerRegistration,
44}
45
46pub struct Runner<'d, const MTU: usize> {
47 tx_chan: zerocopy_channel::Receiver<'d, NoopRawMutex, PacketBuf<MTU>>,
48 rx_chan: zerocopy_channel::Sender<'d, NoopRawMutex, PacketBuf<MTU>>,
49 link_state: &'d Mutex<NoopRawMutex, RefCell<LinkStateState>>,
50}
51
52pub struct RxRunner<'d, const MTU: usize> {
53 rx_chan: zerocopy_channel::Sender<'d, NoopRawMutex, PacketBuf<MTU>>,
54 link_state: &'d Mutex<NoopRawMutex, RefCell<LinkStateState>>,
55}
56
57pub struct TxRunner<'d, const MTU: usize> {
58 tx_chan: zerocopy_channel::Receiver<'d, NoopRawMutex, PacketBuf<MTU>>,
59}
60
61impl<'d, const MTU: usize> Runner<'d, MTU> {
62 pub fn split(self) -> (RxRunner<'d, MTU>, TxRunner<'d, MTU>) {
63 (
64 RxRunner {
65 link_state: self.link_state,
66 rx_chan: self.rx_chan,
67 },
68 TxRunner { tx_chan: self.tx_chan },
69 )
70 }
71
72 pub fn set_link_state(&mut self, state: LinkState) {
73 self.link_state.lock(|s| {
74 let s = &mut *s.borrow_mut();
75 s.state = state;
76 s.waker.wake();
77 });
78 }
79
80 pub async fn rx_buf(&mut self) -> &mut [u8] {
81 let p = self.rx_chan.send().await;
82 &mut p.buf
83 }
84
85 pub fn try_rx_buf(&mut self) -> Option<&mut [u8]> {
86 let p = self.rx_chan.try_send()?;
87 Some(&mut p.buf)
88 }
89
90 pub fn poll_rx_buf(&mut self, cx: &mut Context) -> Poll<&mut [u8]> {
91 match self.rx_chan.poll_send(cx) {
92 Poll::Ready(p) => Poll::Ready(&mut p.buf),
93 Poll::Pending => Poll::Pending,
94 }
95 }
96
97 pub fn rx_done(&mut self, len: usize) {
98 let p = self.rx_chan.try_send().unwrap();
99 p.len = len;
100 self.rx_chan.send_done();
101 }
102
103 pub async fn tx_buf(&mut self) -> &mut [u8] {
104 let p = self.tx_chan.recv().await;
105 &mut p.buf[..p.len]
106 }
107
108 pub fn try_tx_buf(&mut self) -> Option<&mut [u8]> {
109 let p = self.tx_chan.try_recv()?;
110 Some(&mut p.buf[..p.len])
111 }
112
113 pub fn poll_tx_buf(&mut self, cx: &mut Context) -> Poll<&mut [u8]> {
114 match self.tx_chan.poll_recv(cx) {
115 Poll::Ready(p) => Poll::Ready(&mut p.buf[..p.len]),
116 Poll::Pending => Poll::Pending,
117 }
118 }
119
120 pub fn tx_done(&mut self) {
121 self.tx_chan.recv_done();
122 }
123}
124
125impl<'d, const MTU: usize> RxRunner<'d, MTU> {
126 pub fn set_link_state(&mut self, state: LinkState) {
127 self.link_state.lock(|s| {
128 let s = &mut *s.borrow_mut();
129 s.state = state;
130 s.waker.wake();
131 });
132 }
133
134 pub async fn rx_buf(&mut self) -> &mut [u8] {
135 let p = self.rx_chan.send().await;
136 &mut p.buf
137 }
138
139 pub fn try_rx_buf(&mut self) -> Option<&mut [u8]> {
140 let p = self.rx_chan.try_send()?;
141 Some(&mut p.buf)
142 }
143
144 pub fn poll_rx_buf(&mut self, cx: &mut Context) -> Poll<&mut [u8]> {
145 match self.rx_chan.poll_send(cx) {
146 Poll::Ready(p) => Poll::Ready(&mut p.buf),
147 Poll::Pending => Poll::Pending,
148 }
149 }
150
151 pub fn rx_done(&mut self, len: usize) {
152 let p = self.rx_chan.try_send().unwrap();
153 p.len = len;
154 self.rx_chan.send_done();
155 }
156}
157
158impl<'d, const MTU: usize> TxRunner<'d, MTU> {
159 pub async fn tx_buf(&mut self) -> &mut [u8] {
160 let p = self.tx_chan.recv().await;
161 &mut p.buf[..p.len]
162 }
163
164 pub fn try_tx_buf(&mut self) -> Option<&mut [u8]> {
165 let p = self.tx_chan.try_recv()?;
166 Some(&mut p.buf[..p.len])
167 }
168
169 pub fn poll_tx_buf(&mut self, cx: &mut Context) -> Poll<&mut [u8]> {
170 match self.tx_chan.poll_recv(cx) {
171 Poll::Ready(p) => Poll::Ready(&mut p.buf[..p.len]),
172 Poll::Pending => Poll::Pending,
173 }
174 }
175
176 pub fn tx_done(&mut self) {
177 self.tx_chan.recv_done();
178 }
179}
180
181pub fn new<'d, const MTU: usize, const N_RX: usize, const N_TX: usize>(
182 state: &'d mut State<MTU, N_RX, N_TX>,
183 ethernet_address: [u8; 6],
184) -> (Runner<'d, MTU>, Device<'d, MTU>) {
185 let mut caps = Capabilities::default();
186 caps.max_transmission_unit = MTU;
187 caps.medium = Medium::Ethernet;
188
189 // safety: this is a self-referential struct, however:
190 // - it can't move while the `'d` borrow is active.
191 // - when the borrow ends, the dangling references inside the MaybeUninit will never be used again.
192 let state_uninit: *mut MaybeUninit<StateInner<'d, MTU>> =
193 (&mut state.inner as *mut MaybeUninit<StateInner<'static, MTU>>).cast();
194 let state = unsafe { &mut *state_uninit }.write(StateInner {
195 rx: zerocopy_channel::Channel::new(&mut state.rx[..]),
196 tx: zerocopy_channel::Channel::new(&mut state.tx[..]),
197 link_state: Mutex::new(RefCell::new(LinkStateState {
198 state: LinkState::Down,
199 waker: WakerRegistration::new(),
200 })),
201 });
202
203 let (rx_sender, rx_receiver) = state.rx.split();
204 let (tx_sender, tx_receiver) = state.tx.split();
205
206 (
207 Runner {
208 tx_chan: tx_receiver,
209 rx_chan: rx_sender,
210 link_state: &state.link_state,
211 },
212 Device {
213 caps,
214 ethernet_address,
215 link_state: &state.link_state,
216 rx: rx_receiver,
217 tx: tx_sender,
218 },
219 )
220}
221
222pub struct PacketBuf<const MTU: usize> {
223 len: usize,
224 buf: [u8; MTU],
225}
226
227impl<const MTU: usize> PacketBuf<MTU> {
228 pub const fn new() -> Self {
229 Self { len: 0, buf: [0; MTU] }
230 }
231}
232
233pub struct Device<'d, const MTU: usize> {
234 rx: zerocopy_channel::Receiver<'d, NoopRawMutex, PacketBuf<MTU>>,
235 tx: zerocopy_channel::Sender<'d, NoopRawMutex, PacketBuf<MTU>>,
236 link_state: &'d Mutex<NoopRawMutex, RefCell<LinkStateState>>,
237 caps: Capabilities,
238 ethernet_address: [u8; 6],
239}
240
241impl<'d, const MTU: usize> embassy_net_driver::Driver for Device<'d, MTU> {
242 type RxToken<'a> = RxToken<'a, MTU> where Self: 'a ;
243 type TxToken<'a> = TxToken<'a, MTU> where Self: 'a ;
244
245 fn receive(&mut self, cx: &mut Context) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
246 if self.rx.poll_recv(cx).is_ready() && self.tx.poll_send(cx).is_ready() {
247 Some((RxToken { rx: self.rx.borrow() }, TxToken { tx: self.tx.borrow() }))
248 } else {
249 None
250 }
251 }
252
253 /// Construct a transmit token.
254 fn transmit(&mut self, cx: &mut Context) -> Option<Self::TxToken<'_>> {
255 if self.tx.poll_send(cx).is_ready() {
256 Some(TxToken { tx: self.tx.borrow() })
257 } else {
258 None
259 }
260 }
261
262 /// Get a description of device capabilities.
263 fn capabilities(&self) -> Capabilities {
264 self.caps.clone()
265 }
266
267 fn ethernet_address(&self) -> [u8; 6] {
268 self.ethernet_address
269 }
270
271 fn link_state(&mut self, cx: &mut Context) -> LinkState {
272 self.link_state.lock(|s| {
273 let s = &mut *s.borrow_mut();
274 s.waker.register(cx.waker());
275 s.state
276 })
277 }
278}
279
280pub struct RxToken<'a, const MTU: usize> {
281 rx: zerocopy_channel::Receiver<'a, NoopRawMutex, PacketBuf<MTU>>,
282}
283
284impl<'a, const MTU: usize> embassy_net_driver::RxToken for RxToken<'a, MTU> {
285 fn consume<R, F>(mut self, f: F) -> R
286 where
287 F: FnOnce(&mut [u8]) -> R,
288 {
289 // NOTE(unwrap): we checked the queue wasn't full when creating the token.
290 let pkt = unwrap!(self.rx.try_recv());
291 let r = f(&mut pkt.buf[..pkt.len]);
292 self.rx.recv_done();
293 r
294 }
295}
296
297pub struct TxToken<'a, const MTU: usize> {
298 tx: zerocopy_channel::Sender<'a, NoopRawMutex, PacketBuf<MTU>>,
299}
300
301impl<'a, const MTU: usize> embassy_net_driver::TxToken for TxToken<'a, MTU> {
302 fn consume<R, F>(mut self, len: usize, f: F) -> R
303 where
304 F: FnOnce(&mut [u8]) -> R,
305 {
306 // NOTE(unwrap): we checked the queue wasn't full when creating the token.
307 let pkt = unwrap!(self.tx.try_send());
308 let r = f(&mut pkt.buf[..len]);
309 pkt.len = len;
310 self.tx.send_done();
311 r
312 }
313}
314
315mod zerocopy_channel {
316 use core::cell::RefCell;
317 use core::future::poll_fn;
318 use core::marker::PhantomData;
319 use core::task::{Context, Poll};
320
321 use embassy_sync::blocking_mutex::raw::RawMutex;
322 use embassy_sync::blocking_mutex::Mutex;
323 use embassy_sync::waitqueue::WakerRegistration;
324
325 pub struct Channel<'a, M: RawMutex, T> {
326 buf: *mut T,
327 phantom: PhantomData<&'a mut T>,
328 state: Mutex<M, RefCell<State>>,
329 }
330
331 impl<'a, M: RawMutex, T> Channel<'a, M, T> {
332 pub fn new(buf: &'a mut [T]) -> Self {
333 let len = buf.len();
334 assert!(len != 0);
335
336 Self {
337 buf: buf.as_mut_ptr(),
338 phantom: PhantomData,
339 state: Mutex::new(RefCell::new(State {
340 len,
341 front: 0,
342 back: 0,
343 full: false,
344 send_waker: WakerRegistration::new(),
345 recv_waker: WakerRegistration::new(),
346 })),
347 }
348 }
349
350 pub fn split(&mut self) -> (Sender<'_, M, T>, Receiver<'_, M, T>) {
351 (Sender { channel: self }, Receiver { channel: self })
352 }
353 }
354
355 pub struct Sender<'a, M: RawMutex, T> {
356 channel: &'a Channel<'a, M, T>,
357 }
358
359 impl<'a, M: RawMutex, T> Sender<'a, M, T> {
360 pub fn borrow(&mut self) -> Sender<'_, M, T> {
361 Sender { channel: self.channel }
362 }
363
364 pub fn try_send(&mut self) -> Option<&mut T> {
365 self.channel.state.lock(|s| {
366 let s = &mut *s.borrow_mut();
367 match s.push_index() {
368 Some(i) => Some(unsafe { &mut *self.channel.buf.add(i) }),
369 None => None,
370 }
371 })
372 }
373
374 pub fn poll_send(&mut self, cx: &mut Context) -> Poll<&mut T> {
375 self.channel.state.lock(|s| {
376 let s = &mut *s.borrow_mut();
377 match s.push_index() {
378 Some(i) => Poll::Ready(unsafe { &mut *self.channel.buf.add(i) }),
379 None => {
380 s.recv_waker.register(cx.waker());
381 Poll::Pending
382 }
383 }
384 })
385 }
386
387 pub async fn send(&mut self) -> &mut T {
388 let i = poll_fn(|cx| {
389 self.channel.state.lock(|s| {
390 let s = &mut *s.borrow_mut();
391 match s.push_index() {
392 Some(i) => Poll::Ready(i),
393 None => {
394 s.recv_waker.register(cx.waker());
395 Poll::Pending
396 }
397 }
398 })
399 })
400 .await;
401 unsafe { &mut *self.channel.buf.add(i) }
402 }
403
404 pub fn send_done(&mut self) {
405 self.channel.state.lock(|s| s.borrow_mut().push_done())
406 }
407 }
408 pub struct Receiver<'a, M: RawMutex, T> {
409 channel: &'a Channel<'a, M, T>,
410 }
411
412 impl<'a, M: RawMutex, T> Receiver<'a, M, T> {
413 pub fn borrow(&mut self) -> Receiver<'_, M, T> {
414 Receiver { channel: self.channel }
415 }
416
417 pub fn try_recv(&mut self) -> Option<&mut T> {
418 self.channel.state.lock(|s| {
419 let s = &mut *s.borrow_mut();
420 match s.pop_index() {
421 Some(i) => Some(unsafe { &mut *self.channel.buf.add(i) }),
422 None => None,
423 }
424 })
425 }
426
427 pub fn poll_recv(&mut self, cx: &mut Context) -> Poll<&mut T> {
428 self.channel.state.lock(|s| {
429 let s = &mut *s.borrow_mut();
430 match s.pop_index() {
431 Some(i) => Poll::Ready(unsafe { &mut *self.channel.buf.add(i) }),
432 None => {
433 s.send_waker.register(cx.waker());
434 Poll::Pending
435 }
436 }
437 })
438 }
439
440 pub async fn recv(&mut self) -> &mut T {
441 let i = poll_fn(|cx| {
442 self.channel.state.lock(|s| {
443 let s = &mut *s.borrow_mut();
444 match s.pop_index() {
445 Some(i) => Poll::Ready(i),
446 None => {
447 s.send_waker.register(cx.waker());
448 Poll::Pending
449 }
450 }
451 })
452 })
453 .await;
454 unsafe { &mut *self.channel.buf.add(i) }
455 }
456
457 pub fn recv_done(&mut self) {
458 self.channel.state.lock(|s| s.borrow_mut().pop_done())
459 }
460 }
461
462 struct State {
463 len: usize,
464
465 /// Front index. Always 0..=(N-1)
466 front: usize,
467 /// Back index. Always 0..=(N-1).
468 back: usize,
469
470 /// Used to distinguish "empty" and "full" cases when `front == back`.
471 /// May only be `true` if `front == back`, always `false` otherwise.
472 full: bool,
473
474 send_waker: WakerRegistration,
475 recv_waker: WakerRegistration,
476 }
477
478 impl State {
479 fn increment(&self, i: usize) -> usize {
480 if i + 1 == self.len {
481 0
482 } else {
483 i + 1
484 }
485 }
486
487 fn is_full(&self) -> bool {
488 self.full
489 }
490
491 fn is_empty(&self) -> bool {
492 self.front == self.back && !self.full
493 }
494
495 fn push_index(&mut self) -> Option<usize> {
496 match self.is_full() {
497 true => None,
498 false => Some(self.back),
499 }
500 }
501
502 fn push_done(&mut self) {
503 assert!(!self.is_full());
504 self.back = self.increment(self.back);
505 if self.back == self.front {
506 self.full = true;
507 }
508 self.send_waker.wake();
509 }
510
511 fn pop_index(&mut self) -> Option<usize> {
512 match self.is_empty() {
513 true => None,
514 false => Some(self.front),
515 }
516 }
517
518 fn pop_done(&mut self) {
519 assert!(!self.is_empty());
520 self.front = self.increment(self.front);
521 self.full = false;
522 self.recv_waker.wake();
523 }
524 }
525}
diff --git a/embassy-usb/Cargo.toml b/embassy-usb/Cargo.toml
index d0742ceb3..1e567bb94 100644
--- a/embassy-usb/Cargo.toml
+++ b/embassy-usb/Cargo.toml
@@ -19,7 +19,7 @@ default = ["usbd-hid"]
19embassy-futures = { version = "0.1.0", path = "../embassy-futures" } 19embassy-futures = { version = "0.1.0", path = "../embassy-futures" }
20embassy-usb-driver = { version = "0.1.0", path = "../embassy-usb-driver" } 20embassy-usb-driver = { version = "0.1.0", path = "../embassy-usb-driver" }
21embassy-sync = { version = "0.1.0", path = "../embassy-sync" } 21embassy-sync = { version = "0.1.0", path = "../embassy-sync" }
22embassy-net-driver = { version = "0.1.0", path = "../embassy-net-driver" } 22embassy-net-driver-channel = { version = "0.1.0", path = "../embassy-net-driver-channel" }
23 23
24defmt = { version = "0.3", optional = true } 24defmt = { version = "0.3", optional = true }
25log = { version = "0.4.14", optional = true } 25log = { version = "0.4.14", optional = true }
diff --git a/embassy-usb/src/class/cdc_ncm/embassy_net.rs b/embassy-usb/src/class/cdc_ncm/embassy_net.rs
index 093afeff9..7ecf693d2 100644
--- a/embassy-usb/src/class/cdc_ncm/embassy_net.rs
+++ b/embassy-usb/src/class/cdc_ncm/embassy_net.rs
@@ -1,81 +1,45 @@
1use core::cell::RefCell;
2use core::mem::MaybeUninit;
3use core::task::Context;
4
5use embassy_futures::select::{select, Either}; 1use embassy_futures::select::{select, Either};
6use embassy_net_driver::{Capabilities, LinkState, Medium}; 2use embassy_net_driver_channel as ch;
7use embassy_sync::blocking_mutex::raw::NoopRawMutex; 3use embassy_net_driver_channel::driver::LinkState;
8use embassy_sync::blocking_mutex::Mutex;
9use embassy_sync::waitqueue::WakerRegistration;
10use embassy_usb_driver::Driver; 4use embassy_usb_driver::Driver;
11 5
12use super::{CdcNcmClass, Receiver, Sender}; 6use super::{CdcNcmClass, Receiver, Sender};
13 7
14pub struct State<'d, const MTU: usize, const N_RX: usize, const N_TX: usize> { 8pub struct State<const MTU: usize, const N_RX: usize, const N_TX: usize> {
15 rx: [PacketBuf<MTU>; N_RX], 9 ch_state: ch::State<MTU, N_RX, N_TX>,
16 tx: [PacketBuf<MTU>; N_TX],
17 inner: MaybeUninit<StateInner<'d, MTU>>,
18} 10}
19 11
20impl<'d, const MTU: usize, const N_RX: usize, const N_TX: usize> State<'d, MTU, N_RX, N_TX> { 12impl<const MTU: usize, const N_RX: usize, const N_TX: usize> State<MTU, N_RX, N_TX> {
21 const NEW_PACKET: PacketBuf<MTU> = PacketBuf::new();
22
23 pub const fn new() -> Self { 13 pub const fn new() -> Self {
24 Self { 14 Self {
25 rx: [Self::NEW_PACKET; N_RX], 15 ch_state: ch::State::new(),
26 tx: [Self::NEW_PACKET; N_TX],
27 inner: MaybeUninit::uninit(),
28 } 16 }
29 } 17 }
30} 18}
31 19
32struct StateInner<'d, const MTU: usize> {
33 rx: zerocopy_channel::Channel<'d, NoopRawMutex, PacketBuf<MTU>>,
34 tx: zerocopy_channel::Channel<'d, NoopRawMutex, PacketBuf<MTU>>,
35 link_state: Mutex<NoopRawMutex, RefCell<LinkStateState>>,
36}
37
38/// State of the LinkState
39struct LinkStateState {
40 state: LinkState,
41 waker: WakerRegistration,
42}
43
44pub struct Runner<'d, D: Driver<'d>, const MTU: usize> { 20pub struct Runner<'d, D: Driver<'d>, const MTU: usize> {
45 tx_usb: Sender<'d, D>, 21 tx_usb: Sender<'d, D>,
46 tx_chan: zerocopy_channel::Receiver<'d, NoopRawMutex, PacketBuf<MTU>>,
47 rx_usb: Receiver<'d, D>, 22 rx_usb: Receiver<'d, D>,
48 rx_chan: zerocopy_channel::Sender<'d, NoopRawMutex, PacketBuf<MTU>>, 23 ch: ch::Runner<'d, MTU>,
49 link_state: &'d Mutex<NoopRawMutex, RefCell<LinkStateState>>,
50} 24}
51 25
52impl<'d, D: Driver<'d>, const MTU: usize> Runner<'d, D, MTU> { 26impl<'d, D: Driver<'d>, const MTU: usize> Runner<'d, D, MTU> {
53 pub async fn run(mut self) -> ! { 27 pub async fn run(mut self) -> ! {
28 let (mut rx_chan, mut tx_chan) = self.ch.split();
54 let rx_fut = async move { 29 let rx_fut = async move {
55 loop { 30 loop {
56 trace!("WAITING for connection"); 31 trace!("WAITING for connection");
57 self.link_state.lock(|s| { 32 rx_chan.set_link_state(LinkState::Down);
58 let s = &mut *s.borrow_mut();
59 s.state = LinkState::Down;
60 s.waker.wake();
61 });
62 33
63 self.rx_usb.wait_connection().await.unwrap(); 34 self.rx_usb.wait_connection().await.unwrap();
64 35
65 trace!("Connected"); 36 trace!("Connected");
66 self.link_state.lock(|s| { 37 rx_chan.set_link_state(LinkState::Up);
67 let s = &mut *s.borrow_mut();
68 s.state = LinkState::Up;
69 s.waker.wake();
70 });
71 38
72 loop { 39 loop {
73 let p = self.rx_chan.send().await; 40 let p = rx_chan.rx_buf().await;
74 match self.rx_usb.read_packet(&mut p.buf).await { 41 match self.rx_usb.read_packet(p).await {
75 Ok(n) => { 42 Ok(n) => rx_chan.rx_done(n),
76 p.len = n;
77 self.rx_chan.send_done();
78 }
79 Err(e) => { 43 Err(e) => {
80 warn!("error reading packet: {:?}", e); 44 warn!("error reading packet: {:?}", e);
81 break; 45 break;
@@ -86,11 +50,11 @@ impl<'d, D: Driver<'d>, const MTU: usize> Runner<'d, D, MTU> {
86 }; 50 };
87 let tx_fut = async move { 51 let tx_fut = async move {
88 loop { 52 loop {
89 let p = self.tx_chan.recv().await; 53 let p = tx_chan.tx_buf().await;
90 if let Err(e) = self.tx_usb.write_packet(&p.buf[..p.len]).await { 54 if let Err(e) = self.tx_usb.write_packet(p).await {
91 warn!("Failed to TX packet: {:?}", e); 55 warn!("Failed to TX packet: {:?}", e);
92 } 56 }
93 self.tx_chan.recv_done(); 57 tx_chan.tx_done();
94 } 58 }
95 }; 59 };
96 match select(rx_fut, tx_fut).await { 60 match select(rx_fut, tx_fut).await {
@@ -100,350 +64,26 @@ impl<'d, D: Driver<'d>, const MTU: usize> Runner<'d, D, MTU> {
100 } 64 }
101} 65}
102 66
67// would be cool to use a TAIT here, but it gives a "may not live long enough". rustc bug?
68//pub type Device<'d, const MTU: usize> = impl embassy_net_driver_channel::driver::Driver + 'd;
69pub type Device<'d, const MTU: usize> = embassy_net_driver_channel::Device<'d, MTU>;
70
103impl<'d, D: Driver<'d>> CdcNcmClass<'d, D> { 71impl<'d, D: Driver<'d>> CdcNcmClass<'d, D> {
104 pub fn into_embassy_net_device<const MTU: usize, const N_RX: usize, const N_TX: usize>( 72 pub fn into_embassy_net_device<const MTU: usize, const N_RX: usize, const N_TX: usize>(
105 self, 73 self,
106 state: &'d mut State<'d, MTU, N_RX, N_TX>, 74 state: &'d mut State<MTU, N_RX, N_TX>,
107 ethernet_address: [u8; 6], 75 ethernet_address: [u8; 6],
108 ) -> (Runner<'d, D, MTU>, Device<'d, MTU>) { 76 ) -> (Runner<'d, D, MTU>, Device<'d, MTU>) {
109 let (tx_usb, rx_usb) = self.split(); 77 let (tx_usb, rx_usb) = self.split();
110 78 let (runner, device) = ch::new(&mut state.ch_state, ethernet_address);
111 let mut caps = Capabilities::default();
112 caps.max_transmission_unit = 1514; // 1500 IP + 14 ethernet header
113 caps.medium = Medium::Ethernet;
114
115 let state = state.inner.write(StateInner {
116 rx: zerocopy_channel::Channel::new(&mut state.rx[..]),
117 tx: zerocopy_channel::Channel::new(&mut state.tx[..]),
118 link_state: Mutex::new(RefCell::new(LinkStateState {
119 state: LinkState::Down,
120 waker: WakerRegistration::new(),
121 })),
122 });
123
124 let (rx_sender, rx_receiver) = state.rx.split();
125 let (tx_sender, tx_receiver) = state.tx.split();
126 79
127 ( 80 (
128 Runner { 81 Runner {
129 tx_usb, 82 tx_usb,
130 tx_chan: tx_receiver,
131 rx_usb, 83 rx_usb,
132 rx_chan: rx_sender, 84 ch: runner,
133 link_state: &state.link_state,
134 },
135 Device {
136 caps,
137 ethernet_address,
138 link_state: &state.link_state,
139 rx: rx_receiver,
140 tx: tx_sender,
141 }, 85 },
86 device,
142 ) 87 )
143 } 88 }
144} 89}
145
146pub struct PacketBuf<const MTU: usize> {
147 len: usize,
148 buf: [u8; MTU],
149}
150
151impl<const MTU: usize> PacketBuf<MTU> {
152 pub const fn new() -> Self {
153 Self { len: 0, buf: [0; MTU] }
154 }
155}
156
157pub struct Device<'d, const MTU: usize> {
158 rx: zerocopy_channel::Receiver<'d, NoopRawMutex, PacketBuf<MTU>>,
159 tx: zerocopy_channel::Sender<'d, NoopRawMutex, PacketBuf<MTU>>,
160 link_state: &'d Mutex<NoopRawMutex, RefCell<LinkStateState>>,
161 caps: Capabilities,
162 ethernet_address: [u8; 6],
163}
164
165impl<'d, const MTU: usize> embassy_net_driver::Driver for Device<'d, MTU> {
166 type RxToken<'a> = RxToken<'a, MTU> where Self: 'a ;
167 type TxToken<'a> = TxToken<'a, MTU> where Self: 'a ;
168
169 fn receive(&mut self, cx: &mut Context) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
170 if self.rx.poll_recv(cx).is_ready() && self.tx.poll_send(cx).is_ready() {
171 Some((RxToken { rx: self.rx.borrow() }, TxToken { tx: self.tx.borrow() }))
172 } else {
173 None
174 }
175 }
176
177 /// Construct a transmit token.
178 fn transmit(&mut self, cx: &mut Context) -> Option<Self::TxToken<'_>> {
179 if self.tx.poll_send(cx).is_ready() {
180 Some(TxToken { tx: self.tx.borrow() })
181 } else {
182 None
183 }
184 }
185
186 /// Get a description of device capabilities.
187 fn capabilities(&self) -> Capabilities {
188 self.caps.clone()
189 }
190
191 fn ethernet_address(&self) -> [u8; 6] {
192 self.ethernet_address
193 }
194
195 fn link_state(&mut self, cx: &mut Context) -> LinkState {
196 self.link_state.lock(|s| {
197 let s = &mut *s.borrow_mut();
198 s.waker.register(cx.waker());
199 s.state
200 })
201 }
202}
203
204pub struct RxToken<'a, const MTU: usize> {
205 rx: zerocopy_channel::Receiver<'a, NoopRawMutex, PacketBuf<MTU>>,
206}
207
208impl<'a, const MTU: usize> embassy_net_driver::RxToken for RxToken<'a, MTU> {
209 fn consume<R, F>(mut self, f: F) -> R
210 where
211 F: FnOnce(&mut [u8]) -> R,
212 {
213 // NOTE(unwrap): we checked the queue wasn't full when creating the token.
214 let pkt = unwrap!(self.rx.try_recv());
215 let r = f(&mut pkt.buf[..pkt.len]);
216 self.rx.recv_done();
217 r
218 }
219}
220
221pub struct TxToken<'a, const MTU: usize> {
222 tx: zerocopy_channel::Sender<'a, NoopRawMutex, PacketBuf<MTU>>,
223}
224
225impl<'a, const MTU: usize> embassy_net_driver::TxToken for TxToken<'a, MTU> {
226 fn consume<R, F>(mut self, len: usize, f: F) -> R
227 where
228 F: FnOnce(&mut [u8]) -> R,
229 {
230 // NOTE(unwrap): we checked the queue wasn't full when creating the token.
231 let pkt = unwrap!(self.tx.try_send());
232 let r = f(&mut pkt.buf[..len]);
233 pkt.len = len;
234 self.tx.send_done();
235 r
236 }
237}
238
239mod zerocopy_channel {
240 use core::cell::RefCell;
241 use core::future::poll_fn;
242 use core::marker::PhantomData;
243 use core::task::{Context, Poll};
244
245 use embassy_sync::blocking_mutex::raw::RawMutex;
246 use embassy_sync::blocking_mutex::Mutex;
247 use embassy_sync::waitqueue::WakerRegistration;
248
249 pub struct Channel<'a, M: RawMutex, T> {
250 buf: *mut T,
251 phantom: PhantomData<&'a mut T>,
252 state: Mutex<M, RefCell<State>>,
253 }
254
255 impl<'a, M: RawMutex, T> Channel<'a, M, T> {
256 pub fn new(buf: &'a mut [T]) -> Self {
257 let len = buf.len();
258 assert!(len != 0);
259
260 Self {
261 buf: buf.as_mut_ptr(),
262 phantom: PhantomData,
263 state: Mutex::new(RefCell::new(State {
264 len,
265 front: 0,
266 back: 0,
267 full: false,
268 send_waker: WakerRegistration::new(),
269 recv_waker: WakerRegistration::new(),
270 })),
271 }
272 }
273
274 pub fn split(&mut self) -> (Sender<'_, M, T>, Receiver<'_, M, T>) {
275 (Sender { channel: self }, Receiver { channel: self })
276 }
277 }
278
279 pub struct Sender<'a, M: RawMutex, T> {
280 channel: &'a Channel<'a, M, T>,
281 }
282
283 impl<'a, M: RawMutex, T> Sender<'a, M, T> {
284 pub fn borrow(&mut self) -> Sender<'_, M, T> {
285 Sender { channel: self.channel }
286 }
287
288 pub fn try_send(&mut self) -> Option<&mut T> {
289 self.channel.state.lock(|s| {
290 let s = &mut *s.borrow_mut();
291 match s.push_index() {
292 Some(i) => Some(unsafe { &mut *self.channel.buf.add(i) }),
293 None => None,
294 }
295 })
296 }
297
298 pub fn poll_send(&mut self, cx: &mut Context) -> Poll<&mut T> {
299 self.channel.state.lock(|s| {
300 let s = &mut *s.borrow_mut();
301 match s.push_index() {
302 Some(i) => Poll::Ready(unsafe { &mut *self.channel.buf.add(i) }),
303 None => {
304 s.recv_waker.register(cx.waker());
305 Poll::Pending
306 }
307 }
308 })
309 }
310
311 pub async fn send(&mut self) -> &mut T {
312 let i = poll_fn(|cx| {
313 self.channel.state.lock(|s| {
314 let s = &mut *s.borrow_mut();
315 match s.push_index() {
316 Some(i) => Poll::Ready(i),
317 None => {
318 s.recv_waker.register(cx.waker());
319 Poll::Pending
320 }
321 }
322 })
323 })
324 .await;
325 unsafe { &mut *self.channel.buf.add(i) }
326 }
327
328 pub fn send_done(&mut self) {
329 self.channel.state.lock(|s| s.borrow_mut().push_done())
330 }
331 }
332 pub struct Receiver<'a, M: RawMutex, T> {
333 channel: &'a Channel<'a, M, T>,
334 }
335
336 impl<'a, M: RawMutex, T> Receiver<'a, M, T> {
337 pub fn borrow(&mut self) -> Receiver<'_, M, T> {
338 Receiver { channel: self.channel }
339 }
340
341 pub fn try_recv(&mut self) -> Option<&mut T> {
342 self.channel.state.lock(|s| {
343 let s = &mut *s.borrow_mut();
344 match s.pop_index() {
345 Some(i) => Some(unsafe { &mut *self.channel.buf.add(i) }),
346 None => None,
347 }
348 })
349 }
350
351 pub fn poll_recv(&mut self, cx: &mut Context) -> Poll<&mut T> {
352 self.channel.state.lock(|s| {
353 let s = &mut *s.borrow_mut();
354 match s.pop_index() {
355 Some(i) => Poll::Ready(unsafe { &mut *self.channel.buf.add(i) }),
356 None => {
357 s.send_waker.register(cx.waker());
358 Poll::Pending
359 }
360 }
361 })
362 }
363
364 pub async fn recv(&mut self) -> &mut T {
365 let i = poll_fn(|cx| {
366 self.channel.state.lock(|s| {
367 let s = &mut *s.borrow_mut();
368 match s.pop_index() {
369 Some(i) => Poll::Ready(i),
370 None => {
371 s.send_waker.register(cx.waker());
372 Poll::Pending
373 }
374 }
375 })
376 })
377 .await;
378 unsafe { &mut *self.channel.buf.add(i) }
379 }
380
381 pub fn recv_done(&mut self) {
382 self.channel.state.lock(|s| s.borrow_mut().pop_done())
383 }
384 }
385
386 struct State {
387 len: usize,
388
389 /// Front index. Always 0..=(N-1)
390 front: usize,
391 /// Back index. Always 0..=(N-1).
392 back: usize,
393
394 /// Used to distinguish "empty" and "full" cases when `front == back`.
395 /// May only be `true` if `front == back`, always `false` otherwise.
396 full: bool,
397
398 send_waker: WakerRegistration,
399 recv_waker: WakerRegistration,
400 }
401
402 impl State {
403 fn increment(&self, i: usize) -> usize {
404 if i + 1 == self.len {
405 0
406 } else {
407 i + 1
408 }
409 }
410
411 fn is_full(&self) -> bool {
412 self.full
413 }
414
415 fn is_empty(&self) -> bool {
416 self.front == self.back && !self.full
417 }
418
419 fn push_index(&mut self) -> Option<usize> {
420 match self.is_full() {
421 true => None,
422 false => Some(self.back),
423 }
424 }
425
426 fn push_done(&mut self) {
427 assert!(!self.is_full());
428 self.back = self.increment(self.back);
429 if self.back == self.front {
430 self.full = true;
431 }
432 self.send_waker.wake();
433 }
434
435 fn pop_index(&mut self) -> Option<usize> {
436 match self.is_empty() {
437 true => None,
438 false => Some(self.front),
439 }
440 }
441
442 fn pop_done(&mut self) {
443 assert!(!self.is_empty());
444 self.front = self.increment(self.front);
445 self.full = false;
446 self.recv_waker.wake();
447 }
448 }
449}