From dfedf35ea88c8797df9a934c6576520f5e3cb6dd Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Fri, 6 Jan 2023 12:02:05 +0800 Subject: [PATCH] i2s: update examples for the preload feature --- .../i2s_std/main/i2s_std_example_main.c | 21 +++++++++++++------ .../i2s_tdm/main/i2s_tdm_example_main.c | 21 +++++++++++++------ .../i2s_es8311/main/i2s_es8311_example.c | 12 ++++++++++- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/examples/peripherals/i2s/i2s_basic/i2s_std/main/i2s_std_example_main.c b/examples/peripherals/i2s/i2s_basic/i2s_std/main/i2s_std_example_main.c index 9a55aac2c1..e37f55022f 100644 --- a/examples/peripherals/i2s/i2s_basic/i2s_std/main/i2s_std_example_main.c +++ b/examples/peripherals/i2s/i2s_basic/i2s_std/main/i2s_std_example_main.c @@ -53,6 +53,10 @@ static void i2s_example_read_task(void *args) uint8_t *r_buf = (uint8_t *)calloc(1, EXAMPLE_BUFF_SIZE); assert(r_buf); // Check if r_buf allocation success size_t r_bytes = 0; + + /* Enable the RX channel */ + ESP_ERROR_CHECK(i2s_channel_enable(rx_chan)); + /* ATTENTION: The print and delay in the read task only for monitoring the data by human, * Normally there shouldn't be any delays to ensure a short polling time, * Otherwise the dma buffer will overflow and lead to the data lost */ @@ -88,7 +92,16 @@ static void i2s_example_write_task(void *args) w_buf[i + 7] = 0xF0; } - size_t w_bytes = 0; + size_t w_bytes = EXAMPLE_BUFF_SIZE; + + /* (Optional) Preload the data before enabling the TX channel, so that the valid data can be transmit immediately */ + while (w_bytes == EXAMPLE_BUFF_SIZE) { + /* Here we load the target buffer repeatedly, until all the DMA buffers are preloaded */ + ESP_ERROR_CHECK(i2s_channel_preload_writing_data(tx_chan, w_buf, EXAMPLE_BUFF_SIZE, &w_bytes)); + } + + /* Enable the TX channel */ + ESP_ERROR_CHECK(i2s_channel_enable(tx_chan)); while (1) { /* Write i2s data */ if (i2s_channel_write(tx_chan, w_buf, EXAMPLE_BUFF_SIZE, &w_bytes, 1000) == ESP_OK) { @@ -203,11 +216,7 @@ void app_main(void) i2s_example_init_std_simplex(); #endif - /* Step 3: Enable the tx and rx channels before writing or reading data */ - ESP_ERROR_CHECK(i2s_channel_enable(tx_chan)); - ESP_ERROR_CHECK(i2s_channel_enable(rx_chan)); - - /* Step 4: Create writing and reading task */ + /* Step 3: Create writing and reading task, enable and start the channels */ xTaskCreate(i2s_example_read_task, "i2s_example_read_task", 4096, NULL, 5, NULL); xTaskCreate(i2s_example_write_task, "i2s_example_write_task", 4096, NULL, 5, NULL); } diff --git a/examples/peripherals/i2s/i2s_basic/i2s_tdm/main/i2s_tdm_example_main.c b/examples/peripherals/i2s/i2s_basic/i2s_tdm/main/i2s_tdm_example_main.c index b89fdfe342..f841484b76 100644 --- a/examples/peripherals/i2s/i2s_basic/i2s_tdm/main/i2s_tdm_example_main.c +++ b/examples/peripherals/i2s/i2s_basic/i2s_tdm/main/i2s_tdm_example_main.c @@ -38,6 +38,10 @@ static void i2s_example_read_task(void *args) uint8_t *r_buf = (uint8_t *)calloc(1, EXAMPLE_BUFF_SIZE); assert(r_buf); // Check if r_buf allocation success size_t r_bytes = 0; + + /* Enable the RX channel */ + ESP_ERROR_CHECK(i2s_channel_enable(rx_chan)); + /* ATTENTION: The print and delay in the read task only for monitoring the data by human, * Normally there shouldn't be any delays to ensure a short polling time, * Otherwise the dma buffer will overflow and lead to the data lost */ @@ -73,7 +77,16 @@ static void i2s_example_write_task(void *args) w_buf[i + 7] = 0xF0; } - size_t w_bytes = 0; + size_t w_bytes = EXAMPLE_BUFF_SIZE; + + /* (Optional) Preload the data before enabling the TX channel, so that the valid data can be transmit immediately */ + while (w_bytes == EXAMPLE_BUFF_SIZE) { + /* Here we load the target buffer repeatedly, until all the DMA buffers are preloaded */ + ESP_ERROR_CHECK(i2s_channel_preload_writing_data(tx_chan, w_buf, EXAMPLE_BUFF_SIZE, &w_bytes)); + } + + /* Enable the TX channel */ + ESP_ERROR_CHECK(i2s_channel_enable(tx_chan)); while (1) { /* Write i2s data */ if (i2s_channel_write(tx_chan, w_buf, EXAMPLE_BUFF_SIZE, &w_bytes, 1000) == ESP_OK) { @@ -203,11 +216,7 @@ void app_main(void) i2s_example_init_tdm_simplex(); #endif - /* Step 3: Enable the tx and rx channels before writing or reading data */ - ESP_ERROR_CHECK(i2s_channel_enable(tx_chan)); - ESP_ERROR_CHECK(i2s_channel_enable(rx_chan)); - - /* Step 4: Create writing and reading task */ + /* Step 3: Create writing and reading task, enable and start the channels */ xTaskCreate(i2s_example_read_task, "i2s_example_read_task", 4096, NULL, 5, NULL); xTaskCreate(i2s_example_write_task, "i2s_example_write_task", 4096, NULL, 5, NULL); } diff --git a/examples/peripherals/i2s/i2s_codec/i2s_es8311/main/i2s_es8311_example.c b/examples/peripherals/i2s/i2s_codec/i2s_es8311/main/i2s_es8311_example.c index 958d80871d..f633b885a1 100644 --- a/examples/peripherals/i2s/i2s_codec/i2s_es8311/main/i2s_es8311_example.c +++ b/examples/peripherals/i2s/i2s_codec/i2s_es8311/main/i2s_es8311_example.c @@ -115,9 +115,18 @@ static void i2s_music(void *args) { esp_err_t ret = ESP_OK; size_t bytes_write = 0; + uint8_t *data_ptr = (uint8_t *)music_pcm_start; + + /* (Optional) Disable TX channel and preload the data before enabling the TX channel, so that the valid data can be transmit immediately */ + ESP_ERROR_CHECK(i2s_channel_disable(tx_handle)); + ESP_ERROR_CHECK(i2s_channel_preload_writing_data(tx_handle, data_ptr, music_pcm_end - data_ptr, &bytes_write)); + data_ptr += bytes_write; // Move forward the data pointer + + /* Enable the TX channel */ + ESP_ERROR_CHECK(i2s_channel_enable(tx_handle)); while (1) { /* Write music to earphone */ - ret = i2s_channel_write(tx_handle, music_pcm_start, music_pcm_end - music_pcm_start, &bytes_write, portMAX_DELAY); + ret = i2s_channel_write(tx_handle, data_ptr, music_pcm_end - data_ptr, &bytes_write, portMAX_DELAY); if (ret != ESP_OK) { /* Since we set timeout to 'portMAX_DELAY' in 'i2s_channel_write' so you won't reach here unless you set other timeout value, @@ -131,6 +140,7 @@ static void i2s_music(void *args) ESP_LOGE(TAG, "[music] i2s music play failed."); abort(); } + data_ptr = (uint8_t *)music_pcm_start; vTaskDelay(1000 / portTICK_PERIOD_MS); } vTaskDelete(NULL);