Skip to content

Commit fd787c5

Browse files
dhylandsdpgeorge
authored andcommitted
stmhal: Reset the timer counter to zero after changing the auto reload.
Because if the counter is above the new value of the auto-reload register then it may be a long time until the timer wraps around.
1 parent 40d43ea commit fd787c5

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

stmhal/timer.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,10 @@ STATIC mp_obj_t pyb_timer_freq(mp_uint_t n_args, const mp_obj_t *args) {
10881088
uint32_t prescaler = compute_prescaler_period_from_freq(self, args[1], &period);
10891089
self->tim.Instance->PSC = prescaler;
10901090
__HAL_TIM_SetAutoreload(&self->tim, period);
1091+
// Reset the counter to zero. Otherwise, if counter >= period it will
1092+
// continue counting until it wraps (at either 16 or 32 bits depending
1093+
// on the timer).
1094+
__HAL_TIM_SetCounter(&self->tim, 0);
10911095
return mp_const_none;
10921096
}
10931097
}
@@ -1118,6 +1122,10 @@ STATIC mp_obj_t pyb_timer_period(mp_uint_t n_args, const mp_obj_t *args) {
11181122
} else {
11191123
// set
11201124
__HAL_TIM_SetAutoreload(&self->tim, mp_obj_get_int(args[1]) & TIMER_CNT_MASK(self));
1125+
// Reset the counter to zero. Otherwise, if counter >= period it will
1126+
// continue counting until it wraps (at either 16 or 32 bits depending
1127+
// on the timer).
1128+
__HAL_TIM_SetCounter(&self->tim, 0);
11211129
return mp_const_none;
11221130
}
11231131
}

0 commit comments

Comments
 (0)