35. Comparator (COMP)

35.1 Introduction

The device embeds two ultra-low-power comparators, COMP1 and COMP2. It can be used for a variety of functions including:

35.2 COMP main features

35.3 COMP functional description

35.3.1 COMP block diagram

The block diagram of the comparators is shown in the figure below.

Figure 334. Comparator functional block diagram

Figure 334. Comparator functional block diagram showing COMP block with inputs COMPx_INP I/Os, dacx_out via INPSEL[1:0], and COMPx_INM I/Os, Vsense, dac1_out, dac2_out, VREFINT, 3/4 VREFINT, 1/2 VREFINT, 1/4 VREFINT via INMSEL[3:0]. HYST and PWRMODE control the comparator. Polarity selection, blanking logic with BLANKING[3:0] and compx_blk1...compx_blky, ITEN, DFF with CLR, CCxIF, and outputs COMPx_OUT, EXTI Interrupt (Asynchronous), timers, and compx_it Interrupt (Synchronous). MSv74530V2.
Figure 334. Comparator functional block diagram showing COMP block with inputs COMPx_INP I/Os, dacx_out via INPSEL[1:0], and COMPx_INM I/Os, Vsense, dac1_out, dac2_out, VREFINT, 3/4 VREFINT, 1/2 VREFINT, 1/4 VREFINT via INMSEL[3:0]. HYST and PWRMODE control the comparator. Polarity selection, blanking logic with BLANKING[3:0] and compx_blk1...compx_blky, ITEN, DFF with CLR, CCxIF, and outputs COMPx_OUT, EXTI Interrupt (Asynchronous), timers, and compx_it Interrupt (Synchronous). MSv74530V2.

Note: The Vsense buffer is controlled by ADC bloc, to activate it set the TSEN bit in the ADC_CCR register.

35.3.2 COMP pins and internal signals

The I/Os used as comparator inputs must be configured in analog mode in the GPIO registers.

The comparator outputs can be connected to the I/Os through their alternate functions. Refer to the product datasheet for more information.

The outputs can also be internally redirected to a variety of timer inputs for the following purposes:

The comparator output can be simultaneously addressed internally and to the I/O pins.

Table 329. COMP1/2 input/output internal signals

Signal nameSignal typeDescription
dacx_outAnalog inputAnalog signal from DAC peripheral
compx_blkyDigital inputSee Table 334: COMP1 blanking source assignment and Table 335: COMP2 blanking source assignment
Table 329. COMP1/2 input/output internal signals (continued)
Signal nameSignal typeDescription
compx_outDigital outputCOMPx output
compx_itDigital outputCOMPx interrupt out

Table 330. COMP1 non-inverting input assignment

COMP1_INPCOMP1_INPSEL[1:0]
INP0GPIO00
INP1GPIO01
INP2dac1_out110

Note: Other combinations are reserved.

Table 331. COMP1 inverting input assignment

COMP1_INMCOMP1_INMSEL[3:0]
INM0\(\frac{1}{4} V_{REFINT}\)0000
INM1\(\frac{1}{2} V_{REFINT}\)0001
INM2\(\frac{3}{4} V_{REFINT}\)0010
INM3\(V_{REFINT}\)0011
INM4dac1_out20100
INM5dac1_out10101
INM6GPIO0110
INM7GPIO0111
INM8\(V_{SENSE}\)1000
INM9\(V_{BAT}/4\)1001

Note: Other combinations are reserved.

Table 332. COMP2 non-inverting input assignment

COMP2_INPCOMP2_INPSEL[1:0]
INP0GPIO00
INP1GPIO01
INP2dac1_out210

Note: Other combinations are reserved.

Table 333. COMP2 inverting input assignment

COMP2_INMCOMP2_INMSEL[3:0]
INM0\(\frac{1}{4} V_{REFINT}\)0000
INM1\(\frac{1}{2} V_{REFINT}\)0001
INM2\(\frac{3}{4} V_{REFINT}\)0010
INM3\(V_{REFINT}\)0011
INM4dac1_out20100
Table 333. COMP2 inverting input assignment (continued)
COMP2_INMCOMP2_INMSEL[3:0]
INM5dac1_out10101
INM6VDDCORE0110
INM7COMP2_INM1 GPIO0111
INM8COMP2_INM2 GPIO1000
INM9COMP2_INM3 GPIO1001

Note: Other combinations are reserved.

Table 334. COMP1 blanking source assignment
Banking sourceCOMP1_BLANKING[3:0]
No blanking0000
tim1_oc50001
tim2_oc30010
tim3_oc30011
tim3_oc40100
lptim1_ch20101
lptim2_ch20110

