stm32 get system clock

**Setting STM32 System Clock: An In-Depth Guide**

By John Miller
AD

Determining the system clock frequency is a fundamental operation when working with STM32 microcontrollers. The clock configuration of STM32 devices is composed of several clocks, each with its specific function and source. Knowing how to get the system clock will help you understand and control the четверти microcontroller's timing accurately. Let's dive into the world of STM32 clocks and learn how to obtain the system clock frequency.

STM32 microcontrollers incorporate a high flexibility and configurability clock management scheme. The main clock sources for STM32 devices are the High-Speed Internal Clock (HSI), High-Speed External Clock (HSE), and Low-Speed Internal Clock (LSI). These clocks feed the System Clock (SYSCLK) through a complex network of-dividers and multipliers, managed by the Clock Control & Security Compiler (CCS) module.

System Clock Sources and Configuration

Before attempting to retrieve the system clock, it's essential to understand the possible clock sources and the configuration options available in STM32 microcontrollers. The clock configuration can significantly impact the system's performance and power consumption.

AD
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
Bare Metal STM32: Increasing The System Clock And Running Dhrystone
Bare Metal STM32: Increasing The System Clock And Running Dhrystone
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...

The HSI clock is a low-cost, high-speed internal clock that can be used with minimal external components. It's the default clock source for most STM32 devices. On the other hand, the HSE clock provides better accuracy and stability when using an external oscillator or crystal. The LSI clock offers a low-power alternative, suitable for standby or stop modes.

HSI Clock Setup

The HSI clock is enabled and configured via the Power Control and Clock Control registers. To use HSI as the main clock source, set the HSION bit in the RCC_CR register to 1, and then configure the PLLMUL, PLLN, and PLLP ratios in the RCC_PLLCFGR, RCC_CFGR, and RCC_DCKCFGR registers, respectively.

Прикольные часы на базе STM32
Прикольные часы на базе STM32
Digital Clock Using Lisp
Digital Clock Using Lisp
Adjusting Dual Clock using DS3231 on 1.8
Adjusting Dual Clock using DS3231 on 1.8

Adjusting these values allows you to configure the system clock frequency according to your application's requirements. For example, to achieve a 48 MHz SYSCLK from a 16 MHz HSI, you'd set PLLMUL to 4, PLLN to 6, and PLLP to 2.

HSE Clock Setup

Using the HSE clock requires an external oscillator or crystal. Connect the crystal's outputs to the OSC32_IN and OSC32_OUT pins of your STM32 device. Set the HSERDY and HSEON bits in the RCC_CR register to enable the HSE clock. Once the HSE is stable, configure the PLLMUL, PLLN, and PLLP ratios as described in the HSI Clock Setup section to achieve the desired SYSCLK frequency.

AD

STM32 devices also offer clock security features, such as clock detection and secure clock switching. These features protect the system from unauthorized clock manipulation and ensure the integrity of the clock sources.

⏰ ESP32 Real-Time Clock (RTC) Explained!
⏰ ESP32 Real-Time Clock (RTC) Explained!
Tutorial 8: ESP8266 Internet clock
Tutorial 8: ESP8266 Internet clock
Large Seven Segment Digital Clock Using Atmega328/Atmega8 by Manmohan Pal
Large Seven Segment Digital Clock Using Atmega328/Atmega8 by Manmohan Pal
ESP32 Real Time Clock (RTC) using DS1307 Module and display on OLED
ESP32 Real Time Clock (RTC) using DS1307 Module and display on OLED
Tailored Microsecond Delay on STM32
Tailored Microsecond Delay on STM32
First Project using STM32 in STM32CubeIDE - The Engineering Projects
First Project using STM32 in STM32CubeIDE - The Engineering Projects

Retrieving the System Clock Frequency

To get the system clock frequency, you can read the SYSCLK value directly from the Flash Access Control Register (FLASH_ACR) or calculate it based on the configuration registers. The SYSCLK_TICKFT hardware macro provides an easy way to obtain the system clock frequency in Hz.

Here's a simple function to get the system clock frequency using the SYSCLK_TICKFT macro:

AD
```c uint32_t GetSysClock(void) { return ((SYSCLK_TICKFT) * 1) / 1000; // Return the frequency in Hz } ```

Using DWT_CYCCNT Register

Another way to retrieve the system clock frequency is by using the Data Watchpoint and Trace (DWT) module's CYCCNT register. By reading and comparing the CYCCNT value at two different timestamps, you can calculate the system clock frequency.

Atomic Clock and Weather with ESP32 and Nextion
Atomic Clock and Weather with ESP32 and Nextion
Hibrid clock- NTP , DS3231 and DHT22
Hibrid clock- NTP , DS3231 and DHT22
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
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
Adjusting Dual Clock using DS3231 on 1.8
Adjusting Dual Clock using DS3231 on 1.8
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
Accurate digital wall clock with GPS and remote control - Share Project - PCBWay
Accurate digital wall clock with GPS and remote control - Share Project - PCBWay
ESP32 CLOCK TAKES TIME TO GIVE WEATHER INFO, TOO
ESP32 CLOCK TAKES TIME TO GIVE WEATHER INFO, TOO
TechKiwiGadgets
TechKiwiGadgets
Interfacing I2C LCD with STM32F103C8T6 | STM32 I2C LCD Tutorial
Interfacing I2C LCD with STM32F103C8T6 | STM32 I2C LCD Tutorial
ESP8266 NTP Clock on SSD1306 OLED (Arduino IDE)
ESP8266 NTP Clock on SSD1306 OLED (Arduino IDE)

Here's an example of how to use the DWT_CYCCNT register to get the system clock frequency:

```c uint32_t GetSysClock_DWT(void) { uint32_t start, end, cpu_cycles; start = DWT->CYCCNT; volatile int empty = 0; // Ensure DWT is not paused by adding a non-optimizable code empty = ((*DEMO_PTR) & 0x01); // Pointer to a global volatile variable with no use case end = DWT->CYCCNT; cpu_cycles = end - start; return (cpu_cycles / 2u) / 1000; // Return the frequency in Hz } ```

The DWT method is more accurate when the system clock frequency is not a multiple of 2. Also, it requires enabling the SysTick timer to keep track of the CPU cycles accurately.

AD

Understanding and controlling the system clock is crucial for optimizing the performance and power consumption of your STM32 applications. By grasping the clock configuration and retrieval techniques, you'll gain better control over your microcontroller's timing, enabling you to create more efficient and reliable embedded systems.

AD