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 //
290 // CHECKSUM_OFFLOAD (IPC, bit 10): enables RX IPv4/TCP/UDP/ICMP
291 // checksum verification. The MAC stores the result in RDES4
292 // (extended descriptor, ATDS=1). With DMAOPERATION.DT=0 (our
293 // default), the DMA automatically drops frames with checksum
294 // errors before they reach the CPU, so the SW receive path
295 // sees only frames the hardware has verified as good.
296 let mac_cfg = config::PORT_SELECT
297 | config::SPEED_100
298 | config::DUPLEX_FULL
299 | config::AUTO_PAD_CRC_STRIP
300 | config::JABBER_DISABLE
301 | config::WATCHDOG_DISABLE
302 | config::CHECKSUM_OFFLOAD;
303 mac_regs::set_config(mac_cfg);
304
305 // Frame filter: pass all multicast (broadcast accepted by default).
306 mac_regs::set_frame_filter(frame_filter::PASS_ALL_MULTICAST);
307 mac_regs::set_hash_table(0);
308
309 // 9. DMA bus mode and operation mode.
310 //
311 // ATDS = enhanced 8-word descriptor layout (32 bytes per
312 // descriptor). Our `dma::descriptor::{TxDescriptor,
313 // RxDescriptor}` are now 8 words to match.
314 let pbl = 32u32;
315 let bus = bus_mode::FIXED_BURST
316 | bus_mode::AAL
317 | bus_mode::USP
318 | bus_mode::ATDS
319 | ((pbl << bus_mode::PBL_SHIFT) & bus_mode::PBL_MASK);
320 dma_regs::set_bus_mode(bus);
321 dma_regs::set_operation_mode(operation::TSF | operation::RSF);
322 dma_regs::disable_all_interrupts();
323 dma_regs::clear_all_interrupts();
324
325 // 10. Descriptor chains. Returns physical base addresses suitable for
326 // DMARXBASEADDR / DMATXBASEADDR.
327 let (rx_base, tx_base) = self.dma.init();
328 dma_regs::set_rx_desc_list_addr(rx_base);
329 dma_regs::set_tx_desc_list_addr(tx_base);
330
331 // 11. Programme the MAC address into ADDR0H / ADDR0L (with AE bit).
332 // The internal filter latch on this Synopsys GMAC fires on the
333 // LOW write — `regs::mac::set_mac_address` writes HIGH first to
334 // keep the AE bit, then LOW to trigger the latch.
335 crate::regs::mac::set_mac_address(&self.mac_address);
336
337 self.state = EmacState::Initialized;
338 Ok(())
339 }
340
341 /// Start TX/RX (DMA + MAC).
342 pub fn start(&mut self) -> Result<(), EmacError> {
343 match self.state {
344 EmacState::Initialized => {}
345 EmacState::Running => return Ok(()),
346 EmacState::Uninitialized => return Err(EmacError::NotInitialized),
347 }
348
349 // Reset descriptor ownership in case of a previous run, then
350 // re-program `DMARXBASEADDR` / `DMATXBASEADDR` from the base
351 // addresses the engine returns. `dma.reset()` rebuilds chains
352 // and zeroes the software `current_index`; the hardware DMA
353 // pointer wherever it last was (middle of the ring after a
354 // `stop()`/`start()` cycle, or unset on the very first start)
355 // must be put back on the chain head, otherwise software and
356 // hardware will walk different descriptors and RX wedges.
357 let (rx_base, tx_base) = self.dma.reset();
358 dma_regs::set_rx_desc_list_addr(rx_base);
359 dma_regs::set_tx_desc_list_addr(tx_base);
360
361 dma_regs::clear_all_interrupts();
362 dma_regs::enable_default_interrupts();
363
364 // Enable MAC TX, then DMA TX, DMA RX, then MAC RX (matches the
365 // ordering from the ESP32 reference manual / IDF EMAC driver).
366 let cfg = mac_regs::config();
367 mac_regs::set_config(cfg | config::TX_ENABLE);
368
369 dma_regs::start_tx();
370 dma_regs::start_rx();
371
372 let cfg = mac_regs::config();
373 mac_regs::set_config(cfg | config::RX_ENABLE);
374
375 // Issue an RX poll demand so the DMA does not stay in Suspended
376 // state if all descriptors were already CPU-owned.
377 dma_regs::rx_poll_demand();
378
379 self.state = EmacState::Running;
380 Ok(())
381 }
382
383 /// Stop TX/RX.
384 ///
385 /// Polls the TX-FIFO flush bit (`FTF`) for up to
386 /// `TX_FIFO_FLUSH_TIMEOUT_US` microseconds, sleeping `delay` between
387 /// polls so the DMA actually has time to drain. The rest of the
388 /// teardown (MAC RX/TX disable, DMA RX stop, interrupt-status
389 /// clear, state transition to `Initialized`) runs unconditionally
390 /// — even on flush timeout the driver winds up in `Initialized`
391 /// and is safe to re-`start()`.
392 ///
393 /// Returns:
394 /// - `Ok(())` on a clean teardown (FTF self-cleared in time).
395 /// - `Err(EmacError::TxFlushTimeout)` when the FTF poll exhausted
396 /// `TX_FIFO_FLUSH_TIMEOUT_US`. Teardown still completed — at
397 /// least one in-flight TX frame may have been truncated on the
398 /// wire. `state` is `Initialized` either way, so a follow-up
399 /// `start()` is the recoverable path. There is no in-crate
400 /// "full re-init" — [`Emac::init`] is one-shot — so a terminal
401 /// recovery means a peripheral or SoC reset from the
402 /// application layer.
403 /// - `Err(EmacError::NotInitialized)` if called from `Uninitialized`.
404 ///
405 /// Idempotent on an already-stopped driver: calling `stop` while
406 /// in `Initialized` returns `Ok(())` without touching hardware.
407 pub fn stop(&mut self, delay: &mut impl DelayNs) -> Result<(), EmacError> {
408 match self.state {
409 EmacState::Running => {} // proceed with the tear-down below
410 EmacState::Initialized => return Ok(()),
411 EmacState::Uninitialized => return Err(EmacError::NotInitialized),
412 }
413
414 // Stop DMA TX, wait for in-flight data to drain (best effort).
415 dma_regs::stop_tx();
416
417 // Flush TX FIFO and wait for the bit to self-clear.
418 dma_regs::flush_tx_fifo();
419 const POLL_STEP_US: u32 = 10;
420 let mut waited_us = 0u32;
421 let mut flush_timed_out = true;
422 while waited_us < TX_FIFO_FLUSH_TIMEOUT_US {
423 if (dma_regs::operation_mode() & operation::FTF) == 0 {
424 flush_timed_out = false;
425 break;
426 }
427 delay.delay_us(POLL_STEP_US);
428 waited_us += POLL_STEP_US;
429 }
430
431 // Disable MAC TX and RX, then DMA RX.
432 let cfg = mac_regs::config();
433 mac_regs::set_config(cfg & !(config::TX_ENABLE | config::RX_ENABLE));
434
435 dma_regs::stop_rx();
436 dma_regs::disable_all_interrupts();
437 // Acknowledge any W1C bits that latched in DMASTATUS while the
438 // engine was running, so a future `start()` doesn't observe
439 // stale flags through `last_dmastat` / `interrupt_status` and
440 // a re-enable from outside the driver doesn't fire spuriously.
441 dma_regs::clear_all_interrupts();
442
443 self.state = EmacState::Initialized;
444
445 if flush_timed_out {
446 Err(EmacError::TxFlushTimeout)
447 } else {
448 Ok(())
449 }
450 }
451
452 // ── Frame I/O ─────────────────────────────────────────────────────────
453
454 /// Transmit a frame.
455 ///
456 /// Does not block on descriptor availability — caller must check
457 /// [`can_transmit`](Self::can_transmit) (or [`tx_ready`](Self::tx_ready)
458 /// for single-descriptor frames) before calling, or be ready to handle
459 /// `EmacError::NoDescriptorsAvailable` / `EmacError::DescriptorBusy`
460 /// when the TX ring is full, and `EmacError::FrameTooLarge` when the
461 /// payload exceeds the ring's combined capacity.
462 pub fn transmit(&mut self, data: &[u8]) -> Result<usize, EmacError> {
463 if self.state != EmacState::Running {
464 return Err(EmacError::NotInitialized);
465 }
466 let n = self.dma.transmit(data)?;
467 // Kick TX DMA out of suspended state if we just refilled descriptors.
468 dma_regs::tx_poll_demand();
469 Ok(n)
470 }
471
472 /// Receive a frame, if any.
473 ///
474 /// Issues an RX poll-demand whenever a descriptor was potentially
475 /// recycled by `DmaEngine::receive` — that includes the success
476 /// path (`Ok(Some(_))`) **and** the error paths (`FrameError`,
477 /// `BufferTooSmall`, …) where the engine still hands the descriptor
478 /// back to the DMA. Only `Ok(None)` skips the kick, since nothing
479 /// in the ring changed. Without this, an errored frame on a
480 /// suspended ring would leave RX wedged with the `RU` bit asserted
481 /// until the next *successful* receive — exactly the kind of
482 /// post-error hang we hit in the field.
483 pub fn receive(&mut self, buffer: &mut [u8]) -> Result<Option<usize>, EmacError> {
484 if self.state != EmacState::Running {
485 return Err(EmacError::NotInitialized);
486 }
487 let result = self.dma.receive(buffer);
488 if !matches!(result, Ok(None)) {
489 dma_regs::rx_poll_demand();
490 }
491 result
492 }
493
494 /// Whether a received frame is currently waiting in the ring.
495 #[inline(always)]
496 pub fn rx_available(&self) -> bool {
497 self.dma.rx_available()
498 }
499
500 /// Whether the TX ring has room for a frame of `len` bytes.
501 #[inline(always)]
502 pub fn can_transmit(&self, len: usize) -> bool {
503 self.dma.can_transmit(len)
504 }
505
506 /// Whether at least one TX descriptor is available for the next frame.
507 #[inline(always)]
508 pub fn tx_ready(&self) -> bool {
509 self.dma.tx_available() > 0
510 }
511
512 // ── Interrupt helpers ──────────────────────────────────────────────────
513
514 /// Bind an interrupt handler to the EMAC peripheral and enable the
515 /// interrupt at the chip level.
516 #[cfg(feature = "esp-hal")]
517 pub fn bind_interrupt(&mut self, handler: esp_hal::interrupt::InterruptHandler) {
518 use esp_hal::peripherals::Interrupt;
519
520 for core in esp_hal::system::Cpu::other() {
521 esp_hal::interrupt::disable(core, Interrupt::ETH_MAC);
522 }
523 esp_hal::interrupt::bind_handler(Interrupt::ETH_MAC, handler);
524 esp_hal::interrupt::enable(Interrupt::ETH_MAC, handler.priority());
525 }
526
527 /// Disable the EMAC interrupt at the chip level.
528 #[cfg(feature = "esp-hal")]
529 pub fn disable_interrupt(&mut self) {
530 use esp_hal::peripherals::Interrupt;
531 esp_hal::interrupt::disable(esp_hal::system::Cpu::current(), Interrupt::ETH_MAC);
532 }
533
534 /// Read and parse the DMA status register.
535 pub fn interrupt_status(&self) -> InterruptStatus {
536 // SAFETY: read from a known-valid memory-mapped register.
537 let raw = unsafe {
538 core::ptr::read_volatile(
539 (crate::regs::dma::BASE + crate::regs::dma::DMASTATUS) as *const u32,
540 )
541 };
542 InterruptStatus::from_raw(raw)
543 }
544
545 /// Clear DMA status flags via write-1-to-clear.
546 ///
547 /// Writes the raw register snapshot back into `DMASTATUS`,
548 /// masked against [`crate::regs::dma::status::ALL_INTERRUPTS`] so
549 /// only the documented W1C interrupt bits are touched. The
550 /// non-W1C fields in `DMASTATUS` — `RS`/`TS` (process state),
551 /// `EB` (error bits), `MMC`/`PMT`/`TTI` — are read-only and
552 /// silently ignored by the hardware on write, but masking them
553 /// keeps the contract explicit: every bit we send is something
554 /// we mean to acknowledge.
555 ///
556 /// Pass the raw snapshot you previously read so every W1C bit
557 /// (including ones not modeled in [`InterruptStatus`] such as
558 /// `ERI` / `ETI` / `RWT`) is acknowledged in a single write.
559 pub fn clear_interrupts_raw(&self, raw: u32) {
560 // SAFETY: write to a known-valid memory-mapped register.
561 unsafe {
562 core::ptr::write_volatile(
563 (crate::regs::dma::BASE + crate::regs::dma::DMASTATUS) as *mut u32,
564 raw & crate::regs::dma::status::ALL_INTERRUPTS,
565 );
566 }
567 }
568
569 /// Convenience: handle the ISR — read status, clear all flags
570 /// (via the raw snapshot, so unrepresented W1C bits are also
571 /// acknowledged), return the parsed copy.
572 pub fn handle_interrupt(&self) -> InterruptStatus {
573 // SAFETY: read from a known-valid memory-mapped register.
574 let raw = unsafe {
575 core::ptr::read_volatile(
576 (crate::regs::dma::BASE + crate::regs::dma::DMASTATUS) as *const u32,
577 )
578 };
579 self.clear_interrupts_raw(raw);
580 InterruptStatus::from_raw(raw)
581 }
582}
583
584// `Default for Emac` is intentionally not implemented. The clock and pin
585// configuration is hardware-specific and silently picking one (e.g.
586// internal APLL on GPIO17) would mis-drive any board that expects an
587// external PHY-driven clock or that routes MDC/MDIO to non-default
588// GPIOs. Callers must construct an explicit `EmacConfig` — see the
589// crate-level docs and `RmiiClockConfig` for the available modes.
590
591// ── Default ring sizings ──────────────────────────────────────────────
592//
593// Single source of truth for the const generics that parameterize the
594// `EmacDefault` / `EmacSmall` aliases on the MAC side and the matching
595// `EmacDefaultDriver` / `EmacSmallDriver` aliases in `embassy.rs`. Keep
596// the driver aliases pulled from these constants — retuning a value
597// here updates both alias families together.
598
599/// RX descriptor ring size for [`EmacDefault`].
600pub const DEFAULT_RX: usize = 10;
601/// TX descriptor ring size for [`EmacDefault`].
602pub const DEFAULT_TX: usize = 10;
603/// Per-buffer length (bytes) for [`EmacDefault`] / [`EmacSmall`].
604pub const DEFAULT_BUF: usize = 1600;
605
606/// RX descriptor ring size for [`EmacSmall`].
607pub const SMALL_RX: usize = 4;
608/// TX descriptor ring size for [`EmacSmall`].
609pub const SMALL_TX: usize = 4;
610
611/// Convenience alias: [`DEFAULT_RX`] RX / [`DEFAULT_TX`] TX /
612/// [`DEFAULT_BUF`]-byte buffers (10/10/1600).
613pub type EmacDefault = Emac<DEFAULT_RX, DEFAULT_TX, DEFAULT_BUF>;
614
615/// Convenience alias: [`SMALL_RX`] RX / [`SMALL_TX`] TX /
616/// [`DEFAULT_BUF`]-byte buffers (4/4/1600).
617pub type EmacSmall = Emac<SMALL_RX, SMALL_TX, DEFAULT_BUF>;
618
619// =============================================================================
620// Helpers
621// =============================================================================
622
623/// Wraps a `&mut DelayNs` so it can be passed by value to APIs that take
624/// an owned `DelayNs` implementor (such as
625/// [`crate::reset::ResetController::with_timeout`]).
626struct BorrowedDelay<'a, D: DelayNs + ?Sized>(&'a mut D);
627
628impl<D: DelayNs + ?Sized> DelayNs for BorrowedDelay<'_, D> {
629 fn delay_ns(&mut self, ns: u32) {
630 self.0.delay_ns(ns);
631 }
632}
633
634// =============================================================================
635// Tests
636// =============================================================================
637
638#[cfg(test)]
639mod tests {
640 use super::*;
641
642 fn test_config() -> EmacConfig {
643 EmacConfig {
644 clock: RmiiClockConfig::InternalApll {
645 gpio: ClkGpio::Gpio17,
646 xtal: crate::config::XtalFreq::Mhz40,
647 },
648 pins: crate::config::RmiiPins::default(),
649 }
650 }
651
652 #[test]
653 fn new_is_uninitialized() {
654 let emac: EmacDefault = Emac::new(test_config());
655 assert_eq!(emac.state(), EmacState::Uninitialized);
656 assert_eq!(emac.mac_address(), [0u8; 6]);
657 }
658
659 #[test]
660 fn set_mac_before_init_only_caches() {
661 let mut emac: EmacDefault = Emac::new(test_config());
662 let mac = [0x11, 0x22, 0x33, 0x44, 0x55, 0x66];
663 emac.set_mac_address(mac);
664 assert_eq!(emac.mac_address(), mac);
665 // No register writes performed because state is Uninitialized.
666 }
667
668 #[test]
669 fn memory_usage_matches_dma() {
670 // Source the comparison from the same constants as the alias
671 // itself — retuning `DEFAULT_*` continues to match without
672 // touching this test.
673 assert_eq!(
674 EmacDefault::memory_usage(),
675 DmaEngine::<DEFAULT_RX, DEFAULT_TX, DEFAULT_BUF>::memory_usage()
676 );
677 }
678}