stm32 get system clock frequency

STM32: Get System Clock Frequency.

By Richard Wilson
AD

Embarking on your STM32 microcontroller journey, you might find yourself in need of a crucial piece of information: the system clock frequency. After all, this value is pivotal for tasks like timing functions, calculating delays, and ensuring synchronized operation with external peripherals. Let's dive into a comprehensive guide on how to get the system clock frequency on STM32 microcontrollers.

Before we delve into the specifics, understanding the STM32 clock management is essential. STM32 microcontrollers are equipped with a sophisticated clock management module that handles the clock generation and distribution. The system clock frequency, often denoted as HCLK (High Speed Clock), is generated based on an external high-speed oscillator, an internal high-speed oscillator, or a phase-locked loop (PLL).

Understanding the System Clock Tree

The STM32 internal clock tree is designed to provide various clock signals to different peripherals, each with its specific requirements. To grasp the system clock frequency, we first need to navigate this clock tree.

AD
STM32F103 PCB STM32 MCU family developer board schematic STM32F103 PCB STM32 MCU family deve...
STM32F103 PCB STM32 MCU family developer board schematic STM32F103 PCB STM32 MCU family deve...
Simple STM32 Frequency meter from 1Hz to 30 MHz-Arduino IDE
Simple STM32 Frequency meter from 1Hz to 30 MHz-Arduino IDE
ESP32: Guide for DS3231 Real Time Clock Module (RTC) | Random Nerd Tutorials
ESP32: Guide for DS3231 Real Time Clock Module (RTC) | Random Nerd Tutorials

At the core of the clock tree lies the AHB (Advanced High-performance Bus) clock (HCLK), which is distributed to the various bus matrices (AHB, APB1, and APB2). Each bus matrix further distributes the clock signals to the attached peripherals at different frequencies, denoted as PCLK1 (APB1 clock) and PCLK2 (APB2 clock).

Mastering the Clock Types

To understand the system clock frequency, familiarize yourself with the types of clocks in the STM32 microcontroller:

Bare Metal STM32: Increasing The System Clock And Running Dhrystone
Bare Metal STM32: Increasing The System Clock And Running Dhrystone
First Project using STM32 in STM32CubeIDE - The Engineering Projects
First Project using STM32 in STM32CubeIDE - The Engineering Projects
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
  • High-Speed Clock (HCLK): The main system clock, generated based on an oscillator or PLL.
  • Peripheral Clocks (PCLK1, PCLK2): Derived from HCLK and distributed to APB1 and APB2 peripherals, respectively.
  • Phase-Locked Loop (PLL) Clocks: Generated by the PLL, based on the input clock (HSE or HSI).
  • Low-Speed Clock (LSI, LSE): Low-speed internal or external oscillators, typically used as backup or sub-systems.

Each clock type has a specific purpose and frequency, ensuring optimal performance and power efficiency throughout the system.

Factory-Set Clock Configuration

Before powering up your STM32 microcontroller, the clock configuration is set by the manufacturer and stored in the Option Bytes. Common factory-set configurations include:

AD
  • HSI (High-Speed Internal) oscillator as the main clock source.
  • PLL multiplication factor set to 12 or 16, providing an 8 or 10 MHz system clock frequency.
  • Clock security system (CSS) enabled, preventing unauthorized modification of Option Bytes.

Understanding the factory-set clock configuration is crucial for preserving the microcontroller's settings during normal operation.

Why Do Computers Need Clock Signals? ⏰⚡ | Digital Electronics Simplified Part 3
Why Do Computers Need Clock Signals? ⏰⚡ | Digital Electronics Simplified Part 3
3. STM32. Програмування STM32F103. GPIO - Avislab - сайт для палких паяльників
3. STM32. Програмування STM32F103. GPIO - Avislab - сайт для палких паяльників
⏰ ESP32 Real-Time Clock (RTC) Explained!
⏰ ESP32 Real-Time Clock (RTC) Explained!
STM32 Tutorial : Clock, AHB & APB Buses configuration (Part3: Frequency debugging with STMSTUDIO)
STM32 Tutorial : Clock, AHB & APB Buses configuration (Part3: Frequency debugging with STMSTUDIO)
Прикольные часы на базе STM32
Прикольные часы на базе STM32
Getting started with STM: STM32F429 Discovery
Getting started with STM: STM32F429 Discovery

