From 31e5f9a81081e17ddddf0baa6f8c546aede6011b Mon Sep 17 00:00:00 2001 From: whitequark Date: Thu, 26 Jan 2017 17:35:42 +0000 Subject: [PATCH] firmware: read MAC/IP address configuration from flash. --- artiq/firmware/Cargo.lock | 8 ++++---- artiq/firmware/runtime/Cargo.toml | 2 +- artiq/firmware/runtime/config.rs | 5 +++++ artiq/firmware/runtime/lib.rs | 29 ++++++++++++++++++++++++++--- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/artiq/firmware/Cargo.lock b/artiq/firmware/Cargo.lock index 09a63b26e..982dffc91 100644 --- a/artiq/firmware/Cargo.lock +++ b/artiq/firmware/Cargo.lock @@ -97,7 +97,7 @@ dependencies = [ "fringe 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "logger_artiq 0.0.0", - "smoltcp 0.2.0 (git+https://github.com/m-labs/smoltcp?rev=a20f89f)", + "smoltcp 0.2.1 (git+https://github.com/m-labs/smoltcp?rev=ebee5b7)", "std_artiq 0.0.0", ] @@ -115,8 +115,8 @@ dependencies = [ [[package]] name = "smoltcp" -version = "0.2.0" -source = "git+https://github.com/m-labs/smoltcp?rev=a20f89f#a20f89f5eed8ed7519c7be4c66b2f51e3fa5f6fa" +version = "0.2.1" +source = "git+https://github.com/m-labs/smoltcp?rev=ebee5b7#ebee5b7b584399ddae91320818f116c6173fc6b4" dependencies = [ "byteorder 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -150,7 +150,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ab83497bf8bf4ed2a74259c1c802351fcd67a65baa86394b6ba73c36f4838054" "checksum log_buffer 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ec57723b84bbe7bdf76aa93169c9b59e67473317c6de3a83cb2a0f8ccb2aa493" "checksum managed 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5981b4c6de5ce272aaf2caaa56adb8f6fd24a73206b38302db572ab9374aab10" -"checksum smoltcp 0.2.0 (git+https://github.com/m-labs/smoltcp?rev=a20f89f)" = "" +"checksum smoltcp 0.2.1 (git+https://github.com/m-labs/smoltcp?rev=ebee5b7)" = "" "checksum walkdir 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "dd7c16466ecc507c7cb5988db03e6eab4aaeab89a5c37a29251fcfd3ac9b7afe" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/artiq/firmware/runtime/Cargo.toml b/artiq/firmware/runtime/Cargo.toml index 30f912249..b403299fa 100644 --- a/artiq/firmware/runtime/Cargo.toml +++ b/artiq/firmware/runtime/Cargo.toml @@ -23,6 +23,6 @@ byteorder = { version = "1.0", default-features = false } [dependencies.smoltcp] git = "https://github.com/m-labs/smoltcp" -rev = "a20f89f" +rev = "ebee5b7" default-features = false features = ["use_alloc", "use_collections", "use_log"]#, "verbose"] diff --git a/artiq/firmware/runtime/config.rs b/artiq/firmware/runtime/config.rs index df3d1868e..7e67ac6cc 100644 --- a/artiq/firmware/runtime/config.rs +++ b/artiq/firmware/runtime/config.rs @@ -1,5 +1,6 @@ use std::cmp; use std::vec::Vec; +use std::string::String; use libc::{c_void, c_char, c_int, c_uint}; extern { @@ -44,6 +45,10 @@ pub fn read_to_end(key: &str) -> Vec { value } +pub fn read_string(key: &str) -> String { + String::from_utf8(read_to_end(key)).unwrap() +} + pub fn write(key: &str, buf: &[u8]) -> Result<(), ()> { let key_c = c_str!(key); let result = unsafe { diff --git a/artiq/firmware/runtime/lib.rs b/artiq/firmware/runtime/lib.rs index 4354460e4..89e86c7e2 100644 --- a/artiq/firmware/runtime/lib.rs +++ b/artiq/firmware/runtime/lib.rs @@ -15,6 +15,7 @@ extern crate smoltcp; extern crate board; use std::boxed::Box; +use smoltcp::wire::{EthernetAddress, IpAddress}; extern { fn readchar() -> libc::c_char; @@ -81,6 +82,30 @@ fn startup() { #[cfg(has_converter_spi)] board::ad9154::init().expect("cannot initialize ad9154"); + let hardware_addr; + match EthernetAddress::parse(&config::read_string("mac")) { + Err(()) => { + hardware_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]); + warn!("using default MAC address {}; consider changing it", hardware_addr); + } + Ok(addr) => { + hardware_addr = addr; + info!("using MAC address {}", hardware_addr); + } + } + + let protocol_addr; + match IpAddress::parse(&config::read_string("ip")) { + Err(()) | Ok(IpAddress::Unspecified) => { + protocol_addr = IpAddress::v4(192, 168, 1, 50); + info!("using default IP address {}", protocol_addr); + } + Ok(addr) => { + protocol_addr = addr; + info!("using IP address {}", protocol_addr); + } + } + fn _net_trace_writer(printer: smoltcp::wire::PrettyPrinter) where U: smoltcp::wire::pretty_print::PrettyPrint { print!("\x1b[37m{}\x1b[0m", printer) @@ -90,11 +115,9 @@ fn startup() { // let net_device = smoltcp::phy::Tracer::<_, smoltcp::wire::EthernetFrame<&[u8]>> // ::new(net_device, _net_trace_writer); let arp_cache = smoltcp::iface::SliceArpCache::new([Default::default(); 8]); - let hardware_addr = smoltcp::wire::EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]); - let protocol_addrs = [smoltcp::wire::IpAddress::v4(192, 168, 1, 50)]; let mut interface = smoltcp::iface::EthernetInterface::new( Box::new(net_device), Box::new(arp_cache) as Box, - hardware_addr, protocol_addrs); + hardware_addr, [protocol_addr]); let mut scheduler = sched::Scheduler::new(); let io = scheduler.io();