From db8a5912b7dbf90e1a39fd3a8514b559e932bb4d Mon Sep 17 00:00:00 2001 From: Juraj Sadel Date: Mon, 29 Apr 2024 11:36:45 +0200 Subject: [PATCH] Add `Delay` HIL test (#1415) * Add HIL test * fmt --- hil-test/Cargo.toml | 6 ++- hil-test/tests/delay.rs | 80 ++++++++++++++++++++++++++++++++++++ hil-test/tests/gpio.rs | 2 +- hil-test/tests/uart_async.rs | 2 +- 4 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 hil-test/tests/delay.rs diff --git a/hil-test/Cargo.toml b/hil-test/Cargo.toml index b6d3b7fd7..66ac0e2a1 100644 --- a/hil-test/Cargo.toml +++ b/hil-test/Cargo.toml @@ -57,6 +57,10 @@ harness = false name = "get_time" harness = false +[[test]] +name = "delay" +harness = false + [dependencies] cfg-if = "1.0.0" critical-section = "1.1.2" @@ -136,4 +140,4 @@ debug-assertions = false incremental = false opt-level = 3 lto = "fat" -overflow-checks = false +overflow-checks = false \ No newline at end of file diff --git a/hil-test/tests/delay.rs b/hil-test/tests/delay.rs new file mode 100644 index 000000000..26197ce2e --- /dev/null +++ b/hil-test/tests/delay.rs @@ -0,0 +1,80 @@ +//! Delay Test + +#![no_std] +#![no_main] + +use defmt_rtt as _; +use embedded_hal::delay::DelayNs; +use esp_backtrace as _; +use esp_hal::{clock::ClockControl, delay::Delay, peripherals::Peripherals, system::SystemControl}; + +struct Context { + delay: Delay, +} + +impl Context { + pub fn init() -> Self { + let peripherals = Peripherals::take(); + let system = SystemControl::new(peripherals.SYSTEM); + let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + + let delay = Delay::new(&clocks); + + Context { delay } + } +} + +#[cfg(test)] +#[embedded_test::tests] +mod tests { + use super::*; + + #[init] + fn init() -> Context { + Context::init() + } + + #[test] + #[timeout(1)] + fn delay_ns(mut ctx: Context) { + let t1 = esp_hal::time::current_time(); + ctx.delay.delay_ns(600_000_000); + let t2 = esp_hal::time::current_time(); + + assert!(t2 > t1); + assert!((t2 - t1).to_nanos() >= 600_000_000u64); + } + + #[test] + #[timeout(1)] + fn delay_700millis(ctx: Context) { + let t1 = esp_hal::time::current_time(); + ctx.delay.delay_millis(700); + let t2 = esp_hal::time::current_time(); + + assert!(t2 > t1); + assert!((t2 - t1).to_millis() >= 700u64); + } + + #[test] + #[timeout(2)] + fn delay_1_500_000us(mut ctx: Context) { + let t1 = esp_hal::time::current_time(); + ctx.delay.delay_us(1_500_000); + let t2 = esp_hal::time::current_time(); + + assert!(t2 > t1); + assert!((t2 - t1).to_micros() >= 1_500_000u64); + } + + #[test] + #[timeout(5)] + fn delay_3_000ms(mut ctx: Context) { + let t1 = esp_hal::time::current_time(); + ctx.delay.delay_ms(3000); + let t2 = esp_hal::time::current_time(); + + assert!(t2 > t1); + assert!((t2 - t1).to_millis() >= 3000u64); + } +} diff --git a/hil-test/tests/gpio.rs b/hil-test/tests/gpio.rs index 64ef99744..f51847dfc 100644 --- a/hil-test/tests/gpio.rs +++ b/hil-test/tests/gpio.rs @@ -19,8 +19,8 @@ use esp_hal::{ gpio::{GpioPin, Input, Io, Output, OutputPin, PullDown, PushPull, Unknown}, macros::handler, peripherals::Peripherals, - timer::TimerGroup, system::SystemControl, + timer::TimerGroup, }; static COUNTER: Mutex> = Mutex::new(RefCell::new(0)); diff --git a/hil-test/tests/uart_async.rs b/hil-test/tests/uart_async.rs index fe1eb6752..f0f2da4f4 100644 --- a/hil-test/tests/uart_async.rs +++ b/hil-test/tests/uart_async.rs @@ -15,9 +15,9 @@ use esp_hal::{ clock::ClockControl, gpio::Io, peripherals::{Peripherals, UART0}, + system::SystemControl, uart::{config::Config, TxRxPins, Uart, UartRx, UartTx}, Async, - system::SystemControl, }; struct Context {