Skip to content

Commit df931cc

Browse files
committed
Add MIIM support to H7
1 parent 0438352 commit df931cc

File tree

8 files changed

+188
-115
lines changed

8 files changed

+188
-115
lines changed

examples/arp.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ use stm32_eth::{
2121
Parts, MTU,
2222
};
2323

24-
#[cfg(feature = "f-series")]
25-
use stm32_eth::mac::{phy::BarePhy, Phy};
26-
2724
pub mod common;
2825

2926
use stm32_eth::dma::{RxDescriptorRing, TxDescriptorRing, TxError};

examples/common.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ mod pins {
377377

378378
pub type Pps = PB5<Output<PushPull>>;
379379

380-
pub type Mdio = ();
381-
pub type Mdc = ();
380+
pub type Mdio = PA2<Alternate<11>>;
381+
pub type Mdc = PC1<Alternate<11>>;
382382

383383
pub fn setup_pins(
384384
gpio: Gpio,
@@ -403,8 +403,8 @@ mod pins {
403403
let tx_d0 = gpiog.pg13.into_input();
404404
let tx_d1 = gpiob.pb13.into_input();
405405

406-
let mdc = ();
407-
let mdio = ();
406+
let mdio = gpioa.pa2.into_alternate();
407+
let mdc = gpioc.pc1.into_alternate();
408408

409409
let pps = gpiob.pb5.into_push_pull_output();
410410

examples/rtic-timestamp.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@ mod app {
8282
defmt::info!("Configuring ethernet");
8383

8484
let Parts { dma, mac, mut ptp } =
85-
stm32_eth::new(ethernet, rx_ring, tx_ring, clocks, pins).unwrap();
85+
stm32_eth::new_with_mii(ethernet, rx_ring, tx_ring, clocks, pins, mdio, mdc).unwrap();
8686

8787
#[cfg(not(feature = "stm32h7xx-hal"))]
8888
ptp.enable_pps(pps);
8989

9090
defmt::info!("Enabling interrupts");
9191
dma.enable_interrupt();
9292

93-
#[cfg(not(feature = "stm32h7xx-hal"))]
9493
match EthernetPhy::from_miim(mac, 0) {
9594
Ok(mut phy) => {
9695
defmt::info!(

src/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,10 @@ pub use smoltcp;
5656
#[cfg(feature = "device-selected")]
5757
use {
5858
dma::{DmaParts, EthernetDMA, RxDescriptorRing, TxDescriptorRing},
59-
mac::{EthernetMAC, MacParts, Speed, WrongClock},
59+
mac::{EthernetMAC, EthernetMACWithMii, MacParts, MdcPin, MdioPin, Speed, WrongClock},
6060
setup::*,
6161
};
6262

63-
#[cfg(all(feature = "device-selected", feature = "f-series"))]
64-
use mac::{EthernetMACWithMii, MdcPin, MdioPin};
65-
6663
#[cfg(all(feature = "device-selected", feature = "ptp"))]
6764
use ptp::EthernetPTP;
6865

@@ -165,7 +162,7 @@ where
165162
/// accessible by the peripheral. Core-Coupled Memory (CCM) is
166163
/// usually not accessible.
167164
/// - HCLK must be at least 25 MHz.
168-
#[cfg(all(feature = "device-selected", feature = "f-series"))]
165+
#[cfg(all(feature = "device-selected"))]
169166
pub fn new_with_mii<'rx, 'tx, REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1, MDIO, MDC>(
170167
parts: PartsIn,
171168
rx_buffer: RxDescriptorRing<'rx>,
@@ -194,10 +191,13 @@ where
194191

195192
let dma_parts = DmaParts {
196193
eth_dma: parts.dma.into(),
194+
#[cfg(feature = "stm32h7xx-hal")]
195+
eth_mtl: parts.mtl,
197196
};
198197

199198
let mac_parts = MacParts {
200199
eth_mac: parts.mac.into(),
200+
#[cfg(feature = "f-series")]
201201
eth_mmc: parts.mmc.into(),
202202
};
203203

@@ -206,7 +206,12 @@ where
206206

207207
// Configure the ethernet PTP
208208
#[cfg(feature = "ptp")]
209-
let ptp = EthernetPTP::new(parts.ptp.into(), clocks, &dma);
209+
let ptp = EthernetPTP::new(
210+
#[cfg(feature = "f-series")]
211+
parts.ptp.into(),
212+
clocks,
213+
&dma,
214+
);
210215

211216
// Configure the ethernet MAC
212217
let mac =

src/mac/miim/f_series_miim.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use crate::peripherals::ETHERNET_MAC;
2+
use crate::stm32::ethernet_mac::MACMIIAR;
3+
4+
#[inline(always)]
5+
fn miim_wait_ready(iar: &MACMIIAR) {
6+
while iar.read().mb().bit_is_set() {}
7+
}
8+
9+
#[inline(always)]
10+
pub(crate) fn miim_write(eth_mac: &mut ETHERNET_MAC, phy: u8, reg: u8, data: u16) {
11+
miim_wait_ready(&eth_mac.macmiiar);
12+
eth_mac.macmiidr.write(|w| w.md().bits(data));
13+
14+
miim_wait_ready(&eth_mac.macmiiar);
15+
16+
eth_mac.macmiiar.modify(|_, w| {
17+
w.pa()
18+
.bits(phy)
19+
.mr()
20+
.bits(reg)
21+
/* Write operation MW=1*/
22+
.mw()
23+
.set_bit()
24+
.mb()
25+
.set_bit()
26+
});
27+
miim_wait_ready(&eth_mac.macmiiar);
28+
}
29+
30+
#[inline(always)]
31+
pub(crate) fn miim_read(eth_mac: &mut ETHERNET_MAC, phy: u8, reg: u8) -> u16 {
32+
miim_wait_ready(&eth_mac.macmiiar);
33+
eth_mac.macmiiar.modify(|_, w| {
34+
w.pa()
35+
.bits(phy)
36+
.mr()
37+
.bits(reg)
38+
/* Read operation MW=0 */
39+
.mw()
40+
.clear_bit()
41+
.mb()
42+
.set_bit()
43+
});
44+
miim_wait_ready(&eth_mac.macmiiar);
45+
46+
// Return value:
47+
eth_mac.macmiidr.read().md().bits()
48+
}
49+
50+
#[cfg(feature = "stm32f4xx-hal")]
51+
mod pin_impls {
52+
use crate::hal::gpio::{gpioa::PA2, gpioc::PC1, Alternate};
53+
54+
const AF11: u8 = 11;
55+
56+
unsafe impl crate::mac::MdioPin for PA2<Alternate<AF11>> {}
57+
unsafe impl crate::mac::MdcPin for PC1<Alternate<AF11>> {}
58+
}
59+
60+
#[cfg(feature = "stm32f7xx-hal")]
61+
mod pin_impls {
62+
use crate::hal::gpio::{gpioa::PA2, gpioc::PC1, Alternate};
63+
64+
const AF11: u8 = 11;
65+
66+
unsafe impl crate::mac::MdioPin for PA2<Alternate<AF11>> {}
67+
unsafe impl crate::mac::MdcPin for PC1<Alternate<AF11>> {}
68+
}
69+
70+
#[cfg(feature = "stm32f1xx-hal")]
71+
mod pin_impls {
72+
use crate::hal::gpio::{gpioa::PA2, gpioc::PC1, Alternate, PushPull};
73+
74+
unsafe impl crate::mac::MdioPin for PA2<Alternate<PushPull>> {}
75+
unsafe impl crate::mac::MdcPin for PC1<Alternate<PushPull>> {}
76+
}

src/mac/miim/h_miim.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use crate::peripherals::ETHERNET_MAC;
2+
use crate::stm32::ethernet_mac::MACMDIOAR;
3+
4+
use super::{MdcPin, MdioPin};
5+
6+
use crate::hal::gpio::{Alternate, PA2, PC1};
7+
8+
#[inline(always)]
9+
fn miim_wait_ready(iar: &MACMDIOAR) {
10+
while iar.read().mb().bit_is_set() {}
11+
}
12+
13+
#[inline(always)]
14+
pub(crate) fn miim_write(eth_mac: &mut ETHERNET_MAC, phy: u8, reg: u8, data: u16) {
15+
miim_wait_ready(&eth_mac.macmdioar);
16+
17+
eth_mac.macmdiodr.write(|w| unsafe { w.md().bits(data) });
18+
19+
miim_wait_ready(&eth_mac.macmdioar);
20+
21+
eth_mac.macmdioar.modify(|_, w| unsafe {
22+
w.pa()
23+
.bits(phy)
24+
.rda()
25+
.bits(reg)
26+
/* Write operation GOC=01*/
27+
.goc()
28+
.variant(0b01)
29+
.mb()
30+
.set_bit()
31+
});
32+
33+
miim_wait_ready(&eth_mac.macmdioar);
34+
}
35+
36+
#[inline(always)]
37+
pub(crate) fn miim_read(eth_mac: &mut ETHERNET_MAC, phy: u8, reg: u8) -> u16 {
38+
miim_wait_ready(&eth_mac.macmdioar);
39+
40+
eth_mac.macmdioar.modify(|_, w| unsafe {
41+
w.pa()
42+
.bits(phy)
43+
.rda()
44+
.bits(reg)
45+
/* Write operation GOC=11*/
46+
.goc()
47+
.variant(0b11)
48+
.mb()
49+
.set_bit()
50+
});
51+
52+
miim_wait_ready(&eth_mac.macmdioar);
53+
54+
// Return value:
55+
eth_mac.macmdiodr.read().md().bits()
56+
}
57+
58+
unsafe impl MdcPin for PC1<Alternate<11>> {}
59+
unsafe impl MdioPin for PA2<Alternate<11>> {}

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

Lines changed: 10 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ pub use ieee802_3_miim::*;
33

44
use core::ops::{Deref, DerefMut};
55

6-
use crate::{peripherals::ETHERNET_MAC, stm32::ethernet_mac::MACMIIAR};
7-
86
use super::EthernetMAC;
97

8+
#[cfg(feature = "f-series")]
9+
mod f_series_miim;
10+
#[cfg(feature = "f-series")]
11+
use f_series_miim::{miim_read, miim_write};
12+
13+
#[cfg(feature = "stm32h7xx-hal")]
14+
mod h_miim;
15+
#[cfg(feature = "stm32h7xx-hal")]
16+
use h_miim::{miim_read, miim_write};
17+
1018
/// MDIO pin types.
1119
///
1220
/// # Safety
@@ -21,52 +29,6 @@ pub unsafe trait MdioPin {}
2129
/// may implement this trait
2230
pub unsafe trait MdcPin {}
2331

24-
#[inline(always)]
25-
fn miim_wait_ready(iar: &MACMIIAR) {
26-
while iar.read().mb().bit_is_set() {}
27-
}
28-
29-
#[inline(always)]
30-
fn miim_write(eth_mac: &mut ETHERNET_MAC, phy: u8, reg: u8, data: u16) {
31-
miim_wait_ready(&eth_mac.macmiiar);
32-
eth_mac.macmiidr.write(|w| w.md().bits(data));
33-
34-
miim_wait_ready(&eth_mac.macmiiar);
35-
36-
eth_mac.macmiiar.modify(|_, w| {
37-
w.pa()
38-
.bits(phy)
39-
.mr()
40-
.bits(reg)
41-
/* Write operation MW=1*/
42-
.mw()
43-
.set_bit()
44-
.mb()
45-
.set_bit()
46-
});
47-
miim_wait_ready(&eth_mac.macmiiar);
48-
}
49-
50-
#[inline(always)]
51-
fn miim_read(eth_mac: &mut ETHERNET_MAC, phy: u8, reg: u8) -> u16 {
52-
miim_wait_ready(&eth_mac.macmiiar);
53-
eth_mac.macmiiar.modify(|_, w| {
54-
w.pa()
55-
.bits(phy)
56-
.mr()
57-
.bits(reg)
58-
/* Read operation MW=0 */
59-
.mw()
60-
.clear_bit()
61-
.mb()
62-
.set_bit()
63-
});
64-
miim_wait_ready(&eth_mac.macmiiar);
65-
66-
// Return value:
67-
eth_mac.macmiidr.read().md().bits()
68-
}
69-
7032
/// Serial Management Interface
7133
///
7234
/// Borrows an [`EthernetMAC`] and holds a mutable borrow to the SMI pins.
@@ -120,34 +82,6 @@ where
12082
}
12183
}
12284

123-
#[cfg(feature = "stm32f4xx-hal")]
124-
mod pin_impls {
125-
use crate::hal::gpio::{gpioa::PA2, gpioc::PC1, Alternate};
126-
127-
const AF11: u8 = 11;
128-
129-
unsafe impl super::MdioPin for PA2<Alternate<AF11>> {}
130-
unsafe impl super::MdcPin for PC1<Alternate<AF11>> {}
131-
}
132-
133-
#[cfg(feature = "stm32f7xx-hal")]
134-
mod pin_impls {
135-
use crate::hal::gpio::{gpioa::PA2, gpioc::PC1, Alternate};
136-
137-
const AF11: u8 = 11;
138-
139-
unsafe impl super::MdioPin for PA2<Alternate<AF11>> {}
140-
unsafe impl super::MdcPin for PC1<Alternate<AF11>> {}
141-
}
142-
143-
#[cfg(feature = "stm32f1xx-hal")]
144-
mod pin_impls {
145-
use crate::hal::gpio::{gpioa::PA2, gpioc::PC1, Alternate, PushPull};
146-
147-
unsafe impl super::MdioPin for PA2<Alternate<PushPull>> {}
148-
unsafe impl super::MdcPin for PC1<Alternate<PushPull>> {}
149-
}
150-
15185
/// Ethernet media access control (MAC) with owned MII
15286
///
15387
/// This version of the struct owns it's MII pins,

0 commit comments

Comments
 (0)