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