Note: Other combinations are reserved.

Table 335. COMP2 blanking source assignment
Banking sourceCOMP2_BLANKING[3:0]
No blanking0000
tim1_oc60001
tim2_oc30010
tim3_oc30011
tim3_oc40100
tim8_oc50101
tim15_oc20110

Note: Other combinations are reserved.

35.3.3 COMP reset and clocks

The clock comp_pclk is provided by the clock controller is synchronous with the APB clock.

Note: Important: The polarity selection logic and the output redirection to the port work independently from the APB clock. This allows the comparator to work even in Stop mode. The interrupt line, connected to the NVIC of the CPU, requires the APB clock (comp_pclk) to work. In the absence of the APB clock, the interrupt signal comp_it cannot be generated.

35.3.4 Comparator LOCK mechanism

The comparators can be used for safety purposes, such as overcurrent or thermal protection. For applications with specific functional safety requirements, the comparator configuration can be protected against undesired alteration that could happen, for example, at program counter corruption.

For this purpose, the comparator configuration registers can be write-protected (read-only).

Upon configuring a comparator channel, its LOCK bit is set to 1. This causes the whole register set of the comparator channel to become read-only, the LOCK bit inclusive.

The write protection can only be removed through the MCU reset.

35.3.5 Window comparator

The purpose of the window comparator is to monitor the analog voltage if it is within the voltage range defined by the lower and upper threshold.

The two embedded comparators COMP1 and COMP2 can be used to create a window comparator. The monitored analog voltage is connected to the noninverting (plus) inputs of comparators connected together (COMP1 or COMP2). The upper and lower threshold voltages are connected to the inverting (minus) inputs of the comparators.

The output of the window comparator (COMP1 or COMP2) is selected with the control bit WINOUT.

Figure 335. Window comparators functional block diagram

Functional block diagram of window comparators showing COMP1 and COMP2 with inputs for INPSEL, INNSEL, VREFs, DACs, and outputs for comp1_out and comp2_out.

The diagram illustrates the functional block diagram for two comparators, COMP1 and COMP2, configured as a window comparator.
- COMP1 Path:

- COMP2 Path:- Window Mode Logic:- Reference: MSv74529V1

Functional block diagram of window comparators showing COMP1 and COMP2 with inputs for INPSEL, INNSEL, VREFs, DACs, and outputs for comp1_out and comp2_out.

35.3.6 Hysteresis

The comparator includes a programmable hysteresis to avoid spurious output transitions in the case of noisy signals. The hysteresis can be disabled if it is not needed (for instance when exiting from low-power mode) to be able to force the hysteresis value using external components.

Figure 336. Comparator hysteresis

Figure 336: Comparator hysteresis diagram. The top graph shows the input signal (INP) as a sine wave. Two horizontal dashed lines represent the upper threshold (INM) and the lower threshold (INM - V_hyst). The bottom graph shows the comparator output (COMP_OUT) as a square wave. The output transitions from low to high when the input crosses the upper threshold (INM) and from high to low when the input crosses the lower threshold (INM - V_hyst). The output remains high even if the input signal dips slightly below the INM threshold, demonstrating hysteresis. The diagram is labeled MS19984V1 in the bottom right corner.

Figure 336 illustrates the comparator hysteresis. The top graph shows the input signal (INP) as a sine wave. The bottom graph shows the comparator output (COMP_OUT) as a square wave. The output transitions from low to high when the input crosses the upper threshold (INM) and from high to low when the input crosses the lower threshold (INM - Vhyst). The output remains high even if the input signal dips slightly below the INM threshold, demonstrating hysteresis.

Figure 336: Comparator hysteresis diagram. The top graph shows the input signal (INP) as a sine wave. Two horizontal dashed lines represent the upper threshold (INM) and the lower threshold (INM - V_hyst). The bottom graph shows the comparator output (COMP_OUT) as a square wave. The output transitions from low to high when the input crosses the upper threshold (INM) and from high to low when the input crosses the lower threshold (INM - V_hyst). The output remains high even if the input signal dips slightly below the INM threshold, demonstrating hysteresis. The diagram is labeled MS19984V1 in the bottom right corner.

35.3.7 Comparator output blanking function

The purpose of the blanking function is to prevent the current regulation to trip upon short current spikes at the beginning of the PWM period (typically the recovery current in power switches antiparallel diodes). It uses a blanking window defined with a timer output compare signal. Refer to the register description for selectable blanking signals.

