Skip to content

Commit 60d9a16

Browse files
decsnykartben
authored andcommitted
tests: spi_loopback: Add different wordsize tests
Add tests for different data frame sizes, as long as the controller can handle that word size. Starting out with new tests of 7, 9, 16, 24, 32, 64. The skipping behavior of ZTest is still not clear for me so for now just return instead of using zassume or ztest skip if not supported. Signed-off-by: Declan Snyder <[email protected]>
1 parent ddb372e commit 60d9a16

File tree

1 file changed

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

1 file changed

+118
-0
lines changed

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

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ static struct spi_dt_spec *loopback_specs[2] = {&spi_slow, &spi_fast};
4343
static char *spec_names[2] = {"SLOW", "FAST"};
4444
static int spec_idx;
4545

46+
/* Driver may need to reconfigure due to different spi config for the
47+
* some of the tests. If we use the same memory location for the spec,
48+
* it will be treated like the same owner of the bus as previously,
49+
* and many drivers will think it is the same spec and skip reconfiguring.
50+
* Even declaring a copy of the spec on the stack will most likely have it end up at the same
51+
* location on the stack every time and have the same problem.
52+
* It's not clear from the API if this is expected but it is a common trick that is de facto
53+
* standardized by the use of the private spi_context header, so for now consider it
54+
* expected behavior in this test, and write the test accordingly. The point of those cases is
55+
* not to test the specifics of bus ownership in the API, so we just deal with it.
56+
*/
57+
struct spi_dt_spec spec_copies[5];
58+
4659
/*
4760
********************
4861
* SPI test buffers *
@@ -72,6 +85,18 @@ static const char large_tx_data[BUF3_SIZE] = "Thequickbrownfoxjumpsoverthelazydo
7285
static __aligned(32) char large_buffer_tx[BUF3_SIZE] __NOCACHE;
7386
static __aligned(32) char large_buffer_rx[BUF3_SIZE] __NOCACHE;
7487

88+
#define BUFWIDE_SIZE 12
89+
static const uint16_t tx_data_16[] = {0x1234, 0x5678, 0x9ABC, 0xDEF0,
90+
0xFF00, 0x00FF, 0xAAAA, 0x5555,
91+
0xF0F0, 0x0F0F, 0xA5A5, 0x5A5A};
92+
static __aligned(32) uint16_t buffer_tx_16[BUFWIDE_SIZE] __NOCACHE;
93+
static __aligned(32) uint16_t buffer_rx_16[BUFWIDE_SIZE] __NOCACHE;
94+
static const uint32_t tx_data_32[] = {0x12345678, 0x56781234, 0x9ABCDEF0, 0xDEF09ABC,
95+
0xFFFF0000, 0x0000FFFF, 0x00FF00FF, 0xFF00FF00,
96+
0xAAAA5555, 0x5555AAAA, 0xAA55AA55, 0x55AA55AA};
97+
static __aligned(32) uint32_t buffer_tx_32[BUFWIDE_SIZE] __NOCACHE;
98+
static __aligned(32) uint32_t buffer_rx_32[BUFWIDE_SIZE] __NOCACHE;
99+
75100
/*
76101
********************
77102
* Helper functions *
@@ -109,7 +134,12 @@ static void spi_loopback_transceive(struct spi_dt_spec *const spec,
109134

110135
zassert_ok(pm_device_runtime_get(spec->bus));
111136
ret = spi_transceive_dt(spec, tx, rx);
137+
if (ret == -EINVAL || ret == -ENOTSUP) {
138+
TC_PRINT("Spi config invalid for this controller - skip\n");
139+
goto out;
140+
}
112141
zassert_ok(ret, "SPI transceive failed, code %d", ret);
142+
out:
113143
zassert_ok(pm_device_runtime_put(spec->bus));
114144
}
115145

@@ -396,6 +426,90 @@ ZTEST(spi_loopback, test_spi_same_buf_cmd)
396426
buffer_print_tx, buffer_print_rx);
397427
}
398428

429+
430+
static void spi_loopback_test_word_size(struct spi_dt_spec *spec,
431+
const void *tx_data,
432+
void *rx_buffer,
433+
const void *compare_data,
434+
size_t buffer_size,
435+
struct spi_dt_spec *spec_copy,
436+
uint8_t word_size)
437+
{
438+
struct spi_config config_copy = spec->config;
439+
440+
config_copy.operation &= ~SPI_WORD_SIZE_MASK;
441+
config_copy.operation |= SPI_WORD_SET(word_size);
442+
spec_copy->config = config_copy;
443+
spec_copy->bus = spec->bus;
444+
445+
const struct spi_buf_set tx = spi_loopback_setup_xfer(tx_bufs_pool, 1,
446+
tx_data, buffer_size);
447+
const struct spi_buf_set rx = spi_loopback_setup_xfer(rx_bufs_pool, 1,
448+
rx_buffer, buffer_size);
449+
450+
spi_loopback_transceive(spec_copy, &tx, &rx);
451+
452+
zassert_false(memcmp(compare_data, rx_buffer, buffer_size),
453+
"%d-bit word buffer contents are different", word_size);
454+
}
455+
456+
/* Test case for 7-bit word size transfers */
457+
ZTEST(spi_loopback, test_spi_word_size_7)
458+
{
459+
struct spi_dt_spec *spec = loopback_specs[spec_idx];
460+
461+
spi_loopback_test_word_size(spec, tx_data, buffer_rx, tx_data,
462+
sizeof(tx_data), &spec_copies[0], 7);
463+
}
464+
465+
/* Test case for 9-bit word size transfers */
466+
ZTEST(spi_loopback, test_spi_word_size_9)
467+
{
468+
struct spi_dt_spec *spec = loopback_specs[spec_idx];
469+
470+
static __aligned(32) uint16_t tx_data_9[BUFWIDE_SIZE];
471+
472+
for (int i = 0; i < BUFWIDE_SIZE; i++) {
473+
tx_data_9[i] = tx_data_16[i] & 0x1FF;
474+
}
475+
476+
spi_loopback_test_word_size(spec, tx_data_9, buffer_rx_16, tx_data_9,
477+
sizeof(tx_data_9), &spec_copies[1], 9);
478+
}
479+
480+
/* Test case for 16-bit word size transfers */
481+
ZTEST(spi_loopback, test_spi_word_size_16)
482+
{
483+
struct spi_dt_spec *spec = loopback_specs[spec_idx];
484+
485+
spi_loopback_test_word_size(spec, buffer_tx_16, buffer_rx_16, tx_data_16,
486+
sizeof(buffer_tx_16), &spec_copies[2], 16);
487+
}
488+
489+
/* Test case for 24-bit word size transfers */
490+
ZTEST(spi_loopback, test_spi_word_size_24)
491+
{
492+
struct spi_dt_spec *spec = loopback_specs[spec_idx];
493+
494+
static __aligned(32) uint32_t tx_data_24[BUFWIDE_SIZE];
495+
496+
for (int i = 0; i < BUFWIDE_SIZE; i++) {
497+
tx_data_24[i] = tx_data_32[i] & 0xFFFFFF;
498+
}
499+
500+
spi_loopback_test_word_size(spec, tx_data_24, buffer_rx_32, tx_data_24,
501+
sizeof(tx_data_24), &spec_copies[3], 24);
502+
}
503+
504+
/* Test case for 32-bit word size transfers */
505+
ZTEST(spi_loopback, test_spi_word_size_32)
506+
{
507+
struct spi_dt_spec *spec = loopback_specs[spec_idx];
508+
509+
spi_loopback_test_word_size(spec, buffer_tx_32, buffer_rx_32, tx_data_32,
510+
sizeof(buffer_tx_32), &spec_copies[4], 32);
511+
}
512+
399513
#if (CONFIG_SPI_ASYNC)
400514
static struct k_poll_signal async_sig = K_POLL_SIGNAL_INITIALIZER(async_sig);
401515
static struct k_poll_event async_evt =
@@ -507,6 +621,10 @@ static void *spi_loopback_common_setup(void)
507621
memcpy(buffer2_tx, tx2_data, sizeof(tx2_data));
508622
memset(large_buffer_tx, 0, sizeof(large_buffer_tx));
509623
memcpy(large_buffer_tx, large_tx_data, sizeof(large_tx_data));
624+
memset(buffer_tx_16, 0, sizeof(buffer_tx_16));
625+
memcpy(buffer_tx_16, tx_data_16, sizeof(tx_data_16));
626+
memset(buffer_tx_32, 0, sizeof(buffer_tx_32));
627+
memcpy(buffer_tx_32, tx_data_32, sizeof(tx_data_32));
510628
return NULL;
511629
}
512630

0 commit comments

Comments
 (0)