STM32 RCC Clock Configuration – Ultimate Guide

In this tutorial, we’ll discuss the STM32 RCC unit, how to do STM32 Clock Configuration in CubeMX clock tree, different reset options in STM32 microcontrollers, and identify the STM32 Reset Reason event. Without further ado, let’s get right into it!

Table of Contents

  1. STM32 RCC (Reset & Clock Control) Unit
  2. STM32 RCC Clock Configuration (CubeMX)
  3. STM32 Clock Configuration Remarks
  4. Wrap Up

STM32 RCC (Reset & Clock Control) Unit

The RCC unit in STM32 microcontrollers is responsible for controlling the reset system and clock tree configuration. We typically use the CubeMX GUI interface to configure this unit at the beginning of each firmware project.

However, it’s extremely important to discuss this unit in more detail so you can achieve any desired behavior for your system and make the best use of it. And that’s what we’ll be doing hereafter in this tutorial.

STM32 Reset

There are 3 types of reset in STM32 microcontrollers, which are listed below:

  1. System Reset
  2. Power Reset
  3. Backup Domain Reset

1. System Reset: A system reset sets all registers to their reset values except the reset flags in the clock controller register and the registers in the Backup domain.

A system reset is generated when one of the following events occurs:

  • A low level on the NRST pin (external hardware reset)
  • A software reset by user code (SW reset)
  • A failure in the supply voltage applied to VDD
  • A reset from the brownout reset block (BOR reset)
  • Low-power management reset
  • Window watchdog end of count condition (WWDG reset)
  • Independent watchdog end-of-count condition (IWDG reset)
STM32 RCC System Reset Sources
Simplified diagram of the STM32 system reset circuit

2. Power Reset: A power reset is generated when one of the following events occurs:

  • Power-on/power-down reset (POR/PDR reset)
  • When exiting Standby mode

3. Backup Domain Reset: This reset signal affects only the backup domain registers/peripherals. A backup domain reset is generated when one of the following events occurs:

  • Software Reset, triggered by setting the BDRST bit in the Backup domain control register (RCC_BDCR)
  • VDD or VBAT power ON, if both supplies have previously been powered OFF

STM32 Reset Reason Identification

The reset source can be identified by checking the reset flags in the Control/Status register (RCC_CSR).

The CPU can reset those flags by setting the RMVF bit in the same register.

The reset source signals are different from one target STM32 microcontroller to another. However, it’s generally the same exact way of detecting which one caused the system to go into a reset state. In all STM32 microcontrollers, you’ll have to access the RCC_CSR register and manually check the flags to identify the last reset reason.

Here is a quick code snippet example to show you how we can identify a hardware reset event (NRST), brown-out reset (BOR), and window-watchdog timer reset (WWDG).

Using HAL APIs

Using CMSIS (same logic but without HAL)

STM32 Clock Sources

The STM32 RCC provides multiple options of clock generators that can be used to drive the SYSCLK, which include:

  • HSI (High-speed internal oscillator) clock: ~ 8, 16, 32 or 64 MHz
  • HSE (High-speed external oscillator) clock: 4 to 48 MHz
  • LSE (Low-speed external oscillator) clock: 32 kHz
  • LSI (Low-speed internal oscillator) clock: ~ 32 kHz
  • CSI (Low-power internal oscillator) clock: ~4 MHz
  • HSI48 (High-speed 48 MHz internal oscillator) clock: ~48 MHz
  • PLL clock

Each clock source can be switched ON or OFF independently when it is not used, to optimize power consumption.

STM32 Clock Tree

The STM32 clock tree is a diagram you can find in the datasheet that can help you identify which clock sources are feeding the various internal buses, peripherals, the PLL clock generator, and most importantly the SYSCLK.

Even if you’re willing to configure the STM32 clock tree with manual code, it’s better to configure it in CubeMX and generate the configuration code to compare it against your manual code just to validate your calculations and parameter selection.

❕ Note

Note that some of these clock options are unavailable in all STM32 microcontrollers; this will differ from one target device to another.


STM32 RCC Clock Configuration (CubeMX)

In the CubeMX code configurator/generator, you’ll find a dedicated tab (page) for configuring the clock tree, which looks like the one shown below. But keep in mind that it’s going to be different for each STM32 microcontroller given the wide range of product lines within STM32 MCUs.

STM32 RCC Clock Tree Example

You can set the desired SYSCLK frequency, it’ll prompt you to automatically calculate and configure all the PLL parameters, prescalers, etc. Just make sure that it’s using the desired clock source at the end. You can guide this auto-config process by selecting (external clock source HSE, internal HSI, or whatever).


STM32 Clock Configuration Remarks

Before concluding this tutorial, I’d like to give you a couple of hints regarding the STM32 clock configuration which will help you mitigate various issues related to clock setting.

#1

Maximum Clock Speed For STM32 Bus/Peripherals

Some peripherals, like ADC for example, has a maximum clock speed to operate at. This should not be exceeded in order to have a stable performance of the ADC conversion.

While other peripherals, like (USB, Ethernet, and SDIO) for example, have strict requirements for a fixed clock frequency that it needs to have in order to function properly. In certain cases, you may need to sacrifice a couple of MHz in the SYSCLK settings to obey these numbers or just select another clock source.

Using CubeMX code configurator/generator will prevent you from making such mistakes, this is more important for anyone working with manual code.

#2

STM32H7 Can’t Set Clock To 480MHz or More!

While configuring high-performance STM32 microcontrollers like the STM32H7 line of devices, you’ll find an issue in the CubeMX clock configuration when you try to set the system clock to 400MHz, 480, or more.

The reason behind this is that you first need to change the Power Regulator Voltage Scale setting to be “Power Regulator Voltage Scale 0” instead of the default value. Then, you should be able to select any high frequency for the SYSCLK (like 400MHz, 450MHz, 480MHz, 500MHz, 550MHz, etc).

Here is the default setting for the RCC Power regulator voltage scale:

STM32H7 Can't Set Clock To 480MHz issue solution

Trying to set a high SYSCLK frequency will fail, until you change the power regulator voltage scale to 0 as shown below. Note that the latest version of CubeMX and IDE doesn’t have this issue currently, but just in case you find yourself unable to achieve any high frequency (>200MHz), this will be the reason behind that issue.

STM32H750 H743 H723 Max Frequency Setting Issue Solution

#3

STM32 Debugger Halts At SystemClock_Config();

A common issue for a lot of users has been the debugger stuck (halts) at the SystemClock_Config(); line of code. At first glance, it seems like a failure in clock configuration but it might actually be the case.

Therefore, to tackle this situation, you first need to make sure you’ve selected the correct clock settings in CubeMX and you’ve entered the proper oscillator frequency value if you’re using an external one.

And the last thing to check which can be annoying to a lot of us is to check that you’ve enabled serial wire debug in the first place! Yes, this happens frequently. You connect the debugger pins and everything looks okay but it fails to start a debugging session or gets stuck at the beginning of the code at the SystemClock_Config(); function call.

Here is what to look for in CubeMX settings:

STM32 Debugger Stuck At SystemClock_Config() Issue Solution


Required Parts For STM32 Examples

All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:

QTY.Component NameAmazon.comAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★


Wrap Up

In conclusion, we’ve explored the STM32 RCC unit, various reset options, how to identify a reset reason, how to configure the STM32 clock tree with CubeMX, and what to look for while doing so.

If you’ve found this helpful, you can also explore our complete STM32 tutorial series linked below.

Getting Started With STM32 (Tutorials)

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment