Compare commits

...

2 Commits

Author SHA1 Message Date
143bb56ccb main: setup smoltcp
still panics, leading to a DataAbort
2019-08-07 20:38:30 +02:00
a07beb2a45 eth: add smoltcp support 2019-08-07 20:38:29 +02:00
5 changed files with 149 additions and 42 deletions

View File

@ -22,3 +22,4 @@ r0 = "0.2"
volatile-register = "0.2"
bit_field = "0.10"
compiler_builtins = { git = "https://github.com/rust-lang-nursery/compiler-builtins", no-default-features = true, features = ["mem", "no-lang-items"]}
smoltcp = { version = "0.5", default-features = false, features = ["proto-ipv4", "socket-tcp"] }

View File

@ -11,13 +11,13 @@ pub mod tx;
pub const MTU: usize = 1536;
pub const IO_PLL: u32 = 1_000;
pub struct Eth<RX, TX> {
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
@ -163,7 +163,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: (),
@ -174,7 +174,7 @@ impl Eth<(), ()> {
}
}
impl<RX, TX> Eth<RX, TX> {
impl<'r, RX, TX> Eth<'r, RX, TX> {
pub fn setup_gem0_clock(tx_clock: u32) {
let d0 = (IO_PLL / tx_clock).min(63);
let d1 = (IO_PLL / tx_clock / d0).min(63);
@ -341,7 +341,7 @@ impl<RX, TX> Eth<RX, TX> {
);
}
pub fn start_rx<'rx>(self, rx_list: &'rx mut [rx::DescEntry], rx_buffers: &'rx mut [[u8; MTU]]) -> Eth<rx::DescList<'rx>, 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),
@ -359,7 +359,7 @@ impl<RX, TX> Eth<RX, TX> {
new_self
}
pub fn start_tx<'tx>(self, tx_list: &'tx mut [tx::DescEntry], tx_buffers: &'tx mut [[u8; MTU]]) -> Eth<RX, tx::DescList<'tx>> {
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,
@ -404,7 +404,7 @@ impl<RX, TX> Eth<RX, TX> {
}
}
impl<'rx, TX> Eth<rx::DescList<'rx>, TX> {
impl<'r, 'rx, TX> Eth<'r, rx::DescList<'rx>, TX> {
pub fn recv_next<'s: 'p, 'p>(&'s mut self) -> Result<Option<rx::PktRef<'p>>, rx::Error> {
let status = self.regs.rx_status.read();
if status.hresp_not_ok() {
@ -451,13 +451,13 @@ impl<'rx, TX> Eth<rx::DescList<'rx>, TX> {
}
}
impl<'tx, RX> Eth<RX, tx::DescList<'tx>> {
impl<'r, 'tx, RX> Eth<'r, RX, tx::DescList<'tx>> {
pub fn send<'s: 'p, 'p>(&'s mut self, length: usize) -> Option<tx::PktRef<'p>> {
self.tx.send(&mut self.regs, length)
self.tx.send(self.regs, length)
}
}
impl<RX, TX> phy::PhyAccess for Eth<RX, TX> {
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(
@ -486,3 +486,40 @@ impl<RX, TX> phy::PhyAccess for Eth<RX, TX> {
self.wait_phy_idle();
}
}
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>;
fn capabilities(&self) -> smoltcp::phy::DeviceCapabilities {
let mut caps = smoltcp::phy::DeviceCapabilities::default();
caps.max_transmission_unit = MTU;
caps
}
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
match self.rx.recv_next() {
Ok(Some(mut pktref)) => {
let tx_token = tx::Token {
regs: self.regs,
desc_list: &mut self.tx,
};
Some((pktref, tx_token))
}
Ok(None) =>
None,
Err(e) => {
println!("eth recv error: {:?}", e);
None
}
}
}
fn transmit(&'a mut self) -> Option<Self::TxToken> {
Some(tx::Token {
regs: self.regs,
desc_list: &mut self.tx,
})
}
}

View File

@ -120,3 +120,12 @@ impl<'a> Deref for PktRef<'a> {
self.buffer
}
}
impl<'a> smoltcp::phy::RxToken for PktRef<'a> {
fn consume<R, F>(mut self, _timestamp: smoltcp::time::Instant, f: F) -> smoltcp::Result<R>
where
F: FnOnce(&[u8]) -> smoltcp::Result<R>
{
f(self.buffer)
}
}

View File

@ -66,7 +66,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<PktRef<'p>> {
pub fn send<'s: 'p, 'p>(&'s mut self, regs: &'s mut regs::RegisterBlock, length: usize) -> Option<PktRef<'p>> {
let list_len = self.list.len();
let entry = &mut self.list[self.next];
if entry.word1.read().used() {
@ -118,3 +118,26 @@ impl<'a> DerefMut for PktRef<'a> {
self.buffer
}
}
/// TxToken for smoltcp support
pub struct Token<'a, 'tx: 'a> {
pub regs: &'a mut regs::RegisterBlock,
pub desc_list: &'a mut DescList<'tx>,
}
impl<'a, 'tx: 'a> smoltcp::phy::TxToken for Token<'a, 'tx> {
fn consume<R, F>(self, _timestamp: smoltcp::time::Instant, len: usize, f: F) -> smoltcp::Result<R>
where F: FnOnce(&mut [u8]) -> smoltcp::Result<R>
{
match self.desc_list.send(self.regs, len) {
None =>
Err(smoltcp::Error::Exhausted),
Some(mut pktref) => {
let result = f(pktref.deref_mut());
// TODO: on result.is_err() don;t send
drop(pktref);
result
}
}
}
}

View File

@ -6,10 +6,13 @@
#![feature(compiler_builtins_lib)]
#![feature(never_type)]
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;
@ -70,46 +73,80 @@ fn l1_cache_init() {
dccisw();
}
const HWADDR: [u8; 6] = [0, 0x23, 0xde, 0xea, 0xbe, 0xef];
fn main() {
println!("Main.");
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();
let mut rx_descs: [eth::rx::DescEntry; 8] = unsafe { uninitialized() };
let mut rx_buffers = [[0u8; 1536]; 8];
let mut 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 rx_descs: [eth::rx::DescEntry; 2] = unsafe { uninitialized() };
let mut rx_buffers = [[0u8; 1536]; 2];
let eth = eth.start_rx(&mut rx_descs, &mut rx_buffers);
let mut tx_descs: [eth::tx::DescEntry; 2] = unsafe { uninitialized() };
let mut tx_buffers = [[0u8; 1536]; 2];
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"),
// }
}
panic!("End");
}