|
| 1 | +/* |
| 2 | +* Copyright (c) 2020 - 2025 Renesas Electronics Corporation and/or its affiliates |
| 3 | +* |
| 4 | +* SPDX-License-Identifier: BSD-3-Clause |
| 5 | +*/ |
| 6 | + |
| 7 | +#ifndef R_COMPARATOR_API_H |
| 8 | +#define R_COMPARATOR_API_H |
| 9 | + |
| 10 | +/*******************************************************************************************************************//** |
| 11 | + * @defgroup COMPARATOR_API Comparator Interface |
| 12 | + * @ingroup RENESAS_ANALOG_INTERFACES |
| 13 | + * @brief Interface for comparators. |
| 14 | + * |
| 15 | + * @section COMPARATOR_API_SUMMARY Summary |
| 16 | + * The comparator interface provides standard comparator functionality, including generating an event when the |
| 17 | + * comparator result changes. |
| 18 | + * |
| 19 | + * |
| 20 | + * @{ |
| 21 | + **********************************************************************************************************************/ |
| 22 | + |
| 23 | +/*********************************************************************************************************************** |
| 24 | + * Includes |
| 25 | + **********************************************************************************************************************/ |
| 26 | + |
| 27 | +/** Includes board and MCU related header files. */ |
| 28 | +#include "bsp_api.h" |
| 29 | + |
| 30 | +/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */ |
| 31 | +FSP_HEADER |
| 32 | + |
| 33 | +/********************************************************************************************************************** |
| 34 | + * Macro definitions |
| 35 | + **********************************************************************************************************************/ |
| 36 | + |
| 37 | +/***************************************************************************** |
| 38 | + * Typedef definitions |
| 39 | + ******************************************************************************/ |
| 40 | + |
| 41 | +/** Comparator control block. Allocate an instance specific control block to pass into the comparator API calls. |
| 42 | + */ |
| 43 | +typedef void comparator_ctrl_t; |
| 44 | + |
| 45 | +/** Select whether to invert the polarity of the comparator output. */ |
| 46 | +typedef enum e_comparator_mode |
| 47 | +{ |
| 48 | + COMPARATOR_MODE_NORMAL = 0, ///< Normal mode |
| 49 | + COMPARATOR_MODE_WINDOW = 1, ///< Window mode, not supported by all implementations |
| 50 | +} comparator_mode_t; |
| 51 | + |
| 52 | +/** Trigger type: rising edge, falling edge, both edges, low level. */ |
| 53 | +typedef enum e_comparator_trigger |
| 54 | +{ |
| 55 | + COMPARATOR_TRIGGER_RISING = 1, ///< Rising edge trigger |
| 56 | + COMPARATOR_TRIGGER_FALLING = 2, ///< Falling edge trigger |
| 57 | + COMPARATOR_TRIGGER_BOTH_EDGE = 3, ///< Both edges trigger |
| 58 | +} comparator_trigger_t; |
| 59 | + |
| 60 | +/** Select whether to invert the polarity of the comparator output. */ |
| 61 | +typedef enum e_comparator_polarity_invert |
| 62 | +{ |
| 63 | + COMPARATOR_POLARITY_INVERT_OFF = 0, ///< Do not invert polarity |
| 64 | + COMPARATOR_POLARITY_INVERT_ON = 1, ///< Invert polarity |
| 65 | +} comparator_polarity_invert_t; |
| 66 | + |
| 67 | +/** Select whether to include the comparator output on the output pin. */ |
| 68 | +typedef enum e_comparator_pin_output |
| 69 | +{ |
| 70 | + COMPARATOR_PIN_OUTPUT_OFF = 0, ///< Do not include comparator output on output pin |
| 71 | + COMPARATOR_PIN_OUTPUT_ON = 1, ///< Include comparator output on output pin |
| 72 | +} comparator_pin_output_t; |
| 73 | + |
| 74 | +/** Comparator digital filtering sample clock divisor settings. */ |
| 75 | +typedef enum e_comparator_filter |
| 76 | +{ |
| 77 | + COMPARATOR_FILTER_OFF = 0, ///< Disable debounce filter |
| 78 | + COMPARATOR_FILTER_1 = 4, ///< Filter using PCLK divided by 1, not supported by all implementations |
| 79 | + COMPARATOR_FILTER_8 = 1, ///< Filter using PCLK divided by 8 |
| 80 | + COMPARATOR_FILTER_16 = 2, ///< Filter using PCLK divided by 16, not supported by all implementations |
| 81 | + COMPARATOR_FILTER_32 = 3, ///< Filter using PCLK divided by 32 |
| 82 | +} comparator_filter_t; |
| 83 | + |
| 84 | +/** Current comparator state. */ |
| 85 | +typedef enum e_comparator_state |
| 86 | +{ |
| 87 | + COMPARATOR_STATE_OUTPUT_LOW = 0, ///< VCMP < VREF if polarity is not inverted, VCMP > VREF if inverted |
| 88 | + COMPARATOR_STATE_OUTPUT_HIGH = 1, ///< VCMP > VREF if polarity is not inverted, VCMP < VREF if inverted |
| 89 | + COMPARATOR_STATE_OUTPUT_DISABLED = 2, ///< @ref comparator_api_t::outputEnable() has not been called |
| 90 | +} comparator_state_t; |
| 91 | + |
| 92 | +/** Comparator information. */ |
| 93 | +typedef struct st_comparator_info |
| 94 | +{ |
| 95 | + uint32_t min_stabilization_wait_us; ///< Minimum stabilization wait time in microseconds |
| 96 | +} comparator_info_t; |
| 97 | + |
| 98 | +/** Comparator status. */ |
| 99 | +typedef struct st_comparator_status |
| 100 | +{ |
| 101 | + comparator_state_t state; ///< Current comparator state |
| 102 | +} comparator_status_t; |
| 103 | + |
| 104 | +/** Callback function parameter data */ |
| 105 | +typedef struct st_comparator_callback_args |
| 106 | +{ |
| 107 | + /** Placeholder for user data. Set in @ref comparator_api_t::open function in @ref comparator_cfg_t. */ |
| 108 | + void const * p_context; |
| 109 | + uint32_t channel; ///< The physical hardware channel that caused the interrupt. |
| 110 | +} comparator_callback_args_t; |
| 111 | + |
| 112 | +/** User configuration structure, used in open function */ |
| 113 | +typedef struct st_comparator_cfg |
| 114 | +{ |
| 115 | + uint8_t channel; ///< Hardware channel used. |
| 116 | + comparator_mode_t mode; ///< Normal or window mode |
| 117 | + comparator_trigger_t trigger; ///< Trigger setting. |
| 118 | + comparator_filter_t filter; ///< Digital filter clock divisor setting. |
| 119 | + comparator_polarity_invert_t invert; ///< Whether to invert output |
| 120 | + comparator_pin_output_t pin_output; ///< Whether to include output on output pin |
| 121 | + uint8_t vref_select; ///< Internal Vref Select |
| 122 | + uint8_t ipl; ///< Interrupt priority |
| 123 | + IRQn_Type irq; ///< NVIC interrupt number |
| 124 | + |
| 125 | + /** Callback called when comparator event occurs. */ |
| 126 | + void (* p_callback)(comparator_callback_args_t * p_args); |
| 127 | + |
| 128 | + /** Placeholder for user data. Passed to the user callback in @ref comparator_callback_args_t. */ |
| 129 | + void const * p_context; |
| 130 | + void const * p_extend; ///< Comparator hardware dependent configuration. |
| 131 | +} comparator_cfg_t; |
| 132 | + |
| 133 | +/** Comparator functions implemented at the HAL layer will follow this API. */ |
| 134 | +typedef struct st_comparator_api |
| 135 | +{ |
| 136 | + /** Initialize the comparator. |
| 137 | + * |
| 138 | + * @param[in] p_ctrl Pointer to instance control block |
| 139 | + * @param[in] p_cfg Pointer to configuration |
| 140 | + */ |
| 141 | + fsp_err_t (* open)(comparator_ctrl_t * const p_ctrl, comparator_cfg_t const * const p_cfg); |
| 142 | + |
| 143 | + /** Start the comparator. |
| 144 | + * |
| 145 | + * @param[in] p_ctrl Pointer to instance control block |
| 146 | + */ |
| 147 | + fsp_err_t (* outputEnable)(comparator_ctrl_t * const p_ctrl); |
| 148 | + |
| 149 | + /** Provide information such as the recommended minimum stabilization wait time. |
| 150 | + * |
| 151 | + * @param[in] p_ctrl Pointer to instance control block |
| 152 | + * @param[out] p_info Comparator information stored here |
| 153 | + */ |
| 154 | + fsp_err_t (* infoGet)(comparator_ctrl_t * const p_ctrl, comparator_info_t * const p_info); |
| 155 | + |
| 156 | + /** Provide current comparator status. |
| 157 | + * |
| 158 | + * @param[in] p_ctrl Pointer to instance control block |
| 159 | + * @param[out] p_status Status stored here |
| 160 | + */ |
| 161 | + fsp_err_t (* statusGet)(comparator_ctrl_t * const p_ctrl, comparator_status_t * const p_status); |
| 162 | + |
| 163 | + /** Stop the comparator. |
| 164 | + * |
| 165 | + * @param[in] p_ctrl Pointer to instance control block |
| 166 | + */ |
| 167 | + fsp_err_t (* close)(comparator_ctrl_t * const p_ctrl); |
| 168 | +} comparator_api_t; |
| 169 | + |
| 170 | +/** This structure encompasses everything that is needed to use an instance of this interface. */ |
| 171 | +typedef struct st_comparator_instance |
| 172 | +{ |
| 173 | + comparator_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance |
| 174 | + comparator_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this instance |
| 175 | + comparator_api_t const * p_api; ///< Pointer to the API structure for this instance |
| 176 | +} comparator_instance_t; |
| 177 | + |
| 178 | +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ |
| 179 | +FSP_FOOTER |
| 180 | + |
| 181 | +/*******************************************************************************************************************//** |
| 182 | + * @} (end defgroup COMPARATOR_API) |
| 183 | + **********************************************************************************************************************/ |
| 184 | +#endif |
0 commit comments