Skip to content

Commit 95f7347

Browse files
committed
Add total_nanos function
1 parent a81144b commit 95f7347

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/ptp/timestamp.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::dma::desc::Descriptor;
22

3-
use super::Subseconds;
3+
use super::{Subseconds, NANOS_PER_SECOND};
44

55
/// A timestamp produced by the PTP periperhal
66
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -89,6 +89,28 @@ impl Timestamp {
8989
self.subseconds().nanos()
9090
}
9191

92+
/// Get the total amount of nanoseconds in this [`Timestamp`].
93+
///
94+
/// Example:
95+
/// ```rust
96+
/// # use stm32_eth::ptp::{Subseconds, Timestamp};
97+
/// let timestamp = Timestamp::new(false, 500, Subseconds::new_from_nanos(500_000).unwrap());
98+
/// assert_eq!(timestamp.total_nanos(), 500 * 1_000_000_000 + 500_000);
99+
///
100+
///
101+
/// let timestamp_neg = Timestamp::new(true, 500, Subseconds::new_from_nanos(500_000).unwrap());
102+
/// assert_eq!(timestamp_neg.total_nanos(), -1 * (500 * 1_000_000_000 + 500_000));
103+
/// ```
104+
pub const fn total_nanos(&self) -> i64 {
105+
let nanos = self.seconds() as i64 * NANOS_PER_SECOND as i64 + self.nanos() as i64;
106+
107+
if self.is_positive() {
108+
nanos
109+
} else {
110+
-nanos
111+
}
112+
}
113+
92114
/// Create a new timestamp from the provided register values.
93115
pub const fn from_parts(high: u32, low: u32) -> Timestamp {
94116
let negative = (low & Self::SIGN_BIT) == Self::SIGN_BIT;

0 commit comments

Comments
 (0)