From d11e58186260f33cc2fed06bfb67ae4ca6695b0e Mon Sep 17 00:00:00 2001 From: Astro Date: Fri, 5 Jul 2019 00:44:53 +0200 Subject: [PATCH] main: setup smoltcp still panics, leading to a DataAbort --- src/eth/mod.rs | 24 +++++++------- src/eth/tx.rs | 4 +-- src/main.rs | 88 ++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 77 insertions(+), 39 deletions(-) diff --git a/src/eth/mod.rs b/src/eth/mod.rs index b074201..3e248a8 100644 --- a/src/eth/mod.rs +++ b/src/eth/mod.rs @@ -15,13 +15,13 @@ const MAX_MDC: u32 = 2_500_000; /// Clock for GbE const TX_1000: u32 = 125_000_000; -pub struct Eth { - regs: &'static mut regs::RegisterBlock, +pub struct Eth<'r, RX, TX> { + regs: &'r mut regs::RegisterBlock, rx: RX, tx: TX, } -impl Eth<(), ()> { +impl<'r> Eth<'r, (), ()> { pub fn default(macaddr: [u8; 6]) -> Self { slcr::RegisterBlock::unlocked(|slcr| { // Manual example: 0x0000_1280 @@ -167,7 +167,7 @@ impl Eth<(), ()> { Self::from_regs(regs, macaddr) } - fn from_regs(regs: &'static mut regs::RegisterBlock, macaddr: [u8; 6]) -> Self { + fn from_regs(regs: &'r mut regs::RegisterBlock, macaddr: [u8; 6]) -> Self { let mut eth = Eth { regs, rx: (), @@ -178,7 +178,7 @@ impl Eth<(), ()> { } } -impl Eth { +impl<'r, RX, TX> Eth<'r, RX, TX> { pub fn setup_gem0_clock(tx_clock: u32) { let io_pll = CpuClocks::get().io; let d0 = (io_pll / tx_clock).min(63); @@ -349,7 +349,7 @@ impl Eth { ); } - pub fn start_rx<'rx>(self, rx_list: &'rx mut [rx::DescEntry], rx_buffers: &'rx mut [[u8; MTU]]) -> Eth, TX> { + pub fn start_rx<'rx>(self, rx_list: &'rx mut [rx::DescEntry], rx_buffers: &'rx mut [[u8; MTU]]) -> Eth<'r, rx::DescList<'rx>, TX> { let new_self = Eth { regs: self.regs, rx: rx::DescList::new(rx_list, rx_buffers), @@ -367,7 +367,7 @@ impl Eth { new_self } - pub fn start_tx<'tx>(self, tx_list: &'tx mut [tx::DescEntry], tx_buffers: &'tx mut [[u8; MTU]]) -> Eth> { + pub fn start_tx<'tx>(self, tx_list: &'tx mut [tx::DescEntry], tx_buffers: &'tx mut [[u8; MTU]]) -> Eth<'r, RX, tx::DescList<'tx>> { let new_self = Eth { regs: self.regs, rx: self.rx, @@ -412,7 +412,7 @@ impl Eth { } } -impl<'rx, TX> Eth, TX> { +impl<'r, 'rx, TX> Eth<'r, rx::DescList<'rx>, TX> { pub fn recv_next<'s: 'p, 'p>(&'s mut self) -> Result>, rx::Error> { let status = self.regs.rx_status.read(); if status.hresp_not_ok() { @@ -459,13 +459,13 @@ impl<'rx, TX> Eth, TX> { } } -impl<'tx, RX> Eth> { +impl<'r, 'tx, RX> Eth<'r, RX, tx::DescList<'tx>> { pub fn send<'s: 'p, 'p>(&'s mut self, length: usize) -> Option> { - self.tx.send(&mut self.regs, length) + self.tx.send(self.regs, length) } } -impl phy::PhyAccess for Eth { +impl<'r, RX, TX> phy::PhyAccess for Eth<'r, RX, TX> { fn read_phy(&mut self, addr: u8, reg: u8) -> u16 { self.wait_phy_idle(); self.regs.phy_maint.write( @@ -495,7 +495,7 @@ impl phy::PhyAccess for Eth { } } -impl<'a, 'rx: 'a, 'tx: 'a> smoltcp::phy::Device<'a> for Eth, tx::DescList<'tx>> { +impl<'r, 'rx, 'tx: 'a, 'a> smoltcp::phy::Device<'a> for &mut Eth<'r, rx::DescList<'rx>, tx::DescList<'tx>> { type RxToken = rx::PktRef<'a>; type TxToken = tx::Token<'a, 'tx>; diff --git a/src/eth/tx.rs b/src/eth/tx.rs index 8bc31fb..4007363 100644 --- a/src/eth/tx.rs +++ b/src/eth/tx.rs @@ -68,7 +68,7 @@ impl<'a> DescList<'a> { &self.list[0] as *const _ as u32 } - pub fn send<'s: 'p, 'p>(&'s mut self, regs: &'p mut regs::RegisterBlock, length: usize) -> Option> { + pub fn send<'s: 'p, 'p>(&'s mut self, regs: &'s mut regs::RegisterBlock, length: usize) -> Option> { let list_len = self.list.len(); let entry = &mut self.list[self.next]; if entry.word1.read().used() { @@ -122,7 +122,7 @@ impl<'a> DerefMut for PktRef<'a> { } /// TxToken for smoltcp support -pub struct Token<'a, 'tx> { +pub struct Token<'a, 'tx: 'a> { pub regs: &'a mut regs::RegisterBlock, pub desc_list: &'a mut DescList<'tx>, } diff --git a/src/main.rs b/src/main.rs index b5455b0..2392ddc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,10 +8,13 @@ // TODO: disallow unused/dead_code when code moves into a lib crate #![allow(dead_code)] -use core::mem::uninitialized; - +use core::mem::{uninitialized, transmute}; use r0::zero_bss; use compiler_builtins as _; +use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr}; +use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder, EthernetInterface}; +use smoltcp::time::Instant; +use smoltcp::socket::SocketSet; mod regs; mod cortex_a9; @@ -73,6 +76,8 @@ fn l1_cache_init() { dccisw(); } +const HWADDR: [u8; 6] = [0, 0x23, 0xde, 0xea, 0xbe, 0xef]; + fn main() { println!("Main."); let clocks = clocks::CpuClocks::get(); @@ -83,7 +88,7 @@ fn main() { clocks.cpu_2x() / 1_000_000, clocks.cpu_1x() / 1_000_000); - let mut eth = eth::Eth::default([0x0, 0x17, 0xde, 0xea, 0xbe, 0xef]); + let mut eth = eth::Eth::default(HWADDR.clone()); println!("Eth on"); eth.reset_phy(); @@ -92,34 +97,67 @@ fn main() { let eth = eth.start_rx(&mut rx_descs, &mut rx_buffers); let mut tx_descs: [eth::tx::DescEntry; 8] = unsafe { uninitialized() }; let mut tx_buffers = [[0u8; 1536]; 8]; - let mut eth = eth.start_tx(&mut tx_descs, &mut tx_buffers); + //let mut eth = eth.start_tx(&mut tx_descs, &mut tx_buffers); + let mut eth = eth.start_tx( + // HACK + unsafe { transmute(tx_descs.as_mut()) }, + unsafe { transmute(tx_buffers.as_mut()) }, + ); + let ethernet_addr = EthernetAddress(HWADDR); + // IP stack + let local_addr = IpAddress::v4(10, 0, 0, 1); + let mut ip_addrs = [IpCidr::new(local_addr, 24)]; + let mut neighbor_storage = [None; 16]; + let neighbor_cache = NeighborCache::new(&mut neighbor_storage[..]); + let mut iface = EthernetInterfaceBuilder::new(&mut eth) + .ethernet_addr(ethernet_addr) + .ip_addrs(&mut ip_addrs[..]) + .neighbor_cache(neighbor_cache) + .finalize(); + let mut sockets_storage = [ + None, None, None, None, + None, None, None, None + ]; + let mut sockets = SocketSet::new(&mut sockets_storage[..]); + + let mut time = 0u32; loop { - match eth.recv_next() { - Ok(Some(pkt)) => { - print!("eth: rx {} bytes", pkt.len()); - for b in pkt.iter() { - print!(" {:02X}", b); - } - println!(""); - } - Ok(None) => {} + time += 1; + let timestamp = Instant::from_millis(time.into()); + + match iface.poll(&mut sockets, timestamp) { + Ok(_) => {}, Err(e) => { - println!("eth rx error: {:?}", e); + println!("poll error: {}", e); } } - match eth.send(512) { - Some(mut pkt) => { - let mut x = 0; - for b in pkt.iter_mut() { - *b = x; - x += 1; - } - println!("eth tx {} bytes", pkt.len()); - } - None => println!("eth tx shortage"), - } + // match eth.recv_next() { + // Ok(Some(pkt)) => { + // print!("eth: rx {} bytes", pkt.len()); + // for b in pkt.iter() { + // print!(" {:02X}", b); + // } + // println!(""); + // } + // Ok(None) => {} + // Err(e) => { + // println!("eth rx error: {:?}", e); + // } + // } + + // match eth.send(512) { + // Some(mut pkt) => { + // let mut x = 0; + // for b in pkt.iter_mut() { + // *b = x; + // x += 1; + // } + // println!("eth tx {} bytes", pkt.len()); + // } + // None => println!("eth tx shortage"), + // } } }