1 /*
2  * Copyright (c) 2014 - 2025, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef NRFX_LPCOMP_H__
35 #define NRFX_LPCOMP_H__
36 
37 #include <nrfx.h>
38 #include <haly/nrfy_lpcomp.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * @defgroup nrfx_lpcomp LPCOMP driver
46  * @{
47  * @ingroup nrf_lpcomp
48  * @brief   Low Power Comparator (LPCOMP) peripheral driver.
49  */
50 
51 /**
52  * @brief LPCOMP event handler function type.
53  * @param[in] event LPCOMP event.
54  */
55 typedef void (* nrfx_lpcomp_event_handler_t)(nrf_lpcomp_event_t event);
56 
57 /** @brief LPCOMP configuration. */
58 typedef struct
59 {
60 #if NRFX_API_VER_AT_LEAST(3, 2, 0) || defined(__NRFX_DOXYGEN__)
61     nrf_lpcomp_ref_t     reference;          ///< Reference selection.
62     nrf_lpcomp_ext_ref_t ext_ref;            ///< External analog reference selection.
63     nrf_lpcomp_detect_t  detection;          ///< Detection type.
64 #if NRF_LPCOMP_HAS_HYST
65     nrf_lpcomp_hyst_t    hyst;               ///< Comparator hysteresis.
66 #endif
67 #else
68     nrf_lpcomp_config_t  config;             ///< Peripheral configuration.
69 #endif
70     nrf_lpcomp_input_t   input;              ///< Input to be monitored.
71     uint8_t              interrupt_priority; ///< LPCOMP interrupt priority.
72 } nrfx_lpcomp_config_t;
73 
74 #if NRFX_API_VER_AT_LEAST(3, 2, 0) || defined(__NRFX_DOXYGEN__)
75 /**
76  * @brief LPCOMP driver default configuration.
77  *
78  * This configuration sets up LPCOMP with the following options:
79  * - reference voltage: 4/8 of supply voltage
80  * - detection of both up and down crossings
81  * - hysteresis disabled
82  *
83  * @param[in] _input Comparator input pin.
84  */
85 #define NRFX_LPCOMP_DEFAULT_CONFIG(_input)                                               \
86 {                                                                                        \
87     .reference = NRF_LPCOMP_REF_SUPPLY_4_8,                                              \
88     .detection = NRF_LPCOMP_DETECT_CROSS,                                                \
89     NRFX_COND_CODE_1(LPCOMP_FEATURE_HYST_PRESENT, (.hyst = NRF_LPCOMP_HYST_NOHYST,), ()) \
90     .input = (nrf_lpcomp_input_t)_input,                                                 \
91     .interrupt_priority = NRFX_LPCOMP_DEFAULT_CONFIG_IRQ_PRIORITY                        \
92 }
93 #else
94 #define NRFX_LPCOMP_DEFAULT_CONFIG(_input)                                                   \
95 {                                                                                            \
96     .config =                                                                                \
97     {                                                                                        \
98         .reference = NRF_LPCOMP_REF_SUPPLY_4_8,                                              \
99         .detection = NRF_LPCOMP_DETECT_CROSS,                                                \
100         NRFX_COND_CODE_1(LPCOMP_FEATURE_HYST_PRESENT, (.hyst = NRF_LPCOMP_HYST_NOHYST,), ()) \
101     },                                                                                       \
102     .input = (nrf_lpcomp_input_t)_input,                                                     \
103     .interrupt_priority = NRFX_LPCOMP_DEFAULT_CONFIG_IRQ_PRIORITY                            \
104 }
105 #endif
106 
107 /**
108  * @brief Function for initializing the LPCOMP driver.
109  *
110  * This function initializes the LPCOMP driver, but does not enable the peripheral or any interrupts.
111  * To start the driver, call the function nrfx_lpcomp_enable() after initialization.
112  *
113  * @param[in] p_config      Pointer to the structure with the initial configuration.
114  * @param[in] event_handler Event handler provided by the user.
115  *                          Must not be NULL.
116  *
117  * @retval NRFX_SUCCESS             Initialization was successful.
118  * @retval NRFX_ERROR_ALREADY       The driver is already initialized.
119  * @retval NRFX_ERROR_INVALID_STATE The driver is already initialized.
120  *                                  Deprecated - use @ref NRFX_ERROR_ALREADY instead.
121  * @retval NRFX_ERROR_BUSY          The COMP peripheral is already in use.
122  *                                  This is possible only if @ref nrfx_prs module
123  *                                  is enabled.
124  */
125 nrfx_err_t nrfx_lpcomp_init(nrfx_lpcomp_config_t const * p_config,
126                             nrfx_lpcomp_event_handler_t  event_handler);
127 
128 /**
129  * @brief Function for reconfiguring the LPCOMP driver.
130  *
131  * @param[in] p_config Pointer to the structure with the configuration.
132  *
133  * @retval NRFX_SUCCESS             Reconfiguration was successful.
134  * @retval NRFX_ERROR_BUSY          The driver is running and cannot be reconfigured.
135  * @retval NRFX_ERROR_INVALID_STATE The driver is uninitialized.
136  */
137 nrfx_err_t nrfx_lpcomp_reconfigure(nrfx_lpcomp_config_t const * p_config);
138 
139 /**
140  * @brief Function for uninitializing the LPCOMP driver.
141  *
142  * This function uninitializes the LPCOMP driver. The LPCOMP peripheral and
143  * its interrupts are disabled, and local variables are cleaned. After this call, you must
144  * initialize the driver again by calling nrfx_lpcomp_init() if you want to use it.
145  *
146  * @sa nrfx_lpcomp_disable
147  * @sa nrfx_lpcomp_init
148  */
149 void  nrfx_lpcomp_uninit(void);
150 
151 /**
152  * @brief Function for checking if the LPCOMP driver is initialized.
153  *
154  * @retval true  Driver is already initialized.
155  * @retval false Driver is not initialized.
156  */
157 bool nrfx_lpcomp_init_check(void);
158 
159 /**
160  * @brief Function for starting the LPCOMP peripheral and interrupts.
161  *
162  * Before calling this function, the driver must be initialized. This function
163  * enables the LPCOMP peripheral and its interrupts.
164  *
165  * @param[in] lpcomp_evt_en_mask Mask of events to be enabled. This parameter is to be built as
166  *                               an OR of elements from @ref nrf_lpcomp_int_mask_t.
167  * @param[in] lpcomp_shorts_mask Mask of shortcuts to be enabled. This parameter is to be built as
168  *                               an OR of elements from @ref nrf_lpcomp_short_mask_t.
169  *
170  * @sa nrfx_lpcomp_init
171  */
172 void nrfx_lpcomp_start(uint32_t lpcomp_evt_en_mask, uint32_t lpcomp_shorts_mask);
173 
174 /**
175  * @brief Function for enabling the LPCOMP peripheral and interrupts.
176  *
177  * @deprecated Use @ref nrfx_lpcomp_start instead.
178  *
179  * Before calling this function, the driver must be initialized. This function
180  * enables the LPCOMP peripheral and its interrupts.
181  *
182  * @sa nrfx_lpcomp_disable
183  */
184 NRFX_STATIC_INLINE void nrfx_lpcomp_enable(void);
185 
186 /**
187  * @brief Function for stopping the LPCOMP peripheral.
188  *
189  * Before calling this function, the driver must be enabled. This function disables the LPCOMP
190  * peripheral and its interrupts.
191  *
192  * @sa nrfx_lpcomp_uninit
193  */
194 void nrfx_lpcomp_stop(void);
195 
196 /**
197  * @brief Function for disabling the LPCOMP peripheral.
198  *
199  * @deprecated Use @ref nrfx_lpcomp_stop instead.
200  *
201  * Before calling this function, the driver must be initialized. This function disables the LPCOMP
202  * peripheral and its interrupts.
203  *
204  * @sa nrfx_lpcomp_enable
205  */
206 NRFX_STATIC_INLINE void nrfx_lpcomp_disable(void);
207 
208 /**
209  * @brief Function for copying the current state of the low power comparator result to the RESULT register.
210  *
211  * @retval 0 The input voltage is below the threshold (VIN+ < VIN-).
212  * @retval 1 The input voltage is above the threshold (VIN+ > VIN-).
213  */
214 uint32_t nrfx_lpcomp_sample(void);
215 
216 #ifndef NRFX_DECLARE_ONLY
217 
nrfx_lpcomp_enable(void)218 NRFX_STATIC_INLINE void nrfx_lpcomp_enable(void)
219 {
220     nrfx_lpcomp_start(0, 0);
221 }
222 
nrfx_lpcomp_disable(void)223 NRFX_STATIC_INLINE void nrfx_lpcomp_disable(void)
224 {
225     nrfx_lpcomp_stop();
226 }
227 
228 #endif // NRFX_DECLARE_ONLY
229 
230 /** @} */
231 
232 
233 void nrfx_lpcomp_irq_handler(void);
234 
235 
236 #ifdef __cplusplus
237 }
238 #endif
239 
240 #endif // NRFX_LPCOMP_H__
241