2022-12-26 04:31:49 +01:00
|
|
|
#![no_std]
|
2023-06-29 19:51:16 +02:00
|
|
|
#![doc = include_str!("../README.md")]
|
2022-12-26 04:31:49 +01:00
|
|
|
|
|
|
|
// must go first!
|
|
|
|
mod fmt;
|
|
|
|
|
|
|
|
use core::cell::RefCell;
|
|
|
|
use core::mem::MaybeUninit;
|
|
|
|
use core::task::{Context, Poll};
|
|
|
|
|
|
|
|
pub use embassy_net_driver as driver;
|
2023-10-16 23:41:58 +02:00
|
|
|
use embassy_net_driver::{Capabilities, LinkState};
|
2022-12-26 04:31:49 +01:00
|
|
|
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
|
|
|
|
use embassy_sync::blocking_mutex::Mutex;
|
|
|
|
use embassy_sync::waitqueue::WakerRegistration;
|
2023-09-04 22:16:28 +02:00
|
|
|
use embassy_sync::zerocopy_channel;
|
2022-12-26 04:31:49 +01:00
|
|
|
|
|
|
|
pub struct State<const MTU: usize, const N_RX: usize, const N_TX: usize> {
|
|
|
|
rx: [PacketBuf<MTU>; N_RX],
|
|
|
|
tx: [PacketBuf<MTU>; N_TX],
|
|
|
|
inner: MaybeUninit<StateInner<'static, MTU>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<const MTU: usize, const N_RX: usize, const N_TX: usize> State<MTU, N_RX, N_TX> {
|
|
|
|
const NEW_PACKET: PacketBuf<MTU> = PacketBuf::new();
|
|
|
|
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
rx: [Self::NEW_PACKET; N_RX],
|
|
|
|
tx: [Self::NEW_PACKET; N_TX],
|
|
|
|
inner: MaybeUninit::uninit(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StateInner<'d, const MTU: usize> {
|
2023-09-04 22:16:28 +02:00
|
|
|
rx: zerocopy_channel::Channel<'d, NoopRawMutex, PacketBuf<MTU>>,
|
|
|
|
tx: zerocopy_channel::Channel<'d, NoopRawMutex, PacketBuf<MTU>>,
|
2022-12-27 01:04:55 +01:00
|
|
|
shared: Mutex<NoopRawMutex, RefCell<Shared>>,
|
2022-12-26 04:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// State of the LinkState
|
2022-12-27 01:04:55 +01:00
|
|
|
struct Shared {
|
|
|
|
link_state: LinkState,
|
2022-12-26 04:31:49 +01:00
|
|
|
waker: WakerRegistration,
|
2023-07-31 10:40:48 +02:00
|
|
|
hardware_address: driver::HardwareAddress,
|
2022-12-26 04:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Runner<'d, const MTU: usize> {
|
2023-09-04 22:16:28 +02:00
|
|
|
tx_chan: zerocopy_channel::Receiver<'d, NoopRawMutex, PacketBuf<MTU>>,
|
|
|
|
rx_chan: zerocopy_channel::Sender<'d, NoopRawMutex, PacketBuf<MTU>>,
|
2022-12-27 01:04:55 +01:00
|
|
|
shared: &'d Mutex<NoopRawMutex, RefCell<Shared>>,
|
2022-12-26 04:31:49 +01:00
|
|
|
}
|
|
|
|
|
2022-12-27 01:07:58 +01:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct StateRunner<'d> {
|
|
|
|
shared: &'d Mutex<NoopRawMutex, RefCell<Shared>>,
|
|
|
|
}
|
|
|
|
|
2022-12-26 04:31:49 +01:00
|
|
|
pub struct RxRunner<'d, const MTU: usize> {
|
2023-09-04 22:16:28 +02:00
|
|
|
rx_chan: zerocopy_channel::Sender<'d, NoopRawMutex, PacketBuf<MTU>>,
|
2022-12-26 04:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TxRunner<'d, const MTU: usize> {
|
2023-09-04 22:16:28 +02:00
|
|
|
tx_chan: zerocopy_channel::Receiver<'d, NoopRawMutex, PacketBuf<MTU>>,
|
2022-12-26 04:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, const MTU: usize> Runner<'d, MTU> {
|
2022-12-27 01:07:58 +01:00
|
|
|
pub fn split(self) -> (StateRunner<'d>, RxRunner<'d, MTU>, TxRunner<'d, MTU>) {
|
2022-12-26 04:31:49 +01:00
|
|
|
(
|
2022-12-27 01:07:58 +01:00
|
|
|
StateRunner { shared: self.shared },
|
|
|
|
RxRunner { rx_chan: self.rx_chan },
|
2022-12-26 04:31:49 +01:00
|
|
|
TxRunner { tx_chan: self.tx_chan },
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-25 15:57:02 +02:00
|
|
|
pub fn borrow_split(&mut self) -> (StateRunner<'_>, RxRunner<'_, MTU>, TxRunner<'_, MTU>) {
|
|
|
|
(
|
|
|
|
StateRunner { shared: self.shared },
|
|
|
|
RxRunner {
|
|
|
|
rx_chan: self.rx_chan.borrow(),
|
|
|
|
},
|
|
|
|
TxRunner {
|
|
|
|
tx_chan: self.tx_chan.borrow(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-27 01:07:58 +01:00
|
|
|
pub fn state_runner(&self) -> StateRunner<'d> {
|
|
|
|
StateRunner { shared: self.shared }
|
|
|
|
}
|
|
|
|
|
2022-12-26 04:31:49 +01:00
|
|
|
pub fn set_link_state(&mut self, state: LinkState) {
|
2022-12-27 01:04:55 +01:00
|
|
|
self.shared.lock(|s| {
|
2022-12-26 04:31:49 +01:00
|
|
|
let s = &mut *s.borrow_mut();
|
2022-12-27 01:04:55 +01:00
|
|
|
s.link_state = state;
|
|
|
|
s.waker.wake();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-31 10:40:48 +02:00
|
|
|
pub fn set_hardware_address(&mut self, address: driver::HardwareAddress) {
|
2022-12-27 01:04:55 +01:00
|
|
|
self.shared.lock(|s| {
|
|
|
|
let s = &mut *s.borrow_mut();
|
2023-07-28 16:19:24 +02:00
|
|
|
s.hardware_address = address;
|
2023-07-28 15:54:31 +02:00
|
|
|
s.waker.wake();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-26 04:31:49 +01:00
|
|
|
pub async fn rx_buf(&mut self) -> &mut [u8] {
|
|
|
|
let p = self.rx_chan.send().await;
|
|
|
|
&mut p.buf
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_rx_buf(&mut self) -> Option<&mut [u8]> {
|
|
|
|
let p = self.rx_chan.try_send()?;
|
|
|
|
Some(&mut p.buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn poll_rx_buf(&mut self, cx: &mut Context) -> Poll<&mut [u8]> {
|
|
|
|
match self.rx_chan.poll_send(cx) {
|
|
|
|
Poll::Ready(p) => Poll::Ready(&mut p.buf),
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rx_done(&mut self, len: usize) {
|
|
|
|
let p = self.rx_chan.try_send().unwrap();
|
|
|
|
p.len = len;
|
|
|
|
self.rx_chan.send_done();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn tx_buf(&mut self) -> &mut [u8] {
|
2023-08-11 12:07:30 +02:00
|
|
|
let p = self.tx_chan.receive().await;
|
2022-12-26 04:31:49 +01:00
|
|
|
&mut p.buf[..p.len]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_tx_buf(&mut self) -> Option<&mut [u8]> {
|
2023-08-11 12:07:30 +02:00
|
|
|
let p = self.tx_chan.try_receive()?;
|
2022-12-26 04:31:49 +01:00
|
|
|
Some(&mut p.buf[..p.len])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn poll_tx_buf(&mut self, cx: &mut Context) -> Poll<&mut [u8]> {
|
2023-08-11 12:07:30 +02:00
|
|
|
match self.tx_chan.poll_receive(cx) {
|
2022-12-26 04:31:49 +01:00
|
|
|
Poll::Ready(p) => Poll::Ready(&mut p.buf[..p.len]),
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tx_done(&mut self) {
|
2023-08-11 12:07:30 +02:00
|
|
|
self.tx_chan.receive_done();
|
2022-12-26 04:31:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 01:07:58 +01:00
|
|
|
impl<'d> StateRunner<'d> {
|
|
|
|
pub fn set_link_state(&self, state: LinkState) {
|
2022-12-27 01:04:55 +01:00
|
|
|
self.shared.lock(|s| {
|
|
|
|
let s = &mut *s.borrow_mut();
|
|
|
|
s.link_state = state;
|
|
|
|
s.waker.wake();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-10-16 23:41:58 +02:00
|
|
|
pub fn set_hardware_address(&self, address: driver::HardwareAddress) {
|
2022-12-27 01:04:55 +01:00
|
|
|
self.shared.lock(|s| {
|
2022-12-26 04:31:49 +01:00
|
|
|
let s = &mut *s.borrow_mut();
|
2023-10-16 23:41:58 +02:00
|
|
|
s.hardware_address = address;
|
2022-12-26 04:31:49 +01:00
|
|
|
s.waker.wake();
|
|
|
|
});
|
|
|
|
}
|
2022-12-27 01:07:58 +01:00
|
|
|
}
|
2022-12-26 04:31:49 +01:00
|
|
|
|
2022-12-27 01:07:58 +01:00
|
|
|
impl<'d, const MTU: usize> RxRunner<'d, MTU> {
|
2022-12-26 04:31:49 +01:00
|
|
|
pub async fn rx_buf(&mut self) -> &mut [u8] {
|
|
|
|
let p = self.rx_chan.send().await;
|
|
|
|
&mut p.buf
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_rx_buf(&mut self) -> Option<&mut [u8]> {
|
|
|
|
let p = self.rx_chan.try_send()?;
|
|
|
|
Some(&mut p.buf)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn poll_rx_buf(&mut self, cx: &mut Context) -> Poll<&mut [u8]> {
|
|
|
|
match self.rx_chan.poll_send(cx) {
|
|
|
|
Poll::Ready(p) => Poll::Ready(&mut p.buf),
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rx_done(&mut self, len: usize) {
|
|
|
|
let p = self.rx_chan.try_send().unwrap();
|
|
|
|
p.len = len;
|
|
|
|
self.rx_chan.send_done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, const MTU: usize> TxRunner<'d, MTU> {
|
|
|
|
pub async fn tx_buf(&mut self) -> &mut [u8] {
|
2023-08-11 12:07:30 +02:00
|
|
|
let p = self.tx_chan.receive().await;
|
2022-12-26 04:31:49 +01:00
|
|
|
&mut p.buf[..p.len]
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn try_tx_buf(&mut self) -> Option<&mut [u8]> {
|
2023-08-11 12:07:30 +02:00
|
|
|
let p = self.tx_chan.try_receive()?;
|
2022-12-26 04:31:49 +01:00
|
|
|
Some(&mut p.buf[..p.len])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn poll_tx_buf(&mut self, cx: &mut Context) -> Poll<&mut [u8]> {
|
2023-08-11 12:07:30 +02:00
|
|
|
match self.tx_chan.poll_receive(cx) {
|
2022-12-26 04:31:49 +01:00
|
|
|
Poll::Ready(p) => Poll::Ready(&mut p.buf[..p.len]),
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tx_done(&mut self) {
|
2023-08-11 12:07:30 +02:00
|
|
|
self.tx_chan.receive_done();
|
2022-12-26 04:31:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new<'d, const MTU: usize, const N_RX: usize, const N_TX: usize>(
|
|
|
|
state: &'d mut State<MTU, N_RX, N_TX>,
|
2023-07-31 10:40:48 +02:00
|
|
|
hardware_address: driver::HardwareAddress,
|
2022-12-26 04:31:49 +01:00
|
|
|
) -> (Runner<'d, MTU>, Device<'d, MTU>) {
|
|
|
|
let mut caps = Capabilities::default();
|
|
|
|
caps.max_transmission_unit = MTU;
|
|
|
|
|
|
|
|
// safety: this is a self-referential struct, however:
|
|
|
|
// - it can't move while the `'d` borrow is active.
|
|
|
|
// - when the borrow ends, the dangling references inside the MaybeUninit will never be used again.
|
|
|
|
let state_uninit: *mut MaybeUninit<StateInner<'d, MTU>> =
|
|
|
|
(&mut state.inner as *mut MaybeUninit<StateInner<'static, MTU>>).cast();
|
|
|
|
let state = unsafe { &mut *state_uninit }.write(StateInner {
|
2023-09-04 22:16:28 +02:00
|
|
|
rx: zerocopy_channel::Channel::new(&mut state.rx[..]),
|
|
|
|
tx: zerocopy_channel::Channel::new(&mut state.tx[..]),
|
2022-12-27 01:04:55 +01:00
|
|
|
shared: Mutex::new(RefCell::new(Shared {
|
|
|
|
link_state: LinkState::Down,
|
2023-07-31 10:40:48 +02:00
|
|
|
hardware_address,
|
2022-12-26 04:31:49 +01:00
|
|
|
waker: WakerRegistration::new(),
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
|
|
|
|
let (rx_sender, rx_receiver) = state.rx.split();
|
|
|
|
let (tx_sender, tx_receiver) = state.tx.split();
|
|
|
|
|
|
|
|
(
|
|
|
|
Runner {
|
|
|
|
tx_chan: tx_receiver,
|
|
|
|
rx_chan: rx_sender,
|
2022-12-27 01:04:55 +01:00
|
|
|
shared: &state.shared,
|
2022-12-26 04:31:49 +01:00
|
|
|
},
|
|
|
|
Device {
|
|
|
|
caps,
|
2022-12-27 01:04:55 +01:00
|
|
|
shared: &state.shared,
|
2022-12-26 04:31:49 +01:00
|
|
|
rx: rx_receiver,
|
|
|
|
tx: tx_sender,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PacketBuf<const MTU: usize> {
|
|
|
|
len: usize,
|
|
|
|
buf: [u8; MTU],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<const MTU: usize> PacketBuf<MTU> {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self { len: 0, buf: [0; MTU] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Device<'d, const MTU: usize> {
|
2023-09-04 22:16:28 +02:00
|
|
|
rx: zerocopy_channel::Receiver<'d, NoopRawMutex, PacketBuf<MTU>>,
|
|
|
|
tx: zerocopy_channel::Sender<'d, NoopRawMutex, PacketBuf<MTU>>,
|
2022-12-27 01:04:55 +01:00
|
|
|
shared: &'d Mutex<NoopRawMutex, RefCell<Shared>>,
|
2022-12-26 04:31:49 +01:00
|
|
|
caps: Capabilities,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, const MTU: usize> embassy_net_driver::Driver for Device<'d, MTU> {
|
|
|
|
type RxToken<'a> = RxToken<'a, MTU> where Self: 'a ;
|
|
|
|
type TxToken<'a> = TxToken<'a, MTU> where Self: 'a ;
|
|
|
|
|
|
|
|
fn receive(&mut self, cx: &mut Context) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
|
2023-08-11 12:07:30 +02:00
|
|
|
if self.rx.poll_receive(cx).is_ready() && self.tx.poll_send(cx).is_ready() {
|
2022-12-26 04:31:49 +01:00
|
|
|
Some((RxToken { rx: self.rx.borrow() }, TxToken { tx: self.tx.borrow() }))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Construct a transmit token.
|
|
|
|
fn transmit(&mut self, cx: &mut Context) -> Option<Self::TxToken<'_>> {
|
|
|
|
if self.tx.poll_send(cx).is_ready() {
|
|
|
|
Some(TxToken { tx: self.tx.borrow() })
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a description of device capabilities.
|
|
|
|
fn capabilities(&self) -> Capabilities {
|
|
|
|
self.caps.clone()
|
|
|
|
}
|
|
|
|
|
2023-07-31 10:40:48 +02:00
|
|
|
fn hardware_address(&self) -> driver::HardwareAddress {
|
2023-07-28 16:19:24 +02:00
|
|
|
self.shared.lock(|s| s.borrow().hardware_address)
|
2023-07-28 15:54:31 +02:00
|
|
|
}
|
|
|
|
|
2022-12-26 04:31:49 +01:00
|
|
|
fn link_state(&mut self, cx: &mut Context) -> LinkState {
|
2022-12-27 01:04:55 +01:00
|
|
|
self.shared.lock(|s| {
|
2022-12-26 04:31:49 +01:00
|
|
|
let s = &mut *s.borrow_mut();
|
|
|
|
s.waker.register(cx.waker());
|
2022-12-27 01:04:55 +01:00
|
|
|
s.link_state
|
2022-12-26 04:31:49 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RxToken<'a, const MTU: usize> {
|
2023-09-04 22:16:28 +02:00
|
|
|
rx: zerocopy_channel::Receiver<'a, NoopRawMutex, PacketBuf<MTU>>,
|
2022-12-26 04:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, const MTU: usize> embassy_net_driver::RxToken for RxToken<'a, MTU> {
|
|
|
|
fn consume<R, F>(mut self, f: F) -> R
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut [u8]) -> R,
|
|
|
|
{
|
|
|
|
// NOTE(unwrap): we checked the queue wasn't full when creating the token.
|
2023-08-11 12:07:30 +02:00
|
|
|
let pkt = unwrap!(self.rx.try_receive());
|
2022-12-26 04:31:49 +01:00
|
|
|
let r = f(&mut pkt.buf[..pkt.len]);
|
2023-08-11 12:07:30 +02:00
|
|
|
self.rx.receive_done();
|
2022-12-26 04:31:49 +01:00
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct TxToken<'a, const MTU: usize> {
|
2023-09-04 22:16:28 +02:00
|
|
|
tx: zerocopy_channel::Sender<'a, NoopRawMutex, PacketBuf<MTU>>,
|
2022-12-26 04:31:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, const MTU: usize> embassy_net_driver::TxToken for TxToken<'a, MTU> {
|
|
|
|
fn consume<R, F>(mut self, len: usize, f: F) -> R
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut [u8]) -> R,
|
|
|
|
{
|
|
|
|
// NOTE(unwrap): we checked the queue wasn't full when creating the token.
|
|
|
|
let pkt = unwrap!(self.tx.try_send());
|
|
|
|
let r = f(&mut pkt.buf[..len]);
|
|
|
|
pkt.len = len;
|
|
|
|
self.tx.send_done();
|
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|