Skip to content

Commit

Permalink
rp2/machine_pwm: Fix PWM frequency setting.
Browse files Browse the repository at this point in the history
The top value was off by 1: in order to count n ticks it has to be set to
n-1.

Fixes issue #8122.
  • Loading branch information
robert-hh authored and dpgeorge committed Jan 6, 2022
1 parent f4487a0 commit 9e56e63
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ports/rp2/machine_pwm.c
Expand Up @@ -91,7 +91,7 @@ STATIC mp_obj_t mp_machine_pwm_freq_get(machine_pwm_obj_t *self) {
uint32_t source_hz = clock_get_hz(clk_sys);
uint32_t div16 = pwm_hw->slice[self->slice].div;
uint32_t top = pwm_hw->slice[self->slice].top;
uint32_t pwm_freq = 16 * source_hz / div16 / top;
uint32_t pwm_freq = 16 * source_hz / div16 / (top + 1);
return MP_OBJ_NEW_SMALL_INT(pwm_freq);
}

Expand Down Expand Up @@ -123,7 +123,7 @@ STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq) {
mp_raise_ValueError(MP_ERROR_TEXT("freq too small"));
}
pwm_hw->slice[self->slice].div = div16_top;
pwm_hw->slice[self->slice].top = top;
pwm_hw->slice[self->slice].top = top - 1;
}

STATIC mp_obj_t mp_machine_pwm_duty_get_u16(machine_pwm_obj_t *self) {
Expand Down

0 comments on commit 9e56e63

Please sign in to comment.