stm32 read system clock

STM32 Read System Clock: A Comprehensive Guide

By Amanda Smith
AD

Embracing the ever-evolving realm of microcontrollers, the STMicroelectronics STM32 series stands tall with its robust set of features, including the capability to read the system clock. This essential functionality paves the way for a myriad of time-related operations, from timing events to setting up real-time operating systems (RTOS). Here, we delve into the process of reading the system clock on STM32, ensuring you master this crucial aspect for your next embedded project.

Before we dive into the details, let's briefly understand the system clock in STM32. The system clock (SysTick) is a high-resolution timer that ticks at a frequency equal to the system clock. It is initiated and controlled by the SysTick timer, providing a precise time base for software operations. Now, let's explore how to read this system clock in detail.

Reading the System Clock

The first step towards understanding how to read the system clock is knowing the functions and data types involved. The function SysTick_Config SysTick_Config(u32 ticks) is the cornerstone of system clock management. This function initializes the SysTick timer with the specified number of ticks. The data type u32 represents an unsigned 32-bit integer, signifying the number of ticks per second (Hz).

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
Digital Clock Using Lisp
Digital Clock Using Lisp
⏰ ESP32 Real-Time Clock (RTC) Explained!
⏰ ESP32 Real-Time Clock (RTC) Explained!

After initiation, the rollover time of the SysTick counter can be calculated using the formula: rollover_time(ms) = 1000 / SysTick_Config arguing (Hz). This calculation helps in determining the time interval between two consecutive SysTick interrupts, an integral element in timing operations.

Initializing the System Clock

The initial step in reading the system clock is initiating it with a predetermined number of ticks. This is achieved by calling the SysTick_Config function within the main function (int main(void)). The function returns 0 upon successful initialization, enabling the SysTick interrupt and starting the SysTick timer.

Adjusting Dual Clock using DS3231 on 1.8
Adjusting Dual Clock using DS3231 on 1.8
Прикольные часы на базе STM32
Прикольные часы на базе STM32
a blue and white object laying on the ground with wires attached to it's sides
a blue and white object laying on the ground with wires attached to it's sides

For instance, to set the SysTick timer with a 1000 Hz clock, you would write: SysTick_Config(SystemCoreClock / 1000);. Here, SystemCoreClock represents the frequency of the CPU core clock, accessible through the SystemCoreClock variable in CMSIS files.

Reading the System Clock in Interrupts

Once the system clock is initialized, it generates an interrupt at every interval, allowing you to read the system clock value. The SysTick_Handler function (void SysTick_Handler(void)) is called automatically upon a SysTick interrupt, providing an ideal opportunity to read the system clock.

AD

Within the SysTick_Handler function, you can access the SysTick current value using the NVIC_ST_REoload register. This register contains the current SysTick counter value, incremented on each system clock tick. For instance, you can read the system clock as follows: u32 current_value = NVIC_ST_REoload;. This value represents the number of elapsed ticks since the SysTick timer was initialized.

Tutorial 8: ESP8266 Internet clock
Tutorial 8: ESP8266 Internet clock
Adjusting Dual Clock using DS3231 on 1.8
Adjusting Dual Clock using DS3231 on 1.8
Adjusting clock with alarm & thermometer using DS3231 on 1.8\
Adjusting clock with alarm & thermometer using DS3231 on 1.8\
Bare Metal STM32: Increasing The System Clock And Running Dhrystone
Bare Metal STM32: Increasing The System Clock And Running Dhrystone
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
TM1637 4-Digit 7-Segment Display with Arduino. (Digital Clock example) - MYTECTUTOR
TM1637 4-Digit 7-Segment Display with Arduino. (Digital Clock example) - MYTECTUTOR

Calculating Time Delays and Durations

Reading the system clock is quintessential for calculating time delays and durations in software. By comparing the current SysTick counter value with the value at the start of an operation, you can calculate the elapsed time. This is particularly useful in applications where real-time responses are critical, such as in motor control systems or human-machine interfaces (HMIs).

Consider a scenario where you need to measure the duration between two events (e.g., the start and end of a button press). You can calculate the elapsed time in milliseconds using the following algorithm:

AD
  • Initially, disable interrupts and store the current SysTick counter value (start_value = NVIC_ST_REoload;).
  • Enable interrupts and wait for the event to occur.
  • Upon the event occurrence, disable interrupts again and store the current SysTick counter value (end_value = NVIC_ST_REoload;).
  • Calculate the elapsed time in ms: duration(ms) = (end_value - start_value) / ticks_per_ms. Here, ticks_per_ms is calculated as SystemCoreClock / 1000.

Implementing Delays with the System Clock

The system clock's periodic nature also facilitates software delays. By disabling interrupts and waiting for a specific number of ticks, you can introduce delays in your code. This approach ensures predictable timing, vital in synchronous operations like communication protocols or display updates.

ESP32 Quick Internet Clock (automatic Timeset)
ESP32 Quick Internet Clock (automatic Timeset)
ESP32 NTP Digital Clock: Timezone and Daylight Saving Time | Random Nerd Tutorials
ESP32 NTP Digital Clock: Timezone and Daylight Saving Time | Random Nerd Tutorials
How to Create a Clock Using Arduino , DS3231 RTC Module and OLED Display
How to Create a Clock Using Arduino , DS3231 RTC Module and OLED Display
How to Make a Digital Clock Using MAX7219 Display and ESP32 (Without RTC Module)
How to Make a Digital Clock Using MAX7219 Display and ESP32 (Without RTC Module)
How to Make a Digital Clock at Home Using Arduino | Full Tutorial
How to Make a Digital Clock at Home Using Arduino | Full Tutorial
TechKiwiGadgets
TechKiwiGadgets
Self Learning Clock (SLC)
Self Learning Clock (SLC)
How to Make a Digital Clock Using MAX7219 Display and ESP32 (Without RTC Module)
How to Make a Digital Clock Using MAX7219 Display and ESP32 (Without RTC Module)
GitHub - mike-rankin/ESP32_Desktop_Clock: ESP32 based Desktop Clock
GitHub - mike-rankin/ESP32_Desktop_Clock: ESP32 based Desktop Clock
GitHub - mike-rankin/ESP32_Desktop_Clock: ESP32 based Desktop Clock
GitHub - mike-rankin/ESP32_Desktop_Clock: ESP32 based Desktop Clock
ESP8266 NTP Clock on SSD1306 OLED (Arduino IDE)
ESP8266 NTP Clock on SSD1306 OLED (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
Hibrid clock- NTP , DS3231 and DHT22
Hibrid clock- NTP , DS3231 and DHT22

A simple delay function might look like this:

void delay_ms(u32 ms)
u32 ticks = ms * (SystemCoreClock / 1000);
u32 start = NVIC_ST_REoload;
while ((NVIC_ST_REoload - start) < ticks);

The function takes the delay duration in milliseconds as an argument, calculates the required number of ticks, and waits until the SysTick counter reaches that value. This ensures a precise delay, even with varying system loads.

AD

In the vast and dynamic realm of embedded systems, optimizing time-related operations is essential. Mastering the art of reading and manipulating the system clock on STM32 microcontrollers is a significant step towards honing your embedded systems skills. As you embark on your next project, remember that understanding and harnessing the system clock will empower you to build more efficient, responsive, and real-time capable systems.

AD