26. Comparator (COMP)

26.1 Introduction

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

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

26.2 COMP main features

26.3 COMP functional description

26.3.1 COMP block diagram

The block diagram of the comparators is shown in Figure 182 .

Figure 182. Comparator block diagram

Figure 182. Comparator block diagram. The diagram shows a comparator block (COMPx) with two inputs: COMPx_INP and COMPx_INM. COMPx_INP is connected to a multiplexer (COMPx_INPSEL) which selects between COMPx_INP I/Os and COMPx_INM I/Os. COMPx_INM is connected to a multiplexer (COMPx_INMSEL) which selects between DAC_CH1, DAC_CH2, VREFINT, 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT. The output of the comparator is COMPx_VALUE, which is connected to a polarity selection block (COMPx_POL). The output can be connected to a GPIO alternate function (COMPx_OUT), a Wakeup EXTI line interrupt, or TIMERS. The diagram is labeled MS34498V1.
Figure 182. Comparator block diagram. The diagram shows a comparator block (COMPx) with two inputs: COMPx_INP and COMPx_INM. COMPx_INP is connected to a multiplexer (COMPx_INPSEL) which selects between COMPx_INP I/Os and COMPx_INM I/Os. COMPx_INM is connected to a multiplexer (COMPx_INMSEL) which selects between DAC_CH1, DAC_CH2, VREFINT, 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT. The output of the comparator is COMPx_VALUE, which is connected to a polarity selection block (COMPx_POL). The output can be connected to a GPIO alternate function (COMPx_OUT), a Wakeup EXTI line interrupt, or TIMERS. The diagram is labeled MS34498V1.

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

It is possible to have the comparator output simultaneously redirected internally and externally.

Table 172. COMP1 input plus assignment

COMP1_INPCOMP1_INPSEL
PC50
PB21

Table 173. COMP1 input minus assignment

COMP1_INMCOMP1_INMSEL[2:0]
\( \frac{1}{4} \) VREFINT000
\( \frac{1}{2} \) VREFINT001

Table 173. COMP1 input minus assignment (continued)

COMP1_INMCOMP1_INMSEL[2:0]
\( \frac{3}{4} V_{REFINT} \)010
\( V_{REFINT} \)011
DAC Channel1100
DAC Channel2101
PB1110
PC4111

Table 174. COMP2 input plus assignment

COMP2_INPCOMP2_INPSEL
PB40
PB61

Table 175. COMP2 input minus assignment

COMP2_INMCOMP2_INMSEL[2:0]
\( \frac{1}{4} V_{REFINT} \)000
\( \frac{1}{2} V_{REFINT} \)001
\( \frac{3}{4} V_{REFINT} \)010
\( V_{REFINT} \)011
DAC Channel1100
DAC Channel2101
PB3110
PB7111

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

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.

26.3.4 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, it is necessary to insure that the comparator programming cannot 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 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 reset by a MCU reset.

26.3.5 Window comparator

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

Two embedded comparators 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. Two non-inverting inputs can be connected internally together by enabling WINMODE bit to save one IO for other purposes.

Figure 183. Window mode

Figure 183. Window mode diagram showing two comparators, COMPx and COMPy, configured for window comparison. Both comparators have their non-inverting inputs (+) connected to a common analog voltage source. COMPx's inverting input (-) is connected to an upper threshold voltage (COMPx_INM), and COMPy's inverting input (-) is connected to a lower threshold voltage (COMPy_INM). The WINMODE bit is set to connect the non-inverting inputs of both comparators together. The diagram shows input selection multiplexers for both comparators, allowing selection between external I/Os and internal sources. The output of COMPx is connected to the non-inverting input of COMPy.

