esp-hal/esp32s2-hal/examples/embassy_wait.rs
Juraj Sadel 40bf086a0c
Add PeripheralClockControl argument to timg, wdt, sha, usb-serial-jtag and uart constructors (#463)
* PeripheralClockControl timer

* Add PeripheralClockControl to timg, wdt, sha, usb-serial-jtag and uart

* ESP32 updated examples

* ESP32C2 updated examples

* ESP32C3 updated examples

* ESP32S2 updated examples

* ESP32S3 updated examples

* ESP32C6 updated examples

* cargo fmt
2023-03-31 09:51:34 +02:00

82 lines
2.1 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 esp32s2_hal::{
clock::ClockControl,
embassy,
gpio::{Gpio0, Input, PullDown},
peripherals::Peripherals,
prelude::*,
timer::TimerGroup,
Rtc,
IO,
};
use esp_backtrace as _;
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();
#[entry]
fn main() -> ! {
esp_println::println!("Init!");
let peripherals = Peripherals::take();
let mut system = peripherals.SYSTEM.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,
&mut system.peripheral_clock_control,
);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(
peripherals.TIMG1,
&clocks,
&mut system.peripheral_clock_control,
);
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
esp32s2_hal::interrupt::enable(
esp32s2_hal::peripherals::Interrupt::GPIO,
esp32s2_hal::interrupt::Priority::Priority1,
)
.unwrap();
let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
spawner.spawn(ping(input)).ok();
});
}