1 /*
2  * Copyright (c) 2014 - 2024, 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 NRF_LPCOMP_H_
35 #define NRF_LPCOMP_H_
36 
37 #include <nrfx.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @defgroup nrf_lpcomp_hal LPCOMP HAL
45  * @{
46  * @ingroup nrf_lpcomp
47  * @brief   Hardware access layer for managing the Low Power Comparator (LPCOMP) peripheral.
48  */
49 
50 #if defined(LPCOMP_PSEL_PIN_Msk) || defined(__NRFX_DOXYGEN__)
51 /** @brief Symbol indicating whether the configuration of analog input using pin number is present. */
52 #define NRF_LPCOMP_HAS_AIN_AS_PIN 1
53 #else
54 #define NRF_LPCOMP_HAS_AIN_AS_PIN 0
55 #endif
56 
57 #if defined(LPCOMP_HYST_HYST_Msk) || defined(__NRFX_DOXYGEN__)
58 /** @brief Symbol indicating whether the hysteresis is present. */
59 #define NRF_LPCOMP_HAS_HYST 1
60 #else
61 #define NRF_LPCOMP_HAS_HYST 0
62 #endif
63 
64 /** @brief LPCOMP tasks. */
65 typedef enum
66 {
67     NRF_LPCOMP_TASK_START  = offsetof(NRF_LPCOMP_Type, TASKS_START), ///< LPCOMP start sampling task.
68     NRF_LPCOMP_TASK_STOP   = offsetof(NRF_LPCOMP_Type, TASKS_STOP),  ///< LPCOMP stop sampling task.
69     NRF_LPCOMP_TASK_SAMPLE = offsetof(NRF_LPCOMP_Type, TASKS_SAMPLE) ///< Sample comparator value.
70 } nrf_lpcomp_task_t;
71 
72 /** @brief LPCOMP events. */
73 typedef enum
74 {
75     NRF_LPCOMP_EVENT_READY = offsetof(NRF_LPCOMP_Type, EVENTS_READY), ///< LPCOMP is ready and output is valid.
76     NRF_LPCOMP_EVENT_DOWN  = offsetof(NRF_LPCOMP_Type, EVENTS_DOWN),  ///< Input voltage crossed the threshold going down.
77     NRF_LPCOMP_EVENT_UP    = offsetof(NRF_LPCOMP_Type, EVENTS_UP),    ///< Input voltage crossed the threshold going up.
78     NRF_LPCOMP_EVENT_CROSS = offsetof(NRF_LPCOMP_Type, EVENTS_CROSS)  ///< Input voltage crossed the threshold in any direction.
79 } nrf_lpcomp_event_t;
80 
81 /** @brief LPCOMP interrupts. */
82 typedef enum
83 {
84     NRF_LPCOMP_INT_READY_MASK = LPCOMP_INTENSET_READY_Msk, ///< Interrupt on READY event.
85     NRF_LPCOMP_INT_DOWN_MASK  = LPCOMP_INTENSET_DOWN_Msk,  ///< Interrupt on DOWN event.
86     NRF_LPCOMP_INT_UP_MASK    = LPCOMP_INTENSET_UP_Msk,    ///< Interrupt on UP event.
87     NRF_LPCOMP_INT_CROSS_MASK = LPCOMP_INTENSET_CROSS_Msk  ///< Interrupt on CROSS event.
88 } nrf_lpcomp_int_mask_t;
89 
90 /** @brief LPCOMP shortcut masks. */
91 typedef enum
92 {
93     NRF_LPCOMP_SHORT_CROSS_STOP_MASK   = LPCOMP_SHORTS_CROSS_STOP_Msk,  ///< Shortcut between CROSS event and STOP task.
94     NRF_LPCOMP_SHORT_UP_STOP_MASK      = LPCOMP_SHORTS_UP_STOP_Msk,     ///< Shortcut between UP event and STOP task.
95     NRF_LPCOMP_SHORT_DOWN_STOP_MASK    = LPCOMP_SHORTS_DOWN_STOP_Msk,   ///< Shortcut between DOWN event and STOP task.
96     NRF_LPCOMP_SHORT_READY_STOP_MASK   = LPCOMP_SHORTS_READY_STOP_Msk,  ///< Shortcut between READY event and STOP task.
97     NRF_LPCOMP_SHORT_READY_SAMPLE_MASK = LPCOMP_SHORTS_READY_SAMPLE_Msk ///< Shortcut between READY event and SAMPLE task.
98 } nrf_lpcomp_short_mask_t;
99 
100 /** @brief LPCOMP reference selection. */
101 typedef enum
102 {
103 #if (LPCOMP_REFSEL_RESOLUTION == 8) || defined(__NRFX_DOXYGEN__)
104     NRF_LPCOMP_REF_SUPPLY_1_8   = LPCOMP_REFSEL_REFSEL_SupplyOneEighthPrescaling,      ///< Use supply with a 1/8 prescaler as reference.
105     NRF_LPCOMP_REF_SUPPLY_2_8   = LPCOMP_REFSEL_REFSEL_SupplyTwoEighthsPrescaling,     ///< Use supply with a 2/8 prescaler as reference.
106     NRF_LPCOMP_REF_SUPPLY_3_8   = LPCOMP_REFSEL_REFSEL_SupplyThreeEighthsPrescaling,   ///< Use supply with a 3/8 prescaler as reference.
107     NRF_LPCOMP_REF_SUPPLY_4_8   = LPCOMP_REFSEL_REFSEL_SupplyFourEighthsPrescaling,    ///< Use supply with a 4/8 prescaler as reference.
108     NRF_LPCOMP_REF_SUPPLY_5_8   = LPCOMP_REFSEL_REFSEL_SupplyFiveEighthsPrescaling,    ///< Use supply with a 5/8 prescaler as reference.
109     NRF_LPCOMP_REF_SUPPLY_6_8   = LPCOMP_REFSEL_REFSEL_SupplySixEighthsPrescaling,     ///< Use supply with a 6/8 prescaler as reference.
110     NRF_LPCOMP_REF_SUPPLY_7_8   = LPCOMP_REFSEL_REFSEL_SupplySevenEighthsPrescaling,   ///< Use supply with a 7/8 prescaler as reference.
111 #elif (LPCOMP_REFSEL_RESOLUTION == 16) || defined(__NRFX_DOXYGEN__)
112     NRF_LPCOMP_REF_SUPPLY_1_8   = LPCOMP_REFSEL_REFSEL_Ref1_8Vdd,                      ///< Use supply with a 1/8 prescaler as reference.
113     NRF_LPCOMP_REF_SUPPLY_2_8   = LPCOMP_REFSEL_REFSEL_Ref2_8Vdd,                      ///< Use supply with a 2/8 prescaler as reference.
114     NRF_LPCOMP_REF_SUPPLY_3_8   = LPCOMP_REFSEL_REFSEL_Ref3_8Vdd,                      ///< Use supply with a 3/8 prescaler as reference.
115     NRF_LPCOMP_REF_SUPPLY_4_8   = LPCOMP_REFSEL_REFSEL_Ref4_8Vdd,                      ///< Use supply with a 4/8 prescaler as reference.
116     NRF_LPCOMP_REF_SUPPLY_5_8   = LPCOMP_REFSEL_REFSEL_Ref5_8Vdd,                      ///< Use supply with a 5/8 prescaler as reference.
117     NRF_LPCOMP_REF_SUPPLY_6_8   = LPCOMP_REFSEL_REFSEL_Ref6_8Vdd,                      ///< Use supply with a 6/8 prescaler as reference.
118     NRF_LPCOMP_REF_SUPPLY_7_8   = LPCOMP_REFSEL_REFSEL_Ref7_8Vdd,                      ///< Use supply with a 7/8 prescaler as reference.
119     NRF_LPCOMP_REF_SUPPLY_1_16  = LPCOMP_REFSEL_REFSEL_Ref1_16Vdd,                     ///< Use supply with a 1/16 prescaler as reference.
120     NRF_LPCOMP_REF_SUPPLY_3_16  = LPCOMP_REFSEL_REFSEL_Ref3_16Vdd,                     ///< Use supply with a 3/16 prescaler as reference.
121     NRF_LPCOMP_REF_SUPPLY_5_16  = LPCOMP_REFSEL_REFSEL_Ref5_16Vdd,                     ///< Use supply with a 5/16 prescaler as reference.
122     NRF_LPCOMP_REF_SUPPLY_7_16  = LPCOMP_REFSEL_REFSEL_Ref7_16Vdd,                     ///< Use supply with a 7/16 prescaler as reference.
123     NRF_LPCOMP_REF_SUPPLY_9_16  = LPCOMP_REFSEL_REFSEL_Ref9_16Vdd,                     ///< Use supply with a 9/16 prescaler as reference.
124     NRF_LPCOMP_REF_SUPPLY_11_16 = LPCOMP_REFSEL_REFSEL_Ref11_16Vdd,                    ///< Use supply with a 11/16 prescaler as reference.
125     NRF_LPCOMP_REF_SUPPLY_13_16 = LPCOMP_REFSEL_REFSEL_Ref13_16Vdd,                    ///< Use supply with a 13/16 prescaler as reference.
126     NRF_LPCOMP_REF_SUPPLY_15_16 = LPCOMP_REFSEL_REFSEL_Ref15_16Vdd,                    ///< Use supply with a 15/16 prescaler as reference.
127 #endif
128     NRF_LPCOMP_REF_EXT_REF      = LPCOMP_REFSEL_REFSEL_ARef,                           ///< Use external analog reference.
129 #if !NRF_LPCOMP_HAS_AIN_AS_PIN
130     NRF_LPCOMP_REF_EXT_REF0     = LPCOMP_REFSEL_REFSEL_ARef |
131                                   (LPCOMP_EXTREFSEL_EXTREFSEL_AnalogReference0 << 16), ///< @deprecated Use @ref nrf_lpcomp_ext_ref_t instead.
132     NRF_LPCOMP_REF_EXT_REF1     = LPCOMP_REFSEL_REFSEL_ARef |
133                                   (LPCOMP_EXTREFSEL_EXTREFSEL_AnalogReference1 << 16), ///< @deprecated Use @ref nrf_lpcomp_ext_ref_t instead.
134 #endif
135 } nrf_lpcomp_ref_t;
136 
137 /** @brief LPCOMP external reference selection. */
138 #if NRF_LPCOMP_HAS_AIN_AS_PIN
139 typedef uint32_t nrf_lpcomp_ext_ref_t;
140 #else
141 typedef enum
142 {
143     NRF_LPCOMP_EXT_REF_REF0 = LPCOMP_EXTREFSEL_EXTREFSEL_AnalogReference0, ///< External reference 0.
144     NRF_LPCOMP_EXT_REF_REF1 = LPCOMP_EXTREFSEL_EXTREFSEL_AnalogReference1, ///< External reference 1.
145 } nrf_lpcomp_ext_ref_t;
146 #endif
147 
148 /** @brief LPCOMP input selection. */
149 #if NRF_LPCOMP_HAS_AIN_AS_PIN
150 typedef uint32_t nrf_lpcomp_input_t;
151 #else
152 typedef enum
153 {
154     NRF_LPCOMP_INPUT_0 = LPCOMP_PSEL_PSEL_AnalogInput0, ///< Input 0.
155     NRF_LPCOMP_INPUT_1 = LPCOMP_PSEL_PSEL_AnalogInput1, ///< Input 1.
156     NRF_LPCOMP_INPUT_2 = LPCOMP_PSEL_PSEL_AnalogInput2, ///< Input 2.
157     NRF_LPCOMP_INPUT_3 = LPCOMP_PSEL_PSEL_AnalogInput3, ///< Input 3.
158     NRF_LPCOMP_INPUT_4 = LPCOMP_PSEL_PSEL_AnalogInput4, ///< Input 4.
159     NRF_LPCOMP_INPUT_5 = LPCOMP_PSEL_PSEL_AnalogInput5, ///< Input 5.
160     NRF_LPCOMP_INPUT_6 = LPCOMP_PSEL_PSEL_AnalogInput6, ///< Input 6.
161     NRF_LPCOMP_INPUT_7 = LPCOMP_PSEL_PSEL_AnalogInput7  ///< Input 7.
162 } nrf_lpcomp_input_t;
163 #endif
164 
165 /** @brief LPCOMP detection type selection. */
166 typedef enum
167 {
168     NRF_LPCOMP_DETECT_CROSS = LPCOMP_ANADETECT_ANADETECT_Cross, ///< Generate ANADETEC on crossing, both upwards and downwards crossing.
169     NRF_LPCOMP_DETECT_UP    = LPCOMP_ANADETECT_ANADETECT_Up,    ///< Generate ANADETEC on upwards crossing only.
170     NRF_LPCOMP_DETECT_DOWN  = LPCOMP_ANADETECT_ANADETECT_Down   ///< Generate ANADETEC on downwards crossing only.
171 } nrf_lpcomp_detect_t;
172 
173 #if NRF_LPCOMP_HAS_HYST
174 /** @brief LPCOMP hysteresis. */
175 typedef enum
176 {
177 #ifdef LPCOMP_HYST_HYST_NoHyst
178     NRF_LPCOMP_HYST_NOHYST  = LPCOMP_HYST_HYST_NoHyst,   ///< Comparator hysteresis disabled.
179 #else
180     NRF_LPCOMP_HYST_NOHYST  = LPCOMP_HYST_HYST_Disabled, ///< Comparator hysteresis disabled.
181 #endif
182 #ifdef LPCOMP_HYST_HYST_Hyst50mV
183     NRF_LPCOMP_HYST_ENABLED = LPCOMP_HYST_HYST_Hyst50mV  ///< Comparator hysteresis enabled (typically 50 mV).
184 #else
185     NRF_LPCOMP_HYST_ENABLED = LPCOMP_HYST_HYST_Enabled   ///< Comparator hysteresis enabled (typically 50 mV).
186 #endif
187 } nrf_lpcomp_hyst_t;
188 #endif // NRF_LPCOMP_HAS_HYST
189 
190 /** @brief LPCOMP configuration. */
191 typedef struct
192 {
193     nrf_lpcomp_ref_t        reference; ///< LPCOMP reference.
194     nrf_lpcomp_detect_t     detection; ///< LPCOMP detection type.
195 #if NRF_LPCOMP_HAS_HYST
196     nrf_lpcomp_hyst_t       hyst;      ///< LPCOMP hysteresis.
197 #endif // LPCOMP_FEATURE_HYST_PRESENT
198 } nrf_lpcomp_config_t;
199 
200 /**
201  * @brief Function for setting the specified LPCOMP task.
202  *
203  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
204  * @param[in] task  LPCOMP task to be set.
205  */
206 NRF_STATIC_INLINE void nrf_lpcomp_task_trigger(NRF_LPCOMP_Type * p_reg, nrf_lpcomp_task_t task);
207 
208 /**
209  * @brief Function for getting the address of the specified LPCOMP task register.
210  *
211  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
212  * @param[in] task  LPCOMP task.
213  *
214  * @return The address of the specified LPCOMP task.
215  */
216 NRF_STATIC_INLINE uint32_t nrf_lpcomp_task_address_get(NRF_LPCOMP_Type const * p_reg,
217                                                        nrf_lpcomp_task_t       task);
218 
219 /**
220  * @brief Function for retrieving the state of the LPCOMP event.
221  *
222  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
223  * @param[in] event Event to be checked.
224  *
225  * @retval true  The event has been generated.
226  * @retval false The event has not been generated.
227  */
228 NRF_STATIC_INLINE bool nrf_lpcomp_event_check(NRF_LPCOMP_Type const * p_reg,
229                                               nrf_lpcomp_event_t      event);
230 
231 /**
232  * @brief Function for clearing the specified LPCOMP event.
233  *
234  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
235  * @param[in] event LPCOMP event to be cleared.
236  */
237 NRF_STATIC_INLINE void nrf_lpcomp_event_clear(NRF_LPCOMP_Type * p_reg, nrf_lpcomp_event_t event);
238 
239 /**
240  * @brief Function for getting the address of the specified LPCOMP event register.
241  *
242  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
243  * @param[in] event LPCOMP event.
244  *
245  * @return The address of the specified LPCOMP event.
246  */
247 NRF_STATIC_INLINE uint32_t nrf_lpcomp_event_address_get(NRF_LPCOMP_Type const * p_reg,
248                                                         nrf_lpcomp_event_t      event);
249 
250 /**
251  * @brief Function for setting LPCOMP shorts.
252  *
253  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
254  * @param[in] mask  Mask of shortcuts.
255  */
256 NRF_STATIC_INLINE void nrf_lpcomp_shorts_enable(NRF_LPCOMP_Type * p_reg, uint32_t mask);
257 
258 /**
259  * @brief Function for clearing LPCOMP shorts by mask.
260  *
261  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
262  * @param[in] mask  Mask of shortcuts.
263  */
264 NRF_STATIC_INLINE void nrf_lpcomp_shorts_disable(NRF_LPCOMP_Type * p_reg, uint32_t mask);
265 
266 /**
267  * @brief Function for for setting the specified shortcuts.
268  *
269  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
270  * @param[in] mask  Mask of shortcuts.
271  */
272 NRF_STATIC_INLINE void nrf_lpcomp_shorts_set(NRF_LPCOMP_Type * p_reg, uint32_t mask);
273 
274 /**
275  * @brief Function for enabling interrupts from LPCOMP.
276  *
277  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
278  * @param[in] mask  Mask of interrupts to be enabled.
279  *                  Use @ref nrf_lpcomp_int_mask_t values for bit masking.
280  *
281  * @sa nrf_lpcomp_int_disable
282  * @sa nrf_lpcomp_int_enable_check
283  */
284 NRF_STATIC_INLINE void nrf_lpcomp_int_enable(NRF_LPCOMP_Type * p_reg, uint32_t mask);
285 
286 /**
287  * @brief Function for disabling interrupts from LPCOMP.
288  *
289  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
290  * @param[in] mask  Mask of interrupts to be disabled.
291  *                  Use @ref nrf_lpcomp_int_mask_t values for bit masking.
292  *
293  * @sa nrf_lpcomp_int_enable
294  * @sa nrf_lpcomp_int_enable_check
295  */
296 NRF_STATIC_INLINE void nrf_lpcomp_int_disable(NRF_LPCOMP_Type * p_reg, uint32_t mask);
297 
298 /**
299  * @brief Function for checking if the specified interrupts are enabled.
300  *
301  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
302  * @param[in] mask  Mask of interrupts to be checked.
303  *                  Use @ref nrf_lpcomp_int_mask_t values for bit masking.
304  *
305  * @return Mask of enabled interrupts.
306  */
307 NRF_STATIC_INLINE uint32_t nrf_lpcomp_int_enable_check(NRF_LPCOMP_Type const * p_reg,
308                                                        uint32_t                mask);
309 
310 #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
311 /**
312  * @brief Function for setting subscribe configuration for a given LPCOMP task.
313  *
314  * @param[in] p_reg   Pointer to the structure of registers of the peripheral.
315  * @param[in] task    Task for which the configuration is set.
316  * @param[in] channel Channel through which events are subscribed.
317  */
318 NRF_STATIC_INLINE void nrf_lpcomp_subscribe_set(NRF_LPCOMP_Type * p_reg,
319                                                 nrf_lpcomp_task_t task,
320                                                 uint8_t           channel);
321 
322 /**
323  * @brief Function for clearing subscribe configuration for a given LPCOMP task.
324  *
325  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
326  * @param[in] task  Task for which the configuration is cleared.
327  */
328 NRF_STATIC_INLINE void nrf_lpcomp_subscribe_clear(NRF_LPCOMP_Type * p_reg,
329                                                   nrf_lpcomp_task_t task);
330 
331 /**
332  * @brief Function for setting publish configuration for a given LPCOMP event.
333  *
334  * @param[in] p_reg   Pointer to the structure of registers of the peripheral.
335  * @param[in] event   Event for which the configuration is set.
336  * @param[in] channel Channel through which the event is published.
337  */
338 NRF_STATIC_INLINE void nrf_lpcomp_publish_set(NRF_LPCOMP_Type *  p_reg,
339                                               nrf_lpcomp_event_t event,
340                                               uint8_t            channel);
341 
342 /**
343  * @brief Function for clearing publish configuration for a given LPCOMP event.
344  *
345  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
346  * @param[in] event Event for which the configuration is cleared.
347  */
348 NRF_STATIC_INLINE void nrf_lpcomp_publish_clear(NRF_LPCOMP_Type *  p_reg,
349                                                 nrf_lpcomp_event_t event);
350 #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
351 
352 /**
353  * @brief Function for configuring LPCOMP.
354  *
355  * This function powers on LPCOMP and configures it. LPCOMP is in DISABLE state after configuration,
356  * so it must be enabled before using it. All shorts are inactive, events are cleared, and LPCOMP is stopped.
357  *
358  * @deprecated Use the dedicated functions instead.
359  *
360  * @param[in] p_reg    Pointer to the structure of registers of the peripheral.
361  * @param[in] p_config Configuration.
362  */
363 NRF_STATIC_INLINE void nrf_lpcomp_configure(NRF_LPCOMP_Type *           p_reg,
364                                             nrf_lpcomp_config_t const * p_config);
365 
366 /**
367  * @brief Function for setting the reference source.
368  *
369  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
370  * @param[in] reference LPCOMP reference selection.
371  */
372 NRF_STATIC_INLINE void nrf_lpcomp_ref_set(NRF_LPCOMP_Type * p_reg, nrf_lpcomp_ref_t reference);
373 
374 /**
375  * @brief Function for setting the external analog reference source.
376  *
377  * To use external reference first call @ref nrf_lpcomp_ref_set with NRF_LPCOMP_REF_EXT_REF argument.
378  *
379  * @param[in] p_reg   Pointer to the structure of registers of the peripheral.
380  * @param[in] ext_ref LPCOMP external analog reference selection.
381  */
382 NRF_STATIC_INLINE void nrf_lpcomp_ext_ref_set(NRF_LPCOMP_Type *    p_reg,
383                                               nrf_lpcomp_ext_ref_t ext_ref);
384 
385 /**
386  * @brief Function for selecting an active LPCOMP input.
387  *
388  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
389  * @param[in] input Input to be selected.
390  */
391 NRF_STATIC_INLINE void nrf_lpcomp_input_select(NRF_LPCOMP_Type * p_reg, nrf_lpcomp_input_t input);
392 
393 /**
394  * @brief Function for setting the detection type.
395  *
396  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
397  * @param[in] detection LPCOMP detection type.
398  */
399 NRF_STATIC_INLINE void nrf_lpcomp_detection_set(NRF_LPCOMP_Type *   p_reg,
400                                                 nrf_lpcomp_detect_t detection);
401 
402 #if NRF_LPCOMP_HAS_HYST
403 /**
404  * @brief Function for setting the hysteresis.
405  *
406  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
407  * @param[in] hyst  LPCOMP comparator hysteresis.
408  */
409 NRF_STATIC_INLINE void nrf_lpcomp_hysteresis_set(NRF_LPCOMP_Type * p_reg,
410                                                  nrf_lpcomp_hyst_t hyst);
411 #endif
412 
413 /**
414  * @brief Function for enabling the LPCOMP.
415  *
416  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
417  */
418 NRF_STATIC_INLINE void nrf_lpcomp_enable(NRF_LPCOMP_Type * p_reg);
419 
420 /**
421  * @brief Function for disabling the LPCOMP.
422  *
423  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
424  */
425 NRF_STATIC_INLINE void nrf_lpcomp_disable(NRF_LPCOMP_Type * p_reg);
426 
427 /**
428  * @brief Function for checking if the LPCOMP peripheral is enabled.
429  *
430  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
431  *
432  * @retval true  The LPCOMP peripheral is enabled.
433  * @retval false The LPCOMP peripheral is not enabled.
434  */
435 NRF_STATIC_INLINE bool nrf_lpcomp_enable_check(NRF_LPCOMP_Type * p_reg);
436 
437 /**
438  * @brief Function for getting the last LPCOMP compare result.
439  *
440  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
441  *
442  * @return The last compare result. If 0, then VIN+ < VIN-. If 1, then VIN- < VIN+.
443  */
444 NRF_STATIC_INLINE uint32_t nrf_lpcomp_result_get(NRF_LPCOMP_Type const * p_reg);
445 
446 #ifndef NRF_DECLARE_ONLY
447 
nrf_lpcomp_task_trigger(NRF_LPCOMP_Type * p_reg,nrf_lpcomp_task_t task)448 NRF_STATIC_INLINE void nrf_lpcomp_task_trigger(NRF_LPCOMP_Type * p_reg, nrf_lpcomp_task_t task)
449 {
450     *( (volatile uint32_t *)( (uint8_t *)p_reg + (uint32_t)task) ) = 1;
451 }
452 
nrf_lpcomp_task_address_get(NRF_LPCOMP_Type const * p_reg,nrf_lpcomp_task_t task)453 NRF_STATIC_INLINE uint32_t nrf_lpcomp_task_address_get(NRF_LPCOMP_Type const * p_reg,
454                                                        nrf_lpcomp_task_t       task)
455 {
456     return nrf_task_event_address_get(p_reg, task);
457 }
458 
nrf_lpcomp_event_check(NRF_LPCOMP_Type const * p_reg,nrf_lpcomp_event_t event)459 NRF_STATIC_INLINE bool nrf_lpcomp_event_check(NRF_LPCOMP_Type const * p_reg,
460                                               nrf_lpcomp_event_t      event)
461 {
462     return nrf_event_check(p_reg, event);
463 }
464 
nrf_lpcomp_event_clear(NRF_LPCOMP_Type * p_reg,nrf_lpcomp_event_t event)465 NRF_STATIC_INLINE void nrf_lpcomp_event_clear(NRF_LPCOMP_Type * p_reg, nrf_lpcomp_event_t event)
466 {
467     *( (volatile uint32_t *)( (uint8_t *)p_reg + (uint32_t)event) ) = 0;
468     nrf_event_readback((uint8_t *)p_reg + (uint32_t)event);
469 }
470 
nrf_lpcomp_event_address_get(NRF_LPCOMP_Type const * p_reg,nrf_lpcomp_event_t event)471 NRF_STATIC_INLINE uint32_t nrf_lpcomp_event_address_get(NRF_LPCOMP_Type const * p_reg,
472                                                         nrf_lpcomp_event_t      event)
473 {
474     return nrf_task_event_address_get(p_reg, event);
475 }
476 
nrf_lpcomp_shorts_enable(NRF_LPCOMP_Type * p_reg,uint32_t mask)477 NRF_STATIC_INLINE void nrf_lpcomp_shorts_enable(NRF_LPCOMP_Type * p_reg, uint32_t mask)
478 {
479     p_reg->SHORTS |= mask;
480 }
481 
nrf_lpcomp_shorts_disable(NRF_LPCOMP_Type * p_reg,uint32_t mask)482 NRF_STATIC_INLINE void nrf_lpcomp_shorts_disable(NRF_LPCOMP_Type * p_reg, uint32_t mask)
483 {
484     p_reg->SHORTS &= ~mask;
485 }
486 
nrf_lpcomp_shorts_set(NRF_LPCOMP_Type * p_reg,uint32_t mask)487 NRF_STATIC_INLINE void nrf_lpcomp_shorts_set(NRF_LPCOMP_Type * p_reg, uint32_t mask)
488 {
489     p_reg->SHORTS = mask;
490 }
491 
nrf_lpcomp_int_enable(NRF_LPCOMP_Type * p_reg,uint32_t mask)492 NRF_STATIC_INLINE void nrf_lpcomp_int_enable(NRF_LPCOMP_Type * p_reg, uint32_t mask)
493 {
494     p_reg->INTENSET = mask;
495 }
496 
nrf_lpcomp_int_disable(NRF_LPCOMP_Type * p_reg,uint32_t mask)497 NRF_STATIC_INLINE void nrf_lpcomp_int_disable(NRF_LPCOMP_Type * p_reg, uint32_t mask)
498 {
499     p_reg->INTENCLR = mask;
500 }
501 
nrf_lpcomp_int_enable_check(NRF_LPCOMP_Type const * p_reg,uint32_t mask)502 NRF_STATIC_INLINE uint32_t nrf_lpcomp_int_enable_check(NRF_LPCOMP_Type const * p_reg,
503                                                        uint32_t                mask)
504 {
505     return p_reg->INTENSET & mask; // when read this register will return the value of INTEN.
506 }
507 
508 #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
nrf_lpcomp_subscribe_set(NRF_LPCOMP_Type * p_reg,nrf_lpcomp_task_t task,uint8_t channel)509 NRF_STATIC_INLINE void nrf_lpcomp_subscribe_set(NRF_LPCOMP_Type * p_reg,
510                                                 nrf_lpcomp_task_t task,
511                                                 uint8_t           channel)
512 {
513     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
514             ((uint32_t)channel | NRF_SUBSCRIBE_PUBLISH_ENABLE);
515 }
516 
nrf_lpcomp_subscribe_clear(NRF_LPCOMP_Type * p_reg,nrf_lpcomp_task_t task)517 NRF_STATIC_INLINE void nrf_lpcomp_subscribe_clear(NRF_LPCOMP_Type * p_reg,
518                                                   nrf_lpcomp_task_t task)
519 {
520     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
521 }
522 
nrf_lpcomp_publish_set(NRF_LPCOMP_Type * p_reg,nrf_lpcomp_event_t event,uint8_t channel)523 NRF_STATIC_INLINE void nrf_lpcomp_publish_set(NRF_LPCOMP_Type *  p_reg,
524                                               nrf_lpcomp_event_t event,
525                                               uint8_t            channel)
526 {
527     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) =
528             ((uint32_t)channel | NRF_SUBSCRIBE_PUBLISH_ENABLE);
529 }
530 
nrf_lpcomp_publish_clear(NRF_LPCOMP_Type * p_reg,nrf_lpcomp_event_t event)531 NRF_STATIC_INLINE void nrf_lpcomp_publish_clear(NRF_LPCOMP_Type *  p_reg,
532                                                 nrf_lpcomp_event_t event)
533 {
534     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0;
535 }
536 #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
537 
nrf_lpcomp_configure(NRF_LPCOMP_Type * p_reg,nrf_lpcomp_config_t const * p_config)538 NRF_STATIC_INLINE void nrf_lpcomp_configure(NRF_LPCOMP_Type *           p_reg,
539                                             nrf_lpcomp_config_t const * p_config)
540 {
541     p_reg->REFSEL = (p_config->reference << LPCOMP_REFSEL_REFSEL_Pos) & LPCOMP_REFSEL_REFSEL_Msk;
542 
543 #if !NRF_LPCOMP_HAS_AIN_AS_PIN
544     //If external source is choosen extract analog reference index.
545     if ((p_config->reference & LPCOMP_REFSEL_REFSEL_ARef)==LPCOMP_REFSEL_REFSEL_ARef)
546     {
547         uint32_t extref  = p_config->reference >> 16;
548         p_reg->EXTREFSEL = (extref << LPCOMP_EXTREFSEL_EXTREFSEL_Pos) &
549                            LPCOMP_EXTREFSEL_EXTREFSEL_Msk;
550     }
551 #endif
552 
553     p_reg->ANADETECT = (p_config->detection << LPCOMP_ANADETECT_ANADETECT_Pos) &
554                        LPCOMP_ANADETECT_ANADETECT_Msk;
555 #if NRF_LPCOMP_HAS_HYST
556     p_reg->HYST      = ((p_config->hyst) << LPCOMP_HYST_HYST_Pos) & LPCOMP_HYST_HYST_Msk;
557 #endif
558 }
559 
nrf_lpcomp_ref_set(NRF_LPCOMP_Type * p_reg,nrf_lpcomp_ref_t reference)560 NRF_STATIC_INLINE void nrf_lpcomp_ref_set(NRF_LPCOMP_Type * p_reg, nrf_lpcomp_ref_t reference)
561 {
562     p_reg->REFSEL = (reference << LPCOMP_REFSEL_REFSEL_Pos) & LPCOMP_REFSEL_REFSEL_Msk;
563 }
564 
nrf_lpcomp_ext_ref_set(NRF_LPCOMP_Type * p_reg,nrf_lpcomp_ext_ref_t ext_ref)565 NRF_STATIC_INLINE void nrf_lpcomp_ext_ref_set(NRF_LPCOMP_Type *    p_reg,
566                                               nrf_lpcomp_ext_ref_t ext_ref)
567 {
568 #if NRF_LPCOMP_HAS_AIN_AS_PIN
569     p_reg->EXTREFSEL = ((NRF_PIN_NUMBER_TO_PIN(ext_ref) << LPCOMP_EXTREFSEL_PIN_Pos) &
570                         LPCOMP_EXTREFSEL_PIN_Msk)
571                         | ((NRF_PIN_NUMBER_TO_PORT(ext_ref) << LPCOMP_EXTREFSEL_PORT_Pos) &
572                         LPCOMP_EXTREFSEL_PORT_Msk);
573 #else
574     p_reg->EXTREFSEL = (ext_ref << LPCOMP_EXTREFSEL_EXTREFSEL_Pos) &
575                        LPCOMP_EXTREFSEL_EXTREFSEL_Msk;
576 #endif
577 }
578 
nrf_lpcomp_input_select(NRF_LPCOMP_Type * p_reg,nrf_lpcomp_input_t input)579 NRF_STATIC_INLINE void nrf_lpcomp_input_select(NRF_LPCOMP_Type * p_reg, nrf_lpcomp_input_t input)
580 {
581 #if NRF_LPCOMP_HAS_AIN_AS_PIN
582     p_reg->PSEL = (NRF_PIN_NUMBER_TO_PORT(input) << LPCOMP_PSEL_PORT_Pos) |
583                   (NRF_PIN_NUMBER_TO_PIN(input) << LPCOMP_PSEL_PIN_Pos) |
584                   (p_reg->PSEL & ~(LPCOMP_PSEL_PORT_Msk | LPCOMP_PSEL_PIN_Msk));
585 #else
586     p_reg->PSEL = ((uint32_t)input << LPCOMP_PSEL_PSEL_Pos) | (p_reg->PSEL & ~LPCOMP_PSEL_PSEL_Msk);
587 #endif
588 }
589 
nrf_lpcomp_detection_set(NRF_LPCOMP_Type * p_reg,nrf_lpcomp_detect_t detection)590 NRF_STATIC_INLINE void nrf_lpcomp_detection_set(NRF_LPCOMP_Type *   p_reg,
591                                                 nrf_lpcomp_detect_t detection)
592 {
593     p_reg->ANADETECT = (detection << LPCOMP_ANADETECT_ANADETECT_Pos) &
594                        LPCOMP_ANADETECT_ANADETECT_Msk;
595 }
596 
597 #if NRF_LPCOMP_HAS_HYST
nrf_lpcomp_hysteresis_set(NRF_LPCOMP_Type * p_reg,nrf_lpcomp_hyst_t hyst)598 NRF_STATIC_INLINE void nrf_lpcomp_hysteresis_set(NRF_LPCOMP_Type * p_reg,
599                                                  nrf_lpcomp_hyst_t hyst)
600 {
601     p_reg->HYST = ((hyst) << LPCOMP_HYST_HYST_Pos) & LPCOMP_HYST_HYST_Msk;
602 }
603 #endif
604 
nrf_lpcomp_enable(NRF_LPCOMP_Type * p_reg)605 NRF_STATIC_INLINE void nrf_lpcomp_enable(NRF_LPCOMP_Type * p_reg)
606 {
607     p_reg->ENABLE = LPCOMP_ENABLE_ENABLE_Enabled << LPCOMP_ENABLE_ENABLE_Pos;
608 }
609 
nrf_lpcomp_disable(NRF_LPCOMP_Type * p_reg)610 NRF_STATIC_INLINE void nrf_lpcomp_disable(NRF_LPCOMP_Type * p_reg)
611 {
612     p_reg->ENABLE = LPCOMP_ENABLE_ENABLE_Disabled << LPCOMP_ENABLE_ENABLE_Pos;
613 }
614 
nrf_lpcomp_enable_check(NRF_LPCOMP_Type * p_reg)615 NRF_STATIC_INLINE bool nrf_lpcomp_enable_check(NRF_LPCOMP_Type * p_reg)
616 {
617     return ((p_reg->ENABLE) & LPCOMP_ENABLE_ENABLE_Enabled) >> LPCOMP_ENABLE_ENABLE_Pos;
618 }
619 
nrf_lpcomp_result_get(NRF_LPCOMP_Type const * p_reg)620 NRF_STATIC_INLINE uint32_t nrf_lpcomp_result_get(NRF_LPCOMP_Type const * p_reg)
621 {
622     return (uint32_t)p_reg->RESULT;
623 }
624 
625 #endif // NRF_DECLARE_ONLY
626 
627 /** @} */
628 
629 #ifdef __cplusplus
630 }
631 #endif
632 
633 #endif // NRF_LPCOMP_H_
634