Line data Source code
1 1 : /*
2 : * Copyright (c) 2024 Nordic Semiconductor ASA
3 : *
4 : * SPDX-License-Identifier: Apache-2.0
5 : */
6 :
7 : #ifndef ZEPHYR_INCLUDE_DRIVERS_COMPARATOR_H_
8 : #define ZEPHYR_INCLUDE_DRIVERS_COMPARATOR_H_
9 :
10 : /**
11 : * @file
12 : * @ingroup comparator_interface
13 : * @brief Main header file for comparator driver API.
14 : */
15 :
16 : /**
17 : * @brief Interfaces for comparators.
18 : * @defgroup comparator_interface Comparator
19 : * @since 4.0
20 : * @version 0.1.0
21 : * @ingroup io_interfaces
22 : * @{
23 : */
24 :
25 : #include <zephyr/device.h>
26 : #include <errno.h>
27 :
28 : #ifdef __cplusplus
29 : extern "C" {
30 : #endif
31 :
32 : /** Comparator trigger enumerations */
33 1 : enum comparator_trigger {
34 : /** No trigger */
35 : COMPARATOR_TRIGGER_NONE = 0,
36 : /** Trigger on rising edge of comparator output */
37 : COMPARATOR_TRIGGER_RISING_EDGE,
38 : /** Trigger on falling edge of comparator output */
39 : COMPARATOR_TRIGGER_FALLING_EDGE,
40 : /** Trigger on both edges of comparator output */
41 : COMPARATOR_TRIGGER_BOTH_EDGES
42 : };
43 :
44 : /**
45 : * @brief Comparator callback template
46 : *
47 : * @param dev Comparator device
48 : * @param user_data Pointer to the user data that was provided when the trigger callback was set
49 : */
50 1 : typedef void (*comparator_callback_t)(const struct device *dev, void *user_data);
51 :
52 : /** @cond INTERNAL_HIDDEN */
53 :
54 : typedef int (*comparator_api_get_output)(const struct device *dev);
55 : typedef int (*comparator_api_set_trigger)(const struct device *dev,
56 : enum comparator_trigger trigger);
57 : typedef int (*comparator_api_set_trigger_callback)(const struct device *dev,
58 : comparator_callback_t callback,
59 : void *user_data);
60 : typedef int (*comparator_api_trigger_is_pending)(const struct device *dev);
61 :
62 : __subsystem struct comparator_driver_api {
63 : comparator_api_get_output get_output;
64 : comparator_api_set_trigger set_trigger;
65 : comparator_api_set_trigger_callback set_trigger_callback;
66 : comparator_api_trigger_is_pending trigger_is_pending;
67 : };
68 :
69 : /** @endcond */
70 :
71 : /**
72 : * @brief Get comparator's output state
73 : *
74 : * @param dev Comparator device
75 : *
76 : * @retval 1 Output state is high
77 : * @retval 0 Output state is low
78 : * @retval -errno code Failure
79 : */
80 1 : __syscall int comparator_get_output(const struct device *dev);
81 :
82 : static inline int z_impl_comparator_get_output(const struct device *dev)
83 : {
84 : return DEVICE_API_GET(comparator, dev)->get_output(dev);
85 : }
86 :
87 : /**
88 : * @brief Set comparator's trigger
89 : *
90 : * @param dev Comparator device
91 : * @param trigger Trigger for signal and callback
92 : *
93 : * @retval 0 Successful
94 : * @retval -errno code Failure
95 : */
96 1 : __syscall int comparator_set_trigger(const struct device *dev,
97 : enum comparator_trigger trigger);
98 :
99 : static inline int z_impl_comparator_set_trigger(const struct device *dev,
100 : enum comparator_trigger trigger)
101 : {
102 : return DEVICE_API_GET(comparator, dev)->set_trigger(dev, trigger);
103 : }
104 :
105 : /**
106 : * @brief Set comparator's trigger callback
107 : *
108 : * @param dev Comparator device
109 : * @param callback Trigger callback
110 : * @param user_data User data passed to callback
111 : *
112 : * @retval 0 Successful
113 : * @retval -errno code Failure
114 : *
115 : * @note Set callback to NULL to disable callback
116 : * @note Callback is called immediately if trigger is pending
117 : */
118 1 : static inline int comparator_set_trigger_callback(const struct device *dev,
119 : comparator_callback_t callback,
120 : void *user_data)
121 : {
122 : return DEVICE_API_GET(comparator, dev)->set_trigger_callback(dev, callback, user_data);
123 : }
124 :
125 : /**
126 : * @brief Check if comparator's trigger is pending and clear it
127 : *
128 : * @param dev Comparator device
129 : *
130 : * @retval 1 Trigger was pending
131 : * @retval 0 Trigger was cleared
132 : * @retval -errno code Failure
133 : */
134 1 : __syscall int comparator_trigger_is_pending(const struct device *dev);
135 :
136 : static inline int z_impl_comparator_trigger_is_pending(const struct device *dev)
137 : {
138 : return DEVICE_API_GET(comparator, dev)->trigger_is_pending(dev);
139 : }
140 :
141 : #ifdef __cplusplus
142 : }
143 : #endif
144 :
145 : /** @} */
146 :
147 : #include <zephyr/syscalls/comparator.h>
148 :
149 : #endif /* ZEPHYR_INCLUDE_DRIVERS_COMPARATOR_H_ */
|