Skip to content

Commit 6f00e75

Browse files
bjarki-andreasencarlescufi
authored andcommitted
[nrf fromtree] tests: drivers: spi: loopback: test device_deinit
Introduce test for device_deinit() which deinitializes the spi bus, then configures miso as input, mosi as output using gpio, utilizing the loopback to test directly controlling the pins. Then reinit the spi device. Signed-off-by: Bjarki Arge Andreasen <[email protected]> (cherry picked from commit ecb5457)
1 parent 310720c commit 6f00e75

File tree

1 file changed

+32
-1
lines changed
  • tests/drivers/spi/spi_loopback/src

1 file changed

+32
-1
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <zephyr/ztest.h>
1717
#include <zephyr/drivers/spi.h>
1818
#include <zephyr/pm/device_runtime.h>
19+
#include <zephyr/drivers/gpio.h>
1920
#include <zephyr/kernel.h>
2021
#include <stdio.h>
2122
#include <stdarg.h>
@@ -56,7 +57,8 @@ static int spec_idx;
5657
*/
5758
struct spi_dt_spec spec_copies[5];
5859

59-
60+
const struct gpio_dt_spec miso_pin = GPIO_DT_SPEC_GET_OR(DT_PATH(zephyr_user), miso_gpios, {});
61+
const struct gpio_dt_spec mosi_pin = GPIO_DT_SPEC_GET_OR(DT_PATH(zephyr_user), mosi_gpios, {});
6062

6163
/*
6264
********************
@@ -807,6 +809,35 @@ ZTEST(spi_loopback, test_spi_concurrent_transfer_different_spec)
807809
test_spi_concurrent_transfer_helper(specs);
808810
}
809811

812+
ZTEST(spi_loopback, test_spi_deinit)
813+
{
814+
struct spi_dt_spec *spec = loopback_specs[0];
815+
const struct device *dev = spec->bus;
816+
int ret;
817+
818+
if (miso_pin.port == NULL || mosi_pin.port == NULL) {
819+
TC_PRINT(" zephyr,user miso-gpios or mosi-gpios are not defined\n");
820+
ztest_test_skip();
821+
}
822+
823+
ret = device_deinit(dev);
824+
if (ret == -ENOTSUP) {
825+
TC_PRINT(" device deinit not supported\n");
826+
ztest_test_skip();
827+
}
828+
829+
zassert_ok(ret);
830+
zassert_ok(gpio_pin_configure_dt(&miso_pin, GPIO_INPUT));
831+
zassert_ok(gpio_pin_configure_dt(&mosi_pin, GPIO_OUTPUT_INACTIVE));
832+
zassert_equal(gpio_pin_get_dt(&miso_pin), 0);
833+
zassert_ok(gpio_pin_set_dt(&mosi_pin, 1));
834+
zassert_equal(gpio_pin_get_dt(&miso_pin), 1);
835+
zassert_ok(gpio_pin_set_dt(&mosi_pin, 0));
836+
zassert_equal(gpio_pin_get_dt(&miso_pin), 0);
837+
zassert_ok(gpio_pin_configure_dt(&mosi_pin, GPIO_INPUT));
838+
zassert_ok(device_init(dev));
839+
}
840+
810841
#if (CONFIG_SPI_ASYNC)
811842
static struct k_poll_signal async_sig = K_POLL_SIGNAL_INITIALIZER(async_sig);
812843
static struct k_poll_event async_evt =

0 commit comments

Comments
 (0)