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.

13.2 COMP main features

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

Comparator block diagram showing input selection, comparator core, polarity selection, and output redirection.

The diagram illustrates the Comparator (COMP) block structure. It consists of the following components and signal paths:

MS30478V2

Comparator block diagram showing input selection, comparator core, polarity selection, and output redirection.

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:

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: Comparator hysteresis diagram. The graph shows an input signal (INP) as a sine wave and the comparator output (COMP_OUT) as a digital signal. Two horizontal dashed lines represent the upper threshold (INM) and the lower threshold (INM - V_hyst). 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.

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.

Figure 26: Comparator hysteresis diagram. The graph shows an input signal (INP) as a sine wave and the comparator output (COMP_OUT) as a digital signal. Two horizontal dashed lines represent the upper threshold (INM) and the lower threshold (INM - V_hyst). 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

Figure 27: Comparator output blanking diagram. The diagram shows five vertically aligned waveforms: PWM, Current, Raw comp output, Blanking window, and Final comp output. The PWM signal is a square wave. The Current signal shows a ramp-up to a 'Current limit' (dashed line) and a sharp spike at each PWM transition. The Raw comp output is a series of narrow pulses. The Blanking window is a series of narrow pulses that coincide with the PWM transitions. The Final comp output is a series of narrow pulses that are masked by the blanking window. Below the waveforms, a logic diagram shows 'Comp out' and 'Blank' (inverted) inputs to an AND gate, producing 'Comp out (to TIM_BK ...)'. The reference ID 'MS30964V1' is in the bottom right corner.
Figure 27: Comparator output blanking diagram. The diagram shows five vertically aligned waveforms: PWM, Current, Raw comp output, Blanking window, and Final comp output. The PWM signal is a square wave. The Current signal shows a ramp-up to a 'Current limit' (dashed line) and a sharp spike at each PWM transition. The Raw comp output is a series of narrow pulses. The Blanking window is a series of narrow pulses that coincide with the PWM transitions. The Final comp output is a series of narrow pulses that are masked by the blanking window. Below the waveforms, a logic diagram shows 'Comp out' and 'Blank' (inverted) inputs to an AND gate, producing 'Comp out (to TIM_BK ...)'. The reference ID 'MS30964V1' is in the bottom right corner.

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:

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:

Table 35. Comparator behavior in the low power modes

MODEDescription
DeepstopNo effect on the comparator. Comparator registers status is retained.
Comparator interrupt causes the device to wakeup the system and exit the Deepstop mode.
ShutdownThe 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. 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. 2. Configure and enable the NVIC IRQ channel mapped to the corresponding INTAI line
  3. 3. Enable the COMP

To enable the COMP wakeup, it is required to follow this sequence:

  1. 1. Configure and enable the wakeup line corresponding to the COMP output event in the PWR and select polarity
  2. 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

31302928272625242322212019181716
LOCKVALU
E
Res.Res.Res.Res.Res.Res.SCALE
N
BRGE
N
Res.BLANKING[2:0]HYST[1:0]
rwrrwrwrwrwrwrwrw
1514131211109876543210
POLAR
ITY
Res.Res.Res.Res.Res.Res.INPSEL[1:0]INMSEL[2:0]PWRMODE[1:0]Res.EN
rwrwrwrwrwrwrwrwrw

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

OffsetRegister313029282726252423222120191817161514131211109876543210
0x00COMP1_CSRLOCKVALUERes.Res.Res.Res.Res.Res.SCALENBRGENRes.BLANKINGHYSTPOLARITYRes.Res.Res.Res.Res.Res.INPSELINMSELPWRMODERes.EN
Reset value000000000000000000

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