23. Comparator (COMP)

23.1 Introduction

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

23.2 COMP main features

23.3 COMP implementation

23.3.1 COMP implementation for STM32C55x/562 and STM32C59x/5A3 devices

Table 166. COMP implementation for STM32C55x/562 and STM32C59x/5A3 (1)

COMP featuresCOMP1
Window mode-
Programmable power and speedX
Programmable hysteresisX

1. X = supported, - = not supported

Table 167. COMP implementation non-inverting input assignment for STM32C55x/562 and STM32C59x/5A3

INPSEL [2:0]COMP1
000COMP1_INP1
001COMP1_INP2
010COMP1_INP3
011dac1_ch1
OthersReserved

23.3.2 COMP implementation for STM32C53x/542 device

Table 168. COMP implementation for STM32C53x/542(1)

COMP featuresCOMP1COMP2
Window modeXX
Programmable power and speedXX
Programmable hysteresisXX

1. X = supported, - = not supported

Table 169. COMP implementation non-inverting input assignment for STM32C53x/542

INPSEL [2:0]COMP1COMP2
000COMP1_INP1COMP2_INP1
001COMP1_INP2COMP2_INP2
100COMP1_INP3COMP2_INP3
101dac1_ch1dac1_ch2
OthersReservedReserved

The following table lists the paired comparators required for window mode.

Table 170. Window paired comparators

Window comparator#1#2
Upper threshold comparatorCOMP1COMP2
Lower threshold comparatorCOMP2COMP1

23.4 COMP functional description

23.4.1 COMP block diagram

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

Figure 151. Comparator functional block diagram

Figure 151. Comparator functional block diagram showing COMP peripheral inputs, internal routing, comparator core, polarity selection, blanking logic, DFF interrupt logic, and outputs.

The diagram illustrates the functional blocks of the Comparator (COMP) peripheral. Key components include:

MSv76141V1

Figure 151. Comparator functional block diagram showing COMP peripheral inputs, internal routing, comparator core, polarity selection, blanking logic, DFF interrupt logic, and outputs.

Note: The VSENSE buffer is controlled by the ADC peripheral. To activate the buffer, set the TSEN bit in the ADCC_CCR register.

23.4.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 171. COMP input/output internal signals

Signal nameSignal typeDescription
dac1_out1Analog inputAnalog signal from DAC peripheral
comp_x_blkxDigital outputSee Table 173: COMP blanking source assignment
comp_x_outDigital outputCOMPx output
comp_x_itDigital outputCOMPx interrupt out

Note: For the COMP non-inverting input, refer to Table 167: COMP implementation non-inverting input assignment for STM32C55x/562 and STM32C59x/5A3 and Table 169: COMP implementation non-inverting input assignment for STM32C53x/542.

Table 172. COMP inverting input assignment

INMSEL [3:0]COMP1COMP2
0000\(\frac{1}{4} V_{REFINT}\)\(\frac{1}{4} V_{REFINT}\)
0001\(\frac{1}{2} V_{REFINT}\)\(\frac{1}{2} V_{REFINT}\)
0010\(\frac{3}{4} V_{REFINT}\)\(\frac{3}{4} V_{REFINT}\)
0011\(V_{REFINT}\)\(V_{REFINT}\)
0100dac1_ch1dac1_ch2
0101COMP1_INM1COMP2_INM1
0110COMP1_INM2COMP2_INM2
0111COMP1_INM3COMP2_INM3
0100VENSEopamp1_out

Note: Other combinations are reserved.

Table 173. COMP blanking source assignment

BLANKING[3:0]COMP1COMP2
0000No blankingNo blanking
0001tim1_oc5tim1_oc6
0010tim2_oc3tim2_oc3
0011tim5_oc3-
0100tim5_oc4-
0101tim8_oc5(1)tim8_oc5
0110tim15_oc2lptim1_ch1
0111-lptim1_ch2

1. Not available for STM32C53x and STM32C542.

23.4.3 COMP reset and clocks

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

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

23.4.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 entire register set of the comparator channel to become read-only, including the LOCK bit.

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

23.4.5 Window comparator (optional feature)

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

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 152. Window comparators functional block diagram

Functional block diagram of window comparators showing COMP1 and COMP2 with their inputs, DACs, and output logic controlled by WINOUT.

