Skip to content

Commit 66de9ff

Browse files
Johannes DraaijerJohannes Draaijer
authored andcommitted
Remove NoSmi and update examples, add Phy trait
1 parent dddd3ef commit 66de9ff

File tree

10 files changed

+98
-72
lines changed

10 files changed

+98
-72
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn main() {
6666
6767
let mut rx_ring: [RingEntry<_>; 16] = Default::default();
6868
let mut tx_ring: [RingEntry<_>; 8] = Default::default();
69-
let (mut eth_dma, _eth_mac) = stm32_eth::new_no_smi(
69+
let (mut eth_dma, _eth_mac) = stm32_eth::new(
7070
p.ETHERNET_MAC,
7171
p.ETHERNET_MMC,
7272
p.ETHERNET_DMA,

examples/arp-smoltcp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn main() -> ! {
7575

7676
let mut rx_ring: [RingEntry<_>; 16] = Default::default();
7777
let mut tx_ring: [RingEntry<_>; 8] = Default::default();
78-
let (mut eth_dma, mut eth_mac) = stm32_eth::new_borrowed_smi(
78+
let (mut eth_dma, mut eth_mac) = stm32_eth::new(
7979
p.ETHERNET_MAC,
8080
p.ETHERNET_MMC,
8181
p.ETHERNET_DMA,

examples/arp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn main() -> ! {
7373

7474
let mut rx_ring: [RingEntry<_>; 16] = Default::default();
7575
let mut tx_ring: [RingEntry<_>; 8] = Default::default();
76-
let (mut eth_dma, mut eth_mac) = stm32_eth::new_borrowed_smi(
76+
let (mut eth_dma, mut eth_mac) = stm32_eth::new(
7777
p.ETHERNET_MAC,
7878
p.ETHERNET_MMC,
7979
p.ETHERNET_DMA,

examples/ip-f107.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn main() -> ! {
102102

103103
let mut rx_ring: [RingEntry<_>; 8] = Default::default();
104104
let mut tx_ring: [RingEntry<_>; 2] = Default::default();
105-
let (mut eth_dma, _eth_mac) = stm32_eth::new_no_smi(
105+
let (mut eth_dma, _eth_mac) = stm32_eth::new(
106106
p.ETHERNET_MAC,
107107
p.ETHERNET_MMC,
108108
p.ETHERNET_DMA,

examples/ip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn main() -> ! {
8585

8686
let mut rx_ring: [RingEntry<_>; 8] = Default::default();
8787
let mut tx_ring: [RingEntry<_>; 2] = Default::default();
88-
let (mut eth_dma, _eth_mac) = stm32_eth::new_no_smi(
88+
let (mut eth_dma, _eth_mac) = stm32_eth::new(
8989
p.ETHERNET_MAC,
9090
p.ETHERNET_MMC,
9191
p.ETHERNET_DMA,

examples/pktgen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn main() -> ! {
7070

7171
let mut rx_ring: [RingEntry<_>; 16] = Default::default();
7272
let mut tx_ring: [RingEntry<_>; 8] = Default::default();
73-
let (mut eth_dma, mut eth_mac) = stm32_eth::new_borrowed_smi(
73+
let (mut eth_dma, mut eth_mac) = stm32_eth::new(
7474
p.ETHERNET_MAC,
7575
p.ETHERNET_MMC,
7676
p.ETHERNET_DMA,

src/lib.rs

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! An abstraction layer for ethernet periperhals embedded in STM32 processors.
22
//!
3-
//! For initialisation, see [`new_no_smi`], [`new_borrowed_smi`], and [`new_owned_smi`]
3+
//! For initialisation, see [`new`], and [`new_owned_smi`]
44
#![no_std]
55

66
/// Re-export
@@ -84,55 +84,45 @@ pub struct EthernetDMA<'rx, 'tx> {
8484
tx_ring: TxRing<'tx>,
8585
}
8686

87-
macro_rules! new {
88-
($name:ident, $smi_ty:ty) => {
89-
/// Create and initialise the ethernet driver.
90-
///
91-
/// Initialize and start tx and rx DMA engines.
92-
/// Sets up the peripheral clocks and GPIO configuration,
93-
/// and configures the ETH MAC and DMA peripherals.
94-
/// Automatically sets slew rate to VeryHigh.
95-
/// If you wish to use another configuration, please see
96-
/// [new_unchecked](new_unchecked).
97-
///
98-
/// This method does not initialise the external PHY.
99-
///
100-
/// Interacting with a PHY can be done through a struct that implementes the
101-
/// [`StationManagement`] trait.
102-
///
103-
/// In the case of `new_borrowed_smi` you may access SMI through the [`EthernetMAC::smi`] function.
104-
///
105-
/// In the case of `new_no_smi`, the SMI may be accessed by constructing a new [`Smi`] before passing
106-
/// the `ETHERNET_MAC` to the `new_no_smi` function.
107-
pub fn $name<'rx, 'tx, REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1>(
108-
eth_mac: ETHERNET_MAC,
109-
eth_mmc: ETHERNET_MMC,
110-
eth_dma: ETHERNET_DMA,
111-
rx_buffer: &'rx mut [RxRingEntry],
112-
tx_buffer: &'tx mut [TxRingEntry],
113-
clocks: Clocks,
114-
pins: EthPins<REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1>,
115-
) -> Result<(EthernetDMA<'rx, 'tx>, EthernetMAC<$smi_ty>), WrongClock>
116-
where
117-
REFCLK: RmiiRefClk + AlternateVeryHighSpeed,
118-
CRS: RmiiCrsDv + AlternateVeryHighSpeed,
119-
TXEN: RmiiTxEN + AlternateVeryHighSpeed,
120-
TXD0: RmiiTxD0 + AlternateVeryHighSpeed,
121-
TXD1: RmiiTxD1 + AlternateVeryHighSpeed,
122-
RXD0: RmiiRxD0 + AlternateVeryHighSpeed,
123-
RXD1: RmiiRxD1 + AlternateVeryHighSpeed,
124-
{
125-
pins.setup_pins();
126-
127-
let eth_mac = EthernetMAC::<$smi_ty>::new(eth_mac);
128-
129-
unsafe { new_unchecked(eth_mac, eth_mmc, eth_dma, rx_buffer, tx_buffer, clocks) }
130-
}
131-
};
132-
}
87+
/// Create and initialise the ethernet driver.
88+
///
89+
/// Initialize and start tx and rx DMA engines.
90+
/// Sets up the peripheral clocks and GPIO configuration,
91+
/// and configures the ETH MAC and DMA peripherals.
92+
/// Automatically sets slew rate to VeryHigh.
93+
/// If you wish to use another configuration, please see
94+
/// [new_unchecked](new_unchecked).
95+
///
96+
/// This method does not initialise the external PHY.
97+
///
98+
/// Interacting with a PHY can be done through a struct that implementes the
99+
/// [`mac::StationManagement`] trait.
100+
///
101+
/// You may access SMI through the [`EthernetMAC::smi`] function.
102+
pub fn new<'rx, 'tx, REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1>(
103+
eth_mac: ETHERNET_MAC,
104+
eth_mmc: ETHERNET_MMC,
105+
eth_dma: ETHERNET_DMA,
106+
rx_buffer: &'rx mut [RxRingEntry],
107+
tx_buffer: &'tx mut [TxRingEntry],
108+
clocks: Clocks,
109+
pins: EthPins<REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1>,
110+
) -> Result<(EthernetDMA<'rx, 'tx>, EthernetMAC<mac::BorrowedSmi>), WrongClock>
111+
where
112+
REFCLK: RmiiRefClk + AlternateVeryHighSpeed,
113+
CRS: RmiiCrsDv + AlternateVeryHighSpeed,
114+
TXEN: RmiiTxEN + AlternateVeryHighSpeed,
115+
TXD0: RmiiTxD0 + AlternateVeryHighSpeed,
116+
TXD1: RmiiTxD1 + AlternateVeryHighSpeed,
117+
RXD0: RmiiRxD0 + AlternateVeryHighSpeed,
118+
RXD1: RmiiRxD1 + AlternateVeryHighSpeed,
119+
{
120+
pins.setup_pins();
121+
122+
let eth_mac = EthernetMAC::<mac::BorrowedSmi>::new(eth_mac);
133123

134-
new!(new_no_smi, mac::NoSmi);
135-
new!(new_borrowed_smi, mac::BorrowedSmi);
124+
unsafe { new_unchecked(eth_mac, eth_mmc, eth_dma, rx_buffer, tx_buffer, clocks) }
125+
}
136126

137127
/// Create and initialise the ethernet driver.
138128
///
@@ -144,7 +134,7 @@ new!(new_borrowed_smi, mac::BorrowedSmi);
144134
/// [new_unchecked](new_unchecked).
145135
///
146136
/// This method does not initialise the external PHY. The SMI for the external PHY
147-
/// can be accessed through a struct that implements the [`StationManagement`] trait,
137+
/// can be accessed through a struct that implements the [`mac::StationManagement`] trait,
148138
/// which [`EthernetMAC<OwnedSmi>`] does.
149139
///
150140
/// # Note
@@ -190,8 +180,8 @@ where
190180
/// Create and initialise the ethernet driver (without GPIO configuration and validation).
191181
///
192182
/// This method does not initialise the external PHY. However it does return an
193-
/// [EthernetMAC](EthernetMAC) which implements the
194-
/// [StationManagement](StationManagement) trait. This can be used to
183+
/// [`EthernetMAC`] which implements the
184+
/// [`mac::StationManagement`] trait. This can be used to
195185
/// communicate with the external PHY.
196186
///
197187
/// # Note

src/mac.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,6 @@ pub struct EthernetMAC<SMI> {
157157
_state: SMI,
158158
}
159159

160-
impl EthernetMAC<NoSmi> {
161-
pub fn new(eth_mac: ETHERNET_MAC) -> Self {
162-
Self {
163-
eth_mac,
164-
_state: NoSmi {},
165-
}
166-
}
167-
}
168-
169-
/// The MAC does not have and does not provide access to its SMI
170-
pub struct NoSmi;
171-
172160
/// Access to the MAC's SMI can be obtained by borrowing [`MdioPin`] and [`MdcPin`] to it. For [`StationManagement`], see [`EthernetMAC::smi`]
173161
pub struct BorrowedSmi;
174162

src/phy/lan87xxa.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ where
160160

161161
/// Get the link speed
162162
///
163-
/// If this returns `None`, some sort of corruption happened or the PHY is
163+
/// If this returns `None`, some sort of corruption occured, or the PHY is
164164
/// in an illegal state
165-
pub fn get_link_speed(&mut self) -> Option<LinkSpeed> {
165+
pub fn link_speed(&mut self) -> Option<LinkSpeed> {
166166
let link_data = self.read(PHY_REG_SSR);
167167
let link_data = ((link_data >> 2) & 0b111) as u8;
168168
LinkSpeed::try_from(link_data).ok()
@@ -183,3 +183,26 @@ where
183183
self.smi
184184
}
185185
}
186+
187+
impl<S, const EXT_WUCSR_CLEAR: bool> super::Phy for LAN87xxA<S, EXT_WUCSR_CLEAR>
188+
where
189+
S: StationManagement,
190+
{
191+
type LinkSpeed = Option<LinkSpeed>;
192+
193+
fn reset(&mut self) {
194+
self.phy_reset()
195+
}
196+
197+
fn init(&mut self) {
198+
self.phy_init()
199+
}
200+
201+
fn poll_link(&mut self) -> bool {
202+
self.poll_link()
203+
}
204+
205+
fn link_speed(&mut self) -> Self::LinkSpeed {
206+
self.link_speed()
207+
}
208+
}

src/phy/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,28 @@
22
mod lan87xxa;
33
#[cfg(feature = "phy-lan87xxa")]
44
pub use lan87xxa::*;
5+
6+
pub trait Phy {
7+
type LinkSpeed;
8+
9+
/// Reset the PHY
10+
///
11+
/// TODO: describe what "reset" actually is
12+
fn reset(&mut self);
13+
14+
/// Initialize the PHY
15+
///
16+
/// TODO: describe what "init" actually is
17+
fn init(&mut self);
18+
19+
/// Poll the link status of this
20+
fn poll_link(&mut self) -> bool;
21+
22+
/// Get the link speed reported by the PHY
23+
fn link_speed(&mut self) -> Self::LinkSpeed;
24+
25+
/// Check whether the PHY reports that a link is established
26+
fn link_established(&mut self) -> bool {
27+
self.poll_link()
28+
}
29+
}

0 commit comments

Comments
 (0)