forked from M-Labs/zynq-rs
Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
143bb56ccb | |||
a07beb2a45 |
@ -22,3 +22,4 @@ r0 = "0.2"
|
|||||||
volatile-register = "0.2"
|
volatile-register = "0.2"
|
||||||
bit_field = "0.10"
|
bit_field = "0.10"
|
||||||
compiler_builtins = { git = "https://github.com/rust-lang-nursery/compiler-builtins", no-default-features = true, features = ["mem", "no-lang-items"]}
|
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"] }
|
||||||
|
@ -11,13 +11,13 @@ pub mod tx;
|
|||||||
pub const MTU: usize = 1536;
|
pub const MTU: usize = 1536;
|
||||||
pub const IO_PLL: u32 = 1_000;
|
pub const IO_PLL: u32 = 1_000;
|
||||||
|
|
||||||
pub struct Eth<RX, TX> {
|
pub struct Eth<'r, RX, TX> {
|
||||||
regs: &'static mut regs::RegisterBlock,
|
regs: &'r mut regs::RegisterBlock,
|
||||||
rx: RX,
|
rx: RX,
|
||||||
tx: TX,
|
tx: TX,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Eth<(), ()> {
|
impl<'r> Eth<'r, (), ()> {
|
||||||
pub fn default(macaddr: [u8; 6]) -> Self {
|
pub fn default(macaddr: [u8; 6]) -> Self {
|
||||||
slcr::RegisterBlock::unlocked(|slcr| {
|
slcr::RegisterBlock::unlocked(|slcr| {
|
||||||
// Manual example: 0x0000_1280
|
// Manual example: 0x0000_1280
|
||||||
@ -163,7 +163,7 @@ impl Eth<(), ()> {
|
|||||||
Self::from_regs(regs, macaddr)
|
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 {
|
let mut eth = Eth {
|
||||||
regs,
|
regs,
|
||||||
rx: (),
|
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) {
|
pub fn setup_gem0_clock(tx_clock: u32) {
|
||||||
let d0 = (IO_PLL / tx_clock).min(63);
|
let d0 = (IO_PLL / tx_clock).min(63);
|
||||||
let d1 = (IO_PLL / tx_clock / d0).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 {
|
let new_self = Eth {
|
||||||
regs: self.regs,
|
regs: self.regs,
|
||||||
rx: rx::DescList::new(rx_list, rx_buffers),
|
rx: rx::DescList::new(rx_list, rx_buffers),
|
||||||
@ -359,7 +359,7 @@ impl<RX, TX> Eth<RX, TX> {
|
|||||||
new_self
|
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 {
|
let new_self = Eth {
|
||||||
regs: self.regs,
|
regs: self.regs,
|
||||||
rx: self.rx,
|
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> {
|
pub fn recv_next<'s: 'p, 'p>(&'s mut self) -> Result<Option<rx::PktRef<'p>>, rx::Error> {
|
||||||
let status = self.regs.rx_status.read();
|
let status = self.regs.rx_status.read();
|
||||||
if status.hresp_not_ok() {
|
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>> {
|
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 {
|
fn read_phy(&mut self, addr: u8, reg: u8) -> u16 {
|
||||||
self.wait_phy_idle();
|
self.wait_phy_idle();
|
||||||
self.regs.phy_maint.write(
|
self.regs.phy_maint.write(
|
||||||
@ -486,3 +486,40 @@ impl<RX, TX> phy::PhyAccess for Eth<RX, TX> {
|
|||||||
self.wait_phy_idle();
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -120,3 +120,12 @@ impl<'a> Deref for PktRef<'a> {
|
|||||||
self.buffer
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -66,7 +66,7 @@ impl<'a> DescList<'a> {
|
|||||||
&self.list[0] as *const _ as u32
|
&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 list_len = self.list.len();
|
||||||
let entry = &mut self.list[self.next];
|
let entry = &mut self.list[self.next];
|
||||||
if entry.word1.read().used() {
|
if entry.word1.read().used() {
|
||||||
@ -118,3 +118,26 @@ impl<'a> DerefMut for PktRef<'a> {
|
|||||||
self.buffer
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
97
src/main.rs
97
src/main.rs
@ -6,10 +6,13 @@
|
|||||||
#![feature(compiler_builtins_lib)]
|
#![feature(compiler_builtins_lib)]
|
||||||
#![feature(never_type)]
|
#![feature(never_type)]
|
||||||
|
|
||||||
use core::mem::uninitialized;
|
use core::mem::{uninitialized, transmute};
|
||||||
|
|
||||||
use r0::zero_bss;
|
use r0::zero_bss;
|
||||||
use compiler_builtins as _;
|
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 regs;
|
||||||
mod cortex_a9;
|
mod cortex_a9;
|
||||||
@ -70,46 +73,80 @@ fn l1_cache_init() {
|
|||||||
dccisw();
|
dccisw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const HWADDR: [u8; 6] = [0, 0x23, 0xde, 0xea, 0xbe, 0xef];
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("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");
|
println!("Eth on");
|
||||||
eth.reset_phy();
|
eth.reset_phy();
|
||||||
|
|
||||||
let mut rx_descs: [eth::rx::DescEntry; 8] = unsafe { uninitialized() };
|
let mut rx_descs: [eth::rx::DescEntry; 2] = unsafe { uninitialized() };
|
||||||
let mut rx_buffers = [[0u8; 1536]; 8];
|
let mut rx_buffers = [[0u8; 1536]; 2];
|
||||||
let mut eth = eth.start_rx(&mut rx_descs, &mut rx_buffers);
|
let eth = eth.start_rx(&mut rx_descs, &mut rx_buffers);
|
||||||
let mut tx_descs: [eth::tx::DescEntry; 8] = unsafe { uninitialized() };
|
let mut tx_descs: [eth::tx::DescEntry; 2] = unsafe { uninitialized() };
|
||||||
let mut tx_buffers = [[0u8; 1536]; 8];
|
let mut tx_buffers = [[0u8; 1536]; 2];
|
||||||
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 {
|
loop {
|
||||||
match eth.recv_next() {
|
time += 1;
|
||||||
Ok(Some(pkt)) => {
|
let timestamp = Instant::from_millis(time.into());
|
||||||
print!("eth: rx {} bytes", pkt.len());
|
|
||||||
for b in pkt.iter() {
|
match iface.poll(&mut sockets, timestamp) {
|
||||||
print!(" {:02X}", b);
|
Ok(_) => {},
|
||||||
}
|
|
||||||
println!("");
|
|
||||||
}
|
|
||||||
Ok(None) => {}
|
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("eth rx error: {:?}", e);
|
println!("poll error: {}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
match eth.send(512) {
|
// match eth.recv_next() {
|
||||||
Some(mut pkt) => {
|
// Ok(Some(pkt)) => {
|
||||||
let mut x = 0;
|
// print!("eth: rx {} bytes", pkt.len());
|
||||||
for b in pkt.iter_mut() {
|
// for b in pkt.iter() {
|
||||||
*b = x;
|
// print!(" {:02X}", b);
|
||||||
x += 1;
|
// }
|
||||||
}
|
// println!("");
|
||||||
println!("eth tx {} bytes", pkt.len());
|
// }
|
||||||
}
|
// Ok(None) => {}
|
||||||
None => println!("eth tx shortage"),
|
// 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");
|
panic!("End");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user