Skip to content

Commit b2072ff

Browse files
committed
Merge pull request 'improved async gpio example for va108xx' (#13) from improve-async-gpio-example-va108xx into main
Reviewed-on: https://egit.irs.uni-stuttgart.de/rust/vorago-rs/pulls/13
2 parents 52fd8f6 + ae98693 commit b2072ff

File tree

1 file changed

+60
-14
lines changed

1 file changed

+60
-14
lines changed

va108xx/examples/embassy/src/bin/async-gpio.rs

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use embassy_executor::Spawner;
1111
use embassy_sync::channel::{Receiver, Sender};
1212
use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, channel::Channel};
1313
use embassy_time::{Duration, Instant, Timer};
14-
use embedded_hal_async::digital::Wait;
1514
use va108xx_hal::gpio::asynch::{on_interrupt_for_async_gpio_for_port, InputPinAsync};
1615
use va108xx_hal::gpio::{Input, Output, PinState, Port};
1716
use va108xx_hal::pins::{PinsA, PinsB};
@@ -72,8 +71,8 @@ async fn main(spawner: Spawner) {
7271
let out_pb22 = Output::new(portb.pb22, PinState::Low);
7372
let in_pb23 = Input::new_floating(portb.pb23);
7473

75-
let in_pa1_async = InputPinAsync::new(in_pa1, pac::Interrupt::OC10);
76-
let in_pb23_async = InputPinAsync::new(in_pb23, PB22_TO_PB23_IRQ);
74+
let mut in_pa1_async = InputPinAsync::new(in_pa1, pac::Interrupt::OC10);
75+
let mut in_pb23_async = InputPinAsync::new(in_pb23, PB22_TO_PB23_IRQ);
7776

7877
spawner
7978
.spawn(output_task(
@@ -91,12 +90,16 @@ async fn main(spawner: Spawner) {
9190
.unwrap();
9291

9392
if CHECK_PA0_TO_PA1 {
94-
check_pin_to_pin_async_ops("PA0 to PA1", CHANNEL_PA0_PA1.sender(), in_pa1_async).await;
93+
check_pin_to_pin_async_ops("PA0 to PA1", CHANNEL_PA0_PA1.sender(), &mut in_pa1_async).await;
9594
defmt::info!("Example PA0 to PA1 done");
9695
}
9796
if CHECK_PB22_TO_PB23 {
98-
check_pin_to_pin_async_ops("PB22 to PB23", CHANNEL_PB22_TO_PB23.sender(), in_pb23_async)
99-
.await;
97+
check_pin_to_pin_async_ops(
98+
"PB22 to PB23",
99+
CHANNEL_PB22_TO_PB23.sender(),
100+
&mut in_pb23_async,
101+
)
102+
.await;
100103
defmt::info!("Example PB22 to PB23 done");
101104
}
102105

@@ -107,50 +110,68 @@ async fn main(spawner: Spawner) {
107110
}
108111
}
109112

110-
async fn check_pin_to_pin_async_ops(
113+
async fn check_wait_for_high(
111114
ctx: &'static str,
112115
sender: Sender<'static, ThreadModeRawMutex, GpioCmd, 3>,
113-
mut async_input: impl Wait,
116+
async_input: &mut InputPinAsync,
114117
) {
115118
defmt::info!(
116119
"{}: sending SetHigh command ({} ms)",
117120
ctx,
118121
Instant::now().as_millis()
119122
);
120123
sender.send(GpioCmd::new(GpioCmdType::SetHigh, 20)).await;
121-
async_input.wait_for_high().await.unwrap();
124+
async_input.wait_for_high().await;
122125
defmt::info!(
123126
"{}: Input pin is high now ({} ms)",
124127
ctx,
125128
Instant::now().as_millis()
126129
);
130+
}
127131

132+
async fn check_wait_for_low(
133+
ctx: &'static str,
134+
sender: Sender<'static, ThreadModeRawMutex, GpioCmd, 3>,
135+
async_input: &mut InputPinAsync,
136+
) {
128137
defmt::info!(
129138
"{}: sending SetLow command ({} ms)",
130139
ctx,
131140
Instant::now().as_millis()
132141
);
133142
sender.send(GpioCmd::new(GpioCmdType::SetLow, 20)).await;
134-
async_input.wait_for_low().await.unwrap();
143+
async_input.wait_for_low().await;
135144
defmt::info!(
136145
"{}: Input pin is low now ({} ms)",
137146
ctx,
138147
Instant::now().as_millis()
139148
);
149+
}
140150

151+
async fn check_wait_for_rising_edge(
152+
ctx: &'static str,
153+
sender: Sender<'static, ThreadModeRawMutex, GpioCmd, 3>,
154+
async_input: &mut InputPinAsync,
155+
) {
141156
defmt::info!(
142157
"{}: sending RisingEdge command ({} ms)",
143158
ctx,
144159
Instant::now().as_millis()
145160
);
146161
sender.send(GpioCmd::new(GpioCmdType::RisingEdge, 20)).await;
147-
async_input.wait_for_rising_edge().await.unwrap();
162+
async_input.wait_for_rising_edge().await;
148163
defmt::info!(
149164
"{}: input pin had rising edge ({} ms)",
150165
ctx,
151166
Instant::now().as_millis()
152167
);
168+
}
153169

170+
async fn check_wait_for_falling_edge(
171+
ctx: &'static str,
172+
sender: Sender<'static, ThreadModeRawMutex, GpioCmd, 3>,
173+
async_input: &mut InputPinAsync,
174+
) {
154175
defmt::info!(
155176
"{}: sending Falling command ({} ms)",
156177
ctx,
@@ -159,13 +180,19 @@ async fn check_pin_to_pin_async_ops(
159180
sender
160181
.send(GpioCmd::new(GpioCmdType::FallingEdge, 20))
161182
.await;
162-
async_input.wait_for_falling_edge().await.unwrap();
183+
async_input.wait_for_falling_edge().await;
163184
defmt::info!(
164185
"{}: input pin had a falling edge ({} ms)",
165186
ctx,
166187
Instant::now().as_millis()
167188
);
189+
}
168190

191+
async fn check_wait_for_any_edge_with_falling_edge(
192+
ctx: &'static str,
193+
sender: Sender<'static, ThreadModeRawMutex, GpioCmd, 3>,
194+
async_input: &mut InputPinAsync,
195+
) {
169196
defmt::info!(
170197
"{}: sending Falling command ({} ms)",
171198
ctx,
@@ -174,27 +201,46 @@ async fn check_pin_to_pin_async_ops(
174201
sender
175202
.send(GpioCmd::new(GpioCmdType::FallingEdge, 20))
176203
.await;
177-
async_input.wait_for_any_edge().await.unwrap();
204+
async_input.wait_for_any_edge().await;
178205
defmt::info!(
179206
"{}: input pin had a falling (any) edge ({} ms)",
180207
ctx,
181208
Instant::now().as_millis()
182209
);
210+
}
183211

212+
async fn check_wait_for_any_edge_with_rising_edge(
213+
ctx: &'static str,
214+
sender: Sender<'static, ThreadModeRawMutex, GpioCmd, 3>,
215+
async_input: &mut InputPinAsync,
216+
) {
184217
defmt::info!(
185218
"{}: sending Falling command ({} ms)",
186219
ctx,
187220
Instant::now().as_millis()
188221
);
189222
sender.send(GpioCmd::new(GpioCmdType::RisingEdge, 20)).await;
190-
async_input.wait_for_any_edge().await.unwrap();
223+
async_input.wait_for_any_edge().await;
191224
defmt::info!(
192225
"{}: input pin had a rising (any) edge ({} ms)",
193226
ctx,
194227
Instant::now().as_millis()
195228
);
196229
}
197230

231+
async fn check_pin_to_pin_async_ops(
232+
ctx: &'static str,
233+
sender: Sender<'static, ThreadModeRawMutex, GpioCmd, 3>,
234+
async_input: &mut InputPinAsync,
235+
) {
236+
check_wait_for_high(ctx, sender, async_input).await;
237+
check_wait_for_low(ctx, sender, async_input).await;
238+
check_wait_for_rising_edge(ctx, sender, async_input).await;
239+
check_wait_for_falling_edge(ctx, sender, async_input).await;
240+
check_wait_for_any_edge_with_falling_edge(ctx, sender, async_input).await;
241+
check_wait_for_any_edge_with_rising_edge(ctx, sender, async_input).await;
242+
}
243+
198244
#[embassy_executor::task(pool_size = 2)]
199245
async fn output_task(
200246
ctx: &'static str,

0 commit comments

Comments
 (0)