13. Comparator (COMP)
13.1 Introduction
The STM32WL33xx embeds one ultra-low power comparator COMP. The comparator and DAC are not available on STM32WL30xx and STM32WL31xx devices.
The comparator can be used for a variety of functions including.
- • Wake-up from low-power mode triggered by an analog signal,
- • Analog signal conditioning.
- • Cycle-by-cycle current control loop when combined with a PWM output from a timer.
13.2 COMP main features
- • The comparator has configurable plus and minus inputs used for flexible voltage selection:
- – Multiplexed I/O pins
- – Internal reference voltage and three submultiple values (1/4, 1/2, 3/4) provided by scaler (buffered voltage divider)
- – DAC output
- • Programmable hysteresis
- • Programmable speed / consumption
- • The outputs can be redirected to an I/O or to timer inputs for triggering:
- – Break events for fast PWM shutdown
- – Cycle-by-cycle current control, using OCREF_CLR_INT through ETR inputs
- – capture events
- • Comparator output with blanking source
- • The comparator has interrupt generation capability with wake-up from Sleep and Deepstop modes (through the PWR controller).
13.3 Comp functional description
13.3.1 COMP block diagram
Figure 25 shows the block diagram of the Comparator.
Figure 25. Comparator block diagram

The diagram illustrates the Comparator (COMP) block structure. It consists of the following components and signal paths:
- Input Selection: Two multiplexers select inputs for the comparator. The positive input (COMP_INP) is selected from PA14, PB1, or PB2 via the COMP_INPSEL register. The negative input (COMP_INM) is selected from a group including PB3, PB0, PA13, DACOUT_COMPMINUS, VREFINT, 3/4 VREFINT, 1/2 VREFINT, and 1/4 VREFINT via the COMP_INMSEL register.
- Comparator Core: A triangle labeled 'COMP' represents the core comparator, with '+' and '-' inputs connected to COMP_INP and COMP_INM respectively.
- Polarity Selection: A switch block labeled 'Polarity selection' follows the comparator core, allowing the output polarity to be inverted.
- Output Redirection: The COMP_OUT signal is split into two paths:
- GPIO alternate function: Redirects the output to PA4 / PB6 / PB15.
- WAKEUP: Redirects the output to the WAKEUP input.
- Timer Inputs: Redirects the output to TIM2_IC4, TIM2_ETRI, TIM16_IC1, and TIM16_BRK.
MS30478V2
13.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 “GPIOs alternate options” table.
The output can also be internally redirected to a variety of timer input for the following purpose:
- • Emergency shut-down of PWM signals, using BKIN input
- • Input capture for timing measures
It is possible to have the comparator output simultaneously redirected internally and externally.
13.3.3 COMP reset and clocks
The COMP clock provided by the clock controller is synchronous with the APB0 clock.
Note: Important: the polarity selection logic and the output redirection to the port works independently from the APB0 clock. This allows the comparator to work even in Deepstop mode.
13.3.4 Comparator LOCK mechanism
The comparator 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 (readonly).
Once the programming is completed, the COMP_LOCK bit can be set to 1. This causes the whole register to become read-only, including the COMP_LOCK bit.
The write protection can only be reset by a MCU reset.
13.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 26. Comparator hysteresis

Figure 26 illustrates the comparator hysteresis. The input signal (INP) is a sine wave. The comparator output (COMP_OUT) is a digital signal that transitions between high and low states based on the input signal's relationship to two thresholds: INM (upper threshold) and \(INM - V_{hyst}\) (lower threshold). The output transitions from low to high when the input crosses the upper threshold and from high to low when the input crosses the lower threshold, demonstrating hysteresis. The diagram is labeled MS19984V1 in the bottom right corner.
13.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 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 27. Comparator output blanking

