add network address config code

This commit is contained in:
Sebastien Bourdeauducq 2020-07-06 12:04:00 +08:00
parent e207c073ee
commit bbe6812792
4 changed files with 66 additions and 25 deletions

View File

@ -15,7 +15,7 @@ let
version = "0.1.0";
src = ./src;
cargoSha256 = "1ybqf370xqcvs94zs542ydm5jszwrfqwlnr3xwn5yn2zp4cnv0vd";
cargoSha256 = "0lwsbs1b6538b3zzvazy43xwy4rx1q0chxn3c9529nf3dk6xb22n";
nativeBuildInputs = [
pkgs.gnumake

10
src/Cargo.lock generated
View File

@ -200,7 +200,7 @@ dependencies = [
[[package]]
name = "libasync"
version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#9fcf9243f2dd2e9fcd61d17ddb1d6d495a68893f"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#ae4d3e2455d319308d5a1dd27c06a116c8e951f0"
dependencies = [
"embedded-hal",
"libcortex_a9",
@ -212,7 +212,7 @@ dependencies = [
[[package]]
name = "libboard_zynq"
version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#9fcf9243f2dd2e9fcd61d17ddb1d6d495a68893f"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#ae4d3e2455d319308d5a1dd27c06a116c8e951f0"
dependencies = [
"bit_field",
"embedded-hal",
@ -236,7 +236,7 @@ dependencies = [
[[package]]
name = "libcortex_a9"
version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#9fcf9243f2dd2e9fcd61d17ddb1d6d495a68893f"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#ae4d3e2455d319308d5a1dd27c06a116c8e951f0"
dependencies = [
"bit_field",
"libregister",
@ -245,7 +245,7 @@ dependencies = [
[[package]]
name = "libregister"
version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#9fcf9243f2dd2e9fcd61d17ddb1d6d495a68893f"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#ae4d3e2455d319308d5a1dd27c06a116c8e951f0"
dependencies = [
"bit_field",
"vcell",
@ -255,7 +255,7 @@ dependencies = [
[[package]]
name = "libsupport_zynq"
version = "0.0.0"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#9fcf9243f2dd2e9fcd61d17ddb1d6d495a68893f"
source = "git+https://git.m-labs.hk/M-Labs/zc706.git#ae4d3e2455d319308d5a1dd27c06a116c8e951f0"
dependencies = [
"compiler_builtins",
"libboard_zynq",

View File

@ -7,13 +7,14 @@
extern crate alloc;
use core::{cmp, str};
use log::{info, error};
use log::info;
use libboard_zynq::{timer::GlobalTimer, logger, devc, slcr};
use libsupport_zynq::ram;
mod sd_reader;
mod config;
mod net_settings;
mod proto_core_io;
mod proto_async;
mod comms;
@ -49,24 +50,6 @@ pub fn main_core0() {
ram::init_alloc_linker();
match config::Config::new() {
Ok(mut cfg) => {
match cfg.read_str("FOO") {
Ok(val) => info!("FOO = {}", val),
Err(error) => info!("failed to read config FOO: {}", error),
}
match cfg.read_str("BAR") {
Ok(val) => info!("BAR = {}", val),
Err(error) => info!("failed to read config BAR: {}", error),
}
match cfg.read_str("FOOBAR") {
Ok(val) => info!("read FOOBAR = {}", val),
Err(error) => info!("failed to read config FOOBAR: {}", error),
}
},
Err(error) => error!("config failed: {}", error)
}
if devc::DevC::new().is_done() {
info!("gateware already loaded");
// Do not load again: assume that the gateware already present is
@ -89,5 +72,7 @@ pub fn main_core0() {
pl::csr::rtio_core::reset_phy_write(1);
}
info!("network addresses: {}", net_settings::get_adresses());
comms::main(timer);
}

View File

@ -0,0 +1,56 @@
use core::fmt;
use libboard_zynq::smoltcp::wire::{EthernetAddress, IpAddress};
use crate::config;
pub struct NetAddresses {
pub hardware_addr: EthernetAddress,
pub ipv4_addr: IpAddress,
pub ipv6_ll_addr: IpAddress,
pub ipv6_addr: Option<IpAddress>
}
impl fmt::Display for NetAddresses {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "MAC={} IPv4={} IPv6-LL={} IPv6=",
self.hardware_addr, self.ipv4_addr, self.ipv6_ll_addr)?;
match self.ipv6_addr {
Some(addr) => write!(f, "{}", addr)?,
None => write!(f, "no configured address")?
}
Ok(())
}
}
pub fn get_adresses() -> NetAddresses {
let mut hardware_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x52]);
let mut ipv4_addr = IpAddress::v4(192, 168, 1, 52);
let mut ipv6_addr = None;
if let Ok(cfg) = config::Config::new() {
if let Ok(Ok(addr)) = cfg.read_str("mac").map(|s| s.parse()) {
hardware_addr = addr;
}
if let Ok(Ok(addr)) = cfg.read_str("ip").map(|s| s.parse()) {
ipv4_addr = addr;
}
if let Ok(Ok(addr)) = cfg.read_str("ip6").map(|s| s.parse()) {
ipv6_addr = Some(addr);
}
}
let ipv6_ll_addr = IpAddress::v6(
0xfe80, 0x0000, 0x0000, 0x0000,
(((hardware_addr.0[0] ^ 0x02) as u16) << 8) | (hardware_addr.0[1] as u16),
((hardware_addr.0[2] as u16) << 8) | 0x00ff,
0xfe00 | (hardware_addr.0[3] as u16),
((hardware_addr.0[4] as u16) << 8) | (hardware_addr.0[5] as u16));
NetAddresses {
hardware_addr: hardware_addr,
ipv4_addr: ipv4_addr,
ipv6_ll_addr: ipv6_ll_addr,
ipv6_addr: ipv6_addr
}
}