|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/logging/log.h> |
| 8 | +LOG_MODULE_REGISTER(gpio_swd, LOG_LEVEL_INF); |
| 9 | + |
| 10 | +#include <zephyr/kernel.h> |
| 11 | +#include <zephyr/drivers/gpio.h> |
| 12 | +#include <zephyr/ztest.h> |
| 13 | + |
| 14 | + |
| 15 | +#if !DT_NODE_HAS_PROP(DT_PATH(zephyr_user), out_gpios) |
| 16 | +#error "Unsupported board: out_gpios are not defined" |
| 17 | +#endif |
| 18 | + |
| 19 | +const struct gpio_dt_spec out_pins[] = { |
| 20 | + DT_FOREACH_PROP_ELEM_SEP(DT_PATH(zephyr_user), out_gpios, GPIO_DT_SPEC_GET_BY_IDX, (,)) |
| 21 | +}; |
| 22 | +BUILD_ASSERT(ARRAY_SIZE(out_pins) > 0, "missing out pins"); |
| 23 | +const uint8_t npins = ARRAY_SIZE(out_pins); |
| 24 | + |
| 25 | + |
| 26 | +/** |
| 27 | + * @brief Test if GPIOs can be configured as inputs. |
| 28 | + */ |
| 29 | +ZTEST(gpio_swd, test_configure_input) |
| 30 | +{ |
| 31 | + uint8_t i; |
| 32 | + int rc; |
| 33 | + |
| 34 | + for (i = 0; i < npins; i++) { |
| 35 | + rc = gpio_pin_configure_dt(&out_pins[i], GPIO_INPUT); |
| 36 | + zassert_equal(rc, 0, "[%d]: Port %s, pin %d, config GPIO_INPUT failed", |
| 37 | + i, out_pins[i].port->name, out_pins[i].pin); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * @brief Test if GPIOs can be configured as outputs. |
| 43 | + */ |
| 44 | +ZTEST(gpio_swd, test_configure_output) |
| 45 | +{ |
| 46 | + uint8_t i; |
| 47 | + int rc; |
| 48 | + |
| 49 | + for (i = 0; i < npins; i++) { |
| 50 | + rc = gpio_pin_configure_dt(&out_pins[i], GPIO_OUTPUT); |
| 51 | + zassert_equal(rc, 0, "[%d]: Port %s, pin %d, config GPIO_OUTPUT failed", |
| 52 | + i, out_pins[i].port->name, out_pins[i].pin); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * @brief Test if GPIOs configured as output can have its value set. |
| 58 | + */ |
| 59 | +ZTEST(gpio_swd, test_set_output) |
| 60 | +{ |
| 61 | + uint8_t i; |
| 62 | + int rc; |
| 63 | + |
| 64 | + for (i = 0; i < npins; i++) { |
| 65 | + rc = gpio_pin_configure_dt(&out_pins[i], GPIO_OUTPUT_HIGH); |
| 66 | + zassert_equal(rc, 0, "[%d]: Port %s, pin %d, config high failed", |
| 67 | + i, out_pins[i].port->name, out_pins[i].pin); |
| 68 | + rc = gpio_pin_get_dt(&out_pins[i]); |
| 69 | + zassert_equal(rc, 1, "[%d]: Port %s, pin %d is not high", |
| 70 | + i, out_pins[i].port->name, out_pins[i].pin); |
| 71 | + |
| 72 | + rc = gpio_pin_configure_dt(&out_pins[i], GPIO_OUTPUT_LOW); |
| 73 | + zassert_equal(rc, 0, "[%d]: Port %s, pin %d, config low failed", |
| 74 | + i, out_pins[i].port->name, out_pins[i].pin); |
| 75 | + rc = gpio_pin_get_dt(&out_pins[i]); |
| 76 | + zassert_equal(rc, 0, "[%d]: Port %s, pin %d is not low", |
| 77 | + i, out_pins[i].port->name, out_pins[i].pin); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * @brief Test if GPIOs configured as output can have its value set. |
| 83 | + */ |
| 84 | +ZTEST(gpio_swd, test_set_output_after_5_seconds) |
| 85 | +{ |
| 86 | + uint8_t i; |
| 87 | + int rc; |
| 88 | + |
| 89 | + k_msleep(5000); |
| 90 | + for (i = 0; i < npins; i++) { |
| 91 | + rc = gpio_pin_configure_dt(&out_pins[i], GPIO_OUTPUT_HIGH); |
| 92 | + zassert_equal(rc, 0, "[%d]: Port %s, pin %d, config high failed", |
| 93 | + i, out_pins[i].port->name, out_pins[i].pin); |
| 94 | + rc = gpio_pin_get_dt(&out_pins[i]); |
| 95 | + zassert_equal(rc, 1, "[%d]: Port %s, pin %d is not high", |
| 96 | + i, out_pins[i].port->name, out_pins[i].pin); |
| 97 | + |
| 98 | + rc = gpio_pin_configure_dt(&out_pins[i], GPIO_OUTPUT_LOW); |
| 99 | + zassert_equal(rc, 0, "[%d]: Port %s, pin %d, config low failed", |
| 100 | + i, out_pins[i].port->name, out_pins[i].pin); |
| 101 | + rc = gpio_pin_get_dt(&out_pins[i]); |
| 102 | + zassert_equal(rc, 0, "[%d]: Port %s, pin %d is not low", |
| 103 | + i, out_pins[i].port->name, out_pins[i].pin); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +static void *suite_setup(void) |
| 108 | +{ |
| 109 | + uint8_t i; |
| 110 | + |
| 111 | + TC_PRINT("Test executed on %s\n", CONFIG_BOARD_TARGET); |
| 112 | + TC_PRINT("GPIO count: %d\n", npins); |
| 113 | + for (i = 0; i < npins; i++) { |
| 114 | + TC_PRINT("%d: Port %s, pin %d\n", i, out_pins[i].port->name, out_pins[i].pin); |
| 115 | + } |
| 116 | + TC_PRINT("===================================================================\n"); |
| 117 | + |
| 118 | + for (i = 0; i < npins; i++) { |
| 119 | + zassert_true(gpio_is_ready_dt(&out_pins[i]), "OUT[%d] is not ready", i); |
| 120 | + } |
| 121 | + |
| 122 | + return NULL; |
| 123 | +} |
| 124 | + |
| 125 | +ZTEST_SUITE(gpio_swd, NULL, suite_setup, NULL, NULL, NULL); |
0 commit comments