37. Comparator (COMP)

37.1 Introduction

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

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

37.2 COMP main features

37.3 COMP implementation

The following table describes COMP features on the STM32U5 Series devices.

Table 353. COMP features (1)

COMP modes/featuresSTM32U535/545Other devices
COMP1XX
COMP1 INPy maximum inputy = 6y = 3
COMP1_INPSEL[2:0] reserved codes≥ 101≥ 011
COMP2-X
Window comparator feature-X
  1. 1. 'X' = supported
    '-' = non supported

37.4 COMP functional description

37.4.1 COMP block diagram

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

Figure 318. Comparator block diagrams

Figure 318. Comparator block diagrams. This schematic diagram shows the internal architecture of a comparator (COMPx). The non-inverting input (COMPx_INP) is selected via a multiplexer (COMPx_INPSEL) from either COMPx_INP I/Os or COMPx_INM. The inverting input (COMPx_INM) is selected via another multiplexer (COMPx_INMSEL) from sources including DAC_CH1, DAC_CH2, VREFINT, 3/4 VREFINT, 1/2 VREFINT, 1/4 VREFINT, or a blank source. The comparator core (COMPx) compares these inputs. The output is controlled by COMPx_POLARITY and can be directed to a GPIO alternate function (COMPx_OUT) or internally (compx_out (internal)) via a multiplexer (COMPx_WINOUT). The internal output is also influenced by COMPx_VALUE and COMPx_WINMODE signals.
Figure 318. Comparator block diagrams. This schematic diagram shows the internal architecture of a comparator (COMPx). The non-inverting input (COMPx_INP) is selected via a multiplexer (COMPx_INPSEL) from either COMPx_INP I/Os or COMPx_INM. The inverting input (COMPx_INM) is selected via another multiplexer (COMPx_INMSEL) from sources including DAC_CH1, DAC_CH2, VREFINT, 3/4 VREFINT, 1/2 VREFINT, 1/4 VREFINT, or a blank source. The comparator core (COMPx) compares these inputs. The output is controlled by COMPx_POLARITY and can be directed to a GPIO alternate function (COMPx_OUT) or internally (compx_out (internal)) via a multiplexer (COMPx_WINOUT). The internal output is also influenced by COMPx_VALUE and COMPx_WINMODE signals.

37.4.2 COMP pins and internal signals

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

The comparator output can be connected to the I/Os using the alternate function channel given in “Alternate function mapping” table in the datasheet.

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

The comparator output can be simultaneously redirected internally and externally.

Table 354. COMP1 non-inverting input assignment

COMP1_INPCOMP1_INPSEL[2:0]
COMP1_INP1000
COMP1_INP2001
COMP1_INP3010
COMP1_INP4011
COMP1_INP5100
COMP1_INP6101

Table 355. COMP1 inverting input assignment

COMP1_INMCOMP1_INMSEL[3:0]
\( \frac{1}{4} \) V REFINT0000
\( \frac{1}{2} \) V REFINT0001
\( \frac{3}{4} \) V REFINT0010
V REFINT0011
DAC Channel10100
DAC Channel20101
COMP1_INM10110
COMP1_INM20111
Reserved\( \geq 1000 \)

Table 356. COMP2 non-inverting input assignment

COMP2_INPCOMP2_INPSEL[1:0]
COMP2_INP100
COMP2_INP201
Reserved10
Reserved11

Table 357. COMP2 inverting input assignment

COMP2_INMCOMP2_INMSEL[3:0]
\( \frac{1}{4} \) V REFINT0000
\( \frac{1}{2} \) V REFINT0001
\( \frac{3}{4} \) V REFINT0010
V REFINT0011
DAC Channel10100
DAC Channel20101
COMP2_INM10110
COMP2_INM20111
Reserved\( \geq 1000 \)

Table 358. COMP1 output-blanking PWM assignment

PWM outputCOMP1_BLANKSEL[4:0]
None (no blanking)00000
tim1_oc5xxx1
tim2_oc3xxx1x
Table 358. COMP1 output-blanking PWM assignment (continued)
PWM outputCOMP1_BLANKSEL[4:0]
tim3_oc3xx1xx
ReservedOthers
Table 359. COMP2 output-blanking PWM assignment
PWM outputCOMP2_BLANKSEL[4:0]
None (no blanking)00000
tim3_oc4xxxx1
tim8_oc5xxx1x
tim15_oc1xx1xx
ReservedOthers

37.4.3 Comparator LOCK mechanism

The comparators can be used for safety purposes, such as over-current or thermal protection. For applications having specific functional safety requirements, the comparator programming must not be altered in case of spurious register access or program counter corruption. For this purpose, the comparator control and status registers can be write-protected (read-only).

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

The write protection can only be reset by an MCU reset.

