1 /***************************************************************************//**
2 * \file cy_lpcomp.h
3 * \version 1.70
4 *
5 * This file provides constants and parameter values for the Low Power Comparator driver.
6 *
7 ********************************************************************************
8 * \copyright
9 * (c) (2016-2024), Cypress Semiconductor Corporation (an Infineon company) or
10 * an affiliate of Cypress Semiconductor Corporation.
11 *
12 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License");
15 * you may not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
22 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
25 *******************************************************************************/
26
27 /**
28 * \addtogroup group_lpcomp
29 * \{
30 * Provides access to low-power comparators implemented using the fixed-function,
31 * LP comparator block that is present in the CAT1A and CAT1D devices.
32 *
33 * The functions and other declarations used in this driver are in the cy_lpcomp.h file.
34 * You can include the cy_pdl.h to get access to all functions
35 * and declarations in the PDL.
36 *
37 * Such comparators can perform a fast analog signal comparison of internal
38 * and external analog signals in all system power modes. The low-power comparator
39 * output can be inspected by the CPU, used as an interrupt/wakeup source to the
40 * CPU in Low-power mode (Sleep, Low-power Sleep or Deep-Sleep), used as
41 * a wakeup source to system resources in Hibernate mode, or fed to DSI as
42 * an asynchronous or synchronous signal (level or pulse).
43 *
44 * \section group_lpcomp_section_Configuration_Considerations Configuration Considerations
45 * To set up a low-power comparator, the inputs, output, mode, interrupts and
46 * the other configuration parameters are configured. Power the low-power comparator
47 * to start it operate.
48 *
49 * The sequence recommended for the low-power comparator operation:
50 *
51 * 1) To initialize the driver, call the Cy_LPComp_Init_Ext() function providing
52 * the filled cy_stc_lpcomp_config_t structure, the low-power comparator
53 * channel number and the low-power comparator registers structure pointer.
54 *
55 * 2) Optionally, configure the interrupt requests if the interrupt event
56 * triggering is needed. Use the Cy_LPComp_SetInterruptMask() function with
57 * the parameter for the mask available in the configuration file.
58 * Additionally, enable the Global interrupts and initialize the referenced
59 * interrupt by setting the priority and the interrupt vector using
60 * the \ref Cy_SysInt_Init() function of the sysint driver.
61 *
62 * 3) Configure the inputs and the output using the \ref Cy_GPIO_Pin_Init()
63 * functions of the GPIO driver.
64 * High Impedance Analog drive mode is for the inputs and
65 * Strong drive mode - for the output.
66 * Use the Cy_LPComp_SetInputs() function to connect the comparator inputs
67 * to the dedicated IO pins, AMUXBUSA/AMUXBUSB or Vref:
68 * \image html lpcomp_inputs.png
69 *
70 * 4) Power on the comparator using the Cy_LPComp_Enable_Ext() function.
71 *
72 * 5) The comparator output can be monitored using
73 * the Cy_LPComp_GetCompare() function or using the low-power comparator
74 * interrupt (if the interrupt is enabled).
75 *
76 * \note The interrupt is not cleared automatically.
77 * It is the user's responsibility to do that.
78 * The interrupt is cleared by writing "1" in the corresponding interrupt
79 * register bit position. The preferred way to clear interrupt sources
80 * is using the Cy_LPComp_ClearInterrupt() function.
81 *
82 * \note Individual comparator interrupt outputs are ORed together
83 * as a single asynchronous interrupt source before it is sent out and
84 * used to wake up the system in the low-power mode.
85 * For CAT1A, CAT1D devices, the individual comparator interrupt is masked
86 * by the INTR_MASK register. The masked result is captured in
87 * the INTR_MASKED register.
88 * Writing "1" to the INTR register bit will clear the interrupt.
89 *
90 * \section group_lpcomp_lp Low Power Support
91 * The low-power comparator provides callback functions to facilitate
92 * the low-power mode transition.
93 * Call \ref Cy_LPComp_DeepSleepCallback during execution of
94 * \ref Cy_SysPm_CpuEnterDeepSleep.
95 * Call \ref Cy_LPComp_HibernateCallback during execution of
96 * \ref Cy_SysPm_SystemEnterHibernate.
97 * To trigger callback execution, register the callback before calling
98 * the mode transition function.
99 * Refer to \ref group_syspm driver for more
100 * information about low-power mode transition.
101 *
102 * The example below shows the entering Hibernate mode.
103 * The positive low-power comparator input connects to the dedicated GPIO pin
104 * and the negative low-power comparator input connects to the local reference.
105 * The LED blinks twice after a device reset and goes into Hibernate mode.
106 * When the voltage on the positive input is greater than the local reference
107 * voltage (0.45V - 0.75V), the device wakes up and the LED starts blinking. \n
108 * Section of declarations:
109 * \snippet lpcomp/snippet/main.c LP_COMP_CFG_HIBERNATE
110 * Section of application code:
111 * \snippet lpcomp/snippet/main.c LP_COMP_FUNC_HIBERNATE
112 * \section group_lpcomp_more_information More Information
113 *
114 * For a detailed description of the registers, refer to
115 * the appropriate device technical reference manual (TRM).
116 *
117 * \section group_lpcomp_Changelog Changelog
118 * <table class="doxtable">
119 * <tr><th>Version</th><th>Changes</th><th>Reason for Change</th></tr>
120 * <tr>
121 * <td>1.70</td>
122 * <td>Added support for CAT1B and CAT1D devices.</td>
123 * <td>Support of new IP version.</td>
124 * </tr>
125 * <tr>
126 * <td rowspan="2">1.60</td>
127 * <td>Fix in hibernate callback function.</td>
128 * <td>The low power comparator was unable to wake-up the system from hibernate state.</td>
129 * </tr>
130 * <tr>
131 * <td>Documentation update and clarification.</td>
132 * <td>The code snippet in chapter Low Power Support was updated after validation.</td>
133 * </tr>
134 * <tr>
135 * <td rowspan="2">1.50</td>
136 * <td> Minor improvement in implementation of disabling functionality for the comparator.</td>
137 * <td> Power drive mode for the comparator is restored to the level before disabling,
138 * instead of initial level.</td>
139 * </tr>
140 * <tr>
141 * <td> Documented MISRA C-2012 violation of the Rule 10.3.</td>
142 * <td> MISRA C-2012 compliance.</td>
143 * </tr>
144 * <tr>
145 * <td>1.40</td>
146 * <td>Introduced an extended versions of the existing functions with
147 * a pointer to the context structure allocated by the user:
148 * * \ref Cy_LPComp_Init_Ext(),
149 * * \ref Cy_LPComp_Enable_Ext(),
150 * * \ref Cy_LPComp_Disable_Ext(),
151 * * \ref Cy_LPComp_SetInterruptTriggerMode_Ext(),
152 * * \ref Cy_LPComp_SetPower_Ext()
153 * </td>
154 * <td>
155 * * Improved returning of the comparator from disabled to operational state
156 * with restoring power drive and interrupt edge-detect modes, configured before disable.
157 * * Improved thread safe implementation of the PDL.
158 * </td>
159 * </tr>
160 * <tr>
161 * <td>1.30</td>
162 * <td>Fixed/documented MISRA 2012 violations.</td>
163 * <td>MISRA 2012 compliance.</td>
164 * </tr>
165 * <tr>
166 * <td>1.20.1</td>
167 * <td>Minor documentation updates.</td>
168 * <td>Documentation enhancement.</td>
169 * </tr>
170 * <tr>
171 * <td rowspan="2">1.20</td>
172 * <td>Flattened the organization of the driver source code into the single
173 * source directory and the single include directory.
174 * </td>
175 * <td>Driver library directory-structure simplification.</td>
176 * </tr>
177 * <tr>
178 * <td>Added register access layer. Use register access macros instead
179 * of direct register access using de-referenced pointers.</td>
180 * <td>Makes register access device-independent, so that the PDL does
181 * not need to be recompiled for each supported part number.</td>
182 * </tr>
183 * <tr>
184 * <td>1.10.1</td>
185 * <td>Added Low Power Callback section</td>
186 * <td>Documentation update and clarification</td>
187 * </tr>
188 * <tr>
189 * <td>1.10</td>
190 * <td>The CY_WEAK keyword is removed from Cy_LPComp_DeepSleepCallback()
191 * and Cy_LPComp_HibernateCallback() functions<br>
192 * Added input parameter validation to the API functions.</td>
193 * <td></td>
194 * </tr>
195 * <tr>
196 * <td>1.0</td>
197 * <td>Initial version</td>
198 * <td></td>
199 * </tr>
200 * </table>
201 *
202 * \defgroup group_lpcomp_macros Macros
203 * \defgroup group_lpcomp_functions Functions
204 * \{
205 * \defgroup group_lpcomp_functions_syspm_callback Low Power Callback
206 * \}
207 * \defgroup group_lpcomp_data_structures Data Structures
208 * \defgroup group_lpcomp_enums Enumerated Types
209 */
210
211 #ifndef CY_LPCOMP_PDL_H
212 #define CY_LPCOMP_PDL_H
213
214 /******************************************************************************/
215 /* Include files */
216 /******************************************************************************/
217
218 #include "cy_device.h"
219
220 #if defined (CY_IP_MXLPCOMP) || defined (CY_IP_MXS22LPCOMP) || defined (CY_IP_MXS40LPCOMP)
221
222 #include <stdbool.h>
223 #include <stddef.h>
224 #include "cy_syslib.h"
225 #include "cy_syspm.h"
226
227 #ifdef __cplusplus
228 extern "C"
229 {
230 #endif
231
232 /**
233 * \addtogroup group_lpcomp_macros
234 * \{
235 */
236
237 /** Driver major version. */
238 #define CY_LPCOMP_DRV_VERSION_MAJOR 1
239
240 /** Driver minor version. */
241 #define CY_LPCOMP_DRV_VERSION_MINOR 70
242
243 /******************************************************************************
244 * API Constants
245 ******************************************************************************/
246
247 /** The low-power comparator PDL ID. */
248 #define CY_LPCOMP_ID CY_PDL_DRV_ID(0x23u)
249
250 /** The low-power comparator number of channels. */
251 #define CY_LPCOMP_MAX_CHANNEL_NUM (2u)
252
253 /** The low-power comparator comparator 1 interrupt mask. */
254 #define CY_LPCOMP_COMP0 (LPCOMP_INTR_COMP0_Msk)
255 /** The low-power comparator comparator 2 interrupt mask. */
256 #define CY_LPCOMP_COMP1 (LPCOMP_INTR_COMP1_Msk)
257
258 /** \cond INTERNAL_MACROS */
259
260
261 /******************************************************************************
262 * Registers Constants
263 ******************************************************************************/
264
265 #define CY_LPCOMP_MODE_ULP_Pos (0x0uL)
266 #define CY_LPCOMP_MODE_ULP_Msk (0x1uL)
267
268 #define CY_LPCOMP_INTR_Pos (LPCOMP_INTR_COMP0_Pos)
269 #define CY_LPCOMP_INTR_Msk (LPCOMP_INTR_COMP0_Msk | LPCOMP_INTR_COMP1_Msk)
270
271 #define CY_LPCOMP_CMP0_SW_POS_Msk (LPCOMP_CMP0_SW_CMP0_IP0_Msk | \
272 LPCOMP_CMP0_SW_CMP0_AP0_Msk | \
273 LPCOMP_CMP0_SW_CMP0_BP0_Msk)
274 #define CY_LPCOMP_CMP0_SW_NEG_Msk (LPCOMP_CMP0_SW_CMP0_IN0_Msk | \
275 LPCOMP_CMP0_SW_CMP0_AN0_Msk | \
276 LPCOMP_CMP0_SW_CMP0_BN0_Msk | \
277 LPCOMP_CMP0_SW_CMP0_VN0_Msk)
278 #define CY_LPCOMP_CMP1_SW_POS_Msk (LPCOMP_CMP1_SW_CMP1_IP1_Msk | \
279 LPCOMP_CMP1_SW_CMP1_AP1_Msk | \
280 LPCOMP_CMP1_SW_CMP1_BP1_Msk)
281 #define CY_LPCOMP_CMP1_SW_NEG_Msk (LPCOMP_CMP1_SW_CMP1_IN1_Msk | \
282 LPCOMP_CMP1_SW_CMP1_AN1_Msk | \
283 LPCOMP_CMP1_SW_CMP1_BN1_Msk | \
284 LPCOMP_CMP1_SW_CMP1_VN1_Msk)
285
286 #define CY_LPCOMP_CMP0_OUTPUT_CONFIG_Pos LPCOMP_CMP0_CTRL_DSI_BYPASS0_Pos
287 #define CY_LPCOMP_CMP1_OUTPUT_CONFIG_Pos LPCOMP_CMP1_CTRL_DSI_BYPASS1_Pos
288
289 #define CY_LPCOMP_CMP0_OUTPUT_CONFIG_Msk (LPCOMP_CMP0_CTRL_DSI_BYPASS0_Msk | \
290 LPCOMP_CMP0_CTRL_DSI_LEVEL0_Msk)
291
292 #define CY_LPCOMP_CMP1_OUTPUT_CONFIG_Msk (LPCOMP_CMP1_CTRL_DSI_BYPASS1_Msk | \
293 LPCOMP_CMP1_CTRL_DSI_LEVEL1_Msk)
294
295 #define CY_HSIOM_AMUX_SPLIT_CTL_SWITCH_AA_SL_SR_Pos HSIOM_AMUX_SPLIT_CTL_SWITCH_AA_SL_Pos
296
297 #define CY_HSIOM_AMUX_SPLIT_CTL_SWITCH_AA_SL_SR_Msk (HSIOM_AMUX_SPLIT_CTL_SWITCH_AA_SL_Msk | \
298 HSIOM_AMUX_SPLIT_CTL_SWITCH_AA_SR_Msk)
299
300 #define CY_HSIOM_AMUX_SPLIT_CTL_SWITCH_BB_SL_SR_Pos HSIOM_AMUX_SPLIT_CTL_SWITCH_BB_SL_Pos
301
302 #define CY_HSIOM_AMUX_SPLIT_CTL_SWITCH_BB_SL_SR_Msk (HSIOM_AMUX_SPLIT_CTL_SWITCH_BB_SL_Msk | \
303 HSIOM_AMUX_SPLIT_CTL_SWITCH_BB_SR_Msk)
304
305 #define CY_LPCOMP_REF_CONNECTED (1u)
306
307 #define CY_LPCOMP_WAKEUP_PIN0_Msk CY_SYSPM_WAKEUP_LPCOMP0
308 #define CY_LPCOMP_WAKEUP_PIN1_Msk CY_SYSPM_WAKEUP_LPCOMP1
309
310 /* Internal constants for Cy_LPComp_Enable() */
311 #define CY_LPCOMP_NORMAL_POWER_DELAY (3u)
312 #define CY_LPCOMP_LP_POWER_DELAY (6u)
313 #define CY_LPCOMP_ULP_POWER_DELAY (50u)
314
315 /** \endcond */
316 /** \} group_lpcomp_macros */
317
318 /**
319 * \addtogroup group_lpcomp_enums
320 * \{
321 */
322
323 /******************************************************************************
324 * Enumerations
325 *****************************************************************************/
326 /** The low-power comparator output modes. */
327 typedef enum
328 {
329 CY_LPCOMP_OUT_PULSE = 0u, /**< The low-power comparator DSI output with the pulse option, no bypass. */
330 CY_LPCOMP_OUT_DIRECT = 1u, /**< The low-power comparator Bypass mode, the direct output of a comparator. */
331 CY_LPCOMP_OUT_SYNC = 2u /**< The low-power comparator DSI output with the level option, it is similar
332 to Bypass mode but it is 1 cycle slower. */
333 } cy_en_lpcomp_out_t;
334
335 /** The low-power comparator hysteresis modes. */
336 typedef enum
337 {
338 CY_LPCOMP_HYST_ENABLE = 1u, /**< The low-power comparator enable hysteresis. */
339 CY_LPCOMP_HYST_DISABLE = 0u /**< The low-power comparator disable hysteresis. */
340 } cy_en_lpcomp_hyst_t;
341
342 /** The low-power comparator channel number. */
343 typedef enum
344 {
345 CY_LPCOMP_CHANNEL_0 = 0x1u, /**< The low-power comparator 0. */
346 CY_LPCOMP_CHANNEL_1 = 0x2u /**< The low-power comparator 1. */
347 } cy_en_lpcomp_channel_t;
348
349 /** The low-power comparator interrupt modes. */
350 typedef enum
351 {
352 CY_LPCOMP_INTR_DISABLE = 0u, /**< The low-power comparator interrupt is disabled, no interrupt will be detected. */
353 CY_LPCOMP_INTR_RISING = 1u, /**< The low-power comparator interrupt on the rising edge. */
354 CY_LPCOMP_INTR_FALLING = 2u, /**< The low-power comparator interrupt on the falling edge. */
355 CY_LPCOMP_INTR_BOTH = 3u /**< The low-power comparator interrupt on both rising and falling edges. */
356 } cy_en_lpcomp_int_t;
357
358 /** The low-power comparator power-mode selection. */
359 typedef enum
360 {
361 CY_LPCOMP_MODE_OFF = 0u, /**< The low-power comparator channel power-off. */
362 CY_LPCOMP_MODE_ULP = 1u, /**< The low-power comparator channel ULP mode. */
363 CY_LPCOMP_MODE_LP = 2u, /**< The low-power comparator channel LP mode. */
364 CY_LPCOMP_MODE_NORMAL = 3u /**< The low-power comparator channel normal mode. */
365 } cy_en_lpcomp_pwr_t;
366
367 /** The low-power comparator inputs. */
368 typedef enum
369 {
370 CY_LPCOMP_SW_GPIO = 0x01u, /**< The low-power comparator input connects to GPIO pin. */
371 CY_LPCOMP_SW_AMUXBUSA = 0x02u, /**< The low-power comparator input connects to AMUXBUSA. */
372 CY_LPCOMP_SW_AMUXBUSB = 0x04u, /**< The low-power comparator input connects to AMUXBUSB. */
373 CY_LPCOMP_SW_LOCAL_VREF = 0x08u /**< The low-power comparator input connects to local VREF. */
374 } cy_en_lpcomp_inputs_t;
375
376 /** The low-power comparator error codes. */
377 typedef enum
378 {
379 CY_LPCOMP_SUCCESS = 0x00u, /**< Successful */
380 CY_LPCOMP_BAD_PARAM = CY_LPCOMP_ID | CY_PDL_STATUS_ERROR | 0x01u, /**< One or more invalid parameters */
381 CY_LPCOMP_TRIMM_ERR = CY_LPCOMP_ID | CY_PDL_STATUS_ERROR | 0x02u, /**< Read trimmings fails */
382 } cy_en_lpcomp_status_t;
383
384 #if defined (CY_IP_MXS22LPCOMP)
385 /** The low-power comparator trim polarity. */
386 typedef enum
387 {
388 CY_LPCOMP_TRIM_NEGATIVE = 0x00UL, /**< The low-power comparator trim is negative. */
389 CY_LPCOMP_TRIM_POSITIVE = 0x01UL, /**< The low-power comparator trim is positive. */
390 } cy_en_lpcomp_trim_polarity_t;
391
392 /** The low-power comparator trim magnitude. */
393 typedef enum
394 {
395 CY_LPCOMP_TRIM_0mV = 0x00UL, /**< The low-power comparator trim value 0mV. */
396 CY_LPCOMP_TRIM_1mV = 0x01UL, /**< The low-power comparator trim value 1mV. */
397 CY_LPCOMP_TRIM_2mV = 0x02UL, /**< The low-power comparator trim value 2mV. */
398 CY_LPCOMP_TRIM_3mV = 0x03UL, /**< The low-power comparator trim value 3mV. */
399 CY_LPCOMP_TRIM_4mV = 0x04UL, /**< The low-power comparator trim value 4mV. */
400 CY_LPCOMP_TRIM_5mV = 0x05UL, /**< The low-power comparator trim value 5mV. */
401 CY_LPCOMP_TRIM_6mV = 0x06UL, /**< The low-power comparator trim value 6mV. */
402 CY_LPCOMP_TRIM_7mV = 0x07UL, /**< The low-power comparator trim value 7mV. */
403 CY_LPCOMP_TRIM_8mV = 0x08UL, /**< The low-power comparator trim value 8mV. */
404 CY_LPCOMP_TRIM_9mV = 0x09UL, /**< The low-power comparator trim value 9mV. */
405 CY_LPCOMP_TRIM_10mV = 0x0AUL, /**< The low-power comparator trim value 10mV. */
406 CY_LPCOMP_TRIM_11mV = 0x0BUL, /**< The low-power comparator trim value 11mV. */
407 CY_LPCOMP_TRIM_12mV = 0x0CUL, /**< The low-power comparator trim value 12mV. */
408 CY_LPCOMP_TRIM_13mV = 0x0DUL, /**< The low-power comparator trim value 13mV. */
409 CY_LPCOMP_TRIM_14mV = 0x0EUL, /**< The low-power comparator trim value 14mV. */
410 CY_LPCOMP_TRIM_15mV = 0x0FUL /**< The low-power comparator trim value 15mV. */
411 } cy_en_lpcomp_trim_magnitude_t;
412 #endif
413
414 /** \} group_lpcomp_enums */
415
416 /**
417 * \addtogroup group_lpcomp_data_structures
418 * \{
419 */
420
421 /******************************************************************************
422 * Structures
423 *****************************************************************************/
424
425 /** The low-power comparator configuration structure. */
426 typedef struct {
427 cy_en_lpcomp_out_t outputMode; /**< The low-power comparator outputMode: Direct output,
428 Synchronized output or Pulse output */
429 cy_en_lpcomp_hyst_t hysteresis; /**< Enables or disables the low-power comparator hysteresis */
430 cy_en_lpcomp_pwr_t power; /**< Sets the low-power comparator power mode */
431 cy_en_lpcomp_int_t intType; /**< Sets the low-power comparator interrupt mode */
432 } cy_stc_lpcomp_config_t;
433
434 #if defined (CY_IP_MXS22LPCOMP)
435 /** The low-power comparator trim structure. */
436 typedef struct
437 {
438 bool enable; /**< Enables or disables the low-power comparator trim */
439 cy_en_lpcomp_trim_polarity_t polarity; /**< Defines the low-power comparator trim polarity */
440 cy_en_lpcomp_trim_magnitude_t magnitude; /**< Defines the low-power comparator trim magnitude */
441 } cy_en_lpcomp_trim_t;
442 #endif
443
444 /** The low-power comparator context structure, used to improve thread-safe implementation of the PDL.
445 * It is applicable for the following functions:
446 * * \ref Cy_LPComp_Init_Ext(),
447 * * \ref Cy_LPComp_Enable_Ext(),
448 * * \ref Cy_LPComp_Disable_Ext(),
449 * * \ref Cy_LPComp_SetInterruptTriggerMode_Ext(),
450 * * \ref Cy_LPComp_SetPower_Ext()
451 */
452 typedef struct {
453 /** \cond CONTEXT_STRUCTURE */
454 cy_en_lpcomp_int_t intType[CY_LPCOMP_MAX_CHANNEL_NUM];
455 cy_en_lpcomp_pwr_t power[CY_LPCOMP_MAX_CHANNEL_NUM];
456 /** \endcond */
457 } cy_stc_lpcomp_context_t;
458
459 /** \} group_lpcomp_data_structures */
460
461 /** \cond INTERNAL_MACROS */
462
463 /******************************************************************************
464 * Macros
465 *****************************************************************************/
466 #define CY_LPCOMP_IS_CHANNEL_VALID(channel) (((channel) == CY_LPCOMP_CHANNEL_0) || \
467 ((channel) == CY_LPCOMP_CHANNEL_1))
468 #define CY_LPCOMP_IS_OUT_MODE_VALID(mode) (((mode) == CY_LPCOMP_OUT_PULSE) || \
469 ((mode) == CY_LPCOMP_OUT_DIRECT) || \
470 ((mode) == CY_LPCOMP_OUT_SYNC))
471 #define CY_LPCOMP_IS_HYSTERESIS_VALID(hyst) (((hyst) == CY_LPCOMP_HYST_ENABLE) || \
472 ((hyst) == CY_LPCOMP_HYST_DISABLE))
473 #define CY_LPCOMP_IS_INTR_MODE_VALID(intr) (((intr) == CY_LPCOMP_INTR_DISABLE) || \
474 ((intr) == CY_LPCOMP_INTR_RISING) || \
475 ((intr) == CY_LPCOMP_INTR_FALLING) || \
476 ((intr) == CY_LPCOMP_INTR_BOTH))
477 #define CY_LPCOMP_IS_POWER_VALID(power) (((power) == CY_LPCOMP_MODE_OFF) || \
478 ((power) == CY_LPCOMP_MODE_ULP) || \
479 ((power) == CY_LPCOMP_MODE_LP) || \
480 ((power) == CY_LPCOMP_MODE_NORMAL))
481 #define CY_LPCOMP_IS_INTR_VALID(intr) (((intr) == CY_LPCOMP_COMP0) || \
482 ((intr) == CY_LPCOMP_COMP1) || \
483 ((intr) == (CY_LPCOMP_COMP0 | CY_LPCOMP_COMP1)))
484 #define CY_LPCOMP_IS_INPUT_P_VALID(input) (((input) == CY_LPCOMP_SW_GPIO) || \
485 ((input) == CY_LPCOMP_SW_AMUXBUSA) || \
486 ((input) == CY_LPCOMP_SW_AMUXBUSB))
487 #define CY_LPCOMP_IS_INPUT_N_VALID(input) (((input) == CY_LPCOMP_SW_GPIO) || \
488 ((input) == CY_LPCOMP_SW_AMUXBUSA) || \
489 ((input) == CY_LPCOMP_SW_AMUXBUSB) || \
490 ((input) == CY_LPCOMP_SW_LOCAL_VREF))
491 #if defined (CY_IP_MXS22LPCOMP)
492 #define CY_LPCOMP_IS_TRIM_VALID(trim) (((((trim)->polarity) == CY_LPCOMP_TRIM_NEGATIVE) || \
493 (((trim)->polarity) == CY_LPCOMP_TRIM_POSITIVE)) && \
494 (((trim)->magnitude) <= CY_LPCOMP_TRIM_15mV))
495 #endif
496 /** \endcond */
497
498 /**
499 * \addtogroup group_lpcomp_functions
500 * \{
501 */
502
503 /******************************************************************************
504 * Functions
505 *******************************************************************************/
506
507 cy_en_lpcomp_status_t Cy_LPComp_Init_Ext(LPCOMP_Type *base, cy_en_lpcomp_channel_t channel, const cy_stc_lpcomp_config_t *config,
508 cy_stc_lpcomp_context_t *context);
509 void Cy_LPComp_Enable_Ext(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_stc_lpcomp_context_t *context);
510 void Cy_LPComp_Disable_Ext(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_stc_lpcomp_context_t *context);
511 #if defined (CY_IP_MXLPCOMP)
512 cy_en_lpcomp_status_t Cy_LPComp_Init(LPCOMP_Type *base, cy_en_lpcomp_channel_t channel, const cy_stc_lpcomp_config_t *config);
513 void Cy_LPComp_Enable(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel);
514 void Cy_LPComp_Disable(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel);
515 #endif
516 __STATIC_INLINE void Cy_LPComp_GlobalEnable(LPCOMP_Type *base);
517 __STATIC_INLINE void Cy_LPComp_GlobalDisable(LPCOMP_Type *base);
518 __STATIC_INLINE void Cy_LPComp_UlpReferenceEnable(LPCOMP_Type *base);
519 __STATIC_INLINE void Cy_LPComp_UlpReferenceDisable(LPCOMP_Type *base);
520 __STATIC_INLINE uint32_t Cy_LPComp_GetCompare(LPCOMP_Type const * base, cy_en_lpcomp_channel_t channel);
521 void Cy_LPComp_SetPower_Ext(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_pwr_t power,
522 cy_stc_lpcomp_context_t *context);
523 #if defined (CY_IP_MXLPCOMP)
524 void Cy_LPComp_SetPower(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_pwr_t power);
525 #endif
526 void Cy_LPComp_SetHysteresis(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_hyst_t hysteresis);
527 void Cy_LPComp_SetInputs(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_inputs_t inputP, cy_en_lpcomp_inputs_t inputN);
528 void Cy_LPComp_SetOutputMode(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_out_t outType);
529 void Cy_LPComp_SetInterruptTriggerMode_Ext(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_int_t intType,
530 cy_stc_lpcomp_context_t *context);
531 #if defined (CY_IP_MXLPCOMP)
532 void Cy_LPComp_SetInterruptTriggerMode(LPCOMP_Type* base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_int_t intType);
533 #endif
534 __STATIC_INLINE uint32_t Cy_LPComp_GetInterruptStatus(LPCOMP_Type const * base);
535 __STATIC_INLINE void Cy_LPComp_ClearInterrupt(LPCOMP_Type* base, uint32_t interrupt);
536 __STATIC_INLINE void Cy_LPComp_SetInterrupt(LPCOMP_Type* base, uint32_t interrupt);
537 __STATIC_INLINE uint32_t Cy_LPComp_GetInterruptMask(LPCOMP_Type const * base);
538 __STATIC_INLINE void Cy_LPComp_SetInterruptMask(LPCOMP_Type* base, uint32_t interrupt);
539 __STATIC_INLINE uint32_t Cy_LPComp_GetInterruptStatusMasked(LPCOMP_Type const * base);
540 __STATIC_INLINE void Cy_LPComp_ConnectULPReference(LPCOMP_Type *base, cy_en_lpcomp_channel_t channel);
541 #if defined (CY_IP_MXS22LPCOMP)
542 void Cy_LPComp_GetTrim(LPCOMP_Type const * base, cy_en_lpcomp_channel_t channel, cy_en_lpcomp_trim_t * trim);
543 void Cy_LPComp_SetTrim(LPCOMP_Type * base, cy_en_lpcomp_channel_t channel, const cy_en_lpcomp_trim_t * trim);
544 #endif
545 /** \addtogroup group_lpcomp_functions_syspm_callback
546 * The driver supports the SysPm callback for Deep Sleep and Hibernate transition.
547 * \{
548 */
549 cy_en_syspm_status_t Cy_LPComp_DeepSleepCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode);
550 cy_en_syspm_status_t Cy_LPComp_HibernateCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode);
551 /** \} */
552
553
554 /*******************************************************************************
555 * Function Name: Cy_LPComp_GlobalEnable
556 ****************************************************************************//**
557 *
558 * Activates the IP of the low-power comparator hardware block.
559 * Call this function before operating any comparator channel.
560 * \note Interrupts can be enabled after the block is enabled and appropriate
561 * start-up time has elapsed:
562 * * 3 us for normal power mode;
563 * * 6 us for LP mode;
564 * * 50 us for ULP mode.
565 *
566 * \param *base
567 * The low-power comparator register structure pointer.
568 *
569 * \return None.
570 *
571 *******************************************************************************/
Cy_LPComp_GlobalEnable(LPCOMP_Type * base)572 __STATIC_INLINE void Cy_LPComp_GlobalEnable(LPCOMP_Type* base)
573 {
574 LPCOMP_CONFIG(base) |= LPCOMP_CONFIG_ENABLED_Msk;
575 }
576
577
578 /*******************************************************************************
579 * Function Name: Cy_LPComp_GlobalDisable
580 ****************************************************************************//**
581 *
582 * Deactivates the IP of the low-power comparator hardware block.
583 * (Analog is in power down, open all switches, all clocks are off).
584 *
585 * \param *base
586 * The low-power comparator register structure pointer.
587 *
588 * \return None.
589 *
590 *******************************************************************************/
Cy_LPComp_GlobalDisable(LPCOMP_Type * base)591 __STATIC_INLINE void Cy_LPComp_GlobalDisable(LPCOMP_Type *base)
592 {
593 LPCOMP_CONFIG(base) &= (uint32_t) ~LPCOMP_CONFIG_ENABLED_Msk;
594 }
595
596
597 /*******************************************************************************
598 * Function Name: Cy_LPComp_UlpReferenceEnable
599 ****************************************************************************//**
600 *
601 * Enables the local reference-generator circuit.
602 *
603 * \param *base
604 * The low-power comparator register structure pointer.
605 *
606 * \return None.
607 *
608 *******************************************************************************/
Cy_LPComp_UlpReferenceEnable(LPCOMP_Type * base)609 __STATIC_INLINE void Cy_LPComp_UlpReferenceEnable(LPCOMP_Type *base)
610 {
611 LPCOMP_CONFIG(base) |= LPCOMP_CONFIG_LPREF_EN_Msk;
612 }
613
614
615 /*******************************************************************************
616 * Function Name: Cy_LPComp_UlpReferenceDisable
617 ****************************************************************************//**
618 *
619 * Disables the local reference-generator circuit.
620 *
621 * \param *base
622 * The low-power comparator register structure pointer.
623 *
624 * \return None.
625 *
626 *******************************************************************************/
Cy_LPComp_UlpReferenceDisable(LPCOMP_Type * base)627 __STATIC_INLINE void Cy_LPComp_UlpReferenceDisable(LPCOMP_Type *base)
628 {
629 LPCOMP_CONFIG(base) &= (uint32_t) ~LPCOMP_CONFIG_LPREF_EN_Msk;
630 }
631
632
633 /*******************************************************************************
634 * Function Name: Cy_LPComp_GetCompare
635 ****************************************************************************//**
636 *
637 * This function returns a non-zero value when the voltage connected to the
638 * positive input is greater than the negative input voltage.
639 *
640 * \param *base
641 * The low-power comparator register structure pointer.
642 *
643 * \param channel
644 * The low-power comparator channel index.
645 *
646 * \return The low-power comparator compare result.
647 * The value is a non-zero when the voltage connected to the positive
648 * input is greater than the negative input voltage.
649 *
650 *******************************************************************************/
Cy_LPComp_GetCompare(LPCOMP_Type const * base,cy_en_lpcomp_channel_t channel)651 __STATIC_INLINE uint32_t Cy_LPComp_GetCompare(LPCOMP_Type const * base, cy_en_lpcomp_channel_t channel)
652 {
653 uint32_t result;
654
655 CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
656
657 if (CY_LPCOMP_CHANNEL_0 == channel)
658 {
659 result = _FLD2VAL(LPCOMP_STATUS_OUT0, LPCOMP_STATUS(base));
660 }
661 else
662 {
663 result = _FLD2VAL(LPCOMP_STATUS_OUT1, LPCOMP_STATUS(base));
664 }
665
666 return (result);
667 }
668
669
670 /*******************************************************************************
671 * Function Name: Cy_LPComp_SetInterruptMask
672 ****************************************************************************//**
673 *
674 * Configures which bits of the interrupt request register will trigger an
675 * interrupt event.
676 *
677 * \param *base
678 * The low-power comparator register structure pointer.
679 *
680 * \param interrupt
681 * uint32_t interruptMask: Bit Mask of interrupts to set.
682 * * Bit 0: COMP0 interrupt mask
683 * * Bit 1: COMP1 interrupt mask
684 *
685 * \return None.
686 *
687 *******************************************************************************/
Cy_LPComp_SetInterruptMask(LPCOMP_Type * base,uint32_t interrupt)688 __STATIC_INLINE void Cy_LPComp_SetInterruptMask(LPCOMP_Type* base, uint32_t interrupt)
689 {
690 CY_ASSERT_L2(CY_LPCOMP_IS_INTR_VALID(interrupt));
691
692 LPCOMP_INTR_MASK(base) |= interrupt;
693 }
694
695
696 /*******************************************************************************
697 * Function Name: Cy_LPComp_GetInterruptMask
698 ****************************************************************************//**
699 *
700 * Returns an interrupt mask.
701 *
702 * \param *base
703 * The low-power comparator register structure pointer.
704 *
705 * \return bit mapping information:
706 * * Bit 0: COMP0 interrupt mask
707 * * Bit 1: COMP1 interrupt mask
708 *
709 *******************************************************************************/
Cy_LPComp_GetInterruptMask(LPCOMP_Type const * base)710 __STATIC_INLINE uint32_t Cy_LPComp_GetInterruptMask(LPCOMP_Type const * base)
711 {
712 return (LPCOMP_INTR_MASK(base));
713 }
714
715
716 /*******************************************************************************
717 * Function Name: Cy_LPComp_GetInterruptStatusMasked
718 ****************************************************************************//**
719 *
720 * Returns an interrupt request register masked by an interrupt mask.
721 * Returns the result of the bitwise AND operation between the corresponding
722 * interrupt request and mask bits.
723 *
724 * \param *base
725 * The low-power comparator register structure pointer.
726 *
727 * \return bit mapping information:
728 * * Bit 0: COMP0 interrupt masked
729 * * Bit 1: COMP1 interrupt masked
730 *
731 *******************************************************************************/
Cy_LPComp_GetInterruptStatusMasked(LPCOMP_Type const * base)732 __STATIC_INLINE uint32_t Cy_LPComp_GetInterruptStatusMasked(LPCOMP_Type const * base)
733 {
734 return (LPCOMP_INTR_MASKED(base));
735 }
736
737
738 /*******************************************************************************
739 * Function Name: Cy_LPComp_GetInterruptStatus
740 ****************************************************************************//**
741 *
742 * Returns the status of two different low-power comparator interrupt requests.
743 *
744 * \param *base
745 * The low-power comparator register structure pointer.
746 *
747 * \return bit mapping information:
748 * * Bit 0: COMP0 interrupt status
749 * * Bit 1: COMP1 interrupt status
750 *
751 *******************************************************************************/
Cy_LPComp_GetInterruptStatus(LPCOMP_Type const * base)752 __STATIC_INLINE uint32_t Cy_LPComp_GetInterruptStatus(LPCOMP_Type const * base)
753 {
754 return (_FLD2VAL(CY_LPCOMP_INTR, LPCOMP_INTR(base)));
755 }
756
757
758 /*******************************************************************************
759 * Function Name: Cy_LPComp_ClearInterrupt
760 ****************************************************************************//**
761 *
762 * Clears low-power comparator interrupts by setting each bit.
763 *
764 * \param *base
765 * The low-power comparator register structure pointer.
766 *
767 * \param interrupt
768 * * Bit 0: COMP0 interrupt status
769 * * Bit 1: COMP1 interrupt status
770 *
771 * \return None.
772 *
773 *******************************************************************************/
Cy_LPComp_ClearInterrupt(LPCOMP_Type * base,uint32_t interrupt)774 __STATIC_INLINE void Cy_LPComp_ClearInterrupt(LPCOMP_Type* base, uint32_t interrupt)
775 {
776 CY_ASSERT_L2(CY_LPCOMP_IS_INTR_VALID(interrupt));
777 LPCOMP_INTR(base) |= interrupt;
778 /* This dummy reading is necessary here. It provides a guarantee that interrupt is cleared at returning from this function. */
779 (void) LPCOMP_INTR(base);
780 }
781
782
783 /*******************************************************************************
784 * Function Name: Cy_LPComp_SetInterrupt
785 ****************************************************************************//**
786 *
787 * Sets a software interrupt request.
788 * This function is used in the case of combined interrupt signal from the global
789 * signal reference. This function from either component instance can be used
790 * to trigger either or both software interrupts. The function sets the INTR_SET interrupt mask.
791 *
792 * \param *base
793 * The low-power comparator register structure pointer.
794 *
795 * \param interrupt
796 * * Bit 0: COMP0 interrupt status
797 * * Bit 1: COMP1 interrupt status
798 *
799 * \return None.
800 *
801 *******************************************************************************/
Cy_LPComp_SetInterrupt(LPCOMP_Type * base,uint32_t interrupt)802 __STATIC_INLINE void Cy_LPComp_SetInterrupt(LPCOMP_Type* base, uint32_t interrupt)
803 {
804 CY_ASSERT_L2(CY_LPCOMP_IS_INTR_VALID(interrupt));
805 LPCOMP_INTR_SET(base) = interrupt;
806 }
807
808
809 /*******************************************************************************
810 * Function Name: Cy_LPComp_ConnectULPReference
811 ****************************************************************************//**
812 *
813 * Connects the local reference generator output to the comparator negative input.
814 *
815 * \param *base
816 * The low-power comparator register structure pointer.
817 *
818 * \param channel
819 * The low-power comparator channel index.
820 *
821 * \return None.
822 *
823 *******************************************************************************/
Cy_LPComp_ConnectULPReference(LPCOMP_Type * base,cy_en_lpcomp_channel_t channel)824 __STATIC_INLINE void Cy_LPComp_ConnectULPReference(LPCOMP_Type *base, cy_en_lpcomp_channel_t channel)
825 {
826 CY_ASSERT_L3(CY_LPCOMP_IS_CHANNEL_VALID(channel));
827
828 if (CY_LPCOMP_CHANNEL_0 == channel)
829 {
830 LPCOMP_CMP0_SW_CLEAR(base) = CY_LPCOMP_CMP0_SW_NEG_Msk;
831 LPCOMP_CMP0_SW(base) = _CLR_SET_FLD32U(LPCOMP_CMP0_SW(base), LPCOMP_CMP0_SW_CMP0_VN0, CY_LPCOMP_REF_CONNECTED);
832 }
833 else
834 {
835 LPCOMP_CMP1_SW_CLEAR(base) = CY_LPCOMP_CMP1_SW_NEG_Msk;
836 LPCOMP_CMP1_SW(base) = _CLR_SET_FLD32U(LPCOMP_CMP1_SW(base), LPCOMP_CMP1_SW_CMP1_VN1, CY_LPCOMP_REF_CONNECTED);
837 }
838 }
839
840 /** \} group_lpcomp_functions */
841
842 #ifdef __cplusplus
843 }
844 #endif
845
846 #endif /* CY_IP_MXLPCOMP, CY_IP_MXS22LPCOMP, and CY_IP_MXS40LPCOMP */
847
848 #endif /* CY_LPCOMP_PDL_H */
849
850 /** \} group_lpcomp */
851
852 /* [] END OF FILE */
853