From c6fedef8451cb415226cfe7d5b4ddebc0dd2ef55 Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Sat, 21 Dec 2024 12:28:56 +0200 Subject: [PATCH] esp32/pwm: Fix short pulses when freq change. Signed-off-by: IhorNehrutsa Co-Authored-By: Yoann Darche --- ports/esp32/machine_pwm.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/ports/esp32/machine_pwm.c b/ports/esp32/machine_pwm.c index 2096f83cc69a7..a51b7eb490a2c 100755 --- a/ports/esp32/machine_pwm.c +++ b/ports/esp32/machine_pwm.c @@ -416,7 +416,6 @@ static void set_freq(machine_pwm_obj_t *self, unsigned int freq) { timer.clk_cfg = LEDC_USE_REF_TICK; // 1 MHz } #endif - } #if !(CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2) // Check for clock source conflic @@ -433,15 +432,18 @@ static void set_freq(machine_pwm_obj_t *self, unsigned int freq) { // Configure the new resolution timer.duty_resolution = find_suitable_duty_resolution(src_clk_freq, self->freq); - timers[self->mode][self->timer].duty_resolution = timer.duty_resolution; - timers[self->mode][self->timer].clk_cfg = timer.clk_cfg; - // Configure timer - Set frequency - check_esp_err(ledc_timer_config(&timer)); + if ((timers[self->mode][self->timer].duty_resolution == timer.duty_resolution) && (timers[self->mode][self->timer].clk_cfg == timer.clk_cfg)) { + check_esp_err(ledc_set_freq(self->mode, self->timer, freq)); + } else { + check_esp_err(ledc_timer_config(&timer)); + } // Reset the timer if low speed if (self->mode == LEDC_LOW_SPEED_MODE) { check_esp_err(ledc_timer_rst(self->mode, self->timer)); } + timers[self->mode][self->timer].duty_resolution = timer.duty_resolution; + timers[self->mode][self->timer].clk_cfg = timer.clk_cfg; } }