37.4.4 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 can be used to create a window comparator. The monitored analog voltage is connected to the non-inverting (plus) inputs of comparators connected together. The upper and lower threshold voltages are connected to the inverting (minus) inputs of the comparators.

Two non-inverting inputs can be connected internally together by enabling the WINMODE bit to save one IO for other purposes.

Figure 319. Window mode

Figure 319. Window mode diagram showing two comparators, COMPx and COMPy, configured for window mode. COMPx has its non-inverting input (COMPx_INP) connected to an 'Input' signal and its inverting input (COMPx_INM) connected to an 'Upper threshold'. COMPx is configured with WINMODE = 0. Its output (COMPx_VALUE) is connected to one input of an OR gate. COMPy has its non-inverting input (COMPy_INP) connected to a 'Lower threshold' and its inverting input (COMPy_INM) connected to the same 'Input' signal. COMPy is configured with WINMODE = 1. Its output (COMPy_VALUE) is connected to the other input of the OR gate. The OR gate's output is labeled COMPx_OUT. Above the OR gate, COMPx WINOUT = 1 and COMPy WINOUT = 0 are indicated. The diagram is labeled MSv42191V1.
Figure 319. Window mode diagram showing two comparators, COMPx and COMPy, configured for window mode. COMPx has its non-inverting input (COMPx_INP) connected to an 'Input' signal and its inverting input (COMPx_INM) connected to an 'Upper threshold'. COMPx is configured with WINMODE = 0. Its output (COMPx_VALUE) is connected to one input of an OR gate. COMPy has its non-inverting input (COMPy_INP) connected to a 'Lower threshold' and its inverting input (COMPy_INM) connected to the same 'Input' signal. COMPy is configured with WINMODE = 1. Its output (COMPy_VALUE) is connected to the other input of the OR gate. The OR gate's output is labeled COMPx_OUT. Above the OR gate, COMPx WINOUT = 1 and COMPy WINOUT = 0 are indicated. The diagram is labeled MSv42191V1.

37.4.5 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 a low-power mode) to be able to force the hysteresis value using external components.

Figure 320. Comparator hysteresis

Figure 320. Comparator hysteresis graph showing the relationship between the non-inverting input (INP), the inverting input (INM), and the output (COMP_OUT). The INP signal is a sine-like wave. The INM signal is a constant reference voltage. The hysteresis is shown as the difference between the INM level and the INM - Vhyst level. The COMP_OUT signal is a digital signal that transitions between high and low states based on the comparison of INP and INM, with hysteresis. The graph is labeled MS19984V1.
Figure 320. Comparator hysteresis graph showing the relationship between the non-inverting input (INP), the inverting input (INM), and the output (COMP_OUT). The INP signal is a sine-like wave. The INM signal is a constant reference voltage. The hysteresis is shown as the difference between the INM level and the INM - Vhyst level. The COMP_OUT signal is a digital signal that transitions between high and low states based on the comparison of INP and INM, with hysteresis. The graph is labeled MS19984V1.

37.4.6 Comparator output-blanking function

The blanking function prevents 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). This blanking function consists of a selection of a blanking window that is a timer output compare signal. The selection is done by the software (refer to the comparator register description for possible blanking signals).

The complementary of the blanking signal is ANDed with the comparator output to provide the wanted comparator output (see the example in the figure below).

Figure 321. Comparator output blanking

Timing diagram and logic schematic for comparator output blanking. The timing diagram shows five waveforms: PWM, Current limit, Current, Raw comp output, and Final comp output. The PWM signal is a square wave. The Current limit is a dashed horizontal line. The Current signal is a sawtooth-like waveform that rises linearly and then drops sharply. The Raw comp output is a pulse that goes high when the current exceeds the current limit. The Blanking window is a pulse that is high during the rising edge of the current. The Final comp output is a pulse that is the AND of the raw comp output and the inverse of the blanking window. The logic schematic shows a 2-input AND gate with inputs 'Comp out' and 'Blank' (inverted). The output is 'Comp out (to TIM_BK ...)'.

The figure illustrates the comparator output blanking mechanism. The top part is a timing diagram showing the relationship between the PWM signal, the current limit, the actual current, the raw comparator output, the blanking window, and the final comparator output. The bottom part is a logic schematic showing the AND gate that combines the raw comparator output and the inverted blanking window to produce the final output.

Timing diagram and logic schematic for comparator output blanking. The timing diagram shows five waveforms: PWM, Current limit, Current, Raw comp output, and Final comp output. The PWM signal is a square wave. The Current limit is a dashed horizontal line. The Current signal is a sawtooth-like waveform that rises linearly and then drops sharply. The Raw comp output is a pulse that goes high when the current exceeds the current limit. The Blanking window is a pulse that is high during the rising edge of the current. The Final comp output is a pulse that is the AND of the raw comp output and the inverse of the blanking window. The logic schematic shows a 2-input AND gate with inputs 'Comp out' and 'Blank' (inverted). The output is 'Comp out (to TIM_BK ...)'.

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

