1 /*
2 * Copyright (c) 2024 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/drivers/gpio.h>
9 #include <zephyr/dt-bindings/gpio/nordic-nrf-gpio.h>
10 #include <zephyr/ztest.h>
11
12 #if DT_NODE_HAS_PROP(DT_ALIAS(led0), gpios)
13 #define TEST_NODE DT_GPIO_CTLR(DT_ALIAS(led0), gpios)
14 #define TEST_PIN DT_GPIO_PIN(DT_ALIAS(led0), gpios)
15 #else
16 #error Unsupported board
17 #endif
18
19 /*
20 * Nordic Semiconductor specific pin drive configurations
21 */
ZTEST(gpio_nrf,test_gpio_high_drive_strength)22 ZTEST(gpio_nrf, test_gpio_high_drive_strength)
23 {
24 int err;
25 const struct device *port;
26
27 port = DEVICE_DT_GET(TEST_NODE);
28 zassert_true(device_is_ready(port), "GPIO dev is not ready");
29
30 err = gpio_pin_configure(port, TEST_PIN, GPIO_PUSH_PULL | NRF_GPIO_DRIVE_S0H1);
31 zassert_equal(
32 err, 0,
33 "Failed to configure the pin as an P-P output with drive: NRF_GPIO_DRIVE_S0H1, err=%d",
34 err);
35
36 err = gpio_pin_configure(port, TEST_PIN, GPIO_PUSH_PULL | NRF_GPIO_DRIVE_H0S1);
37 zassert_equal(
38 err, 0,
39 "Failed to configure the pin as an P-P output with drive: NRF_GPIO_DRIVE_H0S1, err=%d",
40 err);
41
42 err = gpio_pin_configure(port, TEST_PIN, GPIO_PUSH_PULL | NRF_GPIO_DRIVE_H0H1);
43 zassert_equal(
44 err, 0,
45 "Failed to configure the pin as an P-P output with drive: NRF_GPIO_DRIVE_H0H1, err=%d",
46 err);
47
48 err = gpio_pin_configure(port, TEST_PIN, GPIO_OPEN_DRAIN | NRF_GPIO_DRIVE_H0S1);
49 zassert_equal(
50 err, 0,
51 "Failed to configure the pin as an O-D output with drive: NRF_GPIO_DRIVE_H0S1, err=%d",
52 err);
53
54 err = gpio_pin_configure(port, TEST_PIN, GPIO_OPEN_SOURCE | NRF_GPIO_DRIVE_S0H1);
55 zassert_equal(
56 err, 0,
57 "Failed to configure the pin as an O-S output with drive: NRF_GPIO_DRIVE_S0H1, err=%d",
58 err);
59 }
60
61 /*
62 * Nordic Semiconductor specific,
63 * gpio manipulation with disabled NRFX interrupts
64 */
ZTEST(gpio_nrf,test_gpio_manipulation_nrfx_int_disabled)65 ZTEST(gpio_nrf, test_gpio_manipulation_nrfx_int_disabled)
66 {
67 int response;
68 const struct device *port;
69
70 port = DEVICE_DT_GET(TEST_NODE);
71 zassert_true(device_is_ready(port), "GPIO dev is not ready");
72
73 response = gpio_pin_configure(port, TEST_PIN, GPIO_OUTPUT | GPIO_ACTIVE_HIGH);
74 zassert_ok(response, "Pin configuration failed: %d", response);
75
76 response = gpio_pin_set(port, TEST_PIN, 0);
77 zassert_ok(response, "Pin low state set failed: %d", response);
78
79 response = gpio_pin_set(port, TEST_PIN, 1);
80 zassert_ok(response, "Pin high state set failed: %d", response);
81
82 response = gpio_pin_toggle(port, TEST_PIN);
83 zassert_ok(response, "Pin toggle failed: %d", response);
84
85 response = gpio_pin_configure(port, TEST_PIN, GPIO_INPUT | GPIO_PULL_DOWN);
86 zassert_ok(response, "Failed to configure pin as input with pull down: %d", response);
87
88 response = gpio_pin_get(port, TEST_PIN);
89 zassert_equal(response, 0, "Invalid pin state: %d", response);
90
91 response = gpio_pin_interrupt_configure(port, TEST_PIN, GPIO_INT_ENABLE | GPIO_INT_HIGH_1);
92 zassert_equal(response, -ENOSYS);
93 }
94
95 ZTEST_SUITE(gpio_nrf, NULL, NULL, NULL, NULL, NULL);
96