Skip to content

Commit abc1a7f

Browse files
authored
Merge pull request #45 from datdenkikniet/more_smi
Add `ieee802_3_miim` crate for MIIM support, and allow MAC to borrow or own Mii
2 parents ad93d15 + 9f4974d commit abc1a7f

File tree

10 files changed

+685
-510
lines changed

10 files changed

+685
-510
lines changed

.github/workflows/build.yml

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ jobs:
4343
toolchain:
4444
- stable
4545
features:
46-
- stm32f745,smi
46+
- stm32f745
4747
runs-on: ubuntu-20.04
4848
steps:
4949
- name: Checkout
@@ -74,25 +74,25 @@ jobs:
7474
toolchain:
7575
- stable
7676
features:
77-
- stm32f107,smi
78-
- stm32f407,smi
79-
- stm32f417,smi
80-
- stm32f427,smi
81-
- stm32f429,smi
82-
- stm32f437,smi
83-
- stm32f439,smi
84-
- stm32f469,smi
85-
- stm32f479,smi
86-
- stm32f429,smi
87-
- stm32f745,smi
88-
- stm32f746,smi
89-
- stm32f756,smi
90-
- stm32f765,smi
91-
- stm32f767,smi
92-
- stm32f769,smi
93-
- stm32f777,smi
94-
- stm32f778,smi
95-
- stm32f779,smi
77+
- stm32f107
78+
- stm32f407
79+
- stm32f417
80+
- stm32f427
81+
- stm32f429
82+
- stm32f437
83+
- stm32f439
84+
- stm32f469
85+
- stm32f479
86+
- stm32f429
87+
- stm32f745
88+
- stm32f746
89+
- stm32f756
90+
- stm32f765
91+
- stm32f767
92+
- stm32f769
93+
- stm32f777
94+
- stm32f778
95+
- stm32f779
9696
steps:
9797
- name: Checkout
9898
uses: actions/checkout@v3
@@ -137,7 +137,7 @@ jobs:
137137
with:
138138
use-cross: false
139139
command: build
140-
args: --target=${{ matrix.target }} --example pktgen --features stm32f429,smi
140+
args: --target=${{ matrix.target }} --example pktgen --features stm32f429
141141

142142
- name: cargo build f4 example ip
143143
uses: actions-rs/cargo@v1
@@ -151,14 +151,14 @@ jobs:
151151
with:
152152
use-cross: false
153153
command: build
154-
args: --target=${{ matrix.target }} --example arp --features stm32f407,smi
154+
args: --target=${{ matrix.target }} --example arp --features stm32f407
155155

156156
- name: cargo build f4 example arp-smoltcp
157157
uses: actions-rs/cargo@v1
158158
with:
159159
use-cross: false
160160
command: build
161-
args: --target=${{ matrix.target }} --example arp-smoltcp --features stm32f407,smi,smoltcp-phy,smoltcp/socket-icmp
161+
args: --target=${{ matrix.target }} --example arp-smoltcp --features stm32f407,smoltcp-phy,smoltcp/socket-icmp
162162

163163
- name: cargo build f1 example ip
164164
uses: actions-rs/cargo@v1

Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ travis-ci = { repository = "astro/stm32-eth", branch = "master" }
1515
maintenance = { status = "experimental" }
1616

1717
[package.metadata.docs.rs]
18-
features = ["smi", "smoltcp-phy", "stm32f429", "smoltcp/socket-tcp"]
18+
features = ["smoltcp-phy", "smoltcp/socket-raw", "stm32f429"]
1919

2020
[dependencies]
2121
volatile-register = "0.2"
2222
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.7", optional = true }
2627
cortex-m = "0.7"
2728
log = { version = "0.4", optional = true }
2829

@@ -33,9 +34,9 @@ features = ["medium-ethernet", "proto-ipv4"]
3334
optional = true
3435

3536
[features]
37+
default = [ "ieee802_3_miim" ]
3638
device-selected = []
3739
fence = []
38-
smi = []
3940

4041
stm32f107 = ["stm32f1xx-hal/stm32f107", "device-selected"]
4142

@@ -69,7 +70,7 @@ fugit = "0.3"
6970

