@@ -139,7 +139,7 @@ macro_rules! __internal_create_stm32_timer_interrupt {
139
139
}
140
140
141
141
macro_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) * ) ) ?) => {
143
143
static $overflow: AtomicU64 = AtomicU64 :: new( 0 ) ;
144
144
static $tq: TimerQueue <MonoTimerBackend <pac:: $timer>> = TimerQueue :: new( ) ;
145
145
@@ -164,10 +164,14 @@ macro_rules! make_timer {
164
164
self . tim. dier( ) . modify( |_, w| w. uie( ) . set_bit( ) ) ;
165
165
166
166
// 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( ) ) ;
171
175
172
176
// Trigger an update event to load the prescaler value to the clock.
173
177
self . tim. egr( ) . write( |w| w. ug( ) . set_bit( ) ) ;
@@ -206,23 +210,24 @@ macro_rules! make_timer {
206
210
fn now( ) -> Self :: Ticks {
207
211
calculate_now(
208
212
|| $overflow. load( Ordering :: Relaxed ) ,
209
- || Self :: tim( ) . cnt( ) . read( ) . bits( ) ,
213
+ || Self :: tim( ) . cnt( ) . read( ) . cnt ( ) . bits( ) ,
210
214
)
211
215
}
212
216
213
217
fn set_compare( instant: Self :: Ticks ) {
214
218
let now = Self :: now( ) ;
215
219
216
220
// 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( ) ) ) ;
226
231
}
227
232
228
233
fn clear_compare_flag( ) {
@@ -249,8 +254,8 @@ macro_rules! make_timer {
249
254
assert!( prev % 2 == 1 , "Monotonic must have missed an interrupt!" ) ;
250
255
}
251
256
// 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( ) ) ;
254
259
let prev = $overflow. fetch_add( 1 , Ordering :: Relaxed ) ;
255
260
assert!( prev % 2 == 0 , "Monotonic must have missed an interrupt!" ) ;
256
261
}
@@ -264,16 +269,16 @@ macro_rules! make_timer {
264
269
}
265
270
266
271
#[ 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 ) ;
268
273
269
274
#[ 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 ) ;
271
276
272
277
#[ 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 ) ;
274
279
275
280
#[ 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 ) ;
277
282
278
283
pub trait Irq {
279
284
const IRQ : pac:: Interrupt ;
0 commit comments