esp-hal/hil-test/tests/delay_async.rs
Scott Mabin 2512658653
[3/3] Timer refactor: Timer driver mode, ETM support and erasure (#2586)
* Add mode param and type erasure to high level timer drivers

* Add async impls for the timers plus eha impl

* re add etm example, fix etm for systimer

* re add tests

* Add Into<AnyTimer> + 'static bounds to the Timer trait

* remove set_alarm_active impl detail from the timer trait

* clippy

* doc fix ups

* changelog and migration guide

* review

* fix h2, reuse schedule for delay
2024-11-27 14:10:56 +00:00

173 lines
4.9 KiB
Rust

//! Async Delay Test
//!
//! Specifically tests the various implementations of the
//! `embedded_hal_async::delay::DelayNs` trait.
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
#![no_std]
#![no_main]
use embedded_hal_async::delay::DelayNs;
#[cfg(systimer)]
use esp_hal::timer::systimer::SystemTimer;
use esp_hal::{
peripherals::Peripherals,
timer::{timg::TimerGroup, OneShotTimer},
};
use hil_test as _;
struct Context {
peripherals: Peripherals,
}
async fn test_async_delay_ns(mut timer: impl DelayNs, duration: u32) {
for i in 1..5 {
let t1 = esp_hal::time::now();
timer.delay_ns(duration).await;
let t2 = esp_hal::time::now();
assert!(t2 > t1);
assert!(
(t2 - t1).to_nanos() >= duration as u64,
"diff[{}]: {:?} >= {}",
i,
(t2 - t1).to_nanos(),
duration
);
}
}
async fn test_async_delay_us(mut timer: impl DelayNs, duration: u32) {
for _ in 1..5 {
let t1 = esp_hal::time::now();
timer.delay_us(duration).await;
let t2 = esp_hal::time::now();
assert!(t2 > t1);
assert!(
(t2 - t1).to_nanos() >= duration as u64,
"diff: {:?}",
(t2 - t1).to_nanos()
);
}
}
async fn test_async_delay_ms(mut timer: impl DelayNs, duration: u32) {
for _ in 1..5 {
let t1 = esp_hal::time::now();
timer.delay_ms(duration).await;
let t2 = esp_hal::time::now();
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 {
use super::*;
#[init]
fn init() -> Context {
Context {
peripherals: esp_hal::init(esp_hal::Config::default()),
}
}
#[cfg(systimer)]
#[test]
#[timeout(2)]
async fn test_systimer_async_delay_ns(ctx: Context) {
let alarms = SystemTimer::new(ctx.peripherals.SYSTIMER);
test_async_delay_ns(OneShotTimer::new(alarms.alarm0).into_async(), 10_000_000).await;
}
#[test]
#[timeout(2)]
async fn test_timg0_async_delay_ns(ctx: Context) {
let timg0 = TimerGroup::new(ctx.peripherals.TIMG0);
test_async_delay_ns(OneShotTimer::new(timg0.timer0).into_async(), 10_000_000).await;
#[cfg(timg_timer1)]
test_async_delay_ns(OneShotTimer::new(timg0.timer1).into_async(), 10_000_000).await;
}
#[cfg(timg1)]
#[test]
#[timeout(2)]
async fn test_timg1_async_delay_ns(ctx: Context) {
let timg1 = TimerGroup::new(ctx.peripherals.TIMG1);
test_async_delay_ns(OneShotTimer::new(timg1.timer0).into_async(), 10_000_000).await;
#[cfg(timg_timer1)]
test_async_delay_ns(OneShotTimer::new(timg1.timer1).into_async(), 10_000_000).await;
}
#[cfg(systimer)]
#[test]
#[timeout(2)]
async fn test_systimer_async_delay_us(ctx: Context) {
let alarms = SystemTimer::new(ctx.peripherals.SYSTIMER);
test_async_delay_us(OneShotTimer::new(alarms.alarm0).into_async(), 10_000).await;
}
#[test]
#[timeout(2)]
async fn test_timg0_async_delay_us(ctx: Context) {
let timg0 = TimerGroup::new(ctx.peripherals.TIMG0);
test_async_delay_us(OneShotTimer::new(timg0.timer0).into_async(), 10_000).await;
#[cfg(timg_timer1)]
test_async_delay_us(OneShotTimer::new(timg0.timer1).into_async(), 10_000).await;
}
#[cfg(timg1)]
#[test]
#[timeout(2)]
async fn test_timg1_async_delay_us(ctx: Context) {
let timg1 = TimerGroup::new(ctx.peripherals.TIMG1);
test_async_delay_us(OneShotTimer::new(timg1.timer0).into_async(), 10_000).await;
#[cfg(timg_timer1)]
test_async_delay_us(OneShotTimer::new(timg1.timer1).into_async(), 10_000).await;
}
#[cfg(systimer)]
#[test]
#[timeout(2)]
async fn test_systimer_async_delay_ms(ctx: Context) {
let alarms = SystemTimer::new(ctx.peripherals.SYSTIMER);
test_async_delay_ms(OneShotTimer::new(alarms.alarm0).into_async(), 10).await;
}
#[test]
#[timeout(2)]
async fn test_timg0_async_delay_ms(ctx: Context) {
let timg0 = TimerGroup::new(ctx.peripherals.TIMG0);
test_async_delay_ms(OneShotTimer::new(timg0.timer0).into_async(), 10).await;
#[cfg(timg_timer1)]
test_async_delay_ms(OneShotTimer::new(timg0.timer1).into_async(), 10).await;
}
#[cfg(timg1)]
#[test]
#[timeout(2)]
async fn test_timg1_async_delay_ms(ctx: Context) {
let timg1 = TimerGroup::new(ctx.peripherals.TIMG1);
test_async_delay_ms(OneShotTimer::new(timg1.timer0).into_async(), 10).await;
#[cfg(timg_timer1)]
test_async_delay_ms(OneShotTimer::new(timg1.timer1).into_async(), 10).await;
}
}