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