PSOC E8XXGP Device Support Library

| PSOC E8XXGP Device Support Library

API Reference
LPComp (Low Power Comparator)
PDL API Reference

## General Description

Provides access to low-power comparators implemented using the fixed-function, LP comparator block.

The functions and other declarations used in this driver are in the cy_lpcomp.h file. You can include the cy_pdl.h to get access to all functions and declarations in the PDL.

Such comparators can perform a fast analog signal comparison of internal and external analog signals in all system power modes. The low-power comparator output can be inspected by the CPU, used as an interrupt/wakeup source to the CPU in Low-power mode (Sleep, Low-power Sleep or Deep-Sleep), used as a wakeup source to system resources in Hibernate mode, or fed to DSI as an asynchronous or synchronous signal (level or pulse).

# Configuration Considerations

To set up a low-power comparator, the inputs, output, mode, interrupts and the other configuration parameters are configured. Power the low-power comparator to start it operate.

The sequence recommended for the low-power comparator operation:

1) To initialize the driver, call the Cy_LPComp_Init() function providing the filled cy_stc_lpcomp_config_t structure, the low-power comparator channel number and the low-power comparator registers structure pointer.

2) Optionally, configure the interrupt requests if the interrupt event triggering is needed. Use the Cy_LPComp_SetInterruptMask() function with the parameter for the mask available in the configuration file. Additionally, enable the Global interrupts and initialize the referenced interrupt by setting the priority and the interrupt vector using the Cy_SysInt_Init() function of the sysint driver.

3) Configure the inputs and the output using the Cy_GPIO_Pin_Init() functions of the GPIO driver. High Impedance Analog drive mode is for the inputs and Strong drive mode - for the output. Use the Cy_LPComp_SetInputs() function to connect the comparator inputs to the dedicated IO pins or Vref:

4) Power on the comparator using the Cy_LPComp_Enable() function.

5) The comparator output can be monitored using the Cy_LPComp_GetCompare() function or using the low-power comparator interrupt (if the interrupt is enabled).

Note
  The interrupt is not cleared automatically. It is the user's responsibility to do that. The interrupt is cleared by writing "1" in the corresponding interrupt register bit position. The preferred way to clear interrupt sources is using the Cy_LPComp_ClearInterrupt() function.
  Individual comparator interrupt outputs are ORed together as a single asynchronous interrupt source before it is sent out and used to wake up the system in the low-power mode. The individual comparator interrupt is masked by the INTR_MASK register. The masked result is captured in the INTR_MASKED register. Writing "1" to the INTR register bit will clear the interrupt.

# Low Power Support

The low-power comparator provides callback functions to facilitate the low-power mode transition. Call Cy_LPComp_DeepSleepCallback during execution of Cy_SysPm_CpuEnterDeepSleep. Call Cy_LPComp_HibernateCallback during execution of Cy_SysPm_SystemEnterHibernate. To trigger callback execution, register the callback before calling the mode transition function. Refer to SysPm (System Power Management) driver for more information about low-power mode transition.

The example below shows the entering Hibernate mode. The positive low-power comparator input connects to the dedicated GPIO pin and the negative low-power comparator input connects to the local reference. The LED blinks twice after a device reset and goes into Hibernate mode. When the voltage on the positive input is greater than the local reference voltage (0.45V - 0.75V), the device wakes up and the LED starts blinking.
Section of declarations:

