From 0b1b5bf14fdce874bc47fac1531e8d92a7286e50 Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Wed, 4 Sep 2024 11:19:43 -0700 Subject: [PATCH] Improve tests slightly --- hil-test/tests/delay_async.rs | 51 ++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/hil-test/tests/delay_async.rs b/hil-test/tests/delay_async.rs index 2502ad985a5..4c6b50a7c94 100644 --- a/hil-test/tests/delay_async.rs +++ b/hil-test/tests/delay_async.rs @@ -8,7 +8,7 @@ #![no_std] #![no_main] -use embedded_hal_async::delay::DelayNs as _; +use embedded_hal_async::delay::DelayNs; use esp_hal::{ peripherals::Peripherals, timer::{ @@ -22,6 +22,19 @@ struct Context { peripherals: Peripherals, } +async fn test_async_delay_ns(mut timer: impl DelayNs, duration: u32) { + let t1 = esp_hal::time::current_time(); + timer.delay_ns(duration).await; + let t2 = esp_hal::time::current_time(); + + assert!(t2 > t1); + assert!( + (t2 - t1).to_nanos() >= duration as u64, + "diff: {:?}", + (t2 - t1).to_nanos() + ); +} + #[cfg(test)] #[embedded_test::tests(executor = esp_hal_embassy::Executor::new())] mod tests { @@ -39,35 +52,29 @@ mod tests { async fn test_systimer_async_delay_ns(ctx: Context) { let mut alarms = SystemTimer::new(ctx.peripherals.SYSTIMER); let unit = FrozenUnit::new(&mut alarms.unit0); - let mut alarm0 = Alarm::new_async(alarms.comparator0, &unit).into_periodic(); - - let t1 = esp_hal::time::current_time(); - alarm0.delay_ns(600_000_000).await; - let t2 = esp_hal::time::current_time(); + let alarm0 = Alarm::new_async(alarms.comparator0, &unit).into_periodic(); - assert!(t2 > t1); - assert!( - (t2 - t1).to_nanos() >= 600_000_000u64, - "diff: {:?}", - (t2 - t1).to_nanos() - ); + test_async_delay_ns(alarm0, 600_000_000).await; } #[test] #[timeout(2)] async fn test_timg0_async_delay_ns(ctx: Context) { let timg0 = TimerGroup::new_async(ctx.peripherals.TIMG0); - let mut timer0 = timg0.timer0; - let t1 = esp_hal::time::current_time(); - timer0.delay_ns(600_000_000).await; - let t2 = esp_hal::time::current_time(); + test_async_delay_ns(timg0.timer0, 600_000_000).await; + #[cfg(timg_timer1)] + test_async_delay_ns(timg0.timer1, 600_000_000).await; + } + + #[cfg(timg1)] + #[test] + #[timeout(2)] + async fn test_timg1_async_delay_ns(ctx: Context) { + let timg1 = TimerGroup::new_async(ctx.peripherals.TIMG1); - assert!(t2 > t1); - assert!( - (t2 - t1).to_nanos() >= 600_000_000u64, - "diff: {:?}", - (t2 - t1).to_nanos() - ); + test_async_delay_ns(timg1.timer0, 600_000_000).await; + #[cfg(timg_timer1)] + test_async_delay_ns(timg1.timer1, 600_000_000).await; } }