The diagram illustrates the internal architecture for window mode. It features two comparators, COMPx and COMPy.
COMPx (top): Its non-inverting input (+) is labeled COMPx_INP and its inverting input (-) is labeled COMPx_INM. The inputs are selected via multiplexers labeled COMPx_INPSEL and COMPx_INMSEL. COMPx_INPSEL can select between COMPx_INP I/Os or internal sources. COMPx_INMSEL can select between COMPx_INM I/Os or internal sources.
COMPy (bottom): Its non-inverting input (+) is labeled COMPy_INP and its inverting input (-) is labeled COMPy_INM. Similar to COMPx, it has input selection multiplexers labeled COMPy_INPSEL and COMPy_INMSEL.
WINMODE: A switch controlled by the WINMODE bit connects the non-inverting inputs of COMPx and COMPy together, allowing them to share a common analog voltage source.
Connections: The output of COMPx is connected to the non-inverting input (+) of COMPy. The inverting inputs are connected to threshold levels: COMPx_INM for the upper threshold and COMPy_INM for the lower threshold.
Reference: MS35329V1 is noted in the bottom right corner.

Figure 183. Window mode diagram showing two comparators, COMPx and COMPy, configured for window comparison. Both comparators have their non-inverting inputs (+) connected to a common analog voltage source. COMPx's inverting input (-) is connected to an upper threshold voltage (COMPx_INM), and COMPy's inverting input (-) is connected to a lower threshold voltage (COMPy_INM). The WINMODE bit is set to connect the non-inverting inputs of both comparators together. The diagram shows input selection multiplexers for both comparators, allowing selection between external I/Os and internal sources. The output of COMPx is connected to the non-inverting input of COMPy.

26.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 184. Comparator hysteresis

Figure 184. Comparator hysteresis diagram showing INP, INM, and COMP_OUT signals over time.

This diagram illustrates the hysteresis effect in a comparator. The top graph shows the non-inverting input (INP) as a smooth curve and the inverting input (INM) as a constant reference level. The comparator output (COMP_OUT) is shown as a digital signal that switches between high and low states. The output switches from low to high when INP rises above INM and from high to low when INP falls below INM - V hyst . Vertical dashed lines indicate the transition points. The identifier MS19984V1 is in the bottom right corner.

Figure 184. Comparator hysteresis diagram showing INP, INM, and COMP_OUT signals over time.

26.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 consists of a selection of a blanking window which is a timer output compare signal. The selection is done by software (refer to the comparator register description for possible blanking signals). Then, the complementary of the blanking signal is ANDed with the comparator output to provide the wanted comparator output. See the example provided in the figure below.

Figure 185. Comparator output blanking

Figure 185. Comparator output blanking diagram showing PWM, Current, Raw comp output, Blanking window, and Final comp output signals, along with an AND gate logic diagram.

This diagram shows the comparator output blanking function. The top section displays five timing diagrams: PWM (a periodic square wave), Current (showing a sawtooth-like waveform with sharp spikes), Raw comp output (a digital signal that pulses high during current spikes), Blanking window (a digital signal that is high during the initial part of each PWM period), and Final comp output (the result of ANDing the raw output with the inverted blanking window). The bottom section shows a logic diagram where the 'Comp out' signal and the inverted 'Blank' signal are inputs to an AND gate, which produces the 'Comp out (to TIM_BK ...)' signal. The identifier MS30964V1 is in the bottom right corner.

Figure 185. Comparator output blanking diagram showing PWM, Current, Raw comp output, Blanking window, and Final comp output signals, along with an AND gate logic diagram.

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

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

26.4 COMP low-power modes

Table 176. Comparator behavior in the low power modes

ModeDescription
SleepNo effect on the comparators.
Comparator interrupts cause the device to exit the Sleep mode.
Low-power runNo effect.
Low-power sleepNo effect. COMP interrupts cause the device to exit the Low-power sleep mode.
Stop 0No effect on the comparators.
Comparator interrupts cause the device to exit the Stop mode.
Stop 1
Stop 2
StandbyThe COMP registers are powered down and must be reinitialized after exiting Standby or Shutdown mode.
Shutdown

26.5 COMP interrupts

The comparator outputs are internally connected to the Extended interrupts and events controller. 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 Interrupt and events section for more details.

To enable COMPx interrupt, it is required to 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 COMPx.

Table 177. Interrupt control bits

