Skip to content
This repository was archived by the owner on Aug 9, 2022. It is now read-only.

Commit 0a82f20

Browse files
committed
Extend Timer functionality
1 parent ee3a503 commit 0a82f20

File tree

1 file changed

+130
-36
lines changed

1 file changed

+130
-36
lines changed

src/timer/mod.rs

Lines changed: 130 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,68 @@ pub trait TimerWithInterrupt: CountDown + Periodic + Cancel {
7272

7373
/// Clear interrupt once fired
7474
fn clear_interrupt(&mut self) -> &mut Self;
75+
76+
/// Set timer value
77+
fn set_value<T: Into<TicksU64>>(&mut self, value: T) -> &mut Self;
78+
79+
/// Get timer value
80+
fn get_value(&self) -> TicksU64;
81+
82+
/// Get timer value in ns
83+
fn get_value_in_ns(&self) -> NanoSecondsU64;
84+
85+
/// Get alarm value
86+
fn get_alarm(&self) -> TicksU64;
87+
88+
/// Get alarm value in ns
89+
fn get_alarm_in_ns(&self) -> NanoSecondsU64;
90+
91+
/// Set alarm value
92+
///
93+
/// *Note: timer is not disabled, so there is a risk for false triggering between
94+
/// setting upper and lower 32 bits.*
95+
fn set_alarm<T: Into<TicksU64>>(&mut self, value: T) -> &mut Self;
96+
97+
/// Set alarm value in ns
98+
///
99+
/// *Note: timer is not disabled, so there is a risk for false triggering between
100+
/// setting upper and lower 32 bits.*
101+
fn set_alarm_in_ns<T: Into<NanoSecondsU64>>(&mut self, value: T) -> &mut Self;
102+
103+
/// Enable or disables the timer
104+
fn enable(&mut self, enable: bool) -> &mut Self;
105+
106+
/// Enable or disables the timer
107+
fn is_enabled(&mut self) -> bool;
108+
109+
/// Stop the timer
110+
fn stop(&mut self);
111+
112+
/// Set to true to increase the timer value on each tick, set to false to decrease
113+
/// the timer value on each tick.
114+
fn increasing(&mut self, enable: bool) -> &mut Self;
115+
116+
/// Returns true if the timer is increasing, otherwise decreasing
117+
fn is_increasing(&mut self) -> bool;
118+
119+
/// Set to true if the timer needs to be reloaded to initial value once the alarm
120+
/// is reached.
121+
fn auto_reload(&mut self, enable: bool) -> &mut Self;
122+
123+
/// Enable alarm triggering
124+
fn enable_alarm(&mut self, enable: bool) -> &mut Self;
125+
126+
/// Is the alarm active
127+
fn alarm_active(&mut self) -> bool;
128+
129+
/// Set clock divider.
130+
///
131+
/// Value must be between 2 and 65536 inclusive for Timer 0 and 1 and
132+
/// between 3 and 65535 for TimerLact.
133+
fn set_divider(&mut self, divider: u32) -> Result<&mut Self, Error>;
134+
135+
/// Get the current clock divider
136+
fn get_divider(&self) -> u32;
75137
}
76138