The blanking signal gates the internal comparator output such as to clean the comp_out from spurious pulses due to current spikes, as depicted in the following figure (the COMP channel number is not represented).

Figure 337. Comparator output blanking

Figure 337: Comparator output blanking diagram. The diagram shows five waveforms: PWM, Inverting input (represents current limit), Non-inverting input (represents current), cmp_out (before blanking gate), and cmp_blk (blanking window). The PWM signal is a square wave. The Inverting input is a constant high level. The Non-inverting input is a sawtooth wave that occasionally has sharp upward spikes labeled 'current spike'. The cmp_out (before blanking gate) shows pulses corresponding to when the non-inverting input crosses the inverting input, including a 'spurious pulse' caused by a current spike. The cmp_blk signal is a short high pulse that occurs during the PWM high period. The final comp_out (COMP output) is shown as a logic gate (AND gate with one inverted input) where the inputs are cmp_out and the inverse of cmp_blk. This gate suppresses the spurious pulse. A label 'Blanking gate' points to this gate. The final output is labeled 'comp_out (to I/Os, TIM_BK ...)'. The reference code 'MS30964V2' is in the bottom right corner.
Figure 337: Comparator output blanking diagram. The diagram shows five waveforms: PWM, Inverting input (represents current limit), Non-inverting input (represents current), cmp_out (before blanking gate), and cmp_blk (blanking window). The PWM signal is a square wave. The Inverting input is a constant high level. The Non-inverting input is a sawtooth wave that occasionally has sharp upward spikes labeled 'current spike'. The cmp_out (before blanking gate) shows pulses corresponding to when the non-inverting input crosses the inverting input, including a 'spurious pulse' caused by a current spike. The cmp_blk signal is a short high pulse that occurs during the PWM high period. The final comp_out (COMP output) is shown as a logic gate (AND gate with one inverted input) where the inputs are cmp_out and the inverse of cmp_blk. This gate suppresses the spurious pulse. A label 'Blanking gate' points to this gate. The final output is labeled 'comp_out (to I/Os, TIM_BK ...)'. The reference code 'MS30964V2' is in the bottom right corner.

35.3.8 COMP startup stabilization time

The comparator parameters and its output is undefined after the COMP enable (power-on) for a period defined as startup stabilization time, see the product datasheet for more details. If the comparator output is driving any critical stage of the design, it is recommended to redirect the comparator output to the device I/O through the GPIO alternate function or through IP configuration (such as timers) after the stabilization time elapses.

35.3.9 COMP power and speed modes

The power consumption of the COMP channels versus propagation delay can be adjusted to have the optimum trade-off for a given application.

The bits PWRMODE[1:0] in COMP_CFGRx registers can be programmed as follows:

35.4 COMP low-power modes

Table 336. Comparator behavior in low-power modes

ModeDescription
SleepNo effect on the comparators. Comparator interrupts cause the device to exit the Sleep mode.
StopNo effect on the comparators. Comparator interrupts cause the device to exit the Stop mode.

Note: The comparators cannot be used to exit the device from Sleep or Stop mode when the internal reference voltage is switched off.

35.5 COMP interrupts

There are two ways to use the comparator as an interrupt source.

The comparator outputs are internally connected to the extended interrupt and event controller (EXTI). Each comparator has its own EXTI line and can generate either interrupts or events to make the device exit low-power modes.

The comparator also provides an interrupt line to the NVIC of the CPU. This functionality is used when the CPU is active to handle low latency interrupt. It requires the APB clock running.

35.5.1 Wake-up interrupt through EXTI

Refer to the interrupt and events section for more details.

Sequence to enable the COMPx interrupt through the EXTI block:

  1. 1. Enable the COMPx and wait for stabilization.
  2. 2. Configure the EXTI line, receiving the compx_wkup signal, in interrupt mode, select the rising, falling, or either-edge sensitivity and enable the EXTI line.
  3. 3. Configure and enable the NVIC IRQ channel mapped to the corresponding EXTI lines.

Table 337. Interrupt through EXTI control bits

Interrupt eventEvent flagEnable control bitExit Sleep modeExit Stop modesExit Standby mode
compx_wkupThrough EXTIThrough EXTIYesYesN/A

35.5.2 Interrupt through EXTI

Sequence to enable the COMPx interrupt through the EXTI block:

  1. 1. Enable the COMP and wait for stabilization.
  2. 2. Configure the EXTI line, receiving the comp_it signal, in interrupt mode, select the rising, falling, or either-edge sensitivity and enable the EXTI line.
  3. 3. Configure and enable the ITEN in COMP_CFGRx.

