Repository navigation
fix(i2s): don't append TX data to a buffer the DMA already sends #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9cf93bb
fix(i2s): don't append TX data to a buffer the DMA already sends
floitsch 4e84cad
fix(i2s): raise the GDMA TX EOF once the buffer has been read
floitsch f82bc0f
fix(i2s): raise the legacy TX EOF once the buffer has been read
floitsch c2af268
fix(i2s): track the buffer that the TX DMA sends
floitsch 540542b
test(i2s): cover skipped-buffer timeout accounting
floitsch b7a900d
fix(i2s): protect TX buffer adoption and cache publication
floitsch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
components/esp_driver_i2s/test_apps/i2s/main/test_i2s_tx_timeout.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright (C) 2026 Toit contributors. | ||
| // SPDX-FileCopyrightText: 2026 Toit contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include "driver/i2s_std.h" | ||
| #include "esp_rom_gpio.h" | ||
| #include "soc/gpio_sig_map.h" | ||
| #include "soc/i2s_periph.h" | ||
| #include "unity.h" | ||
| #include "i2s_private.h" | ||
|
|
||
| /* Hold the slave clocks low so there are no EOFs and the free queue cannot | ||
| * refill. No GPIO is driven. This makes partial writes and timeouts deterministic | ||
| * without assuming a particular interrupt latency or requiring loopback wiring. */ | ||
| TEST_CASE("I2S TX timeout preserves an in-flight tail", "[i2s]") | ||
| { | ||
| for (unsigned clear_mode = 0; clear_mode < 3; clear_mode++) { | ||
| i2s_chan_config_t cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_SLAVE); | ||
| cfg.dma_desc_num = 2; | ||
| cfg.dma_frame_num = 240; | ||
| cfg.auto_clear_before_cb = clear_mode == 1; | ||
| cfg.auto_clear_after_cb = clear_mode == 2; | ||
| i2s_chan_handle_t tx = NULL; | ||
| TEST_ESP_OK(i2s_new_channel(&cfg, &tx, NULL)); | ||
| i2s_std_config_t std = { | ||
| .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(10000), | ||
| .slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO), | ||
| .gpio_cfg = { | ||
| .mclk = I2S_GPIO_UNUSED, .bclk = I2S_GPIO_UNUSED, | ||
| .ws = I2S_GPIO_UNUSED, .dout = I2S_GPIO_UNUSED, .din = I2S_GPIO_UNUSED, | ||
| }, | ||
| }; | ||
| TEST_ESP_OK(i2s_channel_init_std_mode(tx, &std)); | ||
| int port = tx->controller->id; | ||
| esp_rom_gpio_connect_in_signal(GPIO_MATRIX_CONST_ZERO_INPUT, i2s_periph_signal[port].s_tx_bck_sig, false); | ||
| esp_rom_gpio_connect_in_signal(GPIO_MATRIX_CONST_ZERO_INPUT, i2s_periph_signal[port].s_tx_ws_sig, false); | ||
|
|
||
| size_t size = tx->dma.buf_size; | ||
| uint8_t *old = malloc(2 * size); | ||
| uint8_t *next = malloc(size + 4); | ||
| TEST_ASSERT_NOT_NULL(old); | ||
| TEST_ASSERT_NOT_NULL(next); | ||
| memset(old, 0xa5, 2 * size); | ||
| memset(next, 0x3c, size + 4); | ||
| size_t loaded = 0; | ||
| TEST_ESP_OK(i2s_channel_preload_data(tx, old, 2 * size, &loaded)); | ||
| TEST_ASSERT_EQUAL_UINT32(2 * size, loaded); | ||
| TEST_ESP_OK(i2s_channel_enable(tx)); | ||
| TEST_ESP_OK(i2s_channel_disable(tx)); | ||
|
|
||
| /* Reset retains DMA storage. Partially preload descriptor 0 again; | ||
| * its unwritten tail represents data from an earlier ring pass. */ | ||
| TEST_ESP_OK(i2s_channel_preload_data(tx, next, 4, &loaded)); | ||
| TEST_ASSERT_EQUAL_UINT32(4, loaded); | ||
| TEST_ESP_OK(i2s_channel_enable(tx)); | ||
|
|
||
| size_t written = SIZE_MAX; | ||
| TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, i2s_channel_write(tx, next, size + 4, &written, 0)); | ||
| TEST_ASSERT_EQUAL_UINT32(size, written); | ||
| TEST_ASSERT_EQUAL_MEMORY(next, tx->dma.desc[0]->buf, 4); | ||
| TEST_ASSERT_EQUAL_MEMORY(old + 4, (const uint8_t *)tx->dma.desc[0]->buf + 4, size - 4); | ||
| TEST_ASSERT_EQUAL_MEMORY(next, tx->dma.desc[1]->buf, size); | ||
|
|
||
| /* A subsequent timed-out call must reset bytes_written to zero and | ||
| * must not overwrite either buffer. Auto-clear only runs at EOF. */ | ||
| written = SIZE_MAX; | ||
| TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, i2s_channel_write(tx, old, 4, &written, 20)); | ||
| TEST_ASSERT_EQUAL_UINT32(0, written); | ||
| TEST_ASSERT_EQUAL_MEMORY(next, tx->dma.desc[0]->buf, 4); | ||
| TEST_ASSERT_EQUAL_MEMORY(old + 4, (const uint8_t *)tx->dma.desc[0]->buf + 4, size - 4); | ||
| TEST_ASSERT_EQUAL_MEMORY(next, tx->dma.desc[1]->buf, size); | ||
| TEST_ESP_OK(i2s_channel_disable(tx)); | ||
| TEST_ESP_OK(i2s_del_channel(tx)); | ||
| free(next); | ||
| free(old); | ||
| printf("TX timeout accounting and untouched tail: clear_mode=%u PASS\n", clear_mode); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.