on
2024-01-15
04:13 AM
- edited on
2024-03-11
02:10 AM
by
Laurids_PETERSE
The MCU clock is as crucial as a heartbeat and is the silicon world’s analogy of the biological heart. The Arm® processor and all embedded peripherals inside the STM32 MCU receive one or several clocks generated by the RCC peripheral of the STM32.
The RCC peripheral and the number of clocks vary depending on the specific STM32 that you are working with. For this example, we use the Nucleo-H503RB in our description and project example. Some of the details like the number of phase-lock loop (PLL) are specific to the STM32H503RBT6 MCU. Overall, most of the information written here can be extrapolated to other STM32 series.
1. HSE, also known as "high-speed external clock." External clock can be derived from two sources:
Source one:
The external clock can be fed to the MCU through the OSC_IN pin and OSC_OUT can be left as high-Z or used as a GPIO. For example, an IC chip with an integrated oscillator circuit (that is, not using MCU’s internal oscillator circuit) and a resonator can feed the clock to theOSC_IN pin. This solution is convenient because it saves you the effort of designing an external resonator circuit. A more specific example of such a system is the NSC5004A-25M. In this mode, the internal MCU oscillator is bypassed.
Source two:
The external clock can be generated by an external resonator and be interfaced with both the OSC_IN and OSC_OUT pins. For this solution, you design an external resonator circuit that interfaces with the internal oscillator circuit of the MCU. You can refer to the section 2.4 "oscillator circuit design" below for more details.
Figure 1: RM0492: Example of external clock source and external resonator circuit clock. Resonator and load capacitors must be picked by the hardware designer. Refer to AN2867.
3. HSI: Default system clock. Low-cost high-speed internal RC oscillator clock with a faster startup time than HSE (a few microseconds), but less accurate than a clock with an external resonator.
4. CSI: Low-power and low-cost internal RC oscillator with a faster startup time than HSE (a few microseconds), but less accurate than a clock with an external resonator. In terms of frequency, it sits between the low-speed and high-speed oscillators.
5. LSI: Low-speed internal RC oscillator. The LSI acts as a low-power clock source. It can be kept running when the system is in stop or standby mode for the independent watchdog (IWDG) and autowakeup unit (AWU). The clock frequency is around 32 kHz.
5. LSE "low-speed external oscillator." The LSE block can generate a clock from two possible sources:
6. HSI48: RC oscillator delivering a 48 MHz clock that can be used directly as a kernel clock. See section 8 "kernel clock" for more details on the kernel clock for some peripherals, such as USB or RNG. The HSI48 oscillator mainly aims at providing a high-precision clock to the USB peripheral. This is done by means of a special clock recovery system (CRS) circuitry that can use the USB SOF signal, the LSE, or an external signal to automatically adjust the oscillator frequency on-the fly, with a very small granularity.
7. PLL: Phase locked loop clock control system multiplies frequency much more than what oscillators are capable of outputting. The clocked output by the resonators (that is, crystals/ceramics/etc.) is usually not sufficed to clock the various high-speed peripherals that are embedded on the STM32. The PLL provides the flexibility to increase the clock speed after the application PCB board has already been designed and deployed.
Multispeed internal oscillator functionality is available on the STM32L series. Multispeed internal oscillator is a configurable oscillator, offering a compromise between ultra-low power and performance.
Table 1: Example of clock source summary (example uses the STM32H503 as reference).
Resonator |
Frequency |
Relative accuracy |
Main benefits |
Comment |
HSE |
Possible resonator frequency ranges: 4-50 MHz |
Accurate |
Accuracy |
Requires an external resonator |
HSI |
64 MHz |
Less accurate |
Low cost and faster startup than HSE |
Inside STM32 MCU |
HSI48 |
48 MHz |
Accurate if used with clock recovery system (CRS) |
Low cost and high precision if CRS enabled |
Inside STM32 MCU |
CSI |
4 MHz |
Less accurate |
Low power, low cost, and faster startup time than HSE |
Inside STM32 MCU |
LSE |
32.768 kHz |
Accurate |
Low power and accuracy |
Requires an external resonator |
LSI |
32 kHz |
Less accurate |
Low power and operates in low power mode |
Inside STM32 MCU |
The oscillator system consists of the crystal and the oscillator circuit. The circuit is usually internal to the MCU, though it can also come in an external package in some crystal oscillator IC. The process of oscillator design for the STM32 usually involves selecting the appropriate load capacitors for your selected crystal. We have a list of recommended crystals and capacitors in the application note AN2867. A more detailed process for the STM32 oscillator design is shared in AN2867. For further guidance in your oscillator design, see the attached spreadsheet calculator "AN2867_Crystal_Calculator.xlsx."
Figure 2 shows the clock tree diagram as found in the STM32H503 reference manual, RM0492. The tree diagram depicts all system clock distributions. It stats from the source and moves through PLL and the prescalars, then ends at the consumer (bus and peripherals).
For most applications, the clock frequencies provided by the resonators are not sufficient, therefore we must use another module to increase the clock speed. The PLL (clock feedback control system) increases the clock frequency to satisfy the peripheral clock speed requirement.
Figure 2: RM0492: Clock tree diagram that is typically found inside the reference manual of any STM32.
Table 2 describes the input and output shown in figure 2.
Table 2: RCC clock I/O signals.
Pin name |
I/O |
Comment |
LSCO |
Output |
Low-speed output clock for external devices |
OSC32_OUT |
Output |
Used as a GPIO or connect to one end of the resonator terminal, if designing an external low speed resonator for your design |
OSC32_IN |
Input |
This can be connected to an input clock from an external module. Or this pin can be connected to a second terminal of the resonator. Applicable if designing a external low speed resonator for your design |
OSC_OUT |
Output |
Similar to OSC32_OUT, but for high-speed resonator |
OSC_IN |
Input |
Similar to OSC32_IN, but for high-speed resonator |
AUDIOCLK |
Input |
External kernel clock input for I2S1, I2S2, and I2S3 |
MCO1 |
Output |
This pin is used to direct the clock to an output pin. See the clock tree for details about which clock sources are connected to this pin. |
MCO2 |
Output |
Same as MCO1. |
Figure 3 illustrates an example of the STM32H503RB MCU clock tree, which can be found under the “Clock Configuration” tab of STM32CubeMX "configurator." A convenience regarding the clock configurator is that you do not have to perform any calculations for your desired clock speed. Any potential clock conflicts are automatically resolved by this tool.
Figure 3: STM32CubeMX clock configurator tab for the STM32H5: Clock tree diagram as shown in STM32CubeMX interface.
In figure 3 above, the CPU clock is configured by the blue HCLK box near the top of the STM32CubeMX clock configuration tree.
To enable external resonators, we must select the “Crystal/Ceramic Resonator” option inside the RCC peripheral. Through RCC, we can also output the generated clock from the clock tree through master clock output 1, 2, LSCO, and SAI1 extern clock. Clock recovery system (CRS) is used to trim the frequency (up or down) when a resonator frequency leaves a set threshold.
Figure 4: STM32CubeMX configurator: enable HSE and LSE through RCC peripheral.
An example of a generalized view on one of the PLL modules is shown in figure 5. In the case of STM32H503, there are two PLLx modules (PLL1 and PLL2). PLL1 is generally used to provide the clock to the CPU and some of the peripherals. PLL2 is used to generate the kernel clock. See section 8 "kernel clock" for more details on kernel clock for the peripherals.
Figure 5: PLL feedback control system used to increase resonator clock speed.
Figure 6 shows the PLL module configuration inside the STM32CubeMX clock configuration tab. Through the parameters M, N, R, Q, P, we can increase or decrease resonator clock speed in fine increments.
Figure 6: STM32CubeMX clock configurator tab for the STM32H5: PLL (PLL1 and PLL2).
This concludes the first part of STM32 MCU clock introduction. In part 2, we discuss additional features and provide an example code generated by the STM32CubeMX HAL generator tool.