Skip to content

Commit ecb5457

Browse files
bjarki-andreasenkartben
authored andcommitted
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 <bjarki.andreasen@nordicsemi.no>
1 parent 2609cfb commit ecb5457

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
********************
@@ -831,6 +833,35 @@ ZTEST(spi_loopback, test_spi_concurrent_transfer_different_spec)
831833
test_spi_concurrent_transfer_helper(specs);
832834
}
833835

836+
ZTEST(spi_loopback, test_spi_deinit)
837+
{
838+
struct spi_dt_spec *spec = loopback_specs[0];
839+
const struct device *dev = spec->bus;
840+
int ret;
841+
842+
if (miso_pin.port == NULL || mosi_pin.port == NULL) {
843+
TC_PRINT(" zephyr,user miso-gpios or mosi-gpios are not defined\n");
844+
ztest_test_skip();
845+
}
846+
847+
ret = device_deinit(dev);
848+
if (ret == -ENOTSUP) {
849+
TC_PRINT(" device deinit not supported\n");
850+
ztest_test_skip();
851+
}
852+
853+
zassert_ok(ret);
854+
zassert_ok(gpio_pin_configure_dt(&miso_pin, GPIO_INPUT));
855+
zassert_ok(gpio_pin_configure_dt(&mosi_pin, GPIO_OUTPUT_INACTIVE));
856+
zassert_equal(gpio_pin_get_dt(&miso_pin), 0);
857+
zassert_ok(gpio_pin_set_dt(&mosi_pin, 1));
858+
zassert_equal(gpio_pin_get_dt(&miso_pin), 1);
859+
zassert_ok(gpio_pin_set_dt(&mosi_pin, 0));
860+
zassert_equal(gpio_pin_get_dt(&miso_pin), 0);
861+
zassert_ok(gpio_pin_configure_dt(&mosi_pin, GPIO_INPUT));
862+
zassert_ok(device_init(dev));
863+
}
864+
834865
#if (CONFIG_SPI_ASYNC)
835866
static struct k_poll_signal async_sig = K_POLL_SIGNAL_INITIALIZER(async_sig);
836867
static struct k_poll_event async_evt =

0 commit comments

Comments
 (0)