Skip to content
Snippets Groups Projects
Commit e5385e11 authored by Per Lindgren's avatar Per Lindgren
Browse files

rtfm-blinky systick

parent 5d5fb65b
No related branches found
No related tags found
No related merge requests found
......@@ -37,6 +37,6 @@ pub mod rcc;
pub mod serial;
// pub mod spi;
pub mod time;
// pub mod timer;
pub mod timer;
pub mod led;
//! Timers
use cortex_m::peripheral::SYST;
use cortex_m::peripheral::syst::SystClkSource;
use cast::{u16, u32};
use hal::timer::{CountDown, Periodic};
use nb;
use stm32f30x::{TIM2, TIM3, TIM4, TIM6, TIM7};
use stm32f4x::{TIM2, TIM3, TIM4, TIM6, TIM7};
use rcc::{APB1, Clocks};
use time::Hertz;
......@@ -21,6 +23,64 @@ pub enum Event {
TimeOut,
}
impl Timer<SYST> {
/// start systic timer
pub fn syst<T>(mut syst: SYST, timeout: T, clocks: Clocks) -> Self
where
T: Into<Hertz>,
{
syst.set_clock_source(SystClkSource::Core);
let mut timer = Timer {
tim: syst,
clocks: clocks,
timeout: Hertz(0),
};
timer.start(timeout);
timer
}
/// Starts listening for an `event`
pub fn listen(&mut self, event: Event) {
match event {
Event::TimeOut => self.tim.enable_interrupt(),
}
}
/// Stops listening for an `event`
pub fn unlisten(&mut self, event: Event) {
match event {
Event::TimeOut => self.tim.disable_interrupt(),
}
}
}
impl CountDown for Timer<SYST> {
type Time = Hertz;
fn start<T>(&mut self, timeout: T)
where
T: Into<Hertz>,
{
let rvr = self.clocks.sysclk().0 / timeout.into().0 - 1;
assert!(rvr < (1 << 24));
self.tim.set_reload(rvr);
self.tim.clear_current();
self.tim.enable_counter();
}
fn wait(&mut self) -> nb::Result<(), !> {
if self.tim.has_wrapped() {
Ok(())
} else {
Err(nb::Error::WouldBlock)
}
}
}
impl Periodic for Timer<SYST> {}
macro_rules! hal {
($($TIM:ident: ($tim:ident, $timXen:ident, $timXrst:ident),)+) => {
$(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment