pub struct DmaEngine<const RX: usize, const TX: usize, const BUF: usize> { /* private fields */ }Expand description
DMA engine with statically allocated buffers.
§Const generics
RX: number of RX descriptors/buffersTX: number of TX descriptors/buffersBUF: buffer size per descriptor (bytes)
Implementations§
Source§impl<const RX: usize, const TX: usize, const BUF: usize> DmaEngine<RX, TX, BUF>
impl<const RX: usize, const TX: usize, const BUF: usize> DmaEngine<RX, TX, BUF>
Sourcepub fn init(&mut self) -> (u32, u32)
pub fn init(&mut self) -> (u32, u32)
Initialize descriptor chains.
Sets up chained descriptors: each points to its buffer and the next descriptor. The last descriptor chains back to the first (circular).
Returns (rx_base_addr, tx_base_addr) for programming DMA registers.
Sourcepub fn is_initialized(&self) -> bool
pub fn is_initialized(&self) -> bool
Check if initialized.
Sourcepub const fn memory_usage() -> usize
pub const fn memory_usage() -> usize
Calculate total static memory usage in bytes.
Sourcepub fn can_transmit(&self, len: usize) -> bool
pub fn can_transmit(&self, len: usize) -> bool
Check if there’s room to transmit a frame of given length.
Sourcepub fn tx_available(&self) -> usize
pub fn tx_available(&self) -> usize
Count available (CPU-owned) TX descriptors starting from current.
Sourcepub fn transmit(&mut self, data: &[u8]) -> Result<usize, EmacError>
pub fn transmit(&mut self, data: &[u8]) -> Result<usize, EmacError>
Submit a frame for transmission. Returns number of bytes sent.
For frames larger than BUF, uses multiple descriptors (scatter-gather).
Descriptors are given to DMA in reverse order to prevent a race where
the DMA starts processing before all descriptors are ready.
Sourcepub fn tx_reclaim(&mut self) -> usize
pub fn tx_reclaim(&mut self) -> usize
Reclaim completed TX descriptors (return from DMA to CPU ownership).
Returns the number of descriptors reclaimed.
Sourcepub fn rx_available(&self) -> bool
pub fn rx_available(&self) -> bool
Check if there is something for receive to do on the current
descriptor. Returns true when:
- the current descriptor is CPU-owned and carries an error flag
(CRC, overflow, etc.) —
receivewill recycle it as part of its error path, so the driver still needs to be woken; or - a complete (possibly multi-descriptor) frame is available, as
determined by
peek_frame_length.
Two earlier implementations both had bugs:
!current.is_owned() && current.is_last()missed multi- descriptor frames whose head wasn’t itselfLAST.peek_frame_length().is_some()fixed (1) but missed error frames (peek declines to report a length for them), so the embassy-net driver never calledreceiveto flush a CRC-failed frame — RX would wedge as the ring filled with untouched error descriptors.
Sourcepub fn receive(&mut self, buffer: &mut [u8]) -> Result<Option<usize>, EmacError>
pub fn receive(&mut self, buffer: &mut [u8]) -> Result<Option<usize>, EmacError>
Receive a frame into the provided buffer.
Returns the number of bytes received, or None if no frame is ready.
For single-descriptor frames, copies payload from the RX buffer.
For multi-descriptor frames, copies from each descriptor’s buffer.
Sourcepub fn peek_frame_length(&self) -> Option<usize>
pub fn peek_frame_length(&self) -> Option<usize>
Get the length of the next available frame without consuming it.
Sourcepub fn rx_free_count(&self) -> usize
pub fn rx_free_count(&self) -> usize
Count free RX descriptors (owned by DMA, ready to receive).
Sourcepub fn reset(&mut self) -> (u32, u32)
pub fn reset(&mut self) -> (u32, u32)
Reset all descriptors to initial state.
Re-initializes the chains and returns base addresses.
Sourcepub fn rx_ring_base(&self) -> u32
pub fn rx_ring_base(&self) -> u32
RX ring base address (for debugging).
Sourcepub fn tx_ring_base(&self) -> u32
pub fn tx_ring_base(&self) -> u32
TX ring base address (for debugging).
Sourcepub fn rx_current_index(&self) -> usize
pub fn rx_current_index(&self) -> usize
Current RX ring index.
Sourcepub fn tx_current_index(&self) -> usize
pub fn tx_current_index(&self) -> usize
Current TX ring index.