From 9e56e630caeec9077cd95ac33b702a7b2187300e Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 30 Dec 2021 12:08:16 +0100 Subject: [PATCH] rp2/machine_pwm: Fix PWM frequency setting. The top value was off by 1: in order to count n ticks it has to be set to n-1. Fixes issue #8122. --- ports/rp2/machine_pwm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/rp2/machine_pwm.c b/ports/rp2/machine_pwm.c index 2952a43062..d2cb937952 100644 --- a/ports/rp2/machine_pwm.c +++ b/ports/rp2/machine_pwm.c @@ -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); } @@ -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) {