/* Global declarations */
#define MYLPCOMP_HW LPCOMP
#define MYLPCOMP_CHANNEL CY_LPCOMP_CHANNEL_0
#define LED_RED_PORT GPIO_PRT0
#define LED_RED_PIN 3U
/* Configure a low-power comparator structure to operate in ULP mode */
const cy_stc_lpcomp_config_t MYLPCOMP_config =
{
.outputMode = CY_LPCOMP_OUT_DIRECT,
.hysteresis = CY_LPCOMP_HYST_ENABLE,
.power = CY_LPCOMP_MODE_ULP,
.intType = CY_LPCOMP_INTR_DISABLE,
};
/* Set the syspm callback parameters */
static cy_stc_syspm_callback_params_t LPCompHibernateCallbackParams =
{
MYLPCOMP_HW,
NULL
};
/* Configure the syspm callback structure */
static cy_stc_syspm_callback_t LPCompHibernateCallback =
{
&Cy_LPComp_HibernateCallback,
CY_SYSPM_HIBERNATE,
0u,
&LPCompHibernateCallbackParams,
NULL,
NULL,
0
};
/* The comparator context structure */
static cy_stc_lpcomp_context_t lpcomp_context;
cy_stc_lpcomp_config_t::outputMode
cy_en_lpcomp_out_t outputMode
The low-power comparator outputMode: Direct output, Synchronized output or Pulse output.
Definition: cy_lpcomp.h:331
cy_stc_lpcomp_config_t
The low-power comparator configuration structure.
Definition: cy_lpcomp.h:330
cy_stc_lpcomp_context_t
The low-power comparator context structure, used to improve thread-safe implementation of the PDL.
Definition: cy_lpcomp.h:347
CY_LPCOMP_MODE_ULP
@ CY_LPCOMP_MODE_ULP
The low-power comparator channel ULP mode.
Definition: cy_lpcomp.h:268
CY_LPCOMP_INTR_DISABLE
@ CY_LPCOMP_INTR_DISABLE
The low-power comparator interrupt is disabled, no interrupt will be detected.
Definition: cy_lpcomp.h:258
CY_LPCOMP_OUT_DIRECT
@ CY_LPCOMP_OUT_DIRECT
The low-power comparator Bypass mode, the direct output of a comparator.
Definition: cy_lpcomp.h:236
CY_LPCOMP_HYST_ENABLE
@ CY_LPCOMP_HYST_ENABLE
The low-power comparator enable hysteresis.
Definition: cy_lpcomp.h:244
Cy_LPComp_HibernateCallback
cy_en_syspm_status_t Cy_LPComp_HibernateCallback(cy_stc_syspm_callback_params_t *callbackParams, cy_en_syspm_callback_mode_t mode)
This function checks the current low-power comparator power mode and disables the low-power comparato...
Definition: cy_lpcomp.c:659
CY_SYSPM_HIBERNATE
@ CY_SYSPM_HIBERNATE
The Hibernate enum callback type.
Definition: cy_syspm.h:1520
cy_stc_syspm_callback_params_t
The structure contains syspm callback parameters.
Definition: cy_syspm.h:2054
cy_stc_syspm_callback_t
Structure with syspm callback configuration elements.
Definition: cy_syspm.h:2068

Section of application code:

