|
| 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 3.7 |
| 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 | + |
| 71 | +static inline int z_impl_comparator_get_output(const struct device *dev) |
| 72 | +{ |
| 73 | + const struct comparator_driver_api *api = |
| 74 | + (const struct comparator_driver_api *)dev->api; |
| 75 | + |
| 76 | + return api->get_output(dev); |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * @brief Set comparator's trigger |
| 81 | + * |
| 82 | + * @param dev Comparator device |
| 83 | + * @param trigger Trigger for signal and callback |
| 84 | + * |
| 85 | + * @retval 0 Successful |
| 86 | + * @retval -errno code Failure |
| 87 | + */ |
| 88 | +__syscall int comparator_set_trigger(const struct device *dev, |
| 89 | + enum comparator_trigger trigger); |
| 90 | + |
| 91 | +static inline int z_impl_comparator_set_trigger(const struct device *dev, |
| 92 | + enum comparator_trigger trigger) |
| 93 | +{ |
| 94 | + const struct comparator_driver_api *api = |
| 95 | + (const struct comparator_driver_api *)dev->api; |
| 96 | + |
| 97 | + return api->set_trigger(dev, trigger); |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * @brief Set comparator's trigger callback |
| 102 | + * |
| 103 | + * @param dev Comparator device |
| 104 | + * @param callback Trigger callback |
| 105 | + * @param user_data User data passed to callback |
| 106 | + * |
| 107 | + * @retval 0 Successful |
| 108 | + * @retval -errno code Failure |
| 109 | + * |
| 110 | + * @note Set callback to NULL to disable callback |
| 111 | + * @note Callback is called immediately if trigger is pending |
| 112 | + */ |
| 113 | +static inline int comparator_set_trigger_callback(const struct device *dev, |
| 114 | + comparator_callback_t callback, |
| 115 | + void *user_data) |
| 116 | +{ |
| 117 | + const struct comparator_driver_api *api = |
| 118 | + (const struct comparator_driver_api *)dev->api; |
| 119 | + |
| 120 | + return api->set_trigger_callback(dev, callback, user_data); |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * @brief Check if comparator's trigger is pending and clear it |
| 125 | + * |
| 126 | + * @param dev Comparator device |
| 127 | + * |
| 128 | + * @retval 1 Trigger was pending |
| 129 | + * @retval 0 Trigger was cleared |
| 130 | + * @retval -errno code Failure |
| 131 | + */ |
| 132 | +__syscall int comparator_trigger_is_pending(const struct device *dev); |
| 133 | + |
| 134 | +static inline int z_impl_comparator_trigger_is_pending(const struct device *dev) |
| 135 | +{ |
| 136 | + const struct comparator_driver_api *api = |
| 137 | + (const struct comparator_driver_api *)dev->api; |
| 138 | + |
| 139 | + return api->trigger_is_pending(dev); |
| 140 | +} |
| 141 | + |
| 142 | +#ifdef __cplusplus |
| 143 | +} |
| 144 | +#endif |
| 145 | + |
| 146 | +/** @} */ |
| 147 | + |
| 148 | +#include <zephyr/syscalls/comparator.h> |
| 149 | + |
| 150 | +#endif /* ZEPHYR_INCLUDE_DRIVERS_COMPARATOR_H_ */ |
0 commit comments