1 /*
2 * Copyright (c) 2023 Vestas Wind Systems A/S
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/drivers/gpio.h>
8 #include <zephyr/ztest.h>
9
10 #define ZEPHYR_USER_NODE DT_PATH(zephyr_user)
11
12 const struct gpio_dt_spec output_high_gpio =
13 GPIO_DT_SPEC_GET_OR(ZEPHYR_USER_NODE, output_high_gpios, {0});
14 const struct gpio_dt_spec output_low_gpio =
15 GPIO_DT_SPEC_GET_OR(ZEPHYR_USER_NODE, output_low_gpios, {0});
16 const struct gpio_dt_spec input_gpio =
17 GPIO_DT_SPEC_GET_OR(ZEPHYR_USER_NODE, input_gpios, {0});
18
assert_gpio_hog_direction(const struct gpio_dt_spec * spec,bool output)19 static void assert_gpio_hog_direction(const struct gpio_dt_spec *spec, bool output)
20 {
21 int err;
22
23 if (spec->port == NULL) {
24 ztest_test_skip();
25 }
26
27 if (output) {
28 err = gpio_pin_is_output(spec->port, spec->pin);
29 } else {
30 err = gpio_pin_is_input(spec->port, spec->pin);
31 }
32
33 if (err == -ENOSYS) {
34 ztest_test_skip();
35 }
36
37 zassert_equal(err, 1, "GPIO hog %s pin %d not configured as %s",
38 spec->port->name, spec->pin,
39 output ? "output" : "input");
40 }
41
ZTEST(gpio_hogs,test_gpio_hog_output_high_direction)42 ZTEST(gpio_hogs, test_gpio_hog_output_high_direction)
43 {
44 assert_gpio_hog_direction(&output_high_gpio, true);
45 }
46
ZTEST(gpio_hogs,test_gpio_hog_output_low_direction)47 ZTEST(gpio_hogs, test_gpio_hog_output_low_direction)
48 {
49 assert_gpio_hog_direction(&output_low_gpio, true);
50 }
51
ZTEST(gpio_hogs,test_gpio_hog_input_direction)52 ZTEST(gpio_hogs, test_gpio_hog_input_direction)
53 {
54 assert_gpio_hog_direction(&input_gpio, false);
55 }
56
assert_gpio_hog_config(const struct gpio_dt_spec * spec,gpio_flags_t expected)57 static void assert_gpio_hog_config(const struct gpio_dt_spec *spec, gpio_flags_t expected)
58 {
59 gpio_flags_t actual;
60 int err;
61
62 if (spec->port == NULL) {
63 ztest_test_skip();
64 }
65
66 err = gpio_pin_get_config_dt(spec, &actual);
67 if (err == -ENOSYS) {
68 ztest_test_skip();
69 }
70
71 zassert_equal(err, 0, "failed to get config for GPIO hog %s, pin %d (err %d)",
72 spec->port->name, spec->pin, err);
73 zassert_equal(actual & expected, expected,
74 "GPIO hog %s, pin %d flags not set (0x%08x vs. 0x%08x)",
75 spec->port->name, spec->pin, actual, expected);
76 }
77
ZTEST(gpio_hogs,test_gpio_hog_output_high_config)78 ZTEST(gpio_hogs, test_gpio_hog_output_high_config)
79 {
80 gpio_flags_t expected = GPIO_OUTPUT;
81
82 if ((output_high_gpio.dt_flags & GPIO_ACTIVE_LOW) != 0) {
83 expected |= GPIO_OUTPUT_INIT_LOW;
84 } else {
85 expected |= GPIO_OUTPUT_INIT_HIGH;
86 }
87
88 assert_gpio_hog_config(&output_high_gpio, expected);
89 }
90
ZTEST(gpio_hogs,test_gpio_hog_output_low_config)91 ZTEST(gpio_hogs, test_gpio_hog_output_low_config)
92 {
93 gpio_flags_t expected = GPIO_OUTPUT;
94
95 if ((output_low_gpio.dt_flags & GPIO_ACTIVE_LOW) == 0) {
96 expected |= GPIO_OUTPUT_INIT_LOW;
97 } else {
98 expected |= GPIO_OUTPUT_INIT_HIGH;
99 }
100
101 assert_gpio_hog_config(&output_low_gpio, expected);
102 }
103
ZTEST(gpio_hogs,test_gpio_hog_input_config)104 ZTEST(gpio_hogs, test_gpio_hog_input_config)
105 {
106 assert_gpio_hog_config(&input_gpio, GPIO_INPUT);
107 }
108
109 ZTEST_SUITE(gpio_hogs, NULL, NULL, NULL, NULL, NULL);
110