The diagram illustrates the functional block diagram for two comparators, COMP1 and COMP2, configured as a window comparator.
- COMP1: Its non-inverting input (+) is selected by a multiplexer (INPSEL / WINMODE) from inputs COMP1_INP1, COMP1_INP2, or dac1_ch1. Its inverting input (-) is selected by a multiplexer (INNSEL) from VREFs, DACs, COMP1_INM1, or COMP1_INM2. The output of COMP1 passes through a Polarity Blanking block.
- COMP2: Its non-inverting input (+) is selected by a multiplexer (INPSEL / WINMODE) from inputs COMP2_INP1, COMP2_INP2, or dac1_ch2. Its inverting input (-) is selected by a multiplexer (INNSEL) from VREFs, DACs, COMP2_INM1, COMP2_INM2, or COMP2_INM3. The output of COMP2 passes through a Polarity Blanking block.
- Window Mode Logic: The outputs of both comparators are fed into OR gates. The output of the COMP1 OR gate is labeled 'comp1_value'. The output of the COMP2 OR gate is labeled 'comp2_value'. These values are then selected by a multiplexer (WINOUT) to produce the final outputs 'comp1_out' and 'comp2_out'.
- Control: A 'Window Mode' control signal is shown influencing the selection logic for the inputs of both comparators.
- Reference: The diagram is labeled MSv76142V1 in the bottom right corner.

Functional block diagram of window comparators showing COMP1 and COMP2 with their inputs, DACs, and output logic controlled by WINOUT.

23.4.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 153. Comparator hysteresis

Figure 153: Comparator hysteresis diagram. The top graph shows a noisy input signal (INM) oscillating around a threshold. The bottom graph shows the comparator output (COMP_OUT) as a digital signal. The hysteresis is illustrated by the fact that the output transitions occur at different input levels: one threshold for rising input and a lower threshold (INM - V_hyst) for falling input. The output remains stable despite the noise on the input signal.

The diagram illustrates the hysteresis effect in a comparator. The top part of the graph shows the input signal (INM) as a noisy waveform. The bottom part shows the comparator output (COMP_OUT) as a digital signal. The hysteresis is represented by two horizontal dashed lines: the upper one is labeled INM and the lower one is labeled INM - Vhyst. The output transitions occur when the input crosses these thresholds, demonstrating how hysteresis prevents spurious transitions caused by noise.

MS19984V1

Figure 153: Comparator hysteresis diagram. The top graph shows a noisy input signal (INM) oscillating around a threshold. The bottom graph shows the comparator output (COMP_OUT) as a digital signal. The hysteresis is illustrated by the fact that the output transitions occur at different input levels: one threshold for rising input and a lower threshold (INM - V_hyst) for falling input. The output remains stable despite the noise on the input signal.

23.4.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 following figure (the COMP channel number is not represented).

Figure 154. Comparator output blanking

Figure 154: Comparator output blanking diagram. The diagram shows five waveforms over time: PWM, Inverting input (represents current limit), Non-inverting input (represents current), cmp_out (before blanking gate), cmp_blk (blanking window), and comp_out (COMP output). The PWM signal is a square wave. The Inverting input is a dashed line representing a current limit. The Non-inverting input is a solid line representing current, which has two sharp upward spikes labeled 'spurious spike'. The cmp_out (before blanking gate) shows narrow pulses at the rising edges of the PWM signal. The cmp_blk signal shows two narrow pulses that coincide with the spurious spikes on the Non-inverting input. The comp_out (COMP output) shows only the pulses that are not blanked. Below the waveforms, a logic diagram shows a D-gate with 'cmp_out' as the data input and 'cmp_blk' (inverted) as the enable input, producing the final 'comp_out (to I/Os, TIM_BK ...)'.

MSv75183V1

Figure 154: Comparator output blanking diagram. The diagram shows five waveforms over time: PWM, Inverting input (represents current limit), Non-inverting input (represents current), cmp_out (before blanking gate), cmp_blk (blanking window), and comp_out (COMP output). The PWM signal is a square wave. The Inverting input is a dashed line representing a current limit. The Non-inverting input is a solid line representing current, which has two sharp upward spikes labeled 'spurious spike'. The cmp_out (before blanking gate) shows narrow pulses at the rising edges of the PWM signal. The cmp_blk signal shows two narrow pulses that coincide with the spurious spikes on the Non-inverting input. The comp_out (COMP output) shows only the pulses that are not blanked. Below the waveforms, a logic diagram shows a D-gate with 'cmp_out' as the data input and 'cmp_blk' (inverted) as the enable input, producing the final 'comp_out (to I/Os, TIM_BK ...)'.

23.4.8 COMP startup stabilization time

