Skip to content

Commit

Permalink
Provide a looping executor for stable async tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Jan 7, 2025
1 parent 5c011d5 commit 1a3d5ce
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 29 deletions.
7 changes: 3 additions & 4 deletions hil-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ cfg-if = "1.0.0"
critical-section = "1.1.3"
defmt = "0.3.8"
defmt-rtt = { version = "0.4.1", optional = true }
embassy-executor = "0.6.0"
embassy-futures = "0.1.1"
embassy-sync = "0.6.0"
embassy-time = "0.3.2"
Expand All @@ -220,7 +221,7 @@ digest = { version = "0.10.7", default-features = false }
elliptic-curve = { version = "0.13.8", default-features = false, features = ["sec1"] }
embassy-executor = { version = "0.6.0", default-features = false }
# Add the `embedded-test/defmt` feature for more verbose testing
embedded-test = { version = "0.5.0", git = "https://github.com/probe-rs/embedded-test.git", rev = "7109473", default-features = false }
embedded-test = { version = "0.5.0", git = "https://github.com/probe-rs/embedded-test.git", rev = "7109473", default-features = false, features = ["embassy", "external-executor"] }
fugit = "0.3.7"
hex-literal = "0.4.1"
nb = "1.1.0"
Expand All @@ -234,7 +235,7 @@ esp-build = { path = "../esp-build" }
esp-metadata = { path = "../esp-metadata" }

[features]
default = ["embassy"]
default = []
unstable = ["esp-hal/unstable"]

defmt = ["dep:defmt-rtt", "esp-hal/defmt", "embedded-test/defmt"]
Expand Down Expand Up @@ -287,8 +288,6 @@ esp32s3 = [
]
# Async & Embassy:
embassy = [
"embedded-test/embassy",
"embedded-test/external-executor",
"dep:esp-hal-embassy",
]
generic-queue = [
Expand Down
39 changes: 39 additions & 0 deletions hil-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,42 @@ macro_rules! unconnected_pin {
}
}};
}

// A simple looping executor to test async code without esp-hal-embassy (which
// needs `esp-hal/unstable`).
#[cfg(not(feature = "embassy"))]
mod executor {
use core::marker::PhantomData;

use embassy_executor::{raw, Spawner};

#[export_name = "__pender"]
fn __pender(_: *mut ()) {}

pub struct Executor {
inner: raw::Executor,
not_send: PhantomData<*mut ()>,
}

impl Executor {
pub fn new() -> Self {
Self {
inner: raw::Executor::new(core::ptr::null_mut()),
not_send: PhantomData,
}
}

pub fn run(&'static mut self, init: impl FnOnce(Spawner)) -> ! {
init(self.inner.spawner());

loop {
unsafe { self.inner.poll() };
}
}
}
}

