From c0ab63c4c5f1bf36e3ca45bae7867fcf0c27a7d7 Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Thu, 23 Jan 2025 12:32:44 +0800 Subject: [PATCH 1/2] fix(i2s): fixed incorrect logic in slot reconfig Closes https://github.com/espressif/esp-idf/issues/15256 --- components/esp_driver_i2s/i2s_pdm.c | 10 ++++++++-- components/esp_driver_i2s/i2s_std.c | 5 ++++- components/esp_driver_i2s/i2s_tdm.c | 5 ++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/components/esp_driver_i2s/i2s_pdm.c b/components/esp_driver_i2s/i2s_pdm.c index 919f975a0b..3c72c2f88e 100644 --- a/components/esp_driver_i2s/i2s_pdm.c +++ b/components/esp_driver_i2s/i2s_pdm.c @@ -126,6 +126,9 @@ static esp_err_t i2s_pdm_tx_set_slot(i2s_chan_handle_t handle, const i2s_pdm_tx_ /* Update the mode info: slot configuration */ i2s_pdm_tx_config_t *pdm_tx_cfg = (i2s_pdm_tx_config_t *)handle->mode_info; memcpy(&(pdm_tx_cfg->slot_cfg), slot_cfg, sizeof(i2s_pdm_tx_slot_config_t)); + /* Update the slot bit width to the actual slot bit width */ + pdm_tx_cfg->slot_cfg.slot_bit_width = (int)pdm_tx_cfg->slot_cfg.slot_bit_width < (int)pdm_tx_cfg->slot_cfg.data_bit_width ? + pdm_tx_cfg->slot_cfg.data_bit_width : pdm_tx_cfg->slot_cfg.slot_bit_width; portENTER_CRITICAL(&g_i2s.spinlock); /* Configure the hardware to apply PDM format */ @@ -311,7 +314,7 @@ esp_err_t i2s_channel_reconfig_pdm_tx_slot(i2s_chan_handle_t handle, const i2s_p /* If the slot bit width changed, then need to update the clock */ uint32_t slot_bits = slot_cfg->slot_bit_width == I2S_SLOT_BIT_WIDTH_AUTO ? slot_cfg->data_bit_width : slot_cfg->slot_bit_width; - if (pdm_tx_cfg->slot_cfg.slot_bit_width == slot_bits) { + if (pdm_tx_cfg->slot_cfg.slot_bit_width != slot_bits) { ESP_GOTO_ON_ERROR(i2s_pdm_tx_set_clock(handle, &pdm_tx_cfg->clk_cfg), err, TAG, "update clock failed"); } @@ -438,6 +441,9 @@ static esp_err_t i2s_pdm_rx_set_slot(i2s_chan_handle_t handle, const i2s_pdm_rx_ /* Update the mode info: slot configuration */ i2s_pdm_rx_config_t *pdm_rx_cfg = (i2s_pdm_rx_config_t *)handle->mode_info; memcpy(&(pdm_rx_cfg->slot_cfg), slot_cfg, sizeof(i2s_pdm_rx_slot_config_t)); + /* Update the slot bit width to the actual slot bit width */ + pdm_rx_cfg->slot_cfg.slot_bit_width = (int)pdm_rx_cfg->slot_cfg.slot_bit_width < (int)pdm_rx_cfg->slot_cfg.data_bit_width ? + pdm_rx_cfg->slot_cfg.data_bit_width : pdm_rx_cfg->slot_cfg.slot_bit_width; portENTER_CRITICAL(&g_i2s.spinlock); /* Configure the hardware to apply PDM format */ @@ -624,7 +630,7 @@ esp_err_t i2s_channel_reconfig_pdm_rx_slot(i2s_chan_handle_t handle, const i2s_p /* If the slot bit width changed, then need to update the clock */ uint32_t slot_bits = slot_cfg->slot_bit_width == I2S_SLOT_BIT_WIDTH_AUTO ? slot_cfg->data_bit_width : slot_cfg->slot_bit_width; - if (pdm_rx_cfg->slot_cfg.slot_bit_width == slot_bits) { + if (pdm_rx_cfg->slot_cfg.slot_bit_width != slot_bits) { ESP_GOTO_ON_ERROR(i2s_pdm_rx_set_clock(handle, &pdm_rx_cfg->clk_cfg), err, TAG, "update clock failed"); } diff --git a/components/esp_driver_i2s/i2s_std.c b/components/esp_driver_i2s/i2s_std.c index 54bf3256c8..d4965ede2c 100644 --- a/components/esp_driver_i2s/i2s_std.c +++ b/components/esp_driver_i2s/i2s_std.c @@ -136,6 +136,9 @@ static esp_err_t i2s_std_set_slot(i2s_chan_handle_t handle, const i2s_std_slot_c /* Update the mode info: slot configuration */ i2s_std_config_t *std_cfg = (i2s_std_config_t *)(handle->mode_info); memcpy(&(std_cfg->slot_cfg), slot_cfg, sizeof(i2s_std_slot_config_t)); + /* Update the slot bit width to the actual slot bit width */ + std_cfg->slot_cfg.slot_bit_width = (int)std_cfg->slot_cfg.slot_bit_width < (int)std_cfg->slot_cfg.data_bit_width ? + std_cfg->slot_cfg.data_bit_width : std_cfg->slot_cfg.slot_bit_width; return ESP_OK; } @@ -334,7 +337,7 @@ esp_err_t i2s_channel_reconfig_std_slot(i2s_chan_handle_t handle, const i2s_std_ /* If the slot bit width changed, then need to update the clock */ uint32_t slot_bits = slot_cfg->slot_bit_width == I2S_SLOT_BIT_WIDTH_AUTO ? slot_cfg->data_bit_width : slot_cfg->slot_bit_width; - if (std_cfg->slot_cfg.slot_bit_width == slot_bits) { + if (std_cfg->slot_cfg.slot_bit_width != slot_bits) { ESP_GOTO_ON_ERROR(i2s_std_set_clock(handle, &std_cfg->clk_cfg), err, TAG, "update clock failed"); } diff --git a/components/esp_driver_i2s/i2s_tdm.c b/components/esp_driver_i2s/i2s_tdm.c index d51ff43905..1b111071a7 100644 --- a/components/esp_driver_i2s/i2s_tdm.c +++ b/components/esp_driver_i2s/i2s_tdm.c @@ -94,6 +94,9 @@ static esp_err_t i2s_tdm_set_clock(i2s_chan_handle_t handle, const i2s_tdm_clk_c /* Update the mode info: clock configuration */ memcpy(&(tdm_cfg->clk_cfg), clk_cfg, sizeof(i2s_tdm_clk_config_t)); + /* Update the slot bit width to the actual slot bit width */ + tdm_cfg->slot_cfg.slot_bit_width = (int)tdm_cfg->slot_cfg.slot_bit_width < (int)tdm_cfg->slot_cfg.data_bit_width ? + tdm_cfg->slot_cfg.data_bit_width : tdm_cfg->slot_cfg.slot_bit_width; return ret; } @@ -343,7 +346,7 @@ esp_err_t i2s_channel_reconfig_tdm_slot(i2s_chan_handle_t handle, const i2s_tdm_ /* If the slot bit width changed, then need to update the clock */ uint32_t slot_bits = slot_cfg->slot_bit_width == I2S_SLOT_BIT_WIDTH_AUTO ? slot_cfg->data_bit_width : slot_cfg->slot_bit_width; - if (tdm_cfg->slot_cfg.slot_bit_width == slot_bits) { + if (tdm_cfg->slot_cfg.slot_bit_width != slot_bits) { ESP_GOTO_ON_ERROR(i2s_tdm_set_clock(handle, &tdm_cfg->clk_cfg), err, TAG, "update clock failed"); } From 2bb9fb267fea88dd4e9a75d223fd50ad3d2e9020 Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Fri, 24 Jan 2025 10:34:44 +0800 Subject: [PATCH 2/2] fix(i2s): fix uninitialize warning for the default macros Closes https://github.com/espressif/esp-idf/issues/15271 --- .../esp_driver_i2s/include/driver/i2s_std.h | 17 ++++++++++++++++- .../esp_driver_i2s/include/driver/i2s_tdm.h | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/components/esp_driver_i2s/include/driver/i2s_std.h b/components/esp_driver_i2s/include/driver/i2s_std.h index 263b69b7d4..85cb06ab0b 100644 --- a/components/esp_driver_i2s/include/driver/i2s_std.h +++ b/components/esp_driver_i2s/include/driver/i2s_std.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -193,6 +193,7 @@ extern "C" { I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(bits_per_sample, mono_or_stereo) // Alias /** @endcond */ +#if SOC_I2S_HW_VERSION_1 /** * @brief I2S default standard clock configuration * @note Please set the mclk_multiple to I2S_MCLK_MULTIPLE_384 while using 24 bits data width @@ -204,6 +205,20 @@ extern "C" { .clk_src = I2S_CLK_SRC_DEFAULT, \ .mclk_multiple = I2S_MCLK_MULTIPLE_256, \ } +#else +/** + * @brief I2S default standard clock configuration + * @note Please set the mclk_multiple to I2S_MCLK_MULTIPLE_384 while using 24 bits data width + * Otherwise the sample rate might be imprecise since the BCLK division is not a integer + * @param rate sample rate + */ +#define I2S_STD_CLK_DEFAULT_CONFIG(rate) { \ + .sample_rate_hz = rate, \ + .clk_src = I2S_CLK_SRC_DEFAULT, \ + .mclk_multiple = I2S_MCLK_MULTIPLE_256, \ + .ext_clk_freq_hz = 0, \ +} +#endif /** * @brief I2S slot configuration for standard mode diff --git a/components/esp_driver_i2s/include/driver/i2s_tdm.h b/components/esp_driver_i2s/include/driver/i2s_tdm.h index a7a911b61d..ae07a24475 100644 --- a/components/esp_driver_i2s/include/driver/i2s_tdm.h +++ b/components/esp_driver_i2s/include/driver/i2s_tdm.h @@ -122,6 +122,7 @@ extern "C" { #define I2S_TDM_CLK_DEFAULT_CONFIG(rate) { \ .sample_rate_hz = rate, \ .clk_src = I2S_CLK_SRC_DEFAULT, \ + .ext_clk_freq_hz = 0, \ .mclk_multiple = I2S_MCLK_MULTIPLE_256, \ .bclk_div = 8, \ }