diff options
author | Daniel Bevenius <daniel.bevenius@gmail.com> | 2022-07-10 06:47:08 +0200 |
---|---|---|
committer | Daniel Bevenius <daniel.bevenius@gmail.com> | 2022-07-14 13:52:22 +0200 |
commit | 8979959dd15be496e74a1a670a468d4d5168f0c8 (patch) | |
tree | 24dea79bdbfe90e7ef733ca9fe751a502c537a7e /tests/rp | |
parent | 5318fe404bed68a38a1d81eb7359ebc38e6fad15 (diff) | |
download | embassy-8979959dd15be496e74a1a670a468d4d5168f0c8.zip |
Add embedded_hal_async support for embassy-rp
This commit adds support for embedded-hal-async to the Embassy
Raspberry PI crate.
Diffstat (limited to 'tests/rp')
-rw-r--r-- | tests/rp/Cargo.toml | 1 | ||||
-rw-r--r-- | tests/rp/src/bin/gpio_async.rs | 148 |
2 files changed, 149 insertions, 0 deletions
diff --git a/tests/rp/Cargo.toml b/tests/rp/Cargo.toml index b3067fff..fe791d0d 100644 --- a/tests/rp/Cargo.toml +++ b/tests/rp/Cargo.toml @@ -16,6 +16,7 @@ embedded-hal = "0.2.6" embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } embedded-hal-async = { version = "0.1.0-alpha.1" } panic-probe = { version = "0.3.0", features = ["print-defmt"] } +futures = { version = "0.3.17", default-features = false, features = ["async-await"] } [profile.dev] debug = 2 diff --git a/tests/rp/src/bin/gpio_async.rs b/tests/rp/src/bin/gpio_async.rs new file mode 100644 index 00000000..46df4105 --- /dev/null +++ b/tests/rp/src/bin/gpio_async.rs @@ -0,0 +1,148 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::{assert, *}; +use embassy::executor::Spawner; +use embassy::time::{Duration, Instant, Timer}; +use embassy_rp::gpio::{Input, Level, Output, Pull}; +use embassy_rp::Peripherals; +use futures::future::join; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("embassy-rp gpio_async test"); + + // On the CI device the following pins are connected with each other. + let (mut output_pin, mut input_pin) = (p.PIN_0, p.PIN_1); + + { + info!("test wait_for_high"); + let mut output = Output::new(&mut output_pin, Level::Low); + let mut input = Input::new(&mut input_pin, Pull::None); + + assert!(input.is_low(), "input was expected to be low"); + + let set_high_future = async { + // Allow time for wait_for_high_future to await wait_for_high(). + Timer::after(Duration::from_millis(10)).await; + output.set_high(); + }; + let wait_for_high_future = async { + let start = Instant::now(); + input.wait_for_high().await; + assert_duration(start); + }; + join(set_high_future, wait_for_high_future).await; + info!("test wait_for_high: OK\n"); + } + + { + info!("test wait_for_low"); + let mut output = Output::new(&mut output_pin, Level::High); + let mut input = Input::new(&mut input_pin, Pull::None); + + assert!(input.is_high(), "input was expected to be high"); + + let set_low_future = async { + Timer::after(Duration::from_millis(10)).await; + output.set_low(); + }; + let wait_for_low_future = async { + let start = Instant::now(); + input.wait_for_low().await; + assert_duration(start); + }; + join(set_low_future, wait_for_low_future).await; + info!("test wait_for_low: OK\n"); + } + + { + info!("test wait_for_rising_edge"); + let mut output = Output::new(&mut output_pin, Level::Low); + let mut input = Input::new(&mut input_pin, Pull::None); + + assert!(input.is_low(), "input was expected to be low"); + + let set_high_future = async { + Timer::after(Duration::from_millis(10)).await; + output.set_high(); + }; + let wait_for_rising_edge_future = async { + let start = Instant::now(); + input.wait_for_rising_edge().await; + assert_duration(start); + }; + join(set_high_future, wait_for_rising_edge_future).await; + info!("test wait_for_rising_edge: OK\n"); + } + + { + info!("test wait_for_falling_edge"); + let mut output = Output::new(&mut output_pin, Level::High); + let mut input = Input::new(&mut input_pin, Pull::None); + + assert!(input.is_high(), "input was expected to be high"); + + let set_low_future = async { + Timer::after(Duration::from_millis(10)).await; + output.set_low(); + }; + let wait_for_falling_edge_future = async { + let start = Instant::now(); + input.wait_for_falling_edge().await; + assert_duration(start); + }; + join(set_low_future, wait_for_falling_edge_future).await; + info!("test wait_for_falling_edge: OK\n"); + } + + { + info!("test wait_for_any_edge (falling)"); + let mut output = Output::new(&mut output_pin, Level::High); + let mut input = Input::new(&mut input_pin, Pull::None); + + assert!(input.is_high(), "input was expected to be high"); + + let set_low_future = async { + Timer::after(Duration::from_millis(10)).await; + output.set_low(); + }; + let wait_for_any_edge_future = async { + let start = Instant::now(); + input.wait_for_any_edge().await; + assert_duration(start); + }; + join(set_low_future, wait_for_any_edge_future).await; + info!("test wait_for_any_edge (falling): OK\n"); + } + + { + info!("test wait_for_any_edge (rising)"); + let mut output = Output::new(&mut output_pin, Level::Low); + let mut input = Input::new(&mut input_pin, Pull::None); + + assert!(input.is_low(), "input was expected to be low"); + + let set_high_future = async { + Timer::after(Duration::from_millis(10)).await; + output.set_high(); + }; + let wait_for_any_edge_future = async { + let start = Instant::now(); + input.wait_for_any_edge().await; + assert_duration(start); + }; + join(set_high_future, wait_for_any_edge_future).await; + info!("test wait_for_any_edge (rising): OK\n"); + } + + info!("Test OK"); + cortex_m::asm::bkpt(); + + fn assert_duration(start: Instant) { + let dur = Instant::now() - start; + assert!(dur >= Duration::from_millis(10) && dur < Duration::from_millis(11)); + } +} |