Skip to content

Commit 2fbe105

Browse files
bjarki-andreasennashif
authored andcommitted
drivers: comparator: add fake comparator
Add fake comparator driver and bindings for use with testing. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent b7648f8 commit 2fbe105

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

drivers/comparator/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ zephyr_syscall_header(${ZEPHYR_BASE}/include/zephyr/drivers/comparator.h)
66
zephyr_library()
77

88
zephyr_library_sources_ifdef(CONFIG_USERSPACE comparator_handlers.c)
9+
zephyr_library_sources_ifdef(CONFIG_COMPARATOR_FAKE_COMP comparator_fake_comp.c)
910
zephyr_library_sources_ifdef(CONFIG_COMPARATOR_MCUX_ACMP comparator_mcux_acmp.c)
1011
zephyr_library_sources_ifdef(CONFIG_COMPARATOR_NRF_COMP comparator_nrf_comp.c)
1112
zephyr_library_sources_ifdef(CONFIG_COMPARATOR_NRF_LPCOMP comparator_nrf_lpcomp.c)

drivers/comparator/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ config COMPARATOR_INIT_PRIORITY
1818
help
1919
Comparator device driver initialization priority.
2020

21+
rsource "Kconfig.fake_comp"
2122
rsource "Kconfig.mcux_acmp"
2223
rsource "Kconfig.nrf_comp"
2324
rsource "Kconfig.nrf_lpcomp"

drivers/comparator/Kconfig.fake_comp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2024 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config COMPARATOR_FAKE_COMP
5+
bool "Fake comparator driver"
6+
default y
7+
depends on DT_HAS_ZEPHYR_FAKE_COMP_ENABLED
8+
depends on ZTEST
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/drivers/comparator/fake_comp.h>
8+
9+
#ifdef CONFIG_ZTEST
10+
#include <zephyr/ztest.h>
11+
#endif
12+
13+
#define DT_DRV_COMPAT zephyr_fake_comp
14+
15+
DEFINE_FAKE_VALUE_FUNC(int,
16+
comp_fake_comp_get_output,
17+
const struct device *);
18+
19+
DEFINE_FAKE_VALUE_FUNC(int,
20+
comp_fake_comp_set_trigger,
21+
const struct device *,
22+
enum comparator_trigger);
23+
24+
DEFINE_FAKE_VALUE_FUNC(int,
25+
comp_fake_comp_set_trigger_callback,
26+
const struct device *,
27+
comparator_callback_t,
28+
void *);
29+
30+
DEFINE_FAKE_VALUE_FUNC(int,
31+
comp_fake_comp_trigger_is_pending,
32+
const struct device *);
33+
34+
static const struct comparator_driver_api fake_comp_api = {
35+
.get_output = comp_fake_comp_get_output,
36+
.set_trigger = comp_fake_comp_set_trigger,
37+
.set_trigger_callback = comp_fake_comp_set_trigger_callback,
38+
.trigger_is_pending = comp_fake_comp_trigger_is_pending,
39+
};
40+
41+
#ifdef CONFIG_ZTEST
42+
static void fake_comp_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
43+
{
44+
ARG_UNUSED(test);
45+
ARG_UNUSED(fixture);
46+
47+
RESET_FAKE(comp_fake_comp_get_output);
48+
RESET_FAKE(comp_fake_comp_set_trigger);
49+
RESET_FAKE(comp_fake_comp_set_trigger_callback);
50+
RESET_FAKE(comp_fake_comp_trigger_is_pending);
51+
}
52+
53+
ZTEST_RULE(comp_fake_comp_reset_rule, fake_comp_reset_rule_before, NULL);
54+
#endif
55+
56+
DEVICE_DT_INST_DEFINE(
57+
0,
58+
NULL,
59+
NULL,
60+
NULL,
61+
NULL,
62+
POST_KERNEL,
63+
CONFIG_COMPARATOR_INIT_PRIORITY,
64+
&fake_comp_api
65+
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2024 Nordic Semiconductor ASA
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Fake comparator device used as stub or mock for testing
5+
6+
compatible: "zephyr,fake-comp"
7+
8+
include: base.yaml
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_DRIVERS_COMPARATOR_FAKE_H_
8+
#define ZEPHYR_INCLUDE_DRIVERS_COMPARATOR_FAKE_H_
9+
10+
#include <zephyr/drivers/comparator.h>
11+
#include <zephyr/fff.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
DECLARE_FAKE_VALUE_FUNC(int,
18+
comp_fake_comp_get_output,
19+
const struct device *);
20+
21+
DECLARE_FAKE_VALUE_FUNC(int,
22+
comp_fake_comp_set_trigger,
23+
const struct device *,
24+
enum comparator_trigger);
25+
26+
DECLARE_FAKE_VALUE_FUNC(int,
27+
comp_fake_comp_set_trigger_callback,
28+
const struct device *,
29+
comparator_callback_t,
30+
void *);
31+
32+
DECLARE_FAKE_VALUE_FUNC(int,
33+
comp_fake_comp_trigger_is_pending,
34+
const struct device *);
35+
36+
#ifdef __cplusplus
37+
}
38+
#endif
39+
40+
#endif /* ZEPHYR_INCLUDE_DRIVERS_COMPARATOR_FAKE_H_ */

0 commit comments

Comments
 (0)