PSoC 6 Peripheral Driver Library
LPComp (Low Power Comparator)

General Description

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:

lpcomp_inputs.png

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.
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 =
{
.hysteresis = CY_LPCOMP_HYST_ENABLE,
};
/* 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 =
{
0u,
&LPCompHibernateCallbackParams,
NULL,
NULL,
0
};
int main(void)
{
{
/* Restore the I/O configuration */
}
/* Initialize LED to the strong drive mode */
Cy_GPIO_SetDrivemode(LED_RED_PORT, LED_RED_PIN, CY_GPIO_DM_STRONG_IN_OFF);
/* Initialize LPComp */
/* 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);
/* Power on the comparator */
Cy_LPComp_SetPower(MYLPCOMP_HW, MYLPCOMP_CHANNEL, MYLPCOMP_config.power);
/* Blink 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 LPComp high output */
/* Register a new syspm callback */
(void)Cy_SysPm_RegisterCallback(&LPCompHibernateCallback);
/* Enter into Hibernate mode */
for (;;)
{
}
}

More Information

Refer to the appropriate device technical reference manual (TRM) for a detailed description of the registers.

MISRA-C Compliance

MISRA Rule Rule Class (Required/Advisory) Rule Description Description of Deviation(s)
11.4 A A cast should not be performed between a pointer to object type and a different pointer to object type. The pointer to the buffer memory is void to allow handling different different data types: uint8_t (4-8 bits) or uint16_t (9-16 bits). The cast operation is safe because the configuration is verified before operation is performed. The function Cy_LPComp_DeepSleepCallback is a callback of the cy_en_syspm_status_t type. The cast operation safety in this function becomes the user's responsibility because the pointers are initialized when a callback is registered in the SysPm driver.

Changelog

VersionChangesReason for Change
1.20.1 Minor documentation updates. Documentation enhancement.
1.20 Flattened the organization of the driver source code into the single source directory and the single include directory. Driver library directory-structure simplification.
Added register access layer. Use register access macros instead of direct register access using dereferenced pointers. Makes register access device-independent, so that the PDL does not need to be recompiled for each supported part number.
1.10.1 Added Low Power Callback section Documentation update and clarification
1.10 The CY_WEAK keyword is removed from Cy_LPComp_DeepSleepCallback() and Cy_LPComp_HibernateCallback() functions
Added input parameter validation to the API functions.
1.0 Initial version

API Reference

 Macros
 
 Functions
 
 Data Structures
 
 Enumerated Types