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  * @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 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 /** Comparator callback template */
39 typedef void (*comparator_callback_t)(const struct device *dev, void *user_data);
40 
41 /** @cond INTERNAL_HIDDEN */
42 
43 typedef int (*comparator_api_get_output)(const struct device *dev);
44 typedef int (*comparator_api_set_trigger)(const struct device *dev,
45 					  enum comparator_trigger trigger);
46 typedef int (*comparator_api_set_trigger_callback)(const struct device *dev,
47 						   comparator_callback_t callback,
48 						   void *user_data);
49 typedef int (*comparator_api_trigger_is_pending)(const struct device *dev);
50 
51 __subsystem struct comparator_driver_api {
52 	comparator_api_get_output get_output;
53 	comparator_api_set_trigger set_trigger;
54 	comparator_api_set_trigger_callback set_trigger_callback;
55 	comparator_api_trigger_is_pending trigger_is_pending;
56 };
57 
58 /** @endcond */
59 
60 /**
61  * @brief Get comparator's output state
62  *
63  * @param dev Comparator device
64  *
65  * @retval 1 Output state is high
66  * @retval 0 Output state is low
67  * @retval -errno code Failure
68  */
69 __syscall int comparator_get_output(const struct device *dev);
70 
z_impl_comparator_get_output(const struct device * dev)71 static inline int z_impl_comparator_get_output(const struct device *dev)
72 {
73 	return DEVICE_API_GET(comparator, dev)->get_output(dev);
74 }
75 
76 /**
77  * @brief Set comparator's trigger
78  *
79  * @param dev Comparator device
80  * @param trigger Trigger for signal and callback
81  *
82  * @retval 0 Successful
83  * @retval -errno code Failure
84  */
85 __syscall int comparator_set_trigger(const struct device *dev,
86 				     enum comparator_trigger trigger);
87 
z_impl_comparator_set_trigger(const struct device * dev,enum comparator_trigger trigger)88 static inline int z_impl_comparator_set_trigger(const struct device *dev,
89 						enum comparator_trigger trigger)
90 {
91 	return DEVICE_API_GET(comparator, dev)->set_trigger(dev, trigger);
92 }
93 
94 /**
95  * @brief Set comparator's trigger callback
96  *
97  * @param dev Comparator device
98  * @param callback Trigger callback
99  * @param user_data User data passed to callback
100  *
101  * @retval 0 Successful
102  * @retval -errno code Failure
103  *
104  * @note Set callback to NULL to disable callback
105  * @note Callback is called immediately if trigger is pending
106  */
comparator_set_trigger_callback(const struct device * dev,comparator_callback_t callback,void * user_data)107 static inline int comparator_set_trigger_callback(const struct device *dev,
108 						  comparator_callback_t callback,
109 						  void *user_data)
110 {
111 	return DEVICE_API_GET(comparator, dev)->set_trigger_callback(dev, callback, user_data);
112 }
113 
114 /**
115  * @brief Check if comparator's trigger is pending and clear it
116  *
117  * @param dev Comparator device
118  *
119  * @retval 1 Trigger was pending
120  * @retval 0 Trigger was cleared
121  * @retval -errno code Failure
122  */
123 __syscall int comparator_trigger_is_pending(const struct device *dev);
124 
z_impl_comparator_trigger_is_pending(const struct device * dev)125 static inline int z_impl_comparator_trigger_is_pending(const struct device *dev)
126 {
127 	return DEVICE_API_GET(comparator, dev)->trigger_is_pending(dev);
128 }
129 
130 #ifdef __cplusplus
131 }
132 #endif
133 
134 /** @} */
135 
136 #include <zephyr/syscalls/comparator.h>
137 
138 #endif /* ZEPHYR_INCLUDE_DRIVERS_COMPARATOR_H_ */
139