Skip to content

Commit 2ff1985

Browse files
committed
Merge branch 'bugfix/fix_i2s_assign_and_check_sequence' into 'master'
fix(i2s): fixed i2s inappropriate check and assign sequence Closes IDFGH-14939 See merge request espressif/esp-idf!38013
2 parents 1f54af8 + b834886 commit 2ff1985

File tree

5 files changed

+24
-27
lines changed

5 files changed

+24
-27
lines changed

components/esp_driver_i2s/i2s_common.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -457,37 +457,39 @@ uint32_t i2s_get_buf_size(i2s_chan_handle_t handle, uint32_t data_bit_width, uin
457457
esp_err_t i2s_free_dma_desc(i2s_chan_handle_t handle)
458458
{
459459
I2S_NULL_POINTER_CHECK(TAG, handle);
460-
if (!handle->dma.desc) {
461-
return ESP_OK;
462-
}
463-
for (int i = 0; i < handle->dma.desc_num; i++) {
464-
if (handle->dma.bufs[i]) {
465-
free(handle->dma.bufs[i]);
466-
handle->dma.bufs[i] = NULL;
467-
}
468-
if (handle->dma.desc[i]) {
469-
free(handle->dma.desc[i]);
470-
handle->dma.desc[i] = NULL;
460+
handle->dma.buf_size = 0;
461+
462+
if (handle->dma.desc) {
463+
for (int i = 0; i < handle->dma.desc_num; i++) {
464+
if (handle->dma.desc[i]) {
465+
free(handle->dma.desc[i]);
466+
handle->dma.desc[i] = NULL;
467+
}
471468
}
469+
free(handle->dma.desc);
470+
handle->dma.desc = NULL;
472471
}
472+
473473
if (handle->dma.bufs) {
474+
for (int i = 0; i < handle->dma.desc_num; i++) {
475+
if (handle->dma.bufs[i]) {
476+
free(handle->dma.bufs[i]);
477+
handle->dma.bufs[i] = NULL;
478+
}
479+
}
474480
free(handle->dma.bufs);
475481
handle->dma.bufs = NULL;
476482
}
477-
if (handle->dma.desc) {
478-
free(handle->dma.desc);
479-
handle->dma.desc = NULL;
480-
}
481483

482484
return ESP_OK;
483485
}
484486

485-
esp_err_t i2s_alloc_dma_desc(i2s_chan_handle_t handle, uint32_t num, uint32_t bufsize)
487+
esp_err_t i2s_alloc_dma_desc(i2s_chan_handle_t handle, uint32_t bufsize)
486488
{
487489
I2S_NULL_POINTER_CHECK(TAG, handle);
488490
esp_err_t ret = ESP_OK;
489491
ESP_RETURN_ON_FALSE(bufsize <= I2S_DMA_BUFFER_MAX_SIZE, ESP_ERR_INVALID_ARG, TAG, "dma buffer can't be bigger than %d", I2S_DMA_BUFFER_MAX_SIZE);
490-
handle->dma.desc_num = num;
492+
uint32_t num = handle->dma.desc_num;
491493
handle->dma.buf_size = bufsize;
492494

493495
/* Descriptors must be in the internal RAM */

components/esp_driver_i2s/i2s_pdm.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ static esp_err_t i2s_pdm_tx_set_slot(i2s_chan_handle_t handle, const i2s_pdm_tx_
121121
uint32_t buf_size = i2s_get_buf_size(handle, slot_cfg->data_bit_width, handle->dma.frame_num);
122122
/* The DMA buffer need to re-allocate if the buffer size changed */
123123
if (handle->dma.buf_size != buf_size) {
124-
handle->dma.buf_size = buf_size;
125124
ESP_RETURN_ON_ERROR(i2s_free_dma_desc(handle), TAG, "failed to free the old dma descriptor");
126-
ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, handle->dma.desc_num, buf_size),
125+
ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, buf_size),
127126
TAG, "allocate memory for dma descriptor failed");
128127
}
129128
/* Share bck and ws signal in full-duplex mode */
@@ -441,9 +440,8 @@ static esp_err_t i2s_pdm_rx_set_slot(i2s_chan_handle_t handle, const i2s_pdm_rx_
441440
uint32_t buf_size = i2s_get_buf_size(handle, slot_cfg->data_bit_width, handle->dma.frame_num);
442441
/* The DMA buffer need to re-allocate if the buffer size changed */
443442
if (handle->dma.buf_size != buf_size) {
444-
handle->dma.buf_size = buf_size;
445443
ESP_RETURN_ON_ERROR(i2s_free_dma_desc(handle), TAG, "failed to free the old dma descriptor");
446-
ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, handle->dma.desc_num, buf_size),
444+
ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, buf_size),
447445
TAG, "allocate memory for dma descriptor failed");
448446
}
449447
/* Share bck and ws signal in full-duplex mode */

components/esp_driver_i2s/i2s_private.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,14 @@ esp_err_t i2s_free_dma_desc(i2s_chan_handle_t handle);
257257
* @brief Allocate memory for I2S DMA descriptor and DMA buffer
258258
*
259259
* @param handle I2S channel handle
260-
* @param num Number of DMA descriptors
261260
* @param bufsize The DMA buffer size
262261
*
263262
* @return
264263
* - ESP_OK Allocate memory success
265264
* - ESP_ERR_INVALID_ARG NULL pointer or bufsize is too big
266265
* - ESP_ERR_NO_MEM No memory for DMA descriptor and DMA buffer
267266
*/
268-
esp_err_t i2s_alloc_dma_desc(i2s_chan_handle_t handle, uint32_t num, uint32_t bufsize);
267+
esp_err_t i2s_alloc_dma_desc(i2s_chan_handle_t handle, uint32_t bufsize);
269268

270269
/**
271270
* @brief Get DMA buffer size

components/esp_driver_i2s/i2s_std.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ static esp_err_t i2s_std_set_slot(i2s_chan_handle_t handle, const i2s_std_slot_c
113113
uint32_t buf_size = i2s_get_buf_size(handle, slot_cfg->data_bit_width, handle->dma.frame_num);
114114
/* The DMA buffer need to re-allocate if the buffer size changed */
115115
if (handle->dma.buf_size != buf_size) {
116-
handle->dma.buf_size = buf_size;
117116
ESP_RETURN_ON_ERROR(i2s_free_dma_desc(handle), TAG, "failed to free the old dma descriptor");
118-
ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, handle->dma.desc_num, buf_size),
117+
ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, buf_size),
119118
TAG, "allocate memory for dma descriptor failed");
120119
}
121120
bool is_slave = handle->role == I2S_ROLE_SLAVE;

components/esp_driver_i2s/i2s_tdm.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ static esp_err_t i2s_tdm_set_slot(i2s_chan_handle_t handle, const i2s_tdm_slot_c
121121
uint32_t buf_size = i2s_get_buf_size(handle, slot_cfg->data_bit_width, handle->dma.frame_num);
122122
/* The DMA buffer need to re-allocate if the buffer size changed */
123123
if (handle->dma.buf_size != buf_size) {
124-
handle->dma.buf_size = buf_size;
125124
ESP_RETURN_ON_ERROR(i2s_free_dma_desc(handle), TAG, "failed to free the old dma descriptor");
126-
ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, handle->dma.desc_num, buf_size),
125+
ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, buf_size),
127126
TAG, "allocate memory for dma descriptor failed");
128127
}
129128
bool is_slave = handle->role == I2S_ROLE_SLAVE;

0 commit comments

Comments
 (0)