Skip to content

Fix spi loopback thread test on fail case #90766

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 2 commits into from
May 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions tests/drivers/spi/spi_loopback/src/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ ZTEST(spi_loopback, test_spi_complete_multiple_timed)
buffer2_rx, BUF2_SIZE);
uint32_t freq = spec->config.frequency;
uint32_t start_time, end_time, cycles_spent;
uint64_t time_spent_us, expected_transfer_time_us;
uint64_t time_spent_us, expected_transfer_time_us, latency_measurement;

/* since this is a test program, there shouldn't be much to interfere with measurement */
start_time = k_cycle_get_32();
Expand Down Expand Up @@ -329,7 +329,12 @@ ZTEST(spi_loopback, test_spi_complete_multiple_timed)
zassert_true(time_spent_us >= minimum_transfer_time_us,
"Transfer faster than theoretically possible");

TC_PRINT("Latency measurement: %llu us\n", time_spent_us - expected_transfer_time_us);
/* handle overflow for print statement */
latency_measurement = time_spent_us - expected_transfer_time_us;
if (latency_measurement > time_spent_us) {
latency_measurement = 0;
}
TC_PRINT("Latency measurement: %llu us\n", latency_measurement);

/* Allow some overhead, but not too much */
zassert_true(time_spent_us <= expected_transfer_time_us * 8, "Very high latency");
Expand Down Expand Up @@ -695,16 +700,17 @@ static K_SEM_DEFINE(sync_sem, 0, 1);
static uint8_t __aligned(32) tx_buffer[3][32];
static uint8_t __aligned(32) rx_buffer[3][32];

atomic_t thread_test_fails;

static void spi_transfer_thread(void *p1, void *p2, void *p3)
{
struct spi_dt_spec *spec = (struct spi_dt_spec *)p1;
uint8_t *tx_buf_ptr = (uint8_t *)p2;
uint8_t *rx_buf_ptr = (uint8_t *)p3;
int ret = 0;

/* Wait for all threads to be ready */
k_sem_give(&thread_sem);
k_sem_take(&sync_sem, K_FOREVER);

/* Perform SPI transfer */
const struct spi_buf_set tx_bufs = {
.buffers = &(struct spi_buf) {
Expand All @@ -721,11 +727,19 @@ static void spi_transfer_thread(void *p1, void *p2, void *p3)
.count = 1,
};

zassert_equal(spi_transceive_dt(spec, &tx_bufs, &rx_bufs), 0,
"SPI concurrent transfer failed");
k_sem_take(&sync_sem, K_FOREVER);

zassert_mem_equal(tx_buf_ptr, rx_buf_ptr, 32,
"SPI concurrent transfer data mismatch");
ret = spi_transceive_dt(spec, &tx_bufs, &rx_bufs);
if (ret) {
TC_PRINT("SPI concurrent transfer failed, spec %p\n", spec);
atomic_inc(&thread_test_fails);
}

ret = memcmp(tx_buf_ptr, rx_buf_ptr, 32);
if (ret) {
TC_PRINT("SPI concurrent transfer data mismatch, spec %p\n", spec);
atomic_inc(&thread_test_fails);
}
}

/* Test case for concurrent SPI transfers */
Expand All @@ -747,6 +761,8 @@ static void test_spi_concurrent_transfer_helper(struct spi_dt_spec **specs)
k_sem_take(&thread_sem, K_FOREVER);
}

atomic_set(&thread_test_fails, 0);

/* Start all threads simultaneously */
for (int i = 0; i < 3; i++) {
k_sem_give(&sync_sem);
Expand All @@ -756,6 +772,8 @@ static void test_spi_concurrent_transfer_helper(struct spi_dt_spec **specs)
for (int i = 0; i < 3; i++) {
k_thread_join(&thread[i], K_FOREVER);
}

zassert_equal(atomic_get(&thread_test_fails), 0);
}

/* test for multiple threads accessing the driver / bus with the same spi_config */
Expand Down