Skip to content

Commit fb72b2c

Browse files
committed
Merge branch 'bugfix/fix_i2s_reconfig_slot_issue' into 'master'
fix(i2s): fixed incorrect logic in slot reconfig Closes IDFGH-14486 See merge request espressif/esp-idf!36574
2 parents c586527 + 2bb9fb2 commit fb72b2c

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

components/esp_driver_i2s/i2s_pdm.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ static esp_err_t i2s_pdm_tx_set_slot(i2s_chan_handle_t handle, const i2s_pdm_tx_
126126
/* Update the mode info: slot configuration */
127127
i2s_pdm_tx_config_t *pdm_tx_cfg = (i2s_pdm_tx_config_t *)handle->mode_info;
128128
memcpy(&(pdm_tx_cfg->slot_cfg), slot_cfg, sizeof(i2s_pdm_tx_slot_config_t));
129+
/* Update the slot bit width to the actual slot bit width */
130+
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 ?
131+
pdm_tx_cfg->slot_cfg.data_bit_width : pdm_tx_cfg->slot_cfg.slot_bit_width;
129132

130133
portENTER_CRITICAL(&g_i2s.spinlock);
131134
/* 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
311314

312315
/* If the slot bit width changed, then need to update the clock */
313316
uint32_t slot_bits = slot_cfg->slot_bit_width == I2S_SLOT_BIT_WIDTH_AUTO ? slot_cfg->data_bit_width : slot_cfg->slot_bit_width;
314-
if (pdm_tx_cfg->slot_cfg.slot_bit_width == slot_bits) {
317+
if (pdm_tx_cfg->slot_cfg.slot_bit_width != slot_bits) {
315318
ESP_GOTO_ON_ERROR(i2s_pdm_tx_set_clock(handle, &pdm_tx_cfg->clk_cfg), err, TAG, "update clock failed");
316319
}
317320

@@ -438,6 +441,9 @@ static esp_err_t i2s_pdm_rx_set_slot(i2s_chan_handle_t handle, const i2s_pdm_rx_
438441
/* Update the mode info: slot configuration */
439442
i2s_pdm_rx_config_t *pdm_rx_cfg = (i2s_pdm_rx_config_t *)handle->mode_info;
440443
memcpy(&(pdm_rx_cfg->slot_cfg), slot_cfg, sizeof(i2s_pdm_rx_slot_config_t));
444+
/* Update the slot bit width to the actual slot bit width */
445+
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 ?
446+
pdm_rx_cfg->slot_cfg.data_bit_width : pdm_rx_cfg->slot_cfg.slot_bit_width;
441447

442448
portENTER_CRITICAL(&g_i2s.spinlock);
443449
/* 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
624630

625631
/* If the slot bit width changed, then need to update the clock */
626632
uint32_t slot_bits = slot_cfg->slot_bit_width == I2S_SLOT_BIT_WIDTH_AUTO ? slot_cfg->data_bit_width : slot_cfg->slot_bit_width;
627-
if (pdm_rx_cfg->slot_cfg.slot_bit_width == slot_bits) {
633+
if (pdm_rx_cfg->slot_cfg.slot_bit_width != slot_bits) {
628634
ESP_GOTO_ON_ERROR(i2s_pdm_rx_set_clock(handle, &pdm_rx_cfg->clk_cfg), err, TAG, "update clock failed");
629635
}
630636

components/esp_driver_i2s/i2s_std.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ static esp_err_t i2s_std_set_slot(i2s_chan_handle_t handle, const i2s_std_slot_c
136136
/* Update the mode info: slot configuration */
137137
i2s_std_config_t *std_cfg = (i2s_std_config_t *)(handle->mode_info);
138138
memcpy(&(std_cfg->slot_cfg), slot_cfg, sizeof(i2s_std_slot_config_t));
139+
/* Update the slot bit width to the actual slot bit width */
140+
std_cfg->slot_cfg.slot_bit_width = (int)std_cfg->slot_cfg.slot_bit_width < (int)std_cfg->slot_cfg.data_bit_width ?
141+
std_cfg->slot_cfg.data_bit_width : std_cfg->slot_cfg.slot_bit_width;
139142

140143
return ESP_OK;
141144
}
@@ -334,7 +337,7 @@ esp_err_t i2s_channel_reconfig_std_slot(i2s_chan_handle_t handle, const i2s_std_
334337

335338
/* If the slot bit width changed, then need to update the clock */
336339
uint32_t slot_bits = slot_cfg->slot_bit_width == I2S_SLOT_BIT_WIDTH_AUTO ? slot_cfg->data_bit_width : slot_cfg->slot_bit_width;
337-
if (std_cfg->slot_cfg.slot_bit_width == slot_bits) {
340+
if (std_cfg->slot_cfg.slot_bit_width != slot_bits) {
338341
ESP_GOTO_ON_ERROR(i2s_std_set_clock(handle, &std_cfg->clk_cfg), err, TAG, "update clock failed");
339342
}
340343

components/esp_driver_i2s/i2s_tdm.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ static esp_err_t i2s_tdm_set_clock(i2s_chan_handle_t handle, const i2s_tdm_clk_c
9494

9595
/* Update the mode info: clock configuration */
9696
memcpy(&(tdm_cfg->clk_cfg), clk_cfg, sizeof(i2s_tdm_clk_config_t));
97+
/* Update the slot bit width to the actual slot bit width */
98+
tdm_cfg->slot_cfg.slot_bit_width = (int)tdm_cfg->slot_cfg.slot_bit_width < (int)tdm_cfg->slot_cfg.data_bit_width ?
99+
tdm_cfg->slot_cfg.data_bit_width : tdm_cfg->slot_cfg.slot_bit_width;
97100

98101
return ret;
99102
}
@@ -343,7 +346,7 @@ esp_err_t i2s_channel_reconfig_tdm_slot(i2s_chan_handle_t handle, const i2s_tdm_
343346

344347
/* If the slot bit width changed, then need to update the clock */
345348
uint32_t slot_bits = slot_cfg->slot_bit_width == I2S_SLOT_BIT_WIDTH_AUTO ? slot_cfg->data_bit_width : slot_cfg->slot_bit_width;
346-
if (tdm_cfg->slot_cfg.slot_bit_width == slot_bits) {
349+
if (tdm_cfg->slot_cfg.slot_bit_width != slot_bits) {
347350
ESP_GOTO_ON_ERROR(i2s_tdm_set_clock(handle, &tdm_cfg->clk_cfg), err, TAG, "update clock failed");
348351
}
349352

components/esp_driver_i2s/include/driver/i2s_std.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -193,6 +193,7 @@ extern "C" {
193193
I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(bits_per_sample, mono_or_stereo) // Alias
194194
/** @endcond */
195195

196+
#if SOC_I2S_HW_VERSION_1
196197
/**
197198
* @brief I2S default standard clock configuration
198199
* @note Please set the mclk_multiple to I2S_MCLK_MULTIPLE_384 while using 24 bits data width
@@ -204,6 +205,20 @@ extern "C" {
204205
.clk_src = I2S_CLK_SRC_DEFAULT, \
205206
.mclk_multiple = I2S_MCLK_MULTIPLE_256, \
206207
}
208+
#else
209+
/**
210+
* @brief I2S default standard clock configuration
211+
* @note Please set the mclk_multiple to I2S_MCLK_MULTIPLE_384 while using 24 bits data width
212+
* Otherwise the sample rate might be imprecise since the BCLK division is not a integer
213+
* @param rate sample rate
214+
*/
215+
#define I2S_STD_CLK_DEFAULT_CONFIG(rate) { \
216+
.sample_rate_hz = rate, \
217+
.clk_src = I2S_CLK_SRC_DEFAULT, \
218+
.mclk_multiple = I2S_MCLK_MULTIPLE_256, \
219+
.ext_clk_freq_hz = 0, \
220+
}
221+
#endif
207222

208223
/**
209224
* @brief I2S slot configuration for standard mode

components/esp_driver_i2s/include/driver/i2s_tdm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ extern "C" {
122122
#define I2S_TDM_CLK_DEFAULT_CONFIG(rate) { \
123123
.sample_rate_hz = rate, \
124124
.clk_src = I2S_CLK_SRC_DEFAULT, \
125+
.ext_clk_freq_hz = 0, \
125126
.mclk_multiple = I2S_MCLK_MULTIPLE_256, \
126127
.bclk_div = 8, \
127128
}

0 commit comments

Comments
 (0)