Skip to content

Commit 4fdf518

Browse files
committed
Track unadjusted PWM duty cycle to avoid accumulating conversion errors
Fixes #2086 When the frequency of a `PWMOut` is change it re-sets the PWM's duty cycle as well, since the registers have to be re-calculated based on the new frequency. Unfortunately, `common_hal_pulseio_pwmout_get_duty_cycle` will return a value very close to, but not exactly, the value passed to `common_hal_pulseio_pwmout_set_duty_cycle`. If the frequency is modified without the calling code also re-setting the duty cycle then the duty cycle will decay over time. This fixes that problem by tracking the unadjusted duty cycle and re-setting the duty cycle to that value when the frequency is changed.
1 parent af1fab1 commit 4fdf518

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

ports/atmel-samd/common-hal/pulseio/PWMOut.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
135135
bool variable_frequency) {
136136
self->pin = pin;
137137
self->variable_frequency = variable_frequency;
138+
self->duty_cycle = duty;
138139

139140
if (pin->timer[0].index >= TC_INST_NUM &&
140141
pin->timer[1].index >= TCC_INST_NUM
@@ -322,6 +323,13 @@ void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
322323
}
323324

324325
extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) {
326+
// Store the unadjusted duty cycle. It turns out the the process of adjusting and calucating
327+
// the duty cycle here and reading it back is lossy - the value will decay over time.
328+
// Track it here so that if frequency is changed we can use this value to recalcuate the
329+
// proper duty cycle.
330+
// See https://github.com/adafruit/circuitpython/issues/2086 for more details
331+
self->duty_cycle = duty;
332+
325333
const pin_timer_t* t = self->timer;
326334
if (t->is_tc) {
327335
uint16_t adjusted_duty = tc_periods[t->index] * duty / 0xffff;
@@ -415,7 +423,6 @@ void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self,
415423
break;
416424
}
417425
}
418-
uint16_t old_duty = common_hal_pulseio_pwmout_get_duty_cycle(self);
419426
if (t->is_tc) {
420427
Tc* tc = tc_insts[t->index];
421428
uint8_t old_divisor = tc->COUNT16.CTRLA.bit.PRESCALER;
@@ -450,7 +457,7 @@ void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self,
450457
#endif
451458
}
452459

453-
common_hal_pulseio_pwmout_set_duty_cycle(self, old_duty);
460+
common_hal_pulseio_pwmout_set_duty_cycle(self, self->duty_cycle);
454461
}
455462

456463
uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self) {

ports/atmel-samd/common-hal/pulseio/PWMOut.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef struct {
3636
const mcu_pin_obj_t *pin;
3737
const pin_timer_t* timer;
3838
bool variable_frequency;
39+
uint16_t duty_cycle;
3940
} pulseio_pwmout_obj_t;
4041

4142
void pwmout_reset(void);

0 commit comments

Comments
 (0)