STM32 Lab by RedCan

HSE and LSE crystal oscillators on STM32: load capacitors, gain margin and measurement

6 min read

Illustration: crystal oscillator embedded electronics workbench

A crystal oscillator is a handful of cheap parts, and also one of the most common sources of trouble on new boards: an LSE crystal that only starts at room temperature, an RTC that loses a minute a week, or a CAN link that drops frames because the clock is off nominal. This is how we size the oscillator following ST’s AN2867, and what we check on the prototype.

How the STM32 oscillator works

The HSE (typically 4–48 MHz) and LSE (32.768 kHz) oscillators in STM32 microcontrollers are Pierce oscillators: an inverting amplifier inside the chip, and outside it the crystal and two capacitors to ground (CL1, CL2). For oscillation to start and stay stable, two conditions must hold: the crystal must see the load capacitance it was calibrated for, and the amplifier must have enough gain to overcome the losses in the crystal.

1. Load capacitors

The crystal manufacturer specifies the load capacitance CL at which the crystal runs at its nominal frequency. The crystal “sees” the series combination of both capacitors plus stray capacitance:

CL = (CL1 × CL2) / (CL1 + CL2) + Cs

Cs is the capacitance of the traces and the microcontroller pins – typically a few picofarads. With CL1 = CL2 the formula becomes CL1 = CL2 = 2 × (CL − Cs).

Worked example. An 8 MHz crystal with CL = 10 pF and an estimated Cs = 5 pF: CL1 = CL2 = 2 × (10 − 5) = 10 pF. Using 22 pF “out of habit” would load the crystal with about 16 pF and pull the frequency below nominal.

The direction of the correction is simple: too little load capacitance raises the frequency, too much lowers it. Cs is only an estimate, so confirm the capacitor values by measuring the frequency on the prototype (see below).

2. Gain margin and gm_crit

The oscillator amplifier must make up for the losses in the crystal. AN2867 expresses this as the critical transconductance – the minimum gain needed to sustain oscillation:

gm_crit = 4 × ESR × (2π × F)² × (C0 + CL)²

ESR is the crystal’s maximum equivalent series resistance from its datasheet, C0 the shunt (package) capacitance, F the frequency. How you check it depends on the oscillator:

  • HSE – the STM32 datasheet gives the amplifier transconductance gm. The note recommends a gain margin gm / gm_crit of at least 5. A smaller margin risks the oscillator failing to start on some units, at low temperature or as the crystal ages.
  • LSE – newer families (e.g. STM32G0, L4, G4) have an adjustable drive level (LSEDRV bits), and the datasheet gives a maximum allowed Gm_critmax for each level. The crystal’s calculated gm_crit must be below it.

LSE example. A 32.768 kHz crystal, ESR max = 70 kΩ, C0 = 1 pF, CL = 7 pF:

gm_crit = 4 × 70 kΩ × (2π × 32,768 Hz)² × (8 pF)² ≈ 0.76 µA/V.

For STM32G0 the datasheet lists Gm_critmax of 0.5 µA/V (low), 0.75 µA/V (medium low), 1.7 µA/V (medium high) and 2.7 µA/V (high). This crystal therefore needs the medium high level. The same crystal in a CL = 12.5 pF version gives about 2.2 µA/V – it only fits the high level, which draws the most current. That is why for battery-powered devices we choose LSE crystals with CL of 6–7 pF and low ESR.

In code, the level is set before enabling the LSE, e.g. __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_MEDIUMHIGH);. A higher level means a faster, more reliable start at the cost of a few hundred nA.

3. PCB layout

  • Crystal and capacitors as close as possible to OSC_IN/OSC_OUT, with short, symmetrical traces.
  • Capacitor grounds returned on a short path to the ground at the microcontroller’s VSS pin; a ground ring around the oscillator and a solid ground plane underneath.
  • No fast-switching signals (SPI clocks, PWM, switching-regulator nodes) under the crystal or next to its traces. This matters most for the LSE – an amplifier running on nanoamps is sensitive to coupling and leakage.
  • For the LSE, avoid flux residue and moisture on the traces: leakage in the megaohm range can stop a 32 kHz oscillator.

