Skip to content

Commit 4047889

Browse files
committed
tests: drivers: pinctrl: add tests for API
Add a set of tests to check the API behavior. The API tests can only run on a platform that does not have an actual pinctrl driver, e.g. native_posix. The test itself implements a pinctrl mock driver and provides the required "pinctrl_soc.h" header with required types/macros. The implementation is used in the tests to verify the behavior of the API or Devicetree macros. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent 85101f6 commit 4047889

File tree

13 files changed

+540
-0
lines changed

13 files changed

+540
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2021 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Test device for pin controller.
5+
6+
compatible: "vnd,pinctrl-device"
7+
8+
include: [base.yaml, pinctrl-device.yaml]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright (c) 2021 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
Test pin controller.
6+
7+
compatible: "vnd,pinctrl-test"
8+
9+
include:
10+
- name: base.yaml
11+
- name: pincfg-node-group.yaml
12+
child-binding:
13+
child-binding:
14+
property-allowlist:
15+
- bias-pull-down
16+
- bias-pull-up
17+
18+
child-binding:
19+
description: |
20+
Test pin controller pin configuration nodes. Each node is composed by one or
21+
more groups, each defining the configuration for a set of pins.
22+
23+
child-binding:
24+
description: |
25+
Test pin controller pin configuration group. Each group contains a list of
26+
pins sharing the same set of properties. Example:
27+
28+
/* node representing default state for test_device0 */
29+
test_device0_default: test_device0_default {
30+
/* group 1 (name is arbitrary) */
31+
pins1 {
32+
/* configure pins 0 and 1 */
33+
pins = <0>, <1>;
34+
/* both pins 0 and 1 have pull-up enabled */
35+
bias-pull-up;
36+
};
37+
...
38+
/* group N (name is arbitrary) */
39+
pinsN {
40+
/* configure pin M */
41+
pins = <M>;
42+
/* pin M has pull-down enabled */
43+
bias-pull-down;
44+
};
45+
};
46+
47+
The list of supported standard properties:
48+
49+
- bias-pull-up: Enable pull-up resistor.
50+
- bias-pull-down: Enable pull-down resistor.
51+
52+
properties:
53+
pins:
54+
required: true
55+
type: array
56+
description: |
57+
An array of pins sharing the same group properties. Each entry is a
58+
32-bit integer that is just used to identify the entry for testing
59+
purposes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2021 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
7+
project(pinctrl_api)
8+
9+
zephyr_include_directories(src)
10+
zephyr_include_directories(../common)
11+
12+
target_sources(app PRIVATE src/main.c src/pinctrl_test.c ../common/test_device.c)

tests/drivers/pinctrl/api/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2021 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
mainmenu "pinctrl API Test"
5+
6+
source "Kconfig.zephyr"
7+
8+
config PINCTRL_TEST_STORE_REG
9+
bool "Store register address"
10+
select PINCTRL_STORE_REG
11+
help
12+
This option should be selected by unit tests that want to store the
13+
register address of devices.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2021 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
pinctrl {
9+
compatible = "vnd,pinctrl-test";
10+
11+
/* default state for device 0 */
12+
test_device0_default: test_device0_default {
13+
pins1 {
14+
pins = <0>;
15+
bias-pull-up;
16+
};
17+
pins2 {
18+
pins = <1>;
19+
bias-pull-down;
20+
};
21+
};
22+
23+
/* sleep state for device 0 */
24+
test_device0_sleep: test_device0_sleep {
25+
pins1 {
26+
pins = <0>, <1>;
27+
};
28+
};
29+
30+
/* alternative default state for device 0 */
31+
test_device0_alt_default: test_device0_alt_default {
32+
pins1 {
33+
pins = <2>;
34+
bias-pull-down;
35+
};
36+
pins2 {
37+
pins = <3>;
38+
bias-pull-up;
39+
};
40+
};
41+
42+
/* alternative sleep state for device 0 */
43+
test_device0_alt_sleep: test_device0_alt_sleep {
44+
pins1 {
45+
pins = <2>, <3>;
46+
};
47+
};
48+
49+
/* default state for device 1 */
50+
test_device1_default: test_device1_default {
51+
pins1 {
52+
pins = <10>, <11>, <12>;
53+
};
54+
};
55+
56+
/* custom state "mystate" for device 1 */
57+
test_device1_mystate: test_device1_mystate {
58+
pins1 {
59+
pins = <10>;
60+
};
61+
pins2 {
62+
pins = <11>;
63+
bias-pull-up;
64+
};
65+
pins3 {
66+
pins = <12>;
67+
bias-pull-down;
68+
};
69+
};
70+
};
71+
72+
zephyr,user {
73+
test_device0_alt_default = <&test_device0_alt_default>;
74+
test_device0_alt_sleep = <&test_device0_alt_sleep>;
75+
};
76+
77+
test_device0: test_device@0 {
78+
compatible = "vnd,pinctrl-device";
79+
reg = <0x0 0x1>;
80+
pinctrl-0 = <&test_device0_default>;
81+
pinctrl-1 = <&test_device0_sleep>;
82+
pinctrl-names = "default", "sleep";
83+
};
84+
85+
test_device1: test_device@1 {
86+
compatible = "vnd,pinctrl-device";
87+
reg = <0x1 0x1>;
88+
pinctrl-0 = <&test_device1_default>;
89+
pinctrl-1 = <&test_device1_mystate>;
90+
pinctrl-names = "default", "mystate";
91+
};
92+
};