Determining the System Clock Frequency

Now that you're equipped with the necessary background, let's see how to determine the system clock frequency at runtime. Two methods stand out: using Alternatively, you can use the SystemCoreClock variable or calculate the frequency based on the clock configuration.

MTIME or Systick Counter

AD

The MTIME and Systick counters in STM32 are 24-bit timers that decrement at the HCLK frequency. By reading and calculating the difference between two consecutive counter values, you can determine the HCLK frequency accurately. Keep in mind that this method requires a stable clock source to provide accurate results.

Bare Metal STM32: The Various Real Time Clock Flavors
Bare Metal STM32: The Various Real Time Clock Flavors
ESP8266 NodeMCU: DS3231 Real Time Clock Module (RTC) | Random Nerd Tutorials
ESP8266 NodeMCU: DS3231 Real Time Clock Module (RTC) | Random Nerd Tutorials
a hand is hooked up to an electronic device with the words emergency motor and clock generator above it
a hand is hooked up to an electronic device with the words emergency motor and clock generator above it
STM32F103C8T6 - Purple Pill
STM32F103C8T6 - Purple Pill
an electronic clock is shown with the words 24 / 7 displayed in front of it
an electronic clock is shown with the words 24 / 7 displayed in front of it
Set Up a Real-Time Clock (RTC) with ESP32 and DS3231 Using NTP Time Synchronization
Set Up a Real-Time Clock (RTC) with ESP32 and DS3231 Using NTP Time Synchronization
Si4732 + Si5351+ STM32F103C8T6 All-Mode Receiver Schematic Si4735 + Si5351+ STM32F103C8T6 A...
Si4732 + Si5351+ STM32F103C8T6 All-Mode Receiver Schematic Si4735 + Si5351+ STM32F103C8T6 A...
Adjusting Dual Clock using DS3231 on 1.8
Adjusting Dual Clock using DS3231 on 1.8
What is Clock Cycle?
What is Clock Cycle?
STM32 power saving: wake up from external source – 7
STM32 power saving: wake up from external source – 7
Начинаем изучать STM32 или Управляем светом по-умному
Начинаем изучать STM32 или Управляем светом по-умному
Interfacing I2C LCD with STM32F103C8T6 | STM32 I2C LCD Tutorial
Interfacing I2C LCD with STM32F103C8T6 | STM32 I2C LCD Tutorial
PWM with STM32 - The Engineering Projects
PWM with STM32 - The Engineering Projects

The formula to calculate the HCLK frequency is:

HCLK = HCLK period / (MTIME_S - MTIME_E)

AD

where HCLK period = 0.01 seconds (for a 10 ms interval) and MTIME_S, MTIME_E are the start and end values of the MTIME counter, respectively.

Using SystemCoreClock Variable

Some STM32 microcontrollers provide a SystemCoreClock variable in the CMSIS-Core header file. This variable is updated automatically by the CMSIS-Core system functions when the clock frequency changes, ensuring its accuracy at runtime.

To use the SystemCoreClock variable, include the CMSIS-Core header file in your project and access the variable as:

uint32_t SystemCoreClock = SystemCoreClockConfig();

This method is convenient and accurate, but it's essential to ensure the CMSIS-Core system functions are initializing and updating the SystemCoreClock variable correctly.

Calculating the Frequency based on Clock Configuration

If the clock configuration is known, you can calculate the HCLK frequency directly. For example, if the PLL is used with a multiplication factor of 'M' and the input clock (HSE or HSI) has a frequency of 'F_in', the HCLK frequency 'F_HCLK' can be calculated as:

F_HCLK = M * F_in

For instance, if HSE oscillates at 8 MHz and the PLL is set with a multiplication factor of 16, the HCLK frequency would be:

F_HCLK = 16 * 8 MHz = 128 MHz

However, keep in mind that this method only provides an approximate value, as it doesn't account for potential clock division factors or the variability of the input clock source.

In summary, determining the STM32 system clock frequency is possible through various means, each with its advantages and disadvantages. By understanding the clock tree structure, clock types, and available determination methods, you're well-equipped to tackle system clock frequency calculations in your projects. Armed with this knowledge, you can now seamlessly synchronize your STM32 microcontroller with its peripherals and operate with confidence.

AD