13.3.7 COMP power and speed modes
COMP power consumption versus propagation delay can be adjusted to have the optimum trade-off for a given application.
The bits COMP_PWR_MODE[1:0] in COMP_CSR registers can be programmed as follows:
- 00: High speed / full power
- 01 or 10: Medium speed / medium power
- 11: Low speed / ultra-low-power
13.4 Comp low-power modes
The comparator is available and functional down to Deepstop mode. The scaler is available in Deepstop mode too. In Deepstop mode:
- • the comparator input channels (p and n) are connected to the selected IOs
- • the analog switches of the selected IOs are kept enable
- • the COMP_OUT (including polarity) can generate a system wakeup if the corresponding line is enabled and configured in the PWR
- • the configuration of the IOs, on which the COMP_OUT is mapped, is retained so the COMP_OUT's activity is propagated outside
- • when the scaler is enabled (SCALE_EN=1) the regulated core voltage (V12) is kept at 1.2 V and is not reduced to 1.0 V
Table 35. Comparator behavior in the low power modes
| MODE | Description |
|---|---|
| Deepstop | No effect on the comparator. Comparator registers status is retained. Comparator interrupt causes the device to wakeup the system and exit the Deepstop mode. |
| Shutdown | The COMP registers are powered down and must be reinitialized after exiting Shutdown mode |
13.5 Comp interrupts and wakeup
The comparator output is internally connected to the System Controller (SYSCFG). The comparator has its own INTAI line and can generate interrupts.
The comparator output is internally connected to the Power Controller (PWR). The comparator has its own wakeup line. This mechanism is used to exit from DeepStop mode.
To enable the COMP interrupt, it is required to follow this sequence:
- 1. Configure and enable the INTAI line corresponding to the COMP output event in the SYSCFG and select the rising, falling, or both edges sensitivity
- 2. Configure and enable the NVIC IRQ channel mapped to the corresponding INTAI line
- 3. Enable the COMP
To enable the COMP wakeup, it is required to follow this sequence:
- 1. Configure and enable the wakeup line corresponding to the COMP output event in the PWR and select polarity
- 2. Enable the COMP
13.6 Comp registers
13.6.1 Comparator control and status register (COMP_CSR)
Address offset: 0x00
Reset value: 0x0000 0000
| 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| LOCK | VALU E | Res. | Res. | Res. | Res. | Res. | Res. | SCALE N | BRGE N | Res. | BLANKING[2:0] | HYST[1:0] | |||
| rw | r | rw | rw | rw | rw | rw | rw | rw | |||||||
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
| POLAR ITY | Res. | Res. | Res. | Res. | Res. | Res. | INPSEL[1:0] | INMSEL[2:0] | PWRMODE[1:0] | Res. | EN | ||||
| rw | rw | rw | rw | rw | rw | rw | rw | rw | |||||||
Bit 31 LOCK: COMP_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 control register, COMP1_CSR[31:0].
0: COMP1_CSR[31:0] are read/write
1: COMP1_CSR[31:0] are read-only
Bit 30 VALUE: Comparator output status bit
This bit is read-only. It reflects the current comparator 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 VREFINT divider available on the minus input of the Comparator
0: scaler disable
1: 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
1: Scaler resistor bridge enable
If SCALEN is set and BRGEN is reset, BG voltage reference is available but not 1/4BGAP, 1/2BGAP, 3/4 BGAP. BGAP value is sent instead of 1/4BGAP, 1/2BGAP, 3/4 BGAP.
If SCALEN and BRGEN are set, 1/4 BGAP 1/2BGAP 3/4BGAP and BGAP voltage references are available.
Bit 21 Reserved, must be kept at reset value.
Bits 20:18 BLANKING[2:0]: Comparator blanking source selection bits
These bits select which timer output controls the comparator output blanking.
000: No blanking
001: TIM2 OC4 selected as blanking source
010: TIM16 OC1 selected as blanking source
All other values: reserved
Bits 17:16 HYST[1:0]: Comparator hysteresis selection bits
These bits are set and cleared by software (only if LOCK not set). They select the Hysteresis voltage of the comparator .
00: No hysteresis
01: Low hysteresis
10: Medium hysteresis
11: High hysteresis
Bit 15 POLARITY: Comparator polarity selection bit
This bit is set and cleared by software (only if LOCK not set). It inverts Comparator polarity.
0: Comparator output value not inverted
1: Comparator output value inverted
Bits 14:9 Reserved, must be kept at reset value.
Bits 8:7 INPSEL[1:0]: Comparator input plus selection bit
This bit is set and cleared by software (only if LOCK not set).
00: PA14
01: PB1
1x: PB2
Bits 6:4 INMSEL: Comparator 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.
000: 1/4 \(V_{REFINT}\)
001: 1/2 \(V_{REFINT}\)
010: 3/4 \(V_{REFINT}\)
011: \(V_{REFINT}\)
100: DAC OUT
101: PA13
110: PB0
111: PB3
Bits 3:2 PWRMODE[1:0]: Power Mode of the comparator
These bits are set and cleared by software (only if LOCK not set). They control the power/speed of the Comparator.
00:High speed
01 or 10:Medium speed
11:Ultra low power
Bit 1 Reserved, must be kept at reset value.
Bit 0 EN: Comparator enable bit
This bit is set and cleared by software (only if LOCK not set). It switches on Comparator.
0: Comparator switched OFF
1: Comparator switched ON
13.7 COMP register map
The following table summarizes the comparator registers.
Table 36. COMP register map and reset values
| Offset | Register | 31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0x00 | COMP1_CSR | LOCK | VALUE | Res. | Res. | Res. | Res. | Res. | Res. | SCALEN | BRGEN | Res. | BLANKING | HYST | POLARITY | Res. | Res. | Res. | Res. | Res. | Res. | INPSEL | INMSEL | PWRMODE | Res. | EN | |||||||
| Reset value | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |||||||||||||||
Refer to Section 2.2.2: Memory map and register boundary addresses for the register boundary addresses.