Skip to content

Commit 7dd1f9e

Browse files
committed
fix(i2s): add check for the minimum sample rate
Closes #15659
1 parent 4fe1529 commit 7dd1f9e

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

components/esp_driver_i2s/i2s_pdm.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static esp_err_t i2s_pdm_tx_calculate_clock(i2s_chan_handle_t handle, const i2s_
5656
clk_info->mclk_div = clk_info->sclk / clk_info->mclk;
5757

5858
/* Check if the configuration is correct. Use float for check in case the mclk division might be carried up in the fine division calculation */
59-
ESP_RETURN_ON_FALSE(clk_info->sclk / (float)clk_info->mclk > 1.99, ESP_ERR_INVALID_ARG, TAG, "sample rate is too large");
59+
ESP_RETURN_ON_FALSE((float)clk_info->sclk > clk_info->mclk * 1.99, ESP_ERR_INVALID_ARG, TAG, "sample rate is too large");
6060
ESP_RETURN_ON_FALSE(clk_info->mclk_div < 256, ESP_ERR_INVALID_ARG, TAG, "sample rate is too small");
6161
#if SOC_I2S_SUPPORTS_PCM2PDM
6262
if (!handle->is_raw_pdm) {
@@ -386,7 +386,8 @@ static esp_err_t i2s_pdm_rx_calculate_clock(i2s_chan_handle_t handle, const i2s_
386386
clk_info->mclk_div = clk_info->sclk / clk_info->mclk;
387387

388388
/* Check if the configuration is correct. Use float for check in case the mclk division might be carried up in the fine division calculation */
389-
ESP_RETURN_ON_FALSE(clk_info->sclk / (float)clk_info->mclk > 1.99, ESP_ERR_INVALID_ARG, TAG, "sample rate is too large");
389+
ESP_RETURN_ON_FALSE((float)clk_info->sclk > clk_info->mclk * 1.99, ESP_ERR_INVALID_ARG, TAG, "sample rate is too large");
390+
ESP_RETURN_ON_FALSE(clk_info->mclk_div < I2S_LL_CLK_FRAC_DIV_N_MAX, ESP_ERR_INVALID_ARG, TAG, "sample rate is too small");
390391
#if SOC_I2S_SUPPORTS_PDM2PCM
391392
if (!handle->is_raw_pdm) {
392393
/* Set down-sampling configuration */

components/esp_driver_i2s/i2s_std.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ static esp_err_t i2s_std_calculate_clock(i2s_chan_handle_t handle, const i2s_std
6060
clk_info->mclk_div = clk_info->sclk / clk_info->mclk;
6161

6262
/* Check if the configuration is correct. Use float for check in case the mclk division might be carried up in the fine division calculation */
63-
ESP_RETURN_ON_FALSE(clk_info->sclk / (float)clk_info->mclk > min_mclk_div, ESP_ERR_INVALID_ARG, TAG, "sample rate or mclk_multiple is too large for the current clock source");
63+
ESP_RETURN_ON_FALSE((float)clk_info->sclk > clk_info->mclk * min_mclk_div, ESP_ERR_INVALID_ARG, TAG, "sample rate is too large");
64+
ESP_RETURN_ON_FALSE(clk_info->mclk_div < I2S_LL_CLK_FRAC_DIV_N_MAX, ESP_ERR_INVALID_ARG, TAG, "sample rate is too small");
6465

6566
return ESP_OK;
6667
}

components/esp_driver_i2s/i2s_tdm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ static esp_err_t i2s_tdm_calculate_clock(i2s_chan_handle_t handle, const i2s_tdm
6666
clk_info->mclk_div = clk_info->sclk / clk_info->mclk;
6767

6868
/* Check if the configuration is correct. Use float for check in case the mclk division might be carried up in the fine division calculation */
69-
ESP_RETURN_ON_FALSE(clk_info->sclk / (float)clk_info->mclk > min_mclk_div, ESP_ERR_INVALID_ARG, TAG, "sample rate or mclk_multiple is too large for the current clock source");
69+
ESP_RETURN_ON_FALSE((float)clk_info->sclk > clk_info->mclk * min_mclk_div, ESP_ERR_INVALID_ARG, TAG, "sample rate is too large");
70+
ESP_RETURN_ON_FALSE(clk_info->mclk_div < I2S_LL_CLK_FRAC_DIV_N_MAX, ESP_ERR_INVALID_ARG, TAG, "sample rate is too small");
7071

7172
return ESP_OK;
7273
}

0 commit comments

Comments
 (0)