From dac174ad76aed3f93e7977557e78abaa03304576 Mon Sep 17 00:00:00 2001 From: morris Date: Thu, 26 Dec 2024 11:35:09 +0800 Subject: [PATCH] fix(rmt): channel resolution divider rounding issue Closes https://github.com/espressif/esp-idf/issues/15092 --- components/driver/rmt/rmt_rx.c | 4 ++-- components/driver/rmt/rmt_tx.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/driver/rmt/rmt_rx.c b/components/driver/rmt/rmt_rx.c index ac82162a4b..9cd1f28b7c 100644 --- a/components/driver/rmt/rmt_rx.c +++ b/components/driver/rmt/rmt_rx.c @@ -247,8 +247,8 @@ esp_err_t rmt_new_rx_channel(const rmt_rx_channel_config_t *config, rmt_channel_ // select the clock source ESP_GOTO_ON_ERROR(rmt_select_periph_clock(&rx_channel->base, config->clk_src), err, TAG, "set group clock failed"); - // set channel clock resolution - uint32_t real_div = group->resolution_hz / config->resolution_hz; + // set channel clock resolution, find the divider to get the closest resolution + uint32_t real_div = (group->resolution_hz + config->resolution_hz / 2) / config->resolution_hz; rmt_ll_rx_set_channel_clock_div(hal->regs, channel_id, real_div); // resolution loss due to division, calculate the real resolution rx_channel->base.resolution_hz = group->resolution_hz / real_div; diff --git a/components/driver/rmt/rmt_tx.c b/components/driver/rmt/rmt_tx.c index 5aeb32f3f7..08027a4eef 100644 --- a/components/driver/rmt/rmt_tx.c +++ b/components/driver/rmt/rmt_tx.c @@ -281,8 +281,8 @@ esp_err_t rmt_new_tx_channel(const rmt_tx_channel_config_t *config, rmt_channel_ #endif // select the clock source ESP_GOTO_ON_ERROR(rmt_select_periph_clock(&tx_channel->base, config->clk_src), err, TAG, "set group clock failed"); - // set channel clock resolution - uint32_t real_div = group->resolution_hz / config->resolution_hz; + // set channel clock resolution, find the divider to get the closest resolution + uint32_t real_div = (group->resolution_hz + config->resolution_hz / 2) / config->resolution_hz; rmt_ll_tx_set_channel_clock_div(hal->regs, channel_id, real_div); // resolution lost due to division, calculate the real resolution tx_channel->base.resolution_hz = group->resolution_hz / real_div;