1 /*
2 * Copyright (c) 2015 - 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_COMP_H_
35 #define NRF_COMP_H_
36
37 #include <nrfx.h>
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /**
44 * @defgroup nrf_comp_hal COMP HAL
45 * @{
46 * @ingroup nrf_comp
47 * @brief Hardware access layer (HAL) for managing the Comparator (COMP) peripheral.
48 */
49
50 #if defined(COMP_ISOURCE_ISOURCE_Msk) || defined (__NRFX_DOXYGEN__)
51 /** @brief Symbol indicating whether COMP has ISOURCE register. */
52 #define NRF_COMP_HAS_ISOURCE 1
53 #else
54 #define NRF_COMP_HAS_ISOURCE 0
55 #endif
56
57 #if defined(COMP_PSEL_PIN_Msk) || defined(__NRFX_DOXYGEN__)
58 /** @brief Symbol indicating whether the configuration of analog input using pin number is present. */
59 #define NRF_COMP_HAS_AIN_AS_PIN 1
60 #else
61 #define NRF_COMP_HAS_AIN_AS_PIN 0
62 #endif
63
64 /** @brief COMP analog pin selection. */
65 #if NRF_COMP_HAS_AIN_AS_PIN
66 typedef uint32_t nrf_comp_input_t;
67 #else
68 typedef enum
69 {
70 NRF_COMP_INPUT_0 = COMP_PSEL_PSEL_AnalogInput0, /*!< AIN0 selected as analog input. */
71 NRF_COMP_INPUT_1 = COMP_PSEL_PSEL_AnalogInput1, /*!< AIN1 selected as analog input. */
72 NRF_COMP_INPUT_2 = COMP_PSEL_PSEL_AnalogInput2, /*!< AIN2 selected as analog input. */
73 NRF_COMP_INPUT_3 = COMP_PSEL_PSEL_AnalogInput3, /*!< AIN3 selected as analog input. */
74 #if defined (COMP_PSEL_PSEL_AnalogInput4) || defined (__NRFX_DOXYGEN__)
75 NRF_COMP_INPUT_4 = COMP_PSEL_PSEL_AnalogInput4, /*!< AIN4 selected as analog input. */
76 #endif
77 #if defined (COMP_PSEL_PSEL_AnalogInput5) || defined (__NRFX_DOXYGEN__)
78 NRF_COMP_INPUT_5 = COMP_PSEL_PSEL_AnalogInput5, /*!< AIN5 selected as analog input. */
79 #endif
80 #if defined (COMP_PSEL_PSEL_AnalogInput6) || defined (__NRFX_DOXYGEN__)
81 NRF_COMP_INPUT_6 = COMP_PSEL_PSEL_AnalogInput6, /*!< AIN6 selected as analog input. */
82 #endif
83 #if defined (COMP_PSEL_PSEL_AnalogInput7) || defined (__NRFX_DOXYGEN__)
84 NRF_COMP_INPUT_7 = COMP_PSEL_PSEL_AnalogInput7, /*!< AIN7 selected as analog input. */
85 #endif
86 #if defined (COMP_PSEL_PSEL_VddDiv2) || defined (__NRFX_DOXYGEN__)
87 NRF_COMP_VDD_DIV2 = COMP_PSEL_PSEL_VddDiv2, /*!< VDD/2 selected as analog input. */
88 #endif
89 #if defined (COMP_PSEL_PSEL_VddhDiv5) || defined (__NRFX_DOXYGEN__)
90 NRF_COMP_VDDH_DIV5 = COMP_PSEL_PSEL_VddhDiv5, /*!< VDDH/5 selected as analog input. */
91 #endif
92 } nrf_comp_input_t;
93 #endif // NRF_COMP_HAS_AIN_AS_PIN
94
95 /** @brief COMP reference selection. */
96 typedef enum
97 {
98 NRF_COMP_REF_INT_1V2 = COMP_REFSEL_REFSEL_Int1V2, /*!< VREF = internal 1.2 V reference (VDD >= 1.7 V). */
99 #if defined(COMP_REFSEL_REFSEL_Int1V8) || defined(__NRFX_DOXYGEN__)
100 NRF_COMP_REF_INT_1V8 = COMP_REFSEL_REFSEL_Int1V8, /*!< VREF = internal 1.8 V reference (VDD >= VREF + 0.2 V). */
101 #endif
102 #if defined(COMP_REFSEL_REFSEL_Int2V4) || defined(__NRFX_DOXYGEN__)
103 NRF_COMP_REF_INT_2V4 = COMP_REFSEL_REFSEL_Int2V4, /*!< VREF = internal 2.4 V reference (VDD >= VREF + 0.2 V). */
104 #endif
105 #if defined(COMP_REFSEL_REFSEL_AVDDAO1V8) || defined(__NRFX_DOXYGEN__)
106 NRF_COMP_REF_AVDDAO1V8 = COMP_REFSEL_REFSEL_AVDDAO1V8, /*!< VREF = AVDD_AO_1V8. */
107 #endif
108 #if defined(COMP_REFSEL_REFSEL_VDD) || defined(__NRFX_DOXYGEN__)
109 NRF_COMP_REF_VDD = COMP_REFSEL_REFSEL_VDD, /*!< VREF = VDD. */
110 #endif
111 NRF_COMP_REF_AREF = COMP_REFSEL_REFSEL_ARef /*!< VREF = AREF (VDD >= VREF >= AREFMIN). */
112 } nrf_comp_ref_t;
113
114 /** @brief COMP external analog reference selection. */
115 #if NRF_COMP_HAS_AIN_AS_PIN
116 typedef uint32_t nrf_comp_ext_ref_t;
117 #else
118 typedef enum
119 {
120 NRF_COMP_EXT_REF_0 = COMP_EXTREFSEL_EXTREFSEL_AnalogReference0, /*!< Use AIN0 as external analog reference. */
121 NRF_COMP_EXT_REF_1 = COMP_EXTREFSEL_EXTREFSEL_AnalogReference1, /*!< Use AIN1 as external analog reference. */
122 NRF_COMP_EXT_REF_2 = COMP_EXTREFSEL_EXTREFSEL_AnalogReference2, /*!< Use AIN2 as external analog reference. */
123 NRF_COMP_EXT_REF_3 = COMP_EXTREFSEL_EXTREFSEL_AnalogReference3, /*!< Use AIN3 as external analog reference. */
124 #if defined (COMP_EXTREFSEL_EXTREFSEL_AnalogReference4) || defined (__NRFX_DOXYGEN__)
125 NRF_COMP_EXT_REF_4 = COMP_EXTREFSEL_EXTREFSEL_AnalogReference4, /*!< Use AIN4 as external analog reference. */
126 #endif
127 #if defined (COMP_EXTREFSEL_EXTREFSEL_AnalogReference5) || defined (__NRFX_DOXYGEN__)
128 NRF_COMP_EXT_REF_5 = COMP_EXTREFSEL_EXTREFSEL_AnalogReference5, /*!< Use AIN5 as external analog reference. */
129 #endif
130 #if defined (COMP_EXTREFSEL_EXTREFSEL_AnalogReference6) || defined (__NRFX_DOXYGEN__)
131 NRF_COMP_EXT_REF_6 = COMP_EXTREFSEL_EXTREFSEL_AnalogReference6, /*!< Use AIN6 as external analog reference. */
132 #endif
133 #if defined (COMP_EXTREFSEL_EXTREFSEL_AnalogReference7) || defined (__NRFX_DOXYGEN__)
134 NRF_COMP_EXT_REF_7 = COMP_EXTREFSEL_EXTREFSEL_AnalogReference7 /*!< Use AIN7 as external analog reference. */
135 #endif
136 } nrf_comp_ext_ref_t;
137 #endif // NRF_COMP_HAS_AIN_AS_PIN
138
139 /** @brief COMP THDOWN and THUP values that are used to calculate the threshold voltages VDOWN and VUP. */
140 typedef struct
141 {
142 uint8_t th_down; /*!< THDOWN value. */
143 uint8_t th_up; /*!< THUP value. */
144 } nrf_comp_th_t;
145
146 /** @brief COMP main operation mode. */
147 typedef enum
148 {
149 NRF_COMP_MAIN_MODE_SE = COMP_MODE_MAIN_SE, /*!< Single-ended mode. */
150 NRF_COMP_MAIN_MODE_DIFF = COMP_MODE_MAIN_Diff /*!< Differential mode. */
151 } nrf_comp_main_mode_t;
152
153 /** @brief COMP speed and power mode. */
154 typedef enum
155 {
156 NRF_COMP_SP_MODE_LOW = COMP_MODE_SP_Low, /*!< Low power mode. */
157 #if defined (COMP_MODE_SP_Normal) || defined (__NRFX_DOXYGEN__)
158 NRF_COMP_SP_MODE_NORMAL = COMP_MODE_SP_Normal, /*!< Normal mode. */
159 #endif
160 NRF_COMP_SP_MODE_HIGH = COMP_MODE_SP_High /*!< High-speed mode. */
161 } nrf_comp_sp_mode_t;
162
163 /** @brief COMP comparator hysteresis. */
164 typedef enum
165 {
166 NRF_COMP_HYST_NO_HYST = COMP_HYST_HYST_NoHyst, /*!< Comparator hysteresis disabled. */
167 #if defined (COMP_HYST_HYST_Hyst40mV) || defined (__NRFX_DOXYGEN__)
168 NRF_COMP_HYST_40MV = COMP_HYST_HYST_Hyst40mV, /*!< Comparator hysteresis enabled at 40 mV level. */
169 #endif
170 #if defined (COMP_HYST_HYST_Hyst50mV) || defined (__NRFX_DOXYGEN__)
171 NRF_COMP_HYST_50MV = COMP_HYST_HYST_Hyst50mV /*!< Comparator hysteresis enabled at 50 mV level. */
172 #endif
173 } nrf_comp_hyst_t;
174
175 #if NRF_COMP_HAS_ISOURCE
176 /** @brief COMP current source selection on analog input. */
177 typedef enum
178 {
179 NRF_COMP_ISOURCE_OFF = COMP_ISOURCE_ISOURCE_Off, /*!< Current source disabled. */
180 #if defined (COMP_ISOURCE_ISOURCE_Ien2uA5) || defined (__NRFX_DOXYGEN__)
181 NRF_COMP_ISOURCE_IEN_2UA5 = COMP_ISOURCE_ISOURCE_Ien2uA5, /*!< Current source enabled (+/- 2.5 uA). */
182 #else
183 NRF_COMP_ISOURCE_IEN_2UA5 = COMP_ISOURCE_ISOURCE_Ien2mA5, /*!< Current source enabled (+/- 2.5 uA). */
184 #endif
185 #if defined (COMP_ISOURCE_ISOURCE_Ien5uA) || defined (__NRFX_DOXYGEN__)
186 NRF_COMP_ISOURCE_IEN_5UA = COMP_ISOURCE_ISOURCE_Ien5uA, /*!< Current source enabled (+/- 5 uA). */
187 #else
188 NRF_COMP_ISOURCE_IEN_5UA = COMP_ISOURCE_ISOURCE_Ien5mA, /*!< Current source enabled (+/- 5 uA). */
189 #endif
190 #if defined (COMP_ISOURCE_ISOURCE_Ien10uA) || defined (__NRFX_DOXYGEN__)
191 NRF_COMP_ISOURCE_IEN_10UA = COMP_ISOURCE_ISOURCE_Ien10uA, /*!< Current source enabled (+/- 10 uA). */
192 #else
193 NRF_COMP_ISOURCE_IEN_10UA = COMP_ISOURCE_ISOURCE_Ien10mA, /*!< Current source enabled (+/- 10 uA). */
194 #endif
195 } nrf_isource_t;
196 #endif // NRF_COMP_HAS_ISOURCE
197
198 /** @brief COMP tasks. */
199 typedef enum
200 {
201 NRF_COMP_TASK_START = offsetof(NRF_COMP_Type, TASKS_START), /*!< COMP start sampling task. */
202 NRF_COMP_TASK_STOP = offsetof(NRF_COMP_Type, TASKS_STOP), /*!< COMP stop sampling task. */
203 NRF_COMP_TASK_SAMPLE = offsetof(NRF_COMP_Type, TASKS_SAMPLE) /*!< Sample comparator value. */
204 } nrf_comp_task_t;
205
206 /** @brief COMP events. */
207 typedef enum
208 {
209 NRF_COMP_EVENT_READY = offsetof(NRF_COMP_Type, EVENTS_READY), /*!< COMP is ready and output is valid. */
210 NRF_COMP_EVENT_DOWN = offsetof(NRF_COMP_Type, EVENTS_DOWN), /*!< Input voltage crossed the threshold going down. */
211 NRF_COMP_EVENT_UP = offsetof(NRF_COMP_Type, EVENTS_UP), /*!< Input voltage crossed the threshold going up. */
212 NRF_COMP_EVENT_CROSS = offsetof(NRF_COMP_Type, EVENTS_CROSS) /*!< Input voltage crossed the threshold in any direction. */
213 } nrf_comp_event_t;
214
215 /** @brief COMP interrupts. */
216 typedef enum
217 {
218 NRF_COMP_INT_READY_MASK = COMP_INTENSET_READY_Msk, /**< Interrupt on READY event. */
219 NRF_COMP_INT_DOWN_MASK = COMP_INTENSET_DOWN_Msk, /**< Interrupt on DOWN event. */
220 NRF_COMP_INT_UP_MASK = COMP_INTENSET_UP_Msk, /**< Interrupt on UP event. */
221 NRF_COMP_INT_CROSS_MASK = COMP_INTENSET_CROSS_Msk /**< Interrupt on CROSS event. */
222 } nrf_comp_int_mask_t;
223
224 /** @brief COMP shortcut masks. */
225 typedef enum
226 {
227 NRF_COMP_SHORT_STOP_CROSS_MASK = COMP_SHORTS_CROSS_STOP_Msk, ///< Shortcut between the CROSS event and the STOP task.
228 NRF_COMP_SHORT_STOP_UP_MASK = COMP_SHORTS_UP_STOP_Msk, ///< Shortcut between the UP event and the STOP task.
229 NRF_COMP_SHORT_STOP_DOWN_MASK = COMP_SHORTS_DOWN_STOP_Msk ///< Shortcut between the DOWN event and the STOP task.
230 } nrf_comp_short_mask_t;
231
232 /** @brief COMP reference configuration. */
233 typedef struct
234 {
235 nrf_comp_ref_t reference; /*!< COMP reference selection. */
236 nrf_comp_ext_ref_t external; /*!< COMP external analog reference selection. */
237 } nrf_comp_ref_conf_t;
238
239 /**
240 * @brief Function for enabling the COMP peripheral.
241 *
242 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
243 */
244 NRF_STATIC_INLINE void nrf_comp_enable(NRF_COMP_Type * p_reg);
245
246 /**
247 * @brief Function for disabling the COMP peripheral.
248 *
249 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
250 */
251 NRF_STATIC_INLINE void nrf_comp_disable(NRF_COMP_Type * p_reg);
252
253 /**
254 * @brief Function for checking if the COMP peripheral is enabled.
255 *
256 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
257 *
258 * @retval true The COMP peripheral is enabled.
259 * @retval false The COMP peripheral is not enabled.
260 */
261 NRF_STATIC_INLINE bool nrf_comp_enable_check(NRF_COMP_Type const * p_reg);
262
263 /**
264 * @brief Function for setting the reference source.
265 *
266 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
267 * @param[in] reference COMP reference selection.
268 */
269 NRF_STATIC_INLINE void nrf_comp_ref_set(NRF_COMP_Type * p_reg, nrf_comp_ref_t reference);
270
271 /**
272 * @brief Function for setting the external analog reference source.
273 *
274 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
275 * @param[in] ext_ref COMP external analog reference selection.
276 */
277 NRF_STATIC_INLINE void nrf_comp_ext_ref_set(NRF_COMP_Type * p_reg, nrf_comp_ext_ref_t ext_ref);
278
279 /**
280 * @brief Function for setting threshold voltages.
281 *
282 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
283 * @param[in] threshold COMP VDOWN and VUP thresholds.
284 */
285 NRF_STATIC_INLINE void nrf_comp_th_set(NRF_COMP_Type * p_reg, nrf_comp_th_t threshold);
286
287 /**
288 * @brief Function for setting the main mode.
289 *
290 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
291 * @param[in] main_mode COMP main operation mode.
292 */
293 NRF_STATIC_INLINE void nrf_comp_main_mode_set(NRF_COMP_Type * p_reg,
294 nrf_comp_main_mode_t main_mode);
295
296 /**
297 * @brief Function for setting the speed mode.
298 *
299 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
300 * @param[in] speed_mode COMP speed and power mode.
301 */
302 NRF_STATIC_INLINE void nrf_comp_speed_mode_set(NRF_COMP_Type * p_reg,
303 nrf_comp_sp_mode_t speed_mode);
304
305 /**
306 * @brief Function for setting the hysteresis.
307 *
308 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
309 * @param[in] hyst COMP comparator hysteresis.
310 */
311 NRF_STATIC_INLINE void nrf_comp_hysteresis_set(NRF_COMP_Type * p_reg, nrf_comp_hyst_t hyst);
312
313 #if NRF_COMP_HAS_ISOURCE
314 /**
315 * @brief Function for setting the current source on the analog input.
316 *
317 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
318 * @param[in] isource COMP current source selection on analog input.
319 */
320 NRF_STATIC_INLINE void nrf_comp_isource_set(NRF_COMP_Type * p_reg, nrf_isource_t isource);
321 #endif
322
323 /**
324 * @brief Function for selecting the active input of the COMP.
325 *
326 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
327 * @param[in] input Input to be selected.
328 */
329 NRF_STATIC_INLINE void nrf_comp_input_select(NRF_COMP_Type * p_reg, nrf_comp_input_t input);
330
331 /**
332 * @brief Function for getting the last COMP compare result.
333 *
334 * @note If VIN+ == VIN-, the return value depends on the previous result.
335 *
336 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
337 *
338 * @return The last compare result. If 0, then VIN+ < VIN-. If 1, then VIN+ > VIN-.
339 */
340 NRF_STATIC_INLINE uint32_t nrf_comp_result_get(NRF_COMP_Type const * p_reg);
341
342 /**
343 * @brief Function for enabling interrupts from COMP.
344 *
345 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
346 * @param[in] mask Mask of interrupts to be enabled.
347 * Use @ref nrf_comp_int_mask_t values for bit masking.
348 *
349 * @sa nrf_comp_int_enable_check
350 */
351 NRF_STATIC_INLINE void nrf_comp_int_enable(NRF_COMP_Type * p_reg, uint32_t mask);
352
353 /**
354 * @brief Function for disabling interrupts from COMP.
355 *
356 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
357 * @param[in] mask Mask of interrupts to be disabled.
358 * Use @ref nrf_comp_int_mask_t values for bit masking.
359 *
360 * @sa nrf_comp_int_enable_check
361 */
362 NRF_STATIC_INLINE void nrf_comp_int_disable(NRF_COMP_Type * p_reg, uint32_t mask);
363
364 /**
365 * @brief Function for checking if the specified interrupts are enabled.
366 *
367 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
368 * @param[in] mask Mask of interrupts to be checked.
369 * Use @ref nrf_comp_int_mask_t values for bit masking.
370 *
371 * @return Mask of enabled interrupts.
372 */
373 NRF_STATIC_INLINE uint32_t nrf_comp_int_enable_check(NRF_COMP_Type const * p_reg, uint32_t mask);
374
375 /**
376 * @brief Function for getting the address of the specified COMP task register.
377 *
378 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
379 * @param[in] task COMP task.
380 *
381 * @return Address of the specified COMP task.
382 */
383 NRF_STATIC_INLINE uint32_t nrf_comp_task_address_get(NRF_COMP_Type const * p_reg,
384 nrf_comp_task_t task);
385
386 /**
387 * @brief Function for getting the address of the specified COMP event register.
388 *
389 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
390 * @param[in] event COMP event.
391 *
392 * @return Address of the specified COMP event.
393 */
394 NRF_STATIC_INLINE uint32_t nrf_comp_event_address_get(NRF_COMP_Type const * p_reg,
395 nrf_comp_event_t event);
396
397 /**
398 * @brief Function for setting COMP shortcuts.
399 *
400 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
401 * @param[in] mask Mask of shortcuts.
402 */
403 NRF_STATIC_INLINE void nrf_comp_shorts_enable(NRF_COMP_Type * p_reg, uint32_t mask);
404
405 /**
406 * @brief Function for clearing COMP shortcuts by mask.
407 *
408 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
409 * @param[in] mask Mask of shortcuts.
410 */
411 NRF_STATIC_INLINE void nrf_comp_shorts_disable(NRF_COMP_Type * p_reg, uint32_t mask);
412
413 /**
414 * @brief Function for setting the specified COMP task.
415 *
416 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
417 * @param[in] task Task to be activated.
418 */
419 NRF_STATIC_INLINE void nrf_comp_task_trigger(NRF_COMP_Type * p_reg, nrf_comp_task_t task);
420
421 /**
422 * @brief Function for clearing the specified COMP event.
423 *
424 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
425 * @param[in] event COMP event to be cleared.
426 */
427 NRF_STATIC_INLINE void nrf_comp_event_clear(NRF_COMP_Type * p_reg, nrf_comp_event_t event);
428
429 /**
430 * @brief Function for retrieving the state of the COMP event.
431 *
432 * @param[in] p_reg Pointer to the structure of registers of the peripheral.
433 * @param[in] event Event to be checked.
434 *
435 * @retval true The event has been generated.
436 * @retval false The event has not been generated.
437 */
438 NRF_STATIC_INLINE bool nrf_comp_event_check(NRF_COMP_Type const * p_reg, nrf_comp_event_t event);
439
440 #ifndef NRF_DECLARE_ONLY
441
nrf_comp_enable(NRF_COMP_Type * p_reg)442 NRF_STATIC_INLINE void nrf_comp_enable(NRF_COMP_Type * p_reg)
443 {
444 p_reg->ENABLE = (COMP_ENABLE_ENABLE_Enabled << COMP_ENABLE_ENABLE_Pos);
445 }
446
nrf_comp_disable(NRF_COMP_Type * p_reg)447 NRF_STATIC_INLINE void nrf_comp_disable(NRF_COMP_Type * p_reg)
448 {
449 p_reg->ENABLE = (COMP_ENABLE_ENABLE_Disabled << COMP_ENABLE_ENABLE_Pos);
450 }
451
nrf_comp_enable_check(NRF_COMP_Type const * p_reg)452 NRF_STATIC_INLINE bool nrf_comp_enable_check(NRF_COMP_Type const * p_reg)
453 {
454 return ((p_reg->ENABLE) & COMP_ENABLE_ENABLE_Enabled);
455 }
456
nrf_comp_ref_set(NRF_COMP_Type * p_reg,nrf_comp_ref_t reference)457 NRF_STATIC_INLINE void nrf_comp_ref_set(NRF_COMP_Type * p_reg, nrf_comp_ref_t reference)
458 {
459 p_reg->REFSEL = (reference << COMP_REFSEL_REFSEL_Pos);
460 }
461
nrf_comp_ext_ref_set(NRF_COMP_Type * p_reg,nrf_comp_ext_ref_t ext_ref)462 NRF_STATIC_INLINE void nrf_comp_ext_ref_set(NRF_COMP_Type * p_reg, nrf_comp_ext_ref_t ext_ref)
463 {
464 #if NRF_COMP_HAS_AIN_AS_PIN
465 p_reg->EXTREFSEL = (NRF_PIN_NUMBER_TO_PORT(ext_ref) << COMP_EXTREFSEL_PORT_Pos) |
466 (NRF_PIN_NUMBER_TO_PIN(ext_ref) << COMP_EXTREFSEL_PIN_Pos) |
467 (p_reg->EXTREFSEL & ~(COMP_EXTREFSEL_PORT_Msk | COMP_EXTREFSEL_PIN_Msk));
468 #else
469 p_reg->EXTREFSEL = ((uint32_t)ext_ref << COMP_EXTREFSEL_EXTREFSEL_Pos) |
470 (p_reg->EXTREFSEL & ~COMP_EXTREFSEL_EXTREFSEL_Msk);
471 #endif
472 }
473
nrf_comp_th_set(NRF_COMP_Type * p_reg,nrf_comp_th_t threshold)474 NRF_STATIC_INLINE void nrf_comp_th_set(NRF_COMP_Type * p_reg, nrf_comp_th_t threshold)
475 {
476 p_reg->TH =
477 (((uint32_t)threshold.th_down << COMP_TH_THDOWN_Pos) & COMP_TH_THDOWN_Msk) |
478 (((uint32_t)threshold.th_up << COMP_TH_THUP_Pos) & COMP_TH_THUP_Msk);
479 }
480
nrf_comp_main_mode_set(NRF_COMP_Type * p_reg,nrf_comp_main_mode_t main_mode)481 NRF_STATIC_INLINE void nrf_comp_main_mode_set(NRF_COMP_Type * p_reg,
482 nrf_comp_main_mode_t main_mode)
483 {
484 p_reg->MODE |= (main_mode << COMP_MODE_MAIN_Pos);
485 }
486
nrf_comp_speed_mode_set(NRF_COMP_Type * p_reg,nrf_comp_sp_mode_t speed_mode)487 NRF_STATIC_INLINE void nrf_comp_speed_mode_set(NRF_COMP_Type * p_reg,
488 nrf_comp_sp_mode_t speed_mode)
489 {
490 p_reg->MODE |= (speed_mode << COMP_MODE_SP_Pos);
491 }
492
nrf_comp_hysteresis_set(NRF_COMP_Type * p_reg,nrf_comp_hyst_t hyst)493 NRF_STATIC_INLINE void nrf_comp_hysteresis_set(NRF_COMP_Type * p_reg, nrf_comp_hyst_t hyst)
494 {
495 p_reg->HYST = (hyst << COMP_HYST_HYST_Pos) & COMP_HYST_HYST_Msk;
496 }
497
498 #if defined (COMP_ISOURCE_ISOURCE_Msk)
nrf_comp_isource_set(NRF_COMP_Type * p_reg,nrf_isource_t isource)499 NRF_STATIC_INLINE void nrf_comp_isource_set(NRF_COMP_Type * p_reg, nrf_isource_t isource)
500 {
501 p_reg->ISOURCE = (isource << COMP_ISOURCE_ISOURCE_Pos) & COMP_ISOURCE_ISOURCE_Msk;
502 }
503 #endif
504
nrf_comp_input_select(NRF_COMP_Type * p_reg,nrf_comp_input_t input)505 NRF_STATIC_INLINE void nrf_comp_input_select(NRF_COMP_Type * p_reg, nrf_comp_input_t input)
506 {
507 #if NRF_COMP_HAS_AIN_AS_PIN
508 p_reg->PSEL = (NRF_PIN_NUMBER_TO_PORT(input) << COMP_PSEL_PORT_Pos) |
509 (NRF_PIN_NUMBER_TO_PIN(input) << COMP_PSEL_PIN_Pos) |
510 (p_reg->PSEL & ~(COMP_PSEL_PORT_Msk | COMP_PSEL_PIN_Msk));
511 #else
512 p_reg->PSEL = ((uint32_t)input << COMP_PSEL_PSEL_Pos) | (p_reg->PSEL & ~COMP_PSEL_PSEL_Msk);
513 #endif
514 }
515
nrf_comp_result_get(NRF_COMP_Type const * p_reg)516 NRF_STATIC_INLINE uint32_t nrf_comp_result_get(NRF_COMP_Type const * p_reg)
517 {
518 return (uint32_t)p_reg->RESULT;
519 }
520
nrf_comp_int_enable(NRF_COMP_Type * p_reg,uint32_t mask)521 NRF_STATIC_INLINE void nrf_comp_int_enable(NRF_COMP_Type * p_reg, uint32_t mask)
522 {
523 p_reg->INTENSET = mask;
524 }
525
nrf_comp_int_disable(NRF_COMP_Type * p_reg,uint32_t mask)526 NRF_STATIC_INLINE void nrf_comp_int_disable(NRF_COMP_Type * p_reg, uint32_t mask)
527 {
528 p_reg->INTENCLR = mask;
529 }
530
nrf_comp_int_enable_check(NRF_COMP_Type const * p_reg,uint32_t mask)531 NRF_STATIC_INLINE uint32_t nrf_comp_int_enable_check(NRF_COMP_Type const * p_reg, uint32_t mask)
532 {
533 return p_reg->INTENSET & mask; // When read, this register returns the value of INTEN.
534 }
535
nrf_comp_task_address_get(NRF_COMP_Type const * p_reg,nrf_comp_task_t task)536 NRF_STATIC_INLINE uint32_t nrf_comp_task_address_get(NRF_COMP_Type const * p_reg,
537 nrf_comp_task_t task)
538 {
539 return nrf_task_event_address_get(p_reg, task);
540 }
541
nrf_comp_event_address_get(NRF_COMP_Type const * p_reg,nrf_comp_event_t event)542 NRF_STATIC_INLINE uint32_t nrf_comp_event_address_get(NRF_COMP_Type const * p_reg,
543 nrf_comp_event_t event)
544 {
545 return nrf_task_event_address_get(p_reg, event);
546 }
547
nrf_comp_shorts_enable(NRF_COMP_Type * p_reg,uint32_t mask)548 NRF_STATIC_INLINE void nrf_comp_shorts_enable(NRF_COMP_Type * p_reg, uint32_t mask)
549 {
550 p_reg->SHORTS |= mask;
551 }
552
nrf_comp_shorts_disable(NRF_COMP_Type * p_reg,uint32_t mask)553 NRF_STATIC_INLINE void nrf_comp_shorts_disable(NRF_COMP_Type * p_reg, uint32_t mask)
554 {
555 p_reg->SHORTS &= ~mask;
556 }
557
nrf_comp_task_trigger(NRF_COMP_Type * p_reg,nrf_comp_task_t task)558 NRF_STATIC_INLINE void nrf_comp_task_trigger(NRF_COMP_Type * p_reg, nrf_comp_task_t task)
559 {
560 *( (volatile uint32_t *)( (uint8_t *)p_reg + (uint32_t)task) ) = 1;
561 }
562
nrf_comp_event_clear(NRF_COMP_Type * p_reg,nrf_comp_event_t event)563 NRF_STATIC_INLINE void nrf_comp_event_clear(NRF_COMP_Type * p_reg, nrf_comp_event_t event)
564 {
565 *( (volatile uint32_t *)( (uint8_t *)p_reg + (uint32_t)event) ) = 0;
566 nrf_event_readback((uint8_t *)p_reg + (uint32_t)event);
567 }
568
nrf_comp_event_check(NRF_COMP_Type const * p_reg,nrf_comp_event_t event)569 NRF_STATIC_INLINE bool nrf_comp_event_check(NRF_COMP_Type const * p_reg, nrf_comp_event_t event)
570 {
571 return nrf_event_check(p_reg, event);
572 }
573
574 #endif // NRF_DECLARE_ONLY
575
576 /** @} */
577
578 #ifdef __cplusplus
579 }
580 #endif
581
582 #endif // NRF_COMP_H_
583