Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve tests slightly
  • Loading branch information
jessebraham authored and JurajSadel committed Sep 24, 2024
commit 3334ab27c9f62b1b77c274c4f591a4dbb7dfe81f
51 changes: 29 additions & 22 deletions hil-test/tests/delay_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -22,6 +22,19 @@ struct Context {
peripherals: Peripherals,
}

async fn test_async_delay_ns(mut timer: impl DelayNs, duration: u32) {
Comment thread
JurajSadel marked this conversation as resolved.
let t1 = esp_hal::time::current_time();
timer.delay_ns(duration).await;
Comment thread
JurajSadel marked this conversation as resolved.
Outdated
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 {
Expand All @@ -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();
Comment thread
jessebraham marked this conversation as resolved.
Outdated

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;
Comment thread
JurajSadel marked this conversation as resolved.
Outdated
#[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;
}
}