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: Option to detect Ethernet PHY address automatically #3795

Merged
merged 6 commits into from
Jan 26, 2025
Merged
Changes from 2 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
22 changes: 21 additions & 1 deletion embassy-stm32/src/eth/generic_smi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use core::task::Context;

#[cfg(feature = "time")]
use embassy_time::{Duration, Timer};
use embassy_time::{Duration, Timer, Delay};
#[cfg(feature = "time")]
use futures_util::FutureExt;

Expand Down Expand Up @@ -51,6 +51,8 @@ pub struct GenericSMI {

impl GenericSMI {
/// Construct the PHY. It assumes the address `phy_addr` in the SMI communication
///
/// Set `phy_addr` to `0xFF` for automatic detection (only with `time` feature enabled)
nikvoid marked this conversation as resolved.
Show resolved Hide resolved
pub fn new(phy_addr: u8) -> Self {
Self {
phy_addr,
Expand All @@ -62,6 +64,24 @@ impl GenericSMI {

unsafe impl PHY for GenericSMI {
fn phy_reset<S: StationManagement>(&mut self, sm: &mut S) {
// Detect SMI address
#[cfg(feature = "time")]
nikvoid marked this conversation as resolved.
Show resolved Hide resolved
if self.phy_addr == 0xFF {
for addr in 0..32 {
sm.smi_write(addr, PHY_REG_BCR, PHY_REG_BCR_RESET);
for _ in 0..10 {
if sm.smi_read(addr, PHY_REG_BCR) & PHY_REG_BCR_RESET != PHY_REG_BCR_RESET {
trace!("Found ETH PHY on address {}", addr);
self.phy_addr = addr;
return;
}
embedded_hal_1::delay::DelayNs::delay_us(&mut Delay, 1000);
nikvoid marked this conversation as resolved.
Show resolved Hide resolved
cortex_m::asm::delay(1000000);
nikvoid marked this conversation as resolved.
Show resolved Hide resolved
}
}
panic!("PHY did not respond");
}

sm.smi_write(self.phy_addr, PHY_REG_BCR, PHY_REG_BCR_RESET);
while sm.smi_read(self.phy_addr, PHY_REG_BCR) & PHY_REG_BCR_RESET == PHY_REG_BCR_RESET {}
}
Expand Down