Skip to content

Commit 061f2cb

Browse files
committed
tests: drivers: hwspinlock: Add dt_spec test
Adds a hwspinlock driver test to exercise the dt spec macros and detect the context initializer build warning that was fixed in commit 8b208b0. Previously the build warning wasn't reproducible in-tree. Signed-off-by: Maureen Helm <maureen.helm@analog.com>
1 parent df8b43d commit 061f2cb

File tree

11 files changed

+166
-0
lines changed

11 files changed

+166
-0
lines changed

drivers/hwspinlock/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
zephyr_library()
44

55
zephyr_library_sources_ifdef(CONFIG_SQN_HWSPINLOCK sqn_hwspinlock.c)
6+
zephyr_library_sources_ifdef(CONFIG_HWSPINLOCK_TEST hwspinlock_test.c)

drivers/hwspinlock/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ config HWSPINLOCK_INIT_PRIORITY
2121
HW spinlock driver device initialization priority.
2222

2323
source "drivers/hwspinlock/Kconfig.sqn"
24+
source "drivers/hwspinlock/Kconfig.test"
2425

2526
endif

drivers/hwspinlock/Kconfig.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) 2025 Analog Devices, Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
config HWSPINLOCK_TEST
5+
def_bool DT_HAS_VND_HWSPINLOCK_ENABLED
6+
depends on DT_HAS_VND_HWSPINLOCK_ENABLED
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2025 Analog Devices, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/drivers/hwspinlock.h>
9+
10+
#define DT_DRV_COMPAT vnd_hwspinlock
11+
12+
static void vnd_hwspinlock_lock(const struct device *dev, uint32_t id)
13+
{
14+
}
15+
16+
static void vnd_hwspinlock_unlock(const struct device *dev, uint32_t id)
17+
{
18+
}
19+
20+
static uint32_t vnd_hwspinlock_get_max_id(const struct device *dev)
21+
{
22+
return 0;
23+
}
24+
25+
static DEVICE_API(hwspinlock, vnd_hwspinlock_api) = {
26+
.lock = vnd_hwspinlock_lock,
27+
.unlock = vnd_hwspinlock_unlock,
28+
.get_max_id = vnd_hwspinlock_get_max_id,
29+
};
30+
31+
#define VND_HWSPINLOCK_INIT(idx) \
32+
DEVICE_DT_INST_DEFINE(idx, NULL, NULL, NULL, NULL, POST_KERNEL, \
33+
CONFIG_HWSPINLOCK_INIT_PRIORITY, \
34+
&vnd_hwspinlock_api)
35+
36+
DT_INST_FOREACH_STATUS_OKAY(VND_HWSPINLOCK_INIT);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2025 Analog Devices, Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Test hardware spinlock device node
5+
6+
compatible: "vnd,hwspinlock-device"
7+
8+
include: base.yaml
9+
10+
properties:
11+
hwlocks:
12+
required: true
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2025 Analog Devices, Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Test hardware spinlock node
5+
6+
compatible: "vnd,hwspinlock"
7+
8+
include: [base.yaml, hwspinlock-controller.yaml]
9+
10+
hwlock-cells:
11+
- id
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 Analog Devices, Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(spi_dt_spec)
8+
9+
FILE(GLOB app_sources src/*.c)
10+
target_sources(app PRIVATE ${app_sources})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2025 Analog Devices, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
*/
7+
8+
/ {
9+
test {
10+
test_hwspinlock: hwspinlock {
11+
compatible = "vnd,hwspinlock";
12+
#hwlock-cells = <1>;
13+
status = "okay";
14+
};
15+
16+
test_hwspinlock_dev: hwspinlock-dev {
17+
compatible = "vnd,hwspinlock-device";
18+
hwlocks = <&test_hwspinlock 1>, <&test_hwspinlock 2>;
19+
hwlock-names = "rd", "wr";
20+
status = "okay";
21+
};
22+
};
23+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Copyright (c) 2025 Analog Devices, Inc.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
CONFIG_ZTEST=y
5+
CONFIG_HWSPINLOCK=y
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2025 Analog Devices, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
#include <zephyr/devicetree.h>
9+
#include <zephyr/device.h>
10+
#include <zephyr/drivers/hwspinlock.h>
11+
12+
#define TEST_HWSPINLOCK \
13+
DT_NODELABEL(test_hwspinlock)
14+
15+
#define TEST_HWSPINLOCK_DEV \
16+
DT_NODELABEL(test_hwspinlock_dev)
17+
18+
#define HWSPINLOCK_BY_IDX(node_id, prop, idx) \
19+
HWSPINLOCK_DT_SPEC_GET_BY_IDX(node_id, idx)
20+
21+
static const struct hwspinlock_dt_spec spec[] = {
22+
DT_FOREACH_PROP_ELEM_SEP(TEST_HWSPINLOCK_DEV, hwlocks, HWSPINLOCK_BY_IDX, (,))
23+
};
24+
25+
static const struct hwspinlock_dt_spec rd =
26+
HWSPINLOCK_DT_SPEC_GET_BY_NAME(TEST_HWSPINLOCK_DEV, rd);
27+
28+
static const struct hwspinlock_dt_spec wr =
29+
HWSPINLOCK_DT_SPEC_GET_BY_NAME(TEST_HWSPINLOCK_DEV, wr);
30+
31+
ZTEST(hwspinlock_dt_spec, test_dt_spec)
32+
{
33+
for (int i = 0; i < ARRAY_SIZE(spec); i++) {
34+
zassert_equal(spec[i].dev, DEVICE_DT_GET(TEST_HWSPINLOCK));
35+
zassert_equal(spec[i].id, i + 1);
36+
}
37+
38+
zassert_equal(rd.dev, DEVICE_DT_GET(TEST_HWSPINLOCK));
39+
zassert_equal(rd.id, 1);
40+
41+
zassert_equal(wr.dev, DEVICE_DT_GET(TEST_HWSPINLOCK));
42+
zassert_equal(wr.id, 2);
43+
}
44+
45+
ZTEST_SUITE(hwspinlock_dt_spec, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)