#![allow(clippy::empty_loop)]
#![allow(unused)]
#![no_std]
#![no_main]
use panic_halt as _;
use stm32f1xx_hal as hal;
use crate::hal::{
gpio::{gpioc, Output, PushPull},
pac::{interrupt, Interrupt, Peripherals, EXTI},
prelude::*,
rtc::Rtc,
};
use core::cell::RefCell;
use cortex_m::{asm::wfi, interrupt::Mutex};
use cortex_m_rt::entry;
type LedPin = gpioc::PC13<Output<PushPull>>;
static G_LED: Mutex<RefCell<Option<LedPin>>> = Mutex::new(RefCell::new(None));
static G_RTC: Mutex<RefCell<Option<Rtc>>> = Mutex::new(RefCell::new(None));
static G_EXTI: Mutex<RefCell<Option<EXTI>>> = Mutex::new(RefCell::new(None));
const TOGGLE_INTERVAL_SECONDS: u32 = 3;
#[cfg(feature = "stm32f101")]
#[entry]
fn main() -> ! {
loop {
continue;
}
}
#[cfg(not(feature = "stm32f101"))]
#[interrupt]
fn RTCALARM() {
static mut LED: Option<LedPin> = None;
static mut RTC: Option<Rtc> = None;
static mut EXTI: Option<EXTI> = None;
let led = LED.get_or_insert_with(|| {
cortex_m::interrupt::free(|cs| G_LED.borrow(cs).replace(None).unwrap())
});
let rtc = RTC.get_or_insert_with(|| {
cortex_m::interrupt::free(|cs| G_RTC.borrow(cs).replace(None).unwrap())
});
let exti = EXTI.get_or_insert_with(|| {
cortex_m::interrupt::free(|cs| G_EXTI.borrow(cs).replace(None).unwrap())
});
exti.pr.write(|w| w.pr17().set_bit());
rtc.set_alarm(rtc.current_time() + TOGGLE_INTERVAL_SECONDS);
let _ = led.toggle();
}
#[cfg(not(feature = "stm32f101"))]
#[entry]
fn main() -> ! {
let dp = Peripherals::take().unwrap();
let mut pwr = dp.PWR;
let rcc = dp.RCC.constrain();
let mut gpioc = dp.GPIOC.split();
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
let _ = led.set_high();
cortex_m::interrupt::free(|cs| *G_LED.borrow(cs).borrow_mut() = Some(led));
let exti = dp.EXTI;
exti.ftsr.write(|w| w.tr17().set_bit());
exti.imr.write(|w| w.mr17().set_bit());
cortex_m::interrupt::free(|cs| *G_EXTI.borrow(cs).borrow_mut() = Some(exti));
let mut backup_domain = rcc.bkp.constrain(dp.BKP, &mut pwr);
let mut rtc = Rtc::new(dp.RTC, &mut backup_domain);
rtc.set_time(0);
rtc.set_alarm(TOGGLE_INTERVAL_SECONDS);
rtc.listen_alarm();
cortex_m::interrupt::free(|cs| *G_RTC.borrow(cs).borrow_mut() = Some(rtc));
unsafe { cortex_m::peripheral::NVIC::unmask(Interrupt::RTCALARM) };
loop {
wfi();
}
}