Skip to main content

esp_emac/
embassy.rs

1// SPDX-License-Identifier: GPL-2.0-or-later OR Apache-2.0
2// Copyright (c) Viacheslav Bocharov <v@baodeep.com> and JetHome (r)
3
4//! Native embassy-net driver for the ESP32 EMAC.
5//!
6//! Wraps [`crate::Emac`] directly (no `ph_esp32_mac::EmbassyEmac` proxy).
7//! [`EmacDriverState`] holds the wakers, link cache, and ISR counters
8//! and is intended to live in `static` storage so the EMAC ISR can
9//! reach it.
10//!
11//! # Lifetime alignment
12//!
13//! [`Emac`] and [`EmacDriverState`] play different roles, with
14//! different uniqueness requirements:
15//!
16//! - [`Emac`] drives a single hardware peripheral. The ESP32 has
17//!   exactly one built-in EMAC and `Emac::init` touches global MMIO,
18//!   so at most one initialized `Emac` instance can be active on a
19//!   running device. Place it in `static mut` storage and take the
20//!   `&'static mut` once at bring-up via
21//!   `unsafe { &mut *core::ptr::addr_of_mut!(EMAC) }`. `Emac::new`
22//!   is a `const fn`, so the value lives in BSS — no runtime stack
23//!   temporary, deterministic on cold boot. (See the *Usage* section
24//!   below for why a `StaticCell::init(EmacDefault::new(..))`
25//!   wrapper is *not* recommended at the default ring sizing.)
26//! - [`EmacDriverState`] is **not** a strict singleton. Multiple
27//!   instances are fine — host-side tests construct one per test, and
28//!   sequential re-initialization with a fresh state on the same
29//!   peripheral is allowed. The constraint is alignment: the
30//!   `EmacDriverState` whose [`handle_emac_interrupt`] runs from the
31//!   ISR must be the same instance you pass to [`EmacDriver::new`]
32//!   alongside the `Emac`. If those two references disagree, RX/TX
33//!   wakers fire against the wrong state and the stack stalls.
34//!
35//! [`EmacDriver`] then ties the pair together. The borrow checker
36//! enforces that **at most one** driver holds the `&'d mut Emac<...>`
37//! at any given moment. Sequential reuse is allowed — once the borrow
38//! ends (driver dropped, scope exited) the same `Emac` can be paired
39//! with a fresh driver again, which is what the unit tests in this
40//! module already exercise.
41//!
42//! # Recovery from task respawn
43//!
44//! `Emac::init` is a one-shot: if the task that owns the
45//! `&'static mut EmacDefault` panics and is respawned by the executor,
46//! the static EMAC retains state from the previous run. Calling
47//! `init` a second time returns [`EmacError::AlreadyInitialized`] and
48//! does nothing. The reborrowed `&'static mut` is still valid — the
49//! peripheral is still configured — but the DMA engine may have
50//! stopped mid-operation (descriptors marked owned by the engine,
51//! TX FIFO partially drained), and the driver state in
52//! [`EmacDriverState`] no longer matches the in-flight wakers from
53//! the previous run.
54//!
55//! Recovery sequence in the respawned task:
56//!
57//! ```ignore
58//! use esp_hal::delay::Delay;
59//!
60//! // Reborrow — same EMAC, still post-init from prior run.
61//! let emac = unsafe { &mut *core::ptr::addr_of_mut!(EMAC) };
62//! // Tear down the running engine and clear DMA state. `stop()`
63//! // takes a `&mut impl DelayNs` for the TX-FIFO flush poll.
64//! // It is idempotent on `Initialized` (returns Ok(())) and rejects
65//! // an `Uninitialized` driver with `EmacError::NotInitialized` —
66//! // neither matters here because the prior task left the EMAC in
67//! // `Running`. `Err(EmacError::TxFlushTimeout)` means teardown
68//! // still completed (state is back at `Initialized`); the warning
69//! // is recoverable, so swallow it.
70//! let mut delay = Delay::new();
71//! let _ = emac.stop(&mut delay);
72//! // Restart fresh. The peripheral keeps its already-programmed
73//! // pins, clocks, and MAC address — only the DMA rings need to
74//! // come back up.
75//! emac.start()?;
76//! ```
77//!
78//! Do **not** call `init()` a second time hoping it will reset the
79//! peripheral — it won't, and the error swallows silently in code
80//! that ignores the `Result`. Use the explicit `stop()` + `start()`
81//! cycle above.
82//!
83//! [`handle_emac_interrupt`]: EmacDriverState::handle_emac_interrupt
84//! [`EmacError::AlreadyInitialized`]: crate::EmacError::AlreadyInitialized
85//!
86//! # Usage
87//!
88//! The driver is non-functional until the EMAC ISR services
89//! `DMASTATUS` and wakes the RX/TX tasks. The ISR body must call
90//! [`EmacDriverState::handle_emac_interrupt`] (or the lower-level
91//! pair [`crate::Emac::handle_interrupt`] +
92//! [`EmacDriverState::on_interrupt_status`]) — without that, RX and
93//! TX block forever in `Driver::receive` / `Driver::transmit` waiting
94//! on wakers that nothing pokes.
95//!
96//! ```ignore
97//! use esp_emac::{
98//!     EmacConfig, RmiiClockConfig, RmiiPins, ClkGpio, XtalFreq,
99//!     EmacDefault,
100//!     embassy::{EmacDefaultDriver, EmacDriverState},
101//! };
102//! use esp_hal::interrupt::{InterruptHandler, Priority};
103//!
104//! // `EmacDefault::new` is a `const fn`, so the EMAC value is built at
105//! // compile time and lives in BSS — zero runtime stack involvement on
106//! // boot. The default ring sizing is currently 10 RX / 10 TX /
107//! // 1600-byte buffers (~32 KiB), sourced from `DEFAULT_RX` /
108//! // `DEFAULT_TX` / `DEFAULT_BUF`. A `StaticCell::init(EmacDefault::new(..))`
109//! // pattern would risk landing that 32 KiB on the caller's stack
110//! // before the move into static storage; the `static mut` form is
111//! // smaller, deterministic and avoids that hazard.
112//! static mut EMAC: EmacDefault = EmacDefault::new(EmacConfig {
113//!     clock: RmiiClockConfig::InternalApll {
114//!         gpio: ClkGpio::Gpio17,
115//!         xtal: XtalFreq::Mhz40,
116//!     },
117//!     pins: RmiiPins { mdc: 23, mdio: 18 },
118//! });
119//! static EMAC_STATE: EmacDriverState = EmacDriverState::new();
120//!
121//! // 1. ISR — must service DMASTATUS and wake stack tasks. The
122//! //    `EMAC_STATE` it touches has to be the same instance the
123//! //    driver is paired with below.
124//! #[esp_hal::handler(priority = Priority::Priority1)]
125//! fn emac_isr() {
126//!     EMAC_STATE.handle_emac_interrupt();
127//! }
128//!
129//! // 2. Bring-up + driver wiring.
130//! # fn example() -> Result<(), esp_emac::EmacError> {
131//! # let mut delay = esp_hal::delay::Delay::new();
132//! // SAFETY: `EMAC` is touched only here — single owner — so no aliasing.
133//! let emac = unsafe { &mut *core::ptr::addr_of_mut!(EMAC) };
134//! emac.set_mac_address([0x00, 0x70, 0x07, 0x24, 0x3B, 0x87]);
135//! emac.init(&mut delay)?;
136//! // ... PHY init + link wait + set_speed/set_duplex omitted ...
137//! emac.bind_interrupt(InterruptHandler::new(emac_isr, Priority::Priority1));
138//! emac.start()?;
139//! // `EmacDefaultDriver` is a type alias whose inherent `new` is the
140//! // same `EmacDriver::new` constructor — keeps the call site free of
141//! // the const-generic ceremony (currently `<10, 10, 1600>`, sourced
142//! // from `DEFAULT_RX` / `DEFAULT_TX` / `DEFAULT_BUF`).
143//! let driver = EmacDefaultDriver::new(emac, &EMAC_STATE);
144//! // Hand `driver` to embassy_net::new() / Stack.
145//! # Ok(()) }
146//! ```
147
148use core::cell::Cell;
149use core::marker::PhantomData;
150use core::sync::atomic::{AtomicU32, Ordering};
151use core::task::Context;
152
153use critical_section::Mutex;
154use embassy_net_driver::{
155    Capabilities, ChecksumCapabilities, Driver, HardwareAddress, LinkState, RxToken, TxToken,
156};
157use embassy_sync::waitqueue::AtomicWaker;
158
159use crate::emac::{Emac, DEFAULT_BUF, DEFAULT_RX, DEFAULT_TX, SMALL_RX, SMALL_TX};
160use crate::interrupt::InterruptStatus;
161
162/// Diagnostic snapshot of the `Driver::receive` / `Driver::transmit`
163/// path: how many times embassy-net asked for a token, how many of
164/// those calls actually had a frame, and how many frames the tokens
165/// failed to push to / pull from the EMAC.
166#[derive(Debug, Clone, Copy, Default)]
167pub struct DriverCounters {
168    /// Calls to `Driver::receive`.
169    pub rx_calls: u32,
170    /// Calls that returned a non-empty token pair (frame available).
171    pub rx_some: u32,
172    /// Frames silently dropped in `EmacRxToken::consume` because the
173    /// underlying `Emac::receive` returned `Err(_)` or `Ok(None)` after
174    /// the driver had already handed out a token. Indicates either an
175    /// errored frame (CRC, oversize) or a race where another path
176    /// consumed the descriptor first.
177    pub rx_dropped: u32,
178    /// Calls to `Driver::transmit`.
179    pub tx_calls: u32,
180    /// Calls that returned a token (TX path was ready).
181    pub tx_some: u32,
182    /// Frames silently dropped in `EmacTxToken::consume` because
183    /// `Emac::transmit` returned `Err(_)` after the driver had already
184    /// handed out a token. Typical cause: descriptor ring exhausted
185    /// between the readiness check and the actual push.
186    pub tx_dropped: u32,
187}
188
189/// Diagnostic snapshot of the ISR counters.
190#[derive(Debug, Clone, Copy, Default)]
191pub struct IrqCounters {
192    /// Total number of times the ISR ran.
193    pub total: u32,
194    /// `RI` (rx_complete) flag observed.
195    pub ri: u32,
196    /// `RU` (rx_buf_unavailable) flag observed.
197    pub ru: u32,
198    /// `TI` (tx_complete) flag observed.
199    pub ti: u32,
200    /// `TU` (tx_buf_unavailable) flag observed.
201    pub tu: u32,
202    /// `ERI` (early receive) flag observed.
203    pub eri: u32,
204    /// At least one error flag observed (UNF/OVF/FBI).
205    pub err: u32,
206    /// Last raw `DMASTATUS` snapshot taken in the ISR (before W1C).
207    pub last_dmastat: u32,
208}
209
210/// Maximum frame size for stack-allocated copy buffers (Ethernet MTU + headers).
211const MAX_FRAME_SIZE: usize = 1600;
212
213/// Standard Ethernet MTU (IP MTU + L2 header). Upper bound on the value
214/// the driver advertises to embassy-net — see
215/// `EmacDriver::effective_mtu` for the per-instance value, which caps
216/// this against the physical TX ring capacity.
217const ETH_MTU: usize = 1514;
218
219// =============================================================================
220// Driver state
221// =============================================================================
222
223/// Shared state for the embassy-net driver.
224///
225/// Holds the RX, TX, and link wakers plus the cached link state. Place
226/// in a `static` so it can be reached from the EMAC ISR.
227pub struct EmacDriverState {
228    rx_waker: AtomicWaker,
229    tx_waker: AtomicWaker,
230    link_waker: AtomicWaker,
231    link_state: Mutex<Cell<LinkState>>,
232    /// Diagnostic counters — incremented in the ISR. Used by the dev-log
233    /// hypotheses H6/H7.
234    irq_count: AtomicU32,
235    irq_ri: AtomicU32,
236    irq_ru: AtomicU32,
237    irq_ti: AtomicU32,
238    irq_tu: AtomicU32,
239    irq_eri: AtomicU32,
240    irq_err: AtomicU32,
241    /// Last observed raw DMASTAT (snapshot taken in ISR, before W1C).
242    last_dmastat: AtomicU32,
243    /// Counters bumped by [`EmacDriver::receive`] / [`EmacDriver::transmit`]
244    /// to see how often embassy-net actually pulls data.
245    drv_rx_calls: AtomicU32,
246    drv_rx_some: AtomicU32,
247    drv_rx_dropped: AtomicU32,
248    drv_tx_calls: AtomicU32,
249    drv_tx_some: AtomicU32,
250    drv_tx_dropped: AtomicU32,
251}
252
253impl Default for EmacDriverState {
254    fn default() -> Self {
255        Self::new()
256    }
257}
258
259impl EmacDriverState {
260    /// Create a new state with link initially down.
261    pub const fn new() -> Self {
262        Self {
263            rx_waker: AtomicWaker::new(),
264            tx_waker: AtomicWaker::new(),
265            link_waker: AtomicWaker::new(),
266            link_state: Mutex::new(Cell::new(LinkState::Down)),
267            irq_count: AtomicU32::new(0),
268            irq_ri: AtomicU32::new(0),
269            irq_ru: AtomicU32::new(0),
270            irq_ti: AtomicU32::new(0),
271            irq_tu: AtomicU32::new(0),
272            irq_eri: AtomicU32::new(0),
273            irq_err: AtomicU32::new(0),
274            last_dmastat: AtomicU32::new(0),
275            drv_rx_calls: AtomicU32::new(0),
276            drv_rx_some: AtomicU32::new(0),
277            drv_rx_dropped: AtomicU32::new(0),
278            drv_tx_calls: AtomicU32::new(0),
279            drv_tx_some: AtomicU32::new(0),
280            drv_tx_dropped: AtomicU32::new(0),
281        }
282    }
283
284    /// Diagnostic counters from the ISR.
285    pub fn irq_counters(&self) -> IrqCounters {
286        IrqCounters {
287            total: self.irq_count.load(Ordering::Relaxed),
288            ri: self.irq_ri.load(Ordering::Relaxed),
289            ru: self.irq_ru.load(Ordering::Relaxed),
290            ti: self.irq_ti.load(Ordering::Relaxed),
291            tu: self.irq_tu.load(Ordering::Relaxed),
292            eri: self.irq_eri.load(Ordering::Relaxed),
293            err: self.irq_err.load(Ordering::Relaxed),
294            last_dmastat: self.last_dmastat.load(Ordering::Relaxed),
295        }
296    }
297
298    /// Diagnostic counters from `Driver::receive` / `Driver::transmit`
299    /// and the matching tokens.
300    pub fn driver_counters(&self) -> DriverCounters {
301        DriverCounters {
302            rx_calls: self.drv_rx_calls.load(Ordering::Relaxed),
303            rx_some: self.drv_rx_some.load(Ordering::Relaxed),
304            rx_dropped: self.drv_rx_dropped.load(Ordering::Relaxed),
305            tx_calls: self.drv_tx_calls.load(Ordering::Relaxed),
306            tx_some: self.drv_tx_some.load(Ordering::Relaxed),
307            tx_dropped: self.drv_tx_dropped.load(Ordering::Relaxed),
308        }
309    }
310
311    /// Read the cached link state.
312    pub fn link_state(&self) -> LinkState {
313        critical_section::with(|cs| self.link_state.borrow(cs).get())
314    }
315
316    /// Update the cached link state and wake stack tasks.
317    pub fn set_link_state(&self, state: LinkState) {
318        critical_section::with(|cs| self.link_state.borrow(cs).set(state));
319        self.link_waker.wake();
320    }
321
322    /// Mark the link as up and wake the stack.
323    pub fn set_link_up(&self) {
324        self.set_link_state(LinkState::Up);
325    }
326
327    /// Mark the link as down and wake the stack.
328    pub fn set_link_down(&self) {
329        self.set_link_state(LinkState::Down);
330    }
331
332    /// Wake RX/TX tasks based on a snapshot of the DMA interrupt status.
333    pub fn on_interrupt_status(&self, status: InterruptStatus) {
334        if status.rx_complete || status.rx_buf_unavailable {
335            self.rx_waker.wake();
336        }
337        if status.tx_complete || status.tx_buf_unavailable {
338            self.tx_waker.wake();
339        }
340        if status.has_error() {
341            self.rx_waker.wake();
342            self.tx_waker.wake();
343        }
344    }
345
346    /// Read the DMA status register, clear the interrupts, and wake
347    /// any tasks waiting on RX or TX.
348    ///
349    /// Does **not** wake `link_waker` — link state isn't reflected in
350    /// `DMASTATUS` and is updated separately by whatever PHY-polling
351    /// task calls [`set_link_up`](Self::set_link_up) /
352    /// [`set_link_down`](Self::set_link_down). That path takes care
353    /// of waking link-state observers itself.
354    ///
355    /// Intended to be called from the EMAC ISR. Touches only memory-
356    /// mapped EMAC registers and the embedded wakers, so there is no
357    /// aliasing concern with the [`EmacDriver`] holding a raw pointer
358    /// to the [`Emac`] state.
359    pub fn handle_emac_interrupt(&self) {
360        let dmastat = crate::regs::dma::BASE + crate::regs::dma::DMASTATUS;
361        // SAFETY: DMASTATUS is a known-valid 32-bit memory-mapped register.
362        let raw = unsafe { core::ptr::read_volatile(dmastat as *const u32) };
363        let status = InterruptStatus::from_raw(raw);
364        // Write-1-to-clear using the raw snapshot, masked against
365        // `ALL_INTERRUPTS` so only the W1C interrupt bits are written
366        // back. This still catches every asserted W1C bit — including
367        // ones outside `InterruptStatus` such as `ERI` (bit 14), `ETI`
368        // (bit 10), `RWT` (bit 9), `TJT` (bit 3) — but excludes the
369        // read-only fields (`RS`/`TS`/`EB`/`MMC`/`PMT`/`TTI`) so we
370        // never write garbage at addresses the hardware doesn't expect.
371        // Round-tripping through `to_raw()` would silently drop those
372        // bits and risk an interrupt storm.
373        // SAFETY: same address; masked write hits only W1C bits.
374        unsafe {
375            core::ptr::write_volatile(
376                dmastat as *mut u32,
377                raw & crate::regs::dma::status::ALL_INTERRUPTS,
378            )
379        };
380
381        self.irq_count.fetch_add(1, Ordering::Relaxed);
382        self.last_dmastat.store(raw, Ordering::Relaxed);
383        if status.rx_complete {
384            self.irq_ri.fetch_add(1, Ordering::Relaxed);
385        }
386        if status.rx_buf_unavailable {
387            self.irq_ru.fetch_add(1, Ordering::Relaxed);
388        }
389        if status.tx_complete {
390            self.irq_ti.fetch_add(1, Ordering::Relaxed);
391        }
392        if status.tx_buf_unavailable {
393            self.irq_tu.fetch_add(1, Ordering::Relaxed);
394        }
395        // Early Receive Interrupt (ERI, bit 14 of DMASTATUS — distinct
396        // from ETI, the Early Transmit Interrupt at bit 10) isn't
397        // surfaced through `InterruptStatus`, so check the raw flag
398        // against the canonical `regs::dma::status::ERI` constant
399        // rather than a magic shift.
400        if (raw & crate::regs::dma::status::ERI) != 0 {
401            self.irq_eri.fetch_add(1, Ordering::Relaxed);
402        }
403        if status.has_error() {
404            self.irq_err.fetch_add(1, Ordering::Relaxed);
405        }
406
407        self.on_interrupt_status(status);
408    }
409}
410
411// =============================================================================
412// Driver wrapper
413// =============================================================================
414
415/// embassy-net driver for the ESP32 EMAC.
416///
417/// The driver holds a raw pointer to a previously-initialized
418/// [`Emac`] together with a reference to a shared [`EmacDriverState`].
419///
420/// # Concurrent ownership
421///
422/// At most **one** `EmacDriver` can hold the `&'d mut Emac<...>` at a
423/// time. The borrow checker enforces that through the `&'d mut`
424/// argument to [`Self::new`] — concurrent aliasing is impossible.
425/// Sequential reuse is fine: once a driver is dropped, the same
426/// `Emac` can be paired with a fresh driver again. The unit tests in
427/// this module exercise that pattern.
428///
429/// The companion [`EmacDriverState`] is **not** a strict singleton —
430/// see the module-level *Lifetime alignment* section. The constraint
431/// is that whichever instance the EMAC ISR's
432/// [`EmacDriverState::handle_emac_interrupt`] runs against must be
433/// the same one passed here as `state`.
434///
435/// For the default ring sizing the
436/// [`EmacDefaultDriver<'d>`](EmacDefaultDriver) alias removes the need
437/// to repeat the const generics in `embassy_executor::task` signatures.
438///
439/// # Safety
440///
441/// The pointer is dereferenced in `Driver` impl methods. The lifetime
442/// `'d` ensures the underlying `Emac` outlives the driver, but the raw
443/// pointer means **mutable aliasing** would be unsound. The
444/// at-most-one-concurrent-driver invariant above is what keeps that
445/// aliasing impossible in practice.
446pub struct EmacDriver<'d, const RX: usize, const TX: usize, const BUF: usize> {
447    emac: *mut Emac<RX, TX, BUF>,
448    state: &'d EmacDriverState,
449    _marker: PhantomData<&'d mut Emac<RX, TX, BUF>>,
450}
451
452// SAFETY contract for `unsafe impl Send`:
453//
454// `EmacDriver` carries a `*mut Emac<...>` (raw pointers are not auto-Send),
455// but the manual impl is sound under the following invariants — break any
456// one of them and the impl becomes unsound, so revisit it together with
457// any change that touches `Emac`'s field layout or the ISR data path:
458//
459// 1. Single ownership. Exactly one `EmacDriver` exists per `Emac`
460//    instance for the lifetime of `'d`. `EmacDriver::new` consumes a
461//    `&'d mut Emac<...>`, which the borrow checker enforces as long
462//    as the pointer isn't laundered through other unsafe code.
463// 2. ISR-side access through `EmacDriverState` only touches MMIO
464//    (`DMASTATUS`) and `AtomicU32` counters — *not* the `Emac` struct
465//    behind the raw pointer. So the ISR is not a concurrent reader
466//    of the data the `Driver` impl mutates.
467// 3. The pointee `Emac<RX, TX, BUF>` is itself `Send`. The raw pointer
468//    hides auto-trait inference, so without an explicit bound a
469//    future `Cell<X>` / `Rc<X>` / `MutexGuard<'_, X>` inside `Emac`
470//    would silently leave this impl claiming `Send`. The
471//    `where Emac<RX, TX, BUF>: Send` clause below promotes that
472//    invariant from documentation to a compile-time check: such a
473//    refactor will fail to compile here instead of producing
474//    unsound `EmacDriver: Send`.
475unsafe impl<const RX: usize, const TX: usize, const BUF: usize> Send for EmacDriver<'_, RX, TX, BUF> where
476    Emac<RX, TX, BUF>: Send
477{
478}
479
480impl<'d, const RX: usize, const TX: usize, const BUF: usize> EmacDriver<'d, RX, TX, BUF> {
481    /// Create a new embassy-net driver.
482    ///
483    /// `emac` must be already initialized and started; `state` must be
484    /// the same instance whose [`on_interrupt_status`] is called from
485    /// the EMAC ISR.
486    pub fn new(emac: &'d mut Emac<RX, TX, BUF>, state: &'d EmacDriverState) -> Self {
487        Self {
488            emac: emac as *mut _,
489            state,
490            _marker: PhantomData,
491        }
492    }
493
494    /// Borrow the shared state.
495    pub fn state(&self) -> &EmacDriverState {
496        self.state
497    }
498
499    /// Effective MTU advertised to embassy-net and used as the
500    /// readiness threshold in `Driver::transmit`.
501    ///
502    /// Capped by the physical TX ring capacity (`TX * BUF`) so the
503    /// driver never advertises — and never gates on — a frame size
504    /// the engine couldn't actually push. On normal rings (e.g.
505    /// `TX=10, BUF=1600`) this returns the standard Ethernet MTU
506    /// of `1514`. On undersized rings (`TX * BUF < 1514`) it shrinks
507    /// to `TX * BUF`, so small frames still flow even though full-MTU
508    /// frames are physically impossible.
509    pub const fn effective_mtu() -> usize {
510        let ring_capacity = TX * BUF;
511        if ring_capacity < ETH_MTU {
512            ring_capacity
513        } else {
514            ETH_MTU
515        }
516    }
517}
518
519// =============================================================================
520// Convenience type aliases
521// =============================================================================
522
523/// Driver for the [`crate::EmacDefault`] ring sizing.
524///
525/// Sourced from the same [`DEFAULT_RX`] / [`DEFAULT_TX`] /
526/// [`DEFAULT_BUF`] constants as `EmacDefault`, so the two aliases
527/// stay paired even if the canonical sizing is retuned. The
528/// `embassy_executor::task` signature for the `net_task` runner can
529/// then read `Runner<'static, EmacDefaultDriver<'static>>` instead
530/// of repeating the const generics at every call site.
531pub type EmacDefaultDriver<'d> = EmacDriver<'d, DEFAULT_RX, DEFAULT_TX, DEFAULT_BUF>;
532
533/// Driver for the [`crate::EmacSmall`] ring sizing.
534///
535/// See [`EmacDefaultDriver`] for the rationale.
536pub type EmacSmallDriver<'d> = EmacDriver<'d, SMALL_RX, SMALL_TX, DEFAULT_BUF>;
537
538// =============================================================================
539// RX / TX tokens
540// =============================================================================
541
542/// embassy-net RX token — copies one received frame on `consume`.
543pub struct EmacRxToken<'a, const RX: usize, const TX: usize, const BUF: usize> {
544    emac: *mut Emac<RX, TX, BUF>,
545    state: &'a EmacDriverState,
546    _marker: PhantomData<&'a mut Emac<RX, TX, BUF>>,
547}
548
549impl<const RX: usize, const TX: usize, const BUF: usize> RxToken for EmacRxToken<'_, RX, TX, BUF> {
550    fn consume<R, F>(self, f: F) -> R
551    where
552        F: FnOnce(&mut [u8]) -> R,
553    {
554        let mut buffer = [0u8; MAX_FRAME_SIZE];
555        // SAFETY: `EmacDriver` guarantees the pointer is valid for the
556        // lifetime tracked by `'a`; tokens are consumed synchronously by
557        // the embassy stack.
558        let emac = unsafe { &mut *self.emac };
559        match emac.receive(&mut buffer) {
560            Ok(Some(n)) => f(&mut buffer[..n]),
561            // No frame after we already handed out a token — either an
562            // error path (FrameError, BufferTooSmall: descriptor was
563            // recycled by the engine) or a race where another caller
564            // consumed it. Bump `rx_dropped` so the drop is observable
565            // and pass an empty slice to satisfy the `RxToken` contract.
566            Ok(None) | Err(_) => {
567                self.state.drv_rx_dropped.fetch_add(1, Ordering::Relaxed);
568                f(&mut [])
569            }
570        }
571    }
572}
573
574/// embassy-net TX token — submits one frame on `consume`.
575pub struct EmacTxToken<'a, const RX: usize, const TX: usize, const BUF: usize> {
576    emac: *mut Emac<RX, TX, BUF>,
577    state: &'a EmacDriverState,
578    _marker: PhantomData<&'a mut Emac<RX, TX, BUF>>,
579}
580
581impl<const RX: usize, const TX: usize, const BUF: usize> TxToken for EmacTxToken<'_, RX, TX, BUF> {
582    fn consume<R, F>(self, len: usize, f: F) -> R
583    where
584        F: FnOnce(&mut [u8]) -> R,
585    {
586        let len = len.min(MAX_FRAME_SIZE);
587        let mut buffer = [0u8; MAX_FRAME_SIZE];
588        let result = f(&mut buffer[..len]);
589        // SAFETY: see `EmacRxToken::consume`.
590        let emac = unsafe { &mut *self.emac };
591        if emac.transmit(&buffer[..len]).is_err() {
592            // `embassy-net-driver`'s `TxToken::consume` has no fallible
593            // return, so a failed push silently drops the frame. Bump
594            // `tx_dropped` for diagnostics.
595            self.state.drv_tx_dropped.fetch_add(1, Ordering::Relaxed);
596        }
597        result
598    }
599}
600
601// =============================================================================
602// Driver trait
603// =============================================================================
604
605impl<const RX: usize, const TX: usize, const BUF: usize> Driver for EmacDriver<'_, RX, TX, BUF> {
606    type RxToken<'a>
607        = EmacRxToken<'a, RX, TX, BUF>
608    where
609        Self: 'a;
610    type TxToken<'a>
611        = EmacTxToken<'a, RX, TX, BUF>
612    where
613        Self: 'a;
614
615    fn receive(&mut self, cx: &mut Context<'_>) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
616        self.state.drv_rx_calls.fetch_add(1, Ordering::Relaxed);
617        // SAFETY: see `EmacDriver` doc.
618        let emac = unsafe { &mut *self.emac };
619
620        if !emac.rx_available() {
621            self.state.rx_waker.register(cx.waker());
622            if !emac.rx_available() {
623                return None;
624            }
625        }
626
627        self.state.drv_rx_some.fetch_add(1, Ordering::Relaxed);
628        Some((
629            EmacRxToken {
630                emac: self.emac,
631                state: self.state,
632                _marker: PhantomData,
633            },
634            EmacTxToken {
635                emac: self.emac,
636                state: self.state,
637                _marker: PhantomData,
638            },
639        ))
640    }
641
642    fn transmit(&mut self, cx: &mut Context<'_>) -> Option<Self::TxToken<'_>> {
643        self.state.drv_tx_calls.fetch_add(1, Ordering::Relaxed);
644        // SAFETY: see `EmacDriver` doc.
645        let emac = unsafe { &mut *self.emac };
646
647        // Gate on capacity for a worst-case MTU-sized frame, not just
648        // "≥ 1 free descriptor". A frame larger than `BUF` consumes
649        // `len.div_ceil(BUF)` descriptors, so on rings where `BUF < MTU`
650        // a single-descriptor readiness check would let the driver hand
651        // out a token that `EmacTxToken::consume` then can't actually
652        // push, silently dropping the frame.
653        //
654        // `effective_mtu()` is capped by `TX * BUF`, so on undersized
655        // rings we still gate on something the engine can transmit
656        // (smaller frames will fit) instead of permanently returning
657        // `None` for a 1514-byte target the ring can never hold.
658        let mtu = Self::effective_mtu();
659        if !emac.can_transmit(mtu) {
660            self.state.tx_waker.register(cx.waker());
661            if !emac.can_transmit(mtu) {
662                return None;
663            }
664        }
665
666        self.state.drv_tx_some.fetch_add(1, Ordering::Relaxed);
667        Some(EmacTxToken {
668            emac: self.emac,
669            state: self.state,
670            _marker: PhantomData,
671        })
672    }
673
674    fn link_state(&mut self, cx: &mut Context<'_>) -> LinkState {
675        self.state.link_waker.register(cx.waker());
676        self.state.link_state()
677    }
678
679    fn capabilities(&self) -> Capabilities {
680        let mut caps = Capabilities::default();
681        // Advertise the value the driver can actually deliver (capped
682        // by ring capacity), not a fixed Ethernet MTU.
683        caps.max_transmission_unit = Self::effective_mtu();
684        caps.max_burst_size = Some(1);
685        caps.checksum = ChecksumCapabilities::default();
686        caps
687    }
688
689    fn hardware_address(&self) -> HardwareAddress {
690        // SAFETY: see `EmacDriver` doc.
691        let emac = unsafe { &*self.emac };
692        HardwareAddress::Ethernet(emac.mac_address())
693    }
694}
695
696// =============================================================================
697// Tests
698// =============================================================================
699
700#[cfg(test)]
701mod tests {
702    use super::*;
703
704    #[test]
705    fn state_starts_link_down() {
706        let s = EmacDriverState::new();
707        assert!(matches!(s.link_state(), LinkState::Down));
708    }
709
710    #[test]
711    fn state_link_set_up_then_down() {
712        let s = EmacDriverState::new();
713        s.set_link_up();
714        assert!(matches!(s.link_state(), LinkState::Up));
715        s.set_link_down();
716        assert!(matches!(s.link_state(), LinkState::Down));
717    }
718
719    #[test]
720    fn state_static_compatible() {
721        static STATE: EmacDriverState = EmacDriverState::new();
722        assert!(matches!(STATE.link_state(), LinkState::Down));
723    }
724
725    // ── Driver wrapper (host-side static behaviour) ──────────────
726
727    fn test_emac() -> Emac<10, 10, 1600> {
728        use crate::config::{ClkGpio, EmacConfig, RmiiClockConfig, RmiiPins, XtalFreq};
729
730        Emac::new(EmacConfig {
731            clock: RmiiClockConfig::InternalApll {
732                gpio: ClkGpio::Gpio17,
733                xtal: XtalFreq::Mhz40,
734            },
735            pins: RmiiPins { mdc: 23, mdio: 18 },
736        })
737    }
738
739    #[test]
740    fn driver_capabilities_advertise_mtu_and_burst() {
741        let mut emac = test_emac();
742        let state = EmacDriverState::new();
743        let driver = EmacDriver::new(&mut emac, &state);
744
745        let caps = driver.capabilities();
746        // 10 × 1600 ring fits a full Ethernet frame, so `effective_mtu`
747        // collapses to the standard ETH_MTU.
748        assert_eq!(caps.max_transmission_unit, ETH_MTU);
749        assert_eq!(caps.max_transmission_unit, 1514);
750        // Single-frame burst — the driver hands out one TX token at a
751        // time, so the stack should not pipeline more than one frame.
752        assert_eq!(caps.max_burst_size, Some(1));
753    }
754
755    #[test]
756    fn effective_mtu_caps_to_ring_capacity() {
757        // Standard configuration: ring is plenty large, full ETH MTU.
758        assert_eq!(EmacDriver::<10, 10, 1600>::effective_mtu(), ETH_MTU);
759        assert_eq!(EmacDriver::<4, 4, 1600>::effective_mtu(), ETH_MTU);
760        // Undersized ring: `TX * BUF = 1024` < 1514. We must NOT advertise
761        // 1514 — the engine can't transmit that. Capped to ring capacity.
762        assert_eq!(EmacDriver::<2, 2, 512>::effective_mtu(), 1024);
763        // Edge: exactly equal to ETH_MTU.
764        assert_eq!(EmacDriver::<1, 1, 1514>::effective_mtu(), ETH_MTU);
765        // One byte short.
766        assert_eq!(EmacDriver::<1, 1, 1513>::effective_mtu(), 1513);
767    }
768
769    #[test]
770    fn driver_hardware_address_reflects_cached_mac() {
771        let mut emac = test_emac();
772        // Before any `set_mac_address`, the cached value is the zero
773        // address — the bring-up code is expected to programme one
774        // before `init` reaches the address-filter step.
775        {
776            let state = EmacDriverState::new();
777            let driver = EmacDriver::new(&mut emac, &state);
778            let HardwareAddress::Ethernet(mac) = driver.hardware_address() else {
779                panic!("expected Ethernet hardware address");
780            };
781            assert_eq!(mac, [0u8; 6]);
782        }
783
784        // Cache a MAC; the driver should reflect it on the next read.
785        let custom = [0xF0, 0x57, 0x8D, 0x01, 0x04, 0xE3];
786        emac.set_mac_address(custom);
787
788        let state = EmacDriverState::new();
789        let driver = EmacDriver::new(&mut emac, &state);
790        let HardwareAddress::Ethernet(mac) = driver.hardware_address() else {
791            panic!("expected Ethernet hardware address");
792        };
793        assert_eq!(mac, custom);
794    }
795}