STM32: Get System Clock Frequency.
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.
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:
- 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:
- 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.
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
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.
The formula to calculate the HCLK frequency is:
HCLK = HCLK period / (MTIME_S - MTIME_E)
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.