forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tick_timer.rs
88 lines (71 loc) · 2.2 KB
/
tick_timer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Demonstrates a microsecond-scale 64-bit timestamp counter
#![no_main]
#![no_std]
use core::{
cell::RefCell,
sync::atomic::{AtomicU32, Ordering},
};
use cortex_m::{asm, interrupt::Mutex};
use cortex_m_rt::entry;
use log::info;
use pac::interrupt;
use stm32h7xx_hal::{pac, prelude::*, timer};
#[path = "utilities/logger.rs"]
mod logger;
static OVERFLOWS: AtomicU32 = AtomicU32::new(0);
static TIMER: Mutex<RefCell<Option<timer::Timer<pac::TIM2>>>> =
Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
logger::init();
let mut cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
// Constrain and Freeze power
info!("Setup PWR... ");
let pwr = dp.PWR.constrain();
let pwrcfg = pwr.freeze();
// Constrain and Freeze clock
info!("Setup RCC... ");
let rcc = dp.RCC.constrain();
let ccdr = rcc.sys_ck(100.MHz()).freeze(pwrcfg, &dp.SYSCFG);
info!("");
info!("stm32h7xx-hal example - Tick Timer");
info!("");
let mut timer =
dp.TIM2
.tick_timer(1.MHz(), ccdr.peripheral.TIM2, &ccdr.clocks);
timer.listen(timer::Event::TimeOut);
cortex_m::interrupt::free(|cs| {
TIMER.borrow(cs).replace(Some(timer));
});
unsafe {
cp.NVIC.set_priority(interrupt::TIM2, 1);
pac::NVIC::unmask(interrupt::TIM2);
}
loop {
info!("timestamp: {}", timestamp());
asm::delay(1000);
}
}
/// Handle timer overflow and count past 32-bits.
///
/// The interrupt should be configured at maximum priority, it won't take very long.
#[interrupt]
fn TIM2() {
OVERFLOWS.fetch_add(1, core::sync::atomic::Ordering::SeqCst);
cortex_m::interrupt::free(|cs| {
let mut rc = TIMER.borrow(cs).borrow_mut();
let timer = rc.as_mut().unwrap();
timer.clear_irq();
})
}
/// Returns the 64-bit number of microseconds since startup
pub fn timestamp() -> u64 {
let overflows = OVERFLOWS.load(Ordering::SeqCst) as u64;
let ctr = cortex_m::interrupt::free(|cs| {
let rc = TIMER.borrow(cs).borrow();
let timer = rc.as_ref().unwrap();
timer.counter() as u64
});
(overflows << 32) + ctr
}