#[cfg(feature = "embassy")]
pub use esp_hal_embassy::Executor;
#[cfg(not(feature = "embassy"))]
pub use executor::Executor;
2 changes: 1 addition & 1 deletion hil-test/tests/delay_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async fn test_async_delay_ms(mut timer: impl DelayNs, duration: u32) {
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 2, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 2, executor = hil_test::Executor::new())]
mod tests {
use super::*;

Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/embassy_interrupt_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct Context {
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 3, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 3, executor = hil_test::Executor::new())]
mod test {
use super::*;

Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/embassy_interrupt_spi_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async fn interrupt_driven_task(mut i2s_tx: esp_hal::i2s::master::I2sTx<'static,
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 3, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 3, executor = hil_test::Executor::new())]
mod test {
use super::*;

Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/embassy_timers_executors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn set_up_embassy_with_systimer(peripherals: Peripherals) {
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 3, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 3, executor = hil_test::Executor::new())]
mod test {
use super::*;
use crate::test_cases::*;
Expand Down
32 changes: 20 additions & 12 deletions hil-test/tests/gpio.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
//! GPIO Test
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
//% FEATURES: unstable generic-queue
//% FEATURES: unstable embassy generic-queue
//% FEATURES(stable):

#![no_std]
#![no_main]

use core::cell::RefCell;

use critical_section::Mutex;
#[cfg(feature = "unstable")]
use embassy_time::{Duration, Timer};
use esp_hal::{
delay::Delay,
gpio::{AnyPin, Input, Io, Level, Output, Pin, Pull},
interrupt::InterruptConfigurable,
gpio::{AnyPin, Input, Level, Output, OutputOpenDrain, Pin, Pull},
macros::handler,
};
#[cfg(feature = "unstable")]
use esp_hal::{
gpio::{Event, Flex, Io},
interrupt::InterruptConfigurable,
timer::timg::TimerGroup,
};
use hil_test as _;
#[cfg(feature = "unstable")]
use portable_atomic::{AtomicUsize, Ordering};

static COUNTER: Mutex<RefCell<u32>> = Mutex::new(RefCell::new(0));
static INPUT_PIN: Mutex<RefCell<Option<Input>>> = Mutex::new(RefCell::new(None));
Expand All @@ -39,27 +48,25 @@ pub fn interrupt_handler() {
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 3, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 3, executor = hil_test::Executor::new())]
mod tests {
use embassy_time::{Duration, Timer};
use esp_hal::gpio::{Event, Flex, OutputOpenDrain};
use portable_atomic::{AtomicUsize, Ordering};

use super::*;

#[init]
fn init() -> Context {
let peripherals = esp_hal::init(esp_hal::Config::default());

let mut io = Io::new(peripherals.IO_MUX);
io.set_interrupt_handler(interrupt_handler);

let delay = Delay::new();

let (gpio1, gpio2) = hil_test::common_test_pins!(peripherals);

#[cfg(feature = "unstable")]
{
// Interrupts are unstable
let mut io = Io::new(peripherals.IO_MUX);
io.set_interrupt_handler(interrupt_handler);

// Timers are unstable
let timg0 = TimerGroup::new(peripherals.TIMG0);
esp_hal_embassy::init(timg0.timer0);
}
Expand All @@ -72,6 +79,7 @@ mod tests {
}

#[test]
#[cfg(feature = "unstable")] // Timers are unstable
async fn async_edge(ctx: Context) {
let counter = AtomicUsize::new(0);
let Context {
Expand Down Expand Up @@ -198,7 +206,7 @@ mod tests {
}

#[test]
#[cfg(feature = "unstable")]
#[cfg(feature = "unstable")] // Interrupts are unstable
fn gpio_interrupt(ctx: Context) {
let mut test_gpio1 = Input::new(ctx.test_gpio1, Pull::Down);
let mut test_gpio2 = Output::new(ctx.test_gpio2, Level::Low);
Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/gpio_custom_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async fn drive_pins(gpio1: impl Into<AnyPin>, gpio2: impl Into<AnyPin>) -> usize
}

#[cfg(test)]
#[embedded_test::tests(executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(executor = hil_test::Executor::new())]
mod tests {

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/i2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn enable_loopback() {
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 3, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 3, executor = hil_test::Executor::new())]
mod tests {
use super::*;

Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/lcd_cam_i8080_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct Context<'d> {
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 3, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 3, executor = hil_test::Executor::new())]
mod tests {
use super::*;

Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/parl_io_tx_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct Context {
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 3, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 3, executor = hil_test::Executor::new())]
mod tests {
use defmt::info;

Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/rsa_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const fn compute_mprime(modulus: &U512) -> u32 {
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 5, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 5, executor = hil_test::Executor::new())]
mod tests {
use super::*;

Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/spi_full_duplex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct Context {
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 3, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 3, executor = hil_test::Executor::new())]
mod tests {
use super::*;

Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/spi_slave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl BitbangSpi {
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 10, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 10, executor = hil_test::Executor::new())]
mod tests {
use super::*;

Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/uart_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct Context {
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 3, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 3, executor = hil_test::Executor::new())]
mod tests {
use super::*;

Expand Down
2 changes: 1 addition & 1 deletion hil-test/tests/uart_tx_rx_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct Context {
}

#[cfg(test)]
#[embedded_test::tests(default_timeout = 3, executor = esp_hal_embassy::Executor::new())]
#[embedded_test::tests(default_timeout = 3, executor = hil_test::Executor::new())]
mod tests {
use super::*;

Expand Down

0 comments on commit 1a3d5ce

Please sign in to comment.