stm32 hal get system clock

STM32 HAL: Get System Clock - Simple & Fast Guide

By David Allen
AD

Working with STM32 microcontrollers often requires understanding and manipulation of system clock settings. The STM32 HAL (Hardware Abstraction Layer) library provides a convenient way to get and manage the system clock. Let's delve into how you can use the HAL to obtain the system clock in your STM32 project.

Before we dive into the specifics, it's crucial to understand the role of the system clock. The system clock is the main clock source that the microcontroller uses for its operations. It determines the speed at which the processor executes instructions, and thus, the system's overall speed.

Understanding the System Clock with STM32 HAL

The STM32 HAL provides a straightforward way to understand and manage the system clock. It offers functions to configure, switch, and read the system clock. Let's explore these functionalities.

AD
STM32 Nucleo UART DMA with STM32CubeIDE and HAL Libraries
STM32 Nucleo UART DMA with STM32CubeIDE and HAL Libraries
Bare Metal STM32: Increasing The System Clock And Running Dhrystone
Bare Metal STM32: Increasing The System Clock And Running Dhrystone
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

The HAL provides a dedicated structure, RCC_ClkInitTypeDef, which contains information about the clock configuration. This structure is used to initialize and read the system clock settings.

Initializing the System Clock

To begin, you need to initialize the system clock. This process involves selecting the clock source (like HSI, HSE, or PLL) and configuring the clock divisors. The function HAL_RCC_ClockConfig is used for this purpose.

Digital Clock Using Lisp
Digital Clock Using Lisp
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...
Hibrid clock- NTP , DS3231 and DHT22
Hibrid clock- NTP , DS3231 and DHT22

Here's a basic example of how to use this function to configure the system clock:

// RCC_ClkInitTypeDef struct initialization...
RCC_ClkInitTypeDef RCC_ClockInitStruct = {0};
RCC_ClockInitStruct.HSIState = RCC_HSI_ON;
RCC_ClockInitStruct.HSICalibrationValue = 16;
RCC_ClockInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_ClockInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_ClockInitStruct.PLL.PLLM = 8;
RCC_ClockInitStruct.PLL.PLLN = 360;
RCC_ClockInitStruct.PLL.PLLP = PLCLOCKDIVR_DIV2;
RCC_ClockInitStruct.PLL.PLLQ = 7;

// Call the HAL function to configure the clock
if (HAL_RCC_ClockConfig(&RCC_ClockInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
 // Manage the error...
}

Reading the System Clock Frequency

After initializing the system clock, you might want to read its frequency. The HAL provides the HAL_RCC_GetSysClockFreq function for this purpose. This function returns the frequency of the system clock in Hz.

AD

Here's how you can use this function:

Tutorial 8: ESP8266 Internet clock
Tutorial 8: ESP8266 Internet clock
Прикольные часы на базе STM32
Прикольные часы на базе STM32
STM32 DMA Tutorial How to Use Direct Memory Access
STM32 DMA Tutorial How to Use Direct Memory Access
ESP8266 NTP Clock on SSD1306 OLED (Arduino IDE)
ESP8266 NTP Clock on SSD1306 OLED (Arduino IDE)
First Project using STM32 in STM32CubeIDE - The Engineering Projects
First Project using STM32 in STM32CubeIDE - The Engineering Projects
Adjusting Dual Clock using DS3231 on 1.8
Adjusting Dual Clock using DS3231 on 1.8
uint32_t SystemClockFrequency = HAL_RCC_GetSysClockFreq();

Switching the Clock Source

You might need to switch the clock source at certain points in your program. The HAL offers functions to switch between different clock sources like HSI, HSE, LSI, LSE, and PLL. These functions include HAL_RCC_HSIOnRAMCalibTrimm, HAL_RCC_HSEConfig, etc.

For instance, to enable the HSE (High-Speed External clock) and switch the clock source, you can use:

AD

Enabling the HSE

Before using the HSE as a clock source, you need to enable it using HAL_RCC_HSEConfig function:

Adjusting Dual Clock using DS3231 on 1.8
Adjusting Dual Clock using DS3231 on 1.8
Accurate digital wall clock with GPS and remote control - Share Project - PCBWay
Accurate digital wall clock with GPS and remote control - Share Project - PCBWay
Bare Metal STM32: The Various Real Time Clock Flavors
Bare Metal STM32: The Various Real Time Clock Flavors
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
ESP32 just turned into a wrist genius 🤖🔥
ESP32 just turned into a wrist genius 🤖🔥
TechKiwiGadgets
TechKiwiGadgets
GitHub - schreibfaul1/ESP32-LED-Matrix-Clock: Clock with six 8x8 Led-Matrix, similar to my video at YouTube: https://www.youtube.com/watch?v=mea5-qX4O54
GitHub - schreibfaul1/ESP32-LED-Matrix-Clock: Clock with six 8x8 Led-Matrix, similar to my video at YouTube: https://www.youtube.com/watch?v=mea5-qX4O54
(Nearly) NIMO Clock
(Nearly) NIMO Clock
Adjusting clock with alarm & thermometer using DS3231 on 1.8\
Adjusting clock with alarm & thermometer using DS3231 on 1.8\
ESP32 NTP Digital Clock: Timezone and Daylight Saving Time | Random Nerd Tutorials
ESP32 NTP Digital Clock: Timezone and Daylight Saving Time | Random Nerd Tutorials
Getting started with STM: STM32F429 Discovery
Getting started with STM: STM32F429 Discovery
Morphing Digital Clock
Morphing Digital Clock
ESP32 with 74HC595 and 4-Digit 7 Segment Display
ESP32 with 74HC595 and 4-Digit 7 Segment Display
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
HAL_RCC_HSEConfig(RCC_TYPE_B);

Switching to the HSE as Clock Source

After enabling the HSE, you can switch to it as the clock source using HAL_RCC_HSIOnRAMCalibTrimm function along with the selection of HSE as clock source:

// Initialize RCC_ClkInitTypeDef
RCC_ClockInitStruct.HSIState = RCC_HSI ascertain;
RCC_ClockInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;

// Call the HAL function to switch to HSE
HAL_RCC_ClockConfig(&RCC_ClockInitStruct, FLASH_LATENCY_7);

Understanding and managing the system clock is integral to effective use of STM32 microcontrollers. The HAL library provides a wealth of functions to assist you in this process. Happy coding!

AD
AD