Compare commits

...

2 Commits

4 changed files with 35 additions and 10 deletions

View File

@ -72,7 +72,7 @@ pub fn main_core0() {
} }
let mut flash = flash.stop(); let mut flash = flash.stop();
let timer = libboard_zynq::timer::GlobalTimer::new(); let timer = libboard_zynq::timer::GlobalTimer::start();
let mut ddr = zynq::ddr::DdrRam::new(); let mut ddr = zynq::ddr::DdrRam::new();
#[cfg(not(feature = "target_zc706"))] #[cfg(not(feature = "target_zc706"))]
@ -268,7 +268,11 @@ pub fn main_core0() {
task::spawn(async move { task::spawn(async move {
loop { loop {
delay(&mut countdown, Milliseconds(1000)).await; 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);
} }
}); });

View File

@ -92,7 +92,7 @@ register!(value_register, ValueRegister, RW, u32);
register_bits!(value_register, value, u32, 0, 31); register_bits!(value_register, value, u32, 0, 31);
register!(global_timer_control, GlobalTimerControl, RW, u32); register!(global_timer_control, GlobalTimerControl, RW, u32);
register_bits!(global_timer_control, prescaler, u16, 8, 15); register_bits!(global_timer_control, prescaler, u8, 8, 15);
register_bit!(global_timer_control, auto_increment_mode, 3); register_bit!(global_timer_control, auto_increment_mode, 3);
register_bit!(global_timer_control, irq_enable, 2); register_bit!(global_timer_control, irq_enable, 2);
register_bit!(global_timer_control, comp_enablea, 1); register_bit!(global_timer_control, comp_enablea, 1);

View File

@ -13,7 +13,14 @@ pub struct GlobalTimer {
} }
impl GlobalTimer { impl GlobalTimer {
pub fn new() -> GlobalTimer { /// Get the potentially uninitialized timer
pub unsafe fn get() -> GlobalTimer {
let mut regs = mpcore::RegisterBlock::new();
GlobalTimer { regs }
}
/// Get the timer with a reset
pub fn start() -> GlobalTimer {
let mut regs = mpcore::RegisterBlock::new(); let mut regs = mpcore::RegisterBlock::new();
Self::reset(&mut regs); Self::reset(&mut regs);
GlobalTimer { regs } GlobalTimer { regs }
@ -33,12 +40,17 @@ impl GlobalTimer {
mpcore::ValueRegister::zeroed() mpcore::ValueRegister::zeroed()
); );
// find a prescaler value that matches CPU speed / 2 to us
let clocks = Clocks::get();
let mut prescaler = clocks.cpu_3x2x() / 1_000_000;
while prescaler > 256 {
prescaler /= 2;
}
// Start // Start
regs.global_timer_control.write( regs.global_timer_control.write(
mpcore::GlobalTimerControl::zeroed() mpcore::GlobalTimerControl::zeroed()
// maximum prescaler is still enough for millisecond .prescaler((prescaler - 1) as u8)
// precision while overflowing after centuries.
.prescaler(255)
.auto_increment_mode(true) .auto_increment_mode(true)
.timer_enable(true) .timer_enable(true)
); );
@ -66,6 +78,14 @@ impl GlobalTimer {
Milliseconds(self.get_counter() * (prescaler + 1) / (clocks.cpu_3x2x() as u64 / 1000)) 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 /// return a handle that has implements
/// `embedded_hal::timer::CountDown` /// `embedded_hal::timer::CountDown`
pub fn countdown(&self) -> CountDown { pub fn countdown(&self) -> CountDown {

View File

@ -1,6 +1,6 @@
//! A logger for the `log` crate //! A logger for the `log` crate
use libboard_zynq::{println, stdio}; use libboard_zynq::{println, stdio, timer::GlobalTimer};
pub static LOGGER: Logger = Logger; pub static LOGGER: Logger = Logger;
@ -17,8 +17,9 @@ impl log::Log for Logger {
fn log(&self, record: &log::Record) { fn log(&self, record: &log::Record) {
if self.enabled(record.metadata()) { if self.enabled(record.metadata()) {
// TODO: let timestamp = clock::get_us(); let timestamp = unsafe {
let timestamp = 0; GlobalTimer::get()
}.get_us();
let seconds = timestamp / 1_000_000; let seconds = timestamp / 1_000_000;
let micros = timestamp % 1_000_000; let micros = timestamp % 1_000_000;