24. Comparator (COMP)

24.1 COMP introduction

The device embeds up to seven ultra-fast analog comparators.

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

24.2 COMP main features

24.3 COMP functional description

24.3.1 COMP block diagram

The block diagram of one comparator channel front-end is shown in Figure 168: Comparator block diagram .

Figure 168. Comparator block diagram

Figure 168. Comparator block diagram. The diagram shows a comparator (COMPx) with two inputs: COMPx_INP and COMPx_INM. The COMPx_INP input is selected via a multiplexer (INPSEL) from COMPx_INP I/Os. The COMPx_INM input is selected via a multiplexer (INMSEL) from a list of sources: COMPx_INM I/Os, DACx_OUTy, VREFINT, 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT. The comparator output (VALUE) is connected to a polarity selection block (POLARITY) which includes a buffer and an inverter. The output can be redirected to a GPIO alternate function (COMPx_OUT), a Wakeup EXTI line interrupt, TIMx, or HRTIM. The comparator also has a hysteresis (HYST) input.
Figure 168. Comparator block diagram. The diagram shows a comparator (COMPx) with two inputs: COMPx_INP and COMPx_INM. The COMPx_INP input is selected via a multiplexer (INPSEL) from COMPx_INP I/Os. The COMPx_INM input is selected via a multiplexer (INMSEL) from a list of sources: COMPx_INM I/Os, DACx_OUTy, VREFINT, 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT. The comparator output (VALUE) is connected to a polarity selection block (POLARITY) which includes a buffer and an inverter. The output can be redirected to a GPIO alternate function (COMPx_OUT), a Wakeup EXTI line interrupt, TIMx, or HRTIM. The comparator also has a hysteresis (HYST) input.

24.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 199. COMPx non-inverting input assignment

INPSELCOMP1_INP_COMP2_INP_COMP3_INP_COMP4_INP_COMP5_INP_COMP6_INP_COMP7_INP_
0PA1PA7PA0PB0PB13PB11PB14
1PB1PA3PC1PE7PD12PD11PD14

Table 200. COMPx inverting input assignment

INMSEL [2:0]COMP1_INM_COMP2_INM_COMP3_INM_COMP4_INM_COMP5_INM_COMP6_INM_COMP7_INM_
0001/4 V REFINT
0011/2 V REFINT
0103/4 V REFINT
011V REFINT
100DAC3_CH1DAC3_CH2DAC3_CH1DAC3_CH2DAC4_CH1DAC4_CH2DAC4_CH1
101DAC1_CH1DAC1_CH2DAC1_CH1DAC1_CH1DAC1_CH2DAC2_CH1DAC2_CH1
110PA4PA5PF1PE8PB10PD10PD15
111PA0PA2PC0PB2PD13PB15PB12

24.3.3 COMP reset and clocks

The COMP clock provided by the clock controller is synchronous with the APB2 clock.

There is no COMP-dedicated clock enable control bit 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 of APB clock. This allows the comparator to work even in Stop mode.

24.3.4 COMP 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. This causes the whole register to become read-only, including the COMPx LOCK bit.

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

24.3.5 COMP hysteresis

The comparator includes a programmable hysteresis to avoid spurious output transitions with noisy input signals. It is non-symmetrical and only acting to falling edge of the comparator output. The internal hysteresis function can be disabled so as to set the amount of hysteresis with external components, which can be useful for example when exiting a low-power mode.

Figure 169. Comparator hysteresis

Waveform diagram showing comparator hysteresis. The top plot shows a sinusoidal input signal INP crossing a reference level INM. A second lower reference level is labeled INM - Vhyst. The bottom plot shows the COMP_OUT signal. COMP_OUT goes high when INP rises above INM. COMP_OUT goes low only when INP falls below the lower threshold INM - Vhyst, demonstrating non-symmetrical hysteresis on the falling edge.

MS19984V1

Waveform diagram showing comparator hysteresis. The top plot shows a sinusoidal input signal INP crossing a reference level INM. A second lower reference level is labeled INM - Vhyst. The bottom plot shows the COMP_OUT signal. COMP_OUT goes high when INP rises above INM. COMP_OUT goes low only when INP falls below the lower threshold INM - Vhyst, demonstrating non-symmetrical hysteresis on the falling edge.

24.3.6 COMP output blanking

The purpose of the blanking function is to prevent the current regulation from tripping upon short current spikes at the beginning of PWM period (typically the recovery current in power switch anti-parallel diodes). This goes through setting a dead window defined with a timer output compare signal. The blanking source is selected individually per comparator channel by software through BLANKSEL[2:0] bitfield of corresponding COMP_CxCSR register, as shown in Table 201: Blanking sources . The inverted blanking signal is logical AND-ed with the comparator stage output to produce the comparator channel x output. See the example provided in the following figure.

