Skip to content

Commit 2eb8cfb

Browse files
decsnykartben
authored andcommitted
tests: spi_loopback: Fix synchronization mistakes
There are a couple things going wrong in this test code right now. First of all, there is no syncrhonization of when the async polling thread should start polling, causing sometimes the poll to fail on tests before the async test ran. Add a semaphore to fix this. The lock test was setting up a nonsense NOP transfer of 0 length. This might cause unexpected behavior and is not the point of this test case. But, still we should add a seperate test case specifically for testing this instead. Also add a teardown function to release the locks to allow other test cases to run in case of some failures. Also a minor change to remove some ARG_UNUSED invocations for some arguments that are actually used. Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
1 parent f107dad commit 2eb8cfb

File tree

1 file changed

+29
-5
lines changed
  • tests/drivers/spi/spi_loopback/src

1 file changed

+29
-5
lines changed

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,21 +321,31 @@ ZTEST(spi_loopback, test_spi_complete_large_transfers)
321321
"Large Buffer contents are different");
322322
}
323323

324+
ZTEST(spi_loopback, test_nop_nil_bufs)
325+
{
326+
struct spi_dt_spec *spec = loopback_specs[spec_idx];
327+
const struct spi_buf_set tx = spi_loopback_setup_xfer(tx_bufs_pool, 1, NULL, 0);
328+
const struct spi_buf_set rx = spi_loopback_setup_xfer(rx_bufs_pool, 1, NULL, 0);
329+
330+
spi_loopback_transceive(spec, &tx, &rx);
331+
332+
/* nothing really to check here, check is done in spi_loopback_transceive */
333+
}
334+
324335
#if (CONFIG_SPI_ASYNC)
325336
static struct k_poll_signal async_sig = K_POLL_SIGNAL_INITIALIZER(async_sig);
326337
static struct k_poll_event async_evt =
327338
K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
328339
K_POLL_MODE_NOTIFY_ONLY,
329340
&async_sig);
330341
static K_SEM_DEFINE(caller, 0, 1);
342+
static K_SEM_DEFINE(start_async, 0, 1);
331343
static int result = 1;
332344

333345
static void spi_async_call_cb(void *p1,
334346
void *p2,
335347
void *p3)
336348
{
337-
ARG_UNUSED(p1);
338-
ARG_UNUSED(p2);
339349
ARG_UNUSED(p3);
340350

341351
struct k_poll_event *evt = p1;
@@ -344,6 +354,8 @@ static void spi_async_call_cb(void *p1,
344354
TC_PRINT("Polling...");
345355

346356
while (1) {
357+
k_sem_take(&start_async, K_FOREVER);
358+
347359
zassert_false(k_poll(evt, 1, K_MSEC(2000)), "one or more events are not ready");
348360

349361
result = evt->signal->result;
@@ -371,6 +383,8 @@ ZTEST(spi_loopback, test_spi_async_call)
371383
memset(buffer2_rx, 0, sizeof(buffer2_rx));
372384
memset(large_buffer_rx, 0, sizeof(large_buffer_rx));
373385

386+
k_sem_give(&start_async);
387+
374388
int ret = spi_transceive_signal(spec->bus, &spec->config, &tx, &rx, &async_sig);
375389

376390
if (ret == -ENOTSUP) {
@@ -396,8 +410,10 @@ ZTEST(spi_loopback, test_spi_async_call)
396410

397411
ZTEST(spi_extra_api_features, test_spi_lock_release)
398412
{
399-
const struct spi_buf_set tx = spi_loopback_setup_xfer(tx_bufs_pool, 1, NULL, 0);
400-
const struct spi_buf_set rx = spi_loopback_setup_xfer(rx_bufs_pool, 1, NULL, 0);
413+
const struct spi_buf_set tx = spi_loopback_setup_xfer(tx_bufs_pool, 1,
414+
buffer_tx, BUF_SIZE);
415+
const struct spi_buf_set rx = spi_loopback_setup_xfer(rx_bufs_pool, 1,
416+
NULL, BUF_SIZE);
401417
struct spi_dt_spec *lock_spec = &spi_slow;
402418
struct spi_dt_spec *try_spec = &spi_fast;
403419

@@ -441,8 +457,16 @@ static void run_after_suite(void *unused)
441457
spec_idx++;
442458
}
443459

460+
static void run_after_lock(void *unused)
461+
{
462+
spi_release_dt(&spi_fast);
463+
spi_release_dt(&spi_slow);
464+
spi_slow.config.operation &= ~SPI_LOCK_ON;
465+
spi_fast.config.operation &= ~SPI_LOCK_ON;
466+
}
467+
444468
ZTEST_SUITE(spi_loopback, NULL, spi_loopback_setup, NULL, NULL, run_after_suite);
445-
ZTEST_SUITE(spi_extra_api_features, NULL, spi_loopback_common_setup, NULL, NULL, NULL);
469+
ZTEST_SUITE(spi_extra_api_features, NULL, spi_loopback_common_setup, NULL, NULL, run_after_lock);
446470

447471
struct k_thread async_thread;
448472
k_tid_t async_thread_id;

0 commit comments

Comments
 (0)