Skip to content

Commit c57ad7c

Browse files
Johannes Draaijerdatdenkikniet
authored andcommitted
Switch to ieee802_3_miim for provisioning of MIIM
1 parent 470a81c commit c57ad7c

File tree

9 files changed

+88
-328
lines changed

9 files changed

+88
-328
lines changed

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ aligned = "0.4"
2323
stm32f7xx-hal = { version = "0.7.0", optional = true }
2424
stm32f4xx-hal = { version = "0.13", optional = true }
2525
stm32f1xx-hal = { version = "0.9", optional = true }
26+
ieee802_3_miim = { version = "0.6", optional = true }
2627
cortex-m = "0.7"
2728
log = { version = "0.4", optional = true }
2829

@@ -38,11 +39,9 @@ default-features = false
3839
optional = true
3940

4041
[features]
41-
default = [ "phy-lan87xxa" ]
42+
default = [ "ieee802_3_miim" ]
4243
device-selected = []
4344
fence = []
44-
phy = [ "num_enum" ]
45-
phy-lan87xxa = [ "phy" ]
4645

4746
stm32f107 = ["stm32f1xx-hal/stm32f107", "device-selected"]
4847

examples/arp-smoltcp.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,19 @@ use cortex_m::interrupt::Mutex;
1818
use cortex_m_rt::{entry, exception};
1919
use cortex_m_semihosting::hprintln;
2020
use fugit::RateExtU32;
21+
use ieee802_3_miim::Phy;
2122
use smoltcp::wire::{
2223
ArpOperation, ArpPacket, ArpRepr, EthernetAddress, EthernetFrame, EthernetProtocol,
2324
EthernetRepr, Ipv4Address,
2425
};
2526
use stm32_eth::{
2627
hal::gpio::{GpioExt, Speed},
2728
hal::rcc::RccExt,
28-
mac::{MdcPin, MdioPin, Smi, SerialManagement},
29+
mac::phy::bare::BarePhy,
2930
stm32::{interrupt, CorePeripherals, Peripherals, SYST},
3031
};
3132
use stm32_eth::{EthPins, RingEntry, TxError};
3233

33-
const PHY_REG_BSR: u8 = 0x01;
34-
const PHY_REG_BSR_UP: u16 = 1 << 2;
35-
3634
const PHY_ADDR: u8 = 0;
3735

