diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-12-07 16:03:03 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-12-13 16:43:25 +0100 |
| commit | e9219405ca04e23b6543fb841fd97df54cf72f94 (patch) | |
| tree | 32d9129286949fdb887898adad1076ab352e95e9 | |
| parent | aaaf5f23a8a3a0df1ad2186802e2afc061a74b72 (diff) | |
usb/cdc-ncm: add embassy-net Device implementation.
| -rw-r--r-- | embassy-usb/Cargo.toml | 1 | ||||
| -rw-r--r-- | embassy-usb/src/class/cdc_ncm/embassy_net.rs | 449 | ||||
| -rw-r--r-- | embassy-usb/src/class/cdc_ncm/mod.rs (renamed from embassy-usb/src/class/cdc_ncm.rs) | 18 | ||||
| -rw-r--r-- | examples/nrf/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/nrf/src/bin/usb_ethernet.rs | 139 | ||||
| -rw-r--r-- | examples/rp/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/rp/src/bin/usb_ethernet.rs | 140 | ||||
| -rw-r--r-- | examples/stm32l5/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/stm32l5/src/bin/usb_ethernet.rs | 140 |
9 files changed, 522 insertions, 371 deletions
diff --git a/embassy-usb/Cargo.toml b/embassy-usb/Cargo.toml index b59ba8a22..1e72ce682 100644 --- a/embassy-usb/Cargo.toml +++ b/embassy-usb/Cargo.toml | |||
| @@ -19,6 +19,7 @@ default = ["usbd-hid"] | |||
| 19 | embassy-futures = { version = "0.1.0", path = "../embassy-futures" } | 19 | embassy-futures = { version = "0.1.0", path = "../embassy-futures" } |
| 20 | embassy-usb-driver = { version = "0.1.0", path = "../embassy-usb-driver" } | 20 | embassy-usb-driver = { version = "0.1.0", path = "../embassy-usb-driver" } |
| 21 | embassy-sync = { version = "0.1.0", path = "../embassy-sync" } | 21 | embassy-sync = { version = "0.1.0", path = "../embassy-sync" } |
| 22 | embassy-net = { version = "0.1.0", path = "../embassy-net", optional = true } | ||
| 22 | 23 | ||
| 23 | defmt = { version = "0.3", optional = true } | 24 | defmt = { version = "0.3", optional = true } |
| 24 | log = { version = "0.4.14", optional = true } | 25 | log = { 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 new file mode 100644 index 000000000..60bbfd8d4 --- /dev/null +++ b/embassy-usb/src/class/cdc_ncm/embassy_net.rs | |||
| @@ -0,0 +1,449 @@ | |||
| 1 | use core::cell::RefCell; | ||
| 2 | use core::mem::MaybeUninit; | ||
| 3 | use core::task::Context; | ||
| 4 | |||
| 5 | use embassy_futures::select::{select, Either}; | ||
| 6 | use embassy_net::device::{Device as DeviceTrait, DeviceCapabilities, LinkState, Medium}; | ||
| 7 | use embassy_sync::blocking_mutex::raw::NoopRawMutex; | ||
| 8 | use embassy_sync::blocking_mutex::Mutex; | ||
| 9 | use embassy_sync::waitqueue::WakerRegistration; | ||
| 10 | use embassy_usb_driver::Driver; | ||
| 11 | |||
| 12 | use super::{CdcNcmClass, Receiver, Sender}; | ||
| 13 | |||
| 14 | pub struct State<'d, const MTU: usize, const N_RX: usize, const N_TX: usize> { | ||
| 15 | rx: [PacketBuf<MTU>; N_RX], | ||
| 16 | tx: [PacketBuf<MTU>; N_TX], | ||
| 17 | inner: MaybeUninit<StateInner<'d, MTU>>, | ||
| 18 | } | ||
| 19 | |||
| 20 | impl<'d, const MTU: usize, const N_RX: usize, const N_TX: usize> State<'d, MTU, N_RX, N_TX> { | ||
| 21 | const NEW_PACKET: PacketBuf<MTU> = PacketBuf::new(); | ||
| 22 | |||
| 23 | pub const fn new() -> Self { | ||
| 24 | Self { | ||
| 25 | rx: [Self::NEW_PACKET; N_RX], | ||
| 26 | tx: [Self::NEW_PACKET; N_TX], | ||
| 27 | inner: MaybeUninit::uninit(), | ||
| 28 | } | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | struct 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 | ||
| 39 | struct LinkStateState { | ||
| 40 | state: LinkState, | ||
| 41 | waker: WakerRegistration, | ||
| 42 | } | ||
| 43 | |||
| 44 | pub struct Runner<'d, D: Driver<'d>, const MTU: usize> { | ||
| 45 | tx_usb: Sender<'d, D>, | ||
| 46 | tx_chan: zerocopy_channel::Receiver<'d, NoopRawMutex, PacketBuf<MTU>>, | ||
| 47 | rx_usb: Receiver<'d, D>, | ||
| 48 | rx_chan: zerocopy_channel::Sender<'d, NoopRawMutex, PacketBuf<MTU>>, | ||
| 49 | link_state: &'d Mutex<NoopRawMutex, RefCell<LinkStateState>>, | ||
| 50 | } | ||
| 51 | |||
| 52 | impl<'d, D: Driver<'d>, const MTU: usize> Runner<'d, D, MTU> { | ||
| 53 | pub async fn run(mut self) -> ! { | ||
| 54 | let rx_fut = async move { | ||
| 55 | loop { | ||
| 56 | trace!("WAITING for connection"); | ||
| 57 | self.link_state.lock(|s| { | ||
| 58 | let s = &mut *s.borrow_mut(); | ||
| 59 | s.state = LinkState::Down; | ||
| 60 | s.waker.wake(); | ||
| 61 | }); | ||
| 62 | |||
| 63 | self.rx_usb.wait_connection().await.unwrap(); | ||
| 64 | |||
| 65 | trace!("Connected"); | ||
| 66 | self.link_state.lock(|s| { | ||
| 67 | let s = &mut *s.borrow_mut(); | ||
| 68 | s.state = LinkState::Up; | ||
| 69 | s.waker.wake(); | ||
| 70 | }); | ||
| 71 | |||
| 72 | loop { | ||
| 73 | let p = self.rx_chan.send().await; | ||
| 74 | match self.rx_usb.read_packet(&mut p.buf).await { | ||
| 75 | Ok(n) => { | ||
| 76 | p.len = n; | ||
| 77 | self.rx_chan.send_done(); | ||
| 78 | } | ||
| 79 | Err(e) => { | ||
| 80 | warn!("error reading packet: {:?}", e); | ||
| 81 | break; | ||
| 82 | } | ||
| 83 | }; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | }; | ||
| 87 | let tx_fut = async move { | ||
| 88 | loop { | ||
| 89 | let p = self.tx_chan.recv().await; | ||
| 90 | if let Err(e) = self.tx_usb.write_packet(&p.buf[..p.len]).await { | ||
| 91 | warn!("Failed to TX packet: {:?}", e); | ||
| 92 | } | ||
| 93 | self.tx_chan.recv_done(); | ||
| 94 | } | ||
| 95 | }; | ||
| 96 | match select(rx_fut, tx_fut).await { | ||
| 97 | Either::First(x) => x, | ||
| 98 | Either::Second(x) => x, | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | impl<'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>( | ||
| 105 | self, | ||
| 106 | state: &'d mut State<'d, MTU, N_RX, N_TX>, | ||
| 107 | ethernet_address: [u8; 6], | ||
| 108 | ) -> (Runner<'d, D, MTU>, Device<'d, MTU>) { | ||
| 109 | let (tx_usb, rx_usb) = self.split(); | ||
| 110 | |||
| 111 | let mut caps = DeviceCapabilities::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 | |||
| 127 | ( | ||
| 128 | Runner { | ||
| 129 | tx_usb, | ||
| 130 | tx_chan: tx_receiver, | ||
| 131 | rx_usb, | ||
| 132 | rx_chan: rx_sender, | ||
| 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 | }, | ||
| 142 | ) | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | pub struct PacketBuf<const MTU: usize> { | ||
| 147 | len: usize, | ||
| 148 | buf: [u8; MTU], | ||
| 149 | } | ||
| 150 | |||
| 151 | impl<const MTU: usize> PacketBuf<MTU> { | ||
| 152 | pub const fn new() -> Self { | ||
| 153 | Self { len: 0, buf: [0; MTU] } | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | pub 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: DeviceCapabilities, | ||
| 162 | ethernet_address: [u8; 6], | ||
| 163 | } | ||
| 164 | |||
| 165 | impl<'d, const MTU: usize> DeviceTrait 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) -> DeviceCapabilities { | ||
| 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 | |||
| 204 | pub struct RxToken<'a, const MTU: usize> { | ||
| 205 | rx: zerocopy_channel::Receiver<'a, NoopRawMutex, PacketBuf<MTU>>, | ||
| 206 | } | ||
| 207 | |||
| 208 | impl<'a, const MTU: usize> embassy_net::device::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 | |||
| 221 | pub struct TxToken<'a, const MTU: usize> { | ||
| 222 | tx: zerocopy_channel::Sender<'a, NoopRawMutex, PacketBuf<MTU>>, | ||
| 223 | } | ||
| 224 | |||
| 225 | impl<'a, const MTU: usize> embassy_net::device::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 | |||
| 239 | mod 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 | } | ||
diff --git a/embassy-usb/src/class/cdc_ncm.rs b/embassy-usb/src/class/cdc_ncm/mod.rs index a39b87e9b..2ee47f68c 100644 --- a/embassy-usb/src/class/cdc_ncm.rs +++ b/embassy-usb/src/class/cdc_ncm/mod.rs | |||
| @@ -1,3 +1,18 @@ | |||
| 1 | /// CDC-NCM, aka Ethernet over USB. | ||
| 2 | /// | ||
| 3 | /// # Compatibility | ||
| 4 | /// | ||
| 5 | /// Windows: NOT supported in Windows 10. Supported in Windows 11. | ||
| 6 | /// | ||
| 7 | /// Linux: Well-supported since forever. | ||
| 8 | /// | ||
| 9 | /// Android: Support for CDC-NCM is spotty and varies across manufacturers. | ||
| 10 | /// | ||
| 11 | /// - On Pixel 4a, it refused to work on Android 11, worked on Android 12. | ||
| 12 | /// - if the host's MAC address has the "locally-administered" bit set (bit 1 of first byte), | ||
| 13 | /// it doesn't work! The "Ethernet tethering" option in settings doesn't get enabled. | ||
| 14 | /// This is due to regex spaghetti: https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-mainline-12.0.0_r84/core/res/res/values/config.xml#417 | ||
| 15 | /// and this nonsense in the linux kernel: https://github.com/torvalds/linux/blob/c00c5e1d157bec0ef0b0b59aa5482eb8dc7e8e49/drivers/net/usb/usbnet.c#L1751-L1757 | ||
| 1 | use core::intrinsics::copy_nonoverlapping; | 16 | use core::intrinsics::copy_nonoverlapping; |
| 2 | use core::mem::{size_of, MaybeUninit}; | 17 | use core::mem::{size_of, MaybeUninit}; |
| 3 | 18 | ||
| @@ -6,6 +21,9 @@ use crate::driver::{Driver, Endpoint, EndpointError, EndpointIn, EndpointOut}; | |||
| 6 | use crate::types::*; | 21 | use crate::types::*; |
| 7 | use crate::Builder; | 22 | use crate::Builder; |
| 8 | 23 | ||
| 24 | #[cfg(feature = "embassy-net")] | ||
| 25 | pub mod embassy_net; | ||
| 26 | |||
| 9 | /// This should be used as `device_class` when building the `UsbDevice`. | 27 | /// This should be used as `device_class` when building the `UsbDevice`. |
| 10 | pub const USB_CLASS_CDC: u8 = 0x02; | 28 | pub const USB_CLASS_CDC: u8 = 0x02; |
| 11 | 29 | ||
diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index 8b95ac3a6..a980a505c 100644 --- a/examples/nrf/Cargo.toml +++ b/examples/nrf/Cargo.toml | |||
| @@ -16,7 +16,7 @@ embassy-executor = { version = "0.1.0", path = "../../embassy-executor", feature | |||
| 16 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } | 16 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } |
| 17 | embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] } | 17 | embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] } |
| 18 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"], optional = true } | 18 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"], optional = true } |
| 19 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"], optional = true } | 19 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt", "embassy-net"], optional = true } |
| 20 | embedded-io = "0.4.0" | 20 | embedded-io = "0.4.0" |
| 21 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx126x", "time", "defmt"], optional = true } | 21 | embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx126x", "time", "defmt"], optional = true } |
| 22 | 22 | ||
diff --git a/examples/nrf/src/bin/usb_ethernet.rs b/examples/nrf/src/bin/usb_ethernet.rs index de93a2b45..e5f704524 100644 --- a/examples/nrf/src/bin/usb_ethernet.rs +++ b/examples/nrf/src/bin/usb_ethernet.rs | |||
| @@ -3,19 +3,16 @@ | |||
| 3 | #![feature(type_alias_impl_trait)] | 3 | #![feature(type_alias_impl_trait)] |
| 4 | 4 | ||
| 5 | use core::mem; | 5 | use core::mem; |
| 6 | use core::sync::atomic::{AtomicBool, Ordering}; | ||
| 7 | use core::task::Waker; | ||
| 8 | 6 | ||
| 9 | use defmt::*; | 7 | use defmt::*; |
| 10 | use embassy_executor::Spawner; | 8 | use embassy_executor::Spawner; |
| 11 | use embassy_net::tcp::TcpSocket; | 9 | use embassy_net::tcp::TcpSocket; |
| 12 | use embassy_net::{PacketBox, PacketBoxExt, PacketBuf, Stack, StackResources}; | 10 | use embassy_net::{Stack, StackResources}; |
| 13 | use embassy_nrf::rng::Rng; | 11 | use embassy_nrf::rng::Rng; |
| 14 | use embassy_nrf::usb::{Driver, PowerUsb}; | 12 | use embassy_nrf::usb::{Driver, PowerUsb}; |
| 15 | use embassy_nrf::{interrupt, pac, peripherals}; | 13 | use embassy_nrf::{interrupt, pac, peripherals}; |
| 16 | use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; | 14 | use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState}; |
| 17 | use embassy_sync::channel::Channel; | 15 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, State}; |
| 18 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, Receiver, Sender, State}; | ||
| 19 | use embassy_usb::{Builder, Config, UsbDevice}; | 16 | use embassy_usb::{Builder, Config, UsbDevice}; |
| 20 | use embedded_io::asynch::Write; | 17 | use embedded_io::asynch::Write; |
| 21 | use static_cell::StaticCell; | 18 | use static_cell::StaticCell; |
| @@ -27,56 +24,25 @@ macro_rules! singleton { | |||
| 27 | ($val:expr) => {{ | 24 | ($val:expr) => {{ |
| 28 | type T = impl Sized; | 25 | type T = impl Sized; |
| 29 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); | 26 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); |
| 30 | STATIC_CELL.init_with(move || $val) | 27 | let (x,) = STATIC_CELL.init(($val,)); |
| 28 | x | ||
| 31 | }}; | 29 | }}; |
| 32 | } | 30 | } |
| 33 | 31 | ||
| 32 | const MTU: usize = 1514; | ||
| 33 | |||
| 34 | #[embassy_executor::task] | 34 | #[embassy_executor::task] |
| 35 | async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { | 35 | async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { |
| 36 | device.run().await | 36 | device.run().await |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | #[embassy_executor::task] | 39 | #[embassy_executor::task] |
| 40 | async fn usb_ncm_rx_task(mut class: Receiver<'static, MyDriver>) { | 40 | async fn usb_ncm_task(class: Runner<'static, MyDriver, MTU>) -> ! { |
| 41 | loop { | 41 | class.run().await |
| 42 | warn!("WAITING for connection"); | ||
| 43 | LINK_UP.store(false, Ordering::Relaxed); | ||
| 44 | |||
| 45 | class.wait_connection().await.unwrap(); | ||
| 46 | |||
| 47 | warn!("Connected"); | ||
| 48 | LINK_UP.store(true, Ordering::Relaxed); | ||
| 49 | |||
| 50 | loop { | ||
| 51 | let mut p = unwrap!(PacketBox::new(embassy_net::Packet::new())); | ||
| 52 | let n = match class.read_packet(&mut p[..]).await { | ||
| 53 | Ok(n) => n, | ||
| 54 | Err(e) => { | ||
| 55 | warn!("error reading packet: {:?}", e); | ||
| 56 | break; | ||
| 57 | } | ||
| 58 | }; | ||
| 59 | |||
| 60 | let buf = p.slice(0..n); | ||
| 61 | if RX_CHANNEL.try_send(buf).is_err() { | ||
| 62 | warn!("Failed pushing rx'd packet to channel."); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | #[embassy_executor::task] | ||
| 69 | async fn usb_ncm_tx_task(mut class: Sender<'static, MyDriver>) { | ||
| 70 | loop { | ||
| 71 | let pkt = TX_CHANNEL.recv().await; | ||
| 72 | if let Err(e) = class.write_packet(&pkt[..]).await { | ||
| 73 | warn!("Failed to TX packet: {:?}", e); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | 42 | } |
| 77 | 43 | ||
| 78 | #[embassy_executor::task] | 44 | #[embassy_executor::task] |
| 79 | async fn net_task(stack: &'static Stack<Device>) -> ! { | 45 | async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! { |
| 80 | stack.run().await | 46 | stack.run().await |
| 81 | } | 47 | } |
| 82 | 48 | ||
| @@ -108,55 +74,32 @@ async fn main(spawner: Spawner) { | |||
| 108 | config.device_sub_class = 0x02; | 74 | config.device_sub_class = 0x02; |
| 109 | config.device_protocol = 0x01; | 75 | config.device_protocol = 0x01; |
| 110 | 76 | ||
| 111 | struct Resources { | ||
| 112 | device_descriptor: [u8; 256], | ||
| 113 | config_descriptor: [u8; 256], | ||
| 114 | bos_descriptor: [u8; 256], | ||
| 115 | control_buf: [u8; 128], | ||
| 116 | serial_state: State<'static>, | ||
| 117 | } | ||
| 118 | let res: &mut Resources = singleton!(Resources { | ||
| 119 | device_descriptor: [0; 256], | ||
| 120 | config_descriptor: [0; 256], | ||
| 121 | bos_descriptor: [0; 256], | ||
| 122 | control_buf: [0; 128], | ||
| 123 | serial_state: State::new(), | ||
| 124 | }); | ||
| 125 | |||
| 126 | // Create embassy-usb DeviceBuilder using the driver and config. | 77 | // Create embassy-usb DeviceBuilder using the driver and config. |
| 127 | let mut builder = Builder::new( | 78 | let mut builder = Builder::new( |
| 128 | driver, | 79 | driver, |
| 129 | config, | 80 | config, |
| 130 | &mut res.device_descriptor, | 81 | &mut singleton!([0; 256])[..], |
| 131 | &mut res.config_descriptor, | 82 | &mut singleton!([0; 256])[..], |
| 132 | &mut res.bos_descriptor, | 83 | &mut singleton!([0; 256])[..], |
| 133 | &mut res.control_buf, | 84 | &mut singleton!([0; 128])[..], |
| 134 | None, | 85 | None, |
| 135 | ); | 86 | ); |
| 136 | 87 | ||
| 137 | // WARNINGS for Android ethernet tethering: | ||
| 138 | // - On Pixel 4a, it refused to work on Android 11, worked on Android 12. | ||
| 139 | // - if the host's MAC address has the "locally-administered" bit set (bit 1 of first byte), | ||
| 140 | // it doesn't work! The "Ethernet tethering" option in settings doesn't get enabled. | ||
| 141 | // This is due to regex spaghetti: https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-mainline-12.0.0_r84/core/res/res/values/config.xml#417 | ||
| 142 | // and this nonsense in the linux kernel: https://github.com/torvalds/linux/blob/c00c5e1d157bec0ef0b0b59aa5482eb8dc7e8e49/drivers/net/usb/usbnet.c#L1751-L1757 | ||
| 143 | |||
| 144 | // Our MAC addr. | 88 | // Our MAC addr. |
| 145 | let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC]; | 89 | let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC]; |
| 146 | // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has. | 90 | // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has. |
| 147 | let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88]; | 91 | let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88]; |
| 148 | 92 | ||
| 149 | // Create classes on the builder. | 93 | // Create classes on the builder. |
| 150 | let class = CdcNcmClass::new(&mut builder, &mut res.serial_state, host_mac_addr, 64); | 94 | let class = CdcNcmClass::new(&mut builder, singleton!(State::new()), host_mac_addr, 64); |
| 151 | 95 | ||
| 152 | // Build the builder. | 96 | // Build the builder. |
| 153 | let usb = builder.build(); | 97 | let usb = builder.build(); |
| 154 | 98 | ||
| 155 | unwrap!(spawner.spawn(usb_task(usb))); | 99 | unwrap!(spawner.spawn(usb_task(usb))); |
| 156 | 100 | ||
| 157 | let (tx, rx) = class.split(); | 101 | let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(singleton!(NetState::new()), our_mac_addr); |
| 158 | unwrap!(spawner.spawn(usb_ncm_rx_task(rx))); | 102 | unwrap!(spawner.spawn(usb_ncm_task(runner))); |
| 159 | unwrap!(spawner.spawn(usb_ncm_tx_task(tx))); | ||
| 160 | 103 | ||
| 161 | let config = embassy_net::ConfigStrategy::Dhcp; | 104 | let config = embassy_net::ConfigStrategy::Dhcp; |
| 162 | //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { | 105 | //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { |
| @@ -172,7 +115,6 @@ async fn main(spawner: Spawner) { | |||
| 172 | let seed = u64::from_le_bytes(seed); | 115 | let seed = u64::from_le_bytes(seed); |
| 173 | 116 | ||
| 174 | // Init network stack | 117 | // Init network stack |
| 175 | let device = Device { mac_addr: our_mac_addr }; | ||
| 176 | let stack = &*singleton!(Stack::new( | 118 | let stack = &*singleton!(Stack::new( |
| 177 | device, | 119 | device, |
| 178 | config, | 120 | config, |
| @@ -225,50 +167,3 @@ async fn main(spawner: Spawner) { | |||
| 225 | } | 167 | } |
| 226 | } | 168 | } |
| 227 | } | 169 | } |
| 228 | |||
| 229 | static TX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 230 | static RX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 231 | static LINK_UP: AtomicBool = AtomicBool::new(false); | ||
| 232 | |||
| 233 | struct Device { | ||
| 234 | mac_addr: [u8; 6], | ||
| 235 | } | ||
| 236 | |||
| 237 | impl embassy_net::Device for Device { | ||
| 238 | fn register_waker(&mut self, waker: &Waker) { | ||
| 239 | // loopy loopy wakey wakey | ||
| 240 | waker.wake_by_ref() | ||
| 241 | } | ||
| 242 | |||
| 243 | fn link_state(&mut self) -> embassy_net::LinkState { | ||
| 244 | match LINK_UP.load(Ordering::Relaxed) { | ||
| 245 | true => embassy_net::LinkState::Up, | ||
| 246 | false => embassy_net::LinkState::Down, | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | fn capabilities(&self) -> embassy_net::DeviceCapabilities { | ||
| 251 | let mut caps = embassy_net::DeviceCapabilities::default(); | ||
| 252 | caps.max_transmission_unit = 1514; // 1500 IP + 14 ethernet header | ||
| 253 | caps.medium = embassy_net::Medium::Ethernet; | ||
| 254 | caps | ||
| 255 | } | ||
| 256 | |||
| 257 | fn is_transmit_ready(&mut self) -> bool { | ||
| 258 | true | ||
| 259 | } | ||
| 260 | |||
| 261 | fn transmit(&mut self, pkt: PacketBuf) { | ||
| 262 | if TX_CHANNEL.try_send(pkt).is_err() { | ||
| 263 | warn!("TX failed") | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | fn receive<'a>(&mut self) -> Option<PacketBuf> { | ||
| 268 | RX_CHANNEL.try_recv().ok() | ||
| 269 | } | ||
| 270 | |||
| 271 | fn ethernet_address(&self) -> [u8; 6] { | ||
| 272 | self.mac_addr | ||
| 273 | } | ||
| 274 | } | ||
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml index dfbc26426..31a08cfb6 100644 --- a/examples/rp/Cargo.toml +++ b/examples/rp/Cargo.toml | |||
| @@ -10,7 +10,7 @@ embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["de | |||
| 10 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } | 10 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } |
| 11 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } | 11 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } |
| 12 | embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] } | 12 | embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] } |
| 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt", "embassy-net"] } |
| 14 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } | 14 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } |
| 15 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | 15 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } |
| 16 | embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" } | 16 | embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" } |
diff --git a/examples/rp/src/bin/usb_ethernet.rs b/examples/rp/src/bin/usb_ethernet.rs index 1057fe7fd..d0aec874a 100644 --- a/examples/rp/src/bin/usb_ethernet.rs +++ b/examples/rp/src/bin/usb_ethernet.rs | |||
| @@ -2,18 +2,14 @@ | |||
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 3 | #![feature(type_alias_impl_trait)] |
| 4 | 4 | ||
| 5 | use core::sync::atomic::{AtomicBool, Ordering}; | ||
| 6 | use core::task::Waker; | ||
| 7 | |||
| 8 | use defmt::*; | 5 | use defmt::*; |
| 9 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 10 | use embassy_net::tcp::TcpSocket; | 7 | use embassy_net::tcp::TcpSocket; |
| 11 | use embassy_net::{PacketBox, PacketBoxExt, PacketBuf, Stack, StackResources}; | 8 | use embassy_net::{Stack, StackResources}; |
| 12 | use embassy_rp::usb::Driver; | 9 | use embassy_rp::usb::Driver; |
| 13 | use embassy_rp::{interrupt, peripherals}; | 10 | use embassy_rp::{interrupt, peripherals}; |
| 14 | use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; | 11 | use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState}; |
| 15 | use embassy_sync::channel::Channel; | 12 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, State}; |
| 16 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, Receiver, Sender, State}; | ||
| 17 | use embassy_usb::{Builder, Config, UsbDevice}; | 13 | use embassy_usb::{Builder, Config, UsbDevice}; |
| 18 | use embedded_io::asynch::Write; | 14 | use embedded_io::asynch::Write; |
| 19 | use static_cell::StaticCell; | 15 | use static_cell::StaticCell; |
| @@ -25,56 +21,25 @@ macro_rules! singleton { | |||
| 25 | ($val:expr) => {{ | 21 | ($val:expr) => {{ |
| 26 | type T = impl Sized; | 22 | type T = impl Sized; |
| 27 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); | 23 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); |
| 28 | STATIC_CELL.init_with(move || $val) | 24 | let (x,) = STATIC_CELL.init(($val,)); |
| 25 | x | ||
| 29 | }}; | 26 | }}; |
| 30 | } | 27 | } |
| 31 | 28 | ||
| 29 | const MTU: usize = 1514; | ||
| 30 | |||
| 32 | #[embassy_executor::task] | 31 | #[embassy_executor::task] |
| 33 | async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { | 32 | async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { |
| 34 | device.run().await | 33 | device.run().await |
| 35 | } | 34 | } |
| 36 | 35 | ||
| 37 | #[embassy_executor::task] | 36 | #[embassy_executor::task] |
| 38 | async fn usb_ncm_rx_task(mut class: Receiver<'static, MyDriver>) { | 37 | async fn usb_ncm_task(class: Runner<'static, MyDriver, MTU>) -> ! { |
| 39 | loop { | 38 | class.run().await |
| 40 | warn!("WAITING for connection"); | ||
| 41 | LINK_UP.store(false, Ordering::Relaxed); | ||
| 42 | |||
| 43 | class.wait_connection().await.unwrap(); | ||
| 44 | |||
| 45 | warn!("Connected"); | ||
| 46 | LINK_UP.store(true, Ordering::Relaxed); | ||
| 47 | |||
| 48 | loop { | ||
| 49 | let mut p = unwrap!(PacketBox::new(embassy_net::Packet::new())); | ||
| 50 | let n = match class.read_packet(&mut p[..]).await { | ||
| 51 | Ok(n) => n, | ||
| 52 | Err(e) => { | ||
| 53 | warn!("error reading packet: {:?}", e); | ||
| 54 | break; | ||
| 55 | } | ||
| 56 | }; | ||
| 57 | |||
| 58 | let buf = p.slice(0..n); | ||
| 59 | if RX_CHANNEL.try_send(buf).is_err() { | ||
| 60 | warn!("Failed pushing rx'd packet to channel."); | ||
| 61 | } | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | #[embassy_executor::task] | ||
| 67 | async fn usb_ncm_tx_task(mut class: Sender<'static, MyDriver>) { | ||
| 68 | loop { | ||
| 69 | let pkt = TX_CHANNEL.recv().await; | ||
| 70 | if let Err(e) = class.write_packet(&pkt[..]).await { | ||
| 71 | warn!("Failed to TX packet: {:?}", e); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | 39 | } |
| 75 | 40 | ||
| 76 | #[embassy_executor::task] | 41 | #[embassy_executor::task] |
| 77 | async fn net_task(stack: &'static Stack<Device>) -> ! { | 42 | async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! { |
| 78 | stack.run().await | 43 | stack.run().await |
| 79 | } | 44 | } |
| 80 | 45 | ||
| @@ -100,55 +65,32 @@ async fn main(spawner: Spawner) { | |||
| 100 | config.device_sub_class = 0x02; | 65 | config.device_sub_class = 0x02; |
| 101 | config.device_protocol = 0x01; | 66 | config.device_protocol = 0x01; |
| 102 | 67 | ||
| 103 | struct Resources { | ||
| 104 | device_descriptor: [u8; 256], | ||
| 105 | config_descriptor: [u8; 256], | ||
| 106 | bos_descriptor: [u8; 256], | ||
| 107 | control_buf: [u8; 128], | ||
| 108 | serial_state: State<'static>, | ||
| 109 | } | ||
| 110 | let res: &mut Resources = singleton!(Resources { | ||
| 111 | device_descriptor: [0; 256], | ||
| 112 | config_descriptor: [0; 256], | ||
| 113 | bos_descriptor: [0; 256], | ||
| 114 | control_buf: [0; 128], | ||
| 115 | serial_state: State::new(), | ||
| 116 | }); | ||
| 117 | |||
| 118 | // Create embassy-usb DeviceBuilder using the driver and config. | 68 | // Create embassy-usb DeviceBuilder using the driver and config. |
| 119 | let mut builder = Builder::new( | 69 | let mut builder = Builder::new( |
| 120 | driver, | 70 | driver, |
| 121 | config, | 71 | config, |
| 122 | &mut res.device_descriptor, | 72 | &mut singleton!([0; 256])[..], |
| 123 | &mut res.config_descriptor, | 73 | &mut singleton!([0; 256])[..], |
| 124 | &mut res.bos_descriptor, | 74 | &mut singleton!([0; 256])[..], |
| 125 | &mut res.control_buf, | 75 | &mut singleton!([0; 128])[..], |
| 126 | None, | 76 | None, |
| 127 | ); | 77 | ); |
| 128 | 78 | ||
| 129 | // WARNINGS for Android ethernet tethering: | ||
| 130 | // - On Pixel 4a, it refused to work on Android 11, worked on Android 12. | ||
| 131 | // - if the host's MAC address has the "locally-administered" bit set (bit 1 of first byte), | ||
| 132 | // it doesn't work! The "Ethernet tethering" option in settings doesn't get enabled. | ||
| 133 | // This is due to regex spaghetti: https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-mainline-12.0.0_r84/core/res/res/values/config.xml#417 | ||
| 134 | // and this nonsense in the linux kernel: https://github.com/torvalds/linux/blob/c00c5e1d157bec0ef0b0b59aa5482eb8dc7e8e49/drivers/net/usb/usbnet.c#L1751-L1757 | ||
| 135 | |||
| 136 | // Our MAC addr. | 79 | // Our MAC addr. |
| 137 | let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC]; | 80 | let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC]; |
| 138 | // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has. | 81 | // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has. |
| 139 | let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88]; | 82 | let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88]; |
| 140 | 83 | ||
| 141 | // Create classes on the builder. | 84 | // Create classes on the builder. |
| 142 | let class = CdcNcmClass::new(&mut builder, &mut res.serial_state, host_mac_addr, 64); | 85 | let class = CdcNcmClass::new(&mut builder, singleton!(State::new()), host_mac_addr, 64); |
| 143 | 86 | ||
| 144 | // Build the builder. | 87 | // Build the builder. |
| 145 | let usb = builder.build(); | 88 | let usb = builder.build(); |
| 146 | 89 | ||
| 147 | unwrap!(spawner.spawn(usb_task(usb))); | 90 | unwrap!(spawner.spawn(usb_task(usb))); |
| 148 | 91 | ||
| 149 | let (tx, rx) = class.split(); | 92 | let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(singleton!(NetState::new()), our_mac_addr); |
| 150 | unwrap!(spawner.spawn(usb_ncm_rx_task(rx))); | 93 | unwrap!(spawner.spawn(usb_ncm_task(runner))); |
| 151 | unwrap!(spawner.spawn(usb_ncm_tx_task(tx))); | ||
| 152 | 94 | ||
| 153 | let config = embassy_net::ConfigStrategy::Dhcp; | 95 | let config = embassy_net::ConfigStrategy::Dhcp; |
| 154 | //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { | 96 | //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { |
| @@ -161,7 +103,6 @@ async fn main(spawner: Spawner) { | |||
| 161 | let seed = 1234; // guaranteed random, chosen by a fair dice roll | 103 | let seed = 1234; // guaranteed random, chosen by a fair dice roll |
| 162 | 104 | ||
| 163 | // Init network stack | 105 | // Init network stack |
| 164 | let device = Device { mac_addr: our_mac_addr }; | ||
| 165 | let stack = &*singleton!(Stack::new( | 106 | let stack = &*singleton!(Stack::new( |
| 166 | device, | 107 | device, |
| 167 | config, | 108 | config, |
| @@ -214,50 +155,3 @@ async fn main(spawner: Spawner) { | |||
| 214 | } | 155 | } |
| 215 | } | 156 | } |
| 216 | } | 157 | } |
| 217 | |||
| 218 | static TX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 219 | static RX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 220 | static LINK_UP: AtomicBool = AtomicBool::new(false); | ||
| 221 | |||
| 222 | struct Device { | ||
| 223 | mac_addr: [u8; 6], | ||
| 224 | } | ||
| 225 | |||
| 226 | impl embassy_net::Device for Device { | ||
| 227 | fn register_waker(&mut self, waker: &Waker) { | ||
| 228 | // loopy loopy wakey wakey | ||
| 229 | waker.wake_by_ref() | ||
| 230 | } | ||
| 231 | |||
| 232 | fn link_state(&mut self) -> embassy_net::LinkState { | ||
| 233 | match LINK_UP.load(Ordering::Relaxed) { | ||
| 234 | true => embassy_net::LinkState::Up, | ||
| 235 | false => embassy_net::LinkState::Down, | ||
| 236 | } | ||
| 237 | } | ||
| 238 | |||
| 239 | fn capabilities(&self) -> embassy_net::DeviceCapabilities { | ||
| 240 | let mut caps = embassy_net::DeviceCapabilities::default(); | ||
| 241 | caps.max_transmission_unit = 1514; // 1500 IP + 14 ethernet header | ||
| 242 | caps.medium = embassy_net::Medium::Ethernet; | ||
| 243 | caps | ||
| 244 | } | ||
| 245 | |||
| 246 | fn is_transmit_ready(&mut self) -> bool { | ||
| 247 | true | ||
| 248 | } | ||
| 249 | |||
| 250 | fn transmit(&mut self, pkt: PacketBuf) { | ||
| 251 | if TX_CHANNEL.try_send(pkt).is_err() { | ||
| 252 | warn!("TX failed") | ||
| 253 | } | ||
| 254 | } | ||
| 255 | |||
| 256 | fn receive<'a>(&mut self) -> Option<PacketBuf> { | ||
| 257 | RX_CHANNEL.try_recv().ok() | ||
| 258 | } | ||
| 259 | |||
| 260 | fn ethernet_address(&self) -> [u8; 6] { | ||
| 261 | self.mac_addr | ||
| 262 | } | ||
| 263 | } | ||
diff --git a/examples/stm32l5/Cargo.toml b/examples/stm32l5/Cargo.toml index 73ad50787..bb8515312 100644 --- a/examples/stm32l5/Cargo.toml +++ b/examples/stm32l5/Cargo.toml | |||
| @@ -11,7 +11,7 @@ embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["de | |||
| 11 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } |
| 13 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l552ze", "time-driver-any", "exti", "unstable-traits", "memory-x"] } | 13 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "unstable-pac", "stm32l552ze", "time-driver-any", "exti", "unstable-traits", "memory-x"] } |
| 14 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | 14 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt", "embassy-net"] } |
| 15 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } | 15 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } |
| 16 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | 16 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } |
| 17 | usbd-hid = "0.6.0" | 17 | usbd-hid = "0.6.0" |
diff --git a/examples/stm32l5/src/bin/usb_ethernet.rs b/examples/stm32l5/src/bin/usb_ethernet.rs index 4f36d3f5a..b49329ea4 100644 --- a/examples/stm32l5/src/bin/usb_ethernet.rs +++ b/examples/stm32l5/src/bin/usb_ethernet.rs | |||
| @@ -2,20 +2,16 @@ | |||
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | #![feature(type_alias_impl_trait)] | 3 | #![feature(type_alias_impl_trait)] |
| 4 | 4 | ||
| 5 | use core::sync::atomic::{AtomicBool, Ordering}; | ||
| 6 | use core::task::Waker; | ||
| 7 | |||
| 8 | use defmt::*; | 5 | use defmt::*; |
| 9 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 10 | use embassy_net::tcp::TcpSocket; | 7 | use embassy_net::tcp::TcpSocket; |
| 11 | use embassy_net::{PacketBox, PacketBoxExt, PacketBuf, Stack, StackResources}; | 8 | use embassy_net::{Stack, StackResources}; |
| 12 | use embassy_stm32::rcc::*; | 9 | use embassy_stm32::rcc::*; |
| 13 | use embassy_stm32::rng::Rng; | 10 | use embassy_stm32::rng::Rng; |
| 14 | use embassy_stm32::usb::Driver; | 11 | use embassy_stm32::usb::Driver; |
| 15 | use embassy_stm32::{interrupt, Config}; | 12 | use embassy_stm32::{interrupt, Config}; |
| 16 | use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; | 13 | use embassy_usb::class::cdc_ncm::embassy_net::{Device, Runner, State as NetState}; |
| 17 | use embassy_sync::channel::Channel; | 14 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, State}; |
| 18 | use embassy_usb::class::cdc_ncm::{CdcNcmClass, Receiver, Sender, State}; | ||
| 19 | use embassy_usb::{Builder, UsbDevice}; | 15 | use embassy_usb::{Builder, UsbDevice}; |
| 20 | use embedded_io::asynch::Write; | 16 | use embedded_io::asynch::Write; |
| 21 | use rand_core::RngCore; | 17 | use rand_core::RngCore; |
| @@ -28,56 +24,25 @@ macro_rules! singleton { | |||
| 28 | ($val:expr) => {{ | 24 | ($val:expr) => {{ |
| 29 | type T = impl Sized; | 25 | type T = impl Sized; |
| 30 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); | 26 | static STATIC_CELL: StaticCell<T> = StaticCell::new(); |
| 31 | STATIC_CELL.init_with(move || $val) | 27 | let (x,) = STATIC_CELL.init(($val,)); |
| 28 | x | ||
| 32 | }}; | 29 | }}; |
| 33 | } | 30 | } |
| 34 | 31 | ||
| 32 | const MTU: usize = 1514; | ||
| 33 | |||
| 35 | #[embassy_executor::task] | 34 | #[embassy_executor::task] |
| 36 | async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { | 35 | async fn usb_task(mut device: UsbDevice<'static, MyDriver>) -> ! { |
| 37 | device.run().await | 36 | device.run().await |
| 38 | } | 37 | } |
| 39 | 38 | ||
| 40 | #[embassy_executor::task] | 39 | #[embassy_executor::task] |
| 41 | async fn usb_ncm_rx_task(mut class: Receiver<'static, MyDriver>) { | 40 | async fn usb_ncm_task(class: Runner<'static, MyDriver, MTU>) -> ! { |
| 42 | loop { | 41 | class.run().await |
| 43 | warn!("WAITING for connection"); | ||
| 44 | LINK_UP.store(false, Ordering::Relaxed); | ||
| 45 | |||
| 46 | class.wait_connection().await.unwrap(); | ||
| 47 | |||
| 48 | warn!("Connected"); | ||
| 49 | LINK_UP.store(true, Ordering::Relaxed); | ||
| 50 | |||
| 51 | loop { | ||
| 52 | let mut p = unwrap!(PacketBox::new(embassy_net::Packet::new())); | ||
| 53 | let n = match class.read_packet(&mut p[..]).await { | ||
| 54 | Ok(n) => n, | ||
| 55 | Err(e) => { | ||
| 56 | warn!("error reading packet: {:?}", e); | ||
| 57 | break; | ||
| 58 | } | ||
| 59 | }; | ||
| 60 | |||
| 61 | let buf = p.slice(0..n); | ||
| 62 | if RX_CHANNEL.try_send(buf).is_err() { | ||
| 63 | warn!("Failed pushing rx'd packet to channel."); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | #[embassy_executor::task] | ||
| 70 | async fn usb_ncm_tx_task(mut class: Sender<'static, MyDriver>) { | ||
| 71 | loop { | ||
| 72 | let pkt = TX_CHANNEL.recv().await; | ||
| 73 | if let Err(e) = class.write_packet(&pkt[..]).await { | ||
| 74 | warn!("Failed to TX packet: {:?}", e); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | } | 42 | } |
| 78 | 43 | ||
| 79 | #[embassy_executor::task] | 44 | #[embassy_executor::task] |
| 80 | async fn net_task(stack: &'static Stack<Device>) -> ! { | 45 | async fn net_task(stack: &'static Stack<Device<'static, MTU>>) -> ! { |
| 81 | stack.run().await | 46 | stack.run().await |
| 82 | } | 47 | } |
| 83 | 48 | ||
| @@ -106,55 +71,32 @@ async fn main(spawner: Spawner) { | |||
| 106 | config.device_sub_class = 0x02; | 71 | config.device_sub_class = 0x02; |
| 107 | config.device_protocol = 0x01; | 72 | config.device_protocol = 0x01; |
| 108 | 73 | ||
| 109 | struct Resources { | ||
| 110 | device_descriptor: [u8; 256], | ||
| 111 | config_descriptor: [u8; 256], | ||
| 112 | bos_descriptor: [u8; 256], | ||
| 113 | control_buf: [u8; 128], | ||
| 114 | serial_state: State<'static>, | ||
| 115 | } | ||
| 116 | let res: &mut Resources = singleton!(Resources { | ||
| 117 | device_descriptor: [0; 256], | ||
| 118 | config_descriptor: [0; 256], | ||
| 119 | bos_descriptor: [0; 256], | ||
| 120 | control_buf: [0; 128], | ||
| 121 | serial_state: State::new(), | ||
| 122 | }); | ||
| 123 | |||
| 124 | // Create embassy-usb DeviceBuilder using the driver and config. | 74 | // Create embassy-usb DeviceBuilder using the driver and config. |
| 125 | let mut builder = Builder::new( | 75 | let mut builder = Builder::new( |
| 126 | driver, | 76 | driver, |
| 127 | config, | 77 | config, |
| 128 | &mut res.device_descriptor, | 78 | &mut singleton!([0; 256])[..], |
| 129 | &mut res.config_descriptor, | 79 | &mut singleton!([0; 256])[..], |
| 130 | &mut res.bos_descriptor, | 80 | &mut singleton!([0; 256])[..], |
| 131 | &mut res.control_buf, | 81 | &mut singleton!([0; 128])[..], |
| 132 | None, | 82 | None, |
| 133 | ); | 83 | ); |
| 134 | 84 | ||
| 135 | // WARNINGS for Android ethernet tethering: | ||
| 136 | // - On Pixel 4a, it refused to work on Android 11, worked on Android 12. | ||
| 137 | // - if the host's MAC address has the "locally-administered" bit set (bit 1 of first byte), | ||
| 138 | // it doesn't work! The "Ethernet tethering" option in settings doesn't get enabled. | ||
| 139 | // This is due to regex spaghetti: https://android.googlesource.com/platform/frameworks/base/+/refs/tags/android-mainline-12.0.0_r84/core/res/res/values/config.xml#417 | ||
| 140 | // and this nonsense in the linux kernel: https://github.com/torvalds/linux/blob/c00c5e1d157bec0ef0b0b59aa5482eb8dc7e8e49/drivers/net/usb/usbnet.c#L1751-L1757 | ||
| 141 | |||
| 142 | // Our MAC addr. | 85 | // Our MAC addr. |
| 143 | let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC]; | 86 | let our_mac_addr = [0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC]; |
| 144 | // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has. | 87 | // Host's MAC addr. This is the MAC the host "thinks" its USB-to-ethernet adapter has. |
| 145 | let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88]; | 88 | let host_mac_addr = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88]; |
| 146 | 89 | ||
| 147 | // Create classes on the builder. | 90 | // Create classes on the builder. |
| 148 | let class = CdcNcmClass::new(&mut builder, &mut res.serial_state, host_mac_addr, 64); | 91 | let class = CdcNcmClass::new(&mut builder, singleton!(State::new()), host_mac_addr, 64); |
| 149 | 92 | ||
| 150 | // Build the builder. | 93 | // Build the builder. |
| 151 | let usb = builder.build(); | 94 | let usb = builder.build(); |
| 152 | 95 | ||
| 153 | unwrap!(spawner.spawn(usb_task(usb))); | 96 | unwrap!(spawner.spawn(usb_task(usb))); |
| 154 | 97 | ||
| 155 | let (tx, rx) = class.split(); | 98 | let (runner, device) = class.into_embassy_net_device::<MTU, 4, 4>(singleton!(NetState::new()), our_mac_addr); |
| 156 | unwrap!(spawner.spawn(usb_ncm_rx_task(rx))); | 99 | unwrap!(spawner.spawn(usb_ncm_task(runner))); |
| 157 | unwrap!(spawner.spawn(usb_ncm_tx_task(tx))); | ||
| 158 | 100 | ||
| 159 | let config = embassy_net::ConfigStrategy::Dhcp; | 101 | let config = embassy_net::ConfigStrategy::Dhcp; |
| 160 | //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { | 102 | //let config = embassy_net::ConfigStrategy::Static(embassy_net::Config { |
| @@ -168,7 +110,6 @@ async fn main(spawner: Spawner) { | |||
| 168 | let seed = rng.next_u64(); | 110 | let seed = rng.next_u64(); |
| 169 | 111 | ||
| 170 | // Init network stack | 112 | // Init network stack |
| 171 | let device = Device { mac_addr: our_mac_addr }; | ||
| 172 | let stack = &*singleton!(Stack::new( | 113 | let stack = &*singleton!(Stack::new( |
| 173 | device, | 114 | device, |
| 174 | config, | 115 | config, |
| @@ -221,50 +162,3 @@ async fn main(spawner: Spawner) { | |||
| 221 | } | 162 | } |
| 222 | } | 163 | } |
| 223 | } | 164 | } |
| 224 | |||
| 225 | static TX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 226 | static RX_CHANNEL: Channel<ThreadModeRawMutex, PacketBuf, 8> = Channel::new(); | ||
| 227 | static LINK_UP: AtomicBool = AtomicBool::new(false); | ||
| 228 | |||
| 229 | struct Device { | ||
| 230 | mac_addr: [u8; 6], | ||
| 231 | } | ||
| 232 | |||
| 233 | impl embassy_net::Device for Device { | ||
| 234 | fn register_waker(&mut self, waker: &Waker) { | ||
| 235 | // loopy loopy wakey wakey | ||
| 236 | waker.wake_by_ref() | ||
| 237 | } | ||
| 238 | |||
| 239 | fn link_state(&mut self) -> embassy_net::LinkState { | ||
| 240 | match LINK_UP.load(Ordering::Relaxed) { | ||
| 241 | true => embassy_net::LinkState::Up, | ||
| 242 | false => embassy_net::LinkState::Down, | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | fn capabilities(&self) -> embassy_net::DeviceCapabilities { | ||
| 247 | let mut caps = embassy_net::DeviceCapabilities::default(); | ||
| 248 | caps.max_transmission_unit = 1514; // 1500 IP + 14 ethernet header | ||
| 249 | caps.medium = embassy_net::Medium::Ethernet; | ||
| 250 | caps | ||
| 251 | } | ||
| 252 | |||
| 253 | fn is_transmit_ready(&mut self) -> bool { | ||
| 254 | true | ||
| 255 | } | ||
| 256 | |||
| 257 | fn transmit(&mut self, pkt: PacketBuf) { | ||
| 258 | if TX_CHANNEL.try_send(pkt).is_err() { | ||
| 259 | warn!("TX failed") | ||
| 260 | } | ||
| 261 | } | ||
| 262 | |||
| 263 | fn receive<'a>(&mut self) -> Option<PacketBuf> { | ||
| 264 | RX_CHANNEL.try_recv().ok() | ||
| 265 | } | ||
| 266 | |||
| 267 | fn ethernet_address(&self) -> [u8; 6] { | ||
| 268 | self.mac_addr | ||
| 269 | } | ||
| 270 | } | ||
