-
Notifications
You must be signed in to change notification settings - Fork 168
/
gpio_interrupt.rs
101 lines (85 loc) · 2.46 KB
/
gpio_interrupt.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#![no_std]
#![no_main]
use core::{cell::RefCell, fmt::Write};
use esp32_hal::{
gpio::{Gpio0, 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));
#[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();
button.listen(Event::FallingEdge);
unsafe {
(&SERIAL).lock(|data| (*data).replace(Some(serial0)));
(&BUTTON).lock(|data| (*data).replace(Some(button)));
}
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() {
unsafe {
(&SERIAL).lock(|data| {
let mut serial = data.borrow_mut();
let serial = serial.as_mut().unwrap();
writeln!(serial, "Interrupt").ok();
});
}
interrupt::clear(
Cpu::ProCpu,
interrupt::CpuInterrupt::Interrupt1LevelPriority1,
);
unsafe {
(&BUTTON).lock(|data| {
let mut button = data.borrow_mut();
let button = button.as_mut().unwrap();
button.clear_interrupt();
});
}
}