aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDaniel Bevenius <[email protected]>2022-07-10 06:47:08 +0200
committerDaniel Bevenius <[email protected]>2022-07-14 13:52:22 +0200
commit8979959dd15be496e74a1a670a468d4d5168f0c8 (patch)
tree24dea79bdbfe90e7ef733ca9fe751a502c537a7e /tests
parent5318fe404bed68a38a1d81eb7359ebc38e6fad15 (diff)
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')
-rw-r--r--tests/rp/Cargo.toml1
-rw-r--r--tests/rp/src/bin/gpio_async.rs148
2 files changed, 149 insertions, 0 deletions
diff --git a/tests/rp/Cargo.toml b/tests/rp/Cargo.toml
index b3067fffd..fe791d0d7 100644
--- a/tests/rp/Cargo.toml
+++ b/tests/rp/Cargo.toml
@@ -16,6 +16,7 @@ embedded-hal = "0.2.6"
16embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" } 16embedded-hal-1 = { package = "embedded-hal", version = "1.0.0-alpha.8" }
17embedded-hal-async = { version = "0.1.0-alpha.1" } 17embedded-hal-async = { version = "0.1.0-alpha.1" }
18panic-probe = { version = "0.3.0", features = ["print-defmt"] } 18panic-probe = { version = "0.3.0", features = ["print-defmt"] }
19futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
19 20
20[profile.dev] 21[profile.dev]
21debug = 2 22debug = 2
diff --git a/tests/rp/src/bin/gpio_async.rs b/tests/rp/src/bin/gpio_async.rs
new file mode 100644
index 000000000..46df4105c
--- /dev/null
+++ b/tests/rp/src/bin/gpio_async.rs
@@ -0,0 +1,148 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::{assert, *};
6use embassy::executor::Spawner;
7use embassy::time::{Duration, Instant, Timer};
8use embassy_rp::gpio::{Input, Level, Output, Pull};
9use embassy_rp::Peripherals;
10use futures::future::join;
11use {defmt_rtt as _, panic_probe as _};
12
13#[embassy::main]
14async fn main(_spawner: Spawner, p: Peripherals) {
15 info!("embassy-rp gpio_async test");
16
17 // On the CI device the following pins are connected with each other.
18 let (mut output_pin, mut input_pin) = (p.PIN_0, p.PIN_1);
19
20 {
21 info!("test wait_for_high");
22 let mut output = Output::new(&mut output_pin, Level::Low);
23 let mut input = Input::new(&mut input_pin, Pull::None);
24
25 assert!(input.is_low(), "input was expected to be low");
26
27 let set_high_future = async {
28 // Allow time for wait_for_high_future to await wait_for_high().
29 Timer::after(Duration::from_millis(10)).await;
30 output.set_high();
31 };
32 let wait_for_high_future = async {
33 let start = Instant::now();
34 input.wait_for_high().await;
35 assert_duration(start);
36 };
37 join(set_high_future, wait_for_high_future).await;
38 info!("test wait_for_high: OK\n");
39 }
40
41 {
42 info!("test wait_for_low");
43 let mut output = Output::new(&mut output_pin, Level::High);
44 let mut input = Input::new(&mut input_pin, Pull::None);
45
46 assert!(input.is_high(), "input was expected to be high");
47
48 let set_low_future = async {
49 Timer::after(Duration::from_millis(10)).await;
50 output.set_low();
51 };
52 let wait_for_low_future = async {
53 let start = Instant::now();
54 input.wait_for_low().await;
55 assert_duration(start);
56 };
57 join(set_low_future, wait_for_low_future).await;
58 info!("test wait_for_low: OK\n");
59 }
60
61 {
62 info!("test wait_for_rising_edge");
63 let mut output = Output::new(&mut output_pin, Level::Low);
64 let mut input = Input::new(&mut input_pin, Pull::None);
65
66 assert!(input.is_low(), "input was expected to be low");
67
68 let set_high_future = async {
69 Timer::after(Duration::from_millis(10)).await;
70 output.set_high();
71 };
72 let wait_for_rising_edge_future = async {
73 let start = Instant::now();
74 input.wait_for_rising_edge().await;
75 assert_duration(start);
76 };
77 join(set_high_future, wait_for_rising_edge_future).await;
78 info!("test wait_for_rising_edge: OK\n");
79 }
80
81 {
82 info!("test wait_for_falling_edge");
83 let mut output = Output::new(&mut output_pin, Level::High);
84 let mut input = Input::new(&mut input_pin, Pull::None);
85
86 assert!(input.is_high(), "input was expected to be high");
87
88 let set_low_future = async {
89 Timer::after(Duration::from_millis(10)).await;
90 output.set_low();
91 };
92 let wait_for_falling_edge_future = async {
93 let start = Instant::now();
94 input.wait_for_falling_edge().await;
95 assert_duration(start);
96 };
97 join(set_low_future, wait_for_falling_edge_future).await;
98 info!("test wait_for_falling_edge: OK\n");
99 }
100
101 {
102 info!("test wait_for_any_edge (falling)");
103 let mut output = Output::new(&mut output_pin, Level::High);
104 let mut input = Input::new(&mut input_pin, Pull::None);
105
106 assert!(input.is_high(), "input was expected to be high");
107
108 let set_low_future = async {
109 Timer::after(Duration::from_millis(10)).await;
110 output.set_low();
111 };
112 let wait_for_any_edge_future = async {
113 let start = Instant::now();
114 input.wait_for_any_edge().await;
115 assert_duration(start);
116 };
117 join(set_low_future, wait_for_any_edge_future).await;
118 info!("test wait_for_any_edge (falling): OK\n");
119 }
120
121 {
122 info!("test wait_for_any_edge (rising)");
123 let mut output = Output::new(&mut output_pin, Level::Low);
124 let mut input = Input::new(&mut input_pin, Pull::None);
125
126 assert!(input.is_low(), "input was expected to be low");
127
128 let set_high_future = async {
129 Timer::after(Duration::from_millis(10)).await;
130 output.set_high();
131 };
132 let wait_for_any_edge_future = async {
133 let start = Instant::now();
134 input.wait_for_any_edge().await;
135 assert_duration(start);
136 };
137 join(set_high_future, wait_for_any_edge_future).await;
138 info!("test wait_for_any_edge (rising): OK\n");
139 }
140
141 info!("Test OK");
142 cortex_m::asm::bkpt();
143
144 fn assert_duration(start: Instant) {
145 let dur = Instant::now() - start;
146 assert!(dur >= Duration::from_millis(10) && dur < Duration::from_millis(11));
147 }
148}