MTB CAT1 Peripheral driver library
LPComp (Low Power Comparator)

General Description

Provides access to low-power comparators implemented using the fixed-function, LP comparator block that is present in the CAT1A and CAT1D devices.

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_Ext() 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, AMUXBUSA/AMUXBUSB or Vref:

lpcomp_inputs.png

4) Power on the comparator using the Cy_LPComp_Enable_Ext() 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. For CAT1A, CAT1D devices, 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 =
{
.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
};
/* The comparator context structure */
static cy_stc_lpcomp_context_t lpcomp_context;

Section of application code:

/* The App code */
{
/* Restore the I/O configuration */
}
/* Initialize the low-power comparator */
(void)Cy_LPComp_Init_Ext(MYLPCOMP_HW, MYLPCOMP_CHANNEL, &MYLPCOMP_config, &lpcomp_context);
/* Enable the local reference for low-power comparator inputN */
Cy_LPComp_ConnectULPReference(MYLPCOMP_HW, MYLPCOMP_CHANNEL);
/* Initialize low-power comparator inputs */
/* Enable the low-power comparator */
Cy_LPComp_Enable_Ext(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 */
/* Register a new syspm callback */
(void)Cy_SysPm_RegisterCallback(&LPCompHibernateCallback);
/* Enter Hibernate mode */

More Information

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

Changelog

VersionChangesReason for Change
1.70 Added support for CAT1B and CAT1D devices. Support of new IP version.
1.60 Fix in hibernate callback function. The low power comparator was unable to wake-up the system from hibernate state.
Documentation update and clarification. The code snippet in chapter Low Power Support was updated after validation.
1.50 Minor improvement in implementation of disabling functionality for the comparator. Power drive mode for the comparator is restored to the level before disabling, instead of initial level.
Documented MISRA C-2012 violation of the Rule 10.3. MISRA C-2012 compliance.
1.40 Introduced an extended versions of the existing functions with a pointer to the context structure allocated by the user:
  • Improved returning of the comparator from disabled to operational state with restoring power drive and interrupt edge-detect modes, configured before disable.
  • Improved thread safe implementation of the PDL.
1.30 Fixed/documented MISRA 2012 violations. MISRA 2012 compliance.
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 de-referenced 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