From 4c5f5f71693600d133d3416634169acdf0042ff7 Mon Sep 17 00:00:00 2001 From: Daniel Franklin Date: Fri, 11 Feb 2022 18:45:23 -0700 Subject: [PATCH 1/2] Add feature defmt-timestamp-uptime Enabling it adds a timestamp of the number of seconds since startup next to defmt log messages using `Instant::now`. --- embassy/Cargo.toml | 4 ++++ embassy/src/fmt.rs | 4 ++++ 2 files changed, 8 insertions(+) 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"] # Implement embedded-hal-async traits if `nightly` is set as well. unstable-traits = ["embedded-hal-1"] +# Display a timestamp of the number of seconds since startup next to defmt log messages +# To use this you must have a time driver provided. +defmt-timestamp-uptime = ["defmt"] + # Enable `embassy::time` module. # NOTE: This feature is only intended to be enabled by crates providing the time driver implementation. # 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..115febb58 100644 --- a/embassy/src/fmt.rs +++ b/embassy/src/fmt.rs @@ -195,6 +195,10 @@ macro_rules! unwrap { } } +#[cfg(feature = "defmt-timestamp-uptime")] +// defmt offers a disply hint for microseconds so we convert from millis +defmt::timestamp! {"{=u64:us}", crate::time::Instant::now().as_millis() * 1_000 } + #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub struct NoneError; From 04ac4883196c25990964968b5d52b4125986d1be Mon Sep 17 00:00:00 2001 From: Daniel Franklin Date: Fri, 11 Feb 2022 19:01:43 -0700 Subject: [PATCH 2/2] Add {from_as}_micros to Instant --- embassy/src/fmt.rs | 3 +-- embassy/src/time/instant.rs | 12 ++++++++++++ 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 { } #[cfg(feature = "defmt-timestamp-uptime")] -// defmt offers a disply hint for microseconds so we convert from millis -defmt::timestamp! {"{=u64:us}", crate::time::Instant::now().as_millis() * 1_000 } +defmt::timestamp! {"{=u64:us}", crate::time::Instant::now().as_micros() } #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub 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 { Self { ticks } } + /// Create an Instant from a microsecond count since system boot. + pub const fn from_micros(micros: u64) -> Self { + Self { + ticks: micros * TICKS_PER_SECOND / 1_000_000, + } + } + /// Create an Instant from a millisecond count since system boot. pub const fn from_millis(millis: u64) -> Self { Self { @@ -57,6 +64,11 @@ impl Instant { self.ticks * 1000 / TICKS_PER_SECOND } + /// Microseconds since system boot. + pub const fn as_micros(&self) -> u64 { + self.ticks * 1_000_000 / TICKS_PER_SECOND + } + /// Duration between this Instant and another Instant /// Panics on over/underflow. pub fn duration_since(&self, earlier: Instant) -> Duration {