mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-09 07:34:32 +02:00
i2s: update examples for the preload feature
This commit is contained in:
committed by
Kevin (Lao Kaiyao)
parent
7397b3f750
commit
dfedf35ea8
@@ -53,6 +53,10 @@ static void i2s_example_read_task(void *args)
|
|||||||
uint8_t *r_buf = (uint8_t *)calloc(1, EXAMPLE_BUFF_SIZE);
|
uint8_t *r_buf = (uint8_t *)calloc(1, EXAMPLE_BUFF_SIZE);
|
||||||
assert(r_buf); // Check if r_buf allocation success
|
assert(r_buf); // Check if r_buf allocation success
|
||||||
size_t r_bytes = 0;
|
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,
|
/* 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,
|
* 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 */
|
* 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;
|
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) {
|
while (1) {
|
||||||
/* Write i2s data */
|
/* Write i2s data */
|
||||||
if (i2s_channel_write(tx_chan, w_buf, EXAMPLE_BUFF_SIZE, &w_bytes, 1000) == ESP_OK) {
|
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();
|
i2s_example_init_std_simplex();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Step 3: Enable the tx and rx channels before writing or reading data */
|
/* Step 3: Create writing and reading task, enable and start the channels */
|
||||||
ESP_ERROR_CHECK(i2s_channel_enable(tx_chan));
|
|
||||||
ESP_ERROR_CHECK(i2s_channel_enable(rx_chan));
|
|
||||||
|
|
||||||
/* Step 4: Create writing and reading task */
|
|
||||||
xTaskCreate(i2s_example_read_task, "i2s_example_read_task", 4096, NULL, 5, NULL);
|
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);
|
xTaskCreate(i2s_example_write_task, "i2s_example_write_task", 4096, NULL, 5, NULL);
|
||||||
}
|
}
|
||||||
|
@@ -38,6 +38,10 @@ static void i2s_example_read_task(void *args)
|
|||||||
uint8_t *r_buf = (uint8_t *)calloc(1, EXAMPLE_BUFF_SIZE);
|
uint8_t *r_buf = (uint8_t *)calloc(1, EXAMPLE_BUFF_SIZE);
|
||||||
assert(r_buf); // Check if r_buf allocation success
|
assert(r_buf); // Check if r_buf allocation success
|
||||||
size_t r_bytes = 0;
|
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,
|
/* 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,
|
* 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 */
|
* 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;
|
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) {
|
while (1) {
|
||||||
/* Write i2s data */
|
/* Write i2s data */
|
||||||
if (i2s_channel_write(tx_chan, w_buf, EXAMPLE_BUFF_SIZE, &w_bytes, 1000) == ESP_OK) {
|
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();
|
i2s_example_init_tdm_simplex();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Step 3: Enable the tx and rx channels before writing or reading data */
|
/* Step 3: Create writing and reading task, enable and start the channels */
|
||||||
ESP_ERROR_CHECK(i2s_channel_enable(tx_chan));
|
|
||||||
ESP_ERROR_CHECK(i2s_channel_enable(rx_chan));
|
|
||||||
|
|
||||||
/* Step 4: Create writing and reading task */
|
|
||||||
xTaskCreate(i2s_example_read_task, "i2s_example_read_task", 4096, NULL, 5, NULL);
|
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);
|
xTaskCreate(i2s_example_write_task, "i2s_example_write_task", 4096, NULL, 5, NULL);
|
||||||
}
|
}
|
||||||
|
@@ -115,9 +115,18 @@ static void i2s_music(void *args)
|
|||||||
{
|
{
|
||||||
esp_err_t ret = ESP_OK;
|
esp_err_t ret = ESP_OK;
|
||||||
size_t bytes_write = 0;
|
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) {
|
while (1) {
|
||||||
/* Write music to earphone */
|
/* 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) {
|
if (ret != ESP_OK) {
|
||||||
/* Since we set timeout to 'portMAX_DELAY' in 'i2s_channel_write'
|
/* Since we set timeout to 'portMAX_DELAY' in 'i2s_channel_write'
|
||||||
so you won't reach here unless you set other timeout value,
|
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.");
|
ESP_LOGE(TAG, "[music] i2s music play failed.");
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
data_ptr = (uint8_t *)music_pcm_start;
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
|
Reference in New Issue
Block a user