Skip to content

Instantly share code, notes, and snippets.

@MabezDev
Created May 15, 2023 16:13
Show Gist options
  • Save MabezDev/624738016d8ee67857b96e12253f656f to your computer and use it in GitHub Desktop.
Save MabezDev/624738016d8ee67857b96e12253f656f to your computer and use it in GitHub Desktop.
//! 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,
cpu_control::CpuControl,
embassy,
gpio::{Gpio0, Input, PullDown},
peripherals::Peripherals,
prelude::*,
timer::TimerGroup,
Rtc,
IO,
};
use esp_backtrace as _;
use esp_println::println;
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!");
esp_println::logger::init_logger_from_env();
let peripherals = Peripherals::take();
let system = peripherals.DPORT.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let mut cpu_control = CpuControl::new(system.cpu_control);
let mut cpu1_fnctn = || {
let peripherals = unsafe { Peripherals::steal() };
let mut system = peripherals.DPORT.split();
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();
println!("Init embassy... on core {:?}", esp32_hal::get_core());
#[cfg(feature = "embassy-time-systick")]
embassy::init(
&clocks,
esp32_hal::systimer::SystemTimer::new(peripherals.SYSTIMER),
);
#[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();
println!("Enable GPIO");
// 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| {
println!("Spawning task");
spawner.spawn(ping(input)).ok();
});
};
println!("About to start CPU1");
let _guard = cpu_control.start_app_core(&mut cpu1_fnctn).unwrap();
println!("Started CPU1");
loop {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment