Skip to content

Commit 16c255f

Browse files
Jonathan Pallant (42 Technology)Yatekii
authored andcommitted
Add Timer 'read()' and TICKS_PER_SECOND.
1 parent 3335ba2 commit 16c255f

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

nrf52-hal-common/src/timer.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ use crate::target::{TIMER3, TIMER4};
2222
///
2323
/// Right now, this is a very basic interface. The timer will always be
2424
/// hardcoded to a frequency of 1 MHz and 32 bits accuracy.
25+
///
26+
/// CC[0] is used for the current/most-recent delay period and CC[1] is used
27+
/// to grab the current value of the counter at a given instant.
2528
pub struct Timer<T>(T);
2629

2730
impl<T> Timer<T>
2831
where
2932
T: Instance,
3033
{
34+
pub const TICKS_PER_SECOND: u32 = 1_000_000;
35+
3136
pub fn new(timer: T) -> Self {
3237
timer
3338
.shorts
@@ -45,6 +50,12 @@ where
4550
self.0
4651
}
4752

53+
/// Return the current value of the counter, by capturing to CC[1].
54+
pub fn read(&self) -> u32 {
55+
self.0.tasks_capture[1].write(|w| w.tasks_capture().trigger());
56+
self.0.cc[1].read().bits()
57+
}
58+
4859
/// Enables the interrupt for this timer
4960
///
5061
/// Enables an interrupt that is fired when the timer reaches the value that

0 commit comments

Comments
 (0)