Skip to content

Commit 7e4bb0a

Browse files
[nrf fromlist] 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. Upstream PR #: 91956 Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent ed912ed commit 7e4bb0a

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
********************
@@ -806,6 +808,35 @@ ZTEST(spi_loopback, test_spi_concurrent_transfer_different_spec)
806808
test_spi_concurrent_transfer_helper(specs);
807809
}
808810

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

0 commit comments

Comments
 (0)