@@ -1311,7 +1311,12 @@ esp_err_t i2s_channel_write(i2s_chan_handle_t handle, const void *src, size_t si
13111311 ESP_RETURN_ON_FALSE (xSemaphoreTake (handle -> binary , pdMS_TO_TICKS (timeout_ms )) == pdTRUE , ESP_ERR_INVALID_STATE , TAG , "The channel is not enabled" );
13121312 src_byte = (char * )src ;
13131313 while (size > 0 && handle -> state == I2S_CHAN_STATE_RUNNING ) {
1314- if (handle -> dma .rw_pos == handle -> dma .buf_size || handle -> dma .curr_ptr == NULL ) {
1314+ /* Acquire the new DMA buffer while:
1315+ * 1. The current buffer is fully filled
1316+ * 2. The current buffer is not set
1317+ * 3. The queue is almost full, i.e., the curr_ptr is nearly to be invalid
1318+ */
1319+ if (handle -> dma .rw_pos == handle -> dma .buf_size || handle -> dma .curr_ptr == NULL || uxQueueSpacesAvailable (handle -> msg_queue ) <= (handle -> dma .desc_num > 2 ? 1 : 0 )) {
13151320 if (xQueueReceive (handle -> msg_queue , & (handle -> dma .curr_ptr ), pdMS_TO_TICKS (timeout_ms )) == pdFALSE ) {
13161321 ret = ESP_ERR_TIMEOUT ;
13171322 break ;
@@ -1356,7 +1361,12 @@ esp_err_t i2s_channel_read(i2s_chan_handle_t handle, void *dest, size_t size, si
13561361 /* The binary semaphore can only be taken when the channel has been enabled and no other reading operation in progress */
13571362 ESP_RETURN_ON_FALSE (xSemaphoreTake (handle -> binary , pdMS_TO_TICKS (timeout_ms )) == pdTRUE , ESP_ERR_INVALID_STATE , TAG , "The channel is not enabled" );
13581363 while (size > 0 && handle -> state == I2S_CHAN_STATE_RUNNING ) {
1359- if (handle -> dma .rw_pos == handle -> dma .buf_size || handle -> dma .curr_ptr == NULL ) {
1364+ /* Acquire the new DMA buffer while:
1365+ * 1. The current buffer is fully filled
1366+ * 2. The current buffer is not set
1367+ * 3. The queue is almost full, i.e., the curr_ptr is nearly to be invalid
1368+ */
1369+ if (handle -> dma .rw_pos == handle -> dma .buf_size || handle -> dma .curr_ptr == NULL || uxQueueSpacesAvailable (handle -> msg_queue ) <= (handle -> dma .desc_num > 2 ? 1 : 0 )) {
13601370 if (xQueueReceive (handle -> msg_queue , & (handle -> dma .curr_ptr ), pdMS_TO_TICKS (timeout_ms )) == pdFALSE ) {
13611371 ret = ESP_ERR_TIMEOUT ;
13621372 break ;
0 commit comments