/* The App code */
if(Cy_SysPm_IoIsFrozen())
{
/* Restore the I/O configuration */
Cy_SysPm_IoUnfreeze();
}
/* Initialize the low-power comparator */
(void)Cy_LPComp_Init(MYLPCOMP_HW, MYLPCOMP_CHANNEL, &MYLPCOMP_config, &lpcomp_context);
/* Enable the local reference for low-power comparator inputN */
Cy_LPComp_UlpReferenceEnable(MYLPCOMP_HW);
Cy_LPComp_ConnectULPReference(MYLPCOMP_HW, MYLPCOMP_CHANNEL);
/* Initialize low-power comparator inputs */
Cy_LPComp_SetInputs(MYLPCOMP_HW, MYLPCOMP_CHANNEL, CY_LPCOMP_SW_GPIO, CY_LPCOMP_SW_LOCAL_VREF);
/* Enable the low-power comparator */
Cy_LPComp_Enable(MYLPCOMP_HW, MYLPCOMP_CHANNEL, &lpcomp_context);
/* Initialize the LED to Strong drive mode */
Cy_GPIO_SetDrivemode(LED_RED_PORT, LED_RED_PIN, CY_GPIO_DM_STRONG_IN_OFF);
/* Blink the LED twice */
for (uint32 i = 1UL; i <= 4UL; i++)
{
Cy_GPIO_Inv(LED_RED_PORT, LED_RED_PIN);
Cy_SysLib_Delay(200/*msec*/);
}
/* Set the Hibernate wake-up source to the low-power comparator high output */
Cy_SysPm_SetHibernateWakeupSource(CY_SYSPM_HIBERNATE_LPCOMP0_HIGH);
/* Register a new syspm callback */
(void)Cy_SysPm_RegisterCallback(&LPCompHibernateCallback);
/* Enter Hibernate mode */
Cy_SysPm_SystemEnterHibernate();
CY_GPIO_DM_STRONG_IN_OFF
#define CY_GPIO_DM_STRONG_IN_OFF
Strong Drive.
Definition: cy_gpio.h:514
Cy_GPIO_Inv
__STATIC_FORCEINLINE void Cy_GPIO_Inv(GPIO_PRT_Type *base, uint32_t pinNum)
Set a pin output logic state to the inverse of the current output logic state.
Definition: cy_gpio.h:1251
Cy_GPIO_SetDrivemode
__STATIC_INLINE void Cy_GPIO_SetDrivemode(GPIO_PRT_Type *base, uint32_t pinNum, uint32_t value)
Configures the pin output buffer drive mode and input buffer enable.
Definition: cy_gpio.h:1286
CY_LPCOMP_SW_LOCAL_VREF
@ CY_LPCOMP_SW_LOCAL_VREF
The low-power comparator input connects to local VREF.
Definition: cy_lpcomp.h:279
CY_LPCOMP_SW_GPIO
@ CY_LPCOMP_SW_GPIO
The low-power comparator input connects to GPIO pin.
Definition: cy_lpcomp.h:278
Cy_LPComp_Init
cy_en_lpcomp_status_t Cy_LPComp_Init(LPCOMP_Type *base, cy_en_lpcomp_channel_t channel, const cy_stc_lpcomp_config_t *config, cy_stc_lpcomp_context_t *context)
This function initializes the low-power comparator and returns the status of the initialization.
Definition: cy_lpcomp.c:86
Cy_LPComp_SetInputs
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)
Sets the comparator input sources.
Definition: cy_lpcomp.c:433
Cy_LPComp_ConnectULPReference
__STATIC_INLINE void Cy_LPComp_ConnectULPReference(LPCOMP_Type *base, cy_en_lpcomp_channel_t channel)
Connects the local reference generator output to the comparator negative input.
Definition: cy_lpcomp.h:702
Cy_LPComp_UlpReferenceEnable
__STATIC_INLINE void Cy_LPComp_UlpReferenceEnable(LPCOMP_Type *base)
Enables the local reference generator circuit to generate the local Vref and Ibias.
Definition: cy_lpcomp.h:487
Cy_LPComp_Enable
void Cy_LPComp_Enable(LPCOMP_Type *base, cy_en_lpcomp_channel_t channel, cy_stc_lpcomp_context_t *context)
This function enables the low-power comparator and sets interrupt edge-detect and drive power modes.
Definition: cy_lpcomp.c:161
Cy_SysLib_Delay
void Cy_SysLib_Delay(uint32_t milliseconds)
The function delays by the specified number of milliseconds.
Definition: cy_syslib.c:104
CY_SYSPM_HIBERNATE_LPCOMP0_HIGH
@ CY_SYSPM_HIBERNATE_LPCOMP0_HIGH
Wake on a high logic level for the LPComp0.
Definition: cy_syspm.h:1385
Cy_SysPm_RegisterCallback
bool Cy_SysPm_RegisterCallback(cy_stc_syspm_callback_t *handler)
Registers a new syspm callback.
Definition: cy_syspm_v4.c:2063
Cy_SysPm_IoIsFrozen
__STATIC_INLINE bool Cy_SysPm_IoIsFrozen(void)
Checks whether IOs are frozen.
Definition: cy_syspm.h:4593
Cy_SysPm_IoUnfreeze
void Cy_SysPm_IoUnfreeze(void)
This function unfreezes the I/O cells that are automatically frozen when Hibernate is entered with th...
Definition: cy_syspm_v4.c:2293
Cy_SysPm_SetHibernateWakeupSource
void Cy_SysPm_SetHibernateWakeupSource(uint32_t wakeupSource)
This function configures sources to wake up the device from the system Hibernate power mode.
Definition: cy_syspm_v4.c:1732
Cy_SysPm_SystemEnterHibernate
cy_en_syspm_status_t Cy_SysPm_SystemEnterHibernate(void)
Sets the device into system Hibernate mode.
Definition: cy_syspm_v4.c:1508

# More Information

For a detailed description of the registers, refer to the appropriate device technical reference manual (TRM).

## API Reference
| Macros
| Functions
| Data Structures
| Enumerated Types