15. Comparator (COMP)

15.1 Introduction

STM32F37xxx devices embed two general purpose comparators COMP1 and COMP2, that can be used either as standalone devices (all terminals are available on I/Os) or combined with the timers.

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

15.2 COMP main features

15.3 COMP functional description

15.3.1 COMP block diagram

The block diagram of the comparators is shown in Figure 52: Comparator 1 and 2 block diagrams .

Figure 52. Comparator 1 and 2 block diagrams

Figure 52. Comparator 1 and 2 block diagrams. The diagram shows two comparators, COMP1 and COMP2. COMP1 has inputs COMP1_INP+ and COMP1_INM-. COMP1_INP+ is connected to PA1 and a multiplexer. COMP1_INM- is connected to a multiplexer. The multiplexer for COMP1 has inputs from PA0, PA4 (DAC1_OUT1), PA5 (DAC1_OUT2), PA6 (DAC2_OUT1), VREFINT, 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT. The output of COMP1 is COMP1_OUT, which is connected to PA0/PA6/PA11/PB8. COMP1 also has a 'Polarity selection' block and generates a 'COMP interrupt request (to EXTI)'. The interrupt request is connected to TIM5_IC4, TIM5_OCref_clr, TIM15_BKIN, TIM2_IC4, TIM2_OCref_clr, TIM3_IC1, and TIM3_OCref_clr. COMP2 has inputs COMP2_INP+ and COMP2_INM-. COMP2_INP+ is connected to PA3 and a 'Window mode' block. COMP2_INM- is connected to a multiplexer. The multiplexer for COMP2 has inputs from PA2, PA4 (DAC1_OUT1), PA5 (DAC1_OUT2), PA6 (DAC2_OUT1), VREFINT, 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT. The output of COMP2 is COMP2_OUT, which is connected to PA2/PA7/PA12/PB9. COMP2 also has a 'Polarity selection' block and generates a 'COMP interrupt request (to EXTI)'. The interrupt request is connected to TIM4_IC1, TIM4_OCref_clr, TIM16_BKIN, TIM2_IC4, TIM2_OCref_clr, TIM3_IC1, and TIM3_OCref_clr. The diagram is labeled MS19986V3.
Figure 52. Comparator 1 and 2 block diagrams. The diagram shows two comparators, COMP1 and COMP2. COMP1 has inputs COMP1_INP+ and COMP1_INM-. COMP1_INP+ is connected to PA1 and a multiplexer. COMP1_INM- is connected to a multiplexer. The multiplexer for COMP1 has inputs from PA0, PA4 (DAC1_OUT1), PA5 (DAC1_OUT2), PA6 (DAC2_OUT1), VREFINT, 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT. The output of COMP1 is COMP1_OUT, which is connected to PA0/PA6/PA11/PB8. COMP1 also has a 'Polarity selection' block and generates a 'COMP interrupt request (to EXTI)'. The interrupt request is connected to TIM5_IC4, TIM5_OCref_clr, TIM15_BKIN, TIM2_IC4, TIM2_OCref_clr, TIM3_IC1, and TIM3_OCref_clr. COMP2 has inputs COMP2_INP+ and COMP2_INM-. COMP2_INP+ is connected to PA3 and a 'Window mode' block. COMP2_INM- is connected to a multiplexer. The multiplexer for COMP2 has inputs from PA2, PA4 (DAC1_OUT1), PA5 (DAC1_OUT2), PA6 (DAC2_OUT1), VREFINT, 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT. The output of COMP2 is COMP2_OUT, which is connected to PA2/PA7/PA12/PB9. COMP2 also has a 'Polarity selection' block and generates a 'COMP interrupt request (to EXTI)'. The interrupt request is connected to TIM4_IC1, TIM4_OCref_clr, TIM16_BKIN, TIM2_IC4, TIM2_OCref_clr, TIM3_IC1, and TIM3_OCref_clr. The diagram is labeled MS19986V3.

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

15.3.3 COMP reset and clocks

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

There is no clock enable control bit provided in the RCC controller.

The clock enable bit is common for both COMP and SYSCFG. However, COMP is only reset by the system reset.

Important: The polarity selection logic and the output redirection to the port works independently from the PCLK clock. This allows the comparator to work even in Stop mode.

