esp-hal/esp32c2-hal/examples/embassy_wait.rs
Jesse Braham 0064766ef2
No longer publicly expose the PeripheralClockControl struct (#817)
* No longer publicly expose the `PeripheralClockControl` struct

* Update examples as needed to get things building again

* Update CHANGELOG.md

* Address review feedback, fix a warning

* Use a critical section for all devices other than the ESP32-C6/H2, as they modify multiple registers

* Rebase and update `etm` driver to fix build errors
2023-09-26 09:07:57 -07:00

68 lines
1.8 KiB
Rust

//! 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 esp32c2_hal::{
clock::ClockControl,
embassy,
gpio::{Gpio9, Input, PullDown},
peripherals::Peripherals,
prelude::*,
IO,
};
use esp_backtrace as _;
use static_cell::make_static;
#[embassy_executor::task]
async fn ping(mut pin: Gpio9<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;
}
}
#[entry]
fn main() -> ! {
esp_println::println!("Init!");
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
#[cfg(feature = "embassy-time-systick")]
embassy::init(
&clocks,
esp32c2_hal::systimer::SystemTimer::new(peripherals.SYSTIMER),
);
#[cfg(feature = "embassy-time-timg0")]
embassy::init(
&clocks,
esp32c2_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks).timer0,
);
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
// GPIO 9 as input
let input = io.pins.gpio9.into_pull_down_input();
// Async requires the GPIO interrupt to wake futures
esp32c2_hal::interrupt::enable(
esp32c2_hal::peripherals::Interrupt::GPIO,
esp32c2_hal::interrupt::Priority::Priority1,
)
.unwrap();
let executor = make_static!(Executor::new());
executor.run(|spawner| {
spawner.spawn(ping(input)).ok();
});
}