Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STM32 GenericPhy automatically recover from resets #3821

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions embassy-stm32/src/eth/generic_phy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct GenericPhy {
phy_addr: u8,
#[cfg(feature = "time")]
poll_interval: Duration,
auto_recover: bool,
}

impl GenericPhy {
Expand All @@ -60,6 +61,7 @@ impl GenericPhy {
phy_addr,
#[cfg(feature = "time")]
poll_interval: Duration::from_millis(500),
auto_recover: false,
}
}

Expand All @@ -72,8 +74,17 @@ impl GenericPhy {
phy_addr: 0xFF,
#[cfg(feature = "time")]
poll_interval: Duration::from_millis(500),
auto_recover: false,
}
}

/// Enables automatic detection and recovery from spurious PHY resets.
///
/// If your hardware works perfectly, this should not be necessary.
pub fn that_auto_recovers(mut self) -> Self {
self.auto_recover = true;
self
}
}

// TODO: Factor out to shared functionality
Expand Down Expand Up @@ -133,17 +144,31 @@ impl Phy for GenericPhy {

let bsr = sm.smi_read(self.phy_addr, PHY_REG_BSR);

let mut link_up = true;

// No link without autonegotiate
if bsr & PHY_REG_BSR_ANDONE == 0 {
return false;
link_up = false;
}
// No link if link is down
if bsr & PHY_REG_BSR_UP == 0 {
return false;
if link_up && (bsr & PHY_REG_BSR_UP == 0) {
link_up = false;
}

if !link_up && self.auto_recover {
let bcr = sm.smi_read(self.phy_addr, PHY_REG_BCR);

if (bcr & PHY_REG_BCR_AN == 0) || (bcr & PHY_REG_BCR_100M == 0) {
#[cfg(feature = "defmt")]
defmt::error!("Spurious PHY reset detected, will automatically reconfigure.");

self.phy_reset(sm);
self.phy_init(sm);
}
}

// Got link
true
link_up
}
}

Expand Down