77139
impl<TIMG: TimerGroup> Timer<TIMG, Timer0> {
@@ -197,11 +259,8 @@ macro_rules! timer {
197259
}
198260
self
199261
}
200-
}
201-
202-
impl<TIMG: TimerGroup> Timer<TIMG, $TIMX> {
203262
/// Set timer value
204-
pub fn set_value<T: Into<TicksU64>>(&mut self, value: T) -> &mut Self {
263+
fn set_value<T: Into<TicksU64>>(&mut self, value: T) -> &mut Self {
205264
unsafe {
206265
let timg = &*(self.timg);
207266
let value: u64 = value.into().into();
@@ -213,7 +272,7 @@ macro_rules! timer {
213272
}
214273

215274
/// Get timer value
216-
pub fn get_value(&mut self) -> TicksU64 {
275+
fn get_value(&self) -> TicksU64 {
217276
unsafe {
218277
let timg = &*(self.timg);
219278
timg.$UPDATE.write(|w| w.bits(1));
@@ -223,8 +282,13 @@ macro_rules! timer {
223282
}
224283
}
225284

285+
/// Get timer value in ns
286+
fn get_value_in_ns(&self) -> NanoSecondsU64 {
287+
self.get_value() / (self.clock_control_config.apb_frequency() / self.get_divider())
288+
}
289+
226290
/// Get alarm value
227-
pub fn get_alarm(&mut self) -> TicksU64 {
291+
fn get_alarm(&self) -> TicksU64 {
228292
unsafe {
229293
let timg = &*(self.timg);
230294
TicksU64(
@@ -234,39 +298,54 @@ macro_rules! timer {
234298
}
235299
}
236300

301+
/// Get alarm value in ns
302+
fn get_alarm_in_ns(&self) -> NanoSecondsU64 {
303+
self.get_alarm() / (self.clock_control_config.apb_frequency() / self.get_divider())
304+
}
305+
237306
/// Set alarm value
238307
///
239308
/// *Note: timer is not disabled, so there is a risk for false triggering between
240309
/// setting upper and lower 32 bits.*
241-
pub fn set_alarm(&mut self, value: TicksU64) -> &mut Self {
310+
fn set_alarm<T: Into<TicksU64>>(&mut self, value: T) -> &mut Self {
242311
unsafe {
243312
let timg = &*(self.timg);
244-
let value: u64 = value.into();
313+
let value = value.into() / TicksU64(1);
245314
timg.$ALARM_HI.write(|w| w.bits((value >> 32) as u32));
246315
timg.$ALARM_LO.write(|w| w.bits(value as u32));
247316
}
248317
self
249318
}
250319

320+
/// Set alarm value in ns
321+
///
322+
/// *Note: timer is not disabled, so there is a risk for false triggering between
323+
/// setting upper and lower 32 bits.*
324+
fn set_alarm_in_ns<T: Into<NanoSecondsU64>>(&mut self, value: T) -> &mut Self {
325+
self.set_alarm(
326+
self.clock_control_config.apb_frequency() / self.get_divider() * value.into(),
327+
)
328+
}
329+
251330
/// Enable or disables the timer
252-
pub fn enable(&mut self, enable: bool) -> &mut Self {
331+
fn enable(&mut self, enable: bool) -> &mut Self {
253332
unsafe { (*(self.timg)).$CONFIG.modify(|_, w| w.$EN().bit(enable)) }
254333
self
255334
}
256335

257336
/// Enable or disables the timer
258-
pub fn is_enabled(&mut self) -> bool {
337+
fn is_enabled(&mut self) -> bool {
259338
unsafe { (*(self.timg)).$CONFIG.read().$EN().bit_is_set() }
260339
}
261340

262341
/// Stop the timer
263-
pub fn stop(&mut self) {
342+
fn stop(&mut self) {
264343
self.enable(false);
265344
}
266345

267346
/// Set to true to increase the timer value on each tick, set to false to decrease
268347
/// the timer value on each tick.
269-
pub fn increasing(&mut self, enable: bool) -> &mut Self {
348+
fn increasing(&mut self, enable: bool) -> &mut Self {
270349
unsafe {
271350
(*(self.timg))
272351
.$CONFIG
@@ -276,13 +355,13 @@ macro_rules! timer {
276355
}
277356

278357
/// Returns true if the timer is increasing, otherwise decreasing
279-
pub fn is_increasing(&mut self) -> bool {
358+
fn is_increasing(&mut self) -> bool {
280359
unsafe { (*(self.timg)).$CONFIG.read().$INCREASE().bit_is_set() }
281360
}
282361

283362
/// Set to true if the timer needs to be reloaded to initial value once the alarm
284363
/// is reached.
285-
pub fn auto_reload(&mut self, enable: bool) -> &mut Self {
364+
fn auto_reload(&mut self, enable: bool) -> &mut Self {
286365
unsafe {
287366
(*(self.timg))
288367
.$CONFIG
@@ -291,24 +370,7 @@ macro_rules! timer {
291370
self
292371
}
293372

294-
fn enable_edge_interrupt(&mut self, enable: bool) -> &mut Self {
295-
unsafe {
296-
(*(self.timg))
297-
.$CONFIG
298-
.modify(|_, w| w.$EDGE_INT_EN().bit(enable))
299-
}
300-
self
301-
}
302-
303-
fn enable_level_interrupt(&mut self, enable: bool) -> &mut Self {
304-
unsafe {
305-
(*(self.timg))
306-
.$CONFIG
307-
.modify(|_, w| w.$LEVEL_INT_EN().bit(enable))
308-
}
309-
self
310-
}
311-
373+
/// Enable alarm triggering
312374
fn enable_alarm(&mut self, enable: bool) -> &mut Self {
313375
unsafe {
314376
(*(self.timg))
@@ -318,15 +380,16 @@ macro_rules! timer {
318380
self
319381
}
320382

383+
/// Is the alarm active
321384
fn alarm_active(&mut self) -> bool {
322-
unsafe { (*(self.timg)).$CONFIG.read().$ALARM_EN().bit_is_set() }
385+
unsafe { (*(self.timg)).$CONFIG.read().$ALARM_EN().bit_is_clear() }
323386
}
324387

325388
/// Set clock divider.
326389
///
327390
/// Value must be between 2 and 65536 inclusive for Timer 0 and 1 and
328391
/// between 3 and 65535 for TimerLact.
329-
pub fn set_divider(&mut self, divider: u32) -> Result<&mut Self, Error> {
392+
fn set_divider(&mut self, divider: u32) -> Result<&mut Self, Error> {
330393
if divider < $MIN_DIV || divider > $MAX_DIV {
331394
return Err(Error::OutOfRange);
332395
}
@@ -340,7 +403,38 @@ macro_rules! timer {
340403

341404
Ok(self)
342405
}
406+
407+
/// Get the current clock divider
408+
fn get_divider(&self) -> u32 {
409+
let divider = unsafe { (*(self.timg)).$CONFIG.read().$DIVIDER().bits() };
410+
if (divider == 0) {
411+
65536
412+
} else {
413+
divider as u32
414+
}
415+
}
343416
}
417+
418+
impl<TIMG: TimerGroup> Timer<TIMG, $TIMX> {
419+
fn enable_edge_interrupt(&mut self, enable: bool) -> &mut Self {
420+
unsafe {
421+
(*(self.timg))
422+
.$CONFIG
423+
.modify(|_, w| w.$EDGE_INT_EN().bit(enable))
424+
}
425+
self
426+
}
427+
428+
fn enable_level_interrupt(&mut self, enable: bool) -> &mut Self {
429+
unsafe {
430+
(*(self.timg))
431+
.$CONFIG
432+
.modify(|_, w| w.$LEVEL_INT_EN().bit(enable))
433+
}
434+
self
435+
}
436+
}
437+
344438
impl<TIMG: TimerGroup> Periodic for Timer<TIMG, $TIMX> {}
345439

346440
impl<TIMG: TimerGroup> CountDown for Timer<TIMG, $TIMX> {
@@ -364,10 +458,10 @@ macro_rules! timer {
364458
/// **Note: if the timeout is handled via an interrupt, this will never return.**
365459
fn wait(&mut self) -> nb::Result<(), void::Void> {
366460
if self.alarm_active() {
367-
Err(nb::Error::WouldBlock)
368-
} else {
369461
self.clear_interrupt();
370462
Ok(())
463+
} else {
464+
Err(nb::Error::WouldBlock)
371465
}
372466
}
373467
}

0 commit comments

Comments
 (0)