From 2d758af9e4ee81fb76ef922d8e91a90f2fcd2e9c Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 21 Mar 2019 17:41:33 +0100 Subject: [PATCH] add feature "generate-hwaddr" to generate indivudal MAC addrs from device electronic signature --- Cargo.toml | 3 +++ src/main.rs | 19 +++++++++++++++++-- src/net.rs | 10 ++++------ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f67ed06..555b500 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,9 +32,12 @@ stm32f4xx-hal = { git = "https://github.com/stm32-rs/stm32f4xx-hal", features = #stm32-eth = { version = "0.1.0", features = ["smoltcp-phy", "nucleo-f429zi"] } stm32-eth = { git = "https://github.com/stm32-rs/stm32-eth", features = ["smoltcp-phy", "nucleo-f429zi"] } smoltcp = { version = "0.5.0", default-features = false, features = ["proto-ipv4", "socket-tcp", "log"] } +hash2hwaddr = { version = "0.0", optional = true } [features] semihosting = ["panic-semihosting", "cortex-m-log/semihosting"] +generate-hwaddr = ["hash2hwaddr"] +default = ["generate-hwaddr"] [profile.release] codegen-units = 1 diff --git a/src/main.rs b/src/main.rs index 383457a..45f56d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,10 @@ use stm32f4xx_hal::{ time::U32Ext, stm32::{CorePeripherals, Peripherals}, }; -use smoltcp::time::Instant; +use smoltcp::{ + time::Instant, + wire::EthernetAddress, +}; mod adc_input; use adc_input::AdcInput; @@ -39,6 +42,9 @@ use led::Led; /// This should be a multiple of the `TIMER_RATE`. const OUTPUT_INTERVAL: u32 = 1000; +#[cfg(not(feature = "generate-hwaddr"))] +const NET_HWADDR: [u8; 6] = [0x02, 0x00, 0xDE, 0xAD, 0xBE, 0xEF]; + #[cfg(not(feature = "semihosting"))] fn init_log() {} @@ -104,8 +110,17 @@ fn main() -> ! { info!("Timer setup"); timer::setup(cp.SYST, clocks); + #[cfg(not(feature = "generate-hwaddr"))] + let hwaddr = EthernetAddress(NET_HWADDR); + #[cfg(feature = "generate-hwaddr")] + let hwaddr = { + let uid = stm32f4xx_hal::signature::Uid::get(); + EthernetAddress(hash2hwaddr::generate_hwaddr(uid)) + }; + info!("Net hwaddr: {}", hwaddr); + info!("Net startup"); - net::run(&mut cp.NVIC, dp.ETHERNET_MAC, dp.ETHERNET_DMA, |iface| { + net::run(&mut cp.NVIC, dp.ETHERNET_MAC, dp.ETHERNET_DMA, hwaddr, |iface| { Server::run(iface, |server| { let mut last_output = 0_u32; loop { diff --git a/src/net.rs b/src/net.rs index 05933e2..be51996 100644 --- a/src/net.rs +++ b/src/net.rs @@ -18,16 +18,15 @@ static mut RX_RING: Option<[RingEntry; 8]> = None; /// ethernet peripheral cannot access) static mut TX_RING: Option<[RingEntry; 2]> = None; -// TODO: generate one from device id -const SRC_MAC: [u8; 6] = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF]; - /// Interrupt pending flag: set by the `ETH` interrupt handler, should /// be cleared before polling the interface. static NET_PENDING: Mutex> = Mutex::new(RefCell::new(false)); /// Run callback `f` with ethernet driver and TCP/IP stack -pub fn run(nvic: &mut NVIC, ethernet_mac: ETHERNET_MAC, ethernet_dma: ETHERNET_DMA, f: F) -where +pub fn run( + nvic: &mut NVIC, ethernet_mac: ETHERNET_MAC, ethernet_dma: ETHERNET_DMA, + ethernet_addr: EthernetAddress, f: F +) where F: FnOnce(EthernetInterface<&mut stm32_eth::Eth<'static, 'static>>), { let rx_ring = unsafe { @@ -48,7 +47,6 @@ where let mut ip_addrs = [IpCidr::new(local_addr, 24)]; let mut neighbor_storage = [None; 16]; let neighbor_cache = NeighborCache::new(&mut neighbor_storage[..]); - let ethernet_addr = EthernetAddress(SRC_MAC); let iface = EthernetInterfaceBuilder::new(&mut eth_dev) .ethernet_addr(ethernet_addr) .ip_addrs(&mut ip_addrs[..])