21. Comparator (COMP)

21.1 COMP introduction

The device embeds two ultra-low-power comparators (COMP1 and COMP2).

These comparators can be used for a variety of functions including the following:

21.2 COMP main features

21.3 COMP functional description

21.3.1 COMP block diagram

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

Figure 94. Comparator block diagram

Figure 94: Comparator block diagram showing input multiplexers for non-inverting and inverting inputs, the COMPx comparator core, and an output polarity selection multiplexer leading to COMPx_VALUE, timers, and GPIO alternate functions.

The diagram illustrates the internal architecture of a comparator (COMP x ). On the left, the non-inverting input (COMP x _INP) is selected via a multiplexer (COMP x _INPSEL) from COMP x _INP I/Os. The inverting input (COMP x _INM) is selected via another multiplexer (COMP x _INMSEL) from sources including COMP x _INM I/Os, DAC_CH1, V REFINT , 3/4 V REFINT , 1/2 V REFINT , and 1/4 V REFINT . These inputs are connected to the COMP x comparator block. The output of the comparator is connected to a third multiplexer controlled by COMP x _POL for polarity selection. The output of this multiplexer is labeled COMP x _VALUE. This signal is connected to a 'Wakeup EXTI line interrupt' and 'Timers'. Additionally, the COMP x _VALUE signal is connected to a 'GPIO alternate function' block, which produces the COMP x _OUT signal.

Figure 94: Comparator block diagram showing input multiplexers for non-inverting and inverting inputs, the COMPx comparator core, and an output polarity selection multiplexer leading to COMPx_VALUE, timers, and GPIO alternate functions.

21.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).

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

The comparator output can be simultaneously redirected internally and externally.

Table 122. COMP1 input plus assignment

COMP1_INPCOMP1_INPSEL
PB400
PB201

Table 123. COMP1 input minus assignment

COMP1_INMCOMP1_INMSEL[2:0]COMP1_INMESEL[1:0]
1/4 V REFINT000Not affected
1/2 V REFINT001Not affected
3/4 V REFINT010Not affected
V REFINT011Not affected
DAC channel1100Not affected
Reserved101Not affected
PB3110Not affected
PA1011100
PA1111101
PA1511110
Reserved11111

Table 124. COMP2 input plus assignment

COMP2_INPCOMP2_INPSEL
PB400
PB101
PA1510

Table 125. COMP2 input minus assignment

COMP2_INMCOMP2_INMSEL[2:0]COMP2_INMESEL[1:0]
1/4 V REFINT000Not affected
1/2 V REFINT001Not affected
3/4 V REFINT010Not affected
V REFINT011Not affected
DAC channel1100Not affected
Reserved101Not affected
PB3110Not affected
PB211100
PA1011101
PA1111110
Reserved11111

21.3.3 COMP reset and clocks

The COMP clock provided by the clock controller is synchronous with the APB2 clock.

There is no clock enable control bit provided in the RCC controller. Reset and clock enable bits are common for COMP and SYSCFG.

Note: Important: The polarity selection logic and the output redirection to the port works independently from the APB2 clock. This allows the comparator to work even in Stop mode.

21.3.4 Comparator LOCK mechanism

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

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

Once the programming is completed, the COMPx LOCK bit can be set to 1. This causes the whole register to become read-only, including the COMPx LOCK bit.

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

21.3.5 Window comparator

The purpose of the window comparator is to monitor the analog voltage and check that it is comprised within the specified voltage range defined by lower and upper thresholds.

COMP1 and COMP2 can be utilized to create window comparator. The monitored analog voltage is connected to the non-inverting (plus) inputs of comparators connected together, and the upper and lower threshold voltages are connected to the inverting (minus) inputs of the comparators. The two non-inverting inputs can be connected internally together by enabling the WINMODE bit to save one I/O for other purposes.

Figure 95. Window mode

