22. Comparator (COMP)

22.1 Introduction

The device embeds an ultra-low-power comparator. It can be used for a variety of functions including:

22.2 COMP main features

22.3 COMP functional description

22.3.1 COMP block diagram

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

Figure 143. Comparator functional block diagram

Figure 143: Comparator functional block diagram showing multiplexer inputs INPSEL[0..2] and INMSEL[3:0] selecting signals into the COMP comparator block with HYST and PWRMODE controls. The output passes through polarity selection, blanking logic, to GPIO alternate function (COMPx_OUT), EXTI line, timers, and an interrupt generator with DFF producing compx_it.
Figure 143: Comparator functional block diagram showing multiplexer inputs INPSEL[0..2] and INMSEL[3:0] selecting signals into the COMP comparator block with HYST and PWRMODE controls. The output passes through polarity selection, blanking logic, to GPIO alternate function (COMPx_OUT), EXTI line, timers, and an interrupt generator with DFF producing compx_it.

22.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 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 routed in a simultaneous way internally and to the I/O pins.

Table 140. COMP1 input/output internal signals

Signal nameSignal typeDescription
dac1_out1Analog inputAnalog signal from DAC peripheral
comp1_blkxDigital outputSee Table 141
comp1_outDigital outputCOMP1 output
comp1_itDigital outputCOMP1 interrupt out

Table 141. COMP1 blanking source selection

Signal nameSignal typeBlanking input source
comp1_blk1Digital inputtim1_oc5
comp1_blk2Digital inputtim2_oc3
comp1_blk3Digital inputtim3_oc3
comp1_blk4Digital inputtim3_oc4
comp1_blk5Digital inputlptim1_ch2
comp1_blk6Digital inputlptim2_ch2

Table 142. COMP1 noninverting input assignment

COMP1_INPCOMP1_INPSEL2COMP1_INPSEL1COMP1_INPSEL0
COMP1_INP1XX1
COMP1_INP2000
COMP1_INP3010
dac1_out11X0

Note: Other combinations are reserved.

Table 143. COMP1 inverting input assignment

COMP1_INMCOMP1_INMSEL[3:0]
\(\frac{1}{4} V_{REFINT}\)0000
\(\frac{1}{2} V_{REFINT}\)0001
\(\frac{3}{4} V_{REFINT}\)0010
\(V_{REFINT}\)0011
dac1_out10100
COMP1_INM10101
COMP1_INM20110
COMP1_INM30111
\(V_{SENSE}\)1000
\(V_{BAT}/4\)1001

Note: Other combinations are reserved.

Table 144. COMP1 blanking source assignment

Banking sourceCOMP1_BLANKING[3:0]
No blanking0000
tim1_oc50001
tim2_oc30010

Table 144. COMP1 blanking source assignment (continued)

Banking sourceCOMP1_BLANKING[3:0]
tim3_oc30011
tim3_oc40100
lptim1_ch20101
lptim2_ch20110

22.3.3 COMP reset and clocks

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

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

22.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 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 whole register set of the comparator channel to become read-only, the LOCK bit inclusive.

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

22.3.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 from low-power mode. This enables the possibility to force the hysteresis value using external components.

Figure 144. Comparator hysteresis

Figure 144. Comparator hysteresis diagram. The diagram shows two waveforms: INP (input) and COMP_OUT (output). The INP waveform is a noisy signal oscillating around a threshold level INM. The COMP_OUT waveform is a digital signal that transitions between high and low states. The hysteresis is represented by two horizontal dashed lines: the upper threshold is INM and the lower threshold is INM - V_hyst. The output transitions occur when the input crosses these thresholds. The output remains high until the input falls below INM - V_hyst, and it remains low until the input rises above INM. The diagram illustrates how hysteresis prevents multiple output transitions caused by noise on the input signal.

MS19984V1

