#![no_main]
#![no_std]
extern crate cortex_m;
extern crate cortex_m_rt;
extern crate panic_halt;
#[macro_use]
extern crate stm32f0xx_hal as hal;
use hal::delay::Delay;
use hal::gpio::*;
use hal::prelude::*;
use cortex_m::interrupt::Mutex;
use cortex_m::peripheral::Peripherals as c_m_Peripherals;
use cortex_m_rt::entry;
pub use hal::stm32;
pub use hal::stm32::*;
use core::cell::RefCell;
use core::ops::DerefMut;
static LED: Mutex<RefCell<Option<gpiob::PB3<Output<PushPull>>>>> = Mutex::new(RefCell::new(None));
static DELAY: Mutex<RefCell<Option<Delay>>> = Mutex::new(RefCell::new(None));
static INT: Mutex<RefCell<Option<EXTI>>> = Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
if let (Some(p), Some(cp)) = (Peripherals::take(), c_m_Peripherals::take()) {
let gpiob = p.GPIOB.split();
let syscfg = p.SYSCFG_COMP;
let exti = p.EXTI;
let mut rcc = p.RCC;
rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit());
let _ = gpiob.pb1.into_pull_down_input();
let mut led = gpiob.pb3.into_push_pull_output();
led.set_low();
let clocks = rcc.constrain().cfgr.sysclk(8.mhz()).freeze();
let mut delay = Delay::new(cp.SYST, clocks);
syscfg
.syscfg_exticr1
.modify(|_, w| unsafe { w.exti1().bits(1) });
exti.imr.modify(|_, w| w.mr1().set_bit());
exti.rtsr.modify(|_, w| w.tr1().set_bit());
cortex_m::interrupt::free(move |cs| {
*LED.borrow(cs).borrow_mut() = Some(led);
*DELAY.borrow(cs).borrow_mut() = Some(delay);
*INT.borrow(cs).borrow_mut() = Some(exti);
});
let mut nvic = cp.NVIC;
nvic.enable(Interrupt::EXTI0_1);
unsafe { nvic.set_priority(Interrupt::EXTI0_1, 1) };
cortex_m::peripheral::NVIC::unpend(Interrupt::EXTI0_1);
}
loop {
continue;
}
}
interrupt!(EXTI0_1, button_press);
fn button_press() {
cortex_m::interrupt::free(|cs| {
if let (&mut Some(ref mut led), &mut Some(ref mut delay), &mut Some(ref mut exti)) = (
LED.borrow(cs).borrow_mut().deref_mut(),
DELAY.borrow(cs).borrow_mut().deref_mut(),
INT.borrow(cs).borrow_mut().deref_mut(),
) {
led.set_high();
delay.delay_ms(1_000_u16);
led.set_low();
exti.pr.modify(|_, w| w.pif1().set_bit());
}
});
}