From 22326f6d99e8e710bbf7e6f9057bb96741e970f6 Mon Sep 17 00:00:00 2001 From: David D Date: Thu, 16 Jan 2020 07:31:56 +0200 Subject: [PATCH] test: drivers: spi: added a test case Added a test case for calling spi_transceive() with a null tx buffer Signed-off-by: David D --- .../spi_loopback/boards/nucleo_f413zh.conf | 2 + tests/drivers/spi/spi_loopback/src/spi.c | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tests/drivers/spi/spi_loopback/boards/nucleo_f413zh.conf diff --git a/tests/drivers/spi/spi_loopback/boards/nucleo_f413zh.conf b/tests/drivers/spi/spi_loopback/boards/nucleo_f413zh.conf new file mode 100644 index 00000000000..2d9df89ecb5 --- /dev/null +++ b/tests/drivers/spi/spi_loopback/boards/nucleo_f413zh.conf @@ -0,0 +1,2 @@ +CONFIG_SPI_LOOPBACK_DRV_NAME="SPI_1" +CONFIG_SPI_STM32_INTERRUPT=y diff --git a/tests/drivers/spi/spi_loopback/src/spi.c b/tests/drivers/spi/spi_loopback/src/spi.c index c94724bfec5..c867979dac4 100644 --- a/tests/drivers/spi/spi_loopback/src/spi.c +++ b/tests/drivers/spi/spi_loopback/src/spi.c @@ -142,6 +142,62 @@ static int spi_complete_loop(struct device *dev, struct spi_config *spi_conf) return 0; } + +static int spi_null_tx_buf(struct device *dev, struct spi_config *spi_conf) +{ + static const u8_t EXPECTED_NOP_RETURN_BUF[BUF_SIZE] = { 0 }; + (void)memset(buffer_rx, 0x77, BUF_SIZE); + + const struct spi_buf tx_bufs[] = { + { + /* + * According to documentation, when sending NULL tx buf - + * NOP frames should be sent on MOSI line + */ + .buf = NULL, + .len = BUF_SIZE, + }, + }; + const struct spi_buf rx_bufs[] = { + { + .buf = buffer_rx, + .len = BUF_SIZE, + }, + }; + const struct spi_buf_set tx = { + .buffers = tx_bufs, + .count = ARRAY_SIZE(tx_bufs) + }; + const struct spi_buf_set rx = { + .buffers = rx_bufs, + .count = ARRAY_SIZE(rx_bufs) + }; + + int ret; + + LOG_INF("Start"); + + ret = spi_transceive(dev, spi_conf, &tx, &rx); + if (ret) { + LOG_ERR("Code %d", ret); + zassert_false(ret, "SPI transceive failed"); + return ret; + } + + + if (memcmp(buffer_rx, EXPECTED_NOP_RETURN_BUF, BUF_SIZE)) { + to_display_format(buffer_rx, BUF_SIZE, buffer_print_rx); + LOG_ERR("Rx Buffer should contain NOP frames but got: %s", + buffer_print_rx); + zassert_false(1, "Buffer not as expected"); + return -1; + } + + LOG_INF("Passed"); + + return 0; +} + static int spi_rx_half_start(struct device *dev, struct spi_config *spi_conf) { const struct spi_buf tx_bufs[] = { @@ -462,6 +518,7 @@ void test_spi_loopback(void) #endif if (spi_complete_loop(spi_slow, &spi_cfg_slow) || + spi_null_tx_buf(spi_slow, &spi_cfg_slow) || spi_rx_half_start(spi_slow, &spi_cfg_slow) || spi_rx_half_end(spi_slow, &spi_cfg_slow) || spi_rx_every_4(spi_slow, &spi_cfg_slow) @@ -473,6 +530,7 @@ void test_spi_loopback(void) } if (spi_complete_loop(spi_fast, &spi_cfg_fast) || + spi_null_tx_buf(spi_fast, &spi_cfg_fast) || spi_rx_half_start(spi_fast, &spi_cfg_fast) || spi_rx_half_end(spi_fast, &spi_cfg_fast) || spi_rx_every_4(spi_fast, &spi_cfg_fast)