#![no_main]
#![no_std]
use panic_halt as _;
use gd32f1x0_hal::{
gpio::{gpioc, Output, PushPull},
pac::{interrupt, Interrupt, Peripherals, TIMER1},
prelude::*,
timer::{CountDownTimer, Event, Timer},
};
use core::cell::RefCell;
use cortex_m::{asm::wfi, interrupt::Mutex};
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
type LedPin = gpioc::PC13<Output<PushPull>>;
static G_LED: Mutex<RefCell<Option<LedPin>>> = Mutex::new(RefCell::new(None));
static G_TIM: Mutex<RefCell<Option<CountDownTimer<TIMER1>>>> = Mutex::new(RefCell::new(None));
#[interrupt]
fn TIMER1() {
static mut LED: Option<LedPin> = None;
static mut TIM: Option<CountDownTimer<TIMER1>> = 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 mut flash = dp.FMC.constrain();
let mut rcu = dp.RCU.constrain();
let clocks = rcu
.cfgr
.sysclk(8.mhz())
.pclk1(8.mhz())
.freeze(&mut flash.ws);
let mut gpioc = dp.GPIOC.split(&mut rcu.ahb);
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.config);
let _ = led.set_high();
cortex_m::interrupt::free(|cs| *G_LED.borrow(cs).borrow_mut() = Some(led));
let mut timer = Timer::timer1(dp.TIMER1, &clocks, &mut rcu.apb1).start_count_down(1.hz());
timer.listen(Event::Update);
cortex_m::interrupt::free(|cs| *G_TIM.borrow(cs).borrow_mut() = Some(timer));
unsafe {
cortex_m::peripheral::NVIC::unmask(Interrupt::TIMER1);
}
loop {
wfi();
}
}