19. Comparator (COMP)

19.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:

19.2 COMP main features

19.3 COMP functional description

19.3.1 COMP block diagram

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

Figure 80. Comparator block diagram

Figure 80. Comparator block diagram. The diagram shows a comparator block labeled COMPx. The non-inverting input (+) is labeled COMPx_INP and is connected to a multiplexer. The inverting input (-) is labeled COMPx_INM and is connected to another multiplexer. The COMPx_INP multiplexer has inputs from COMPx_INP I/Os and COMPx_INPSEL. The COMPx_INM multiplexer has inputs from COMPx_INM I/Os, COMPx_INM I/Os, DAC_CH1, VREFINT, 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT. The output of the comparator is labeled COMPx_VALUE and is connected to a polarity selection block. The polarity selection block has inputs from COMPx_POL and an inverter. The output of the polarity selection block is connected to COMPx_OUT, which is labeled 'GPIO alternate function'. The COMPx_VALUE output is also connected to 'Wakeup EXTI line interrupt' and 'Timers'. The diagram is labeled MSv60765V1.
Figure 80. Comparator block diagram. The diagram shows a comparator block labeled COMPx. The non-inverting input (+) is labeled COMPx_INP and is connected to a multiplexer. The inverting input (-) is labeled COMPx_INM and is connected to another multiplexer. The COMPx_INP multiplexer has inputs from COMPx_INP I/Os and COMPx_INPSEL. The COMPx_INM multiplexer has inputs from COMPx_INM I/Os, COMPx_INM I/Os, DAC_CH1, VREFINT, 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT. The output of the comparator is labeled COMPx_VALUE and is connected to a polarity selection block. The polarity selection block has inputs from COMPx_POL and an inverter. The output of the polarity selection block is connected to COMPx_OUT, which is labeled 'GPIO alternate function'. The COMPx_VALUE output is also connected to 'Wakeup EXTI line interrupt' and 'Timers'. The diagram is labeled MSv60765V1.

19.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 110. COMP1 input plus assignment

COMP1_INPCOMP1_INPSEL
PB400
PB201

Table 111. 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 112. COMP2 input plus assignment

COMP2_INPCOMP2_INPSEL
PB400
PB101
PA1510

Table 113. 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

19.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.

19.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.

19.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 81. 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 window mode configuration for two comparators, COMPx and COMPy. Each comparator has two multiplexers for input selection: INPSEL for the non-inverting input (INP) and INMSEL for the inverting input (INM). The INPSEL multiplexers select between external I/Os and internal sources. The INMSEL multiplexers select between external I/Os and internal sources. The 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.

19.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 82. Comparator hysteresis

Timing diagram showing the hysteresis effect. The top graph plots INP (non-inverting input) and INM (inverting input) over time. The bottom graph plots COMP_OUT (output) over time. The output transitions are shown with hysteresis, indicated by the difference between the rising and falling thresholds (INM and INM - V_hyst). The diagram is labeled MS19984V1.

The diagram shows the hysteresis effect in a comparator. The top graph plots the non-inverting input (INP) and the inverting input (INM) over time. The bottom graph plots the output (COMP_OUT) over time. The output transitions are shown with hysteresis, indicated by the difference between the rising and falling thresholds (INM and INM - V hyst ). The diagram is labeled MS19984V1.

Timing diagram showing the hysteresis effect. The top graph plots INP (non-inverting input) and INM (inverting input) over time. The bottom graph plots COMP_OUT (output) over time. The output transitions are shown with hysteresis, indicated by the difference between the rising and falling thresholds (INM and INM - V_hyst). The diagram is labeled MS19984V1.

19.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 83. Comparator output blanking

Timing diagram and logic schematic for comparator output blanking. The timing diagram shows four waveforms: PWM, Inverting input (current limit), Non-inverting input (current), and cmp_out (before blanking gate). A 'current spike' in the non-inverting input causes a 'spurious pulse' in the cmp_out signal. The cmp_blk signal (blanking window) is shown as a series of pulses. The final comp_out signal is the result of an AND gate (Blanking gate) with inputs cmp_out and cmp_blk (inverted). The output is labeled 'comp_out (to I/Os, TIM_BK ...)'.

The figure illustrates the comparator output blanking function. It includes a timing diagram and a logic schematic. The timing diagram shows the following signals over time:

Below the timing diagram, a logic schematic shows a 2-input AND gate labeled 'Blanking gate'. The inputs are cmp_out and cmp_blk (with a bubble indicating inversion). The output is labeled comp_out (to I/Os, TIM_BK ...) .

MS30964V2

Timing diagram and logic schematic for comparator output blanking. The timing diagram shows four waveforms: PWM, Inverting input (current limit), Non-inverting input (current), and cmp_out (before blanking gate). A 'current spike' in the non-inverting input causes a 'spurious pulse' in the cmp_out signal. The cmp_blk signal (blanking window) is shown as a series of pulses. The final comp_out signal is the result of an AND gate (Blanking gate) with inputs cmp_out and cmp_blk (inverted). The output is labeled 'comp_out (to I/Os, TIM_BK ...)'.

19.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:

19.4 COMP low-power modes

Table 114. 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

19.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 115. 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

19.6 COMP registers

19.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

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] : 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

19.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

19.6.3 COMP register map

Table 116. 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.4 for the register boundary addresses.