From e054dc904bae8970a029a8ef0208cf0426cea92e Mon Sep 17 00:00:00 2001 From: Scott Mabin Date: Thu, 5 Sep 2024 12:57:03 +0100 Subject: [PATCH] rename esp_hal::time::current_time to esp_hal::time::uptime --- esp-hal/MIGRATING-0.20.md | 9 ++++++++ esp-hal/src/time.rs | 7 ++++--- esp-wifi/src/timer/riscv.rs | 2 +- esp-wifi/src/timer/xtensa.rs | 2 +- examples/src/bin/timer_interrupt.rs | 4 +--- hil-test/tests/delay.rs | 16 +++++++-------- hil-test/tests/embassy_timers_executors.rs | 24 +++++++++++----------- hil-test/tests/get_time.rs | 8 ++++---- 8 files changed, 40 insertions(+), 32 deletions(-) diff --git a/esp-hal/MIGRATING-0.20.md b/esp-hal/MIGRATING-0.20.md index 4902d004644..bc3a690fc41 100644 --- a/esp-hal/MIGRATING-0.20.md +++ b/esp-hal/MIGRATING-0.20.md @@ -41,3 +41,12 @@ Instead of manually grabbing peripherals and setting up clocks, you should now c - The `GpioN` type aliasses are no longer available. You can use `GpioPin` instead. - The `AnyInputOnlyPin` has been removed. Replace any use with `AnyPin`. - The `NoPinType` has been removed. You can use `DummyPin` in its place. + +## `esp_hal::time::current_time` rename + +To avoid confusion with the `Rtc::current_time` wall clock time APIs, we've renamed `esp_hal::time::current_time` to `esp_hal::time::uptime()`. + +```diff +- use esp_hal::time::current_time; ++ use esp_hal::time::uptime; +``` diff --git a/esp-hal/src/time.rs b/esp-hal/src/time.rs index 82aefad3cd7..8013979cc22 100644 --- a/esp-hal/src/time.rs +++ b/esp-hal/src/time.rs @@ -10,7 +10,7 @@ #[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 current_time() -> fugit::Instant { +pub fn uptime() -> fugit::Instant { #[cfg(esp32)] let (ticks, div) = { // on ESP32 use LACT @@ -50,9 +50,10 @@ pub fn current_time() -> fugit::Instant { #[cfg(esp32)] pub(crate) fn time_init() { + let apb = Clocks::get().apb_clock.to_Hz(); // we assume 80MHz APB clock source - there is no way to configure it in a // different way currently - const APB_FREQUENCY: u32 = 80_000_000u32; + assert!(apb, 80_000_000u32); let tg0 = unsafe { crate::peripherals::TIMG0::steal() }; @@ -63,7 +64,7 @@ pub(crate) fn time_init() { // 16 MHz counter tg0.lactconfig() - .modify(|_, w| unsafe { w.divider().bits((APB_FREQUENCY / 16_000_000u32) as u16) }); + .modify(|_, w| unsafe { w.divider().bits((apb / 16_000_000u32) as u16) }); tg0.lactconfig().modify(|_, w| { w.increase().bit(true); w.autoreload().bit(true); diff --git a/esp-wifi/src/timer/riscv.rs b/esp-wifi/src/timer/riscv.rs index a4f0df7076a..ca05936561d 100644 --- a/esp-wifi/src/timer/riscv.rs +++ b/esp-wifi/src/timer/riscv.rs @@ -89,7 +89,7 @@ pub fn yield_task() { /// Current systimer count value /// A tick is 1 / 1_000_000 seconds pub fn get_systimer_count() -> u64 { - esp_hal::time::current_time().ticks() + esp_hal::time::uptime().ticks() } // TODO: use an Instance type instead... diff --git a/esp-wifi/src/timer/xtensa.rs b/esp-wifi/src/timer/xtensa.rs index 78b5891ebec..e4447fb4472 100644 --- a/esp-wifi/src/timer/xtensa.rs +++ b/esp-wifi/src/timer/xtensa.rs @@ -20,7 +20,7 @@ pub const TICKS_PER_SECOND: u64 = 1_000_000; /// This function must not be called in a critical section. Doing so may return /// an incorrect value. pub fn get_systimer_count() -> u64 { - esp_hal::time::current_time().ticks() + esp_hal::time::uptime().ticks() } pub fn setup_timer(mut timer1: TimeBase) -> Result<(), esp_hal::timer::Error> { diff --git a/examples/src/bin/timer_interrupt.rs b/examples/src/bin/timer_interrupt.rs index 11a1f3c8796..99734293f6a 100644 --- a/examples/src/bin/timer_interrupt.rs +++ b/examples/src/bin/timer_interrupt.rs @@ -46,9 +46,7 @@ fn tg0_t0_level() { critical_section::with(|cs| { esp_println::println!( "Interrupt at {} ms", - esp_hal::time::current_time() - .duration_since_epoch() - .to_millis() + esp_hal::time::uptime().duration_since_epoch().to_millis() ); let mut timer0 = TIMER0.borrow_ref_mut(cs); diff --git a/hil-test/tests/delay.rs b/hil-test/tests/delay.rs index 82296ed94f7..a3193ceaa7c 100644 --- a/hil-test/tests/delay.rs +++ b/hil-test/tests/delay.rs @@ -29,9 +29,9 @@ mod tests { #[test] #[timeout(2)] fn delay_ns(mut ctx: Context) { - let t1 = esp_hal::time::current_time(); + let t1 = esp_hal::time::uptime(); ctx.delay.delay_ns(600_000_000); - let t2 = esp_hal::time::current_time(); + let t2 = esp_hal::time::uptime(); assert!(t2 > t1); assert!( @@ -44,9 +44,9 @@ mod tests { #[test] #[timeout(2)] fn delay_700millis(ctx: Context) { - let t1 = esp_hal::time::current_time(); + let t1 = esp_hal::time::uptime(); ctx.delay.delay_millis(700); - let t2 = esp_hal::time::current_time(); + let t2 = esp_hal::time::uptime(); assert!(t2 > t1); assert!( @@ -59,9 +59,9 @@ mod tests { #[test] #[timeout(2)] fn delay_1_500_000us(mut ctx: Context) { - let t1 = esp_hal::time::current_time(); + let t1 = esp_hal::time::uptime(); ctx.delay.delay_us(1_500_000); - let t2 = esp_hal::time::current_time(); + let t2 = esp_hal::time::uptime(); assert!(t2 > t1); assert!( @@ -74,9 +74,9 @@ mod tests { #[test] #[timeout(5)] fn delay_3_000ms(mut ctx: Context) { - let t1 = esp_hal::time::current_time(); + let t1 = esp_hal::time::uptime(); ctx.delay.delay_ms(3000); - let t2 = esp_hal::time::current_time(); + let t2 = esp_hal::time::uptime(); assert!(t2 > t1); assert!( diff --git a/hil-test/tests/embassy_timers_executors.rs b/hil-test/tests/embassy_timers_executors.rs index 648719cf77f..75342dae275 100644 --- a/hil-test/tests/embassy_timers_executors.rs +++ b/hil-test/tests/embassy_timers_executors.rs @@ -48,10 +48,10 @@ mod test_cases { use super::*; pub async fn run_test_one_shot_async() { - let t1 = esp_hal::time::current_time(); + let t1 = esp_hal::time::uptime(); Timer::after_millis(50).await; Timer::after_millis(30).await; - let t2 = esp_hal::time::current_time(); + let t2 = esp_hal::time::uptime(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); assert!( @@ -64,11 +64,11 @@ mod test_cases { pub fn run_test_periodic_timer(timer: impl Peripheral

) { let mut periodic = PeriodicTimer::new(timer); - let t1 = esp_hal::time::current_time(); + let t1 = esp_hal::time::uptime(); periodic.start(100.millis()).unwrap(); nb::block!(periodic.wait()).unwrap(); - let t2 = esp_hal::time::current_time(); + let t2 = esp_hal::time::uptime(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); assert!( @@ -81,9 +81,9 @@ mod test_cases { pub fn run_test_oneshot_timer(timer: impl Peripheral

) { let timer = OneShotTimer::new(timer); - let t1 = esp_hal::time::current_time(); + let t1 = esp_hal::time::uptime(); timer.delay_millis(50); - let t2 = esp_hal::time::current_time(); + let t2 = esp_hal::time::uptime(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); assert!( @@ -94,10 +94,10 @@ mod test_cases { } pub async fn run_join_test() { - let t1 = esp_hal::time::current_time(); + let t1 = esp_hal::time::uptime(); embassy_futures::join::join(Timer::after_millis(50), Timer::after_millis(30)).await; Timer::after_millis(50).await; - let t2 = esp_hal::time::current_time(); + let t2 = esp_hal::time::uptime(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); assert!( @@ -232,11 +232,11 @@ mod test { let outcome = async { let mut ticker = Ticker::every(Duration::from_millis(30)); - let t1 = esp_hal::time::current_time(); + let t1 = esp_hal::time::uptime(); ticker.next().await; ticker.next().await; ticker.next().await; - let t2 = esp_hal::time::current_time(); + let t2 = esp_hal::time::uptime(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); assert!( @@ -268,13 +268,13 @@ mod test { // We are retrying 5 times because probe-rs polling RTT may introduce some // jitter. for _ in 0..5 { - let t1 = esp_hal::time::current_time(); + let t1 = esp_hal::time::uptime(); let mut ticker = Ticker::every(Duration::from_hz(100_000)); for _ in 0..2000 { ticker.next().await; } - let t2 = esp_hal::time::current_time(); + let t2 = esp_hal::time::uptime(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); let duration = (t2 - t1).to_micros(); diff --git a/hil-test/tests/get_time.rs b/hil-test/tests/get_time.rs index 6c7eadecd62..ba8bfd7b3fa 100644 --- a/hil-test/tests/get_time.rs +++ b/hil-test/tests/get_time.rs @@ -13,9 +13,9 @@ struct Context { } fn time_moves_forward_during(ctx: Context, f: F) { - let t1 = esp_hal::time::current_time(); + let t1 = esp_hal::time::uptime(); f(ctx); - let t2 = esp_hal::time::current_time(); + let t2 = esp_hal::time::uptime(); assert!(t2 > t1); } @@ -37,9 +37,9 @@ mod tests { #[test] #[timeout(3)] fn test_current_time(ctx: Context) { - let t1 = esp_hal::time::current_time(); + let t1 = esp_hal::time::uptime(); ctx.delay.delay_millis(500); - let t2 = esp_hal::time::current_time(); + let t2 = esp_hal::time::uptime(); assert!(t2 > t1); assert!((t2 - t1).to_millis() >= 500u64);