Schematic diagram of window mode for two comparators, COMPx and COMPy. Each comparator has multiple input sources (I/Os and internal sources) connected through multiplexers (INPSEL and INMSEL). The non-inverting input (INP) and inverting input (INM) are connected to the comparator. A WINMODE signal is connected to the non-inverting input of COMPy. The diagram is labeled MSV37667V1.

The diagram illustrates the internal architecture for window mode. It features two comparators, COMPx and COMPy. Each comparator has its non-inverting input (INP) and inverting input (INM) connected to a multiplexer. The multiplexers allow selection between external I/Os and internal sources. For COMPx, the inputs are labeled COMPx_INP I/Os, COMPx_INM I/Os, and Internal sources, controlled by COMPx_INPSEL and COMPx_INMSEL. For COMPy, the inputs are labeled COMPy_INP I/Os, COMPy_INM I/Os, and Internal sources, controlled by COMPy_INPSEL and COMPy_INMSEL. A WINMODE signal is connected to the non-inverting input of COMPy. The diagram is labeled MSV37667V1.

Schematic diagram of window mode for two comparators, COMPx and COMPy. Each comparator has multiple input sources (I/Os and internal sources) connected through multiplexers (INPSEL and INMSEL). The non-inverting input (INP) and inverting input (INM) are connected to the comparator. A WINMODE signal is connected to the non-inverting input of COMPy. The diagram is labeled MSV37667V1.

21.3.6 Hysteresis

The comparator includes a programmable hysteresis to avoid spurious output transitions in 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 96. Comparator hysteresis

Timing diagram showing hysteresis. The top graph plots INP and INM signals over time. The bottom graph plots the COMP_OUT signal. The INM signal is a dashed line, and the INP signal is a solid line. The hysteresis is indicated by the difference between the INM and INM - V_hyst lines. The diagram is labeled MS19984V1.

The diagram shows the effect of hysteresis on the comparator output. The top graph plots the non-inverting input (INP) and inverting input (INM) signals over time. The INM signal is a dashed line, and the INP signal is a solid line. The hysteresis is indicated by the difference between the INM and INM - V hyst lines. The bottom graph plots the COMP_OUT signal, which is a digital signal that transitions between high and low states based on the input signals. The diagram is labeled MS19984V1.

Timing diagram showing hysteresis. The top graph plots INP and INM signals over time. The bottom graph plots the COMP_OUT signal. The INM signal is a dashed line, and the INP signal is a solid line. The hysteresis is indicated by the difference between the INM and INM - V_hyst lines. The diagram is labeled MS19984V1.

21.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 anti parallel 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 figure below.

Figure 97. Comparator output blanking

Timing diagram illustrating the comparator output blanking function. The diagram shows five waveforms: PWM, Inverting input (represents current limit), Non-inverting input (represents current), cmp_out (before blanking gate), and comp_out (COMP output). The PWM signal is a periodic square wave. The Inverting input is a sawtooth-like waveform representing the current limit. The Non-inverting input is a sawtooth-like waveform representing the current. The cmp_out signal shows a 'current spike' and a 'spurious pulse' that occur when the current exceeds the limit. The cmp_blk signal is a rectangular pulse representing the blanking window. The comp_out signal is the output of a blanking gate, which is the AND of cmp_out and the inverted cmp_blk signal. The diagram shows that the spurious pulse is suppressed by the blanking function. A logic diagram at the bottom shows the blanking gate with inputs cmp_out and cmp_blk (inverted) and output comp_out (to I/Os, TIM_BK ...).

The figure is a timing diagram showing the relationship between several signals over time. At the top is the PWM signal, a periodic square wave. Below it is the Inverting input (represents current limit) , which shows a sawtooth-like waveform. Below that is the Non-inverting input (represents current) , which also shows a sawtooth-like waveform. The cmp_out (before blanking gate) signal shows a 'current spike' and a 'spurious pulse' that occur when the current exceeds the limit. The cmp_blk (blanking window) signal is a rectangular pulse. The comp_out (COMP output) signal is the output of a blanking gate, which is the AND of cmp_out and the inverted cmp_blk signal. The diagram shows that the spurious pulse is suppressed by the blanking function. At the bottom, a logic diagram shows the blanking gate with inputs cmp_out and cmp_blk (inverted) and output comp_out (to I/Os, TIM_BK ...). The diagram is labeled MS30964V2.

