Skip to main content

esp_emac/
emac.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 ESP32 EMAC driver.
5//!
6//! Owns the DMA engine and drives the bring-up sequence directly via
7//! the local register helper modules in `crate::regs::*` and
8//! [`crate::reset::ResetController`]. No `ph-esp32-mac` dependency.
9
10use embedded_hal::delay::DelayNs;
11
12use crate::regs::dma as dma_regs;
13use crate::regs::ext as ext_regs;
14use crate::regs::gpio as gpio_matrix;
15use crate::regs::mac as mac_regs;
16use crate::reset::ResetController;
17
18use crate::config::{ClkGpio, EmacConfig, RmiiClockConfig};
19use crate::dma::engine::DmaEngine;
20use crate::error::EmacError;
21use crate::interrupt::InterruptStatus;
22use crate::regs::dma::{bus_mode, operation};
23use crate::regs::mac::{config, frame_filter};
24
25const TX_FIFO_FLUSH_TIMEOUT_US: u32 = 100_000;
26
27// =============================================================================
28// Link parameters and driver state
29// =============================================================================
30
31// Re-export the link-parameter enums from the trait crate so a PHY
32// driver's `LinkStatus` lands directly into `set_speed` / `set_duplex`
33// without the call-site `.into()` boilerplate that was needed when
34// these were duplicate local types. Keeping the types in one place
35// (eth_mdio_phy) also means a future minor-release variant addition
36// (`Speed::Mbps1000`) propagates through both ends of the stack with
37// a single bump.
38//
39// Gated by the `mdio-phy` feature because that feature is what pulls
40// `eth_mdio_phy` in as a dependency. Users without the feature can
41// still drop down to `crate::regs::mac::set_speed_100mbps` /
42// `set_duplex_full` directly — see the module-level docs.
43#[cfg(feature = "mdio-phy")]
44pub use eth_mdio_phy::{Duplex, Speed};
45
46/// EMAC driver state.
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48#[cfg_attr(feature = "defmt", derive(defmt::Format))]
49pub enum EmacState {
50    /// Not yet initialized.
51    Uninitialized,
52    /// `init()` succeeded but DMA/MAC are not running.
53    Initialized,
54    /// `start()` succeeded — DMA active, can transmit/receive.
55    Running,
56}
57
58// =============================================================================
59// EMAC driver
60// =============================================================================
61
62/// ESP32 EMAC driver with statically allocated DMA buffers.
63///
64/// The DMA descriptor chain is self-referential, so the driver MUST be
65/// placed in its final memory location BEFORE [`init`](Self::init) is
66/// called.
67pub struct Emac<const RX: usize = 10, const TX: usize = 10, const BUF: usize = 1600> {
68    dma: DmaEngine<RX, TX, BUF>,
69    config: EmacConfig,
70    state: EmacState,
71    mac_address: [u8; 6],
72}
73
74impl<const RX: usize, const TX: usize, const BUF: usize> Emac<RX, TX, BUF> {
75    /// Create a new (uninitialized) driver.
76    pub const fn new(config: EmacConfig) -> Self {
77        Self {
78            dma: DmaEngine::new(),
79            config,
80            state: EmacState::Uninitialized,
81            mac_address: [0; 6],
82        }
83    }
84
85    // ── State accessors ────────────────────────────────────────────────────
86
87    #[inline(always)]
88    pub fn state(&self) -> EmacState {
89        self.state
90    }
91
92    #[inline(always)]
93    pub fn mac_address(&self) -> [u8; 6] {
94        self.mac_address
95    }
96
97    #[inline(always)]
98    pub fn config(&self) -> &EmacConfig {
99        &self.config
100    }
101
102    /// Total static memory used by this EMAC instance.
103    pub const fn memory_usage() -> usize {
104        DmaEngine::<RX, TX, BUF>::memory_usage()
105    }
106
107    // ── Configuration ──────────────────────────────────────────────────────
108
109    /// Set the MAC address.
110    ///
111    /// If the driver has been initialized, the hardware filter registers
112    /// are updated immediately.
113    pub fn set_mac_address(&mut self, mac: [u8; 6]) {
114        self.mac_address = mac;
115        if self.state != EmacState::Uninitialized {
116            crate::regs::mac::set_mac_address(&mac);
117        }
118    }
119
120    /// Apply the link speed reported by the PHY.
121    ///
122    /// The ESP32 EMAC peripheral physically supports only 10 Mbps and
123    /// 100 Mbps. `Speed` is `#[non_exhaustive]` in the trait crate, so
124    /// future variants (e.g. a hypothetical `Mbps1000`) compile but
125    /// have no register encoding here. They are clamped to 100 Mbps —
126    /// the highest mode the EMAC actually supports — and a warning is
127    /// emitted under the `defmt` feature so the discrepancy is
128    /// visible at runtime.
129    ///
130    /// Available only with the `mdio-phy` feature, which is also what
131    /// pulls in the [`Speed`] type from `eth_mdio_phy`. Without the
132    /// feature, drop down to [`crate::regs::mac::set_speed_100mbps`].
133    #[cfg(feature = "mdio-phy")]
134    pub fn set_speed(&mut self, speed: Speed) {
135        if self.state == EmacState::Uninitialized {
136            return;
137        }
138        let is_100 = match speed {
139            Speed::Mbps10 => false,
140            Speed::Mbps100 => true,
141            _ => {
142                #[cfg(feature = "defmt")]
143                defmt::warn!(
144                    "esp-emac: unsupported Speed variant, clamping to 100 Mbps \
145                     (ESP32 EMAC is 10/100 only)"
146                );
147                true
148            }
149        };
150        mac_regs::set_speed_100mbps(is_100);
151    }
152
153    /// Apply the duplex mode reported by the PHY.
154    ///
155    /// `Duplex` is `#[non_exhaustive]` in the trait crate. ESP32 EMAC
156    /// has only the two MII-canonical modes (Half/Full); any future
157    /// variant is clamped to Full (the more permissive default) with
158    /// a `defmt::warn!` so the unexpected input doesn't pass silently.
159    ///
160    /// Available only with the `mdio-phy` feature, which is also what
161    /// pulls in the [`Duplex`] type from `eth_mdio_phy`. Without the
162    /// feature, drop down to [`crate::regs::mac::set_duplex_full`].
163    #[cfg(feature = "mdio-phy")]
164    pub fn set_duplex(&mut self, duplex: Duplex) {
165        if self.state == EmacState::Uninitialized {
166            return;
167        }
168        let is_full = match duplex {
169            Duplex::Half => false,
170            Duplex::Full => true,
171            _ => {
172                #[cfg(feature = "defmt")]
173                defmt::warn!(
174                    "esp-emac: unsupported Duplex variant, clamping to Full \
175                     (ESP32 EMAC supports Half/Full only)"
176                );
177                true
178            }
179        };
180        mac_regs::set_duplex_full(is_full);
181    }
182
183    // ── Initialization ─────────────────────────────────────────────────────
184
185    /// Initialize the EMAC peripheral.
186    ///
187    /// Sequence (mirrors the canonical ESP32 GMAC bring-up):
188    /// 1. APLL 50 MHz programming — only when MCU is the RMII clock master
189    ///    (`RmiiClockConfig::InternalApll`); skipped for `External`.
190    /// 2. RMII reference-clock pad routing (GPIO0 input for External,
191    ///    GPIO16/17 output for InternalApll).
192    /// 3. SMI + RMII data-pin routing.
193    /// 4. DPORT EMAC peripheral clock enable.
194    /// 5. PHY interface mode (RMII) + clock source select.
195    /// 6. EMAC extension clocks + RAM power-up.
196    /// 7. DMA software reset.
197    /// 8. MAC config defaults (PS/FES/DM/ACS/JD/WD).
198    /// 9. DMA bus mode + operation mode defaults.
199    /// 10. DMA descriptor chains and base-address registers.
200    /// 11. MAC address program.
201    pub fn init(&mut self, delay: &mut impl DelayNs) -> Result<(), EmacError> {
202        if self.state != EmacState::Uninitialized {
203            return Err(EmacError::AlreadyInitialized);
204        }
205
206        // 0. Validate user-configurable pins before touching any
207        //    registers, so a bad `EmacConfig::pins` is rejected loudly
208        //    rather than silently writing to unintended MMIO.
209        if !gpio_matrix::is_valid_smi_pin(self.config.pins.mdc)
210            || !gpio_matrix::is_valid_smi_pin(self.config.pins.mdio)
211        {
212            return Err(EmacError::InvalidConfig);
213        }
214
215        // RMII reference-clock pad direction on ESP32 is fixed by the
216        // IO_MUX function:
217        //
218        // - GPIO0  function 5 = `EMAC_TX_CLK`         — INPUT only
219        // - GPIO16 function 5 = `EMAC_CLK_OUT`        — OUTPUT only
220        // - GPIO17 function 5 = `EMAC_CLK_OUT_180`    — OUTPUT only
221        //
222        // External clock therefore requires GPIO0 (the only input pad);
223        // internal APLL output requires GPIO16 or GPIO17. Any other
224        // combination is hardware-impossible — reject it before we
225        // start writing IO_MUX bits.
226        match self.config.clock {
227            RmiiClockConfig::External { gpio } if !matches!(gpio, ClkGpio::Gpio0) => {
228                return Err(EmacError::InvalidConfig);
229            }
230            RmiiClockConfig::InternalApll {
231                gpio: ClkGpio::Gpio0,
232                ..
233            } => {
234                return Err(EmacError::InvalidConfig);
235            }
236            _ => {}
237        }
238
239        // 1. APLL — programmed only when the MCU is the RMII clock
240        //    master. SDM coefficients are picked from the configured
241        //    on-board crystal (`xtal`) so the same code lands on
242        //    50 MHz on 26/32/40 MHz boards alike. APLL is independent
243        //    of the EMAC peripheral clock (only writes RTC analog +
244        //    ROM I2C on the always-on APB), so order here doesn't
245        //    matter. Skipped entirely for `External`.
246        if let RmiiClockConfig::InternalApll { xtal, .. } = self.config.clock {
247            crate::clock::configure_apll_50mhz(xtal);
248        }
249
250        // 2. Route the RMII reference-clock pad: input on GPIO0 for
251        //    `External`, or output on GPIO16/17 for `InternalApll`.
252        match self.config.clock {
253            RmiiClockConfig::External { gpio } => crate::clock::configure_emac_clk_in(gpio),
254            RmiiClockConfig::InternalApll { gpio, .. } => {
255                crate::clock::configure_emac_clk_out(gpio)
256            }
257        }
258
259        // 3. Configure SMI pins (MDC/MDIO from `EmacConfig::pins`) and
260        //    RMII data pins (fixed function 5 — not configurable).
261        gpio_matrix::configure_smi_pins(self.config.pins.mdc, self.config.pins.mdio);
262        gpio_matrix::configure_rmii_pins();
263
264        // 4. Enable EMAC peripheral clock through DPORT.
265        ext_regs::enable_peripheral_clock();
266
267        // 5. PHY interface — RMII with the appropriate clock source.
268        ext_regs::set_rmii_mode();
269        match self.config.clock {
270            RmiiClockConfig::External { .. } => ext_regs::set_rmii_clock_external(),
271            RmiiClockConfig::InternalApll { .. } => ext_regs::set_rmii_clock_internal(),
272        }
273
274        // 6. EMAC extension clocks + RAM power.
275        ext_regs::enable_clocks();
276        ext_regs::power_up_ram();
277
278        // 7. Software reset of the DMA controller. `ResetController::new`
279        //    uses the canonical `crate::reset::SOFT_RESET_TIMEOUT_MS`
280        //    default — single source of truth for the reset window.
281        //    `ResetError::Timeout` converts to `EmacError::DmaResetTimeout`
282        //    via the `From` impl, so callers can distinguish DMA-stuck
283        //    from MDIO timeouts.
284        let mut reset_ctrl = ResetController::new(BorrowedDelay(delay));
285        reset_ctrl.soft_reset()?;
286
287        // 8. MAC configuration defaults: 100 Mbps full duplex, port select,
288        //    auto pad/CRC strip, jabber + watchdog disabled.
289        let mac_cfg = config::PORT_SELECT
290            | config::SPEED_100
291            | config::DUPLEX_FULL
292            | config::AUTO_PAD_CRC_STRIP
293            | config::JABBER_DISABLE
294            | config::WATCHDOG_DISABLE;
295        mac_regs::set_config(mac_cfg);
296
297        // Frame filter: pass all multicast (broadcast accepted by default).
298        mac_regs::set_frame_filter(frame_filter::PASS_ALL_MULTICAST);
299        mac_regs::set_hash_table(0);
300
301        // 9. DMA bus mode and operation mode.
302        //
303        // ATDS = enhanced 8-word descriptor layout (32 bytes per
304        // descriptor). Our `dma::descriptor::{TxDescriptor,
305        // RxDescriptor}` are now 8 words to match.
306        let pbl = 32u32;
307        let bus = bus_mode::FIXED_BURST
308            | bus_mode::AAL
309            | bus_mode::USP
310            | bus_mode::ATDS
311            | ((pbl << bus_mode::PBL_SHIFT) & bus_mode::PBL_MASK);
312        dma_regs::set_bus_mode(bus);
313        dma_regs::set_operation_mode(operation::TSF | operation::RSF);
314        dma_regs::disable_all_interrupts();
315        dma_regs::clear_all_interrupts();
316
317        // 10. Descriptor chains. Returns physical base addresses suitable for
318        //     DMARXBASEADDR / DMATXBASEADDR.
319        let (rx_base, tx_base) = self.dma.init();
320        dma_regs::set_rx_desc_list_addr(rx_base);
321        dma_regs::set_tx_desc_list_addr(tx_base);
322
323        // 11. Programme the MAC address into ADDR0H / ADDR0L (with AE bit).
324        //     The internal filter latch on this Synopsys GMAC fires on the
325        //     LOW write — `regs::mac::set_mac_address` writes HIGH first to
326        //     keep the AE bit, then LOW to trigger the latch.
327        crate::regs::mac::set_mac_address(&self.mac_address);
328
329        self.state = EmacState::Initialized;
330        Ok(())
331    }
332
333    /// Start TX/RX (DMA + MAC).
334    pub fn start(&mut self) -> Result<(), EmacError> {
335        match self.state {
336            EmacState::Initialized => {}
337            EmacState::Running => return Ok(()),
338            EmacState::Uninitialized => return Err(EmacError::NotInitialized),
339        }
340
341        // Reset descriptor ownership in case of a previous run, then
342        // re-program `DMARXBASEADDR` / `DMATXBASEADDR` from the base
343        // addresses the engine returns. `dma.reset()` rebuilds chains
344        // and zeroes the software `current_index`; the hardware DMA
345        // pointer wherever it last was (middle of the ring after a
346        // `stop()`/`start()` cycle, or unset on the very first start)
347        // must be put back on the chain head, otherwise software and
348        // hardware will walk different descriptors and RX wedges.
349        let (rx_base, tx_base) = self.dma.reset();
350        dma_regs::set_rx_desc_list_addr(rx_base);
351        dma_regs::set_tx_desc_list_addr(tx_base);
352
353        dma_regs::clear_all_interrupts();
354        dma_regs::enable_default_interrupts();
355
356        // Enable MAC TX, then DMA TX, DMA RX, then MAC RX (matches the
357        // ordering from the ESP32 reference manual / IDF EMAC driver).
358        let cfg = mac_regs::config();
359        mac_regs::set_config(cfg | config::TX_ENABLE);
360
361        dma_regs::start_tx();
362        dma_regs::start_rx();
363
364        let cfg = mac_regs::config();
365        mac_regs::set_config(cfg | config::RX_ENABLE);
366
367        // Issue an RX poll demand so the DMA does not stay in Suspended
368        // state if all descriptors were already CPU-owned.
369        dma_regs::rx_poll_demand();
370
371        self.state = EmacState::Running;
372        Ok(())
373    }
374
375    /// Stop TX/RX.
376    ///
377    /// Polls the TX-FIFO flush bit (`FTF`) for up to
378    /// `TX_FIFO_FLUSH_TIMEOUT_US` microseconds, sleeping `delay` between
379    /// polls so the DMA actually has time to drain. The rest of the
380    /// teardown (MAC RX/TX disable, DMA RX stop, interrupt-status
381    /// clear, state transition to `Initialized`) runs unconditionally
382    /// — even on flush timeout the driver winds up in `Initialized`
383    /// and is safe to re-`start()`.
384    ///
385    /// Returns:
386    /// - `Ok(())` on a clean teardown (FTF self-cleared in time).
387    /// - `Err(EmacError::TxFlushTimeout)` when the FTF poll exhausted
388    ///   `TX_FIFO_FLUSH_TIMEOUT_US`. Teardown still completed — at
389    ///   least one in-flight TX frame may have been truncated on the
390    ///   wire. `state` is `Initialized` either way, so a follow-up
391    ///   `start()` is the recoverable path. There is no in-crate
392    ///   "full re-init" — [`Emac::init`] is one-shot — so a terminal
393    ///   recovery means a peripheral or SoC reset from the
394    ///   application layer.
395    /// - `Err(EmacError::NotInitialized)` if called from `Uninitialized`.
396    ///
397    /// Idempotent on an already-stopped driver: calling `stop` while
398    /// in `Initialized` returns `Ok(())` without touching hardware.
399    pub fn stop(&mut self, delay: &mut impl DelayNs) -> Result<(), EmacError> {
400        match self.state {
401            EmacState::Running => {} // proceed with the tear-down below
402            EmacState::Initialized => return Ok(()),
403            EmacState::Uninitialized => return Err(EmacError::NotInitialized),
404        }
405
406        // Stop DMA TX, wait for in-flight data to drain (best effort).
407        dma_regs::stop_tx();
408
409        // Flush TX FIFO and wait for the bit to self-clear.
410        dma_regs::flush_tx_fifo();
411        const POLL_STEP_US: u32 = 10;
412        let mut waited_us = 0u32;
413        let mut flush_timed_out = true;
414        while waited_us < TX_FIFO_FLUSH_TIMEOUT_US {
415            if (dma_regs::operation_mode() & operation::FTF) == 0 {
416                flush_timed_out = false;
417                break;
418            }
419            delay.delay_us(POLL_STEP_US);
420            waited_us += POLL_STEP_US;
421        }
422
423        // Disable MAC TX and RX, then DMA RX.
424        let cfg = mac_regs::config();
425        mac_regs::set_config(cfg & !(config::TX_ENABLE | config::RX_ENABLE));
426
427        dma_regs::stop_rx();
428        dma_regs::disable_all_interrupts();
429        // Acknowledge any W1C bits that latched in DMASTATUS while the
430        // engine was running, so a future `start()` doesn't observe
431        // stale flags through `last_dmastat` / `interrupt_status` and
432        // a re-enable from outside the driver doesn't fire spuriously.
433        dma_regs::clear_all_interrupts();
434
435        self.state = EmacState::Initialized;
436
437        if flush_timed_out {
438            Err(EmacError::TxFlushTimeout)
439        } else {
440            Ok(())
441        }
442    }
443
444    // ── Frame I/O ─────────────────────────────────────────────────────────
445
446    /// Transmit a frame.
447    ///
448    /// Does not block on descriptor availability — caller must check
449    /// [`can_transmit`](Self::can_transmit) (or [`tx_ready`](Self::tx_ready)
450    /// for single-descriptor frames) before calling, or be ready to handle
451    /// `EmacError::NoDescriptorsAvailable` / `EmacError::DescriptorBusy`
452    /// when the TX ring is full, and `EmacError::FrameTooLarge` when the
453    /// payload exceeds the ring's combined capacity.
454    pub fn transmit(&mut self, data: &[u8]) -> Result<usize, EmacError> {
455        if self.state != EmacState::Running {
456            return Err(EmacError::NotInitialized);
457        }
458        let n = self.dma.transmit(data)?;
459        // Kick TX DMA out of suspended state if we just refilled descriptors.
460        dma_regs::tx_poll_demand();
461        Ok(n)
462    }
463
464    /// Receive a frame, if any.
465    ///
466    /// Issues an RX poll-demand whenever a descriptor was potentially
467    /// recycled by `DmaEngine::receive` — that includes the success
468    /// path (`Ok(Some(_))`) **and** the error paths (`FrameError`,
469    /// `BufferTooSmall`, …) where the engine still hands the descriptor
470    /// back to the DMA. Only `Ok(None)` skips the kick, since nothing
471    /// in the ring changed. Without this, an errored frame on a
472    /// suspended ring would leave RX wedged with the `RU` bit asserted
473    /// until the next *successful* receive — exactly the kind of
474    /// post-error hang we hit in the field.
475    pub fn receive(&mut self, buffer: &mut [u8]) -> Result<Option<usize>, EmacError> {
476        if self.state != EmacState::Running {
477            return Err(EmacError::NotInitialized);
478        }
479        let result = self.dma.receive(buffer);
480        if !matches!(result, Ok(None)) {
481            dma_regs::rx_poll_demand();
482        }
483        result
484    }
485
486    /// Whether a received frame is currently waiting in the ring.
487    #[inline(always)]
488    pub fn rx_available(&self) -> bool {
489        self.dma.rx_available()
490    }
491
492    /// Whether the TX ring has room for a frame of `len` bytes.
493    #[inline(always)]
494    pub fn can_transmit(&self, len: usize) -> bool {
495        self.dma.can_transmit(len)
496    }
497
498    /// Whether at least one TX descriptor is available for the next frame.
499    #[inline(always)]
500    pub fn tx_ready(&self) -> bool {
501        self.dma.tx_available() > 0
502    }
503
504    // ── Interrupt helpers ──────────────────────────────────────────────────
505
506    /// Bind an interrupt handler to the EMAC peripheral and enable the
507    /// interrupt at the chip level.
508    #[cfg(feature = "esp-hal")]
509    pub fn bind_interrupt(&mut self, handler: esp_hal::interrupt::InterruptHandler) {
510        use esp_hal::peripherals::Interrupt;
511
512        for core in esp_hal::system::Cpu::other() {
513            esp_hal::interrupt::disable(core, Interrupt::ETH_MAC);
514        }
515        esp_hal::interrupt::bind_handler(Interrupt::ETH_MAC, handler);
516        esp_hal::interrupt::enable(Interrupt::ETH_MAC, handler.priority());
517    }
518
519    /// Disable the EMAC interrupt at the chip level.
520    #[cfg(feature = "esp-hal")]
521    pub fn disable_interrupt(&mut self) {
522        use esp_hal::peripherals::Interrupt;
523        esp_hal::interrupt::disable(esp_hal::system::Cpu::current(), Interrupt::ETH_MAC);
524    }
525
526    /// Read and parse the DMA status register.
527    pub fn interrupt_status(&self) -> InterruptStatus {
528        // SAFETY: read from a known-valid memory-mapped register.
529        let raw = unsafe {
530            core::ptr::read_volatile(
531                (crate::regs::dma::BASE + crate::regs::dma::DMASTATUS) as *const u32,
532            )
533        };
534        InterruptStatus::from_raw(raw)
535    }
536
537    /// Clear DMA status flags via write-1-to-clear.
538    ///
539    /// Writes the raw register snapshot back into `DMASTATUS`,
540    /// masked against [`crate::regs::dma::status::ALL_INTERRUPTS`] so
541    /// only the documented W1C interrupt bits are touched. The
542    /// non-W1C fields in `DMASTATUS` — `RS`/`TS` (process state),
543    /// `EB` (error bits), `MMC`/`PMT`/`TTI` — are read-only and
544    /// silently ignored by the hardware on write, but masking them
545    /// keeps the contract explicit: every bit we send is something
546    /// we mean to acknowledge.
547    ///
548    /// Pass the raw snapshot you previously read so every W1C bit
549    /// (including ones not modeled in [`InterruptStatus`] such as
550    /// `ERI` / `ETI` / `RWT`) is acknowledged in a single write.
551    pub fn clear_interrupts_raw(&self, raw: u32) {
552        // SAFETY: write to a known-valid memory-mapped register.
553        unsafe {
554            core::ptr::write_volatile(
555                (crate::regs::dma::BASE + crate::regs::dma::DMASTATUS) as *mut u32,
556                raw & crate::regs::dma::status::ALL_INTERRUPTS,
557            );
558        }
559    }
560
561    /// Convenience: handle the ISR — read status, clear all flags
562    /// (via the raw snapshot, so unrepresented W1C bits are also
563    /// acknowledged), return the parsed copy.
564    pub fn handle_interrupt(&self) -> InterruptStatus {
565        // SAFETY: read from a known-valid memory-mapped register.
566        let raw = unsafe {
567            core::ptr::read_volatile(
568                (crate::regs::dma::BASE + crate::regs::dma::DMASTATUS) as *const u32,
569            )
570        };
571        self.clear_interrupts_raw(raw);
572        InterruptStatus::from_raw(raw)
573    }
574}
575
576// `Default for Emac` is intentionally not implemented. The clock and pin
577// configuration is hardware-specific and silently picking one (e.g.
578// internal APLL on GPIO17) would mis-drive any board that expects an
579// external PHY-driven clock or that routes MDC/MDIO to non-default
580// GPIOs. Callers must construct an explicit `EmacConfig` — see the
581// crate-level docs and `RmiiClockConfig` for the available modes.
582
583// ── Default ring sizings ──────────────────────────────────────────────
584//
585// Single source of truth for the const generics that parameterize the
586// `EmacDefault` / `EmacSmall` aliases on the MAC side and the matching
587// `EmacDefaultDriver` / `EmacSmallDriver` aliases in `embassy.rs`. Keep
588// the driver aliases pulled from these constants — retuning a value
589// here updates both alias families together.
590
591/// RX descriptor ring size for [`EmacDefault`].
592pub const DEFAULT_RX: usize = 10;
593/// TX descriptor ring size for [`EmacDefault`].
594pub const DEFAULT_TX: usize = 10;
595/// Per-buffer length (bytes) for [`EmacDefault`] / [`EmacSmall`].
596pub const DEFAULT_BUF: usize = 1600;
597
598/// RX descriptor ring size for [`EmacSmall`].
599pub const SMALL_RX: usize = 4;
600/// TX descriptor ring size for [`EmacSmall`].
601pub const SMALL_TX: usize = 4;
602
603/// Convenience alias: [`DEFAULT_RX`] RX / [`DEFAULT_TX`] TX /
604/// [`DEFAULT_BUF`]-byte buffers (10/10/1600).
605pub type EmacDefault = Emac<DEFAULT_RX, DEFAULT_TX, DEFAULT_BUF>;
606
607/// Convenience alias: [`SMALL_RX`] RX / [`SMALL_TX`] TX /
608/// [`DEFAULT_BUF`]-byte buffers (4/4/1600).
609pub type EmacSmall = Emac<SMALL_RX, SMALL_TX, DEFAULT_BUF>;
610
611// =============================================================================
612// Helpers
613// =============================================================================
614
615/// Wraps a `&mut DelayNs` so it can be passed by value to APIs that take
616/// an owned `DelayNs` implementor (such as
617/// [`crate::reset::ResetController::with_timeout`]).
618struct BorrowedDelay<'a, D: DelayNs + ?Sized>(&'a mut D);
619
620impl<D: DelayNs + ?Sized> DelayNs for BorrowedDelay<'_, D> {
621    fn delay_ns(&mut self, ns: u32) {
622        self.0.delay_ns(ns);
623    }
624}
625
626// =============================================================================
627// Tests
628// =============================================================================
629
630#[cfg(test)]
631mod tests {
632    use super::*;
633
634    fn test_config() -> EmacConfig {
635        EmacConfig {
636            clock: RmiiClockConfig::InternalApll {
637                gpio: ClkGpio::Gpio17,
638                xtal: crate::config::XtalFreq::Mhz40,
639            },
640            pins: crate::config::RmiiPins::default(),
641        }
642    }
643
644    #[test]
645    fn new_is_uninitialized() {
646        let emac: EmacDefault = Emac::new(test_config());
647        assert_eq!(emac.state(), EmacState::Uninitialized);
648        assert_eq!(emac.mac_address(), [0u8; 6]);
649    }
650
651    #[test]
652    fn set_mac_before_init_only_caches() {
653        let mut emac: EmacDefault = Emac::new(test_config());
654        let mac = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66];
655        emac.set_mac_address(mac);
656        assert_eq!(emac.mac_address(), mac);
657        // No register writes performed because state is Uninitialized.
658    }
659
660    #[test]
661    fn memory_usage_matches_dma() {
662        // Source the comparison from the same constants as the alias
663        // itself — retuning `DEFAULT_*` continues to match without
664        // touching this test.
665        assert_eq!(
666            EmacDefault::memory_usage(),
667            DmaEngine::<DEFAULT_RX, DEFAULT_TX, DEFAULT_BUF>::memory_usage()
668        );
669    }
670}