Table 338. Interrupt through NVIC control bits

Interrupt eventInterrupt flagEnable control bitInterrupt clear bitExit Sleep modeExit Stop modes
compx_itCxIF in COMP_SRITEN in COMP_CFGRxCC1xFYes (With APB clock)No

Note: It is mandatory to enable the APB clock to use this interrupt. If the clock is not enabled, an interrupt is not generated.

35.5.3 Interrupt through the NVIC of the CPU

Sequence to enable the COMP interrupt through the NVIC of the CPU:

  1. 1. Enable the COMP and wait for stabilization.
  2. 2. Configure and enable the NVIC IRQ channel mapped to the comp_it line.
  3. 3. Configure and enable the ITEN in COMP_CFGRx.

Table 339. Interrupt through NVIC control bits

Interrupt eventInterrupt flagEnable control bitInterrupt clear bitExit Sleep modeExit Stop modes
comp_itC1IF inITEN in COMP_CFGR1CC1IFYes (with APB clock)No
comp_itC2IF inITEN in COMP_CFGR2CC2IFYes (with APB clock)No

Note: It is mandatory to enable the APB clock to use this interrupt. If the clock is not enabled, an interrupt is not generated.
The interrupt is active on level and not automatically clear by hardware. It must be cleared in the comparator interrupt handler by firmware with ITEN=0 (COMP_CFGR1 register).
The interrupt signal comp_it is generated on comparator level, for this reason: upon interruption occurrence, the comp_it signal must be set to low as soon as possible to avoid comparator interrupt handler reentrance. This can be done by setting ITEN=0 or changing the comparator POLARITY bit.

35.6 SCALER function

The scaler block is available to provide the different voltage reference levels to the comparator inputs. It is based on an amplifier driving a resistor bridge. The amplifier input is connected to the internal voltage reference.

The amplifier and the resistor bridge can be enabled separately. The amplifier is enabled by the SCALEN bits of the COMP_CFGRx registers. The resistor bridge is enabled by the BRGEN bits of the COMP_CFGRx registers.

When the resistor-divided voltage is not used, the resistor bridge can be disconnected to reduce the consumption.

Figure 338. Scaler block diagram

Figure 338. Scaler block diagram. The diagram shows a dashed box containing an operational amplifier and a resistor ladder. The non-inverting input (+) of the op-amp is connected to VREFINT. The inverting input (-) is connected to a node between the op-amp output and a resistor ladder. The op-amp output is also connected to VREFINT. The resistor ladder consists of four resistors in series. The nodes between the resistors are labeled 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT. The bottom of the ladder is connected to ground through a switch controlled by the BRGEN signal. The SCALEN signal is connected to the non-inverting input of the op-amp.

MSv74531V1

Figure 338. Scaler block diagram. The diagram shows a dashed box containing an operational amplifier and a resistor ladder. The non-inverting input (+) of the op-amp is connected to VREFINT. The inverting input (-) is connected to a node between the op-amp output and a resistor ladder. The op-amp output is also connected to VREFINT. The resistor ladder consists of four resistors in series. The nodes between the resistors are labeled 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT. The bottom of the ladder is connected to ground through a switch controlled by the BRGEN signal. The SCALEN signal is connected to the non-inverting input of the op-amp.

35.7 COMP registers

35.7.1 Comparator status register (COMP_SR)

The COMP_SR is the comparator status register.

Address offset: 0x00

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.C2IFC1IF
rr
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.C2VALC1VAL
rr

Bits 31:18 Reserved, must be kept at reset value.

Bit 17 C2IF: COMP2 Channel interrupt flag

This bit is set by hardware when the COMP2 Channel output is set

This bit is cleared by software writing 1 the CC2IF bit in the COMP_ICFR register.

Bit 16 C1IF: COMP1 Channel interrupt flag

This bit is set by hardware when the COMP1 Channel output is set

This bit is cleared by software writing 1 the CC1IF bit in the COMP_ICFR register.

Bits 15:2 Reserved, must be kept at reset value.

Bit 1 C2VAL: COMP2 Channel output status bit

This bit is read-only. It reflects the current COMP2 Channel output taking into account POLARITY and BLANKING bits effect.

Bit 0 C1VAL: COMP1 Channel output status bit

This bit is read-only. It reflects the current COMP1 Channel output taking into account POLARITY and BLANKING bits effect.

35.7.2 Comparator interrupt clear flag register (COMP_ICFR)

The COMP_ICFR is the comparator interrupt clear flag register.