Figure 170. Comparator output blanking

Timing diagram showing PWM, Current limit, Current, Raw comp output, Blanking window, and Final comp output. It includes an AND gate logic diagram at the bottom with inputs 'Comp out' and 'Blank' (inverted).

The diagram illustrates the timing relationship between a PWM signal, current limit, actual current, raw comparator output, a blanking window, and the final comparator output. The PWM signal is a square wave. The current limit is a dashed horizontal line. The current is a sawtooth-like waveform that rises when PWM is high and falls when PWM is low, bounded by the current limit. 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 PWM. The final comp output is the result of an AND gate with inputs from the raw comp output and the inverted blanking window. The logic diagram at the bottom shows 'Comp out' and 'Blank' (with an inversion circle) as inputs to an AND gate, with the output labeled 'Comp out (to TIM_BK ...)'.

Timing diagram showing PWM, Current limit, Current, Raw comp output, Blanking window, and Final comp output. It includes an AND gate logic diagram at the bottom with inputs 'Comp out' and 'Blank' (inverted).

MS30964V1

Table 201. Blanking sources

BLANKSEL [2:0]COMP1COMP2COMP3COMP4COMP5COMP6COMP7
001TIM1_OC5TIM1_OC5TIM1_OC5TIM3_OC4TIM2_OC3TIM8_OC5TIM1_OC5
010TIM2_OC3TIM2_OC3TIM3_OC3TIM8_OC5TIM8_OC5TIM2_OC4TIM8_OC5
011TIM3_OC3TIM3_OC3TIM2_OC4TIM15_OC1TIM3_OC3TIM15_OC2TIM3_OC3
100TIM8_OC5TIM8_OC5TIM8_OC5TIM1_OC5TIM1_OC5TIM1_OC5TIM15_OC2
101TIM20_OC5TIM20_OC5TIM20_OC5TIM20_OC5TIM20_OC5TIM20_OC5TIM20_OC5
110TIM15_OC1TIM15_OC1TIM15_OC1TIM15_OC1TIM15_OC1TIM15_OC1TIM15_OC1
111TIM4_OC3TIM4_OC3TIM4_OC3TIM4_OC3TIM4_OC3TIM4_OC3TIM4_OC3

24.4 COMP low-power modes

Table 202. Comparator behavior in low-power modes

ModeDescription
SleepNo effect on the comparators.
Comparator interrupts cause the device to exit the Sleep mode.
Low-power runNo effect.

Table 202. Comparator behavior in low-power modes (continued)

ModeDescription
Low-power sleepNo effect. COMP interrupts cause the device to exit the Low-power sleep mode.
StopNo effect on the comparators.
Comparator interrupts cause the device to exit the Stop mode.
Standby, ShutdownThe COMP registers are powered down and must be reinitialized after exiting Standby or Shutdown mode.

24.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 Sleep and Stop low-power modes.

Refer to Interrupt and events section for more details.

To enable the 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 sensitivity to rising edge, falling edge or to both edges
  2. 2. Configure and enable the NVIC IRQ channel mapped to the corresponding EXTI lines
  3. 3. Enable COMPx

Interrupt events are flagged through flags in EXTI_PR1/EXTI_PR2 registers.

24.6 COMP registers

24.6.1 Comparator x control and status register (COMP_CxCSR)

For x = 1 through 7, the COMP_CxCSR register contains all bits and flags related to the comparator x.

Address offset: 4(x-1), where x = 1 to 7

System reset value: 0x0000 0000

31302928272625242322212019181716
LOCKVALUERes.Res.Res.Res.Res.Res.SCALENBRGENBLANKSEL[2:0]HYST[2:0]
rwrrwrwrwrwrwrwrwrw

1514131211109876543210
POLRes.Res.Res.Res.Res.Res.INPSELRes.INMSEL[2:0]Res.Res.Res.EN
rwrwrwrwrwrw
Bit 31 LOCK: COMP_CxCSR register lock

This bit is set by software and cleared by a hardware system reset. It locks the whole content of the comparator x control register COMP_CxCSR[31:0]. When locked, all control bits and flags can be read only but not written. When unlocked, the control bits can also be written by software.

0: Unlock

1: Lock

Bit 30 VALUE: Comparator x output status

This read-only flag reflects the level of the comparator x output before the polarity selector and blanking, as indicated in Figure 168 .

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

