From 1d158ddd10834c6d828da8f1e441dc4eb2dbab26 Mon Sep 17 00:00:00 2001 From: IhorNehrutsa Date: Wed, 5 Jul 2023 16:11:48 +0300 Subject: [PATCH] fix(ledc): fix frequency calculation ledc.c: Fix frequency calculation. Round int instead of truncate. Merges https://github.com/espressif/esp-idf/pull/11810 --- components/driver/ledc/ledc.c | 2 +- components/driver/test_apps/ledc/main/test_ledc.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/components/driver/ledc/ledc.c b/components/driver/ledc/ledc.c index ede1d7a39cce..cdfdaaa6a840 100644 --- a/components/driver/ledc/ledc.c +++ b/components/driver/ledc/ledc.c @@ -781,7 +781,7 @@ uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num) ESP_LOGW(LEDC_TAG, "LEDC timer not configured, call ledc_timer_config to set timer frequency"); return 0; } - return ((uint64_t) src_clk_freq << 8) / precision / clock_divider; + return (((uint64_t) src_clk_freq << LEDC_LL_FRACTIONAL_BITS) + (uint64_t) precision * clock_divider / 2) / precision / clock_divider; } static inline void IRAM_ATTR ledc_calc_fade_end_channel(uint32_t *fade_end_status, uint32_t *channel) diff --git a/components/driver/test_apps/ledc/main/test_ledc.c b/components/driver/test_apps/ledc/main/test_ledc.c index 1e8ab5cde177..5761484c39dc 100644 --- a/components/driver/test_apps/ledc/main/test_ledc.c +++ b/components/driver/test_apps/ledc/main/test_ledc.c @@ -553,7 +553,7 @@ static void timer_frequency_test(ledc_channel_t channel, ledc_timer_bit_t timer_ uint32_t clk_src_freq = 0; esp_clk_tree_src_get_freq_hz((soc_module_clk_t)TEST_DEFAULT_CLK_CFG, ESP_CLK_TREE_SRC_FREQ_PRECISION_EXACT, &clk_src_freq); if (clk_src_freq == 80 * 1000 * 1000) { - theoretical_freq = 8992; + theoretical_freq = 8993; } else if (clk_src_freq == 96 * 1000 * 1000) { theoretical_freq = 9009; }