2022-07-10 06:47:08 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
2023-08-16 17:51:47 +02:00
|
|
|
teleprobe_meta::target!(b"rpi-pico");
|
2022-07-10 06:47:08 +02:00
|
|
|
|
|
|
|
use defmt::{assert, *};
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2022-09-22 16:48:35 +02:00
|
|
|
use embassy_futures::join::join;
|
2022-07-10 06:47:08 +02:00
|
|
|
use embassy_rp::gpio::{Input, Level, Output, Pull};
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_time::{Duration, Instant, Timer};
|
2022-07-10 06:47:08 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 18:49:55 +02:00
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_rp::init(Default::default());
|
2022-07-10 06:47:08 +02:00
|
|
|
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().
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(10).await;
|
2022-07-10 06:47:08 +02:00
|
|
|
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 {
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(10).await;
|
2022-07-10 06:47:08 +02:00
|
|
|
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 {
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(10).await;
|
2022-07-10 06:47:08 +02:00
|
|
|
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 {
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(10).await;
|
2022-07-10 06:47:08 +02:00
|
|
|
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 {
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(10).await;
|
2022-07-10 06:47:08 +02:00
|
|
|
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 {
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(10).await;
|
2022-07-10 06:47:08 +02:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|