#![no_std]
#![no_main]
use panic_halt as _;
use rp2040_hal as hal;
use hal::pac;
use embedded_hal::digital::v2::ToggleableOutputPin;
use hal::pac::interrupt;
use core::cell::RefCell;
use critical_section::Mutex;
use rp2040_hal::gpio;
use rp2040_hal::gpio::Interrupt::EdgeLow;
#[link_section = ".boot2"]
#[used]
pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_GENERIC_03H;
const XTAL_FREQ_HZ: u32 = 12_000_000u32;
type LedPin = gpio::Pin<gpio::bank0::Gpio25, gpio::PushPullOutput>;
type ButtonPin = gpio::Pin<gpio::bank0::Gpio26, gpio::PullUpInput>;
type LedAndButton = (LedPin, ButtonPin);
static GLOBAL_PINS: Mutex<RefCell<Option<LedAndButton>>> = Mutex::new(RefCell::new(None));
#[rp2040_hal::entry]
fn main() -> ! {
let mut pac = pac::Peripherals::take().unwrap();
let mut watchdog = hal::Watchdog::new(pac.WATCHDOG);
let _clocks = hal::clocks::init_clocks_and_plls(
XTAL_FREQ_HZ,
pac.XOSC,
pac.CLOCKS,
pac.PLL_SYS,
pac.PLL_USB,
&mut pac.RESETS,
&mut watchdog,
)
.ok()
.unwrap();
let sio = hal::Sio::new(pac.SIO);
let pins = hal::gpio::Pins::new(
pac.IO_BANK0,
pac.PADS_BANK0,
sio.gpio_bank0,
&mut pac.RESETS,
);
let led = pins.gpio25.into_mode();
let in_pin = pins.gpio26.into_mode();
in_pin.set_interrupt_enabled(EdgeLow, true);
critical_section::with(|cs| {
GLOBAL_PINS.borrow(cs).replace(Some((led, in_pin)));
});
unsafe {
pac::NVIC::unmask(pac::Interrupt::IO_IRQ_BANK0);
}
loop {
cortex_m::asm::wfi();
}
}
#[interrupt]
fn IO_IRQ_BANK0() {
static mut LED_AND_BUTTON: Option<LedAndButton> = None;
if LED_AND_BUTTON.is_none() {
critical_section::with(|cs| {
*LED_AND_BUTTON = GLOBAL_PINS.borrow(cs).take();
});
}
if let Some(gpios) = LED_AND_BUTTON {
let (led, button) = gpios;
if button.interrupt_status(EdgeLow) {
let _ = led.toggle();
button.clear_interrupt(EdgeLow);
}
}
}