summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-09-25 15:25:08 +1000
committerDamien George <damien.p.george@gmail.com>2017-09-25 15:25:08 +1000
commitd29b7096424638f89d82a2a4ce295e5fd9325fa2 (patch)
tree0dd381ac3eebb22f5585be0b10652c890ee4f467
parent4a314a6f63c7157a824aa98c01009a8fbafb7475 (diff)
stm32/timer: Enable ARPE so that timer freq can be changed smoothly.
The timer prescaler is buffered by default, and this patch enables ARPE which buffers the auto-reload register. With both of these registers buffered it's now possible to smoothly change the timer's frequency and have a smoothly varying PWM output.
-rw-r--r--ports/stm32/timer.c14
1 files changed, 6 insertions, 8 deletions
diff --git a/ports/stm32/timer.c b/ports/stm32/timer.c
index f24c2e6bf..96a6baa02 100644
--- a/ports/stm32/timer.c
+++ b/ports/stm32/timer.c
@@ -620,6 +620,12 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *self, size_t n_args, cons
#endif
config_deadtime(self, args[6].u_int);
}
+
+ // Enable ARPE so that the auto-reload register is buffered.
+ // This allows to smoothly change the frequency of the timer.
+ self->tim.Instance->CR1 |= TIM_CR1_ARPE;
+
+ // Start the timer running
if (args[5].u_obj == mp_const_none) {
HAL_TIM_Base_Start(&self->tim);
} else {
@@ -1121,10 +1127,6 @@ STATIC mp_obj_t pyb_timer_freq(size_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;
}
}
@@ -1155,10 +1157,6 @@ STATIC mp_obj_t pyb_timer_period(size_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;
}
}