#![no_main]
#![no_std]
use panic_halt as _;
use stm32f7xx_hal as hal;
use crate::hal::{
gpio::{self, Output, PushPull},
pac::{interrupt, Interrupt, Peripherals, TIM2},
prelude::*,
timer::{CounterUs, Event},
};
use core::cell::RefCell;
use cortex_m::interrupt::Mutex;
use cortex_m_rt::entry;
type LedPin = gpio::PA5<Output<PushPull>>;
static G_LED: Mutex<RefCell<Option<LedPin>>> = Mutex::new(RefCell::new(None));
static G_TIM: Mutex<RefCell<Option<CounterUs<TIM2>>>> = Mutex::new(RefCell::new(None));
#[interrupt]
fn TIM2() {
static mut LED: Option<LedPin> = None;
static mut TIM: Option<CounterUs<TIM2>> = None;
let led = LED.get_or_insert_with(|| {
cortex_m::interrupt::free(|cs| {
G_LED.borrow(cs).replace(None).unwrap()
})
});
let tim = TIM.get_or_insert_with(|| {
cortex_m::interrupt::free(|cs| {
G_TIM.borrow(cs).replace(None).unwrap()
})
});
let _ = led.toggle();
let _ = tim.wait();
}
#[entry]
fn main() -> ! {
let dp = Peripherals::take().unwrap();
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.sysclk(16.MHz()).pclk1(8.MHz()).freeze();
let gpioa = dp.GPIOA.split();
let mut led = gpioa.pa5.into_push_pull_output();
let _ = led.set_high();
cortex_m::interrupt::free(|cs| *G_LED.borrow(cs).borrow_mut() = Some(led));
let mut timer = dp.TIM2.counter(&clocks);
timer.start(1.secs()).unwrap();
timer.listen(Event::Update);
cortex_m::interrupt::free(|cs| *G_TIM.borrow(cs).borrow_mut() = Some(timer));
unsafe {
cortex_m::peripheral::NVIC::unmask(Interrupt::TIM2);
}
#[allow(clippy::empty_loop)]
loop {
}
}