@@ -72,6 +72,68 @@ pub trait TimerWithInterrupt: CountDown + Periodic + Cancel {
72
72
73
73
/// Clear interrupt once fired
74
74
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 ;
75
137
}
76
138
77
139
impl < TIMG : TimerGroup > Timer < TIMG , Timer0 > {
@@ -197,11 +259,8 @@ macro_rules! timer {
197
259
}
198
260
self
199
261
}
200
- }
201
-
202
- impl <TIMG : TimerGroup > Timer <TIMG , $TIMX> {
203
262
/// 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 {
205
264
unsafe {
206
265
let timg = & * ( self . timg) ;
207
266
let value: u64 = value. into( ) . into( ) ;
@@ -213,7 +272,7 @@ macro_rules! timer {
213
272
}
214
273
215
274
/// Get timer value
216
- pub fn get_value( & mut self ) -> TicksU64 {
275
+ fn get_value( & self ) -> TicksU64 {
217
276
unsafe {
218
277
let timg = & * ( self . timg) ;
219
278
timg. $UPDATE. write( |w| w. bits( 1 ) ) ;
@@ -223,8 +282,13 @@ macro_rules! timer {
223
282
}
224
283
}
225
284
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
+
226
290
/// Get alarm value
227
- pub fn get_alarm( & mut self ) -> TicksU64 {
291
+ fn get_alarm( & self ) -> TicksU64 {
228
292
unsafe {
229
293
let timg = & * ( self . timg) ;
230
294
TicksU64 (
@@ -234,39 +298,54 @@ macro_rules! timer {
234
298
}
235
299
}
236
300
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
+
237
306
/// Set alarm value
238
307
///
239
308
/// *Note: timer is not disabled, so there is a risk for false triggering between
240
309
/// 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 {
242
311
unsafe {
243
312
let timg = & * ( self . timg) ;
244
- let value: u64 = value. into( ) ;
313
+ let value = value. into( ) / TicksU64 ( 1 ) ;
245
314
timg. $ALARM_HI. write( |w| w. bits( ( value >> 32 ) as u32 ) ) ;
246
315
timg. $ALARM_LO. write( |w| w. bits( value as u32 ) ) ;
247
316
}
248
317
self
249
318
}
250
319
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
+
251
330
/// Enable or disables the timer
252
- pub fn enable( & mut self , enable: bool ) -> & mut Self {
331
+ fn enable( & mut self , enable: bool ) -> & mut Self {
253
332
unsafe { ( * ( self . timg) ) . $CONFIG. modify( |_, w| w. $EN( ) . bit( enable) ) }
254
333
self
255
334
}
256
335
257
336
/// Enable or disables the timer
258
- pub fn is_enabled( & mut self ) -> bool {
337
+ fn is_enabled( & mut self ) -> bool {
259
338
unsafe { ( * ( self . timg) ) . $CONFIG. read( ) . $EN( ) . bit_is_set( ) }
260
339
}
261
340
262
341
/// Stop the timer
263
- pub fn stop( & mut self ) {
342
+ fn stop( & mut self ) {
264
343
self . enable( false ) ;
265
344
}
266
345
267
346
/// Set to true to increase the timer value on each tick, set to false to decrease
268
347
/// 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 {
270
349
unsafe {
271
350
( * ( self . timg) )
272
351
. $CONFIG
@@ -276,13 +355,13 @@ macro_rules! timer {
276
355
}
277
356
278
357
/// Returns true if the timer is increasing, otherwise decreasing
279
- pub fn is_increasing( & mut self ) -> bool {
358
+ fn is_increasing( & mut self ) -> bool {
280
359
unsafe { ( * ( self . timg) ) . $CONFIG. read( ) . $INCREASE( ) . bit_is_set( ) }
281
360
}
282
361
283
362
/// Set to true if the timer needs to be reloaded to initial value once the alarm
284
363
/// is reached.
285
- pub fn auto_reload( & mut self , enable: bool ) -> & mut Self {
364
+ fn auto_reload( & mut self , enable: bool ) -> & mut Self {
286
365
unsafe {
287
366
( * ( self . timg) )
288
367
. $CONFIG
@@ -291,24 +370,7 @@ macro_rules! timer {
291
370
self
292
371
}
293
372
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
312
374
fn enable_alarm( & mut self , enable: bool ) -> & mut Self {
313
375
unsafe {
314
376
( * ( self . timg) )
@@ -318,15 +380,16 @@ macro_rules! timer {
318
380
self
319
381
}
320
382
383
+ /// Is the alarm active
321
384
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 ( ) }
323
386
}
324
387
325
388
/// Set clock divider.
326
389
///
327
390
/// Value must be between 2 and 65536 inclusive for Timer 0 and 1 and
328
391
/// 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 > {
330
393
if divider < $MIN_DIV || divider > $MAX_DIV {
331
394
return Err ( Error :: OutOfRange ) ;
332
395
}
@@ -340,7 +403,38 @@ macro_rules! timer {
340
403
341
404
Ok ( self )
342
405
}
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
+ }
343
416
}
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
+
344
438
impl <TIMG : TimerGroup > Periodic for Timer <TIMG , $TIMX> { }
345
439
346
440
impl <TIMG : TimerGroup > CountDown for Timer <TIMG , $TIMX> {
@@ -364,10 +458,10 @@ macro_rules! timer {
364
458
/// **Note: if the timeout is handled via an interrupt, this will never return.**
365
459
fn wait( & mut self ) -> nb:: Result <( ) , void:: Void > {
366
460
if self . alarm_active( ) {
367
- Err ( nb:: Error :: WouldBlock )
368
- } else {
369
461
self . clear_interrupt( ) ;
370
462
Ok ( ( ) )
463
+ } else {
464
+ Err ( nb:: Error :: WouldBlock )
371
465
}
372
466
}
373
467
}
0 commit comments