Skip to content

Commit e97cf5d

Browse files
datdenkiknietJohannes Draaijer
authored andcommitted
Make EthernetMAC::new and EthernetDMA::new more safe
1 parent eac500a commit e97cf5d

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* General
33
* Remove the `smi` feature and always enable miim/smi. Use `ieee802_3_miim` for SMI access
44
* Split MAC and DMA setup into their own separate modules
5+
* CI
6+
* Test compilability of examples more extensively
57
* Examples:
68
* Switch to `defmt` as logger
79
* Use `probe-run` as runner

src/dma.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use cortex_m::peripheral::NVIC;
22

33
use crate::{
44
rx::{RxPacket, RxRing},
5-
stm32::{Interrupt, ETHERNET_DMA},
5+
stm32::{Interrupt, ETHERNET_DMA, ETHERNET_MAC},
66
tx::TxRing,
77
RxError, RxRingEntry, TxError, TxRingEntry,
88
};
@@ -23,6 +23,11 @@ impl<'rx, 'tx> EthernetDMA<'rx, 'tx> {
2323
/// usually not accessible.
2424
pub(crate) fn new(
2525
eth_dma: ETHERNET_DMA,
26+
// Take a reference to ETHERNET_MAC to ensure that
27+
// this function cannot be called before `EthernetMAC::new`.
28+
// If we do that, shenanigans ensues (presumably clock and wait
29+
// condition mismatch)
30+
#[allow(unused_variables)] eth_mac: &ETHERNET_MAC,
2631
rx_buffer: &'rx mut [RxRingEntry],
2732
tx_buffer: &'tx mut [TxRingEntry],
2833
) -> Self {

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ where
124124
// Note: this _must_ happen before configuring the MAC.
125125
// It's not entirely clear why, but no interrupts are
126126
// generated if the order is reversed.
127-
let dma = EthernetDMA::new(eth_dma, rx_buffer, tx_buffer);
127+
let dma = EthernetDMA::new(eth_dma, &eth_mac, rx_buffer, tx_buffer);
128128

129129
// Configure the ethernet MAC
130-
let mac = EthernetMAC::new(eth_mac, eth_mmc, clocks)?;
130+
let mac = EthernetMAC::new(eth_mac, eth_mmc, &dma, clocks)?;
131131

132132
Ok((dma, mac))
133133
}
@@ -182,10 +182,10 @@ where
182182
// Note: this _must_ happen before configuring the MAC.
183183
// It's not entirely clear why, but no interrupts are
184184
// generated if the order is reversed.
185-
let dma = EthernetDMA::new(eth_dma, rx_buffer, tx_buffer);
185+
let dma = EthernetDMA::new(eth_dma, &eth_mac, rx_buffer, tx_buffer);
186186

187187
// Configure the ethernet MAC
188-
let mac = EthernetMAC::new(eth_mac, eth_mmc, clocks)?.with_mii(mdio, mdc);
188+
let mac = EthernetMAC::new(eth_mac, eth_mmc, &dma, clocks)?.with_mii(mdio, mdc);
189189

190190
Ok((dma, mac))
191191
}

src/mac/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use core::ops::Deref;
55
use crate::{
66
hal::rcc::Clocks,
77
stm32::{ETHERNET_MAC, ETHERNET_MMC},
8+
EthernetDMA,
89
};
910

1011
mod miim;
@@ -48,6 +49,10 @@ impl EthernetMAC {
4849
pub(crate) fn new(
4950
eth_mac: ETHERNET_MAC,
5051
eth_mmc: ETHERNET_MMC,
52+
// Take a reference to EthernetDMA to ensure
53+
// that `EthernetDMA` has been called before
54+
// this function.
55+
#[allow(unused)] eth_dma: &EthernetDMA,
5156
clocks: Clocks,
5257
) -> Result<Self, WrongClock> {
5358
let clock_frequency = clocks.hclk().to_Hz();

0 commit comments

Comments
 (0)