Timing diagram illustrating the comparator output blanking function. The diagram shows five waveforms: PWM, Inverting input (represents current limit), Non-inverting input (represents current), cmp_out (before blanking gate), and comp_out (COMP output). The PWM signal is a periodic square wave. The Inverting input is a sawtooth-like waveform representing the current limit. The Non-inverting input is a sawtooth-like waveform representing the current. The cmp_out signal shows a 'current spike' and a 'spurious pulse' that occur when the current exceeds the limit. The cmp_blk signal is a rectangular pulse representing the blanking window. The comp_out signal is the output of a blanking gate, which is the AND of cmp_out and the inverted cmp_blk signal. The diagram shows that the spurious pulse is suppressed by the blanking function. A logic diagram at the bottom shows the blanking gate with inputs cmp_out and cmp_blk (inverted) and output comp_out (to I/Os, TIM_BK ...).

21.3.8 COMP power and speed modes

COMP1 and COMP2 power consumption versus propagation delay can be adjusted to have the optimum trade-off for a given application.

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

21.4 COMP low-power modes

Table 126. Comparator behavior in the low-power modes

ModeDescription
SleepNo effect on the comparators
Comparator interrupts cause the device to exit the Sleep mode.
LPRunNo effect
LPSleepNo effect on the comparators
Comparator interrupts cause the device to exit the LPSleep mode.
Stop 0No effect on the comparators
Comparator interrupts cause the device to exit the Stop mode.
Stop 1
Stop 2
StandbyCOMP registers are powered down and must be reinitialized after exiting Standby or Shutdown mode.
Shutdown

21.5 COMP interrupts

The comparator outputs are internally connected to the extended interrupts and events controller (EXTI). Each comparator has its own EXTI line and can generate either interrupts or events. The same mechanism is used to exit from low-power modes.

Refer to the “Interrupt and events” section for more details.

The following sequence enables the COMPx interrupt through EXTI block:

  1. 1. Configure and enable the EXTI line corresponding to the COMPx output event in interrupt mode and select the rising, falling or both edges sensitivity.
  2. 2. Configure and enable the NVIC IRQ channel mapped to the corresponding EXTI lines.
  3. 3. Enable the COMPx.

Table 127. Interrupt control bits

Interrupt eventEvent flagEnable control bitExit from Sleep modeExit from Stop modesExit from Standby mode
COMP1 outputVALUE in COMP1_CSRThrough EXTIYesYesNot applicable
COMP2 outputVALUE in COMP2_CSRThrough EXTIYesYesNot applicable

21.6 COMP registers

21.6.1 COMP1 control and status register (COMP1_CSR)

Address offset: 0x00

Reset value: 0x0000 0000

31302928272625242322212019181716
LOCKVALUERes.Res.Res.INMESEL[1:0]Res.SCALENBRGENRes.BLANKING[2:0]HYST[1:0]
rsrrwrwrwrwrwrwrwrwrw

1514131211109876543210
POLARITYRes.Res.Res.Res.Res.Res.INPSEL[1:0]INMSEL[2:0]PWRMODE[1:0]Res.EN
rwrwrwrwrwrwrwrwrw

Bit 31 LOCK : locks the whole content of the register, COMP1_CSR[31:0]

This bit is set by software and cleared by a hardware system reset.

0: COMP1_CSR[31:0] are read/write.

1: COMP1_CSR[31:0] are read-only.

Bit 30 VALUE : COMP1 output status bit

This bit is read-only. It reflects the current COMP1 output taking into account the effect of the POLARITY bit.

Bits 29:27 Reserved, must be kept at reset value.

Bits 26:25 INMESEL[1:0] : COMP1 input minus extended selection

These bits are set and cleared by software. They select which extended GPIO input is connected to the input minus of COMP1, if INMSEL[2:0] = 111.

00: PA10

01: PA11

