@@ -139,7 +139,7 @@ macro_rules! __internal_create_stm32_timer_interrupt {
139139}
140140
141141macro_rules! make_timer {
142- ( $tim: ident, $timer: ident, $bits : ident , $ overflow: ident, $tq: ident$( , doc: ( $( $doc: tt) * ) ) ?) => {
142+ ( $tim: ident, $timer: ident, $overflow: ident, $tq: ident$( , doc: ( $( $doc: tt) * ) ) ?) => {
143143 static $overflow: AtomicU64 = AtomicU64 :: new( 0 ) ;
144144 static $tq: TimerQueue <MonoTimerBackend <pac:: $timer>> = TimerQueue :: new( ) ;
145145
@@ -164,10 +164,14 @@ macro_rules! make_timer {
164164 self . tim. dier( ) . modify( |_, w| w. uie( ) . set_bit( ) ) ;
165165
166166 // Configure and enable half-period interrupt
167- self . tim
168- . ccr( 2 )
169- . write( |w| w. ccr( ) . set( ( $bits:: MAX - ( $bits:: MAX >> 1 ) ) . into( ) ) ) ;
170- self . tim. dier( ) . modify( |_, w| w. cc3ie( ) . set_bit( ) ) ;
167+ self . tim. ccr1( ) . write( |w| {
168+ w. ccr( ) . set(
169+ ( <pac:: $timer as General >:: Width :: MAX
170+ - ( <pac:: $timer as General >:: Width :: MAX >> 1 ) )
171+ . into( ) ,
172+ )
173+ } ) ;
174+ self . tim. dier( ) . modify( |_, w| w. cc1ie( ) . set_bit( ) ) ;
171175
172176 // Trigger an update event to load the prescaler value to the clock.
173177 self . tim. egr( ) . write( |w| w. ug( ) . set_bit( ) ) ;
@@ -206,23 +210,24 @@ macro_rules! make_timer {
206210 fn now( ) -> Self :: Ticks {
207211 calculate_now(
208212 || $overflow. load( Ordering :: Relaxed ) ,
209- || Self :: tim( ) . cnt( ) . read( ) . bits( ) ,
213+ || Self :: tim( ) . cnt( ) . read( ) . cnt ( ) . bits( ) ,
210214 )
211215 }
212216
213217 fn set_compare( instant: Self :: Ticks ) {
214218 let now = Self :: now( ) ;
215219
216220 // Since the timer may or may not overflow based on the requested compare val, we check how many ticks are left.
217- // `wrapping_sup` takes care of the u64 integer overflow special case.
218- let val = if instant. wrapping_sub( now) <= ( $bits:: MAX as u64 ) {
219- instant as $bits
220- } else {
221- // In the past or will overflow
222- 0
223- } ;
224-
225- Self :: tim( ) . ccr( 1 ) . write( |r| r. ccr( ) . set( val. into( ) ) ) ;
221+ // `wrapping_sub` takes care of the u64 integer overflow special case.
222+ let val =
223+ if instant. wrapping_sub( now) <= ( <pac:: $timer as General >:: Width :: MAX as u64 ) {
224+ instant as <pac:: $timer as General >:: Width
225+ } else {
226+ // In the past or will overflow
227+ 0
228+ } ;
229+
230+ Self :: tim( ) . ccr2( ) . write( |r| r. ccr( ) . set( val. into( ) ) ) ;
226231 }
227232
228233 fn clear_compare_flag( ) {
@@ -249,8 +254,8 @@ macro_rules! make_timer {
249254 assert!( prev % 2 == 1 , "Monotonic must have missed an interrupt!" ) ;
250255 }
251256 // Half period
252- if Self :: tim( ) . sr( ) . read( ) . cc3if ( ) . bit_is_set( ) {
253- Self :: tim( ) . sr( ) . modify( |_, w| w. cc3if ( ) . clear_bit( ) ) ;
257+ if Self :: tim( ) . sr( ) . read( ) . cc1if ( ) . bit_is_set( ) {
258+ Self :: tim( ) . sr( ) . modify( |_, w| w. cc1if ( ) . clear_bit( ) ) ;
254259 let prev = $overflow. fetch_add( 1 , Ordering :: Relaxed ) ;
255260 assert!( prev % 2 == 0 , "Monotonic must have missed an interrupt!" ) ;
256261 }
@@ -264,16 +269,16 @@ macro_rules! make_timer {
264269}
265270
266271#[ cfg( all( feature = "tim2" , feature = "rtic-tim2" ) ) ]
267- make_timer ! ( tim2, TIM2 , u32 , TIMER2_OVERFLOWS , TIMER2_TQ ) ;
272+ make_timer ! ( tim2, TIM2 , TIMER2_OVERFLOWS , TIMER2_TQ ) ;
268273
269274#[ cfg( all( feature = "tim3" , feature = "rtic-tim3" ) ) ]
270- make_timer ! ( tim3, TIM3 , u16 , TIMER3_OVERFLOWS , TIMER3_TQ ) ;
275+ make_timer ! ( tim3, TIM3 , TIMER3_OVERFLOWS , TIMER3_TQ ) ;
271276
272277#[ cfg( all( feature = "tim4" , feature = "rtic-tim4" ) ) ]
273- make_timer ! ( tim4, TIM4 , u16 , TIMER4_OVERFLOWS , TIMER4_TQ ) ;
278+ make_timer ! ( tim4, TIM4 , TIMER4_OVERFLOWS , TIMER4_TQ ) ;
274279
275280#[ cfg( all( feature = "tim5" , feature = "rtic-tim5" ) ) ]
276- make_timer ! ( tim5, TIM5 , u16 , TIMER5_OVERFLOWS , TIMER5_TQ ) ;
281+ make_timer ! ( tim5, TIM5 , TIMER5_OVERFLOWS , TIMER5_TQ ) ;
277282
278283pub trait Irq {
279284 const IRQ : pac:: Interrupt ;
0 commit comments