Skip to content

Commit

Permalink
hil tests
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Apr 3, 2024
1 parent 6b2a9dc commit edafb55
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 48 deletions.
2 changes: 1 addition & 1 deletion examples/src/bin/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() -> ! {

// Set GPIO0 as an output, and set its state high initially.
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let mut led = io.pins.gpio0.into_push_pull_output();
let mut led = io.pins.gpio8.into_push_pull_output();

led.set_high();

Expand Down
2 changes: 1 addition & 1 deletion examples/src/bin/ledc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() -> ! {
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let led = io.pins.gpio0.into_push_pull_output();
let led = io.pins.gpio8.into_push_pull_output();

let mut ledc = LEDC::new(peripherals.LEDC, &clocks);

Expand Down
90 changes: 44 additions & 46 deletions hil-test/tests/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use core::cell::RefCell;

use critical_section::Mutex;
use defmt_rtt as _;
use embedded_hal::digital::{InputPin as _, OutputPin as _, StatefulOutputPin as _};
use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
Expand Down Expand Up @@ -74,7 +73,6 @@ pub fn interrupt_handler() {
mod tests {
use defmt::assert_eq;
use embassy_time::{Duration, Timer};
use embedded_hal_async::digital::Wait;
use esp_hal::gpio::{Event, Pin};
use portable_atomic::{AtomicUsize, Ordering};

Expand All @@ -84,7 +82,7 @@ mod tests {
fn init() -> Context {
let mut ctx = Context::init();
// make sure tests don't interfere with each other
ctx.io4.set_low().ok();
ctx.io4.set_low();
ctx
}

Expand All @@ -97,15 +95,15 @@ mod tests {
embassy_futures::select::select(
async {
loop {
io2.wait_for_rising_edge().await.unwrap();
io2.wait_for_rising_edge().await;
counter.fetch_add(1, Ordering::SeqCst);
}
},
async {
for _ in 0..5 {
io4.set_high().unwrap();
io4.set_high();
Timer::after(Duration::from_millis(25)).await;
io4.set_low().unwrap();
io4.set_low();
Timer::after(Duration::from_millis(25)).await;
}
},
Expand All @@ -128,30 +126,30 @@ mod tests {
}

#[test]
fn test_gpio_input(mut ctx: Context) {
fn test_gpio_input(ctx: Context) {
// `InputPin`:
assert_eq!(ctx.io2.is_low(), Ok(true));
assert_eq!(ctx.io2.is_high(), Ok(false));
assert_eq!(ctx.io2.is_low(), true);
assert_eq!(ctx.io2.is_high(), false);
}

#[test]
fn test_gpio_output(mut ctx: Context) {
// `StatefulOutputPin`:
assert_eq!(ctx.io4.is_set_low(), Ok(true));
assert_eq!(ctx.io4.is_set_high(), Ok(false));
assert!(ctx.io4.set_high().is_ok());
assert_eq!(ctx.io4.is_set_low(), Ok(false));
assert_eq!(ctx.io4.is_set_high(), Ok(true));
assert_eq!(ctx.io4.is_set_low(), true);
assert_eq!(ctx.io4.is_set_high(), false);
ctx.io4.set_high();
assert_eq!(ctx.io4.is_set_low(), false);
assert_eq!(ctx.io4.is_set_high(), true);

// `ToggleableOutputPin`:
assert!(ctx.io4.toggle().is_ok());
assert_eq!(ctx.io4.is_set_low(), Ok(true));
assert_eq!(ctx.io4.is_set_high(), Ok(false));
assert!(ctx.io4.toggle().is_ok());
assert_eq!(ctx.io4.is_set_low(), Ok(false));
assert_eq!(ctx.io4.is_set_high(), Ok(true));
ctx.io4.toggle();
assert_eq!(ctx.io4.is_set_low(), true);
assert_eq!(ctx.io4.is_set_high(), false);
ctx.io4.toggle();
assert_eq!(ctx.io4.is_set_low(), false);
assert_eq!(ctx.io4.is_set_high(), true);
// Leave in initial state for next test
assert!(ctx.io4.toggle().is_ok());
ctx.io4.toggle();
}

#[test]
Expand All @@ -161,23 +159,23 @@ mod tests {
ctx.io2.listen(Event::AnyEdge);
INPUT_PIN.borrow_ref_mut(cs).replace(ctx.io2);
});
assert!(ctx.io4.set_high().is_ok());
ctx.io4.set_high();
ctx.delay.delay_millis(1);
assert!(ctx.io4.set_low().is_ok());
ctx.io4.set_low();
ctx.delay.delay_millis(1);
assert!(ctx.io4.set_high().is_ok());
ctx.io4.set_high();
ctx.delay.delay_millis(1);
assert!(ctx.io4.set_low().is_ok());
ctx.io4.set_low();
ctx.delay.delay_millis(1);
assert!(ctx.io4.set_high().is_ok());
ctx.io4.set_high();
ctx.delay.delay_millis(1);
assert!(ctx.io4.set_low().is_ok());
ctx.io4.set_low();
ctx.delay.delay_millis(1);
assert!(ctx.io4.set_high().is_ok());
ctx.io4.set_high();
ctx.delay.delay_millis(1);
assert!(ctx.io4.set_low().is_ok());
ctx.io4.set_low();
ctx.delay.delay_millis(1);
assert!(ctx.io4.set_high().is_ok());
ctx.io4.set_high();
ctx.delay.delay_millis(1);

let count = critical_section::with(|cs| *COUNTER.borrow_ref(cs));
Expand All @@ -194,32 +192,32 @@ mod tests {
let mut io4 = ctx.io4.into_open_drain_output();
io4.internal_pull_up(true);

assert!(io2.set_high().is_ok());
assert!(io4.set_high().is_ok());
io2.set_high();
io4.set_high();
ctx.delay.delay_millis(1);

assert_eq!(io2.is_high(), Ok(true));
assert_eq!(io4.is_high(), Ok(true));
assert_eq!(io2.is_high(), true);
assert_eq!(io4.is_high(), true);

assert!(io2.set_low().is_ok());
assert!(io4.set_high().is_ok());
io2.set_low();
io4.set_high();
ctx.delay.delay_millis(1);

assert_eq!(io2.is_low(), Ok(true));
assert_eq!(io4.is_low(), Ok(true));
assert_eq!(io2.is_low(), true);
assert_eq!(io4.is_low(), true);

assert!(io2.set_high().is_ok());
assert!(io4.set_high().is_ok());
io2.set_high();
io4.set_high();
ctx.delay.delay_millis(1);

assert_eq!(io2.is_high(), Ok(true));
assert_eq!(io4.is_high(), Ok(true));
assert_eq!(io2.is_high(), true);
assert_eq!(io4.is_high(), true);

assert!(io2.set_high().is_ok());
assert!(io4.set_low().is_ok());
io2.set_high();
io4.set_low();
ctx.delay.delay_millis(1);

assert_eq!(io2.is_low(), Ok(true));
assert_eq!(io4.is_low(), Ok(true));
assert_eq!(io2.is_low(), true);
assert_eq!(io4.is_low(), true);
}
}

0 comments on commit edafb55

Please sign in to comment.