Skip to content

Instantly share code, notes, and snippets.

@durka
Last active Jan 28, 2019
Embed
What would you like to do?
static BUTTON: AtomicBool = AtomicBool::new(false);
#[entry]
fn main() {
let mut cp = cortex_m::Peripherals::take().unwrap();
let dp = stm32f30x::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let delay = Delay::new(cp.SYST, clocks);
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
let button = gpioa.pa0.into_floating_input(&mut gpioa.moder, &mut gpioa.pupdr);
cp.NVIC.enable(Interrupt::EXTI0);
unsafe {
dp.SYSCFG.exticr1.write(|w| w.exti0().bits(0)); // EXTI0 source = PA0 (button)
dp.EXTI.imr1.write(|w| w.mr0().set_bit()); // unmask EXTI0
dp.EXTI.ftsr1.write(|w| w.tr0().set_bit()); // trigger on falling edge
cp.NVIC.set_priority(Interrupt::EXTI0, 1);
NVIC::unpend(Interrupt::EXTI0);
}
let mut itm = cp.ITM;
loop {
if BUTTON.load(Ordering::SeqCst) {
iprintln!(&mut itm.stim[0], "Button pushed!");
BUTTON.store(false, Ordering::SeqCst);
}
}
}
interrupt!(EXTI0, button_isr);
fn button_isr() {
BUTTON.store(true, Ordering::SeqCst);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment