STM32L152 – Clock Overview

This section goes through the STM32L152’s clocks, what they are and how to set them up.  Code examples will include directly writing to the registers as well as HAL examples, though I feel its really important to try not use framework libraries if you really want to understand the processor. When you know it well or well enough then its a lot easier to step through framework code and understand what going on or, as is more the case, why something is not working.

Getting to know the clock architecture of an MCU is really important – without understanding the clocks you will always be frustrated as to why things may not work as expected.

Most MCU’s have a number of clocks that are used to drive different aspects of the peripherals.

The ‘Clock Configuration’ tab in ST’s STM32CubeMX is a great interactive tool to get to know the clocks. Of course, this application is used to help configure and auto-generate the code necessary to initialise the device, but it is also great just as a reference. Be sure to configure MCO and the external clocks in the Pinout Tab so that the full diagram is interactive. I find this diagram far easier to take in than the clock tree in the reference manual.

stm32CubeMX clock diagram

System Clock – This is the main clock that drives the main processor and is often used to drive everything else from the main program to some or all the peripherals.
PLL clock – This is a ‘virtual clock’ in that it is driven by another clock but multiplies the clock frequency by a specific number to gain a much faster clock rate. As an example, the STM32L152 has an internal 16Mhz clock but needs 48Mhz for the USB. The PLL lock can then be used to multiply up the 16

There are 4 main clocks on the STM32L152

HSE – High Speed external – requires an external oscillator
HSI – High speed internal – On this processor its a 16Mhz clock
MSI – Multi speed internal – there are different pre-set clocks speeds to choose from
LSE – Low Speed external – this is usually a 32.768Kttx for the Real-time Clock
LSI – Loe speed internal clock – 32Khz
PLL – Phased locked loop clock for generating frequencies higher than the system clock

 

Internal vs external 

You can either use the internal oscillator or provide your own crystal/resonator. The difference is usually down to accuracy. If your application requires very specific and accurate timing then use an external crystal at the frequency you require.

The internal oscillators are useful when frequency accuracy is not that important and also in that, you have less external components.  In the case of this processor, which has a Real Time Clock (RTC), the frequency is important, but it also allows for an external Low-speed clock for this purpose, so no need to two external crystals.

High-Speed Clocks

The internal or external high-speed clocks are generally used as the main system clock that drives the CPU and the peripherals. The internal clock is set (in this case) to 16Mhz whereas other devices will have different internal clocks. The STM32L4 series, for instance, has an 84Mhz internal clock.

MSI – Multi-Speed Internal clock

This processor contains a Multi-speed clock offering the following frequencies

Frequency Hz Hex Value Some Applications
65.536 Khz 216 0x10000 Real-time Clock (twice the frequency of the standard 37.768Khz)
131.072 Khz 217 0x20000 Radio, Navigation,Communications, Radio Tags
262.144 Khz 218 0x40000 High Precision clocks
524.288 Khz 219 0x80000
1.048 Mhz 220
2.097 Mhz 221
4.194 Mhz 222

 

None of these frequencies will be exactly accurate, however, each frequency offers useful divisions so that counters can be easily used. There are calibration and trim registers to help tune the frequencies.

 

Low-Speed Clocks

The low-speed clocks are used to drive the RTC real time clock) and the LCD controller. The external clock is a 32.768 kHz crystal can provide a very accurate clock for time requirements such as the RTC.

32.768 is used as its 2^15 and hence a 15 bit counter can be used to derive a 1Hz clock pulse.

The Internal Low-speed clock is fixed at 37kHz and hence not as accurate, however, if you were only using the LCD controller and has no need for the real-time clock then 37kHz is more than sufficient and a low power clock.

 

PLL Clock

The PLL or Phased-locked loop clock is a different kettle of fish altogether. It does not have its own resonator but is driven by the HSI or HSE. This clock is used to generate clock speeds greater than the input such as the 48MHz clock speed required for USB communication. As the HSE’s maximum frequency (in the case of the STM32L251) is 24Mkz, the PLL is used to generate the higher clock frequency.

Using the Multiplier, the PLL clock can generate up to 384Mkz. Note though that PLL cannot drive the main System-Clock at that speed. The maximum would be 32Mhz using PLL multiplier of 4 and PLL divider of 2