Bit 23 SCALEN: V REFINT scaler enable

This bit controlled by software enables the operation of V REFINT scaler at the inverting input of all comparator. To disable the V REFINT scaler, SCALEN bits of all COMP_CxCSR registers must be set to Disable state. When the V REFINT scaler is disabled, the 1/4 V REFINT , 1/2 V REFINT , 3/4 V REFINT and V REFINT inputs of the multiplexer should not be selected.

0: Disable

1: Enable

Bit 22 BRGEN: V REFINT scaler resistor bridge enable

This bit controlled by software enables the operation of resistor bridge in the V REFINT scaler. To disable the resistor bridge, BRGEN bits of all COMP_CxCSR registers must be set to Disable state. When the resistor bridge is disabled, the 1/4 V REFINT , 1/2 V REFINT , and 3/4 V REFINT inputs of the input selector receive V REFINT voltage.

0: Disable

1: Enable

Bits 21:19 BLANKSEL[2:0]: Comparator x blanking signal select

This bitfield controlled by software selects the blanking signal for comparator channel x, as shown in Table 201: Blanking sources .

Bits 18:16 HYST[2:0]: Comparator x hysteresis

This bitfield controlled by software selects the hysteresis of the comparator x:

000: No hysteresis

001: 10mV hysteresis

010: 20mV hysteresis

011: 30mV hysteresis

100: 40mV hysteresis

101: 50mV hysteresis

110: 60mV hysteresis

111: 70mV hysteresis

Bit 15 POL: Comparator x polarity

This bit controlled by software selects the comparator x output polarity:

0: Non-inverted

1: Inverted

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

Bit 8 INPSEL: Comparator x signal select for non-inverting input

This bitfield controlled by software selects the signal for the non-inverting input COMPx_INP of the comparator x, as shown in Table 199: COMPx non-inverting input assignment .

Bit 7 Reserved, must be kept at reset value

Bits 6:4 INMSEL[2:0] : Comparator x signal select for inverting input

This bitfield controlled by software selects the signal for the inverting input COMPx_INM of the comparator x, as shown in Table 200: COMPx inverting input assignment .

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

Bit 0 EN : Comparator x enable

This bit controlled by software enables the operation of comparator x:

0: Disable

1: Enable

24.6.2 COMP register map

The following table summarizes the comparator registers.

The comparator registers share SYSCFG peripheral register base addresses.

Table 203. COMP register map and reset values

OffsetRegister313029282726252423222120191817161514131211109876543210
0x00COMP_C1CSRLOCKVALUEResResResResResResSCALENBRGENBLANKSEL[2:0]HYST[2:0]POLResResResResResResINPSELResINMSEL[2:0]ResResResEN
Reset value0000000000000000
0x04COMP_C2CSRLOCKVALUEResResResResResResSCALENBRGENBLANKSEL[2:0]HYST[2:0]POLResResResResResResINPSELResINMSEL[2:0]ResResResEN
Reset value0000000000000000
0x08COMP_C3CSRLOCKVALUEResResResResResResSCALENBRGENBLANKSEL[2:0]HYST[2:0]POLResResResResResResINPSELResINMSEL[2:0]ResResResEN
Reset value0000000000000000
0x0CCOMP_C4CSRLOCKVALUEResResResResResResSCALENBRGENBLANKSEL[2:0]HYST[2:0]POLResResResResResResINPSELResINMSEL[2:0]ResResResEN
Reset value0000000000000000
0x10COMP_C5CSRLOCKVALUEResResResResResResSCALENBRGENBLANKSEL[2:0]HYST[2:0]POLResResResResResResINPSELResINMSEL[2:0]ResResResEN
Reset value0000000000000000

Table 203. COMP register map and reset values (continued)

OffsetRegister313029282726252423222120191817161514131211109876543210
0x14COMP_C6CSRLOCKVALUERes.Res.Res.Res.Res.Res.SCALENBRGENBLANKSEL[2:0]HYST[2:0]POLRes.Res.Res.Res.Res.Res.INPSEL.Res.INMSEL[2:0]Res.Res.Res.EN
Reset value0000000000000000
0x18COMP_C7CSRLOCKVALUERes.Res.Res.Res.Res.Res.SCALENBRGENBLANKSEL[2:0]HYST[2:0]POLRes.Res.Res.Res.Res.Res.INPSEL.Res.INMSEL[2:0]Res.Res.Res.EN
Reset value0000000000000000

Refer to Section 2.2.2: Memory map and register boundary addresses for the register boundary addresses.