1
0
forked from M-Labs/kirdy

Load & Config IP Settings from Flash at StartUp

This commit is contained in:
linuswck 2024-03-05 16:44:03 +08:00
parent 111d9a4226
commit 911d9a7bc9
3 changed files with 61 additions and 22 deletions

View File

@ -4,7 +4,7 @@ use crate::laser_diode::ld_ctrl::{*};
use crate::laser_diode::laser_diode::LdDrive;
use crate::thermostat::max1968::MAX1968;
use crate::thermostat::thermostat::Thermostat;
use crate::net::net::ServerHandle;
use crate::net::net::{IpSettings, ServerHandle};
use stm32_eth;
use fugit::ExtU32;
use log::{info, debug};
@ -15,7 +15,7 @@ use stm32f4xx_hal::{
time::MegaHertz,
watchdog::IndependentWatchdog,
};
use crate::DeviceSettings;
use uom::si::electric_current::milliampere;
use uom::si::{electric_current::ampere, f32::ElectricCurrent};
@ -83,6 +83,22 @@ pub fn bootup(
debug!("Setting up Internal Flash Driver");
let flash_store = flash_store::store(perif.FLASH);
let mut ip_settings: IpSettings = IpSettings::default();
let device_settings : DeviceSettings;
match flash_store.read_value("Device") {
Ok(Some(config)) => {
device_settings = config;
ip_settings = device_settings.ip_settings;
debug!("Found Device Settings");
}
Ok(None) => {
debug!("Flash does not have IP Settings");
}
Err(e) => {
debug!("Cannot Store Flash: {:?}", e);
}
}
debug!("Setting up ETH");
let mac_addr = hw_rev.get_mac_address();
let ethernet_parts_in = stm32_eth::PartsIn {
@ -91,7 +107,7 @@ pub fn bootup(
mmc: perif.ETHERNET_MMC,
ptp: perif.ETHERNET_PTP,
};
ServerHandle::new(eth_pins, eth_mgmt_pins, ethernet_parts_in, clocks, mac_addr);
ServerHandle::new(eth_pins, eth_mgmt_pins, ethernet_parts_in, clocks, mac_addr, ip_settings);
debug!("Setting Watchdog");
let mut wd = IndependentWatchdog::new(perif.IWDG);

View File

@ -10,6 +10,7 @@ mod thermostat;
mod net;
use device::{boot::bootup, log_setup, sys_timer};
use crate::net::net::IpSettings;
use serde::{Deserialize, Serialize};
use stm32f4xx_hal::pac::SCB;
@ -31,6 +32,7 @@ static mut ETH_DATA_BUFFER: [u8; 1024] = [0; 1024];
#[derive(Deserialize, Serialize, Clone, Copy, Debug)]
pub struct DeviceSettings{
report_readings: bool,
ip_settings: IpSettings,
}
const CONFIG_KEY: [&str; 3] = ["Device", "Laser_0", "Thermostat_0"];
@ -57,6 +59,7 @@ fn main() -> ! {
let mut device_settings = DeviceSettings {
report_readings: false,
ip_settings: IpSettings::default()
};
let mut state = State::default();

View File

@ -21,21 +21,24 @@ use stm32f4xx_hal::{
rcc::Clocks,
interrupt,
};
use serde::{Deserialize, Serialize};
const IPV4_ADDR: (u8, u8, u8, u8) = (192, 168, 1, 132);
const IP_INIT: IpCidr = IpCidr::Ipv4(Ipv4Cidr::new(
Ipv4Address::new(IPV4_ADDR.0, IPV4_ADDR.1, IPV4_ADDR.2, IPV4_ADDR.3),
24,
));
const ADDRESS: (IpAddress, u16) = (
IpAddress::Ipv4(Ipv4Address::new(
IPV4_ADDR.0,
IPV4_ADDR.1,
IPV4_ADDR.2,
IPV4_ADDR.3,
)),
1337,
);
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
pub struct IpSettings {
addr: [u8; 4],
port: u16,
prefix_len: u8,
}
impl Default for IpSettings {
fn default() -> Self {
IpSettings {
addr: [192, 168, 1, 132],
port: 1337,
prefix_len: 24,
}
}
}
/// Interrupt pending flag: set by the `ETH` interrupt handler, should
/// be cleared before polling the interface.
@ -44,6 +47,7 @@ static NET_PENDING: Mutex<RefCell<bool>> = Mutex::new(RefCell::new(false));
pub struct ServerHandle {
socket_handle: SocketHandle,
socket_set: SocketSet<'static>,
socket_addr: (IpAddress, u16),
iface: EthInterface,
dma: EthernetDMA<'static, 'static>,
}
@ -87,7 +91,8 @@ impl ServerHandle {
eth_mgmt_pins: EthernetMgmtPins,
ethernet_parts_in: PartsIn,
clocks: Clocks,
mac_addr: [u8; 6]
mac_addr: [u8; 6],
ip_settings: IpSettings,
) {
let rx_ring = unsafe { RX_RING.get_or_insert(Default::default()) };
let tx_ring = unsafe { TX_RING.get_or_insert(Default::default()) };
@ -109,6 +114,20 @@ impl ServerHandle {
eth_mgmt_pins.mdc
).unwrap();
let ip_init = IpCidr::Ipv4(Ipv4Cidr::new(
Ipv4Address::new(ip_settings.addr[0], ip_settings.addr[1], ip_settings.addr[2], ip_settings.addr[3]),
ip_settings.prefix_len,
));
let socket_addr: (IpAddress, u16) = (
IpAddress::Ipv4(Ipv4Address::new(
ip_settings.addr[0],
ip_settings.addr[1],
ip_settings.addr[2],
ip_settings.addr[3],
)),
ip_settings.port,
);
let mut routes = smoltcp::iface::Routes::new();
routes
.add_default_ipv4_route(Ipv4Address::new(192, 168, 1, 1))
@ -123,20 +142,21 @@ impl ServerHandle {
let mut iface = Interface::new(config, &mut &mut dma, smoltcp::time::Instant::ZERO);
iface.set_hardware_addr(EthernetAddress(mac_addr).into());
debug!("MAC ADDRESS: {:02X?}", EthernetAddress(mac_addr));
debug!("IP: {:?}", IP_INIT);
debug!("IP Settings: {:?}", ip_settings);
iface.update_ip_addrs(|addr| {
addr.push(IP_INIT).unwrap();
addr.push(ip_init).unwrap();
});
let mut sockets = SocketSet::new(&mut socket_storage[..]);
let tcp_handle = sockets.add(socket);
let socket = sockets.get_mut::<Socket>(tcp_handle);
socket.listen(ADDRESS).ok();
socket.listen(socket_addr).ok();
iface.poll(Instant::from_millis(i64::from(sys_timer::now())), &mut &mut dma, &mut sockets);
let server = ServerHandle {
socket_handle: tcp_handle,
socket_set: sockets,
socket_addr: socket_addr,
iface: iface,
dma: dma,
};
@ -193,7 +213,7 @@ impl ServerHandle {
let socket = self.socket_set.get_mut::<Socket>(self.socket_handle);
if !socket.is_listening() && !socket.is_open() || socket.state() == State::CloseWait {
socket.abort();
socket.listen(ADDRESS).ok();
socket.listen(self.socket_addr).ok();
info!("Disconnected... Reopening listening socket.");
return false;
} else if socket.state() == State::Closed || socket.state() == State::Closing {