aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Franklin <[email protected]>2022-02-11 19:01:43 -0700
committerDaniel Franklin <[email protected]>2022-02-11 19:01:43 -0700
commit04ac4883196c25990964968b5d52b4125986d1be (patch)
treedbf4279edfadab7bf761b1ff2c0f391c493f5afa
parent4c5f5f71693600d133d3416634169acdf0042ff7 (diff)
Add {from_as}_micros to Instant
-rw-r--r--embassy/src/fmt.rs3
-rw-r--r--embassy/src/time/instant.rs12
2 files changed, 13 insertions, 2 deletions
diff --git a/embassy/src/fmt.rs b/embassy/src/fmt.rs
index 115febb58..f8bb0a035 100644
--- a/embassy/src/fmt.rs
+++ b/embassy/src/fmt.rs
@@ -196,8 +196,7 @@ macro_rules! unwrap {
196} 196}
197 197
198#[cfg(feature = "defmt-timestamp-uptime")] 198#[cfg(feature = "defmt-timestamp-uptime")]
199// defmt offers a disply hint for microseconds so we convert from millis 199defmt::timestamp! {"{=u64:us}", crate::time::Instant::now().as_micros() }
200defmt::timestamp! {"{=u64:us}", crate::time::Instant::now().as_millis() * 1_000 }
201 200
202#[derive(Debug, Copy, Clone, Eq, PartialEq)] 201#[derive(Debug, Copy, Clone, Eq, PartialEq)]
203pub struct NoneError; 202pub struct NoneError;
diff --git a/embassy/src/time/instant.rs b/embassy/src/time/instant.rs
index 36c2b2dc2..aff83be20 100644
--- a/embassy/src/time/instant.rs
+++ b/embassy/src/time/instant.rs
@@ -28,6 +28,13 @@ impl Instant {
28 Self { ticks } 28 Self { ticks }
29 } 29 }
30 30
31 /// Create an Instant from a microsecond count since system boot.
32 pub const fn from_micros(micros: u64) -> Self {
33 Self {
34 ticks: micros * TICKS_PER_SECOND / 1_000_000,
35 }
36 }
37
31 /// Create an Instant from a millisecond count since system boot. 38 /// Create an Instant from a millisecond count since system boot.
32 pub const fn from_millis(millis: u64) -> Self { 39 pub const fn from_millis(millis: u64) -> Self {
33 Self { 40 Self {
@@ -57,6 +64,11 @@ impl Instant {
57 self.ticks * 1000 / TICKS_PER_SECOND 64 self.ticks * 1000 / TICKS_PER_SECOND
58 } 65 }
59 66
67 /// Microseconds since system boot.
68 pub const fn as_micros(&self) -> u64 {
69 self.ticks * 1_000_000 / TICKS_PER_SECOND
70 }
71
60 /// Duration between this Instant and another Instant 72 /// Duration between this Instant and another Instant
61 /// Panics on over/underflow. 73 /// Panics on over/underflow.
62 pub fn duration_since(&self, earlier: Instant) -> Duration { 74 pub fn duration_since(&self, earlier: Instant) -> Duration {