2020-04-25 06:28:40 +08:00
|
|
|
use void::Void;
|
2020-04-25 06:18:45 +08:00
|
|
|
use libregister::{RegisterR, RegisterW};
|
|
|
|
use crate::{
|
|
|
|
clocks::Clocks,
|
|
|
|
mpcore,
|
|
|
|
time::Milliseconds,
|
|
|
|
};
|
|
|
|
|
2020-04-25 06:28:40 +08:00
|
|
|
/// "uptime"
|
2020-04-25 07:18:49 +08:00
|
|
|
#[derive(Clone, Copy)]
|
2020-04-25 06:18:45 +08:00
|
|
|
pub struct GlobalTimer {
|
2020-04-25 07:18:49 +08:00
|
|
|
regs: &'static mpcore::RegisterBlock,
|
2020-04-25 06:18:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GlobalTimer {
|
|
|
|
pub fn new() -> GlobalTimer {
|
2020-04-25 07:18:49 +08:00
|
|
|
let mut regs = mpcore::RegisterBlock::new();
|
|
|
|
Self::reset(&mut regs);
|
2020-04-25 06:18:45 +08:00
|
|
|
GlobalTimer { regs }
|
|
|
|
}
|
2020-04-25 06:28:40 +08:00
|
|
|
|
2020-04-25 07:18:49 +08:00
|
|
|
fn reset(regs: &mut mpcore::RegisterBlock) {
|
2020-04-25 06:18:45 +08:00
|
|
|
// Disable
|
2020-04-25 07:18:49 +08:00
|
|
|
regs.global_timer_control.write(
|
2020-04-25 06:18:45 +08:00
|
|
|
mpcore::GlobalTimerControl::zeroed()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Reset counters
|
2020-04-25 07:18:49 +08:00
|
|
|
regs.global_timer_counter0.write(
|
2020-04-25 06:18:45 +08:00
|
|
|
mpcore::ValueRegister::zeroed()
|
|
|
|
);
|
2020-04-25 07:18:49 +08:00
|
|
|
regs.global_timer_counter1.write(
|
2020-04-25 06:18:45 +08:00
|
|
|
mpcore::ValueRegister::zeroed()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Start
|
2020-04-25 07:18:49 +08:00
|
|
|
regs.global_timer_control.write(
|
2020-04-25 06:18:45 +08:00
|
|
|
mpcore::GlobalTimerControl::zeroed()
|
2020-04-25 06:28:40 +08:00
|
|
|
// maximum prescaler is still enough for millisecond
|
|
|
|
// precision while overflowing after centuries.
|
2020-04-25 06:18:45 +08:00
|
|
|
.prescaler(255)
|
|
|
|
.auto_increment_mode(true)
|
|
|
|
.timer_enable(true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-25 06:28:40 +08:00
|
|
|
/// read the raw counter value
|
2020-04-25 06:18:45 +08:00
|
|
|
pub fn get_counter(&self) -> u64 {
|
|
|
|
loop {
|
2020-04-25 06:28:40 +08:00
|
|
|
let c1_pre = self.regs.global_timer_counter1.read().value();
|
2020-04-25 06:18:45 +08:00
|
|
|
let c0 = self.regs.global_timer_counter0.read().value();
|
|
|
|
let c1_post = self.regs.global_timer_counter1.read().value();
|
|
|
|
|
2020-04-25 06:28:40 +08:00
|
|
|
if c1_pre == c1_post {
|
|
|
|
return ((c1_pre as u64) << 32) | (c0 as u64);
|
2020-04-25 06:18:45 +08:00
|
|
|
}
|
2020-04-25 06:28:40 +08:00
|
|
|
// retry if c0 has wrapped while reading.
|
2020-04-25 06:18:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 06:28:40 +08:00
|
|
|
/// read and convert to time
|
2020-04-25 06:18:45 +08:00
|
|
|
pub fn get_time(&self) -> Milliseconds {
|
|
|
|
let prescaler = self.regs.global_timer_control.read().prescaler() as u64;
|
|
|
|
let clocks = Clocks::get();
|
|
|
|
|
|
|
|
Milliseconds(self.get_counter() * (prescaler + 1) / (clocks.cpu_3x2x() as u64 / 1000))
|
|
|
|
}
|
2020-04-25 06:28:40 +08:00
|
|
|
|
|
|
|
/// return a handle that has implements
|
|
|
|
/// `embedded_hal::timer::CountDown`
|
|
|
|
pub fn countdown(&self) -> CountDown {
|
|
|
|
CountDown {
|
2020-04-25 07:18:49 +08:00
|
|
|
timer: self.clone(),
|
2020-04-25 06:28:40 +08:00
|
|
|
timeout: Milliseconds(0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2020-04-25 07:18:49 +08:00
|
|
|
pub struct CountDown {
|
|
|
|
timer: GlobalTimer,
|
2020-04-25 06:28:40 +08:00
|
|
|
timeout: Milliseconds,
|
|
|
|
}
|
|
|
|
|
2020-04-25 07:18:49 +08:00
|
|
|
impl embedded_hal::timer::CountDown for CountDown {
|
2020-04-25 06:28:40 +08:00
|
|
|
type Time = Milliseconds;
|
|
|
|
|
|
|
|
fn start<T: Into<Self::Time>>(&mut self, count: T) {
|
2020-04-25 07:18:49 +08:00
|
|
|
self.timeout = self.timer.get_time() + count.into();
|
2020-04-25 06:28:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wait(&mut self) -> nb::Result<(), Void> {
|
|
|
|
if self.timer.get_time() < self.timeout {
|
|
|
|
Err(nb::Error::WouldBlock)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 06:18:45 +08:00
|
|
|
}
|