-
Notifications
You must be signed in to change notification settings - Fork 3
/
btn_toggle.rs
57 lines (41 loc) · 1.26 KB
/
btn_toggle.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
#![no_main]
#![no_std]
use core::{cell::RefCell, sync::atomic::{Ordering, AtomicU16}};
use cortex_m::{interrupt::Mutex, asm::wfi};
use cortex_m_rt::entry;
use panic_probe as _;
use stm32f4xx_hal::{delay::Delay, prelude::*, pac, interrupt};
use btn::*;
static G_BUTTON: Mutex<RefCell<Option<BtnPin>>> = Mutex::new(RefCell::new(None));
static CTR: AtomicU16 = AtomicU16::new(0);
#[entry]
fn main() -> ! {
let mut setup = setup_board_minimal().unwrap();
let mut led = setup.gpioa.pa5.into_push_pull_output();
cortex_m::interrupt::free(|cs| {
let button = Button::new();
button.init(&cs, &mut setup.exti, setup.gpioc, &mut setup.syscfg);
*G_BUTTON.borrow(cs).borrow_mut() = Some(button.into_inner(&cs).unwrap())
});
unsafe {
cortex_m::peripheral::NVIC::unmask(pac::Interrupt::EXTI15_10);
}
loop {
if CTR.load(Ordering::SeqCst) % 2 == 0 {
led.set_high();
}
else{
led.set_low();
}
wfi();
}
}
#[interrupt]
fn EXTI15_10() {
static mut BUTTON: Option<BtnPin> = None;
let button = steal!(BUTTON, G_BUTTON);
cortex_m::interrupt::free(|_cs| {
button.clear_interrupt_pending_bit();
});
let _ = CTR.fetch_add(1, Ordering::SeqCst);
}