Skip to content

Commit ddb372e

Browse files
decsnykartben
authored andcommitted
tests: spi_loopback: Add testcases for same bufs
A common usage of the SPI API by in tree consumers is to have one underlying buffer used to both RX to and TX from. Add some test cases ensuring this works properly: - A case to ensure that RX writes back properly the same data that was TX from the buf. - A case simulating a very common paradigm which is to use one underlying buf but declare the spi buf sets as though the first word is a command from TX and the rest are data for RX. Signed-off-by: Declan Snyder <[email protected]>
1 parent 610a7e2 commit ddb372e

File tree

1 file changed

+36
-0
lines changed
  • tests/drivers/spi/spi_loopback/src

1 file changed

+36
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,42 @@ ZTEST(spi_loopback, test_nop_nil_bufs)
360360
/* nothing really to check here, check is done in spi_loopback_transceive */
361361
}
362362

363+
/* test using the same buffer for RX and TX will write same data back */
364+
ZTEST(spi_loopback, test_spi_write_back)
365+
{
366+
struct spi_dt_spec *spec = loopback_specs[spec_idx];
367+
const struct spi_buf_set tx = spi_loopback_setup_xfer(rx_bufs_pool, 1,
368+
buffer_rx, BUF_SIZE);
369+
const struct spi_buf_set rx = spi_loopback_setup_xfer(rx_bufs_pool, 1,
370+
buffer_rx, BUF_SIZE);
371+
372+
memcpy(buffer_rx, tx_data, sizeof(tx_data));
373+
374+
spi_loopback_transceive(spec, &tx, &rx);
375+
376+
spi_loopback_compare_bufs(tx_data, buffer_rx, BUF_SIZE,
377+
buffer_print_tx, buffer_print_rx);
378+
}
379+
380+
/* similar to test_spi_write_back, simulates the real common case of 1 word command */
381+
ZTEST(spi_loopback, test_spi_same_buf_cmd)
382+
{
383+
struct spi_dt_spec *spec = loopback_specs[spec_idx];
384+
const struct spi_buf_set tx = spi_loopback_setup_xfer(rx_bufs_pool, 2,
385+
buffer_rx, 1,
386+
NULL, BUF_SIZE - 1);
387+
const struct spi_buf_set rx = spi_loopback_setup_xfer(rx_bufs_pool, 1,
388+
NULL, BUF_SIZE - 1,
389+
buffer_rx+(BUF_SIZE - 1), 1);
390+
391+
memcpy(buffer_rx, tx_data, sizeof(tx_data));
392+
393+
spi_loopback_transceive(spec, &tx, &rx);
394+
395+
spi_loopback_compare_bufs(tx_data, buffer_rx, BUF_SIZE,
396+
buffer_print_tx, buffer_print_rx);
397+
}
398+
363399
#if (CONFIG_SPI_ASYNC)
364400
static struct k_poll_signal async_sig = K_POLL_SIGNAL_INITIALIZER(async_sig);
365401
static struct k_poll_event async_evt =

0 commit comments

Comments
 (0)