Skip to content

Commit ecc1aab

Browse files
authored
Merge pull request #3813 from embassy-rs/stm32-eth-simplify-smi
stm32/eth: rename PHY->Phy, GenericSMI -> GenericPhy. Remove unneeded unsafes.
2 parents c8d29a1 + b124585 commit ecc1aab

File tree

12 files changed

+56
-66
lines changed

12 files changed

+56
-66
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use embassy_time::{Duration, Timer};
77
#[cfg(feature = "time")]
88
use futures_util::FutureExt;
99

10-
use super::{StationManagement, PHY};
10+
use super::{Phy, StationManagement};
1111

1212
#[allow(dead_code)]
1313
mod phy_consts {
@@ -43,13 +43,13 @@ mod phy_consts {
4343
use self::phy_consts::*;
4444

4545
/// Generic SMI Ethernet PHY implementation
46-
pub struct GenericSMI {
46+
pub struct GenericPhy {
4747
phy_addr: u8,
4848
#[cfg(feature = "time")]
4949
poll_interval: Duration,
5050
}
5151

52-
impl GenericSMI {
52+
impl GenericPhy {
5353
/// Construct the PHY. It assumes the address `phy_addr` in the SMI communication
5454
///
5555
/// # Panics
@@ -89,7 +89,7 @@ fn blocking_delay_us(us: u32) {
8989
}
9090
}
9191

92-
unsafe impl PHY for GenericSMI {
92+
impl Phy for GenericPhy {
9393
fn phy_reset<S: StationManagement>(&mut self, sm: &mut S) {
9494
// Detect SMI address
9595
if self.phy_addr == 0xFF {
@@ -148,7 +148,7 @@ unsafe impl PHY for GenericSMI {
148148
}
149149

150150
/// Public functions for the PHY
151-
impl GenericSMI {
151+
impl GenericPhy {
152152
/// Set the SMI polling interval.
153153
#[cfg(feature = "time")]
154154
pub fn set_poll_interval(&mut self, poll_interval: Duration) {

embassy-stm32/src/eth/mod.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#[cfg_attr(any(eth_v1a, eth_v1b, eth_v1c), path = "v1/mod.rs")]
55
#[cfg_attr(eth_v2, path = "v2/mod.rs")]
66
mod _version;
7-
pub mod generic_smi;
7+
mod generic_phy;
88

99
use core::mem::MaybeUninit;
1010
use core::task::Context;
@@ -13,6 +13,7 @@ use embassy_net_driver::{Capabilities, HardwareAddress, LinkState};
1313
use embassy_sync::waitqueue::AtomicWaker;
1414

1515
pub use self::_version::{InterruptHandler, *};
16+
pub use self::generic_phy::*;
1617
use crate::rcc::RccPeripheral;
1718

1819
#[allow(unused)]
@@ -71,7 +72,7 @@ impl<const TX: usize, const RX: usize> PacketQueue<TX, RX> {
7172

7273
static WAKER: AtomicWaker = AtomicWaker::new();
7374

74-
impl<'d, T: Instance, P: PHY> embassy_net_driver::Driver for Ethernet<'d, T, P> {
75+
impl<'d, T: Instance, P: Phy> embassy_net_driver::Driver for Ethernet<'d, T, P> {
7576
type RxToken<'a>
7677
= RxToken<'a, 'd>
7778
where
@@ -156,23 +157,15 @@ impl<'a, 'd> embassy_net_driver::TxToken for TxToken<'a, 'd> {
156157
}
157158

158159
/// Station Management Interface (SMI) on an ethernet PHY
159-
///
160-
/// # Safety
161-
///
162-
/// The methods cannot move out of self
163-
pub unsafe trait StationManagement {
160+
pub trait StationManagement {
164161
/// Read a register over SMI.
165162
fn smi_read(&mut self, phy_addr: u8, reg: u8) -> u16;
166163
/// Write a register over SMI.
167164
fn smi_write(&mut self, phy_addr: u8, reg: u8, val: u16);
168165
}
169166

170-
/// Traits for an Ethernet PHY
171-
///
172-
/// # Safety
173-
///
174-
/// The methods cannot move S
175-
pub unsafe trait PHY {
167+
/// Trait for an Ethernet PHY
168+
pub trait Phy {
176169
/// Reset PHY and wait for it to come out of reset.
177170
fn phy_reset<S: StationManagement>(&mut self, sm: &mut S);
178171
/// PHY initialisation.
@@ -181,18 +174,23 @@ pub unsafe trait PHY {
181174
fn poll_link<S: StationManagement>(&mut self, sm: &mut S, cx: &mut Context) -> bool;
182175
}
183176

184-
impl<'d, T: Instance, P: PHY> Ethernet<'d, T, P> {
177+
impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> {
185178
/// Directly expose the SMI interface used by the Ethernet driver.
186179
///
187180
/// This can be used to for example configure special PHY registers for compliance testing.
188-
///
189-
/// # Safety
190-
///
191-
/// Revert any temporary PHY register changes such as to enable test modes before handing
192-
/// the Ethernet device over to the networking stack otherwise things likely won't work.
193-
pub unsafe fn station_management(&mut self) -> &mut impl StationManagement {
181+
pub fn station_management(&mut self) -> &mut impl StationManagement {
194182
&mut self.station_management
195183
}
184+
185+
/// Access the user-supplied `Phy`.
186+
pub fn phy(&self) -> &P {
187+
&self.phy
188+
}
189+
190+
/// Mutably access the user-supplied `Phy`.
191+
pub fn phy_mut(&mut self) -> &mut P {
192+
&mut self.phy
193+
}
196194
}
197195

198196
trait SealedInstance {

embassy-stm32/src/eth/v1/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl interrupt::typelevel::Handler<interrupt::typelevel::ETH> for InterruptHandl
4646
}
4747

4848
/// Ethernet driver.
49-
pub struct Ethernet<'d, T: Instance, P: PHY> {
49+
pub struct Ethernet<'d, T: Instance, P: Phy> {
5050
_peri: PeripheralRef<'d, T>,
5151
pub(crate) tx: TDesRing<'d>,
5252
pub(crate) rx: RDesRing<'d>,
@@ -91,7 +91,7 @@ macro_rules! config_pins {
9191
};
9292
}
9393

94-
impl<'d, T: Instance, P: PHY> Ethernet<'d, T, P> {
94+
impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> {
9595
/// safety: the returned instance is not leak-safe
9696
pub fn new<const TX: usize, const RX: usize>(
9797
queue: &'d mut PacketQueue<TX, RX>,
@@ -272,12 +272,12 @@ impl<'d, T: Instance, P: PHY> Ethernet<'d, T, P> {
272272
}
273273

274274
/// Ethernet station management interface.
275-
pub struct EthernetStationManagement<T: Instance> {
275+
pub(crate) struct EthernetStationManagement<T: Instance> {
276276
peri: PhantomData<T>,
277277
clock_range: Cr,
278278
}
279279

280-
unsafe impl<T: Instance> StationManagement for EthernetStationManagement<T> {
280+
impl<T: Instance> StationManagement for EthernetStationManagement<T> {
281281
fn smi_read(&mut self, phy_addr: u8, reg: u8) -> u16 {
282282
let mac = T::regs().ethernet_mac();
283283

@@ -307,7 +307,7 @@ unsafe impl<T: Instance> StationManagement for EthernetStationManagement<T> {
307307
}
308308
}
309309

310-
impl<'d, T: Instance, P: PHY> Drop for Ethernet<'d, T, P> {
310+
impl<'d, T: Instance, P: Phy> Drop for Ethernet<'d, T, P> {
311311
fn drop(&mut self) {
312312
let dma = T::regs().ethernet_dma();
313313
let mac = T::regs().ethernet_mac();

embassy-stm32/src/eth/v2/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl interrupt::typelevel::Handler<interrupt::typelevel::ETH> for InterruptHandl
3636
}
3737

3838
/// Ethernet driver.
39-
pub struct Ethernet<'d, T: Instance, P: PHY> {
39+
pub struct Ethernet<'d, T: Instance, P: Phy> {
4040
_peri: PeripheralRef<'d, T>,
4141
pub(crate) tx: TDesRing<'d>,
4242
pub(crate) rx: RDesRing<'d>,
@@ -63,7 +63,7 @@ macro_rules! config_pins {
6363
};
6464
}
6565

66-
impl<'d, T: Instance, P: PHY> Ethernet<'d, T, P> {
66+
impl<'d, T: Instance, P: Phy> Ethernet<'d, T, P> {
6767
/// Create a new RMII ethernet driver using 9 pins.
6868
pub fn new<const TX: usize, const RX: usize>(
6969
queue: &'d mut PacketQueue<TX, RX>,
@@ -304,7 +304,7 @@ pub struct EthernetStationManagement<T: Instance> {
304304
clock_range: u8,
305305
}
306306

307-
unsafe impl<T: Instance> StationManagement for EthernetStationManagement<T> {
307+
impl<T: Instance> StationManagement for EthernetStationManagement<T> {
308308
fn smi_read(&mut self, phy_addr: u8, reg: u8) -> u16 {
309309
let mac = T::regs().ethernet_mac();
310310

@@ -334,7 +334,7 @@ unsafe impl<T: Instance> StationManagement for EthernetStationManagement<T> {
334334
}
335335
}
336336

337-
impl<'d, T: Instance, P: PHY> Drop for Ethernet<'d, T, P> {
337+
impl<'d, T: Instance, P: Phy> Drop for Ethernet<'d, T, P> {
338338
fn drop(&mut self) {
339339
let dma = T::regs().ethernet_dma();
340340
let mac = T::regs().ethernet_mac();

examples/stm32f4/src/bin/eth.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use defmt::*;
55
use embassy_executor::Spawner;
66
use embassy_net::tcp::TcpSocket;
77
use embassy_net::{Ipv4Address, StackResources};
8-
use embassy_stm32::eth::generic_smi::GenericSMI;
9-
use embassy_stm32::eth::{Ethernet, PacketQueue};
8+
use embassy_stm32::eth::{Ethernet, GenericPhy, PacketQueue};
109
use embassy_stm32::peripherals::ETH;
1110
use embassy_stm32::rng::Rng;
1211
use embassy_stm32::time::Hertz;
@@ -21,7 +20,7 @@ bind_interrupts!(struct Irqs {
2120
HASH_RNG => rng::InterruptHandler<peripherals::RNG>;
2221
});
2322

24-
type Device = Ethernet<'static, ETH, GenericSMI>;
23+
type Device = Ethernet<'static, ETH, GenericPhy>;
2524

2625
#[embassy_executor::task]
2726
async fn net_task(mut runner: embassy_net::Runner<'static, Device>) -> ! {
@@ -76,7 +75,7 @@ async fn main(spawner: Spawner) -> ! {
7675
p.PG13,
7776
p.PB13,
7877
p.PG11,
79-
GenericSMI::new_auto(),
78+
GenericPhy::new_auto(),
8079
mac_addr,
8180
);
8281

examples/stm32f4/src/bin/eth_compliance_test.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
use defmt::*;
55
use embassy_executor::Spawner;
6-
use embassy_stm32::eth::generic_smi::GenericSMI;
7-
use embassy_stm32::eth::{Ethernet, PacketQueue, StationManagement};
6+
use embassy_stm32::eth::{Ethernet, GenericPhy, PacketQueue, StationManagement};
87
use embassy_stm32::time::Hertz;
98
use embassy_stm32::{bind_interrupts, eth, peripherals, rng, Config};
109
use embassy_time::Timer;
@@ -59,11 +58,11 @@ async fn main(_spawner: Spawner) -> ! {
5958
p.PG13,
6059
p.PB13,
6160
p.PG11,
62-
GenericSMI::new(PHY_ADDR),
61+
GenericPhy::new(PHY_ADDR),
6362
mac_addr,
6463
);
6564

66-
let sm = unsafe { device.station_management() };
65+
let sm = device.station_management();
6766

6867
// Just an example. Exact register settings depend on the specific PHY and test.
6968
sm.smi_write(PHY_ADDR, 0, 0x2100);

examples/stm32f7/src/bin/eth.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use defmt::*;
55
use embassy_executor::Spawner;
66
use embassy_net::tcp::TcpSocket;
77
use embassy_net::{Ipv4Address, StackResources};
8-
use embassy_stm32::eth::generic_smi::GenericSMI;
9-
use embassy_stm32::eth::{Ethernet, PacketQueue};
8+
use embassy_stm32::eth::{Ethernet, GenericPhy, PacketQueue};
109
use embassy_stm32::peripherals::ETH;
1110
use embassy_stm32::rng::Rng;
1211
use embassy_stm32::time::Hertz;
@@ -22,7 +21,7 @@ bind_interrupts!(struct Irqs {
2221
HASH_RNG => rng::InterruptHandler<peripherals::RNG>;
2322
});
2423

25-
type Device = Ethernet<'static, ETH, GenericSMI>;
24+
type Device = Ethernet<'static, ETH, GenericPhy>;
2625

2726
#[embassy_executor::task]
2827
async fn net_task(mut runner: embassy_net::Runner<'static, Device>) -> ! {
@@ -77,7 +76,7 @@ async fn main(spawner: Spawner) -> ! {
7776
p.PG13,
7877
p.PB13,
7978
p.PG11,
80-
GenericSMI::new_auto(),
79+
GenericPhy::new_auto(),
8180
mac_addr,
8281
);
8382

examples/stm32h5/src/bin/eth.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use defmt::*;
55
use embassy_executor::Spawner;
66
use embassy_net::tcp::TcpSocket;
77
use embassy_net::{Ipv4Address, StackResources};
8-
use embassy_stm32::eth::generic_smi::GenericSMI;
9-
use embassy_stm32::eth::{Ethernet, PacketQueue};
8+
use embassy_stm32::eth::{Ethernet, GenericPhy, PacketQueue};
109
use embassy_stm32::peripherals::ETH;
1110
use embassy_stm32::rcc::{
1211
AHBPrescaler, APBPrescaler, Hse, HseMode, Pll, PllDiv, PllMul, PllPreDiv, PllSource, Sysclk, VoltageScale,
@@ -25,7 +24,7 @@ bind_interrupts!(struct Irqs {
2524
RNG => rng::InterruptHandler<peripherals::RNG>;
2625
});
2726

28-
type Device = Ethernet<'static, ETH, GenericSMI>;
27+
type Device = Ethernet<'static, ETH, GenericPhy>;
2928

3029
#[embassy_executor::task]
3130
async fn net_task(mut runner: embassy_net::Runner<'static, Device>) -> ! {
@@ -80,7 +79,7 @@ async fn main(spawner: Spawner) -> ! {
8079
p.PG13,
8180
p.PB15,
8281
p.PG11,
83-
GenericSMI::new_auto(),
82+
GenericPhy::new_auto(),
8483
mac_addr,
8584
);
8685

examples/stm32h7/src/bin/eth.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use defmt::*;
55
use embassy_executor::Spawner;
66
use embassy_net::tcp::TcpSocket;
77
use embassy_net::{Ipv4Address, StackResources};
8-
use embassy_stm32::eth::generic_smi::GenericSMI;
9-
use embassy_stm32::eth::{Ethernet, PacketQueue};
8+
use embassy_stm32::eth::{Ethernet, GenericPhy, PacketQueue};
109
use embassy_stm32::peripherals::ETH;
1110
use embassy_stm32::rng::Rng;
1211
use embassy_stm32::{bind_interrupts, eth, peripherals, rng, Config};
@@ -21,7 +20,7 @@ bind_interrupts!(struct Irqs {
2120
RNG => rng::InterruptHandler<peripherals::RNG>;
2221
});
2322

24-
type Device = Ethernet<'static, ETH, GenericSMI>;
23+
type Device = Ethernet<'static, ETH, GenericPhy>;
2524

2625
#[embassy_executor::task]
2726
async fn net_task(mut runner: embassy_net::Runner<'static, Device>) -> ! {
@@ -79,7 +78,7 @@ async fn main(spawner: Spawner) -> ! {
7978
p.PG13, // TX_D0: Transmit Bit 0
8079
p.PB13, // TX_D1: Transmit Bit 1
8180
p.PG11, // TX_EN: Transmit Enable
82-
GenericSMI::new_auto(),
81+
GenericPhy::new_auto(),
8382
mac_addr,
8483
);
8584

examples/stm32h7/src/bin/eth_client.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use defmt::*;
77
use embassy_executor::Spawner;
88
use embassy_net::tcp::client::{TcpClient, TcpClientState};
99
use embassy_net::StackResources;
10-
use embassy_stm32::eth::generic_smi::GenericSMI;
11-
use embassy_stm32::eth::{Ethernet, PacketQueue};
10+
use embassy_stm32::eth::{Ethernet, GenericPhy, PacketQueue};
1211
use embassy_stm32::peripherals::ETH;
1312
use embassy_stm32::rng::Rng;
1413
use embassy_stm32::{bind_interrupts, eth, peripherals, rng, Config};
@@ -24,7 +23,7 @@ bind_interrupts!(struct Irqs {
2423
RNG => rng::InterruptHandler<peripherals::RNG>;
2524
});
2625

27-
type Device = Ethernet<'static, ETH, GenericSMI>;
26+
type Device = Ethernet<'static, ETH, GenericPhy>;
2827

2928
#[embassy_executor::task]
3029
async fn net_task(mut runner: embassy_net::Runner<'static, Device>) -> ! {
@@ -81,7 +80,7 @@ async fn main(spawner: Spawner) -> ! {
8180
p.PG13,
8281
p.PB13,
8382
p.PG11,
84-
GenericSMI::new_auto(),
83+
GenericPhy::new_auto(),
8584
mac_addr,
8685
);
8786

0 commit comments

Comments
 (0)