Skip to content

Instantly share code, notes, and snippets.

@m1el
Last active November 3, 2021 15:46
Show Gist options
  • Save m1el/41b2c1c210d92b45ff015483c9dd59d8 to your computer and use it in GitHub Desktop.
Save m1el/41b2c1c210d92b45ff015483c9dd59d8 to your computer and use it in GitHub Desktop.
#![feature(naked_functions)]
#![no_std]
#![no_main]
use esp8266_hal::prelude::*;
use esp8266_hal::interrupt::{enable_interrupt, disable_interrupt, InterruptType};
use esp8266_hal::gpio::InterruptMode;
use esp8266_hal::target::{self, Peripherals};
use core::fmt::Write;
use core::sync::atomic::{AtomicUsize, Ordering};
use panic_halt as _;
static PRESSES: AtomicUsize = AtomicUsize::new(0);
#[interrupt(gpio)]
fn gpio_intr() {
// disable_interrupt(InterruptType::GPIO);
unsafe {
// clear interrupt status on all inputs.
// without this, the interrupt routine is getting repeatedly called
// (*target::GPIO::ptr()).gpio_status_w1tc.write(|w| w.bits(0xffff));
// toggle GPIO16 pin
(*target::RTC::ptr()).rtc_gpio_out.modify(|r, w| w.bits(r.bits() ^ 0x1));
}
PRESSES.store(PRESSES.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
}
#[entry]
fn main() -> ! {
let dp = Peripherals::take().unwrap();
let pins = dp.GPIO.split();
let mut gpio16 = pins.gpio16.into_push_pull_output();
gpio16.set_low();
let mut button = pins.gpio5.into_floating_input();
button.set_interrupt_mode(InterruptMode::PositiveEdge);
let mut serial = dp
.UART0
.serial(pins.gpio1.into_uart(), pins.gpio3.into_uart());
let (mut timer1, _) = dp.TIMER.timers();
let mut last_press = PRESSES.load(Ordering::Relaxed);
enable_interrupt(InterruptType::GPIO);
loop {
timer1.delay_ms(500);
let new_press = PRESSES.load(Ordering::Relaxed);
write!(&mut serial, "ping\r\n").unwrap();
if last_press != new_press {
write!(&mut serial, "new press: {}\r\n", new_press).unwrap();
last_press = new_press;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment