Skip to content

Commit f322f11

Browse files
committed
MAC module
1 parent 2e4d90f commit f322f11

File tree

6 files changed

+493
-328
lines changed

6 files changed

+493
-328
lines changed

src/lib.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub use smoltcp;
5555
#[cfg(feature = "device-selected")]
5656
use {
5757
dma::{EthernetDMA, RxRingEntry, TxRingEntry},
58-
mac::{EthernetMAC, EthernetMACWithMii, MdcPin, MdioPin, Speed, WrongClock},
58+
mac::{EthernetMAC, EthernetMACWithMii, MacParts, MdcPin, MdioPin, Speed, WrongClock},
5959
setup::*,
6060
};
6161

@@ -140,8 +140,6 @@ where
140140
// Set up the clocks and reset the MAC periperhal
141141
setup::setup();
142142

143-
let eth_mac = parts.mac.into();
144-
145143
// Congfigure and start up the ethernet DMA.
146144
let dma = EthernetDMA::new(parts.dma.into(), rx_buffer, tx_buffer);
147145

@@ -154,8 +152,14 @@ where
154152
&dma,
155153
);
156154

155+
let mac_parts = MacParts {
156+
eth_mac: parts.mac.into(),
157+
#[cfg(feature = "f-series")]
158+
eth_mmc: parts.mmc.into(),
159+
};
160+
157161
// Configure the ethernet MAC
158-
let mac = EthernetMAC::new(eth_mac, parts.mmc, clocks, Speed::FullDuplexBase100Tx, &dma)?;
162+
let mac = EthernetMAC::new(mac_parts, clocks, Speed::FullDuplexBase100Tx, &dma)?;
159163

160164
let parts = Parts {
161165
mac,
@@ -214,8 +218,6 @@ where
214218
// Set up the clocks and reset the MAC periperhal
215219
setup::setup();
216220

217-
let eth_mac = parts.mac.into();
218-
219221
// Congfigure and start up the ethernet DMA.
220222
let dma = EthernetDMA::new(parts.dma.into(), rx_buffer, tx_buffer);
221223

@@ -228,9 +230,15 @@ where
228230
&dma,
229231
);
230232

233+
let mac_parts = MacParts {
234+
eth_mac: parts.mac.into(),
235+
#[cfg(feature = "f-series")]
236+
eth_mmc: parts.mmc.into(),
237+
};
238+
231239
// Configure the ethernet MAC
232-
let mac = EthernetMAC::new(eth_mac, parts.mmc, clocks, Speed::FullDuplexBase100Tx, &dma)?
233-
.with_mii(mdio, mdc);
240+
let mac =
241+
EthernetMAC::new(mac_parts, clocks, Speed::FullDuplexBase100Tx, &dma)?.with_mii(mdio, mdc);
234242

235243
let parts = Parts {
236244
mac,

src/mac/miim.rs

Lines changed: 0 additions & 148 deletions
This file was deleted.

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>> {}

0 commit comments

Comments
 (0)