Skip to content

Commit 374a899

Browse files
committed
Make packetcounter standalone
1 parent ed70ab3 commit 374a899

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/dma/mod.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub struct EthernetDMA<'rx, 'tx> {
4747
pub(crate) tx_ring: TxRing<'tx>,
4848

4949
#[cfg(feature = "ptp")]
50-
packet_id_counter: u32,
50+
packet_id_counter: PacketIdCounter,
5151
}
5252

5353
impl<'rx, 'tx> EthernetDMA<'rx, 'tx> {
@@ -125,7 +125,7 @@ impl<'rx, 'tx> EthernetDMA<'rx, 'tx> {
125125
tx_ring: TxRing::new(tx_buffer),
126126

127127
#[cfg(feature = "ptp")]
128-
packet_id_counter: 0,
128+
packet_id_counter: PacketIdCounter(0),
129129
};
130130

131131
dma.rx_ring.start(&dma.eth_dma);
@@ -375,9 +375,7 @@ impl EthernetDMA<'_, '_> {
375375

376376
/// Get the next packet ID.
377377
pub fn next_packet_id(&mut self) -> PacketId {
378-
let id = PacketId(self.packet_id_counter);
379-
self.packet_id_counter = self.packet_id_counter.wrapping_add(1);
380-
id
378+
self.packet_id_counter.next()
381379
}
382380
}
383381

@@ -393,3 +391,15 @@ pub struct InterruptReasonSummary {
393391
/// The interrupt was caused by an error event.
394392
pub is_error: bool,
395393
}
394+
395+
#[cfg(feature = "ptp")]
396+
struct PacketIdCounter(u32);
397+
398+
#[cfg(feature = "ptp")]
399+
impl PacketIdCounter {
400+
pub fn next(&mut self) -> PacketId {
401+
let id = PacketId(self.0);
402+
self.0 = self.0.wrapping_add(1);
403+
id
404+
}
405+
}

src/dma/smoltcp_phy.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ impl<'a, 'rx, 'tx> Device for &'a mut EthernetDMA<'rx, 'tx> {
2828
}
2929

3030
fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
31-
if self.tx_available() && self.rx_available() {
31+
let EthernetDMA {
32+
rx_ring,
33+
tx_ring,
3234
#[cfg(feature = "ptp")]
33-
let rx_packet_id = self.next_packet_id();
35+
packet_id_counter,
36+
..
37+
} = self;
3438

35-
let EthernetDMA {
36-
rx_ring, tx_ring, ..
37-
} = self;
39+
if tx_ring.next_entry_available() && rx_ring.next_entry_available() {
40+
#[cfg(feature = "ptp")]
41+
let rx_packet_id = packet_id_counter.next();
3842

3943
let rx = EthRxToken {
4044
rx_ring,

0 commit comments

Comments
 (0)