10: PA15

11: reserved

Bit 24 Reserved, must be kept at reset value.

Bit 23 SCALEN : voltage scaler enable

This bit is set and cleared by software. It enables outputs of the V REFINT divider available on the minus input of COMP1.

0: Bandgap scaler disabled (if SCALEN bit of COMP2_CSR register is also reset)

1: Bandgap scaler enabled

Bit 22 BRGEN : scaler bridge enable

This bit is set and cleared by software. It enables the bridge of the scaler.

If SCALEN is set and BRGEN is reset, the BG voltage reference is available but not 1/4 BGAP, 1/2 BGAP or 3/4 BGAP. The BGAP value is sent instead of 1/4 BGAP, 1/2 BGAP, 3/4 BGAP. If SCALEN and BRGEN are both set, 1/4 BGAP 1/2 BGAP 3/4 BGAP and BGAP voltage references are available.

0: Scaler resistor bridge disabled (if BRGEN bit of COMP2_CSR register is also reset)

1: Scaler resistor bridge enabled

Bit 21 Reserved, must be kept at reset value.

Bits 20:18 BLANKING[2:0] : COMP1 blanking source selection

These bits select which timer output controls the COMP1 output blanking.

000: No blanking

001: TIM1 OC5 selected as blanking source

010: TIM2 OC3 selected as blanking source

Others: reserved

Bits 17:16 HYST[1:0] : COMP1 hysteresis selection

These bits are set and cleared by software. They select the COMP1 hysteresis voltage.

00: No hysteresis

01: Low hysteresis

10: Medium hysteresis

11: High hysteresis

Bit 15 POLARITY : COMP1 polarity selection

This bit is set and cleared by software. It inverts COMP1 polarity.

0: COMP1 output value not inverted

1: COMP1 output value inverted

Bits 14:9 Reserved, must be kept at reset value.

Bits 8:7 INPSEL[1:0] : COMP1 input plus selection

These bits are set and cleared by software.

00: PB4

01: PB2

10: reserved

11: reserved

Bits 6:4 INMSEL[2:0] : COMP1 input minus selection

These bits are set and cleared by software. They select which input is connected to the input minus of COMP1.

000: 1/4 V REFINT

001: 1/2 V REFINT

010: 3/4 V REFINT

011: V REFINT

100: DAC channel1

101: reserved

110: PB3

111: GPIOx selected by INMESEL[1:0] bits

Bits 3:2 PWRMODE[1:0] : COMP1 power mode

These bits are set and cleared by software. They control the power and speed of COMP1.

00: High speed

01: Medium speed

10: Medium speed

11: Ultra low-power

Bit 1 Reserved, must be kept at reset value.

Bit 0 EN : COMP1 enable

This bit is set and cleared by software. It switches COMP1 on.

0: COMP1 switched off

1: COMP1 switched on

21.6.2 COMP2 control and status register (COMP2_CSR)

Address offset: 0x04

Reset value: 0x0000 0000

31302928272625242322212019181716
LOCKVALUERes.Res.Res.INMESEL[1:0]Res.SCALENBRGENRes.BLANKING[2:0]HYST[1:0]
rsrrwrwrwrwrwrwrwrwrw

1514131211109876543210
POLARITYRes.Res.Res.Res.Res.WINMODEINPSEL[1:0]INMSEL[2:0]PWRMODE[1:0]Res.EN
rwrwrwrwrwrwrwrwrwrw

Bit 31 LOCK : locks the whole content of the register, COMP2_CSR[31:0]

This bit is set by software and cleared by a hardware system reset.

0: COMP2_CSR[31:0] are read/write.

1: COMP2_CSR[31:0] are read-only.

Bit 30 VALUE : COMP2 output status bit

This bit is read-only. It reflects the current COMP2 output taking into account the effect of the POLARITY bit.

Bits 29:27 Reserved, must be kept at reset value.

Bits 26:25 INMESEL[1:0] : COMP2 input minus extended selection

These bits are set and cleared by software. They select which extended GPIO input is connected to the input minus of COMP2, if INMSEL[2:0] = 111.

