aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-02-12 02:12:48 +0000
committerGitHub <[email protected]>2022-02-12 02:12:48 +0000
commitb74ccf2d348e9bba1e201e196381f772197e3257 (patch)
tree1156c3097dc5dfd21de2fc939d686f8e90cf2aa6
parentbaadd9b2346323aac901c36cfca7bd6482140262 (diff)
parent04ac4883196c25990964968b5d52b4125986d1be (diff)
Merge #617
617: Add feature defmt-timestamp-uptime r=Dirbaio a=danielzfranklin Add the feature defmt-timestamp-uptime. Enabling it adds a timestamp of the number of seconds since startup next to defmt log messages using `Instant::now`. Co-authored-by: Daniel Franklin <[email protected]>
-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 {