Skip to content

Commit

Permalink
Now with RX timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
datdenkikniet committed Feb 4, 2023
1 parent 886c6ba commit accc603
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
25 changes: 17 additions & 8 deletions src/dma/rx/h_desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ mod consts {
pub const RXDESC_3_BUF1V: u32 = 1 << 24;

// Write-back bits
/// Timestamp Dropped
pub const RXDESC_1_TD: u32 = 1 << 16;
/// Timestamp Avaialble
pub const RXDESC_1_TSA: u32 = 1 << 14;
/// Context Descriptor
pub const RXDESC_3_CTXT: u32 = 1 << 30;
/// First Descriptor
Expand Down Expand Up @@ -102,7 +106,7 @@ impl RxDescriptor {
}

/// Is owned by the DMA engine?
fn is_owned(&self) -> bool {
pub(super) fn is_owned(&self) -> bool {
(self.inner_raw.read(3) & RXDESC_3_OWN) == RXDESC_3_OWN
}

Expand All @@ -122,6 +126,10 @@ impl RxDescriptor {
self.inner_raw.read(3) & RXDESC_3_CTXT == RXDESC_3_CTXT
}

pub(super) fn has_timestamp(&self) -> bool {
(self.inner_raw.read(1) & RXDESC_1_TSA) == RXDESC_1_TSA && self.is_last()
}

pub(super) fn frame_length(&self) -> usize {
if self.is_owned() {
0
Expand Down Expand Up @@ -200,10 +208,6 @@ impl RxDescriptor {

self.packet_id = packet_id;

// Cache the PTP timestamps if PTP is enabled.
#[cfg(feature = "ptp")]
self.attach_timestamp();

Ok(())
} else {
self.set_owned(buffer);
Expand All @@ -216,11 +220,16 @@ impl RxDescriptor {
impl RxDescriptor {
/// Get PTP timestamps if available
pub(super) fn read_timestamp(&self) -> Option<Timestamp> {
todo!();
if self.is_context() && !self.is_owned() {
let (high, low) = (self.inner_raw.read(1), self.inner_raw.read(0));
Some(Timestamp::from_parts(high, low))
} else {
None
}
}

fn attach_timestamp(&mut self) {
self.cached_timestamp = self.read_timestamp();
pub(super) fn attach_timestamp(&mut self, timestamp: Option<Timestamp>) {
self.cached_timestamp = timestamp;
}

pub(super) fn timestamp(&self) -> Option<&Timestamp> {
Expand Down
41 changes: 39 additions & 2 deletions src/dma/rx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,54 @@ impl<'data> RxRing<'data, Running> {
self.demand_poll(eth_dma);
}

let entry = self.next_entry;
let entries_len = self.ring.len();
let (descriptor, buffer) = self.ring.get(self.next_entry);
let (descriptor, buffer) = self.ring.get(entry);

let mut res = descriptor.take_received(packet_id, buffer);

if res.as_mut().err() != Some(&mut RxError::WouldBlock) {
self.next_entry = (self.next_entry + 1) % entries_len;
}

#[cfg(all(feature = "ptp", feature = "stm32h7xx-hal"))]
let (timestamp, descriptor, buffer) = {
if res.as_mut().err() != Some(&mut RxError::WouldBlock) {
let desc_has_timestamp = descriptor.has_timestamp();

drop(descriptor);
drop(buffer);

// On H7's, the timestamp is stored in the next Context
// descriptor.
let timestamp = if desc_has_timestamp {
let (ctx_descriptor, ctx_des_buffer) = self.ring.get(self.next_entry);
if let Some(timestamp) = ctx_descriptor.read_timestamp() {
ctx_descriptor.set_owned(ctx_des_buffer);
// Advance over this buffer
self.next_entry = (self.next_entry + 1) % entries_len;
Some(timestamp)
} else {
None
}
} else {
None
};

let (descriptor, buffer) = self.ring.get(entry);

descriptor.attach_timestamp(timestamp);

(timestamp, descriptor, buffer)
} else {
let (descriptor, buffer) = self.ring.get(entry);
descriptor.attach_timestamp(None);
(None, descriptor, buffer)
}
};

res.map(move |_| {
#[cfg(feature = "ptp")]
#[cfg(all(feature = "ptp", feature = "f-series"))]
let timestamp = descriptor.read_timestamp();

RxPacket {
Expand Down
3 changes: 3 additions & 0 deletions src/ptp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ impl EthernetPTP {
// Enable all timestamps
.tsena()
.set_bit()
// Tell MAC to overwrite non-read timestamps
.txtsstsm()
.set_bit()
});

// Set up the subsecond increment
Expand Down

0 comments on commit accc603

Please sign in to comment.