File tree Expand file tree Collapse file tree 2 files changed +15
-8
lines changed Expand file tree Collapse file tree 2 files changed +15
-8
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ use void::Void;
16
16
use crate :: hal:: timer:: { CountDown , Periodic } ;
17
17
use crate :: pac:: { Interrupt , RCC } ;
18
18
use crate :: rcc:: { Clocks , APB1 , APB2 } ;
19
- use crate :: time:: rate:: * ;
19
+ use crate :: time:: { duration , fixed_point :: FixedPoint , rate:: Hertz } ;
20
20
21
21
pub mod interrupts;
22
22
@@ -224,21 +224,23 @@ impl<TIM> CountDown for Timer<TIM>
224
224
where
225
225
TIM : Instance ,
226
226
{
227
- type Time = Hertz ;
227
+ type Time = duration :: Generic < u32 > ;
228
228
229
229
fn start < T > ( & mut self , timeout : T )
230
230
where
231
231
T : Into < Self :: Time > ,
232
232
{
233
233
self . stop ( ) ;
234
234
235
- let ticks = TIM :: clock ( & self . clocks ) . 0 / timeout. into ( ) . 0 ;
236
- let psc = crate :: unwrap!( u16 :: try_from( ( ticks - 1 ) / ( 1 << 16 ) ) . ok( ) ) ;
235
+ let timeout: Self :: Time = timeout. into ( ) ;
236
+ let clock = TIM :: clock ( & self . clocks ) ;
237
+
238
+ let ticks = clock. integer ( ) * * timeout. scaling_factor ( ) * timeout. integer ( ) ;
237
239
240
+ let psc = crate :: unwrap!( u16 :: try_from( ( ticks - 1 ) / ( 1 << 16 ) ) . ok( ) ) ;
238
241
self . tim . set_psc ( psc) ;
239
242
240
243
let arr = crate :: unwrap!( u16 :: try_from( ticks / u32 :: from( psc + 1 ) ) . ok( ) ) ;
241
-
242
244
self . tim . set_arr ( arr) ;
243
245
244
246
// Ensure that the below procedure does not create an unexpected interrupt.
Original file line number Diff line number Diff line change @@ -71,15 +71,20 @@ mod tests {
71
71
let freqcyc = state. mono_timer . frequency ( ) . integer ( ) ;
72
72
73
73
let instant = state. mono_timer . now ( ) ;
74
- timer. start ( 1 . Hz ( ) ) ;
74
+ timer. start ( 1 . seconds ( ) ) ;
75
75
assert ! ( !timer. is_event_triggered( Event :: Update ) ) ;
76
76
unwrap ! ( nb:: block!( timer. wait( ) ) . ok( ) ) ;
77
77
let elapsed = instant. elapsed ( ) ;
78
78
79
79
defmt:: info!( "elapsed: {}" , elapsed) ;
80
80
// TODO: Test multiple frequencies and change the cycle counter window to a percentage
81
81
// to measure the overhead of start?
82
- assert ! ( ( ( freqcyc - 1000 ) ..( freqcyc + 1000 ) ) . contains( & elapsed) ) ;
82
+
83
+ // Differentiate between releas and debug build.
84
+ #[ cfg( not( debug_assertions) ) ]
85
+ assert ! ( ( ( freqcyc - 500 ) ..( freqcyc + 500 ) ) . contains( & elapsed) ) ;
86
+ #[ cfg( debug_assertions) ]
87
+ assert ! ( ( ( freqcyc - 2000 ) ..( freqcyc + 2000 ) ) . contains( & elapsed) ) ;
83
88
84
89
state. timer = Some ( timer) ;
85
90
}
@@ -96,7 +101,7 @@ mod tests {
96
101
97
102
assert ! ( !INTERRUPT_FIRED . load( Ordering :: SeqCst ) ) ;
98
103
timer. enable_interrupt ( Event :: Update ) ;
99
- timer. start ( 100_000 . Hz ( ) ) ;
104
+ timer. start ( 1 . milliseconds ( ) ) ;
100
105
101
106
while !INTERRUPT_FIRED . load ( Ordering :: SeqCst ) { }
102
107
You can’t perform that action at this time.
0 commit comments