aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy/Cargo.toml4
-rw-r--r--embassy/src/fmt.rs3
-rw-r--r--embassy/src/time/instant.rs12
3 files changed, 19 insertions, 0 deletions
diff --git a/embassy/Cargo.toml b/embassy/Cargo.toml
index d10a8874e..9a5467c6f 100644
--- a/embassy/Cargo.toml
+++ b/embassy/Cargo.toml
@@ -17,6 +17,10 @@ nightly = ["embedded-hal-async"]
17# Implement embedded-hal-async traits if `nightly` is set as well. 17# Implement embedded-hal-async traits if `nightly` is set as well.
18unstable-traits = ["embedded-hal-1"] 18unstable-traits = ["embedded-hal-1"]
19 19
20# Display a timestamp of the number of seconds since startup next to defmt log messages
21# To use this you must have a time driver provided.
22defmt-timestamp-uptime = ["defmt"]
23
20# Enable `embassy::time` module. 24# Enable `embassy::time` module.
21# NOTE: This feature is only intended to be enabled by crates providing the time driver implementation. 25# NOTE: This feature is only intended to be enabled by crates providing the time driver implementation.
22# Enabling it directly without supplying a time driver will fail to link. 26# Enabling it directly without supplying a time driver will fail to link.
diff --git a/embassy/src/fmt.rs b/embassy/src/fmt.rs
index 066970813..f8bb0a035 100644
--- a/embassy/src/fmt.rs
+++ b/embassy/src/fmt.rs
@@ -195,6 +195,9 @@ macro_rules! unwrap {
195 } 195 }
196} 196}
197 197
198#[cfg(feature = "defmt-timestamp-uptime")]
199defmt::timestamp! {"{=u64:us}", crate::time::Instant::now().as_micros() }
200
198#[derive(Debug, Copy, Clone, Eq, PartialEq)] 201#[derive(Debug, Copy, Clone, Eq, PartialEq)]
199pub struct NoneError; 202pub struct NoneError;
200 203
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 {