#![no_main] #![no_std]
extern crate cortex_m; #[macro_use(entry, exception)] extern crate cortex_m_rt; extern crate cortex_m_semihosting; extern crate panic_semihosting; extern crate stm32f103xx_hal as bluepill_hal; #[macro_use]
extern crate stm32f103xx;
extern crate nb;
extern crate embedded_hal;
extern crate pid_control;
extern crate qei;
use cortex_m::Peripherals as CortexPeripherals;
use bluepill_hal::prelude::*; use bluepill_hal::qei::Qei;
use bluepill_hal::stm32f103xx as f103;
use bluepill_hal::stm32f103xx::Peripherals;
use core::fmt::Write; use cortex_m_rt::ExceptionFrame; use cortex_m_semihosting::hio;
use f103::Interrupt;
use qei::QeiManager;
entry!(main);
type QeiPins = (
bluepill_hal::gpio::gpiob::PB6<bluepill_hal::gpio::Input<bluepill_hal::gpio::Floating>>,
bluepill_hal::gpio::gpiob::PB7<bluepill_hal::gpio::Input<bluepill_hal::gpio::Floating>>,
);
static mut QEIM: Option<QeiManager<Qei<bluepill_hal::stm32f103xx::TIM4, QeiPins>>> = None;
fn tim2_interrupt() {
unsafe {
(*f103::TIM2::ptr()).sr.modify(|_, w| w.uif().clear_bit());
QEIM.as_mut().unwrap().sample_unwrap();
};
}
fn main() -> ! {
let mut debug_out = hio::hstdout().unwrap();
let bluepill = Peripherals::take().unwrap();
let cortex = CortexPeripherals::take().unwrap();
let mut nvic = cortex.NVIC;
let mut rcc = bluepill.RCC.constrain();
let mut flash = bluepill.FLASH.constrain();
let mut afio = bluepill.AFIO.constrain(&mut rcc.apb2);
let clocks = rcc.cfgr.freeze(&mut flash.acr);
let gpiob = bluepill.GPIOB.split(&mut rcc.apb2);
let pb6 = gpiob.pb6;
let pb7 = gpiob.pb7;
let mut tim2 =
bluepill_hal::timer::Timer::tim2(bluepill.TIM2, 1000.hz(), clocks, &mut rcc.apb1);
let qei = Qei::tim4(bluepill.TIM4, (pb6, pb7), &mut afio.mapr, &mut rcc.apb1);
unsafe {
QEIM = Some(QeiManager::new(qei));
}
nvic.enable(Interrupt::TIM2);
tim2.listen(bluepill_hal::timer::Event::Update);
loop {
writeln!(
debug_out,
"Count : {}",
unsafe { QEIM.as_mut().unwrap().count()},
)
.unwrap();
}
}
interrupt!(TIM2, tim2_interrupt);
exception!(HardFault, hard_fault);
fn hard_fault(ef: &ExceptionFrame) -> ! {
panic!("Hard fault: {:#?}", ef);
}
exception!(*, default_handler);
fn default_handler(irqn: i16) {
panic!("Unhandled exception (IRQn = {})", irqn);
}