-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Async: GPIO #333
Merged
Merged
Async: GPIO #333
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ec116cf
Add `is_listening` to `Pin` trait
MabezDev 0273968
Add `Wait` impl for Gpio Input
MabezDev fe42ba1
Add GPIO wait example for C3
MabezDev 1ccb99e
Ensure correct bank is accessed in interrupt
MabezDev a1ef9f1
Add esp32c2 wait example
MabezDev dbd84b3
Add esp32s3 wait example
MabezDev 283b8b7
Add esp32s2 wait example
MabezDev 0cc61a7
Add esp32 wait example
MabezDev 8dd1257
Run fmt
MabezDev 9f7547b
Add example to cargo tomls
MabezDev 92ea1a3
Add top level docs for embassy examples
MabezDev 3c42b09
Mention the higher MSRV for async in the README
jessebraham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//! embassy wait | ||
//! | ||
//! This is an example of asynchronously `Wait`ing for a pin state to change. | ||
|
||
#![no_std] | ||
#![no_main] | ||
#![feature(type_alias_impl_trait)] | ||
|
||
use embassy_executor::Executor; | ||
use embassy_time::{Duration, Timer}; | ||
use embedded_hal_async::digital::Wait; | ||
use esp32_hal::{ | ||
clock::ClockControl, | ||
embassy, | ||
peripherals::Peripherals, | ||
prelude::*, | ||
timer::TimerGroup, | ||
Rtc, | ||
IO, | ||
}; | ||
use esp_backtrace as _; | ||
use esp_hal_common::{Gpio0, Input, PullDown}; | ||
use static_cell::StaticCell; | ||
|
||
#[embassy_executor::task] | ||
async fn ping(mut pin: Gpio0<Input<PullDown>>) { | ||
loop { | ||
esp_println::println!("Waiting..."); | ||
pin.wait_for_rising_edge().await.unwrap(); | ||
esp_println::println!("Ping!"); | ||
Timer::after(Duration::from_millis(100)).await; | ||
} | ||
} | ||
|
||
static EXECUTOR: StaticCell<Executor> = StaticCell::new(); | ||
|
||
#[xtensa_lx_rt::entry] | ||
fn main() -> ! { | ||
esp_println::println!("Init!"); | ||
let peripherals = Peripherals::take(); | ||
let system = peripherals.DPORT.split(); | ||
let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); | ||
|
||
let mut rtc = Rtc::new(peripherals.RTC_CNTL); | ||
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); | ||
let mut wdt0 = timer_group0.wdt; | ||
let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks); | ||
let mut wdt1 = timer_group1.wdt; | ||
|
||
// Disable watchdog timers | ||
rtc.rwdt.disable(); | ||
wdt0.disable(); | ||
wdt1.disable(); | ||
|
||
#[cfg(feature = "embassy-time-timg0")] | ||
embassy::init(&clocks, timer_group0.timer0); | ||
|
||
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); | ||
// GPIO 0 as input | ||
let input = io.pins.gpio0.into_pull_down_input(); | ||
|
||
// Async requires the GPIO interrupt to wake futures | ||
esp32_hal::interrupt::enable( | ||
esp32_hal::peripherals::Interrupt::GPIO, | ||
esp32_hal::interrupt::Priority::Priority1, | ||
) | ||
.unwrap(); | ||
|
||
let executor = EXECUTOR.init(Executor::new()); | ||
executor.run(|spawner| { | ||
spawner.spawn(ping(input)).ok(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MabezDev I'm not entirely sure but I think that
set_int_enable
does not implicitly clear interrupt status bits, it is possible for an edge-triggered listener to wake a task twice (in this call, and at a later GPIO interrupt request).I think it is also possible that we lose GPIO interrupt requests here, because read and clear are not atomic. Probably only those bits should be cleared here that are set in
intrs
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite understand your first point, but I do agree that we should only clear the interrupts read from the status inside the GPIO interrupt handler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am in the process of opening an issue with more data that confirms my suspicions: level-triggered interrupts fire multiple times, it looks like. But I might just have incorrect assumptions here, we'll see :)