Skip to content

Commit cd5ca18

Browse files
committed
[nrf fromtree] tests: boards: nordic: add nrf comp tests.
Tests use nordic specific comparator api which enable it's runtime reconfiguration. As there were no tests prepared when introducing this api, PR adds some to extend test coverage. Signed-off-by: Bartlomiej Buczek <[email protected]> (cherry picked from commit dc7c396)
1 parent 693769a commit cd5ca18

File tree

6 files changed

+393
-0
lines changed

6 files changed

+393
-0
lines changed

tests/boards/nrf/comp/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2024 Nordic Semiconductor
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+
project(comp)
7+
8+
target_sources(app PRIVATE src/test.c)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
aliases {
9+
test-comp = &comp;
10+
};
11+
12+
zephyr,user {
13+
first-gpios = <&gpio7 2 GPIO_ACTIVE_HIGH>;
14+
second-gpios = <&gpio7 1 GPIO_ACTIVE_HIGH>;
15+
};
16+
};
17+
18+
&gpio7 {
19+
status = "okay";
20+
};
21+
22+
&gpio1 {
23+
status = "okay";
24+
};
25+
26+
&comp {
27+
status = "okay";
28+
psel = "AIN5";
29+
refsel = "AREF";
30+
extrefsel= "AIN1";
31+
sp-mode = "HIGH";
32+
th-up = <36>;
33+
th-down = <30>;
34+
isource = "DISABLED";
35+
enable-hyst;
36+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/ {
8+
aliases {
9+
test-comp = &comp;
10+
};
11+
12+
zephyr,user {
13+
first-gpios = <&gpio1 10 GPIO_ACTIVE_HIGH>;
14+
second-gpios = <&gpio1 13 GPIO_ACTIVE_HIGH>;
15+
};
16+
};
17+
18+
&led3 {
19+
status = "disabled";
20+
};
21+
22+
&button0 {
23+
status = "disabled";
24+
};
25+
26+
&gpio1 {
27+
status = "okay";
28+
};
29+
30+
&comp {
31+
status = "okay";
32+
psel = "AIN4";
33+
refsel = "AREF";
34+
extrefsel= "AIN3";
35+
sp-mode = "NORMAL";
36+
th-up = <36>;
37+
th-down = <30>;
38+
isource = "DISABLED";
39+
enable-hyst;
40+
};

tests/boards/nrf/comp/prj.conf

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

tests/boards/nrf/comp/src/test.c

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
/*
2+
* Copyright (c) 2024, Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
#include <zephyr/drivers/comparator/nrf_comp.h>
9+
#include <zephyr/drivers/gpio.h>
10+
#include <zephyr/kernel.h>
11+
12+
static const struct device *test_dev = DEVICE_DT_GET(DT_ALIAS(test_comp));
13+
static const struct gpio_dt_spec test_pin_1 = GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), first_gpios);
14+
static const struct gpio_dt_spec test_pin_2 = GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), second_gpios);
15+
16+
17+
struct comp_nrf_comp_se_config comp_se_config = {
18+
.psel = COMP_NRF_COMP_PSEL_AIN5,
19+
.sp_mode = COMP_NRF_COMP_SP_MODE_HIGH,
20+
.isource = COMP_NRF_COMP_ISOURCE_DISABLED,
21+
.refsel = COMP_NRF_COMP_REFSEL_AREF,
22+
.extrefsel = COMP_NRF_COMP_EXTREFSEL_AIN1,
23+
.th_up = 32,
24+
.th_down = 32,
25+
};
26+
27+
struct comp_nrf_comp_diff_config comp_diff_config = {
28+
.psel = COMP_NRF_COMP_PSEL_AIN4,
29+
.sp_mode = COMP_NRF_COMP_SP_MODE_LOW,
30+
.isource = COMP_NRF_COMP_ISOURCE_DISABLED,
31+
.extrefsel = COMP_NRF_COMP_EXTREFSEL_AIN5,
32+
.enable_hyst = true,
33+
};
34+
35+
atomic_t counter;
36+
37+
static void test_callback(const struct device *dev, void *user_data)
38+
{
39+
counter++;
40+
}
41+
42+
/**
43+
* @brief Configure comparator in single-ended mode with
44+
* external voltage reference.
45+
* Check if events were detected.
46+
*/
47+
48+
ZTEST(comparator_runtime_configure, test_comp_config_se_aref)
49+
{
50+
int rc;
51+
52+
rc = comp_nrf_comp_configure_se(test_dev, &comp_se_config);
53+
zassert_equal(rc, 0, "Cannot configure comparator.");
54+
55+
rc = comparator_set_trigger_callback(test_dev, test_callback, NULL);
56+
zassert_equal(rc, 0, "Cannot set callback for comparator.");
57+
58+
rc = comparator_set_trigger(test_dev, COMPARATOR_TRIGGER_BOTH_EDGES);
59+
zassert_equal(rc, 0, "Cannot set trigger for comparator.");
60+
61+
k_msleep(10);
62+
63+
atomic_clear(&counter);
64+
gpio_pin_set_dt(&test_pin_2, 1);
65+
k_msleep(10);
66+
zassert_equal(atomic_get(&counter), 1, "COMP was not triggered for first threshold cross");
67+
gpio_pin_set_dt(&test_pin_2, 0);
68+
k_msleep(10);
69+
zassert_equal(atomic_get(&counter), 2, "COMP was not triggered for second threshold cross");
70+
71+
}
72+
73+
/**
74+
* @brief Configure comparator in single-ended mode with
75+
* internal voltage reference.
76+
* Check if events were detected.
77+
*/
78+
79+
80+
ZTEST(comparator_runtime_configure, test_comp_config_se_vdd)
81+
{
82+
int rc;
83+
84+
struct comp_nrf_comp_se_config conf = comp_se_config;
85+
86+
#ifdef COMP_REFSEL_REFSEL_AVDDAO1V8
87+
conf.refsel = COMP_NRF_COMP_REFSEL_AVDDAO1V8;
88+
#else
89+
conf.refsel = COMP_NRF_COMP_REFSEL_VDD;
90+
#endif
91+
rc = comp_nrf_comp_configure_se(test_dev, &conf);
92+
zassert_equal(rc, 0, "Cannot configure comparator.");
93+
94+
rc = comparator_set_trigger_callback(test_dev, test_callback, NULL);
95+
zassert_equal(rc, 0, "Cannot set callback for comparator.");
96+
97+
rc = comparator_set_trigger(test_dev, COMPARATOR_TRIGGER_BOTH_EDGES);
98+
zassert_equal(rc, 0, "Cannot set trigger for comparator.");
99+
100+
k_msleep(10);
101+
102+
atomic_clear(&counter);
103+
gpio_pin_set_dt(&test_pin_2, 1);
104+
k_msleep(10);
105+
zassert_equal(atomic_get(&counter), 1, "COMP was not triggered for first threshold cross");
106+
gpio_pin_set_dt(&test_pin_2, 0);
107+
k_msleep(10);
108+
zassert_equal(atomic_get(&counter), 2, "COMP was not triggered for second threshold cross");
109+
110+
}
111+
112+
/**
113+
* @brief Configure comparator in differential mode
114+
* Check if events were detected.
115+
*/
116+
117+
ZTEST(comparator_runtime_configure, test_comp_config_diff_both)
118+
{
119+
int rc;
120+
121+
gpio_pin_set_dt(&test_pin_1, 1);
122+
gpio_pin_set_dt(&test_pin_2, 0);
123+
comparator_trigger_is_pending(test_dev);
124+
125+
atomic_clear(&counter);
126+
struct comp_nrf_comp_diff_config config = comp_diff_config;
127+
128+
config.isource = COMP_NRF_COMP_ISOURCE_2UA5;
129+
rc = comp_nrf_comp_configure_diff(test_dev, &config);
130+
zassert_equal(rc, 0, "Cannot configure comparator.");
131+
132+
rc = comparator_set_trigger_callback(test_dev, test_callback, NULL);
133+
zassert_equal(rc, 0, "Cannot set callback for comparator.");
134+
135+
rc = comparator_set_trigger(test_dev, COMPARATOR_TRIGGER_BOTH_EDGES);
136+
zassert_equal(rc, 0, "Cannot set trigger for comparator.");
137+
k_msleep(10);
138+
139+
atomic_clear(&counter);
140+
gpio_pin_set_dt(&test_pin_1, 0);
141+
gpio_pin_set_dt(&test_pin_2, 1);
142+
k_msleep(10);
143+
zassert_equal(atomic_get(&counter), 1, "COMP was not triggered for first threshold cross");
144+
145+
gpio_pin_set_dt(&test_pin_2, 0);
146+
gpio_pin_set_dt(&test_pin_1, 1);
147+
k_msleep(10);
148+
149+
zassert_equal(atomic_get(&counter), 2, "COMP was not triggered for second threshold cross");
150+
}
151+
152+
/**
153+
* @brief Configure comparator in differential mode
154+
* trigger both edges, event should be detected for falling one
155+
*/
156+
157+
ZTEST(comparator_runtime_configure, test_comp_config_diff_falling)
158+
{
159+
int rc;
160+
161+
gpio_pin_set_dt(&test_pin_1, 0);
162+
gpio_pin_set_dt(&test_pin_2, 1);
163+
comparator_trigger_is_pending(test_dev);
164+
165+
atomic_clear(&counter);
166+
struct comp_nrf_comp_diff_config config = comp_diff_config;
167+
168+
config.isource = COMP_NRF_COMP_ISOURCE_5UA;
169+
rc = comp_nrf_comp_configure_diff(test_dev, &config);
170+
171+
zassert_equal(rc, 0, "Cannot configure comparator.");
172+
173+
rc = comparator_set_trigger_callback(test_dev, test_callback, NULL);
174+
zassert_equal(rc, 0, "Cannot set callback for comparator.");
175+
176+
rc = comparator_set_trigger(test_dev, COMPARATOR_TRIGGER_FALLING_EDGE);
177+
zassert_equal(rc, 0, "Cannot set trigger for comparator.");
178+
179+
atomic_clear(&counter);
180+
gpio_pin_set_dt(&test_pin_1, 1);
181+
gpio_pin_set_dt(&test_pin_2, 0);
182+
k_msleep(10);
183+
184+
zassert_equal(counter, 0, "COMP was triggered for rising threshold cross");
185+
186+
gpio_pin_set_dt(&test_pin_1, 0);
187+
gpio_pin_set_dt(&test_pin_2, 1);
188+
k_msleep(10);
189+
190+
zassert_equal(atomic_get(&counter), 1, "COMP wasn't triggered for falling threshold cross");
191+
}
192+
193+
/**
194+
* @brief Configure comparator in differential mode
195+
* trigger both edges, event should be detected for rising one
196+
*/
197+
198+
ZTEST(comparator_runtime_configure, test_comp_config_diff_rising)
199+
{
200+
int rc;
201+
202+
gpio_pin_set_dt(&test_pin_1, 1);
203+
gpio_pin_set_dt(&test_pin_2, 0);
204+
comparator_trigger_is_pending(test_dev);
205+
206+
atomic_clear(&counter);
207+
struct comp_nrf_comp_diff_config config = comp_diff_config;
208+
209+
config.isource = COMP_NRF_COMP_ISOURCE_10UA;
210+
rc = comp_nrf_comp_configure_diff(test_dev, &config);
211+
zassert_equal(rc, 0, "Cannot configure comparator.");
212+
213+
rc = comparator_set_trigger_callback(test_dev, test_callback, NULL);
214+
zassert_equal(rc, 0, "Cannot set callback for comparator.");
215+
216+
rc = comparator_set_trigger(test_dev, COMPARATOR_TRIGGER_RISING_EDGE);
217+
zassert_equal(rc, 0, "Cannot set trigger for comparator.");
218+
219+
atomic_clear(&counter);
220+
gpio_pin_set_dt(&test_pin_1, 0);
221+
gpio_pin_set_dt(&test_pin_2, 1);
222+
k_msleep(10);
223+
224+
zassert_equal(atomic_get(&counter), 0, "COMP was triggered for falling threshold cross");
225+
226+
gpio_pin_set_dt(&test_pin_1, 1);
227+
gpio_pin_set_dt(&test_pin_2, 0);
228+
k_msleep(10);
229+
230+
zassert_equal(atomic_get(&counter), 1, "COMP was not triggered for rising threshold cross");
231+
}
232+
233+
/**
234+
* @brief Configure comparator in differential mode
235+
* trigger both edges, event should not be detected.
236+
*/
237+
238+
ZTEST(comparator_runtime_configure, test_comp_config_diff_none)
239+
{
240+
int rc;
241+
242+
gpio_pin_set_dt(&test_pin_1, 1);
243+
gpio_pin_set_dt(&test_pin_2, 0);
244+
comparator_trigger_is_pending(test_dev);
245+
246+
atomic_clear(&counter);
247+
struct comp_nrf_comp_diff_config config = comp_diff_config;
248+
249+
config.isource = COMP_NRF_COMP_ISOURCE_10UA;
250+
rc = comp_nrf_comp_configure_diff(test_dev, &config);
251+
zassert_equal(rc, 0, "Cannot configure comparator.");
252+
253+
rc = comparator_set_trigger_callback(test_dev, test_callback, NULL);
254+
zassert_equal(rc, 0, "Cannot set callback for comparator.");
255+
256+
rc = comparator_set_trigger(test_dev, COMPARATOR_TRIGGER_NONE);
257+
zassert_equal(rc, 0, "Cannot set trigger for comparator.");
258+
259+
atomic_clear(&counter);
260+
gpio_pin_set_dt(&test_pin_1, 0);
261+
gpio_pin_set_dt(&test_pin_2, 1);
262+
k_msleep(10);
263+
264+
zassert_equal(atomic_get(&counter), 0, "COMP was triggered for falling threshold cross");
265+
266+
gpio_pin_set_dt(&test_pin_1, 1);
267+
gpio_pin_set_dt(&test_pin_2, 0);
268+
k_msleep(10);
269+
270+
zassert_equal(atomic_get(&counter), 0, "COMP was not triggered for rising threshold cross");
271+
}
272+
273+
static void *suite_setup(void)
274+
{
275+
TC_PRINT("Test executed on %s\n", CONFIG_BOARD_TARGET);
276+
TC_PRINT("===================================================================\n");
277+
278+
return NULL;
279+
}
280+
281+
static void test_before(void *f)
282+
{
283+
ARG_UNUSED(f);
284+
gpio_pin_configure_dt(&test_pin_1, GPIO_OUTPUT_INACTIVE);
285+
gpio_pin_configure_dt(&test_pin_2, GPIO_OUTPUT_INACTIVE);
286+
287+
}
288+
289+
ZTEST_SUITE(comparator_runtime_configure, NULL, suite_setup, test_before, NULL, NULL);

tests/boards/nrf/comp/testcase.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) 2024 Nordic Semiconductor
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
common:
5+
harness_config:
6+
fixture: gpio_loopback
7+
tags:
8+
- drivers
9+
- comparator
10+
tests:
11+
drivers.comparator.nrf_comp:
12+
platform_allow:
13+
- nrf54h20dk/nrf54h20/cpuapp
14+
- nrf54l15dk/nrf54l15/cpuapp

0 commit comments

Comments
 (0)