diff --git a/components/esp_driver_i2s/i2s_common.c b/components/esp_driver_i2s/i2s_common.c index 9c568eaa11..eb5cea6ae3 100644 --- a/components/esp_driver_i2s/i2s_common.c +++ b/components/esp_driver_i2s/i2s_common.c @@ -389,37 +389,39 @@ uint32_t i2s_get_buf_size(i2s_chan_handle_t handle, uint32_t data_bit_width, uin esp_err_t i2s_free_dma_desc(i2s_chan_handle_t handle) { I2S_NULL_POINTER_CHECK(TAG, handle); - if (!handle->dma.desc) { - return ESP_OK; - } - for (int i = 0; i < handle->dma.desc_num; i++) { - if (handle->dma.bufs[i]) { - free(handle->dma.bufs[i]); - handle->dma.bufs[i] = NULL; - } - if (handle->dma.desc[i]) { - free(handle->dma.desc[i]); - handle->dma.desc[i] = NULL; - } - } - if (handle->dma.bufs) { - free(handle->dma.bufs); - handle->dma.bufs = NULL; - } + handle->dma.buf_size = 0; + if (handle->dma.desc) { + for (int i = 0; i < handle->dma.desc_num; i++) { + if (handle->dma.desc[i]) { + free(handle->dma.desc[i]); + handle->dma.desc[i] = NULL; + } + } free(handle->dma.desc); handle->dma.desc = NULL; } + if (handle->dma.bufs) { + for (int i = 0; i < handle->dma.desc_num; i++) { + if (handle->dma.bufs[i]) { + free(handle->dma.bufs[i]); + handle->dma.bufs[i] = NULL; + } + } + free(handle->dma.bufs); + handle->dma.bufs = NULL; + } + return ESP_OK; } -esp_err_t i2s_alloc_dma_desc(i2s_chan_handle_t handle, uint32_t num, uint32_t bufsize) +esp_err_t i2s_alloc_dma_desc(i2s_chan_handle_t handle, uint32_t bufsize) { I2S_NULL_POINTER_CHECK(TAG, handle); esp_err_t ret = ESP_OK; 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); - handle->dma.desc_num = num; + uint32_t num = handle->dma.desc_num; handle->dma.buf_size = bufsize; /* Descriptors must be in the internal RAM */ diff --git a/components/esp_driver_i2s/i2s_pdm.c b/components/esp_driver_i2s/i2s_pdm.c index 9e0a4bdb13..5921ea4d2f 100644 --- a/components/esp_driver_i2s/i2s_pdm.c +++ b/components/esp_driver_i2s/i2s_pdm.c @@ -95,9 +95,8 @@ static esp_err_t i2s_pdm_tx_set_slot(i2s_chan_handle_t handle, const i2s_pdm_tx_ uint32_t buf_size = i2s_get_buf_size(handle, slot_cfg->data_bit_width, handle->dma.frame_num); /* The DMA buffer need to re-allocate if the buffer size changed */ if (handle->dma.buf_size != buf_size) { - handle->dma.buf_size = buf_size; ESP_RETURN_ON_ERROR(i2s_free_dma_desc(handle), TAG, "failed to free the old dma descriptor"); - ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, handle->dma.desc_num, buf_size), + ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, buf_size), TAG, "allocate memory for dma descriptor failed"); } /* Share bck and ws signal in full-duplex mode */ @@ -388,9 +387,8 @@ static esp_err_t i2s_pdm_rx_set_slot(i2s_chan_handle_t handle, const i2s_pdm_rx_ uint32_t buf_size = i2s_get_buf_size(handle, slot_cfg->data_bit_width, handle->dma.frame_num); /* The DMA buffer need to re-allocate if the buffer size changed */ if (handle->dma.buf_size != buf_size) { - handle->dma.buf_size = buf_size; ESP_RETURN_ON_ERROR(i2s_free_dma_desc(handle), TAG, "failed to free the old dma descriptor"); - ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, handle->dma.desc_num, buf_size), + ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, buf_size), TAG, "allocate memory for dma descriptor failed"); } /* Share bck and ws signal in full-duplex mode */ diff --git a/components/esp_driver_i2s/i2s_private.h b/components/esp_driver_i2s/i2s_private.h index d2e914b80a..cd1fd571b8 100644 --- a/components/esp_driver_i2s/i2s_private.h +++ b/components/esp_driver_i2s/i2s_private.h @@ -191,7 +191,6 @@ esp_err_t i2s_free_dma_desc(i2s_chan_handle_t handle); * @brief Allocate memory for I2S DMA descriptor and DMA buffer * * @param handle I2S channel handle - * @param num Number of DMA descriptors * @param bufsize The DMA buffer size * * @return @@ -199,7 +198,7 @@ esp_err_t i2s_free_dma_desc(i2s_chan_handle_t handle); * - ESP_ERR_INVALID_ARG NULL pointer or bufsize is too big * - ESP_ERR_NO_MEM No memory for DMA descriptor and DMA buffer */ -esp_err_t i2s_alloc_dma_desc(i2s_chan_handle_t handle, uint32_t num, uint32_t bufsize); +esp_err_t i2s_alloc_dma_desc(i2s_chan_handle_t handle, uint32_t bufsize); /** * @brief Get DMA buffer size diff --git a/components/esp_driver_i2s/i2s_std.c b/components/esp_driver_i2s/i2s_std.c index dfa34610f9..dbed7dc3b2 100644 --- a/components/esp_driver_i2s/i2s_std.c +++ b/components/esp_driver_i2s/i2s_std.c @@ -106,9 +106,8 @@ static esp_err_t i2s_std_set_slot(i2s_chan_handle_t handle, const i2s_std_slot_c uint32_t buf_size = i2s_get_buf_size(handle, slot_cfg->data_bit_width, handle->dma.frame_num); /* The DMA buffer need to re-allocate if the buffer size changed */ if (handle->dma.buf_size != buf_size) { - handle->dma.buf_size = buf_size; ESP_RETURN_ON_ERROR(i2s_free_dma_desc(handle), TAG, "failed to free the old dma descriptor"); - ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, handle->dma.desc_num, buf_size), + ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, buf_size), TAG, "allocate memory for dma descriptor failed"); } bool is_slave = handle->role == I2S_ROLE_SLAVE; diff --git a/components/esp_driver_i2s/i2s_tdm.c b/components/esp_driver_i2s/i2s_tdm.c index aa6329aa39..b5a2e6ae20 100644 --- a/components/esp_driver_i2s/i2s_tdm.c +++ b/components/esp_driver_i2s/i2s_tdm.c @@ -114,9 +114,8 @@ static esp_err_t i2s_tdm_set_slot(i2s_chan_handle_t handle, const i2s_tdm_slot_c uint32_t buf_size = i2s_get_buf_size(handle, slot_cfg->data_bit_width, handle->dma.frame_num); /* The DMA buffer need to re-allocate if the buffer size changed */ if (handle->dma.buf_size != buf_size) { - handle->dma.buf_size = buf_size; ESP_RETURN_ON_ERROR(i2s_free_dma_desc(handle), TAG, "failed to free the old dma descriptor"); - ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, handle->dma.desc_num, buf_size), + ESP_RETURN_ON_ERROR(i2s_alloc_dma_desc(handle, buf_size), TAG, "allocate memory for dma descriptor failed"); } bool is_slave = handle->role == I2S_ROLE_SLAVE;