Skip to content

Commit

Permalink
Add GPIO wait example for C3
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev committed Dec 7, 2022
1 parent 04fec69 commit fd64ecf
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions esp32c3-hal/examples/embassy_wait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use embassy_executor::Executor;
use embassy_time::{Duration, Timer};

use esp32c3_hal::{
clock::ClockControl,
prelude::*,
timer::TimerGroup,
Rtc, embassy, pac::Peripherals, IO,
};
use esp_backtrace as _;
use esp_hal_common::{PullDown, Bank0GpioRegisterAccess, InputOutputAnalogPinType, Input, GpioPin};
use static_cell::StaticCell;

use embedded_hal_async::digital::Wait;

#[embassy_executor::task]
async fn ping(mut pin: GpioPin<Input<PullDown>, Bank0GpioRegisterAccess, InputOutputAnalogPinType, 5>) {
loop {
esp_println::println!("Waiting...");
pin.wait_for_high().await.unwrap();
esp_println::println!("Ping!");
Timer::after(Duration::from_millis(100)).await;
}
}

static EXECUTOR: StaticCell<Executor> = StaticCell::new();

#[riscv_rt::entry]
fn main() -> ! {
esp_println::println!("Init!");
let peripherals = Peripherals::take().unwrap();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
embassy::init(&clocks);

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.swd.disable();
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();

// Set GPIO5 as an output, and set its state high initially.
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let input = io.pins.gpio5.into_pull_down_input();

esp32c3_hal::interrupt::enable(
esp32c3_hal::pac::Interrupt::GPIO,
esp32c3_hal::interrupt::Priority::Priority1,
)
.unwrap();

let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
spawner.spawn(ping(input)).ok();
});
}

0 comments on commit fd64ecf

Please sign in to comment.