Figure 144. Comparator hysteresis diagram. The diagram shows two waveforms: INP (input) and COMP_OUT (output). The INP waveform is a noisy signal oscillating around a threshold level INM. The COMP_OUT waveform is a digital signal that transitions between high and low states. The hysteresis is represented by two horizontal dashed lines: the upper threshold is INM and the lower threshold is INM - V_hyst. The output transitions occur when the input crosses these thresholds. The output remains high until the input falls below INM - V_hyst, and it remains low until the input rises above INM. The diagram illustrates how hysteresis prevents multiple output transitions caused by noise on the input signal.

22.3.6 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 antiparallel 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 145. Comparator output blanking

Figure 145: 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 periodic 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 sharp upward spikes at the beginning of each PWM period. The cmp_out (before blanking gate) shows pulses corresponding to when the non-inverting input crosses the inverting input. The cmp_blk signal is a square wave that is high during the blanking window at the start of each PWM period. The final comp_out (COMP output) is the result of a blanking gate (AND gate with inverted cmp_blk input), which suppresses the spurious pulses in the cmp_out that occur during the cmp_blk high period. A legend at the bottom shows the blanking gate logic: cmp_out and cmp_blk (inverted) are inputs to an AND gate, producing the final comp_out (to I/Os, TIM_BK ...).

MS30964V2

Figure 145: 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 periodic 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 sharp upward spikes at the beginning of each PWM period. The cmp_out (before blanking gate) shows pulses corresponding to when the non-inverting input crosses the inverting input. The cmp_blk signal is a square wave that is high during the blanking window at the start of each PWM period. The final comp_out (COMP output) is the result of a blanking gate (AND gate with inverted cmp_blk input), which suppresses the spurious pulses in the cmp_out that occur during the cmp_blk high period. A legend at the bottom shows the blanking gate logic: cmp_out and cmp_blk (inverted) are inputs to an AND gate, producing the final comp_out (to I/Os, TIM_BK ...).

22.3.7 COMP startup stabilization time

