Why do STM32 have two crystal oscillators?

ampheoampheo
3 min read

The STM32 microcontrollers typically include two crystal oscillator circuits (one high-speed external oscillator (HSE) and one low-speed external oscillator (LSE)) for several key reasons related to power efficiency, precision, and system flexibility:


1. Different Clock Requirements for Different Functions

STM32 MCUs serve multiple roles that require different clock frequencies:

  • High-Speed Clock (HSE - 4-48MHz)

    • Used for the CPU core, peripherals (USB, Ethernet, high-speed timers)

    • Provides high accuracy for time-critical tasks (e.g., USB communication, motor control PWM).

    • Often connected to an 8-25MHz crystal (e.g., 8MHz for STM32F4).

  • Low-Speed Clock (LSE - 32.768kHz)

    • Used for the Real-Time Clock (RTC) and low-power modes.

    • Optimized for battery-backed operation (e.g., keeping time in sleep mode).

    • The 32.768kHz frequency allows easy division to 1Hz for RTC seconds counting.


2. Power Efficiency

  • HSE can be disabled in low-power modes (e.g., STOP or STANDBY) to save energy.

  • LSE remains active in deep sleep (consuming <1µA) to maintain RTC functionality.


3. Redundancy & Reliability

  • If the HSE fails, the STM32 can switch to the internal oscillator (HSI) for fault tolerance.

  • The LSE ensures the RTC keeps running even if the main system clock fails.


4. Peripheral Flexibility

  • Some peripherals require independent clocks:

    • USB OTG needs a 48MHz (±0.25% accuracy) clock (often derived from HSE via PLL).

    • CAN bus requires a precise clock for reliable communication.

    • RTC needs a stable 32.768kHz source for calendar/timekeeping.


5. Internal vs. External Oscillators

OscillatorTypeFrequencyUse CaseAccuracy
HSEExternal Crystal4-48MHzMain CPU, USB, Ethernet, Timers±10-50ppm
HSIInternal RC8-64MHz (MCU-dependent)Fallback clock, less accurate±1-2%
LSEExternal Crystal32.768kHzRTC, Low-power modes±20-100ppm
LSIInternal RC~32kHzWatchdog, RTC (no crystal needed)±5%

6. When Can You Skip One?

  • LSE is optional if:

    • You don’t need an accurate RTC (use LSI instead).

    • The application runs only on internal oscillators (HSI/LSI).

  • HSE is optional if:

    • The application doesn’t need USB/Ethernet or high-precision timing.

    • The internal HSI is sufficient (but less accurate).


7. Example STM32 Clock Configuration

A typical setup in STM32CubeIDE might:

  1. Use HSE (8MHz) → PLL → 72MHz CPU clock.

  2. Use LSE (32.768kHz) → RTC for timekeeping.

  3. Enable HSI (16MHz) as a backup.

c

// STM32 HAL Clock Initialization Example
RCC_OscInitTypeDef osc = {0};
osc.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSE;
osc.HSEState = RCC_HSE_ON;       // Enable 8MHz crystal
osc.LSEState = RCC_LSE_ON;       // Enable 32.768kHz crystal
HAL_RCC_OscConfig(&osc);

8. Key Takeaways

  • HSE = High performance, precision (for core and fast peripherals).

  • LSE = Low-power, RTC accuracy (for battery-backed timekeeping).

  • Using both ensures optimal power/performance tradeoffs.

  • If precision isn’t critical, you can rely on internal oscillators (HSI/LSI).

For USB, Ethernet, or CAN, the HSE is mandatory due to tight timing requirements. For wearables or battery-powered devices, the LSE is essential for low-power RTC operation.

0
Subscribe to my newsletter

Read articles from ampheo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ampheo
ampheo