pub type Duration = fugit::Duration<u64, 1, 1_000_000>;
pub type Instant = fugit::Instant<u64, 1, 1_000_000>;
#[cfg_attr(esp32, doc = "36_558 years")]
#[cfg_attr(esp32s2, doc = "7_311 years")]
#[cfg_attr(not(any(esp32, esp32s2)), doc = "more than 7 years")]
pub fn now() -> Instant {
#[cfg(esp32)]
let (ticks, div) = {
let tg0 = unsafe { crate::peripherals::TIMG0::steal() };
tg0.lactupdate().write(|w| unsafe { w.update().bits(1) });
let lo_initial = tg0.lactlo().read().bits();
let mut div = tg0.lactconfig().read().divider().bits();
let lo = loop {
let lo = tg0.lactlo().read().bits();
if lo != lo_initial || div == 0 {
break lo;
}
div -= 1;
};
let hi = tg0.lacthi().read().bits();
let ticks = (hi as u64) << 32u64 | lo as u64;
(ticks, 16)
};
#[cfg(not(esp32))]
let (ticks, div) = {
let ticks = crate::timer::systimer::SystemTimer::now();
(
ticks,
(crate::timer::systimer::SystemTimer::ticks_per_second() / 1_000_000),
)
};
Instant::from_ticks(ticks / div)
}
#[cfg(esp32)]
pub(crate) fn time_init() {
let apb = crate::Clocks::get().apb_clock.to_Hz();
assert_eq!(apb, 80_000_000u32);
let tg0 = unsafe { crate::peripherals::TIMG0::steal() };
tg0.lactconfig().write(|w| unsafe { w.bits(0) });
tg0.lactalarmhi().write(|w| unsafe { w.bits(u32::MAX) });
tg0.lactalarmlo().write(|w| unsafe { w.bits(u32::MAX) });
tg0.lactload().write(|w| unsafe { w.load().bits(1) });
tg0.lactconfig()
.modify(|_, w| unsafe { w.divider().bits((apb / 16_000_000u32) as u16) });
tg0.lactconfig().modify(|_, w| {
w.increase().bit(true);
w.autoreload().bit(true);
w.en().bit(true)
});
}