Skip to content

Commit 8572c80

Browse files
committed
Take and return a collection struct instead of individual items
1 parent e6766ed commit 8572c80

File tree

8 files changed

+85
-75
lines changed

8 files changed

+85
-75
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,15 @@ fn main() {
5858
5959
let mut rx_ring: [RxRingEntry; 16] = Default::default();
6060
let mut tx_ring: [TxRingEntry; 8] = Default::default();
61-
let (mut eth_dma, _eth_mac) = stm32_eth::new(
62-
p.ETHERNET_MAC,
63-
p.ETHERNET_MMC,
64-
p.ETHERNET_DMA,
61+
62+
let parts = stm32_eth::PartsIn {
63+
mac: p.ETHERNET_MAC,
64+
mmc: p.ETHERNET_MMC,
65+
dma: p.ETHERNET_DMA,
66+
};
67+
68+
let stm32_eth::Parts { dma: mut eth_dma, mac: _ } = stm32_eth::new(
69+
parts,
6570
&mut rx_ring[..],
6671
&mut tx_ring[..],
6772
clocks,

examples/arp.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use cortex_m::interrupt::Mutex;
1818
use stm32_eth::{
1919
mac::{phy::BarePhy, Phy},
2020
stm32::{interrupt, CorePeripherals, Peripherals, SYST},
21+
Parts,
2122
};
2223

2324
pub mod common;
@@ -45,21 +46,19 @@ fn main() -> ! {
4546
let mut rx_ring: [RxRingEntry; 2] = Default::default();
4647
let mut tx_ring: [TxRingEntry; 2] = Default::default();
4748

48-
let (mut eth_dma, eth_mac) = stm32_eth::new(
49-
ethernet.mac,
50-
ethernet.mmc,
51-
ethernet.dma,
49+
let Parts { mut dma, mac } = stm32_eth::new(
50+
ethernet,
5251
&mut rx_ring[..],
5352
&mut tx_ring[..],
5453
clocks,
5554
eth_pins,
5655
)
5756
.unwrap();
58-
eth_dma.enable_interrupt();
57+
dma.enable_interrupt();
5958

6059
let mut last_link_up = false;
6160

62-
let mut bare_phy = BarePhy::new(eth_mac.with_mii(mdio, mdc), PHY_ADDR, Default::default());
61+
let mut bare_phy = BarePhy::new(mac.with_mii(mdio, mdc), PHY_ADDR, Default::default());
6362

6463
loop {
6564
let link_up = bare_phy.phy_link_up();
@@ -88,7 +87,7 @@ fn main() -> ! {
8887
const TARGET_MAC: [u8; 6] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
8988
const TARGET_IP: [u8; 4] = [0x0A, 0x00, 0x00, 0x02]; // 10.0.0.2
9089

91-
let r = eth_dma.send(SIZE, |buf| {
90+
let r = dma.send(SIZE, |buf| {
9291
buf[0..6].copy_from_slice(&DST_MAC);
9392
buf[6..12].copy_from_slice(&SRC_MAC);
9493
buf[12..14].copy_from_slice(&ETH_TYPE);

examples/common.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,21 @@
66
77
use stm32_eth::{
88
hal::{gpio::GpioExt, rcc::Clocks},
9-
stm32::{ETHERNET_DMA, ETHERNET_MAC, ETHERNET_MMC},
9+
PartsIn,
1010
};
1111

1212
pub use pins::{setup_pins, Gpio};
1313

1414
use fugit::RateExtU32;
1515
use stm32_eth::hal::rcc::RccExt;
1616

17-
pub struct EthernetPeripherals {
18-
pub dma: ETHERNET_DMA,
19-
pub mac: ETHERNET_MAC,
20-
pub mmc: ETHERNET_MMC,
21-
}
22-
2317
/// Setup the clocks and return clocks and a GPIO struct that
2418
/// can be used to set up all of the pins.
2519
///
2620
/// This configures HCLK to be at least 25 MHz, which is the minimum required
2721
/// for ethernet operation to be valid.
28-
pub fn setup_peripherals(p: stm32_eth::stm32::Peripherals) -> (Clocks, Gpio, EthernetPeripherals) {
29-
let ethernet = EthernetPeripherals {
22+
pub fn setup_peripherals(p: stm32_eth::stm32::Peripherals) -> (Clocks, Gpio, PartsIn) {
23+
let ethernet = PartsIn {
3024
dma: p.ETHERNET_DMA,
3125
mac: p.ETHERNET_MAC,
3226
mmc: p.ETHERNET_MMC,

examples/ip.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address};
2222

2323
pub mod common;
2424

25-
use stm32_eth::dma::{RxRingEntry, TxRingEntry};
25+
use stm32_eth::{
26+
dma::{RxRingEntry, TxRingEntry},
27+
Parts,
28+
};
2629

2730
const SRC_MAC: [u8; 6] = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF];
2831

@@ -44,17 +47,15 @@ fn main() -> ! {
4447

4548
let mut rx_ring: [RxRingEntry; 2] = Default::default();
4649
let mut tx_ring: [TxRingEntry; 2] = Default::default();
47-
let (mut eth_dma, _eth_mac) = stm32_eth::new(
48-
ethernet.mac,
49-
ethernet.mmc,
50-
ethernet.dma,
50+
let Parts { mut dma, mac: _ } = stm32_eth::new(
51+
ethernet,
5152
&mut rx_ring[..],
5253
&mut tx_ring[..],
5354
clocks,
5455
eth_pins,
5556
)
5657
.unwrap();
57-
eth_dma.enable_interrupt();
58+
dma.enable_interrupt();
5859

5960
let local_addr = Ipv4Address::new(10, 0, 0, 1);
6061
let ip_addr = IpCidr::new(IpAddress::from(local_addr), 24);
@@ -64,7 +65,7 @@ fn main() -> ! {
6465
let ethernet_addr = EthernetAddress(SRC_MAC);
6566

6667
let mut sockets: [_; 1] = Default::default();
67-
let mut iface = InterfaceBuilder::new(&mut eth_dma, &mut sockets[..])
68+
let mut iface = InterfaceBuilder::new(&mut dma, &mut sockets[..])
6869
.hardware_addr(ethernet_addr.into())
6970
.ip_addrs(&mut ip_addrs[..])
7071
.neighbor_cache(neighbor_cache)

examples/pktgen.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use cortex_m::interrupt::Mutex;
1515
use stm32_eth::{
1616
mac::{phy::BarePhy, Phy},
1717
stm32::{interrupt, CorePeripherals, Peripherals, SYST},
18+
Parts,
1819
};
1920

2021
use stm32_eth::dma::{RxRingEntry, TxError, TxRingEntry};
@@ -43,17 +44,15 @@ fn main() -> ! {
4344

4445
let mut rx_ring: [RxRingEntry; 2] = Default::default();
4546
let mut tx_ring: [TxRingEntry; 2] = Default::default();
46-
let (mut eth_dma, eth_mac) = stm32_eth::new(
47-
ethernet.mac,
48-
ethernet.mmc,
49-
ethernet.dma,
47+
let Parts { mut dma, mac } = stm32_eth::new(
48+
ethernet,
5049
&mut rx_ring[..],
5150
&mut tx_ring[..],
5251
clocks,
5352
eth_pins,
5453
)
5554
.unwrap();
56-
eth_dma.enable_interrupt();
55+
dma.enable_interrupt();
5756

5857
// Main loop
5958
let mut last_stats_time = 0usize;
@@ -63,7 +62,7 @@ fn main() -> ! {
6362
let mut tx_pkts = 0usize;
6463
let mut last_link_up = false;
6564

66-
let mut phy = BarePhy::new(eth_mac.with_mii(mdio, mdc), PHY_ADDR, Default::default());
65+
let mut phy = BarePhy::new(mac.with_mii(mdio, mdc), PHY_ADDR, Default::default());
6766

6867
loop {
6968
let time: usize = cortex_m::interrupt::free(|cs| *TIME.borrow(cs).borrow());
@@ -107,7 +106,7 @@ fn main() -> ! {
107106
// handle rx packet
108107
{
109108
let mut recvd = 0usize;
110-
while let Ok(pkt) = eth_dma.recv_next() {
109+
while let Ok(pkt) = dma.recv_next() {
111110
rx_bytes += pkt.len();
112111
rx_pkts += 1;
113112
pkt.free();
@@ -119,15 +118,15 @@ fn main() -> ! {
119118
}
120119
}
121120
}
122-
if !eth_dma.rx_is_running() {
121+
if !dma.rx_is_running() {
123122
defmt::info!("RX stopped");
124123
}
125124

126125
// fill tx queue
127126
const SIZE: usize = 1500;
128127
if phy.phy_link_up() {
129128
'egress: loop {
130-
let r = eth_dma.send(SIZE, |buf| {
129+
let r = dma.send(SIZE, |buf| {
131130
buf[0..6].copy_from_slice(&DST_MAC);
132131
buf[6..12].copy_from_slice(&SRC_MAC);
133132
buf[12..14].copy_from_slice(&ETH_TYPE);

examples/rtic-echo.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ mod app {
3131
use stm32_eth::{
3232
dma::{EthernetDMA, RxRingEntry, TxRingEntry},
3333
mac::Speed,
34+
Parts,
3435
};
3536

3637
use smoltcp::{
@@ -81,18 +82,8 @@ mod app {
8182

8283
defmt::info!("Configuring ethernet");
8384

84-
let (dma, mac) = stm32_eth::new_with_mii(
85-
ethernet.mac,
86-
ethernet.mmc,
87-
ethernet.dma,
88-
rx_ring,
89-
tx_ring,
90-
clocks,
91-
pins,
92-
mdio,
93-
mdc,
94-
)
95-
.unwrap();
85+
let Parts { dma, mac } =
86+
stm32_eth::new_with_mii(ethernet, rx_ring, tx_ring, clocks, pins, mdio, mdc).unwrap();
9687

9788
let dma = cx.local.dma.write(dma);
9889

src/lib.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ pub use stm32f1xx_hal as hal;
2626
pub use stm32f1xx_hal::pac as stm32;
2727

2828
#[cfg(feature = "device-selected")]
29-
use {
30-
hal::rcc::Clocks,
31-
stm32::{ETHERNET_DMA, ETHERNET_MAC, ETHERNET_MMC},
32-
};
29+
use hal::rcc::Clocks;
3330

3431
#[cfg(feature = "device-selected")]
3532
pub mod dma;
@@ -46,11 +43,11 @@ use mac::{EthernetMAC, EthernetMACWithMii, Speed, WrongClock};
4643
#[cfg(feature = "device-selected")]
4744
pub mod setup;
4845
#[cfg(feature = "device-selected")]
49-
pub use setup::EthPins;
50-
#[cfg(feature = "device-selected")]
5146
use setup::{
5247
AlternateVeryHighSpeed, RmiiCrsDv, RmiiRefClk, RmiiRxD0, RmiiRxD1, RmiiTxD0, RmiiTxD1, RmiiTxEN,
5348
};
49+
#[cfg(feature = "device-selected")]
50+
pub use setup::{EthPins, Parts, PartsIn};
5451

5552
#[cfg(feature = "device-selected")]
5653
mod peripherals;
@@ -81,14 +78,12 @@ compile_error!("No device was selected! Exactly one stm32fxxx feature must be se
8178
/// - HCLK must be at least 25 MHz.
8279
#[cfg(feature = "device-selected")]
8380
pub fn new<'rx, 'tx, REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1>(
84-
eth_mac: ETHERNET_MAC,
85-
eth_mmc: ETHERNET_MMC,
86-
eth_dma: ETHERNET_DMA,
81+
parts: PartsIn,
8782
rx_buffer: &'rx mut [RxRingEntry],
8883
tx_buffer: &'tx mut [TxRingEntry],
8984
clocks: Clocks,
9085
pins: EthPins<REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1>,
91-
) -> Result<(EthernetDMA<'rx, 'tx>, EthernetMAC), WrongClock>
86+
) -> Result<Parts<'rx, 'tx, EthernetMAC>, WrongClock>
9287
where
9388
REFCLK: RmiiRefClk + AlternateVeryHighSpeed,
9489
CRS: RmiiCrsDv + AlternateVeryHighSpeed,
@@ -104,15 +99,18 @@ where
10499
// Set up the clocks and reset the MAC periperhal
105100
setup::setup();
106101

107-
let eth_mac = eth_mac.into();
108-
109102
// Congfigure and start up the ethernet DMA.
110-
let dma = EthernetDMA::new(eth_dma.into(), rx_buffer, tx_buffer);
103+
let dma = EthernetDMA::new(parts.dma.into(), rx_buffer, tx_buffer);
111104

112105
// Configure the ethernet MAC
113-
let mac = EthernetMAC::new(eth_mac, eth_mmc, clocks, Speed::FullDuplexBase100Tx)?;
114-
115-
Ok((dma, mac))
106+
let mac = EthernetMAC::new(
107+
parts.mac.into(),
108+
parts.mmc,
109+
clocks,
110+
Speed::FullDuplexBase100Tx,
111+
)?;
112+
113+
Ok(Parts { dma, mac })
116114
}
117115

118116
/// Create and initialise the ethernet driver.
@@ -137,16 +135,14 @@ where
137135
/// - HCLK must be at least 25 MHz.
138136
#[cfg(feature = "device-selected")]
139137
pub fn new_with_mii<'rx, 'tx, REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1, MDIO, MDC>(
140-
eth_mac: ETHERNET_MAC,
141-
eth_mmc: ETHERNET_MMC,
142-
eth_dma: ETHERNET_DMA,
138+
parts: PartsIn,
143139
rx_buffer: &'rx mut [RxRingEntry],
144140
tx_buffer: &'tx mut [TxRingEntry],
145141
clocks: Clocks,
146142
pins: EthPins<REFCLK, CRS, TXEN, TXD0, TXD1, RXD0, RXD1>,
147143
mdio: MDIO,
148144
mdc: MDC,
149-
) -> Result<(EthernetDMA<'rx, 'tx>, EthernetMACWithMii<MDIO, MDC>), WrongClock>
145+
) -> Result<Parts<'rx, 'tx, EthernetMACWithMii<MDIO, MDC>>, WrongClock>
150146
where
151147
REFCLK: RmiiRefClk + AlternateVeryHighSpeed,
152148
CRS: RmiiCrsDv + AlternateVeryHighSpeed,
@@ -164,16 +160,19 @@ where
164160
// Set up the clocks and reset the MAC periperhal
165161
setup::setup();
166162

167-
let eth_mac = eth_mac.into();
168-
169163
// Congfigure and start up the ethernet DMA.
170-
let dma = EthernetDMA::new(eth_dma.into(), rx_buffer, tx_buffer);
164+
let dma = EthernetDMA::new(parts.dma.into(), rx_buffer, tx_buffer);
171165

172166
// Configure the ethernet MAC
173-
let mac =
174-
EthernetMAC::new(eth_mac, eth_mmc, clocks, Speed::FullDuplexBase100Tx)?.with_mii(mdio, mdc);
175-
176-
Ok((dma, mac))
167+
let mac = EthernetMAC::new(
168+
parts.mac.into(),
169+
parts.mmc,
170+
clocks,
171+
Speed::FullDuplexBase100Tx,
172+
)?
173+
.with_mii(mdio, mdc);
174+
175+
Ok(Parts { dma, mac })
177176
}
178177

179178
/// This block ensures that README.md is checked when `cargo test` is run.

src/setup.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ use stm32f7xx_hal::{
3333
pac::{RCC, SYSCFG},
3434
};
3535

36+
use crate::{
37+
stm32::{ETHERNET_DMA, ETHERNET_MAC, ETHERNET_MMC},
38+
EthernetDMA,
39+
};
40+
3641
// Enable syscfg and ethernet clocks. Reset the Ethernet MAC.
3742
pub(crate) fn setup() {
3843
#[cfg(feature = "stm32f4xx-hal")]
@@ -173,6 +178,23 @@ pub trait AlternateVeryHighSpeed {
173178
fn into_af11_very_high_speed(self);
174179
}
175180

181+
/// A struct that contains all parts required to configure
182+
/// the ethernet peripheral.
183+
#[allow(missing_docs)]
184+
pub struct PartsIn {
185+
pub mac: ETHERNET_MAC,
186+
pub mmc: ETHERNET_MMC,
187+
pub dma: ETHERNET_DMA,
188+
}
189+
190+
/// Access to all configured parts of the ethernet peripheral.
191+
pub struct Parts<'rx, 'tx, T> {
192+
/// Access to and control over the ethernet MAC.
193+
pub mac: T,
194+
/// Access to and control over the ethernet DMA.
195+
pub dma: EthernetDMA<'rx, 'tx>,
196+
}
197+
176198
/// A struct that represents a combination of pins to be used
177199
/// as RMII pins for the ethernet peripheral(s)
178200
// NOTE(missing_docs): all fields of this struct are self-explanatory

0 commit comments

Comments
 (0)