15.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, using bits 30:16 and 15:0 of COMP_CSR, the COMPx LOCK bit can be set to 1. This causes the whole COMP_CSR register to become read-only, including the COMPx LOCK bit.

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

15.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) to be able to force the hysteresis value using external components.

Figure 53. Comparator hysteresis

Figure 53. Comparator hysteresis. A 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 - V_hyst level. The COMP_OUT signal is a digital output that switches between high and low states based on the comparison of INP and INM. The output transitions occur at the points where the INP signal crosses the INM and INM - V_hyst levels.

The figure illustrates the hysteresis effect in a comparator. The top graph shows the non-inverting input (INP) as a sinusoidal-like waveform. The inverting input (INM) is a constant reference voltage, represented by a dashed horizontal line. A second dashed horizontal line, labeled INM - V hyst , represents the lower threshold for the hysteresis. The bottom graph shows the output (COMP_OUT) as a digital signal. The output is high when INP > INM and low when INP < INM - V hyst . The output transitions between high and low states at the points where the INP signal crosses the INM and INM - V hyst levels, as indicated by vertical dashed lines. The hysteresis width is the voltage difference between INM and INM - V hyst . The text 'MS19984V1' is visible in the bottom right corner of the graph area.

Figure 53. Comparator hysteresis. A 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 - V_hyst level. The COMP_OUT signal is a digital output that switches between high and low states based on the comparison of INP and INM. The output transitions occur at the points where the INP signal crosses the INM and INM - V_hyst levels.

15.3.6 Power mode

The comparator power consumption versus propagation delay can be adjusted to have the optimum trade-off for a given application. The bits COMPxMODE[1:0] in COMP_CSR register can be programmed as follows:

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

15.5 COMP registers

15.5.1 COMP control and status register (COMP_CSR)

Address offset: 0x1C

Reset value: 0x0000 0000

31302928272625242322212019181716
COMP2LOCKCOMP2OUTCOMP2HYST [1:0]COMP2POLCOMP2OUTSEL[2:0]WNDW ENCOMP2INSEL[2:0]COMP2MODE [1:0]Res.COMP2 EN
rworrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/r

1514131211109876543210
COMP1LOCKCOMP1OUTCOMP1HYST [1:0]COMP1POLCOMP1OUTSEL[2:0]Res.COMP1INSEL[2:0]COMP1MODE [1:0]COMP1 _INP_D ACCOMP1 EN
rworrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/r
Bit 31 COMP2LOCK : Comparator 2 lock

This bit is write-once. It is set by software. It can only be cleared by a system reset.
It allows to have all control bits of comparator 2 as read-only.

0: COMP_CSR[31:16] bits are read-write.

1: COMP_CSR[31:16] bits are read-only.

Bit 30 COMP2OUT : Comparator 2 output

This read-only bit is a copy of comparator 2 output state.

0: Output is low (non-inverting input below inverting input).

1: Output is high (non-inverting input above inverting input).

Bits 29:28 COMP2HYST[1:0] : Comparator 2 hysteresis

These bits control the hysteresis level.

00: No hysteresis

01: Low hysteresis

10: Medium hysteresis

11: High hysteresis

Please refer to the electrical characteristics for the hysteresis values.

Bit 27 COMP2POL : Comparator 2 output polarity

This bit is used to invert the comparator 2 output.

0: Output is not inverted

1: Output is inverted

Bits 26:24 COMP2OUTSEL[2:0] : Comparator 2 output selection

These bits select the destination of the comparator output.

000: No selection

001: Timer 16 break input

010: Timer 4 Input capture 1

011: Timer 4 OCrefclear input

100: Timer 2 input capture 4

101: Timer 2 OCrefclear input

110: Timer 3 input capture 1

111: Timer 3 OCrefclear input

Bit 23 WNDWEN : Window mode enable

This bit connects the non-inverting input of COMP2 to COMP1's non-inverting input, which is simultaneously disconnected from PA3.

0: Window mode disabled

1: Window mode enabled

Bits 22:20 COMP2INSEL[2:0] : Comparator 2 inverting input selection

These bits allows to select the source connected to the inverting input of the comparator 2.

