diff --git a/CHANGELOG.md b/CHANGELOG.md index 0106e8460..d45575bf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added +* Optional specification of a static IP address ([#509](https://github.com/quartiq/stabilizer/pull/509)) * Telemetry ([#341](https://github.com/quartiq/stabilizer/pull/341)) * Logging via RTT (real time tracing) over SWD/JTAG instead of semihosting for fast and low-overhead debugging ([#393](https://github.com/quartiq/stabilizer/pull/393) [#391](https://github.com/quartiq/stabilizer/pull/391) [#358](https://github.com/quartiq/stabilizer/pull/358)) diff --git a/book/src/setup.md b/book/src/setup.md index b3e7f771d..e3364c219 100644 --- a/book/src/setup.md +++ b/book/src/setup.md @@ -27,6 +27,8 @@ Stabilizer supports 10Base-T or 100Base-T with Auto MDI-X. Stabilizer uses DHCP to obtain its network configuration information. Ensure there is a properly configured DHCP server running on the network segment that Stabilizer is connected to. +Alternatively, a static IP can be enforced in the firmware build command by specifying +the environmental variable `STATIC_IP` analogous to how a specific broker IP is set. > **Note:** If Stabilizer is connected directly to an Ubuntu system (for example using a USB-Ethernet dongle) you can set the IPv4 settings of this Ethernet connection in the Ubuntu network settings to diff --git a/src/hardware/setup.rs b/src/hardware/setup.rs index a6bd3aa1c..05b22dcec 100644 --- a/src/hardware/setup.rs +++ b/src/hardware/setup.rs @@ -688,17 +688,18 @@ pub fn setup( unsafe { ethernet::enable_interrupt() }; + // Configure IP address according to DHCP socket availability + let ip_addrs: smoltcp::wire::IpAddress = option_env!("STATIC_IP") + .unwrap_or("0.0.0.0") + .parse() + .unwrap(); + // Note(unwrap): The hardware configuration function is only allowed to be called once. // Unwrapping is intended to panic if called again to prevent re-use of global memory. let store = cortex_m::singleton!(: NetStorage = NetStorage::default()).unwrap(); - store.ip_addrs[0] = smoltcp::wire::IpCidr::new( - smoltcp::wire::IpAddress::Ipv4( - smoltcp::wire::Ipv4Address::UNSPECIFIED, - ), - 0, - ); + store.ip_addrs[0] = smoltcp::wire::IpCidr::new(ip_addrs, 24); let mut routes = smoltcp::iface::Routes::new(&mut store.routes_cache[..]); @@ -719,7 +720,9 @@ pub fn setup( .routes(routes) .finalize(); - interface.add_socket(smoltcp::socket::Dhcpv4Socket::new()); + if ip_addrs.is_unspecified() { + interface.add_socket(smoltcp::socket::Dhcpv4Socket::new()); + } for storage in store.tcp_socket_storage[..].iter_mut() { let tcp_socket = {