Interrupt eventEvent flagEnable control bitExit from Sleep modeExit from Stop modesExit from Standby mode
COMP1 outputVALUE in COMP1_CSRThrough EXTIYesYesN/A
COMP2 outputVALUE in COMP2_CSRThrough EXTIYesYesN/A

26.6 COMP registers

26.6.1 Comparator 1 control and status register (COMP1_CSR)

The COMP1_CSR is the Comparator 1 control/status register. It contains all the bits /flags related to comparator1.

Address offset: 0x00

System reset value: 0x0000 0000

31302928272625242322212019181716
LOCKVALUERes.Res.Res.Res.Res.Res.SCAL ENBRG ENRes.BLANKINGHYST
rsrrwrwrwrw

1514131211109876543210
POLARITYRes.Res.Res.Res.Res.Res.Res.INP SEL.INMSELPWRMODERes.EN
rwrwrwrwrw

Bit 31 LOCK : COMP1_CSR register lock bit

This bit is set by software and cleared by a hardware system reset. It locks the whole content of the comparator 1 control register, COMP1_CSR[31:0].

0: COMP1_CSR[31:0] for comparator 1 are read/write

1: COMP1_CSR[31:0] for comparator 1 are read-only

Bit 30 VALUE : Comparator 1 output status bit

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

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

Bit 23 SCALEN : Voltage scaler enable bit

This bit is set and cleared by software. This bit enable the outputs of the V REFINT divider available on the minus input of the Comparator 1.

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

1: Bandgap scaler enable

Bit 22 BRGEN : Scaler bridge enable

This bit is set and cleared by software (only if LOCK not set). This bit enable the bridge of the scaler.

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

1: Scaler resistor bridge enable

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

If SCALEN and BRGEN are set, 1/4 BGAP 1/2 BGAP 3/4 BGAP and BGAP voltage references are available.

Bit 21 Reserved, must be kept at reset value

Bits 20:18 BLANKING[2:0] : Comparator 1 blanking source selection bits

These bits select which timer output controls the comparator 1 output blanking.

000: No blanking

001: TIM1 OC5 selected as blanking source

010: TIM2 OC3 selected as blanking source

All other values: reserved

Bits 17:16 HYST[1:0] : Comparator 1 hysteresis selection bits

These bits are set and cleared by software (only if LOCK not set). They select the hysteresis voltage of the comparator 1.

00: No hysteresis

01: Low hysteresis

10: Medium hysteresis

11: High hysteresis

Bit 15 POLARITY : Comparator 1 polarity selection bit

This bit is set and cleared by software (only if LOCK not set). It inverts Comparator 1 polarity.

0: Comparator 1 output value not inverted

1: Comparator 1 output value inverted

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

Bit 7 INPSEL : Comparator1 input plus selection bit

This bit is set and cleared by software (only if LOCK not set).

0: external IO - PC5

1: PB2

Bits 6:4 INMSEL : Comparator 1 input minus selection bits

These bits are set and cleared by software (only if LOCK not set). They select which input is connected to the input minus of comparator 1.

000 = 1/4 V REFINT

001 = 1/2 V REFINT

010 = 3/4 V REFINT

011 = V REFINT

100 = DAC Channel1

101 = DAC Channel2

110 = PB1111 = PC4

Bits 3:2 PWRMODE[1:0] : Power Mode of the comparator 1

These bits are set and cleared by software (only if LOCK not set). They control the power/speed of the Comparator 1.

Bit 1 Reserved, must be kept cleared.

Bit 0 EN : Comparator 1 enable bit

This bit is set and cleared by software (only if LOCK not set). It switches on Comparator1.

26.6.2 Comparator 2 control and status register (COMP2_CSR)

The COMP2_CSR is the Comparator 2 control/status register. It contains all the bits /flags related to comparator 2.

Address offset: 0x04

System reset value: 0x0000 0000

31302928272625242322212019181716
LOCKVALUERes.Res.Res.Res.Res.Res.SCAL ENBRG ENRes.BLANKINGHYST
rsrrwrwrwrw

