STM32 HAL: Get System Clock - Simple & Fast Guide
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.
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.
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.
Here's how you can use this function:
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:
Enabling the HSE
Before using the HSE as a clock source, you need to enable it using HAL_RCC_HSEConfig function:
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!