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
- STM32 RCC (Reset & Clock Control) Unit
- STM32 RCC Clock Configuration (CubeMX)
- STM32 Clock Configuration Remarks
- 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:
- System Reset
- Power Reset
- 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)
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void SysReset_Check(void) { if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST)){ // NRST Pin LOW (Hardware Reset) Occurred, Handle it here! } else if(__HAL_RCC_GET_FLAG(RCC_FLAG_BORRST)){ // Brown-Out Reset Occurred, Handle it here! } else if(__HAL_RCC_GET_FLAG(RCC_FLAG_WWDGRST)){ // Window-Watchdog Reset Occurred, Handle it here! } ... __HAL_RCC_CLEAR_RESET_FLAGS(); // Clear All Reset Flags } |
Using CMSIS (same logic but without HAL)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void SysReset_Check(void) { if(__HAL_RCC_GET_FLAG(RCC_CSR_PINRSTF)){ // NRST Pin LOW (Hardware Reset) Occurred, Handle it here! } else if(RCC->CSR & RCC_CSR_BORRSTF){ // Brown-Out Reset Occurred, Handle it here! } else if(RCC->CSR & RCC_CSR_IWDGRSTF){ // Window-Watchdog Reset Occurred, Handle it here! } ... RCC->CSR |= RCC_CSR_RMVF; // Clear All Reset Flags } |
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 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.
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:
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.
#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:
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 Name | Amazon.com | AliExpress | eBay |
1 | STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz) | Amazon | AliExpress | eBay |
1 | Nucleo-L432KC (ARM Cortex-M4 @ 80MHz) | Amazon | AliExpress | eBay |
1 | ST-Link V2 Debugger | Amazon | AliExpress | eBay |
2 | BreadBoard | Amazon | AliExpress | eBay |
1 | LEDs Kit | Amazon & Amazon | AliExpress | eBay |
1 | Resistors Kit | Amazon & Amazon | AliExpress | eBay |
1 | Capacitors Kit | Amazon & Amazon | AliExpress & AliExpress | eBay & eBay |
1 | Jumper Wires Pack | Amazon & Amazon | AliExpress & AliExpress | eBay & eBay |
1 | Push Buttons | Amazon & Amazon | AliExpress | eBay |
1 | Potentiometers | Amazon | AliExpress | eBay |
1 | Micro USB Cable | Amazon | AliExpress | eBay |
★ 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.