4. Measuring on the prototype

The most common mistake is probing the crystal pin directly with an oscilloscope probe. The probe adds several to over ten pF and loads a high-impedance node: the frequency shifts, and an LSE may simply stop. Instead:

  1. route the clock to the MCO pin (HSE, LSE) or LSCO (LSE on newer families) and measure it there – ideally with a frequency counter or an oscilloscope with an accurate timebase;
  2. compare the frequency with nominal and convert the ppm error into a capacitor correction (frequency too high → increase CL1/CL2);
  3. check start-up at the temperature extremes and at minimum supply voltage – in a climate chamber, or at least with freeze spray;
  4. measure the LSE start-up time (datasheets typically quote about 2 s) and set a sensible timeout in firmware (LSE_STARTUP_TIMEOUT in HAL).

For the HSE it is also worth checking the crystal drive level – exceeding the crystal’s rating accelerates ageing. AN2867 describes measuring it with a current probe and limiting it with a series resistor Rext on OSC_OUT.

5. Safeguards in firmware

  • Clock Security System (CSS) on the HSE – if the clock fails, the microcontroller switches to the HSI and raises an NMI. In the NMI handler we put the device into a safe state (e.g. switch off power outputs) and report the fault.
  • LSE CSS (e.g. STM32L4, G0, G4) – detects an LSE failure, including in low-power modes.
  • RTC calibration – the remaining LSE error is compensated with RTC smooth calibration (RTC_CALR register, a range of about ±488 ppm). Measure it on the RTC calibration output or via LSCO, and store the value in the device during production.

When to use an oscillator module instead of a crystal

If the device needs better than a few tens of ppm over a wide temperature range (e.g. time synchronisation, some radio interfaces), we use a TCXO and HSE bypass mode – the signal is fed only to OSC_IN and the internal amplifier is disabled. An equivalent mode exists for the LSE.

RedCan checklist: HSE/LSE oscillator

  • Crystal CL, ESR and C0 taken from the crystal datasheet, not from another project.
  • CL1 = CL2 = 2 × (CL − Cs), Cs estimated, value to be confirmed by measurement.
  • HSE: gain margin gm / gm_crit ≥ 5.
  • LSE: gm_crit below Gm_critmax for the selected LSEDRV level.
  • Low-CL (6–7 pF) LSE crystal for battery-powered devices.
  • Layout: short traces, ground ring, no signals under the crystal.
  • Frequency measured via MCO/LSCO, not with a probe on the crystal pin.
  • Start-up checked at temperature extremes and minimum voltage.
  • Clock start-up timeouts and CSS handling in firmware.
  • RTC calibration planned in production when timekeeping matters.

Related reading: STM32 board bring-up and CAN FD bit timing.

Further reading

  • AN2867 – Guidelines for oscillator design on STM8AF/AL/S and STM32 MCUs/MPUs (STMicroelectronics) – Pierce oscillator theory, gm_crit, drive level, layout and a list of tested crystals.
  • The datasheet of your STM32 – the HSE/LSE characteristics section (gm, Gm_critmax, start-up time).
  • The family reference manual – the RCC chapter (CSS, MCO, LSEDRV) and the RTC chapter (smooth calibration).

We design STM32 boards from schematic to bring-up – see electronics and PCB design.

Written by the RedCan engineering team – we design electronics, PCBs and firmware on STM32 for device manufacturers.

Building a device on STM32?

We can help with microcontroller selection, schematic and PCB, prototype bring-up, firmware and tests.

STM32 device design →
Discuss your STM32 project

STM32 is a registered trademark of STMicroelectronics International N.V. or its affiliates in the EU and/or elsewhere. STM32 Lab by RedCan is an independent series written by RedCan engineers.