Skip to content
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

[nrf fromtree] tests: drivers: spi: spi_slave: Extend test scope #1726

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(spi_slave)
project(spi_controller_peripheral)

FILE(GLOB app_sources src/*.c)

Expand Down
14 changes: 14 additions & 0 deletions tests/drivers/spi/spi_controller_peripheral/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2024 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0

config TESTED_SPI_MODE
int "SPI mode"
default 0
help
SPI mode value (clock polarity and phase) used in the test.
0: CPOL 0 (Active high), CPHA 0 (leading)
1: CPOL 0 (Active high), CPHA 1 (trailing)
2: CPOL 1 (Active low), CPHA 0 (leading)
3: CPOL 1 (Active low), CPHA 1 (trailing)

source "Kconfig.zephyr"
9 changes: 9 additions & 0 deletions tests/drivers/spi/spi_controller_peripheral/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
In this test suite two instances of the SPI peripheral are connected together.
One SPI instance works as a controller, second one is configured as a peripheral.
In each test, both instances get identical configuration (CPOL, CPHA, bitrate, etc.).

Four GPIO loopbacks are required (see overlay for nrf54l15pdk for reference):
1. spi22-SPIM_SCK connected with spi21-SPIS_SCK,
2. spi22-SPIM_MISO connected with spi21-SPIS_MISO,
3. spi22-SPIM_MOSI connected with spi21-SPIS_MOSI,
4. spi22-cs-gpios connected with spi21-SPIS_CSN.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

&dut_spi_dt {
spi-max-frequency = <1000000>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

&dut_spi_dt {
spi-max-frequency = <250000>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

&dut_spi_dt {
spi-max-frequency = <2000000>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

&dut_spi_dt {
spi-max-frequency = <4000000>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

&dut_spi_dt {
spi-max-frequency = <500000>;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2024 Nordic Semiconductor
*
* SPDX-License-Identifier: Apache-2.0
*/

&dut_spi_dt {
spi-max-frequency = <8000000>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
dut_spi_dt: test-spi-dev@0 {
compatible = "vnd,spi-device";
reg = <0>;
spi-max-frequency = <4000000>;
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ dut_spi: &spi130 {
dut_spi_dt: test-spi-dev@0 {
compatible = "vnd,spi-device";
reg = <0>;
spi-max-frequency = <500000>;
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
dut_spi_dt: test-spi-dev@0 {
compatible = "vnd,spi-device";
reg = <0>;
spi-max-frequency = <4000000>;
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
#include <zephyr/linker/devicetree_regions.h>
#include <zephyr/ztest.h>

#define SPI_MODE (SPI_MODE_CPOL | SPI_MODE_CPHA | SPI_WORD_SET(8) | SPI_LINES_SINGLE)
#if CONFIG_TESTED_SPI_MODE == 0
#define SPI_MODE (SPI_WORD_SET(8) | SPI_LINES_SINGLE | SPI_TRANSFER_LSB)
#elif CONFIG_TESTED_SPI_MODE == 1
#define SPI_MODE (SPI_WORD_SET(8) | SPI_LINES_SINGLE | SPI_TRANSFER_MSB | SPI_MODE_CPHA)
#elif CONFIG_TESTED_SPI_MODE == 2
#define SPI_MODE (SPI_WORD_SET(8) | SPI_LINES_SINGLE | SPI_TRANSFER_LSB | SPI_MODE_CPOL)
#elif CONFIG_TESTED_SPI_MODE == 3
#define SPI_MODE (SPI_WORD_SET(8) | SPI_LINES_SINGLE | SPI_TRANSFER_MSB | SPI_MODE_CPHA \
| SPI_MODE_CPOL)
#endif

#define SPIM_OP (SPI_OP_MODE_MASTER | SPI_MODE)
#define SPIS_OP (SPI_OP_MODE_SLAVE | SPI_MODE)

Expand Down Expand Up @@ -136,15 +146,15 @@ static int check_buffers(struct spi_buf_set *tx_set, struct spi_buf_set *rx_set,
return memcmp(tx_data, rx_data, rx_len);
}

/** Calculate expected number of received bytes by the slave.
/** Calculate expected number of received bytes by the SPI peripheral.
*
* It is used to check if SPI API call for slave returns correct value.
* It is used to check if SPI API call for peripheral SPI device returns correct value.
* @param tx_set TX set.
* @param rx_set RX set.
*
* @return Expected amount of received bytes.
*/
static int slave_rx_len(struct spi_buf_set *tx_set, struct spi_buf_set *rx_set)
static int peripheral_rx_len(struct spi_buf_set *tx_set, struct spi_buf_set *rx_set)
{
size_t tx_len = 0;
size_t rx_len = 0;
Expand All @@ -168,15 +178,15 @@ static int slave_rx_len(struct spi_buf_set *tx_set, struct spi_buf_set *rx_set)
static void run_test(bool m_same_size, bool s_same_size, bool async)
{
int rv;
int slave_rv;
int periph_rv;
int srx_len;

rv = k_work_schedule(&tdata.test_work, K_MSEC(10));
zassert_equal(rv, 1);

if (!async) {
slave_rv = spi_transceive(spis_dev, &spis_config, tdata.stx_set, tdata.srx_set);
if (slave_rv == -ENOTSUP) {
periph_rv = spi_transceive(spis_dev, &spis_config, tdata.stx_set, tdata.srx_set);
if (periph_rv == -ENOTSUP) {
ztest_test_skip();
}
} else {
Expand All @@ -194,7 +204,7 @@ static void run_test(bool m_same_size, bool s_same_size, bool async)
rv = k_poll(&async_evt, 1, K_MSEC(200));
zassert_false(rv, "one or more events are not ready");

slave_rv = async_evt.signal->result;
periph_rv = async_evt.signal->result;

/* Reinitializing for next call */
async_evt.signal->signaled = 0U;
Expand All @@ -204,9 +214,9 @@ static void run_test(bool m_same_size, bool s_same_size, bool async)
rv = k_sem_take(&tdata.sem, K_MSEC(100));
zassert_equal(rv, 0);

srx_len = slave_rx_len(tdata.mtx_set, tdata.srx_set);
srx_len = peripheral_rx_len(tdata.mtx_set, tdata.srx_set);

zassert_equal(slave_rv, srx_len, "Got: %d but expected:%d", slave_rv, srx_len);
zassert_equal(periph_rv, srx_len, "Got: %d but expected:%d", periph_rv, srx_len);

rv = check_buffers(tdata.mtx_set, tdata.srx_set, m_same_size);
zassert_equal(rv, 0);
Expand All @@ -215,7 +225,7 @@ static void run_test(bool m_same_size, bool s_same_size, bool async)
zassert_equal(rv, 0);
}

/** Basic test where slave and master have RX and TX sets which contains only one
/** Basic test where SPI controller and SPI peripheral have RX and TX sets which contains only one
* same size buffer.
*/
static void test_basic(bool async)
Expand All @@ -237,19 +247,70 @@ static void test_basic(bool async)
run_test(true, true, async);
}

ZTEST(spi_slave, test_basic)
ZTEST(spi_controller_peripheral, test_basic)
{
test_basic(false);
}

ZTEST(spi_slave, test_basic_async)
ZTEST(spi_controller_peripheral, test_basic_async)
{
test_basic(true);
}

/** Setup a transfer where RX buffer on master and slave are shorter than
* TX buffers. RX buffers shall contain beginning of TX data and last TX
* bytes that did not fit in the RX buffers shall be lost.
/** Basic test with zero length buffers.
*/
void test_basic_zero_len(bool async)
{
size_t len = 8;

/* SPIM */
tdata.bufs[0].buf = buf_alloc(len, true);
tdata.bufs[0].len = len;
tdata.bufs[1].buf = buf_alloc(len, true);
/* Intentionally len was set to 0 - second buffer "is empty". */
tdata.bufs[1].len = 0;
tdata.sets[0].buffers = &tdata.bufs[0];
tdata.sets[0].count = 2;
tdata.mtx_set = &tdata.sets[0];

tdata.bufs[2].buf = buf_alloc(len, true);
tdata.bufs[2].len = len;
tdata.bufs[3].buf = buf_alloc(len, true);
/* Intentionally len was set to 0 - second buffer "is empty". */
tdata.bufs[3].len = 0;
tdata.sets[1].buffers = &tdata.bufs[2];
tdata.sets[1].count = 2;
tdata.mrx_set = &tdata.sets[1];

/* SPIS */
tdata.bufs[4].buf = buf_alloc(len, false);
tdata.bufs[4].len = len;
tdata.sets[2].buffers = &tdata.bufs[4];
tdata.sets[2].count = 1;
tdata.stx_set = &tdata.sets[2];

tdata.bufs[6].buf = buf_alloc(len, false);
tdata.bufs[6].len = len;
tdata.sets[3].buffers = &tdata.bufs[6];
tdata.sets[3].count = 1;
tdata.srx_set = &tdata.sets[3];

run_test(true, true, async);
}

ZTEST(spi_controller_peripheral, test_basic_zero_len)
{
test_basic_zero_len(false);
}

ZTEST(spi_controller_peripheral, test_basic_zero_len_async)
{
test_basic_zero_len(true);
}

/** Setup a transfer where RX buffer on SPI controller and SPI peripheral are
* shorter than TX buffers. RX buffers shall contain beginning of TX data
* and last TX bytes that did not fit in the RX buffers shall be lost.
*/
static void test_short_rx(bool async)
{
Expand Down Expand Up @@ -277,12 +338,12 @@ static void test_short_rx(bool async)
run_test(false, false, async);
}

ZTEST(spi_slave, test_short_rx)
ZTEST(spi_controller_peripheral, test_short_rx)
{
test_short_rx(false);
}

ZTEST(spi_slave, test_short_rx_async)
ZTEST(spi_controller_peripheral, test_short_rx_async)
{
test_short_rx(true);
}
Expand Down Expand Up @@ -311,17 +372,17 @@ static void test_only_tx(bool async)
run_test(true, true, async);
}

ZTEST(spi_slave, test_only_tx)
ZTEST(spi_controller_peripheral, test_only_tx)
{
test_only_tx(false);
}

ZTEST(spi_slave, test_only_tx_async)
ZTEST(spi_controller_peripheral, test_only_tx_async)
{
test_only_tx(true);
}

/** Test where only master transmits and slave receives in chunks. */
/** Test where only SPI controller transmits and SPI peripheral receives in chunks. */
static void test_only_tx_in_chunks(bool async)
{
size_t len1 = 7;
Expand All @@ -348,17 +409,17 @@ static void test_only_tx_in_chunks(bool async)
run_test(true, true, async);
}

ZTEST(spi_slave, test_only_tx_in_chunks)
ZTEST(spi_controller_peripheral, test_only_tx_in_chunks)
{
test_only_tx_in_chunks(false);
}

ZTEST(spi_slave, test_only_tx_in_chunks_async)
ZTEST(spi_controller_peripheral, test_only_tx_in_chunks_async)
{
test_only_tx_in_chunks(true);
}

/** Test where only slave transmits. */
/** Test where only SPI peripheral transmits. */
static void test_only_rx(bool async)
{
size_t len = 16;
Expand All @@ -382,17 +443,17 @@ static void test_only_rx(bool async)
run_test(true, true, async);
}

ZTEST(spi_slave, test_only_rx)
ZTEST(spi_controller_peripheral, test_only_rx)
{
test_only_rx(false);
}

ZTEST(spi_slave, test_only_rx_async)
ZTEST(spi_controller_peripheral, test_only_rx_async)
{
test_only_rx(true);
}

/** Test where only slave transmits in chunks. */
/** Test where only SPI peripheral transmits in chunks. */
static void test_only_rx_in_chunks(bool async)
{
size_t len1 = 7;
Expand All @@ -419,12 +480,12 @@ static void test_only_rx_in_chunks(bool async)
run_test(true, true, async);
}

ZTEST(spi_slave, test_only_rx_in_chunks)
ZTEST(spi_controller_peripheral, test_only_rx_in_chunks)
{
test_only_rx_in_chunks(false);
}

ZTEST(spi_slave, test_only_rx_in_chunks_async)
ZTEST(spi_controller_peripheral, test_only_rx_in_chunks_async)
{
test_only_rx_in_chunks(true);
}
Expand All @@ -450,4 +511,4 @@ static void *suite_setup(void)
return NULL;
}

ZTEST_SUITE(spi_slave, NULL, suite_setup, before, NULL, NULL);
ZTEST_SUITE(spi_controller_peripheral, NULL, suite_setup, before, NULL, NULL);
Loading
Loading