1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/comparator.h>
8 #include <zephyr/drivers/gpio.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/ztest.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 = GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), test_gpios);
14 static K_SEM_DEFINE(test_sem, 0, 1);
15 
test_callback(const struct device * dev,void * user_data)16 static void test_callback(const struct device *dev, void *user_data)
17 {
18 	zassert_equal(&test_sem, user_data);
19 	k_sem_give(&test_sem);
20 }
21 
test_setup(void)22 static void *test_setup(void)
23 {
24 	zassert_ok(gpio_pin_configure_dt(&test_pin, GPIO_OUTPUT_INACTIVE));
25 	return NULL;
26 }
27 
test_before(void * f)28 static void test_before(void *f)
29 {
30 	ARG_UNUSED(f);
31 
32 	k_sem_reset(&test_sem);
33 	zassert_ok(gpio_pin_set_dt(&test_pin, 0));
34 	zassert_ok(comparator_set_trigger(test_dev, COMPARATOR_TRIGGER_NONE));
35 	zassert_ok(comparator_set_trigger_callback(test_dev, NULL, NULL));
36 	zassert_between_inclusive(comparator_trigger_is_pending(test_dev), 0, 1);
37 }
38 
39 ZTEST_SUITE(comparator_gpio_loopback, NULL, test_setup, test_before, NULL, NULL);
40 
ZTEST(comparator_gpio_loopback,test_get_output)41 ZTEST(comparator_gpio_loopback, test_get_output)
42 {
43 	zassert_equal(comparator_get_output(test_dev), 0);
44 	k_msleep(1);
45 	zassert_ok(gpio_pin_set_dt(&test_pin, 1));
46 	k_msleep(1);
47 	zassert_equal(comparator_get_output(test_dev), 1);
48 	k_msleep(1);
49 	zassert_ok(gpio_pin_set_dt(&test_pin, 0));
50 	k_msleep(1);
51 	zassert_equal(comparator_get_output(test_dev), 0);
52 }
53 
ZTEST(comparator_gpio_loopback,test_no_trigger_no_pending)54 ZTEST(comparator_gpio_loopback, test_no_trigger_no_pending)
55 {
56 	zassert_ok(gpio_pin_set_dt(&test_pin, 1));
57 	k_msleep(1);
58 	zassert_equal(comparator_trigger_is_pending(test_dev), 0);
59 	zassert_ok(gpio_pin_set_dt(&test_pin, 0));
60 	k_msleep(1);
61 	zassert_equal(comparator_trigger_is_pending(test_dev), 0);
62 }
63 
ZTEST(comparator_gpio_loopback,test_trigger_rising_edge_pending)64 ZTEST(comparator_gpio_loopback, test_trigger_rising_edge_pending)
65 {
66 	zassert_ok(comparator_set_trigger(test_dev, COMPARATOR_TRIGGER_RISING_EDGE));
67 	k_msleep(1);
68 	zassert_equal(comparator_trigger_is_pending(test_dev), 0);
69 	zassert_ok(gpio_pin_set_dt(&test_pin, 1));
70 	k_msleep(1);
71 	zassert_equal(comparator_trigger_is_pending(test_dev), 1);
72 	zassert_equal(comparator_trigger_is_pending(test_dev), 0);
73 	zassert_ok(gpio_pin_set_dt(&test_pin, 0));
74 	k_msleep(1);
75 	zassert_equal(comparator_trigger_is_pending(test_dev), 0);
76 }
77 
ZTEST(comparator_gpio_loopback,test_trigger_falling_edge_pending)78 ZTEST(comparator_gpio_loopback, test_trigger_falling_edge_pending)
79 {
80 	zassert_ok(comparator_set_trigger(test_dev, COMPARATOR_TRIGGER_FALLING_EDGE));
81 	k_msleep(1);
82 	zassert_equal(comparator_trigger_is_pending(test_dev), 0);
83 	zassert_ok(gpio_pin_set_dt(&test_pin, 1));
84 	k_msleep(1);
85 	zassert_equal(comparator_trigger_is_pending(test_dev), 0);
86 	zassert_ok(gpio_pin_set_dt(&test_pin, 0));
87 	k_msleep(1);
88 	zassert_equal(comparator_trigger_is_pending(test_dev), 1);
89 	zassert_equal(comparator_trigger_is_pending(test_dev), 0);
90 }
91 
ZTEST(comparator_gpio_loopback,test_trigger_both_edges_pending)92 ZTEST(comparator_gpio_loopback, test_trigger_both_edges_pending)
93 {
94 	zassert_ok(comparator_set_trigger(test_dev, COMPARATOR_TRIGGER_BOTH_EDGES));
95 	k_msleep(1);
96 	zassert_equal(comparator_trigger_is_pending(test_dev), 0);
97 	zassert_ok(gpio_pin_set_dt(&test_pin, 1));
98 	k_msleep(1);
99 	zassert_equal(comparator_trigger_is_pending(test_dev), 1);
100 	zassert_equal(comparator_trigger_is_pending(test_dev), 0);
101 	zassert_ok(gpio_pin_set_dt(&test_pin, 0));
102 	k_msleep(1);
103 	zassert_equal(comparator_trigger_is_pending(test_dev), 1);
104 	zassert_equal(comparator_trigger_is_pending(test_dev), 0);
105 }
106 
ZTEST(comparator_gpio_loopback,test_trigger_callback)107 ZTEST(comparator_gpio_loopback, test_trigger_callback)
108 {
109 	zassert_ok(comparator_set_trigger_callback(test_dev, test_callback, &test_sem));
110 	k_msleep(1);
111 	zassert_equal(k_sem_take(&test_sem, K_NO_WAIT), -EBUSY);
112 	zassert_ok(comparator_set_trigger(test_dev, COMPARATOR_TRIGGER_RISING_EDGE));
113 	k_msleep(1);
114 	zassert_equal(k_sem_take(&test_sem, K_NO_WAIT), -EBUSY);
115 	zassert_ok(gpio_pin_set_dt(&test_pin, 1));
116 	k_msleep(1);
117 	zassert_ok(k_sem_take(&test_sem, K_NO_WAIT));
118 	zassert_equal(comparator_trigger_is_pending(test_dev), 0);
119 }
120