i2s: update examples for the preload feature

This commit is contained in:
laokaiyao 2023-01-06 12:02:05 +08:00 committed by Kevin (Lao Kaiyao)
parent 7397b3f750
commit dfedf35ea8
3 changed files with 41 additions and 13 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);