tests/drivers/pinctrl/api/prj.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) 2021 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_ZTEST=y
5+
CONFIG_ZTEST_MOCKING=y
6+
CONFIG_PINCTRL=y
7+
CONFIG_PINCTRL_DYNAMIC=y

tests/drivers/pinctrl/api/reg.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) 2021 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_PINCTRL_TEST_STORE_REG=y
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Copyright (c) 2021 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <drivers/pinctrl.h>
8+
#include <ztest.h>
9+
10+
#include "test_device.h"
11+
12+
/* test device 0 */
13+
#define TEST_DEVICE0 DT_NODELABEL(test_device0)
14+
PINCTRL_DT_DEV_CONFIG_DECLARE(TEST_DEVICE0);
15+
static struct pinctrl_dev_config *pcfg0 = PINCTRL_DT_DEV_CONFIG_GET(TEST_DEVICE0);
16+
17+
/* test device 1 */
18+
#define TEST_DEVICE1 DT_NODELABEL(test_device1)
19+
PINCTRL_DT_DEV_CONFIG_DECLARE(TEST_DEVICE1);
20+
static struct pinctrl_dev_config *pcfg1 = PINCTRL_DT_DEV_CONFIG_GET(TEST_DEVICE1);
21+
22+
/**
23+
* @brief Test if configuration for device 0 has been stored as expected.
24+
*
25+
* Device 0 is also used to verify that certain states are skipped
26+
* automatically, e.g. "sleep" if CONFIG_PM_DEVICE is not active.
27+
*
28+
* This test together with test_config_dev1 is used to verify that the whole
29+
* set of macros used to define and initialize pin control config from
30+
* Devicetree works as expected.
31+
*/
32+
static void test_config_dev0(void)
33+
{
34+
const struct pinctrl_state *scfg;
35+
36+
zassert_equal(pcfg0->state_cnt, 1, NULL);
37+
#ifdef CONFIG_PINCTRL_STORE_REG
38+
zassert_equal(pcfg0->reg, 0, NULL);
39+
#endif
40+
41+
scfg = &pcfg0->states[0];
42+
zassert_equal(scfg->id, PINCTRL_STATE_DEFAULT, NULL);
43+
zassert_equal(TEST_GET_PIN(scfg->pins[0]), 0, NULL);
44+
zassert_equal(TEST_GET_PULL(scfg->pins[0]), TEST_PULL_UP, NULL);
45+
zassert_equal(TEST_GET_PIN(scfg->pins[1]), 1, NULL);
46+
zassert_equal(TEST_GET_PULL(scfg->pins[1]), TEST_PULL_DOWN, NULL);
47+
}
48+
49+
/**
50+
* @brief Test if configuration for device 1 has been stored as expected.
51+
*
52+
* Device 1 is also used to verify that custom states can be defined.
53+
*
54+
* @see test_config_dev0()
55+
*/
56+
static void test_config_dev1(void)
57+
{
58+
const struct pinctrl_state *scfg;
59+
60+
zassert_equal(pcfg1->state_cnt, 2, NULL);
61+
#ifdef CONFIG_PINCTRL_STORE_REG
62+
zassert_equal(pcfg1->reg, 1, NULL);
63+
#endif
64+
65+
scfg = &pcfg1->states[0];
66+
zassert_equal(scfg->id, PINCTRL_STATE_DEFAULT, NULL);
67+
zassert_equal(scfg->pin_cnt, 3, NULL);
68+
zassert_equal(TEST_GET_PIN(scfg->pins[0]), 10, NULL);
69+
zassert_equal(TEST_GET_PULL(scfg->pins[0]), TEST_PULL_DISABLE, NULL);
70+
zassert_equal(TEST_GET_PIN(scfg->pins[1]), 11, NULL);
71+
zassert_equal(TEST_GET_PULL(scfg->pins[1]), TEST_PULL_DISABLE, NULL);
72+
zassert_equal(TEST_GET_PIN(scfg->pins[2]), 12, NULL);
73+
zassert_equal(TEST_GET_PULL(scfg->pins[2]), TEST_PULL_DISABLE, NULL);
74+
75+
scfg = &pcfg1->states[1];
76+
zassert_equal(scfg->id, PINCTRL_STATE_MYSTATE, NULL);
77+
zassert_equal(scfg->pin_cnt, 3, NULL);
78+
zassert_equal(TEST_GET_PIN(scfg->pins[0]), 10, NULL);
79+
zassert_equal(TEST_GET_PULL(scfg->pins[0]), TEST_PULL_DISABLE, NULL);
80+
zassert_equal(TEST_GET_PIN(scfg->pins[1]), 11, NULL);
81+
zassert_equal(TEST_GET_PULL(scfg->pins[1]), TEST_PULL_UP, NULL);
82+
zassert_equal(TEST_GET_PIN(scfg->pins[2]), 12, NULL);
83+
zassert_equal(TEST_GET_PULL(scfg->pins[2]), TEST_PULL_DOWN, NULL);
84+
}
85+
86+
/**
87+
* @brief Test that pinctrl_lookup_state() works as expected
88+
*/
89+
static void test_lookup_state(void)
90+
{
91+
int ret;
92+
const struct pinctrl_state *scfg;
93+
94+
ret = pinctrl_lookup_state(pcfg0, PINCTRL_STATE_DEFAULT, &scfg);
95+
zassert_equal(ret, 0, NULL);
96+
zassert_equal_ptr(scfg, &pcfg0->states[0], NULL);
97+
98+
ret = pinctrl_lookup_state(pcfg0, PINCTRL_STATE_SLEEP, &scfg);
99+
zassert_equal(ret, -ENOENT, NULL);
100+
}
101+
102+
/**
103+
* @brief Test that pinctrl_apply_state() works as expected.
104+
*/
105+
static void test_apply_state(void)
106+
{
107+
int ret;
108+
109+
ztest_expect_data(pinctrl_configure_pins, pins, pcfg0->states[0].pins);
110+
ztest_expect_value(pinctrl_configure_pins, pin_cnt,
111+
pcfg0->states[0].pin_cnt);
112+
#ifdef CONFIG_PINCTRL_STORE_REG
113+
ztest_expect_value(pinctrl_configure_pins, reg, 0);
114+
#else
115+
ztest_expect_value(pinctrl_configure_pins, reg, PINCTRL_REG_NONE);
116+
#endif
117+
118+
ret = pinctrl_apply_state(pcfg0, PINCTRL_STATE_DEFAULT);
119+
zassert_equal(ret, 0, NULL);
120+
}
121+
122+
/** Test device 0 alternative pins for default state */
123+
PINCTRL_DT_STATE_PINS_DEFINE(DT_PATH(zephyr_user), test_device0_alt_default);
124+
/** Test device 0 alternative pins for sleep state */
125+
PINCTRL_DT_STATE_PINS_DEFINE(DT_PATH(zephyr_user), test_device0_alt_sleep);
126+
127+
/** Test device 0 alternative states. */
128+
static const struct pinctrl_state test_device0_alt[] = {
129+
PINCTRL_DT_STATE_INIT(test_device0_alt_default, PINCTRL_STATE_DEFAULT),
130+
};
131+
132+
/** Test device 0 invalid alternative states. */
133+
static const struct pinctrl_state test_device0_alt_invalid[] = {
134+
PINCTRL_DT_STATE_INIT(test_device0_alt_default, PINCTRL_STATE_DEFAULT),
135+
/* sleep state is skipped (no CONFIG_PM_DEVICE), so it is invalid */
136+
PINCTRL_DT_STATE_INIT(test_device0_alt_sleep, PINCTRL_STATE_SLEEP),
137+
};
138+
139+
/**
140+
* @brief This test checks if pinctrl_update_states() works as expected.
141+
*/
142+
static void test_update_states(void)
143+
{
144+
int ret;
145+
const struct pinctrl_state *scfg;
146+
147+
ret = pinctrl_update_states(pcfg0, test_device0_alt,
148+
ARRAY_SIZE(test_device0_alt));
149+
zassert_equal(ret, 0, NULL);
150+
151+
scfg = &pcfg0->states[0];
152+
zassert_equal(TEST_GET_PIN(scfg->pins[0]), 2, NULL);
153+
zassert_equal(TEST_GET_PULL(scfg->pins[0]), TEST_PULL_DOWN, NULL);
154+
zassert_equal(TEST_GET_PIN(scfg->pins[1]), 3, NULL);
155+
zassert_equal(TEST_GET_PULL(scfg->pins[1]), TEST_PULL_UP, NULL);
156+
157+
ret = pinctrl_update_states(pcfg0, test_device0_alt_invalid,
158+
ARRAY_SIZE(test_device0_alt_invalid));
159+
zassert_equal(ret, -EINVAL, NULL);
160+
}
161+
162+
void test_main(void)
163+
{
164+
ztest_test_suite(pinctrl_api,
165+
ztest_unit_test(test_config_dev0),
166+
ztest_unit_test(test_config_dev1),
167+
ztest_unit_test(test_lookup_state),
168+
ztest_unit_test(test_apply_state),
169+
ztest_unit_test(test_update_states));
170+
ztest_run_test_suite(pinctrl_api);
171+
}

0 commit comments

Comments
 (0)