The comparator parameters and its output is undefined after the COMP is enabled (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.

23.4.9 COMP power and speed modes

The power consumption of the COMP channels versus propagation delay can be programmed with PWRMODE[1:0] bit field in COMP_CFGRx registers.

23.5 COMP low-power modes

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

23.6 COMP interrupts

There are two ways to use the comparator as 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 APB clock running.

23.6.1 COMP wake-up interrupt through EXTI

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

Sequence to enable the COMPx interrupt through 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 175. COMP wake-up interrupt through EXTI control bits

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

23.6.2 COMP NVIC interrupt through EXTI

Refer to the Section 16: Extended interrupts and event controller (EXTI) and Section 15: Nested vectored interrupt controller (NVIC) for more details.

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_out signal, in interrupt mode, select the rising, falling, or either-edge sensitivity and enable the EXTI line.
  3. 3. Configure and enable the IMx in EXTI_IMRx.

Table 176. COMP NVIC interrupt through EXTI control bits

Interrupt eventInterrupt flagEnable control bitInterrupt clear bitExit Sleep modeExit Stop modes
compx_itRPIFx or FPIFx in EXTI_RPRx or EXTI.FPRxRTx or FTx in EXTI.RTSRx or EXTI.FTSRx and IMx in EXTI.IMRxRPIFx or FPIFx in EXTI_RPRx or EXTI.FPRxYes
(with APB clock)
Yes

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

23.6.3 COMP interrupt through NVIC of the CPU

Refer to the Section 15: Nested vectored interrupt controller (NVIC) for more details.

Sequence to enable the COMP interrupt through 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 177. COMP interrupt through NVIC of the CPU control bits

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

Note: It is mandatory to enable APB clock to use this interrupt. If clock is not enabled, 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 re-entrance. This can be done by setting ITEN = 0 or changing the comparator POLARITY bit.

23.7 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 \(V_{REFINT}\).

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 disconnect to reduce the consumption.

Figure 155. Scaler block diagram

Figure 155. Scaler block diagram showing operational amplifier buffer for VREFINT with resistor ladder tap outputs: VREFINT, 3/4 VREFINT, 1/2 VREFINT, 1/4 VREFINT, controlled by SCALEN and BRGEN.
Figure 155. Scaler block diagram showing operational amplifier buffer for VREFINT with resistor ladder tap outputs: VREFINT, 3/4 VREFINT, 1/2 VREFINT, 1/4 VREFINT, controlled by SCALEN and BRGEN.

23.8 COMP registers

23.8.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 by writing CC12F = 1 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 by writing CC1IF = 1 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 COMP1 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.

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

23.8.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.INPSEL[2:0]INMSEL[3:0]
rwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
Res.WINOUTPWRMODE[1:0]Res.Res.HYST[1:0]Res.ITENRes.WINMODEPOLARITYSCALE 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_CFGR1[31:0]

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

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

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

Bits 27:24 BLANKING[3:0]: COMP1 channel blanking source selection

Bits of this field are set and cleared by software (only if LOCK not set).

The field selects the input source for COMP1 channel output blanking.

Note: See Table 173: COMP blanking source assignment for more details.

Bit 23 Reserved, must be kept at reset value.

Bit 2 SCALEN: Voltage scaler enable

This bit is set and cleared by software (only if LOCK not set). This bit enables the \(V_{REFINT}\) scaler for the COMP channels.

0: \(V_{REFINT}\) scaler disabled

1: \(V_{REFINT}\) 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

23.8.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.INPSEL[2:0]INMSEL[3:0]
rwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
Res.WINOUTPWRMODE[1:0]Res.Res.HYST[1:0]Res.ITENRes.WINMODEPOLARITYSCALENBRGENEN
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_CFGR2[31:0] register is read/write

1: COMP_CFGR2[31:0] is read-only

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

Bits 27:24 BLANKING[3:0]: COMP2 channel blanking source selection

Bits of this field are set and cleared by software (only if LOCK not set).

The field selects the input source for COMP1 channel output blanking.

Note: See Table 173: COMP blanking source assignment for more details.

Bit 23 Reserved, must be kept at reset value.

Bits 22:20 INPSEL[2:0]: COMP2 non-inverting input selection

These bit are set and cleared by software (only if LOCK not set).

They select which input is connected to the non-inverting input of COMP2 channel.

Note: See Table 169: COMP implementation non-inverting input assignment for STM32C53x/542 for more details.

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

23.8.5 COMP register map

Table 178. 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.CC2IFCC1IFRes.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.INPSEL [2:0]INMSEL [3:0]Res.WINMOUTPWRRMODE [1:0]Res.Res.HYST[1:0]Res.ITENRes.WINMODEPOLARITYSCALENBRGENEN
Reset value0000000000000000000000000
0x10COMP_CFGR2LOCKRes.Res.Res.BLANKING [3:0]Res.INPSEL [2:0]INMSEL [3:0]Res.WINMOUTPWRRMODE [1:0]Res.Res.HYST[1:0]Res.ITENRes.WINMODEPOLARITYSCALENBRGENEN
Reset value0000000000000000000000000

Refer to Section 2.2: Memory organization for the register boundary addresses.