37.4.8 Scaler function

The scaler block provides the different voltage reference levels to the comparator inputs. This block 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 are enabled by setting the INMSEL value in the COMP_CFGRx registers, to connect the corresponding inverting input to the scaler output.

When the resistor divided voltage is not used, the resistor bridge and the amplifier are disabled to reduce the consumption. When the resistor bridge is disconnected, the 1/4 VREF_COMP, 1/2 VREF_COMP, and 3/4 VREF_COMP levels are equal to VREF_COMP.

Figure 322. Scaler

Figure 322. Scaler diagram showing a comparator with its non-inverting input connected to VREFINT and its inverting input connected to a tap on a resistor ladder. The ladder is connected between VREF_COMP and INMSEL < 3. The taps are labeled 3/4 VREF_COMP, 1/2 VREF_COMP, and 1/4 VREF_COMP. The comparator output is connected to the VREF_COMP line. The diagram is labeled MSV63603V1.
Figure 322. Scaler diagram showing a comparator with its non-inverting input connected to VREFINT and its inverting input connected to a tap on a resistor ladder. The ladder is connected between VREF_COMP and INMSEL < 3. The taps are labeled 3/4 VREF_COMP, 1/2 VREF_COMP, and 1/4 VREF_COMP. The comparator output is connected to the VREF_COMP line. The diagram is labeled MSV63603V1.

37.5 COMP low-power modes

Table 360. Comparator behavior in the low-power modes

ModeDescription
SleepNo effect on the comparators.
Comparator interrupts cause the device to exit Sleep mode.
StopNo effect on the comparators.
Comparator interrupts cause the device to exit Stop mode.
StandbyThe COMP registers are powered down and must be reinitialized after exiting Standby mode.

37.6 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 the low-power modes.

Refer to Section 23: Extended interrupts and event controller (EXTI) for more details.

To enable the COMPx interrupt, follow this sequence:

  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 361. Interrupt control bits

Interrupt eventEvent flagEnable control bitExit Sleep modeExit Stop modesExit Standby mode
COMP1 outputVALUE in COMP1_CSRthrough EXTIYesYesNo
COMP2 outputVALUE in COMP2_CSRthrough EXTIYesYesNo

37.7 COMP registers

37.7.1 COMP1 control and status register (COMP1_CSR)

Address offset: 0x00

Reset value: 0x0000 0000

31302928272625242322212019181716
LOCKVALUERes.Res.Res.Res.Res.BLANKSEL[4:0]PWRMODE[1:0]HYST[1:0]
rwrrwrwrwrwrwrwrwrwrw

1514131211109876543210
POLARITYWINOUTRes.Res.WIN MODEINPSEL[2:0]INMSEL[3:0]Res.Res.Res.Res.EN
rwrwrwrwrwrwrwrwrwrwrw

Bit 31 LOCK : COMP1_CSR register lock

This bit is set by the software and cleared by reset. It locks the whole content of COMP1_CSR.

0: COMP1_CSR read/write bits can be written by the software.

1: COMP1_CSR bits can be read but not written by the software.

Bit 30 VALUE : COMP1 output status

This bit is read-only. It reflects the level of the COMP1 output after the polarity selector and blanking (see Figure 321 ).

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

Bits 24:20 BLANKSEL[4:0] : COMP1 blanking source selector

This field is controlled by the software (if not locked) and selects the PWM signal for comparator output-blanking (see Table 358 for the assignment).

Bits 19:18 PWRMODE[1:0] : COMP1 power mode selector

This bitfield is controlled by the software (if not locked). It selects the power consumption and, as a consequence, the speed of the COMP1.

00: High speed

01-10: Medium speed and power

11: Ultra-low-power

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

This bitfield is controlled by the software (if not locked). It selects the COMP1 hysteresis.

00: None

01: Low hysteresis

10: Medium hysteresis

11: High hysteresis

Bit 15 POLARITY : COMP1 polarity selector

This bit is controlled by the software (if not locked). It selects the COMP1 output polarity.

0: Non-inverted

1: Inverted

Bit 14 WINOUT : COMP1 output selector

This bit is controlled by the software (if not locked). It selects the COMP1 output.

This bit must be kept at zero when window comparator feature is not supported.

0: COMP1_VALUE

1: COMP1_VALUE XOR COMP2_VALUE (required for window mode, see Figure 319 )

Bits 13:12 Reserved, must be kept at reset value.

Bit 11 WINMODE : COMP1 non-inverting input selector for window mode

This bit is controlled by the software (if not locked). It selects the signal for the COMP1_INP input of the COMP1.

