From 81a892b618aba5f4808d254a8cd319b04ccb9421 Mon Sep 17 00:00:00 2001 From: Astro Date: Mon, 10 Jun 2019 02:44:29 +0200 Subject: [PATCH] eth: recv_next() --- src/eth/mod.rs | 6 ++++++ src/eth/rx.rs | 22 +++++++++++++++++++++- src/main.rs | 9 ++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/eth/mod.rs b/src/eth/mod.rs index 3ba5a6d..e7ed093 100644 --- a/src/eth/mod.rs +++ b/src/eth/mod.rs @@ -332,6 +332,12 @@ impl Eth { } } +impl<'rx, TX> Eth, TX> { + pub fn recv_next(&mut self) -> Option<&[u8]> { + self.rx.recv_next() + } +} + impl phy::PhyAccess for Eth { fn read_phy(&mut self, addr: u8, reg: u8) -> u16 { self.wait_phy_idle(); diff --git a/src/eth/rx.rs b/src/eth/rx.rs index ae4a86b..c850a40 100644 --- a/src/eth/rx.rs +++ b/src/eth/rx.rs @@ -39,6 +39,7 @@ pub const DESCS: usize = 8; pub struct DescList<'a> { list: [DescEntry; DESCS], buffers: [&'a mut [u8]; DESCS], + next: usize, } impl<'a> DescList<'a> { @@ -59,6 +60,25 @@ impl<'a> DescList<'a> { ); } - DescList { list, buffers } + DescList { + list, buffers, + next: 0, + } + } + + pub fn recv_next(&mut self) -> Option<&[u8]> { + if self.list[self.next].word0.read().used() { + let len = self.list[self.next].word1 + .read().frame_length_lsbs() + .into(); + let pkt = &self.buffers[self.next][0..len]; + self.next += 1; + if self.next >= self.list.len() { + self.next = 0; + } + Some(pkt) + } else { + None + } } } diff --git a/src/main.rs b/src/main.rs index 3f0ca6e..54ad484 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,7 +102,14 @@ fn main() { } let mut eth = eth.start_rx(rx_buffer_ptrs); - loop {} + loop { + match eth.recv_next() { + None => {} + Some(pkt) => { + writeln!(uart, "eth: received {} bytes", pkt.len()); + } + } + } panic!("End"); }