The comparator parameters and its output are undefined after the COMP enable (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 the IP configuration (such as timers) after the stabilization time elapses.

22.3.8 COMP power and speed modes

The power consumption of the COMP channels versus propagation delay can be adjusted to have the optimum trade-off for a given application.

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

22.4 COMP low-power modes

Table 145. 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 mode or stop mode when the internal reference voltage is switched off.

22.5 COMP interrupts

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

22.5.1 Interrupt through EXTI

Refer to the interrupt and events section for more details.

Sequence to enable the COMP interrupt through the EXTI block:

  1. 1. Enable the COMP and wait for stabilization.
  2. 2. Configure the EXTI line, receiving the comp_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 146. Interrupt through EXTI control bits

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

22.5.2 Interrupt through the NVIC of the CPU

Sequence to enable the COMP interrupt through the 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 147. Interrupt through NVIC control bits

Interrupt eventInterrupt flagEnable control bitInterrupt clear bitExit Sleep modeExit Stop modes
comp1_itC1IF in COMP_CFGR1ITEN in COMP_CFGR1CC1IFYes
(with APB clock)
No

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

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

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 can be disconnected in order to reduce the consumption. When it is disconnected, the \(\frac{1}{4} V_{REF\_COMP}\), \(\frac{1}{2} V_{REF\_COMP}\), and \(\frac{3}{4} V_{REF\_COMP}\) levels are equal to \(V_{REF\_COMP}\).

Figure 146. Scaler block diagram

Scaler block diagram showing an operational amplifier with feedback resistors and control inputs.

The diagram illustrates the Scaler block. It features an operational amplifier (op-amp) with a feedback network. The non-inverting input (+) is connected to a reference voltage \(V_{REFINT}\). The inverting input (-) is connected to a feedback node. The output of the op-amp is connected to a series of four resistors. The output of the first resistor is \(V_{REF\_COMP}\). The output of the second resistor is \(3/4 V_{REF\_COMP}\). The output of the third resistor is \(1/2 V_{REF\_COMP}\). The output of the fourth resistor is \(1/4 V_{REF\_COMP}\). A control signal BRGEN is connected to a switch that can connect the bottom of the resistor chain to ground. The input SCALEN is connected to the non-inverting input of the op-amp. The entire block is enclosed in a dashed box. The reference MSv70601V1 is located in the bottom right corner.

Scaler block diagram showing an operational amplifier with feedback resistors and control inputs.

22.7 COMP registers

22.7.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.Res.C1IF
r
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.C1VAL
r

Bits 31:17 Reserved, must be kept at reset value.

Bit 16 C1IF: COMP Channel1 interrupt flag

This bit is set by hardware when the COMP Channel1 output is set

This bit is cleared by software writing 1 the CC1IF bit in the COMP_ICFR register.

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

Bit 0 C1VAL: COMP Channel1 output status bit

This bit is read-only. It reflects the current COMP Channel1 output taking into account POLARITY and BLANKING bits effect.

22.7.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.Res.CC1IF
rc_w1
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.

Bits 31:17 Reserved, must be kept at reset value.

Bit 16 CC1IF: Clear COMP Channel1 interrupt flag

Writing 1 clears the C1IF flag in the COMP_SR register.

Bits 15:0 Reserved, must be kept at reset value.

22.7.3 Comparator configuration register 1 (COMP_CFGR1)

The COMP_CFGR1 is the COMP configuration register.

Address offset: 0x0C

Reset value: 0x0000 0000

31302928272625242322212019181716
LOCKRes.Res.Res.BLANKING[3:0]Res.INPSE L2Res.INPSE L1INMSEL[3:0]
rsrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
Res.Res.PWRMODE[1:0]Res.Res.HYST[1:0]Res.ITENRes.Res.POLARITYSCALE NBRGENEN
rwrwrwrwrwrwrwrwrw

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 Channel1 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 5:4 Reserved, must be kept at reset value.

Bit 3 POLARITY: COMP channel1 polarity selection

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

0: COMP channel1 output is not inverted

1: COMP Channel1 output is inverted

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

If SCALEN is set and BRGEN is reset, all four scaler outputs provide the same level \(V_{REF\_COMP}\) (similar to \(V_{REFINT}\)).

If SCALEN and BRGEN are set, the four scaler outputs provide \(V_{REF\_COMP}\), \(3/4 V_{REF\_COMP}\), \(1/2 V_{REF\_COMP}\) and \(1/4 V_{REF\_COMP}\) levels, respectively.

Bit 0 EN: COMP Channel1 enable

This bit is set and cleared by software (only if LOCK not set). It enables the COMP Channel1.

0: Disable

1: Enable

22.7.4 Comparator configuration register 2 (COMP_CFGR2)

The COMP_CFGR2 is the COMP configuration register.

Address offset: 0x10

Reset value: 0x0000 0000

31302928272625242322212019181716
LOCKRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
rs
1514131211109876543210
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.INPSEL0Res.Res.Res.Res.
rw

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 Channel1 configuration register COMP_CFGR2[31:0]

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

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

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

Bit 4 INPSEL0: COMP non-inverting input selection

This bit is set and cleared by software (only if LOCK not set). They select which input is connected to the positive input of COMP channel.

See Table 142: COMP1 noninverting input assignment for more details.

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

22.7.5 COMP register map

Table 148. COMP register map and reset values

OffsetRegister name313029282726252423222120191817161514131211109876543210
0x00COMP_SRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.C1IFRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.C1VAL
Reset value00
0x04COMP_ICFRRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.CC1IFRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
Reset value0
0x0CCOMP_CFGR1LOCKRes.Res.Res.Res.BLANKING [3:0]Res..INPSEL2Res.INPSEL1INMSEL [3:0]Res.PWRMODE [1:0]Res.Res.HYST[1:0]Res.Res.ITENRes.Res.Res.POLARITYSCALENBRGENEN
Reset value0000000000000000000
0x10COMP_CFGR2LOCKRes.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.INPSEL0Res.Res.Res.Res.Res.
Reset value00

Refer to Section 2.2 for the register boundary addresses.