From 5885369f47d1260e58656bfc13bebba6339cf6cc Mon Sep 17 00:00:00 2001 From: nikvoid Date: Wed, 22 Jan 2025 13:23:29 +0200 Subject: [PATCH] Option to detect Ethernet PHY address automatically --- embassy-stm32/src/eth/generic_smi.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/embassy-stm32/src/eth/generic_smi.rs b/embassy-stm32/src/eth/generic_smi.rs index 3b43051f4..06d01124f 100644 --- a/embassy-stm32/src/eth/generic_smi.rs +++ b/embassy-stm32/src/eth/generic_smi.rs @@ -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 pub fn new(phy_addr: u8) -> Self { Self { phy_addr, @@ -62,8 +64,24 @@ impl GenericSMI { unsafe impl PHY for GenericSMI { fn phy_reset(&mut self, sm: &mut S) { - 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 {} + // Detect SMI address + 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; + } + cortex_m::asm::delay(1000); + } + } + panic!("PHY did not respond"); + } else { + 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 {} + } } fn phy_init(&mut self, sm: &mut S) {