This bit must be kept at zero when window comparator feature is not supported.

0: Signal selected with INPSEL[1:0]

1: COMP2_INP signal of COMP2 (required for window mode, see Figure 319 )

Bits 10:8 INPSEL[2:0] : COMP1 signal selector for non-inverting input

This field is controlled by the software (if not locked). It selects the signal for the non-inverting input COMP1_INP (see Table 354 for the assignment).

Bits 7:4 INMSEL[3:0] : COMP1 signal selector for inverting input INM

This field is controlled by the software (if not locked). It selects the signal for the inverting input COMP1_INM (see Table 355 for the assignment).

Bits 3:1 Reserved, must be kept at reset value.

Bit 0 EN : COMP1 enable

This bit is controlled by the software (if not locked). It enables COMP1.

0: COMP1 disabled

1: COMP1 enabled

37.7.2 COMP2 control and status register (COMP2_CSR)

Address offset: 0x04

Reset value: 0x0000 0000

31302928272625242322212019181716
LOCKVALUERes.Res.Res.Res.Res.BLANKSEL[4:0]PWRMODE[1:0]HYST[1:0]
rwrrwrwrwrwrwrwrwrwrw

1514131211109876543210
POLARITYWIN OUTRes.Res.WIN MODERes.INPSEL[1:0]INMSEL[3:0]Res.Res.Res.EN
rwrwrwrwrwrwrwrwrwrw

Bit 31 LOCK : COMP2_CSR register lock

This bit is set by the software and cleared by reset. It locks the whole content of COMP2_CSR.

0: COMP2_CSR read/write bits can be written by the software.

1: COMP2_CSR bits can be read but not written by the software.

Bit 30 VALUE : COMP2 output status

This bit is read-only. It reflects the level of the COMP2 output after the polarity selector and blanking (see Figure 321 ).

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

Bits 24:20 BLANKSEL[4:0] : COMP2 blanking source selector

This field is controlled by the software (if not locked) and selects the PWM signal for comparator output-blanking (see Table 359 for the assignment).

Bits 19:18 PWRMODE[1:0] : COMP2 power mode selector

This bitfield is controlled by the software (if not locked). It selects the power consumption and, as a consequence, the speed of the COMP2.

00: High speed

01-10: Medium speed and power

11: Ultra-low-power

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

This bitfield is controlled by the software (if not locked). It selects the COMP2 hysteresis.

00: None

01: Low hysteresis

10: Medium hysteresis

11: High hysteresis

Bit 15 POLARITY : COMP2 polarity selector

This bit is controlled by the software (if not locked). It selects the COMP2 output polarity.

0: Non-inverted

1: Inverted

Bit 14 WINOUT : COMP2 output selector

This bit is controlled by the software (if not locked). It selects the COMP2 output.

0: COMP2_VALUE

1: COMP1_VALUE XOR COMP2_VALUE (required for window mode, see Figure 319 )

Bits 13:12 Reserved, must be kept at reset value.

Bit 11 WINMODE : COMP2 non-inverting input selector for window mode

This bit is controlled by the software (if not locked). It selects the signal for the COMP2_INP input of the COMP2.

0: Signal selected with INPSEL[1:0]

1: COMP1_INP signal of COMP1 (required for window mode, see Figure 319 )

Bit 10 Reserved, must be kept at reset value.

Bits 9:8 INPSEL[1:0] : COMP2 signal selector for non-inverting input

This field is controlled by the software (if not locked). It selects the signal for the non-inverting input COMP2_INP (see Table 356 for the assignment).

Bits 7:4 INMSEL[3:0] : COMP2 signal selector for inverting input INM

This field is controlled by the software (if not locked). It selects the signal for the inverting input COMP2_INM (see Table 357 for the assignment).

Bits 3:1 Reserved, must be kept at reset value.

Bit 0 EN : COMP2 enable

This bit is controlled by the software (if not locked). It enables COMP2.

0: COMP2 disabled

1: COMP2 enabled

37.7.3 COMP register map

Table 362. COMP register map and reset values

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x00COMP1_CSRLOCKVALUERes.Res.Res.Res.Res.BLANKSEL[4:0]PWRMODE[1:0]HYST[1:0]POLARITYWINOUTRes.Res.WINMODEINPSEL[2:0]INPSEL[3:0]Res.Res.Res.EN
Reset value0000000000000000000000
0x04COMP2_CSRLOCKVALUERes.Res.Res.Res.Res.BLANKSEL[4:0]PWRMODE[1:0]HYST[1:0]POLARITYWINOUTRes.Res.WINMODEINPSEL[1:0]INPSEL[3:0]Res.Res.Res.EN
Reset value000000000000000000000

Refer to Section 2.3 for the register boundary addresses.