Skip to content

Instantly share code, notes, and snippets.

@bjoernQ
Created April 19, 2022 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoernQ/fb473b7965e1ae6bfc1979c9ed90a90b to your computer and use it in GitHub Desktop.
Save bjoernQ/fb473b7965e1ae6bfc1979c9ed90a90b to your computer and use it in GitHub Desktop.
ESP32 ESP-HAL 3 BUTTON INTERRUPTS
#![no_std]
#![no_main]
use core::{cell::RefCell, fmt::Write};
use esp32_hal::{
gpio::{Gpio0, Gpio5, Gpio12, IO},
pac::{self, Peripherals, UART0},
prelude::*,
Delay,
RtcCntl,
Serial,
Timer,
};
use esp_hal_common::{
gpio::{Event, Pin},
interrupt,
Cpu,
Input,
PullDown,
};
use panic_halt as _;
use xtensa_lx::mutex::{Mutex, SpinLockMutex};
use xtensa_lx_rt::entry;
static mut SERIAL: SpinLockMutex<RefCell<Option<Serial<UART0>>>> =
SpinLockMutex::new(RefCell::new(None));
static mut BUTTON: SpinLockMutex<RefCell<Option<Gpio0<Input<PullDown>>>>> =
SpinLockMutex::new(RefCell::new(None));
static mut BUTTON2: SpinLockMutex<RefCell<Option<Gpio5<Input<PullDown>>>>> =
SpinLockMutex::new(RefCell::new(None));
static mut BUTTON3: SpinLockMutex<RefCell<Option<Gpio12<Input<PullDown>>>>> =
SpinLockMutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take().unwrap();
// Disable the TIMG watchdog timer.
let mut timer0 = Timer::new(peripherals.TIMG0);
let serial0 = Serial::new(peripherals.UART0).unwrap();
let mut rtc_cntl = RtcCntl::new(peripherals.RTC_CNTL);
// Disable MWDT and RWDT (Watchdog) flash boot protection
timer0.disable();
rtc_cntl.set_wdt_global_enable(false);
// Set GPIO15 as an output, and set its state high initially.
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let mut led = io.pins.gpio15.into_push_pull_output();
let mut button = io.pins.gpio0.into_pull_down_input();
let mut button2 = io.pins.gpio5.into_pull_down_input();
let mut button3 = io.pins.gpio12.into_pull_down_input();
button.listen(Event::FallingEdge);
button2.listen(Event::FallingEdge);
button3.listen(Event::FallingEdge);
unsafe {
(&SERIAL).lock(|data| (*data).replace(Some(serial0)));
(&BUTTON).lock(|data| (*data).replace(Some(button)));
(&BUTTON2).lock(|data| (*data).replace(Some(button2)));
(&BUTTON3).lock(|data| (*data).replace(Some(button3)));
}
interrupt::enable(
Cpu::ProCpu,
pac::Interrupt::GPIO,
interrupt::CpuInterrupt::Interrupt1LevelPriority1,
);
led.set_high().unwrap();
// Initialize the Delay peripheral, and use it to toggle the LED state in a
// loop.
let mut delay = Delay::new();
unsafe {
xtensa_lx::interrupt::enable_mask(
1 << 1
);
}
loop {
led.toggle().unwrap();
delay.delay_ms(500u32);
}
}
#[no_mangle]
pub fn level1_interrupt() {
interrupt::clear(
Cpu::ProCpu,
interrupt::CpuInterrupt::Interrupt1LevelPriority1,
);
unsafe {
(&BUTTON).lock(|data| {
let mut button = data.borrow_mut();
let button = button.as_mut().unwrap();
if button.is_pcore_interrupt_set() {
button.clear_interrupt();
(&SERIAL).lock(|data| {
let mut serial = data.borrow_mut();
let serial = serial.as_mut().unwrap();
writeln!(serial, "Interrupt Button 1").ok();
});
}
});
(&BUTTON2).lock(|data| {
let mut button = data.borrow_mut();
let button = button.as_mut().unwrap();
if button.is_pcore_interrupt_set() {
button.clear_interrupt();
(&SERIAL).lock(|data| {
let mut serial = data.borrow_mut();
let serial = serial.as_mut().unwrap();
writeln!(serial, "Interrupt Button 2").ok();
});
}
});
(&BUTTON3).lock(|data| {
let mut button = data.borrow_mut();
let button = button.as_mut().unwrap();
if button.is_pcore_interrupt_set() {
button.clear_interrupt();
(&SERIAL).lock(|data| {
let mut serial = data.borrow_mut();
let serial = serial.as_mut().unwrap();
writeln!(serial, "Interrupt Button 3").ok();
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment