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 <daniel@danielzfranklin.org>
This commit is contained in:
bors[bot] 2022-02-12 02:12:48 +00:00 committed by GitHub
commit b74ccf2d34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

View file

@ -17,6 +17,10 @@ nightly = ["embedded-hal-async"]
# Implement embedded-hal-async traits if `nightly` is set as well. # Implement embedded-hal-async traits if `nightly` is set as well.
unstable-traits = ["embedded-hal-1"] 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. # Enable `embassy::time` module.
# NOTE: This feature is only intended to be enabled by crates providing the time driver implementation. # 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. # Enabling it directly without supplying a time driver will fail to link.

View file

@ -195,6 +195,9 @@ macro_rules! unwrap {
} }
} }
#[cfg(feature = "defmt-timestamp-uptime")]
defmt::timestamp! {"{=u64:us}", crate::time::Instant::now().as_micros() }
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct NoneError; pub struct NoneError;

View file

@ -28,6 +28,13 @@ impl Instant {
Self { ticks } 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. /// Create an Instant from a millisecond count since system boot.
pub const fn from_millis(millis: u64) -> Self { pub const fn from_millis(millis: u64) -> Self {
Self { Self {
@ -57,6 +64,11 @@ impl Instant {
self.ticks * 1000 / TICKS_PER_SECOND 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 /// Duration between this Instant and another Instant
/// Panics on over/underflow. /// Panics on over/underflow.
pub fn duration_since(&self, earlier: Instant) -> Duration { pub fn duration_since(&self, earlier: Instant) -> Duration {