3836
static TIME: Mutex<RefCell<usize>> = Mutex::new(RefCell::new(0));
@@ -75,7 +73,7 @@ fn main() -> ! {
7573

7674
let mut rx_ring: [RingEntry<_>; 16] = Default::default();
7775
let mut tx_ring: [RingEntry<_>; 8] = Default::default();
78-
let (mut eth_dma, mut eth_mac) = stm32_eth::new(
76+
let (mut eth_dma, eth_mac) = stm32_eth::new(
7977
p.ETHERNET_MAC,
8078
p.ETHERNET_MMC,
8179
p.ETHERNET_DMA,
@@ -89,8 +87,10 @@ fn main() -> ! {
8987

9088
let mut last_link_up = false;
9189

90+
let mut bare_phy = BarePhy::new(eth_mac.with_smi(mdio, mdc), PHY_ADDR, Default::default());
91+
9292
loop {
93-
let link_up = link_detected(eth_mac.smi(&mut mdio, &mut mdc));
93+
let link_up = bare_phy.phy_link_up();
9494

9595
if link_up != last_link_up {
9696
if link_up {
@@ -179,12 +179,3 @@ fn ETH() {
179179
let p = unsafe { Peripherals::steal() };
180180
stm32_eth::eth_interrupt_handler(&p.ETHERNET_DMA);
181181
}
182-
183-
fn link_detected<Mdio, Mdc>(smi: Smi<Mdio, Mdc>) -> bool
184-
where
185-
Mdio: MdioPin,
186-
Mdc: MdcPin,
187-
{
188-
let status = smi.read(PHY_ADDR, PHY_REG_BSR);
189-
(status & PHY_REG_BSR_UP) == PHY_REG_BSR_UP
190-
}

examples/arp.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,14 @@ use fugit::RateExtU32;
2020
use stm32_eth::{
2121
hal::gpio::{GpioExt, Speed},
2222
hal::rcc::RccExt,
23-
mac::{self, SerialManagement},
23+
mac::{phy::bare::BarePhy, Phy},
2424
stm32::{interrupt, CorePeripherals, Peripherals, SYST},
2525
};
2626

2727
use cortex_m_semihosting::hprintln;
2828

2929
use stm32_eth::{EthPins, RingEntry, TxError};
3030

31-
const PHY_REG_BSR: u8 = 0x01;
32-
const PHY_REG_BSR_UP: u16 = 1 << 2;
33-
3431
const PHY_ADDR: u8 = 0;
3532

3633
static TIME: Mutex<RefCell<usize>> = Mutex::new(RefCell::new(0));
@@ -73,7 +70,7 @@ fn main() -> ! {
7370

7471
let mut rx_ring: [RingEntry<_>; 16] = Default::default();
7572
let mut tx_ring: [RingEntry<_>; 8] = Default::default();
76-
let (mut eth_dma, mut eth_mac) = stm32_eth::new(
73+
let (mut eth_dma, eth_mac) = stm32_eth::new(
7774
p.ETHERNET_MAC,
7875
p.ETHERNET_MMC,
7976
p.ETHERNET_DMA,
@@ -87,8 +84,10 @@ fn main() -> ! {
8784

8885
let mut last_link_up = false;
8986

87+
let mut bare_phy = BarePhy::new(eth_mac.with_smi(mdio, mdc), PHY_ADDR, Default::default());
88+
9089
loop {
91-
let link_up = link_detected(eth_mac.smi(&mut mdio, &mut mdc));
90+
let link_up = bare_phy.phy_link_up();
9291

9392
if link_up != last_link_up {
9493
if link_up {
@@ -177,12 +176,3 @@ fn ETH() {
177176
let p = unsafe { Peripherals::steal() };
178177
stm32_eth::eth_interrupt_handler(&p.ETHERNET_DMA);
179178
}
180-
181-
fn link_detected<Mdio, Mdc>(smi: mac::Smi<Mdio, Mdc>) -> bool
182-
where
183-
Mdio: mac::MdioPin,
184-
Mdc: mac::MdcPin,
185-
{
186-
let status = smi.read(PHY_ADDR, PHY_REG_BSR);
187-
(status & PHY_REG_BSR_UP) == PHY_REG_BSR_UP
188-
}

examples/pktgen.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use fugit::RateExtU32;
1717
use stm32_eth::{
1818
hal::gpio::{GpioExt, Speed},
1919
hal::rcc::RccExt,
20-
mac,
20+
mac::{phy::bare::BarePhy, Phy},
2121
stm32::{interrupt, CorePeripherals, Peripherals, SYST},
2222
};
2323

@@ -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(
73+
let (mut eth_dma, eth_mac) = stm32_eth::new(
7474
p.ETHERNET_MAC,
7575
p.ETHERNET_MMC,
7676
p.ETHERNET_DMA,
@@ -90,6 +90,8 @@ fn main() -> ! {
9090
let mut tx_pkts = 0usize;
9191
let mut last_link_up = false;
9292

93+
let mut phy = BarePhy::new(eth_mac.with_smi(mdio, mdc), PHY_ADDR, Default::default());
94+
9395
loop {
9496
let time: usize = cortex_m::interrupt::free(|cs| *TIME.borrow(cs).borrow());
9597

@@ -115,7 +117,7 @@ fn main() -> ! {
115117
}
116118

117119
// Link change detection
118-
let link_up = link_detected(eth_mac.smi(&mut mdio, &mut mdc));
120+
let link_up = phy.phy_link_up();
119121
if link_up != last_link_up {
120122
if link_up {
121123
writeln!(stdout, "Ethernet: no link detected").unwrap();
@@ -151,7 +153,7 @@ fn main() -> ! {
151153

152154
// fill tx queue
153155
const SIZE: usize = 1500;
154-
if link_detected(eth_mac.smi(&mut mdio, &mut mdc)) {
156+
if phy.phy_link_up() {
155157
'egress: loop {
156158
let r = eth_dma.send(SIZE, |buf| {
157159
buf[0..6].copy_from_slice(&DST_MAC);
@@ -213,14 +215,3 @@ fn ETH() {
213215
let p = unsafe { Peripherals::steal() };
214216
stm32_eth::eth_interrupt_handler(&p.ETHERNET_DMA);
215217
}
216-
217-
fn link_detected<Mdio, Mdc>(smi: mac::Smi<Mdio, Mdc>) -> bool
218-
where
219-
Mdio: mac::MdioPin,
220-
Mdc: mac::MdcPin,
221-
{
222-
const STATUS_REG_ADDR: u8 = 0x3;
223-
const STATUS_REG_UP_MASK: u16 = 1 << 2;
224-
let status = smi.read(PHY_ADDR, STATUS_REG_ADDR);
225-
(status & STATUS_REG_UP_MASK) == STATUS_REG_UP_MASK
226-
}

src/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! For initialisation, see [`new`], and [`new_with_smi`]
44
#![no_std]
55

6-
use mac::EthernetMACWithSmi;
6+
use mac::EthernetMACWithMiim;
77
/// Re-export
88
#[cfg(feature = "stm32f7xx-hal")]
99
pub use stm32f7xx_hal as hal;
@@ -52,11 +52,6 @@ mod smoltcp_phy;
5252
#[cfg(feature = "smoltcp-phy")]
5353
pub use smoltcp_phy::{EthRxToken, EthTxToken};
5454

55-
#[cfg(feature = "phy")]
56-
mod phy;
57-
#[cfg(feature = "phy")]
58-
pub use phy::*;
59-
6055
/// From the datasheet: *VLAN Frame maxsize = 1522*
6156
const MTU: usize = 1522;
6257

@@ -152,7 +147,7 @@ pub fn new_with_smi<'rx, 'tx, REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1, MDIO, M
152147
pins: EthPins<REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1>,
153148
mdio: MDIO,
154149
mdc: MDC,
155-
) -> Result<(EthernetDMA<'rx, 'tx>, EthernetMACWithSmi<MDIO, MDC>), WrongClock>
150+
) -> Result<(EthernetDMA<'rx, 'tx>, EthernetMACWithMiim<MDIO, MDC>), WrongClock>
156151
where
157152
REFCLK: RmiiRefClk + AlternateVeryHighSpeed,
158153
CRS: RmiiCrsDv + AlternateVeryHighSpeed,

src/mac/smi.rs renamed to src/mac/miim.rs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1+
#[cfg(feature = "ieee802_3_miim")]
2+
pub use ieee802_3_miim::Miim;
3+
4+
#[cfg(feature = "ieee802_3_miim")]
5+
pub use ieee802_3_miim::*;
6+
17
use crate::stm32::ethernet_mac::{MACMIIAR, MACMIIDR};
28

39
/// MDIO pin types.
410
pub unsafe trait MdioPin {}
511
/// MDC pin types.
612
pub unsafe trait MdcPin {}
713

8-
/// A trait used for implementing access to SMI
9-
/// peripherals/functionality
10-
pub trait SerialManagement {
11-
/// Read an SMI register
12-
fn read(&self, phy: u8, reg: u8) -> u16;
13-
/// Write an SMI register
14-
fn write(&mut self, phy: u8, reg: u8, data: u16);
15-
}
16-
1714
#[inline(always)]
18-
fn smi_wait_ready(iar: &MACMIIAR) {
15+
fn miim_wait_ready(iar: &MACMIIAR) {
1916
while iar.read().mb().bit_is_set() {}
2017
}
2118

2219
#[inline(always)]
23-
pub(crate) fn smi_write(iar: &MACMIIAR, dr: &MACMIIDR, phy: u8, reg: u8, data: u16) {
20+
pub(crate) fn miim_write(iar: &MACMIIAR, dr: &MACMIIDR, phy: u8, reg: u8, data: u16) {
2421
dr.write(|w| w.md().bits(data));
2522

2623
iar.modify(|_, w| {
@@ -34,11 +31,11 @@ pub(crate) fn smi_write(iar: &MACMIIAR, dr: &MACMIIDR, phy: u8, reg: u8, data: u
3431
.mb()
3532
.set_bit()
3633
});
37-
smi_wait_ready(iar);
34+
miim_wait_ready(iar);
3835
}
3936

4037
#[inline(always)]
41-
pub(crate) fn smi_read(iar: &MACMIIAR, dr: &MACMIIDR, phy: u8, reg: u8) -> u16 {
38+
pub(crate) fn miim_read(iar: &MACMIIAR, dr: &MACMIIDR, phy: u8, reg: u8) -> u16 {
4239
iar.modify(|_, w| {
4340
w.pa()
4441
.bits(phy)
@@ -50,7 +47,7 @@ pub(crate) fn smi_read(iar: &MACMIIAR, dr: &MACMIIDR, phy: u8, reg: u8) -> u16 {
5047
.mb()
5148
.set_bit()
5249
});
53-
smi_wait_ready(iar);
50+
miim_wait_ready(iar);
5451

5552
// Return value:
5653
dr.read().md().bits()
@@ -60,28 +57,43 @@ pub(crate) fn smi_read(iar: &MACMIIAR, dr: &MACMIIDR, phy: u8, reg: u8) -> u16 {
6057
///
6158
/// Borrows [`MACMIIAR`] and [`MACMIIDR`] from (ETHERNET_MAC)[`crate::stm32::ETHERNET_MAC`], and holds a mutable borrow
6259
/// to the SMI pins.
63-
pub struct Smi<'eth, 'pins, Mdio, Mdc> {
60+
pub struct Stm32Miim<'eth, 'pins, Mdio, Mdc> {
6461
macmiiar: &'eth MACMIIAR,
6562
macmiidr: &'eth MACMIIDR,
6663
_mdio: &'pins mut Mdio,
6764
_mdc: &'pins mut Mdc,
6865
}
6966

70-
impl<'eth, 'pins, Mdio, Mdc> SerialManagement for Smi<'eth, 'pins, Mdio, Mdc>
67+
impl<'eth, 'pins, Mdio, Mdc> Stm32Miim<'eth, 'pins, Mdio, Mdc>
68+
where
69+
Mdio: MdioPin,
70+
Mdc: MdcPin,
71+
{
72+
pub fn read(&mut self, phy: u8, reg: u8) -> u16 {
73+
miim_read(&self.macmiiar, &self.macmiidr, phy, reg)
74+
}
75+
76+
pub fn write(&mut self, phy: u8, reg: u8, data: u16) {
77+
miim_write(&self.macmiiar, &self.macmiidr, phy, reg, data)
78+
}
79+
}
80+
81+
#[cfg(feature = "ieee802_3_miim")]
82+
impl<'eth, 'pins, Mdio, Mdc> Miim for Stm32Miim<'eth, 'pins, Mdio, Mdc>
7183
where
7284
Mdio: MdioPin,
7385
Mdc: MdcPin,
7486
{
75-
fn read(&self, phy: u8, reg: u8) -> u16 {
76-
smi_read(&self.macmiiar, &self.macmiidr, phy, reg)
87+
fn read(&mut self, phy: u8, reg: u8) -> u16 {
88+
self.read(phy, reg)
7789
}
7890

7991
fn write(&mut self, phy: u8, reg: u8, data: u16) {
80-
smi_write(&self.macmiiar, &self.macmiidr, phy, reg, data)
92+
self.write(phy, reg, data)
8193
}
8294
}
8395

84-
impl<'eth, 'pins, Mdio, Mdc> Smi<'eth, 'pins, Mdio, Mdc>
96+
impl<'eth, 'pins, Mdio, Mdc> Stm32Miim<'eth, 'pins, Mdio, Mdc>
8597
where
8698
Mdio: MdioPin,
8799
Mdc: MdcPin,

0 commit comments

Comments
 (0)