|
1 | | -//! The `time` module offers a way to get the system uptime. |
| 1 | +//! `SysUptime` implements proposed embedded-hal `TimeCount` trait to get system uptime |
2 | 2 | //! |
3 | 3 | //! ### Example |
4 | 4 | //! ```no_run |
5 | 5 | //! let time = time::current_time(); |
6 | 6 | //! ``` |
7 | 7 | #![warn(missing_docs)] |
| 8 | +use counters::TimeCount; |
8 | 9 |
|
9 | | -/// Provides time since system start in microseconds precision |
| 10 | +/// Provides an interface by which to access the platforms clock. Can be used where |
| 11 | +/// (proposed) embedded-hal `TimeCount` implementor is expected. |
| 12 | +pub struct SysUptime; |
| 13 | + |
| 14 | +/// Provides time since system start |
10 | 15 | /// |
11 | 16 | /// The counter won’t measure time in sleep-mode. |
12 | 17 | /// |
13 | 18 | /// The timer will wrap after |
14 | 19 | #[cfg_attr(esp32, doc = "36_558 years")] |
15 | 20 | #[cfg_attr(esp32s2, doc = "7_311 years")] |
16 | 21 | #[cfg_attr(not(any(esp32, esp32s2)), doc = "more than 7 years")] |
17 | | -pub fn current_time() -> fugit::Instant<u64, 1, 1_000_000> { |
| 22 | +impl counters::TimeCount for SysUptime { |
| 23 | + type RawData = u64; |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + /// Defines granularity of the timer. Combines with the raw tick count to derive an instant |
| 28 | + #[cfg(not(esp32))] |
| 29 | + type TickMeasure = fugit::Instant<Self::RawData, 1, {crate::timer::systimer::TICKS_PER_SECOND}>; |
18 | 30 | #[cfg(esp32)] |
19 | | - let (ticks, div) = { |
20 | | - // on ESP32 use LACT |
21 | | - let tg0 = unsafe { crate::peripherals::TIMG0::steal() }; |
22 | | - tg0.lactupdate().write(|w| unsafe { w.update().bits(1) }); |
23 | | - |
24 | | - // The peripheral doesn't have a bit to indicate that the update is done, so we |
25 | | - // poll the lower 32 bit part of the counter until it changes, or a timeout |
26 | | - // expires. |
27 | | - let lo_initial = tg0.lactlo().read().bits(); |
28 | | - let mut div = tg0.lactconfig().read().divider().bits(); |
29 | | - let lo = loop { |
30 | | - let lo = tg0.lactlo().read().bits(); |
31 | | - if lo != lo_initial || div == 0 { |
32 | | - break lo; |
33 | | - } |
34 | | - div -= 1; |
| 31 | + type TickMeasure = fugit::Duration<Self::RawData, 1, 16_000_000>; |
| 32 | + |
| 33 | + type Error = (); |
| 34 | + |
| 35 | + fn try_now_raw(&self) -> Result<Self::RawData, Self::Error> { |
| 36 | + #[cfg(esp32)] |
| 37 | + let ticks = { |
| 38 | + // on ESP32 use LACT |
| 39 | + let tg0 = unsafe { crate::peripherals::TIMG0::steal() }; |
| 40 | + tg0.lactupdate().write(|w| unsafe { w.update().bits(1) }); |
| 41 | + |
| 42 | + // The peripheral doesn't have a bit to indicate that the update is done, so we |
| 43 | + // poll the lower 32 bit part of the counter until it changes, or a timeout |
| 44 | + // expires. |
| 45 | + let lo_initial = tg0.lactlo().read().bits(); |
| 46 | + let mut div = tg0.lactconfig().read().divider().bits(); |
| 47 | + let lo = loop { |
| 48 | + let lo = tg0.lactlo().read().bits(); |
| 49 | + if lo != lo_initial || div == 0 { |
| 50 | + break lo; |
| 51 | + } |
| 52 | + div -= 1; |
| 53 | + }; |
| 54 | + let hi = tg0.lacthi().read().bits(); |
| 55 | + |
| 56 | + let ticks = (hi as u64) << 32u64 | lo as u64; |
| 57 | + ticks |
35 | 58 | }; |
36 | | - let hi = tg0.lacthi().read().bits(); |
37 | 59 |
|
38 | | - let ticks = (hi as u64) << 32u64 | lo as u64; |
39 | | - (ticks, 16) |
40 | | - }; |
| 60 | + #[cfg(not(esp32))] |
| 61 | + let ticks = { |
| 62 | + // otherwise use SYSTIMER |
| 63 | + crate::timer::systimer::SystemTimer::now() |
| 64 | + }; |
41 | 65 |
|
42 | | - #[cfg(not(esp32))] |
43 | | - let (ticks, div) = { |
44 | | - // otherwise use SYSTIMER |
45 | | - let ticks = crate::timer::systimer::SystemTimer::now(); |
46 | | - ( |
47 | | - ticks, |
48 | | - (crate::timer::systimer::SystemTimer::TICKS_PER_SECOND / 1_000_000), |
49 | | - ) |
50 | | - }; |
51 | | - |
52 | | - fugit::Instant::<u64, 1, 1_000_000>::from_ticks(ticks / div) |
| 66 | + Ok(ticks) |
| 67 | + } |
| 68 | + |
| 69 | + fn try_now(&self) -> Result<Self::TickMeasure, Self::Error> { |
| 70 | + let ticks = self.try_now_raw()?; |
| 71 | + Ok(Self::TickMeasure::from_ticks(ticks)) |
| 72 | + |
| 73 | + } |
53 | 74 | } |
54 | 75 |
|
55 | | -#[cfg(esp32)] |
56 | | -pub(crate) fn time_init() { |
57 | | - // we assume 80MHz APB clock source - there is no way to configure it in a |
58 | | - // different way currently |
59 | | - const APB_FREQUENCY: u32 = 80_000_000u32; |
60 | | - |
61 | | - let tg0 = unsafe { crate::peripherals::TIMG0::steal() }; |
62 | | - |
63 | | - tg0.lactconfig().write(|w| unsafe { w.bits(0) }); |
64 | | - tg0.lactalarmhi().write(|w| unsafe { w.bits(u32::MAX) }); |
65 | | - tg0.lactalarmlo().write(|w| unsafe { w.bits(u32::MAX) }); |
66 | | - tg0.lactload().write(|w| unsafe { w.load().bits(1) }); |
67 | | - |
68 | | - // 16 MHz counter |
69 | | - tg0.lactconfig() |
70 | | - .modify(|_, w| unsafe { w.divider().bits((APB_FREQUENCY / 16_000_000u32) as u16) }); |
71 | | - tg0.lactconfig().modify(|_, w| { |
72 | | - w.increase().bit(true); |
73 | | - w.autoreload().bit(true); |
74 | | - w.en().bit(true) |
75 | | - }); |
| 76 | +impl SysUptime { |
| 77 | + #[cfg(esp32)] |
| 78 | + pub(crate) fn time_init() -> Self { |
| 79 | + // we assume 80MHz APB clock source - there is no way to configure it in a |
| 80 | + // different way currently |
| 81 | + const APB_FREQUENCY: u32 = 80_000_000u32; |
| 82 | + |
| 83 | + let tg0 = unsafe { crate::peripherals::TIMG0::steal() }; |
| 84 | + |
| 85 | + tg0.lactconfig().write(|w| unsafe { w.bits(0) }); |
| 86 | + tg0.lactalarmhi().write(|w| unsafe { w.bits(u32::MAX) }); |
| 87 | + tg0.lactalarmlo().write(|w| unsafe { w.bits(u32::MAX) }); |
| 88 | + tg0.lactload().write(|w| unsafe { w.load().bits(1) }); |
| 89 | + |
| 90 | + // 16 MHz counter |
| 91 | + tg0.lactconfig() |
| 92 | + .modify(|_, w| unsafe { w.divider().bits((APB_FREQUENCY / 16_000_000u32) as u16) }); |
| 93 | + tg0.lactconfig().modify(|_, w| { |
| 94 | + w.increase().bit(true); |
| 95 | + w.autoreload().bit(true); |
| 96 | + w.en().bit(true) |
| 97 | + }); |
| 98 | + Self |
| 99 | + } |
| 100 | + #[deprecated(note = "Use SysUptime::try_now() instead")] |
| 101 | + #[allow(missing_docs)] |
| 102 | + pub fn current_time(&self) -> <Self as counters::TimeCount>::TickMeasure { |
| 103 | + self.try_now().unwrap() |
| 104 | + } |
76 | 105 | } |
0 commit comments