From aa9379463207a02ee7ca4e597054a447b9a90232 Mon Sep 17 00:00:00 2001 From: Astro Date: Sat, 25 Apr 2020 03:01:19 +0200 Subject: [PATCH] libboard_zynq: add GlobalTimer::get_us(), use in libsupport_zynq::logger --- experiments/src/main.rs | 6 +++++- libboard_zynq/src/timer/global.rs | 8 ++++++++ libsupport_zynq/src/logger.rs | 7 ++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/experiments/src/main.rs b/experiments/src/main.rs index 65b7848..ee22cae 100644 --- a/experiments/src/main.rs +++ b/experiments/src/main.rs @@ -268,7 +268,11 @@ pub fn main_core0() { task::spawn(async move { loop { delay(&mut countdown, Milliseconds(1000)).await; - println!("time: {} ms", timer.get_time().0); + + let timestamp = timer.get_us(); + let seconds = timestamp / 1_000_000; + let micros = timestamp % 1_000_000; + println!("time: {:6}.{:06}s", seconds, micros); } }); diff --git a/libboard_zynq/src/timer/global.rs b/libboard_zynq/src/timer/global.rs index 633a7eb..12eb27d 100644 --- a/libboard_zynq/src/timer/global.rs +++ b/libboard_zynq/src/timer/global.rs @@ -78,6 +78,14 @@ impl GlobalTimer { Milliseconds(self.get_counter() * (prescaler + 1) / (clocks.cpu_3x2x() as u64 / 1000)) } + /// read with high precision + pub fn get_us(&self) -> u64 { + let prescaler = self.regs.global_timer_control.read().prescaler() as u64; + let clocks = Clocks::get(); + + 1_000_000 * self.get_counter() * (prescaler + 1) / clocks.cpu_3x2x() as u64 + } + /// return a handle that has implements /// `embedded_hal::timer::CountDown` pub fn countdown(&self) -> CountDown { diff --git a/libsupport_zynq/src/logger.rs b/libsupport_zynq/src/logger.rs index b663b48..48cd92d 100644 --- a/libsupport_zynq/src/logger.rs +++ b/libsupport_zynq/src/logger.rs @@ -1,6 +1,6 @@ //! A logger for the `log` crate -use libboard_zynq::{println, stdio}; +use libboard_zynq::{println, stdio, timer::GlobalTimer}; pub static LOGGER: Logger = Logger; @@ -17,8 +17,9 @@ impl log::Log for Logger { fn log(&self, record: &log::Record) { if self.enabled(record.metadata()) { - // TODO: let timestamp = clock::get_us(); - let timestamp = 0; + let timestamp = unsafe { + GlobalTimer::get() + }.get_us(); let seconds = timestamp / 1_000_000; let micros = timestamp % 1_000_000;