aboutsummaryrefslogtreecommitdiff
path: root/embassy-time/src
diff options
context:
space:
mode:
authorMatteo Meluzzi <[email protected]>2025-10-02 10:53:31 +0200
committerMatteo Meluzzi <[email protected]>2025-10-02 10:53:31 +0200
commit828a8df18d04877df1f55f04354980b28ff2f2f8 (patch)
treec4fa405f5eba7a14b6d435d6cc746c9e0dc52632 /embassy-time/src
parent176649e71ad442ca9856af6c11989b0b2f228c4b (diff)
parent194a721d0eab929a2af0a2a4e45ca8e70e0d3f0a (diff)
Merge branch 'main' into 17-add-support-for-boot-protocol
Diffstat (limited to 'embassy-time/src')
-rw-r--r--embassy-time/src/duration.rs5
-rw-r--r--embassy-time/src/instant.rs25
2 files changed, 29 insertions, 1 deletions
diff --git a/embassy-time/src/duration.rs b/embassy-time/src/duration.rs
index 5b140eeff..b3ea0468d 100644
--- a/embassy-time/src/duration.rs
+++ b/embassy-time/src/duration.rs
@@ -37,6 +37,11 @@ impl Duration {
37 self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M) 37 self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M)
38 } 38 }
39 39
40 /// Convert the `Duration` to nanoseconds, rounding down.
41 pub const fn as_nanos(&self) -> u64 {
42 self.ticks * (1_000_000_000 / GCD_1G) / (TICK_HZ / GCD_1G)
43 }
44
40 /// Creates a duration from the specified number of clock ticks 45 /// Creates a duration from the specified number of clock ticks
41 pub const fn from_ticks(ticks: u64) -> Duration { 46 pub const fn from_ticks(ticks: u64) -> Duration {
42 Duration { ticks } 47 Duration { ticks }
diff --git a/embassy-time/src/instant.rs b/embassy-time/src/instant.rs
index 6571bea62..de5ebebf8 100644
--- a/embassy-time/src/instant.rs
+++ b/embassy-time/src/instant.rs
@@ -1,7 +1,7 @@
1use core::fmt; 1use core::fmt;
2use core::ops::{Add, AddAssign, Sub, SubAssign}; 2use core::ops::{Add, AddAssign, Sub, SubAssign};
3 3
4use super::{Duration, GCD_1K, GCD_1M, TICK_HZ}; 4use super::{Duration, GCD_1G, GCD_1K, GCD_1M, TICK_HZ};
5 5
6#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] 6#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
7#[cfg_attr(feature = "defmt", derive(defmt::Format))] 7#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -29,6 +29,13 @@ impl Instant {
29 Self { ticks } 29 Self { ticks }
30 } 30 }
31 31
32 /// Create an Instant from a nanosecond count since system boot.
33 pub const fn from_nanos(nanos: u64) -> Self {
34 Self {
35 ticks: nanos * (TICK_HZ / GCD_1G) / (1_000_000_000 / GCD_1G),
36 }
37 }
38
32 /// Create an Instant from a microsecond count since system boot. 39 /// Create an Instant from a microsecond count since system boot.
33 pub const fn from_micros(micros: u64) -> Self { 40 pub const fn from_micros(micros: u64) -> Self {
34 Self { 41 Self {
@@ -50,6 +57,17 @@ impl Instant {
50 } 57 }
51 } 58 }
52 59
60 /// Try to create an Instant from a nanosecond count since system boot.
61 /// Fails if the number of nanoseconds is too large.
62 pub const fn try_from_nanos(nanos: u64) -> Option<Self> {
63 let Some(value) = nanos.checked_mul(TICK_HZ / GCD_1G) else {
64 return None;
65 };
66 Some(Self {
67 ticks: value / (1_000_000_000 / GCD_1G),
68 })
69 }
70
53 /// Try to create an Instant from a microsecond count since system boot. 71 /// Try to create an Instant from a microsecond count since system boot.
54 /// Fails if the number of microseconds is too large. 72 /// Fails if the number of microseconds is too large.
55 pub const fn try_from_micros(micros: u64) -> Option<Self> { 73 pub const fn try_from_micros(micros: u64) -> Option<Self> {
@@ -101,6 +119,11 @@ impl Instant {
101 self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M) 119 self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M)
102 } 120 }
103 121
122 /// Nanoseconds since system boot.
123 pub const fn as_nanos(&self) -> u64 {
124 self.ticks * (1_000_000_000 / GCD_1G) / (TICK_HZ / GCD_1G)
125 }
126
104 /// Duration between this Instant and another Instant 127 /// Duration between this Instant and another Instant
105 /// Panics on over/underflow. 128 /// Panics on over/underflow.
106 pub fn duration_since(&self, earlier: Instant) -> Duration { 129 pub fn duration_since(&self, earlier: Instant) -> Duration {