00: PB2

01: PA10

10: PA11

11: reserved

Bit 24 Reserved, must be kept at reset value.

Bit 23 SCALEN : voltage scaler enable

This bit is set and cleared by software. It enables outputs of the V REFINT divider available on the minus input of COMP2.

0: Bandgap scaler disabled (if SCALEN bit of COMP1_CSR register is also reset)

1: Bandgap scaler enabled

Bit 22 BRGEN : scaler bridge enable

This bit is set and cleared by software. It enables the bridge of the scaler.

If SCALEN is set and BRGEN is reset, the BG voltage reference is available but not 1/4 BGAP, 1/2 BGAP or 3/4 BGAP. The BGAP value is sent instead of 1/4 BGAP, 1/2 BGAP, 3/4 BGAP. If SCALEN and BRGEN are both set, 1/4 BGAP 1/2 BGAP 3/4 BGAP and BGAP voltage references are available.

0: Scaler resistor bridge disabled (if BRGEN bit of COMP1_CSR register is also reset)

1: Scaler resistor bridge enabled

Bit 21 Reserved, must be kept at reset value.

Bits 20:18 BLANKING[2:0] : COMP2 blanking source selection

These bits select which timer output controls the COMP2 output blanking.

000: No blanking

001: TIM1 OC5 selected as blanking source

010: TIM2 OC3 selected as blanking source

Others: reserved

Bits 17:16 HYST[1:0] : COMP2 hysteresis selection

These bits are set and cleared by software. They select the COMP2 hysteresis voltage.

00: No hysteresis

01: Low hysteresis

10: Medium hysteresis

11: High hysteresis

Bit 15 POLARITY : COMP2 polarity selection

This bit is set and cleared by software. It inverts COMP2 polarity.

0: COMP2 output value not inverted

1: COMP2 output value inverted

Bits 14:10 Reserved, must be kept at reset value.

Bit 9 WINMODE : window mode selection

This bit is set and cleared by software. It selects the window mode of the comparators. If set, both positive inputs of comparators are connected together.

0: COMP2 input plus is not connected to COMP1.

1: COMP2 input plus is connected to COMP1.

Bits 8:7 INPSEL[1:0] : COMP2 input plus selection

These bits are set and cleared by software.

00: PB4

01: PB1

10: PA15

11: reserved

Bits 6:4 INMSEL[2:0] : COMP2 input minus selection

These bits are set and cleared by software. They select which input is connected to the input minus of COMP2.

000: 1/4 V REFINT

001: 1/2 V REFINT

000: 3/4 V REFINT

011: V REFINT

100: DAC channel1

101: reserved

110: PB3

111: GPIOx selected by INMESEL[1:0] bits

Bits 3:2 PWRMODE[1:0] : COMP2 power mode

These bits are set and cleared by software. They control the power and speed of COMP2.

00: High speed

01: Medium speed

10: Medium speed

11: Ultra low-power

Bit 1 Reserved, must be kept at reset value.

Bit 0 EN : COMP2 enable

This bit is set and cleared by software. It switches COMP2 on.

0: COMP2 switched off

1: COMP2 switched on

21.6.3 COMP register map

Table 128. COMP register map and reset values

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x00COMP1_CSRLOCKVALUERes.Res.Res.INMESEL[1:0]Res.SCALENBRGENRes.BLANKING[2:0]HYST[1:0]POLARITYRes.Res.Res.Res.Res.Res.INPSEL[1:0]INMSEL[2:0]PWRMODE[1:0]Res.EN
Reset value000000000000000000000
0x04COMP2_CSRLOCKVALUERes.Res.Res.INMESEL[1:0]Res.SCALENBRGENRes.BLANKING[2:0]HYST[1:0]POLARITYRes.Res.Res.Res.Res.Res.WINMODEINPSEL[1:0]INMSEL[2:0]PWRMODE[1:0]Res.EN
Reset value0000000000000000000000

Refer to Section 2.6 for the register boundary addresses.