# LPComp (Low Power Comparator)¶

group group_lpcomp

Provides access to the low-power comparators implemented using the fixed-function LP comparator block that is present in PSoC 6.

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

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

Configuration Considerations

To set up an LPComp, the inputs, the output, the mode, the interrupts and other configuration parameters should be configured. Power the LPComp to operate.

The sequence recommended for the LPComp operation:

1) To initialize the driver, call the Cy_LPComp_Init() function providing the filled cy_stc_lpcomp_config_t structure, the LPComp channel number, and the LPCOMP 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. The High Impedance Analog drive mode is for the inputs and the Strong drive mode is for the output. Use the Cy_LPComp_SetInputs() function to connect the comparator inputs to the dedicated IO pins, AMUXBUSA/AMUXBUSB 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 LPComp 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 a 1 in the corresponding interrupt register bit position. The preferred way to clear interrupt sources is using the Cy_LPComp_ClearInterrupt() function.

note

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. For PSoC 6 devices, the individual comparator interrupt is masked by the INTR_MASK register. The masked result is captured in the INTR_MASKED register. Writing a 1 to the INTR register bit will clear the interrupt.

Low Power Support

The LPComp provides the callback functions to facilitate the low-power mode transition. The callback Cy_LPComp_DeepSleepCallback must be called during execution of Cy_SysPm_CpuEnterDeepSleep; Cy_LPComp_HibernateCallback must be called during execution of Cy_SysPm_SystemEnterHibernate. To trigger the callback execution, the callback must be registered before calling the mode transition function. Refer to SysPm (System Power Management) driver for more information about low-power mode transitions.

The example below shows the entering into Hibernate mode. The positive LPComp input connects to dedicated GPIO pin and the negative LPComp input connects to the local reference. The LED blinks twice after device reset and goes into Hibernate mode. When the voltage on the positive input great than the local reference voltage (0.45V - 0.75V) the device wakes up and LED begins blinking.

#define MYLPCOMP_HW      LPCOMP
#define MYLPCOMP_CHANNEL CY_LPCOMP_CHANNEL_0
#define LED_RED_PORT     GPIO_PRT0
#define LED_RED_PIN      3U

/* Configure the LPComp 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
};

int main(void)
{
if(Cy_SysPm_IoIsFrozen())
{
/* Restore the I/O configuration */
Cy_SysPm_IoUnfreeze();
}

/* Initialize LED to the strong drive mode */
Cy_GPIO_SetDrivemode(LED_RED_PORT, LED_RED_PIN, CY_GPIO_DM_STRONG_IN_OFF);

/* Initialize LPComp */
Cy_LPComp_SetInputs(MYLPCOMP_HW, MYLPCOMP_CHANNEL, CY_LPCOMP_SW_GPIO, CY_LPCOMP_SW_LOCAL_VREF);

/* Enable local reference for LPComp inputN */
(void)Cy_LPComp_Init(MYLPCOMP_HW, MYLPCOMP_CHANNEL, &MYLPCOMP_config);

/* Enable local reference for LPComp inputN */
Cy_LPComp_ConnectULPReference(MYLPCOMP_HW, MYLPCOMP_CHANNEL);
Cy_LPComp_UlpReferenceEnable(MYLPCOMP_HW);

/* Power on the comparator */
Cy_LPComp_SetPower(MYLPCOMP_HW, MYLPCOMP_CHANNEL, MYLPCOMP_config.power);

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 LPComp high output */
Cy_SysPm_SetHibernateWakeupSource(CY_SYSPM_HIBERNATE_LPCOMP0_HIGH);

/* Register a new syspm callback */
(void)Cy_SysPm_RegisterCallback(&LPCompHibernateCallback);

/* Enter into Hibernate mode */
Cy_SysPm_SystemEnterHibernate();

for (;;)
{

}
}