I have found a bug for STM32L071xx series in using the comperators with vrefint in the actual Reference Manual V7.0 and the actual HAL STMCubeL0 V.1.7.0:
When selecting the the vrefint for the comperator minus input the comperator is not working as expected and triggers at random values.
In the Reference Manual is said (Page 325):
Note: If VREFINT or a fraction of VREFINT (using the scaler) is selected, then EN_VREFINT
bit must be set in the SYSCFG_CFGR3 register (see Section 9.2.3: Reference control
and status register (SYSCFG_CFGR3)).
But the EN_VREFINT is not set by the HAL in the function HAL_COMP_Init.
Further in the Reference Manual (Page 235):
Note: It is forbidden to configure both EN_VREFINT and ULP to 1 if the device is in Stop
mode or in Sleep/Low-power sleep mode (refer to Section 6.4.1: PWR power control
register (PWR_CR) for a description of the ULP bit). If the device is not in low-power
mode, VREFINT is always enabled whatever the state of EN_VREFINT and ULP.
This is not correct. In my test the VREFINT is not enabled for the comperator when the MCU is in RUN Mode.
As a workaround I enabled the the EN_VREFINT bit manually with:
Now the comperator is working as expected with VREFINT as minus input (only tested COMP2).
Additionally the bit 29 in SYSCFG_CFGR3 is set to 1. But don't now what it means because the bit name is "Res.".
Be aware that you reset the EN_VREFINT bit before entering stop mode otherwise the power consumption in stop mode is much higher. Setting the ULP bit in PWR_CR Register is not working for me. But it has to be set to get the a low power consumption in stop mode.
Please check this and confirm.