2020-07-23 05:41:15 +08:00
|
|
|
use core::ops::Add;
|
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,
|
2020-07-23 05:41:15 +08:00
|
|
|
time::{Milliseconds, Microseconds, TimeSource},
|
2020-04-25 06:18:45 +08:00
|
|
|
};
|
|
|
|
|
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 {
|
2020-04-25 08:59:48 +08:00
|
|
|
/// Get the potentially uninitialized timer
|
|
|
|
pub unsafe fn get() -> GlobalTimer {
|
2020-08-13 13:39:04 +08:00
|
|
|
let regs = mpcore::RegisterBlock::mpcore();
|
2020-04-25 08:59:48 +08:00
|
|
|
GlobalTimer { regs }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the timer with a reset
|
|
|
|
pub fn start() -> GlobalTimer {
|
2020-08-13 13:39:04 +08:00
|
|
|
let mut regs = mpcore::RegisterBlock::mpcore();
|
2020-04-25 07:18:49 +08:00
|
|
|
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()
|
|
|
|
);
|
|
|
|
|
2020-04-25 08:59:48 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2020-04-25 06:18:45 +08:00
|
|
|
// 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 08:59:48 +08:00
|
|
|
.prescaler((prescaler - 1) as u8)
|
2020-04-25 06:18:45 +08:00
|
|
|
.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
|
|
|
|
2020-04-25 09:01:19 +08:00
|
|
|
/// read with high precision
|
2020-07-23 05:41:15 +08:00
|
|
|
pub fn get_us(&self) -> Microseconds {
|
2020-04-25 09:01:19 +08:00
|
|
|
let prescaler = self.regs.global_timer_control.read().prescaler() as u64;
|
|
|
|
let clocks = Clocks::get();
|
|
|
|
|
2020-07-23 05:41:15 +08:00
|
|
|
Microseconds(1_000_000 * self.get_counter() * (prescaler + 1) / clocks.cpu_3x2x() as u64)
|
2020-04-25 09:01:19 +08:00
|
|
|
}
|
|
|
|
|
2020-04-25 06:28:40 +08:00
|
|
|
/// return a handle that has implements
|
|
|
|
/// `embedded_hal::timer::CountDown`
|
2020-07-23 05:41:15 +08:00
|
|
|
pub fn countdown<U>(&self) -> CountDown<U>
|
|
|
|
where
|
|
|
|
Self: TimeSource<U>,
|
|
|
|
{
|
2020-04-25 06:28:40 +08:00
|
|
|
CountDown {
|
2020-04-25 07:18:49 +08:00
|
|
|
timer: self.clone(),
|
2020-07-23 05:41:15 +08:00
|
|
|
timeout: self.now(),
|
2020-04-25 06:28:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 05:41:15 +08:00
|
|
|
impl TimeSource<Milliseconds> for GlobalTimer {
|
|
|
|
fn now(&self) -> Milliseconds {
|
|
|
|
self.get_time()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TimeSource<Microseconds> for GlobalTimer {
|
|
|
|
fn now(&self) -> Microseconds {
|
|
|
|
self.get_us()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 06:28:40 +08:00
|
|
|
#[derive(Clone)]
|
2020-07-23 05:41:15 +08:00
|
|
|
pub struct CountDown<U> {
|
2020-04-25 07:18:49 +08:00
|
|
|
timer: GlobalTimer,
|
2020-07-23 05:41:15 +08:00
|
|
|
timeout: U,
|
2020-04-25 06:28:40 +08:00
|
|
|
}
|
|
|
|
|
2020-07-23 05:41:15 +08:00
|
|
|
/// embedded-hal async API
|
|
|
|
impl<U: Add<Output=U> + PartialOrd> embedded_hal::timer::CountDown for CountDown<U>
|
|
|
|
where
|
|
|
|
GlobalTimer: TimeSource<U>,
|
|
|
|
{
|
|
|
|
type Time = U;
|
2020-04-25 06:28:40 +08:00
|
|
|
|
|
|
|
fn start<T: Into<Self::Time>>(&mut self, count: T) {
|
2020-07-23 05:41:15 +08:00
|
|
|
self.timeout = self.timer.now() + count.into();
|
2020-04-25 06:28:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wait(&mut self) -> nb::Result<(), Void> {
|
2020-07-23 05:41:15 +08:00
|
|
|
if self.timer.now() <= self.timeout {
|
2020-04-25 06:28:40 +08:00
|
|
|
Err(nb::Error::WouldBlock)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 06:18:45 +08:00
|
|
|
}
|
2020-07-23 05:41:15 +08:00
|
|
|
|
2020-08-05 20:10:30 +08:00
|
|
|
impl<U: PartialOrd> CountDown<U>
|
|
|
|
where
|
|
|
|
GlobalTimer: TimeSource<U>,
|
|
|
|
{
|
|
|
|
pub fn waiting(&self) -> bool {
|
|
|
|
self.timer.now() <= self.timeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 05:41:15 +08:00
|
|
|
/// embedded-hal sync API
|
|
|
|
impl embedded_hal::blocking::delay::DelayMs<u64> for GlobalTimer {
|
|
|
|
fn delay_ms(&mut self, ms: u64) {
|
|
|
|
use embedded_hal::timer::CountDown;
|
|
|
|
|
|
|
|
let mut countdown = self.countdown::<Milliseconds>();
|
|
|
|
countdown.start(Milliseconds(ms));
|
|
|
|
nb::block!(countdown.wait()).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// embedded-hal sync API
|
|
|
|
impl embedded_hal::blocking::delay::DelayUs<u64> for GlobalTimer {
|
|
|
|
fn delay_us(&mut self, us: u64) {
|
|
|
|
use embedded_hal::timer::CountDown;
|
|
|
|
|
|
|
|
let mut countdown = self.countdown::<Microseconds>();
|
|
|
|
countdown.start(Microseconds(us));
|
|
|
|
nb::block!(countdown.wait()).unwrap();
|
|
|
|
}
|
|
|
|
}
|