diff options
author | Dave Hylands <dhylands@gmail.com> | 2015-04-22 15:01:25 -0700 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-04-22 23:31:56 +0100 |
commit | fd787c5e4ed369ddf8a178a62b6faf8a421e2bbc (patch) | |
tree | 71acde106be32180a71b9193adbd4bf583ca1df4 /stmhal/timer.c | |
parent | 40d43ea88dfff431dd863146dd2e62ea8c24683e (diff) | |
download | micropython-fd787c5e4ed369ddf8a178a62b6faf8a421e2bbc.tar.gz micropython-fd787c5e4ed369ddf8a178a62b6faf8a421e2bbc.zip |
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.
Diffstat (limited to 'stmhal/timer.c')
-rw-r--r-- | stmhal/timer.c | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/stmhal/timer.c b/stmhal/timer.c index b75dd688c0..ecb1f3bf7c 100644 --- a/stmhal/timer.c +++ b/stmhal/timer.c @@ -1088,6 +1088,10 @@ STATIC mp_obj_t pyb_timer_freq(mp_uint_t n_args, const mp_obj_t *args) { uint32_t prescaler = compute_prescaler_period_from_freq(self, args[1], &period); self->tim.Instance->PSC = prescaler; __HAL_TIM_SetAutoreload(&self->tim, period); + // Reset the counter to zero. Otherwise, if counter >= period it will + // continue counting until it wraps (at either 16 or 32 bits depending + // on the timer). + __HAL_TIM_SetCounter(&self->tim, 0); return mp_const_none; } } @@ -1118,6 +1122,10 @@ STATIC mp_obj_t pyb_timer_period(mp_uint_t n_args, const mp_obj_t *args) { } else { // set __HAL_TIM_SetAutoreload(&self->tim, mp_obj_get_int(args[1]) & TIMER_CNT_MASK(self)); + // Reset the counter to zero. Otherwise, if counter >= period it will + // continue counting until it wraps (at either 16 or 32 bits depending + // on the timer). + __HAL_TIM_SetCounter(&self->tim, 0); return mp_const_none; } } |