1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
teleprobe_meta::target!(b"rpi-pico");
use defmt::{assert, *};
use embassy_executor::Spawner;
use embassy_futures::join::join;
use embassy_rp::gpio::{Input, Level, Output, Pull};
use embassy_time::{Duration, Instant, Timer};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
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_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_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_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_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_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_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));
}
}
|