add feature "generate-hwaddr" to generate indivudal MAC addrs from device electronic signature

This commit is contained in:
Astro 2019-03-21 17:41:33 +01:00
parent 22e7a77d80
commit 2d758af9e4
3 changed files with 24 additions and 8 deletions

View File

@ -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 = { version = "0.1.0", features = ["smoltcp-phy", "nucleo-f429zi"] }
stm32-eth = { git = "https://github.com/stm32-rs/stm32-eth", 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"] } smoltcp = { version = "0.5.0", default-features = false, features = ["proto-ipv4", "socket-tcp", "log"] }
hash2hwaddr = { version = "0.0", optional = true }
[features] [features]
semihosting = ["panic-semihosting", "cortex-m-log/semihosting"] semihosting = ["panic-semihosting", "cortex-m-log/semihosting"]
generate-hwaddr = ["hash2hwaddr"]
default = ["generate-hwaddr"]
[profile.release] [profile.release]
codegen-units = 1 codegen-units = 1

View File

@ -22,7 +22,10 @@ use stm32f4xx_hal::{
time::U32Ext, time::U32Ext,
stm32::{CorePeripherals, Peripherals}, stm32::{CorePeripherals, Peripherals},
}; };
use smoltcp::time::Instant; use smoltcp::{
time::Instant,
wire::EthernetAddress,
};
mod adc_input; mod adc_input;
use adc_input::AdcInput; use adc_input::AdcInput;
@ -39,6 +42,9 @@ use led::Led;
/// This should be a multiple of the `TIMER_RATE`. /// This should be a multiple of the `TIMER_RATE`.
const OUTPUT_INTERVAL: u32 = 1000; 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"))] #[cfg(not(feature = "semihosting"))]
fn init_log() {} fn init_log() {}
@ -104,8 +110,17 @@ fn main() -> ! {
info!("Timer setup"); info!("Timer setup");
timer::setup(cp.SYST, clocks); 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"); 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| { Server::run(iface, |server| {
let mut last_output = 0_u32; let mut last_output = 0_u32;
loop { loop {

View File

@ -18,16 +18,15 @@ static mut RX_RING: Option<[RingEntry<RxDescriptor>; 8]> = None;
/// ethernet peripheral cannot access) /// ethernet peripheral cannot access)
static mut TX_RING: Option<[RingEntry<TxDescriptor>; 2]> = None; static mut TX_RING: Option<[RingEntry<TxDescriptor>; 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 /// Interrupt pending flag: set by the `ETH` interrupt handler, should
/// be cleared before polling the interface. /// be cleared before polling the interface.
static NET_PENDING: Mutex<RefCell<bool>> = Mutex::new(RefCell::new(false)); static NET_PENDING: Mutex<RefCell<bool>> = Mutex::new(RefCell::new(false));
/// Run callback `f` with ethernet driver and TCP/IP stack /// Run callback `f` with ethernet driver and TCP/IP stack
pub fn run<F>(nvic: &mut NVIC, ethernet_mac: ETHERNET_MAC, ethernet_dma: ETHERNET_DMA, f: F) pub fn run<F>(
where 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>>), F: FnOnce(EthernetInterface<&mut stm32_eth::Eth<'static, 'static>>),
{ {
let rx_ring = unsafe { let rx_ring = unsafe {
@ -48,7 +47,6 @@ where
let mut ip_addrs = [IpCidr::new(local_addr, 24)]; let mut ip_addrs = [IpCidr::new(local_addr, 24)];
let mut neighbor_storage = [None; 16]; let mut neighbor_storage = [None; 16];
let neighbor_cache = NeighborCache::new(&mut neighbor_storage[..]); let neighbor_cache = NeighborCache::new(&mut neighbor_storage[..]);
let ethernet_addr = EthernetAddress(SRC_MAC);
let iface = EthernetInterfaceBuilder::new(&mut eth_dev) let iface = EthernetInterfaceBuilder::new(&mut eth_dev)
.ethernet_addr(ethernet_addr) .ethernet_addr(ethernet_addr)
.ip_addrs(&mut ip_addrs[..]) .ip_addrs(&mut ip_addrs[..])