From eff8adc184a539cd49e833ffe0c9bd3ca35afebb Mon Sep 17 00:00:00 2001 From: linuswck Date: Wed, 6 Mar 2024 12:15:08 +0800 Subject: [PATCH] Update Eth Link Speed with the detected Speed - adds support to Half/FullDuplexBase10T, HalfDuplexBase100Tx Eth Speed - fix a bug which Kirdy requires link to up to enter main loop --- src/main.rs | 2 ++ src/net/net.rs | 60 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2b9e20a..9185442 100644 --- a/src/main.rs +++ b/src/main.rs @@ -67,6 +67,8 @@ fn main() -> ! { loop { wd.feed(); + net::net::eth_poll_and_update_link_speed(); + match state { State::LoadFlashSettings => { // State Transition diff --git a/src/net/net.rs b/src/net/net.rs index 023bdd5..daaaa97 100644 --- a/src/net/net.rs +++ b/src/net/net.rs @@ -12,12 +12,12 @@ use smoltcp::{ }; use stm32_eth::{ Parts, EthPins, PartsIn, - mac::Speed, + mac::{Speed, EthernetMACWithMii}, dma::{ TxRingEntry, RxRingEntry, EthernetDMA }}; use stm32f4xx_hal::{ - gpio::{gpioa::*, gpiob::*, gpioc::*, Alternate, Input}, + gpio::{gpioa::*, gpiob::*, gpioc::*, Alternate, Input, Pin}, rcc::Clocks, interrupt, }; @@ -52,6 +52,8 @@ pub struct ServerHandle { socket_addr: (IpAddress, u16), iface: EthInterface, dma: EthernetDMA<'static, 'static>, + phy: EthernetPhy>, Pin<'C', 1, Alternate<11>>>>, + link_was_up: bool, } pub type EthernetPins = EthPins, PA7, PB11, PB12, PB13, PC4, PC5>; @@ -155,14 +157,6 @@ impl ServerHandle { 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, - }; - if let Ok(mut phy) = EthernetPhy::from_miim(mac, 0) { info!( "Resetting PHY as an extra step. Type: {}", @@ -170,27 +164,40 @@ impl ServerHandle { ); phy.phy_init(); - while !phy.phy_link_up() {} + + let server = ServerHandle { + socket_handle: tcp_handle, + socket_set: sockets, + socket_addr: socket_addr, + iface: iface, + dma: dma, + phy: phy, + link_was_up: false, + }; + + unsafe { + SERVER_HANDLE = Some(server); + } + } else { + panic!("Ethernet Phy is not supported"); + } + } - if let Some(speed) = phy.speed().map(|s| match s { + pub fn update_link_speed(&mut self) { + if !self.link_was_up & self.phy.phy_link_up() { + if let Some(speed) = self.phy.speed().map(|s| match s { PhySpeed::HalfDuplexBase10T => Speed::HalfDuplexBase10T, PhySpeed::FullDuplexBase10T => Speed::FullDuplexBase10T, PhySpeed::HalfDuplexBase100Tx => Speed::HalfDuplexBase100Tx, PhySpeed::FullDuplexBase100Tx => Speed::FullDuplexBase100Tx, }) { - phy.get_miim().set_speed(speed); - info!("Detected link speed: {:?}", speed); + info!("New eth link is up. Setting detected PhySpeed: {:?}", speed); + self.phy.get_miim().set_speed(speed); } else { debug!("Failed to detect link speed."); } - - } else { - info!("Not resetting unsupported PHY."); - } - - unsafe { - SERVER_HANDLE = Some(server); } + self.link_was_up = self.phy.phy_link_up(); } pub fn recv(&mut self, buffer: &mut [u8])-> Result { @@ -332,6 +339,17 @@ impl EthernetPhy { } } +pub fn eth_poll_and_update_link_speed() { + unsafe { + if let Some(ref mut server_handle ) = SERVER_HANDLE { + server_handle.update_link_speed(); + } + else { + panic!("eth_poll_and_update_link_speed is called before init"); + } + } +} + pub fn eth_send(buffer: &mut [u8], num_bytes: usize) { unsafe { if let Some(ref mut server_handle ) = SERVER_HANDLE {