1514131211109876543210
POLA RITYRes.Res.Res.Res.Res.WIN MODERes.INP SELINMSELPWRMODERes.EN
rwrwrwrwrwrw

Bit 31 LOCK : CSR register lock bit

This bit is set by software and cleared by a hardware system reset. It locks the whole content of the comparator 2 control register, COMP2_CSR[31:0].

Bit 30 VALUE : Comparator 2 output status bit

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

Bits 29:24 Reserved, must be kept at reset value

Bit 23 SCALEN : Voltage scaler enable bit

This bit is set and cleared by software. This bit enable the outputs of the V REFINT divider available on the minus input of the Comparator 2.

Bit 22 BRGEN : Scaler bridge enable

This bit is set and cleared by software (only if LOCK not set). This bit enable the bridge of the scaler.

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

1: Scaler resistor bridge enable

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

If SCALEN and BRGEN are set, 1/4 BGAP 1/2 BGAP 3/4 BGAP and BGAP voltage references are available.

Bit 21 Reserved, must be kept at reset value

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

These bits select which timer output controls the comparator 2 output blanking.

000: No blanking

100: TIM15 OC1 selected as blanking source

All other values: reserved

Bits 17:16 HYST[1:0] : Comparator 2 hysteresis selection bits

These bits are set and cleared by software (only if LOCK not set). Select the hysteresis voltage of the comparator 2.

00: No hysteresis

01: Low hysteresis

10: Medium hysteresis

11: High hysteresis

Bit 15 POLARITY : Comparator 2 polarity selection bit

This bit is set and cleared by software (only if LOCK not set). It inverts Comparator 2 polarity.

0: Comparator 2 output value not inverted

1: Comparator 2 output value inverted

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

Bit 9 WINMODE : Windows mode selection bit

This bit is set and cleared by software (only if LOCK not set). This bit selects the window mode of the comparators. If set, both positive inputs of comparators will be connected together.

0: Input plus of Comparator 2 is not connected to Comparator 1

1: Input plus of Comparator 2 is connected with input plus of Comparator 1

Bit 8 Reserved, must be kept at reset value.

Bit 7 INPSEL : Comparator 1 input plus selection bit

This bit is set and cleared by software (only if LOCK not set).

0: PB4

1: PB6

Bits 6:4 INMSEL : Comparator 2 input minus selection bits

These bits are set and cleared by software (only if LOCK not set). They select which input is connected to the input minus of comparator 2.

000 = 1/4 V REFINT

001 = 1/2 V REFINT

010 = 3/4 V REFINT

011 = V REFINT

100 = DAC Channel1

101 = DAC Channel2

110 = PB3

111 = PB7

Bits 3:2 PWRMODE[1:0] : Power Mode of the comparator 2

These bits are set and cleared by software (only if LOCK not set). They control the power/speed of the Comparator 2.

00: High speed

01 or 10: Medium speed

11: Ultra low power

Bit 1 Reserved, must be kept cleared.

Bit 0 EN : Comparator 2 enable bit

This bit is set and cleared by software (only if LOCK not set). It switches on comparator2.

0: Comparator 2 switched OFF

1: Comparator 2 switched ON

26.6.3 COMP register map

The following table summarizes the comparator registers.

Table 178. COMP register map and reset values

OffsetRegister313029282726252423222120191817161514131211109876543210
0x00COMP1_CSRLOCKVALUERes.Res.Res.Res.Res.Res.SCALENBRGENRes.BLANKINGHYSTPOLARITY.Res.Res.Res.Res.Res.Res.Res.Res.Res.INPSEL.INMSELPWRMODERes.EN
Reset value0000
0x04COMP2_CSRLOCKVALUERes.Res.Res.Res.Res.Res.SCALENBRGENRes.BLANKINGHYSTPOLARITY.Res.Res.Res.Res.Res.Res.Res.Res.WINMODERes.INPSELINMSELPWRMODERes.EN
Reset value00000

Refer to Section 2.2 on page 93 for the register boundary addresses.