Address offset: 0x04

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.CC2IFCC1IF
rc_w1rc_w1
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.

Bits 31:18 Reserved, must be kept at reset value.

Bit 17 CC2IF: Clear COMP2 Channel interrupt flag

Writing 1 clears the C2IF flag in the COMP_SR register.

Bit 16 CC1IF: Clear COMP1 Channel interrupt flag

Writing 1 clears the C1IF flag in the COMP_SR register.

Bits 15:0 Reserved, must be kept at reset value.

35.7.3 Comparator configuration register 1 (COMP_CFGR1)

The COMP_CFGR1 is the COMP1 configuration register.

Address offset: 0x0C

Reset value: 0x0000 0000

31302928272625242322212019181716
LOCKRes.Res.Res.BLANKING[3:0]Res.INP SEL1Res.INP SEL0INMSEL[3:0]
rwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
Res.WIN OUTPWRMODE[1:0]Res.Res.HYST[1:0]Res.ITENRes.WIN MODEPOLARITYSCALENBRGENEN
rwrwrwrwrwrwrwrwrwrwrw

Bit 31 LOCK: Lock

This bit is set by software and cleared by a hardware system reset. It locks the whole content of the COMP Channel configuration register COMP_CFGR1[31:0].

0: COMP_CFGR1[31:0] register is read/write

1: COMP_CFGR1[31:0] register is read-only

Bit 5 Reserved, must be kept at reset value.

Bit 4 WINMODE: COMP1 non-inverting input selector for window mode
This bit is controlled by software (if not locked). It selects the signal for COMP1_INP input of the COMP1.
0: Signal selected with INPSEL[1:0]
1: COMP2_INP signal of COMP2 (required for window mode, see Figure 335: Window comparators functional block diagram)

Bit 3 POLARITY: COMP Channel polarity selection
This bit is set and cleared by software (only if LOCK not set). It inverts COMP Channel polarity.
0: COMP Channel output is not inverted
1: COMP Channel output is inverted

Bit 2 SCALEN: Voltage scaler enable
This bit is set and cleared by software (only if LOCK not set). This bit enables the VREFINT scaler for the COMP channels.
0: VREFINT scaler disabled
1: VREFINT scaler enabled

Bit 1 BRGEN: Scaler bridge enable
This bit is set and cleared by software (only if LOCK not set). This bit enables the bridge of the scaler.
0: Scaler resistor bridge disabled
1: Scaler resistor bridge enabled

Bit 0 EN: COMP Channel enable
This bit is set and cleared by software (only if LOCK not set). It enables the COMP Channel.
0: Disable
1: Enable

35.7.4 Comparator configuration register 2 (COMP_CFGR2)

The COMP_CFGR2 is the COMP2 configuration register.

Address offset: 0x10

Reset value: 0x0000 0000

31302928272625242322212019181716
LOCKRes.Res.Res.BLANKING[3:0]Res.INPSEL1Res.INPSEL0INMSEL[3:0]
rwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
Res.WIN OUTPWRMODE[1:0]Res.Res.HYST[1:0]Res.ITENRes.WIN MODEPOLARITYSCALE NBRGENEN
rwrwrwrwrwrwrwrwrwrwrw

Bit 31 LOCK: Lock
This bit is set by software and cleared by a hardware system reset. It locks the whole content of the COMP Channel configuration register COMP_CFGR2[31:0].
0: COMP_CFGR2[31:0] register is read/write
1: COMP_CFGR2[31:0] register is read-only

Bits 30:28 Reserved, must be kept at reset value.

35.7.5 COMP register map

Table 340. COMP register map and reset values

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x00COMP_SRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.C2IFC1IFRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.C2VALC1VAL
Reset value0000
0x04COMP_ICFRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.C2IFC1IFRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
Reset value00
0x0CCOMP_CFGR1LOCKRes.Res.Res.BLANKING [3:0]Res..INPSEL1Res.INPSEL0INMSEL [3:0]Res.WINOUTPWRMODE [1:0]WINMODERes.HYST[1:0]Res.Res.ITENRes.Res.POLARITYSCALENBRGENEN
Reset value00000000000000000000000
0x10COMP_CFGR2LOCKRes.Res.Res.BLANKING [3:0]Res..INPSEL1Res.INPSEL0INMSEL [3:0]Res.WINOUTPWRMODE [1:0]WINMODERes.HYST[1:0]Res.Res.ITENRes.Res.POLARITYSCALENBRGENEN
Reset value0000000000000000000000

Refer to Section 2.3 for the register boundary addresses.