From a32a89b00295e0b9e1b2d3567250cce760f6eb78 Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Mon, 1 Aug 2022 18:05:29 +0800 Subject: [PATCH 1/2] i2s_legacy_test: fix the clock test issue --- components/driver/deprecated/i2s_legacy.c | 2 + .../i2s_test_apps/i2s/main/test_i2s.c | 39 +++++++++++++++---- .../legacy_i2s_driver/main/test_legacy_i2s.c | 15 +++++++ .../legacy_i2s_driver/sdkconfig.ci.release | 1 - 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/components/driver/deprecated/i2s_legacy.c b/components/driver/deprecated/i2s_legacy.c index 21e7dde2f6..3fe8cc17df 100644 --- a/components/driver/deprecated/i2s_legacy.c +++ b/components/driver/deprecated/i2s_legacy.c @@ -804,6 +804,8 @@ static esp_err_t i2s_calculate_clock(i2s_port_t i2s_num, i2s_hal_clock_info_t *c /* Calculate clock for common mode */ ESP_RETURN_ON_ERROR(i2s_calculate_common_clock(i2s_num, clk_info), TAG, "Common clock calculate failed"); + ESP_LOGD(TAG, "[sclk] %d [mclk] %d [mclk_div] %d [bclk] %d [bclk_div] %d", + clk_info->sclk, clk_info->mclk, clk_info->mclk_div, clk_info->bclk, clk_info->bclk_div); return ESP_OK; } diff --git a/components/driver/test_apps/i2s_test_apps/i2s/main/test_i2s.c b/components/driver/test_apps/i2s_test_apps/i2s/main/test_i2s.c index 6d028e4dde..599dc6c743 100644 --- a/components/driver/test_apps/i2s_test_apps/i2s/main/test_i2s.c +++ b/components/driver/test_apps/i2s_test_apps/i2s/main/test_i2s.c @@ -367,7 +367,7 @@ TEST_CASE("I2S_thread_concurrent_safety_test", "[i2s]") static uint32_t get_start_index(uint16_t *buf, uint32_t len, uint32_t start_val) { uint32_t i = 0; - for (i = 0; i < len; i++) { + for (i = 100; i < len; i++) { if (buf[i] == start_val) { printf("%d %d %d %d %d %d %d %d\n", buf[i], buf[i+1], buf[i+2], buf[i+3], @@ -394,6 +394,8 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s]") i2s_chan_handle_t tx_handle; i2s_chan_handle_t rx_handle; + bool is_failed = false; + i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_0, I2S_ROLE_MASTER); chan_cfg.dma_desc_num = 8; chan_cfg.dma_frame_num = 128; @@ -454,7 +456,11 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s]") printf("Data start index: %d\n", index); TEST_ASSERT(index < READ_BUF_LEN / 2 - 50); for (int16_t j = 1; j < 100; j += 2) { - TEST_ASSERT_EQUAL_INT16(r_buf[index++], j); + if (r_buf[index++] != j) { + printf("rx right mono test failed\n"); + is_failed = true; + goto err; + } } printf("rx right mono test passed\n"); @@ -483,7 +489,11 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s]") printf("Data start index: %d\n", index); TEST_ASSERT(index < READ_BUF_LEN / 2 - 50); for (int16_t j = 2; j < 100; j += 2) { - TEST_ASSERT_EQUAL_INT16(r_buf[index++], j); + if (r_buf[index++] != j) { + printf("rx left mono test failed\n"); + is_failed = true; + goto err; + } } printf("rx left mono test passed\n"); @@ -506,7 +516,11 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s]") printf("Data start index: %d\n", index); TEST_ASSERT(index < READ_BUF_LEN / 2 - 100); for (int16_t j = 1; j < 100; j ++) { - TEST_ASSERT_EQUAL_INT16(r_buf[index++], j); // receive all number + if (r_buf[index++] != j) { + printf("tx/rx stereo test failed\n"); + is_failed = true; + goto err; + } } printf("tx/rx stereo test passed\n"); @@ -528,19 +542,30 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s]") } printf("Data start index: %d\n", index); TEST_ASSERT(index < READ_BUF_LEN / 2 - 200); - for (int16_t j = 1; j < 100; j ++) { - TEST_ASSERT_EQUAL_INT16(r_buf[index], j); - index += 2; + for (int16_t j = 1; j < 100; j ++, index += 2) { + if (r_buf[index] != j) { + printf("tx mono rx stereo test failed\n"); + is_failed = true; + goto err; + } } printf("tx mono rx stereo test passed\n"); #endif +err: + if (is_failed) { + for (int i = 0; i < READ_BUF_LEN / 2; i++) { + printf("%x ", r_buf[i]); + } + printf("\n"); + } free(w_buf); free(r_buf); TEST_ESP_OK(i2s_channel_disable(tx_handle)); TEST_ESP_OK(i2s_channel_disable(rx_handle)); TEST_ESP_OK(i2s_del_channel(tx_handle)); TEST_ESP_OK(i2s_del_channel(rx_handle)); + TEST_ASSERT_FALSE(is_failed); } TEST_CASE("I2S_memory_leak_test", "[i2s]") diff --git a/components/driver/test_apps/i2s_test_apps/legacy_i2s_driver/main/test_legacy_i2s.c b/components/driver/test_apps/i2s_test_apps/legacy_i2s_driver/main/test_legacy_i2s.c index f8ab460e54..8899e8e9ad 100644 --- a/components/driver/test_apps/i2s_test_apps/legacy_i2s_driver/main/test_legacy_i2s.c +++ b/components/driver/test_apps/i2s_test_apps/legacy_i2s_driver/main/test_legacy_i2s.c @@ -29,6 +29,9 @@ #include "driver/pulse_cnt.h" #include "soc/pcnt_periph.h" #endif +#ifdef CONFIG_PM_ENABLE +#include "esp_pm.h" +#endif #include "../../test_inc/test_i2s.h" @@ -854,6 +857,14 @@ static void i2s_test_common_sample_rate(i2s_port_t id) 32000, 44100, 48000, 64000, 88200, 96000, 128000, 144000, 196000}; int real_pulse = 0; + + // Acquire the PM lock incase Dynamic Frequency Scaling(DFS) lower the frequency +#ifdef CONFIG_PM_ENABLE + esp_pm_lock_handle_t pm_lock; + esp_pm_lock_type_t pm_type = ESP_PM_APB_FREQ_MAX; + TEST_ESP_OK(esp_pm_lock_create(pm_type, 0, "legacy_i2s_test", &pm_lock)); + esp_pm_lock_acquire(pm_lock); +#endif for (int i = 0; i < 15; i++) { int expt_pulse = (int16_t)((float)test_freq[i] * (TEST_I2S_PERIOD_MS / 1000.0)); TEST_ESP_OK(i2s_set_clk(id, test_freq[i], SAMPLE_BITS, I2S_CHANNEL_STEREO)); @@ -868,6 +879,10 @@ static void i2s_test_common_sample_rate(i2s_port_t id) // Check if the error between real pulse number and expected pulse number is within 1% TEST_ASSERT_INT_WITHIN(expt_pulse * 0.01, expt_pulse, real_pulse); } +#ifdef CONFIG_PM_ENABLE + esp_pm_lock_release(pm_lock); + esp_pm_lock_delete(pm_lock); +#endif TEST_ESP_OK(pcnt_del_channel(pcnt_chan)); TEST_ESP_OK(pcnt_unit_stop(pcnt_unit)); TEST_ESP_OK(pcnt_unit_disable(pcnt_unit)); diff --git a/components/driver/test_apps/i2s_test_apps/legacy_i2s_driver/sdkconfig.ci.release b/components/driver/test_apps/i2s_test_apps/legacy_i2s_driver/sdkconfig.ci.release index 74be71a29b..91d93f163e 100644 --- a/components/driver/test_apps/i2s_test_apps/legacy_i2s_driver/sdkconfig.ci.release +++ b/components/driver/test_apps/i2s_test_apps/legacy_i2s_driver/sdkconfig.ci.release @@ -3,4 +3,3 @@ CONFIG_FREERTOS_USE_TICKLESS_IDLE=y CONFIG_COMPILER_OPTIMIZATION_SIZE=y CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE=y CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT=y -CONFIG_I2S_ISR_IRAM_SAFE=y From 4c446222e1d171dd38b4634b260cf4a2ba7003bc Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Wed, 3 Aug 2022 13:14:36 +0800 Subject: [PATCH 2/2] i2s_test: fix mono stereo test --- .../i2s_test_apps/i2s/main/test_i2s.c | 104 ++++++------- .../legacy_i2s_driver/main/test_legacy_i2s.c | 141 ++++++++++-------- 2 files changed, 134 insertions(+), 111 deletions(-) diff --git a/components/driver/test_apps/i2s_test_apps/i2s/main/test_i2s.c b/components/driver/test_apps/i2s_test_apps/i2s/main/test_i2s.c index 599dc6c743..52cc31eed6 100644 --- a/components/driver/test_apps/i2s_test_apps/i2s/main/test_i2s.c +++ b/components/driver/test_apps/i2s_test_apps/i2s/main/test_i2s.c @@ -364,18 +364,26 @@ TEST_CASE("I2S_thread_concurrent_safety_test", "[i2s]") TEST_ESP_OK(i2s_del_channel(rx_handle)); } -static uint32_t get_start_index(uint16_t *buf, uint32_t len, uint32_t start_val) +static bool whether_contains_exapected_data(uint16_t *src, uint32_t src_len, uint32_t src_step, uint32_t start_val, uint32_t val_step) { - uint32_t i = 0; - for (i = 100; i < len; i++) { - if (buf[i] == start_val) { - printf("%d %d %d %d %d %d %d %d\n", - buf[i], buf[i+1], buf[i+2], buf[i+3], - buf[i+4], buf[i+5], buf[i+6], buf[i+7]); - break; + uint32_t val = start_val; + uint32_t index_step = 1; + for (int i = 0; val < 100 && i < src_len; i += index_step) { + if (src[i] == val) { + if (val == start_val && i < src_len - 8) { + printf("start index: %d ---> \n%d %d %d %d %d %d %d %d\n", i, + src[i], src[i+1], src[i+2], src[i+3], + src[i+4], src[i+5], src[i+6], src[i+7]); + } + index_step = src_step; + val += val_step; + } else { + index_step = 1; + val = start_val; } } - return i; + + return val >= 100; } /** @@ -431,7 +439,6 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s]") uint16_t *r_buf = calloc(1, READ_BUF_LEN); size_t w_bytes = 0; size_t r_bytes = 0; - uint32_t index = 0; uint32_t retry = 0; for (int n = 0; n < WRITE_BUF_LEN / 2; n++) { w_buf[n] = n%100; @@ -441,7 +448,7 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s]") * tx format: 0x00[L] 0x01[R] 0x02[L] 0x03[R] ... * rx receive: 0x01[R] 0x03[R] ... */ TEST_ESP_OK(i2s_channel_write(tx_handle, w_buf, WRITE_BUF_LEN, &w_bytes, portMAX_DELAY)); - for (retry = 0, index = READ_BUF_LEN / 2; (retry < RETEY_TIMES) && (index >= READ_BUF_LEN / 2 - 50); retry++) { + for (retry = 0; retry < RETEY_TIMES; retry++) { TEST_ESP_OK(i2s_channel_read(rx_handle, r_buf, READ_BUF_LEN, &r_bytes, portMAX_DELAY)); #if CONFIG_IDF_TARGET_ESP32 /* The data of tx/rx channels are flipped on ESP32 */ @@ -451,17 +458,16 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s]") r_buf[n+1] = temp; } #endif - index = get_start_index(r_buf, READ_BUF_LEN / 2, 1); - } - printf("Data start index: %d\n", index); - TEST_ASSERT(index < READ_BUF_LEN / 2 - 50); - for (int16_t j = 1; j < 100; j += 2) { - if (r_buf[index++] != j) { - printf("rx right mono test failed\n"); - is_failed = true; - goto err; + /* Expected: 1 3 5 7 9 ... 97 99 */ + if (whether_contains_exapected_data(r_buf, READ_BUF_LEN / 2, 1, 1, 2)) { + break; } } + if (retry >= RETEY_TIMES) { + printf("rx right mono test failed\n"); + is_failed = true; + goto err; + } printf("rx right mono test passed\n"); /* rx left mono test @@ -474,7 +480,7 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s]") TEST_ESP_OK(i2s_channel_enable(tx_handle)); TEST_ESP_OK(i2s_channel_enable(rx_handle)); TEST_ESP_OK(i2s_channel_write(tx_handle, w_buf, WRITE_BUF_LEN, &w_bytes, portMAX_DELAY)); - for (retry = 0, index = READ_BUF_LEN / 2; (retry < RETEY_TIMES) && (index >= READ_BUF_LEN / 2 - 50); retry++) { + for (retry = 0; retry < RETEY_TIMES; retry++) { TEST_ESP_OK(i2s_channel_read(rx_handle, r_buf, READ_BUF_LEN, &r_bytes, portMAX_DELAY)); #if CONFIG_IDF_TARGET_ESP32 /* The data of tx/rx channels are flipped on ESP32 */ @@ -484,17 +490,16 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s]") r_buf[n+1] = temp; } #endif - index = get_start_index(r_buf, READ_BUF_LEN / 2, 2); - } - printf("Data start index: %d\n", index); - TEST_ASSERT(index < READ_BUF_LEN / 2 - 50); - for (int16_t j = 2; j < 100; j += 2) { - if (r_buf[index++] != j) { - printf("rx left mono test failed\n"); - is_failed = true; - goto err; + /* Expected: 2 4 6 8 10 ... 96 98 */ + if (whether_contains_exapected_data(r_buf, READ_BUF_LEN / 2, 1, 2, 2)) { + break; } } + if (retry >= RETEY_TIMES) { + printf("rx left mono test failed\n"); + is_failed = true; + goto err; + } printf("rx left mono test passed\n"); /* tx/rx stereo test @@ -508,20 +513,18 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s]") TEST_ESP_OK(i2s_channel_enable(tx_handle)); TEST_ESP_OK(i2s_channel_enable(rx_handle)); TEST_ESP_OK(i2s_channel_write(tx_handle, w_buf, WRITE_BUF_LEN, &w_bytes, portMAX_DELAY)); - for (retry = 0, index = READ_BUF_LEN / 2; (retry < RETEY_TIMES) && (index >= READ_BUF_LEN / 2 - 100); retry++) { + for (retry = 0; retry < RETEY_TIMES; retry++) { TEST_ESP_OK(i2s_channel_read(rx_handle, r_buf, READ_BUF_LEN, &r_bytes, portMAX_DELAY)); - index = get_start_index(r_buf, READ_BUF_LEN / 2, 1); - } - - printf("Data start index: %d\n", index); - TEST_ASSERT(index < READ_BUF_LEN / 2 - 100); - for (int16_t j = 1; j < 100; j ++) { - if (r_buf[index++] != j) { - printf("tx/rx stereo test failed\n"); - is_failed = true; - goto err; + /* Expected: 1 2 3 4 ... 98 99 */ + if (whether_contains_exapected_data(r_buf, READ_BUF_LEN / 2, 1, 1, 1)) { + break; } } + if (retry >= RETEY_TIMES) { + printf("tx/rx stereo test failed\n"); + is_failed = true; + goto err; + } printf("tx/rx stereo test passed\n"); #if !CONFIG_IDF_TARGET_ESP32 // the 16 bit channel sequence on ESP32 is incorrect @@ -536,19 +539,18 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s]") TEST_ESP_OK(i2s_channel_enable(tx_handle)); TEST_ESP_OK(i2s_channel_enable(rx_handle)); TEST_ESP_OK(i2s_channel_write(tx_handle, w_buf, WRITE_BUF_LEN, &w_bytes, portMAX_DELAY)); - for (retry = 0, index = READ_BUF_LEN / 2; (retry < RETEY_TIMES) && (index >= READ_BUF_LEN / 2 - 100); retry++) { + for (retry = 0; retry < RETEY_TIMES; retry++) { TEST_ESP_OK(i2s_channel_read(rx_handle, r_buf, READ_BUF_LEN, &r_bytes, portMAX_DELAY)); - index = get_start_index(r_buf, READ_BUF_LEN / 2, 1); - } - printf("Data start index: %d\n", index); - TEST_ASSERT(index < READ_BUF_LEN / 2 - 200); - for (int16_t j = 1; j < 100; j ++, index += 2) { - if (r_buf[index] != j) { - printf("tx mono rx stereo test failed\n"); - is_failed = true; - goto err; + /* Expected: 1 x 2 x 3 x ... 98 x 99 */ + if (whether_contains_exapected_data(r_buf, READ_BUF_LEN / 2, 2, 1, 1)) { + break; } } + if (retry >= RETEY_TIMES) { + printf("tx mono rx stereo test failed\n"); + is_failed = true; + goto err; + } printf("tx mono rx stereo test passed\n"); #endif diff --git a/components/driver/test_apps/i2s_test_apps/legacy_i2s_driver/main/test_legacy_i2s.c b/components/driver/test_apps/i2s_test_apps/legacy_i2s_driver/main/test_legacy_i2s.c index 8899e8e9ad..7d87bfc643 100644 --- a/components/driver/test_apps/i2s_test_apps/legacy_i2s_driver/main/test_legacy_i2s.c +++ b/components/driver/test_apps/i2s_test_apps/legacy_i2s_driver/main/test_legacy_i2s.c @@ -214,6 +214,28 @@ TEST_CASE("I2S_basic_driver_installation_uninstallation_and_settings_test", "[i2 TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, i2s_driver_uninstall(I2S_NUM_0)); } +static bool whether_contains_exapected_data(uint16_t *src, uint32_t src_len, uint32_t src_step, uint32_t start_val, uint32_t val_step) +{ + uint32_t val = start_val; + uint32_t index_step = 1; + for (int i = 0; val < 100 && i < src_len; i += index_step) { + if (src[i] == val) { + if (val == start_val && i < src_len - 8) { + printf("start index: %d ---> \n%d %d %d %d %d %d %d %d\n", i, + src[i], src[i+1], src[i+2], src[i+3], + src[i+4], src[i+5], src[i+6], src[i+7]); + } + index_step = src_step; + val += val_step; + } else { + index_step = 1; + val = start_val; + } + } + + return val >= 100; +} + /** * @brief Test mono and stereo mode of I2S by loopback * @note Only rx channel distinguish left mono and right mono, tx channel does not @@ -225,6 +247,7 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s_legacy]") { #define WRITE_BUF_LEN 2000 #define READ_BUF_LEN 4000 +#define RETEY_TIMES 3 // master driver installed and send data i2s_config_t master_i2s_config = { .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX, @@ -272,6 +295,8 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s_legacy]") uint16_t *r_buf = calloc(1, READ_BUF_LEN); size_t w_bytes = 0; size_t r_bytes = 0; + uint32_t retry = 0; + bool is_failed = false; for (int n = 0; n < WRITE_BUF_LEN / 2; n++) { w_buf[n] = n%100; } @@ -279,28 +304,25 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s_legacy]") * tx format: 0x00[L] 0x01[R] 0x02[L] 0x03[R] ... * rx receive: 0x01[R] 0x03[R] ... */ TEST_ESP_OK(i2s_write(I2S_NUM_0, w_buf, WRITE_BUF_LEN, &w_bytes, portMAX_DELAY)); - TEST_ESP_OK(i2s_read(I2S_NUM_0, r_buf, READ_BUF_LEN, &r_bytes, portMAX_DELAY)); -#if CONFIG_IDF_TARGET_ESP32 - /* The data of tx/rx channels are flipped on ESP32 */ - for (int n = 0; n < READ_BUF_LEN / 2; n += 2) { - int16_t temp = r_buf[n]; - r_buf[n] = r_buf[n+1]; - r_buf[n+1] = temp; - } -#endif - int i = 0; - for (i = 0; (i < READ_BUF_LEN / 2); i++) { - if (r_buf[i] == 1) { - printf("%d %d %d %d\n%d %d %d %d\n", - r_buf[i], r_buf[i+1], r_buf[i+2], r_buf[i+3], - r_buf[i+4], r_buf[i+5], r_buf[i+6], r_buf[i+7]); + for (retry = 0; retry < RETEY_TIMES; retry++) { + TEST_ESP_OK(i2s_read(I2S_NUM_0, r_buf, READ_BUF_LEN, &r_bytes, portMAX_DELAY)); + #if CONFIG_IDF_TARGET_ESP32 + /* The data of tx/rx channels are flipped on ESP32 */ + for (int n = 0; n < READ_BUF_LEN / 2; n += 2) { + int16_t temp = r_buf[n]; + r_buf[n] = r_buf[n+1]; + r_buf[n+1] = temp; + } + #endif + /* Expected: 1 3 5 7 9 ... 97 99 */ + if (whether_contains_exapected_data(r_buf, READ_BUF_LEN / 2, 1, 1, 2)) { break; } } - printf("Data start index: %d\n", i); - TEST_ASSERT(i < READ_BUF_LEN / 2 - 50); - for (int16_t j = 1; j < 100; j += 2) { - TEST_ASSERT_EQUAL_INT16(r_buf[i++], j); + if (retry >= RETEY_TIMES) { + printf("rx right mono test failed\n"); + is_failed = true; + goto err; } printf("rx right mono test passed\n"); @@ -309,21 +331,17 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s_legacy]") * rx receive: 0x00[L] 0x01[R] 0x02[L] 0x03[R] ... */ TEST_ESP_OK(i2s_set_clk(I2S_NUM_0, SAMPLE_RATE, SAMPLE_BITS, I2S_CHANNEL_STEREO)); TEST_ESP_OK(i2s_write(I2S_NUM_0, w_buf, WRITE_BUF_LEN, &w_bytes, portMAX_DELAY)); - TEST_ESP_OK(i2s_read(I2S_NUM_0, r_buf, READ_BUF_LEN, &r_bytes, portMAX_DELAY)); - - for (i = 0; (i < READ_BUF_LEN / 2); i++) { - if (r_buf[i] == 1) { - printf("%d %d %d %d\n%d %d %d %d\n", - r_buf[i], r_buf[i+1], r_buf[i+2], r_buf[i+3], - r_buf[i+4], r_buf[i+5], r_buf[i+6], r_buf[i+7]); + for (retry = 0; retry < RETEY_TIMES; retry++) { + TEST_ESP_OK(i2s_read(I2S_NUM_0, r_buf, READ_BUF_LEN, &r_bytes, portMAX_DELAY)); + /* Expected: 1 2 3 4 ... 98 99 */ + if (whether_contains_exapected_data(r_buf, READ_BUF_LEN / 2, 1, 1, 1)) { break; } } - printf("Data start index: %d\n", i); - TEST_ASSERT(i < READ_BUF_LEN / 2 - 100); - TEST_ASSERT(i % 2); - for (int16_t j = 1; j < 100; j ++) { - TEST_ASSERT_EQUAL_INT16(r_buf[i++], j); // receive all number + if (retry >= RETEY_TIMES) { + printf("tx/rx stereo test failed\n"); + is_failed = true; + goto err; } printf("tx/rx stereo test passed\n"); @@ -332,20 +350,17 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s_legacy]") * rx receive: 0x01[R] 0x02[R] ... */ TEST_ESP_OK(i2s_set_clk(I2S_NUM_0, SAMPLE_RATE, I2S_BITS_PER_SAMPLE_32BIT, I2S_CHANNEL_MONO)); TEST_ESP_OK(i2s_write(I2S_NUM_0, w_buf, WRITE_BUF_LEN, &w_bytes, portMAX_DELAY)); - TEST_ESP_OK(i2s_read(I2S_NUM_0, r_buf, READ_BUF_LEN, &r_bytes, portMAX_DELAY)); - - for (i = 0; i < READ_BUF_LEN / 2; i++) { - if (r_buf[i] == 1) { - printf("%d %d %d %d\n%d %d %d %d\n", - r_buf[i], r_buf[i+1], r_buf[i+2], r_buf[i+3], - r_buf[i+4], r_buf[i+5], r_buf[i+6], r_buf[i+7]); + for (retry = 0; retry < RETEY_TIMES; retry++) { + TEST_ESP_OK(i2s_read(I2S_NUM_0, r_buf, READ_BUF_LEN, &r_bytes, portMAX_DELAY)); + /* Expected: 1 2 3 4 ... 98 99 */ + if (whether_contains_exapected_data(r_buf, READ_BUF_LEN / 2, 1, 1, 1)) { break; } } - printf("Data start index: %d\n", i); - TEST_ASSERT(i < READ_BUF_LEN / 2 - 100); - for (int16_t j = 1; j < 100; j ++) { - TEST_ASSERT_EQUAL_INT16(r_buf[i++], j); + if (retry >= RETEY_TIMES) { + printf("tx/rx mono test failed\n"); + is_failed = true; + goto err; } printf("tx/rx mono test passed\n"); @@ -367,33 +382,39 @@ TEST_CASE("I2S_mono_stereo_loopback_test", "[i2s_legacy]") * tx format: 0x00[L] 0x01[R] 0x02[L] 0x03[R] ... * rx receive: 0x00[R] 0x02[R] ... */ TEST_ESP_OK(i2s_write(I2S_NUM_0, w_buf, WRITE_BUF_LEN, &w_bytes, portMAX_DELAY)); - TEST_ESP_OK(i2s_read(I2S_NUM_0, r_buf, READ_BUF_LEN, &r_bytes, portMAX_DELAY)); -#if CONFIG_IDF_TARGET_ESP32 - /* The data of tx/rx channels are flipped on ESP32 */ - for (int n = 0; n < READ_BUF_LEN / 2; n += 2) { - int16_t temp = r_buf[n]; - r_buf[n] = r_buf[n+1]; - r_buf[n+1] = temp; - } -#endif - for (i = 0; (i < READ_BUF_LEN / 2); i++) { - if (r_buf[i] == 2) { - printf("%d %d %d %d\n%d %d %d %d\n", - r_buf[i], r_buf[i+1], r_buf[i+2], r_buf[i+3], - r_buf[i+4], r_buf[i+5], r_buf[i+6], r_buf[i+7]); + for (retry = 0; retry < RETEY_TIMES; retry++) { + TEST_ESP_OK(i2s_read(I2S_NUM_0, r_buf, READ_BUF_LEN, &r_bytes, portMAX_DELAY)); + #if CONFIG_IDF_TARGET_ESP32 + /* The data of tx/rx channels are flipped on ESP32 */ + for (int n = 0; n < READ_BUF_LEN / 2; n += 2) { + int16_t temp = r_buf[n]; + r_buf[n] = r_buf[n+1]; + r_buf[n+1] = temp; + } + #endif + /* Expected: 2 4 6 8 10 ... 96 98 */ + if (whether_contains_exapected_data(r_buf, READ_BUF_LEN / 2, 1, 2, 2)) { break; } } - printf("Data start index: %d\n", i); - TEST_ASSERT(i < READ_BUF_LEN / 2 - 50); - for (int16_t j = 2; j < 100; j += 2) { - TEST_ASSERT_EQUAL_INT16(r_buf[i++], j); + if (retry >= RETEY_TIMES) { + printf("rx left mono test failed\n"); + is_failed = true; + goto err; } printf("rx left mono test passed\n"); +err: + if (is_failed) { + for (int i = 0; i < READ_BUF_LEN / 2; i++) { + printf("%x ", r_buf[i]); + } + printf("\n"); + } free(w_buf); free(r_buf); TEST_ESP_OK(i2s_driver_uninstall(I2S_NUM_0)); + TEST_ASSERT_FALSE(is_failed); } #if SOC_I2S_SUPPORTS_TDM