7071
[[example]]
7172
name = "pktgen"
72-
required-features = ["smi", "stm32f429"]
73+
required-features = ["stm32f429"]
7374

7475
[[example]]
7576
name = "ip"
@@ -87,11 +88,11 @@ required-features = [
8788

8889
[[example]]
8990
name = "arp"
90-
required-features = ["stm32f407", "smi"]
91+
required-features = ["stm32f407"]
9192

9293
[[example]]
9394
name = "arp-smoltcp"
94-
required-features = ["stm32f407", "smi", "smoltcp-phy", "smoltcp/socket-icmp"]
95+
required-features = ["stm32f407", "smoltcp-phy", "smoltcp/socket-icmp"]
9596

9697
[profile.release]
9798
debug = 2

examples/arp-smoltcp.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// cargo build --example arp-smoltcp --features=stm32f407,smi,smoltcp-phy,smoltcp/socket-tcp,smoltcp/socket-icmp
1+
// cargo build --example arp-smoltcp --features=stm32f407,smoltcp-phy,smoltcp/socket-tcp,smoltcp/socket-icmp
22
// This example uses the STM32F407 and the KSZ8051R as PHY. If necessary the pins,
33
// the PHY register addresses and masks have to be adapted, as well as the IPs.
44
// With Wireshark, you can see the ARP packets, which should look like this:
@@ -25,14 +25,11 @@ use smoltcp::wire::{
2525
use stm32_eth::{
2626
hal::gpio::{GpioExt, Speed},
2727
hal::rcc::RccExt,
28-
smi,
28+
mac::{phy::BarePhy, Phy},
2929
stm32::{interrupt, CorePeripherals, Peripherals, SYST},
3030
};
3131
use stm32_eth::{EthPins, RingEntry, TxError};
3232

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

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

7673
let mut rx_ring: [RingEntry<_>; 16] = Default::default();
7774
let mut tx_ring: [RingEntry<_>; 8] = Default::default();
78-
let (mut eth_dma, mut eth_mac) = stm32_eth::new(
75+
let (mut eth_dma, eth_mac) = stm32_eth::new(
7976
p.ETHERNET_MAC,
8077
p.ETHERNET_MMC,
8178
p.ETHERNET_DMA,
@@ -89,8 +86,10 @@ fn main() -> ! {
8986

9087
let mut last_link_up = false;
9188

89+
let mut bare_phy = BarePhy::new(eth_mac.with_mii(mdio, mdc), PHY_ADDR, Default::default());
90+
9291
loop {
93-
let link_up = link_detected(eth_mac.smi(&mut mdio, &mut mdc));
92+
let link_up = bare_phy.phy_link_up();
9493

9594
if link_up != last_link_up {
9695
if link_up {
@@ -179,12 +178,3 @@ fn ETH() {
179178
let p = unsafe { Peripherals::steal() };
180179
stm32_eth::eth_interrupt_handler(&p.ETHERNET_DMA);
181180
}
182-
183-
fn link_detected<Mdio, Mdc>(smi: smi::Smi<Mdio, Mdc>) -> bool
184-
where
185-
Mdio: smi::MdioPin,
186-
Mdc: smi::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: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// cargo build --example arp --features=stm32f407,smi
1+
// cargo build --example arp --features=stm32f407
22
// This example uses the STM32F407 and the KSZ8051R as PHY. If necessary the pins,
33
// the PHY register addresses and masks have to be adapted, as well as the IPs.
44
// With Wireshark, you can see the ARP packets, which should look like this:
@@ -20,17 +20,14 @@ use fugit::RateExtU32;
2020
use stm32_eth::{
2121
hal::gpio::{GpioExt, Speed},
2222
hal::rcc::RccExt,
23-
smi,
23+
mac::{phy::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_mii(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: smi::Smi<Mdio, Mdc>) -> bool
182-
where
183-
Mdio: smi::MdioPin,
184-
Mdc: smi::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-
smi,
20+
mac::{phy::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_mii(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: smi::Smi<Mdio, Mdc>) -> bool
218-
where
219-
Mdio: smi::MdioPin,
220-
Mdc: smi::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-
}

0 commit comments

Comments
 (0)