Skip to content

Commit b2b1198

Browse files
committed
tests: spi_loopback: Fix fail handle of thread test
Right now the thread test case does not handle when the test fails, the test will be aborted by ztest in one of the spawned threads, and cause desynchronization which messes up the rest of the test. Fix by synchronizing in the case of fails and return fail from main test thread instead of the spawned ones. Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
1 parent 714d544 commit b2b1198

File tree

1 file changed

+19
-6
lines changed
  • tests/drivers/spi/spi_loopback/src

1 file changed

+19
-6
lines changed

tests/drivers/spi/spi_loopback/src/spi.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -700,16 +700,17 @@ static K_SEM_DEFINE(sync_sem, 0, 1);
700700
static uint8_t __aligned(32) tx_buffer[3][32];
701701
static uint8_t __aligned(32) rx_buffer[3][32];
702702

703+
atomic_t thread_test_fails;
704+
703705
static void spi_transfer_thread(void *p1, void *p2, void *p3)
704706
{
705707
struct spi_dt_spec *spec = (struct spi_dt_spec *)p1;
706708
uint8_t *tx_buf_ptr = (uint8_t *)p2;
707709
uint8_t *rx_buf_ptr = (uint8_t *)p3;
710+
int ret = 0;
708711

709712
/* Wait for all threads to be ready */
710713
k_sem_give(&thread_sem);
711-
k_sem_take(&sync_sem, K_FOREVER);
712-
713714
/* Perform SPI transfer */
714715
const struct spi_buf_set tx_bufs = {
715716
.buffers = &(struct spi_buf) {
@@ -726,11 +727,19 @@ static void spi_transfer_thread(void *p1, void *p2, void *p3)
726727
.count = 1,
727728
};
728729

729-
zassert_equal(spi_transceive_dt(spec, &tx_bufs, &rx_bufs), 0,
730-
"SPI concurrent transfer failed");
730+
k_sem_take(&sync_sem, K_FOREVER);
731+
732+
ret = spi_transceive_dt(spec, &tx_bufs, &rx_bufs);
733+
if (ret) {
734+
TC_PRINT("SPI concurrent transfer failed, spec %p\n", spec);
735+
atomic_inc(&thread_test_fails);
736+
}
731737

732-
zassert_mem_equal(tx_buf_ptr, rx_buf_ptr, 32,
733-
"SPI concurrent transfer data mismatch");
738+
ret = memcmp(tx_buf_ptr, rx_buf_ptr, 32);
739+
if (ret) {
740+
TC_PRINT("SPI concurrent transfer data mismatch, spec %p\n", spec);
741+
atomic_inc(&thread_test_fails);
742+
}
734743
}
735744

736745
/* Test case for concurrent SPI transfers */
@@ -752,6 +761,8 @@ static void test_spi_concurrent_transfer_helper(struct spi_dt_spec **specs)
752761
k_sem_take(&thread_sem, K_FOREVER);
753762
}
754763

764+
atomic_set(&thread_test_fails, 0);
765+
755766
/* Start all threads simultaneously */
756767
for (int i = 0; i < 3; i++) {
757768
k_sem_give(&sync_sem);
@@ -761,6 +772,8 @@ static void test_spi_concurrent_transfer_helper(struct spi_dt_spec **specs)
761772
for (int i = 0; i < 3; i++) {
762773
k_thread_join(&thread[i], K_FOREVER);
763774
}
775+
776+
zassert_equal(atomic_get(&thread_test_fails), 0);
764777
}
765778

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

0 commit comments

Comments
 (0)