000: 1/4 of Vrefint

001: 1/2 of Vrefint

010: 3/4 of Vrefint

011: Vrefint

100: DAC1_OUT1 output (and PA4 output)

101: Input from PA5 (and DAC1_OUT2 output)

110: Input from PA2

111: Input from PA6 (and DAC2_OUT1 output)

Bits 19:18 COMP2MODE[1:0] : Comparator 2 mode

These bits control the operating mode of the comparator2 and allows to adjust the speed/consumption.

00: High speed/

01: Medium speed

10: Low-power

11: Ultra-low-power

Bit 17 Reserved, must be kept at reset value.

Bit 16 COMP2EN : Comparator 2 enable

This bit switches ON/OFF the comparator2.

0: Comparator 2 disabled

1: Comparator 2 enabled

Bit 15 COMP1LOCK : Comparator 1 lock

This bit is write-once. It is set by software. It can only be cleared by a system reset.

It allows to have all control bits of comparator 1 as read-only.

0: COMP_CSR[15:0] bits are read-write.

1: COMP_CSR[15:0] bits are read-only.

Bit 14 COMP1OUT : Comparator 1 output

This read-only bit is a copy of comparator 1 output state.

0: Output is low (non-inverting input below inverting input).

1: Output is high (non-inverting input above inverting input).

Bits 13:12 COMP1HYST[1:0] Comparator 1 hysteresis

These bits are controlling the hysteresis level.

00: No hysteresis

01: Low hysteresis

10: Medium hysteresis

11: High hysteresis

Please refer to the electrical characteristics for the hysteresis values.

Bit 11 COMP1POL : Comparator 1 output polarity

This bit is used to invert the comparator 1 output.

0: output is not inverted

1: output is inverted

Bits 10:8 COMP1OUTSEL[2:0] : Comparator 1 output selection

These bits selects the destination of the comparator 1 output.

000: no selection

001: Timer 15 break input

010: Timer 3 Input capture 1

011: Timer 3 OCrefclear input

100: Timer 2 input capture 4

101: Timer 2 OCrefclear input

110: Timer 5 input capture 4

111: Timer 5 OCrefclear input

Bit 7 Reserved, must be kept at reset value.

Bits 6:4 COMP1INSEL[2:0] : Comparator 1 inverting input selection

These bits select the source connected to the inverting input of the comparator 1.

000: 1/4 of Vrefint

001: 1/2 of Vrefint

010: 3/4 of Vrefint

011: Vrefint

100: DAC1_OUT1 output (and PA4)

101: Input from PA5 (and DAC1_OUT2 output)

110: Input from PA0

111: Input from PA6 (and DAC2_OUT1 output)

Bits 3:2 COMP1MODE[1:0] : Comparator 1 mode

These bits control the operating mode of the comparator1 and allows to adjust the speed/consumption.

00: High speed / full power

01: Medium speed / medium power

10: Low speed / low-power

11: Very-low speed / ultra-low-power

Bit 1 COMP1_INP_DAC : Comparator 1 non-inverting input connection to DAC output.

This bit closes a switch between comparator 1 non-inverting input on PA0 and PA4 (DAC) I/O.

0: Switch open

1: Switch closed

Note: This switch is solely intended to redirect signals onto high impedance input, such as COMP1 non-inverting input (highly resistive switch).

Bit 0 COMP1EN : Comparator 1 enable

This bit switches COMP1 ON/OFF.

0: Comparator 1 disabled

1: Comparator 1 enabled

15.5.2 COMP register map

The following table summarizes the comparator registers.

Table 43. COMP register map and reset values

OffsetRegister313029282726252423222120191817161514131211109876543210
0x1CCOMP_CSRCOMP2LOCKCOMP2OUTCOMP2HYST1[1:0]COMP2POLCOMP2OUTSEL[2:0]WNDWENCOMP2INSEL[2:0]COMP2MODE1[1:0]ResCOMP2ENCOMP1LOCKCOMP1OUTCOMP1HYST1[1:0]COMP1POLCOMP1OUTSEL[2:0]ResCOMP1INSEL[2:0]COMP1MODE1[1:0]COMP1_INP_DACCOMP1EN
Reset value0000000000000000000000000000000

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