STM32 Clock Configuration

Posted by

I discussed about basic hardware sanity, check in   Importance of LED   (Click to check it out)

In this new session, i’m discussing about the mechanism of clocking present in the embedded ICs,

which according to me is a very vital concept from board bring-up perspective.

Here we are using the STM32 board, …well worried? never used it before? lucky you this board gives you the simplest and best understanding of the why’s n how’s of clocking.

Here are some of the basic questions, that will help you have a better understanding.

What clock sources are available in STM32L4R5ZI device ?

  • There is provision of low speed, high speed and multi-speed clock sources that can be generated through internal RC oscillators.
  • If you are looking for accuracy you may use external crystal for both low and high speed options. There is a provision of PLL and prescalers to generate a range of clock frequencies.
  • These are named as LSI/LSE, HSI/HSE and MSI. This gives flexibility to choose, various frequencies for the system to achieve both speed and power effectively.

What clock source options are implemented in Nucleo-L4R5ZI board ?

Here are the snapshots of  Board Schematic.  You may notice 32 Khz crystal connected to STM32 device.

X3 crystal for HSE is not installed and corresponding connection is open as well.

Both LSE and HSE options are visible on the top side of board

Is there any utility/tool available for clock configuration ?

As there are multiple clock configuration options , So the GUI based tool STM32CubeMX is the best option to configure and generate required code.


How to verify the clock validity ?

  1. Using Hardware: You can probe  MCO and LSCO pins for verification during board bring up.  You can also use these output signals for any user application purpose .
  2. Using software: Your can refer to default startup source code created by TrueStudio for clock configuration. By default, MSI ( 4 MHz ) is used as system clock source after system resets.

Additionally , the code given below can be used to verify LSE .

How to enable LSE( Low Speed External ) Crystal ?

One can enable LSE only after disabling it;s write protection using CR1[DBP] bit of PWR block .

#define CHK_LSE_VALID ( (RCC->BDCR) & (0x02) )

void enable_LSE()

RCC->APB1ENR1 |= 0x10000000 ; //PWR interface clock enable

PWR->CR1 |= 0x100; //Enable writing to backup register with DBP

RCC->BDCR |= 0x01; //LSE enabled

PWR->CR1 &= 0xfffffeff ; // PWR interface clock disabled


void main() {


while (!CHK_LSE_VALID) ; // Check RCC->BDCR[LSERDY] bit   . Code will stuck here if LSE is not valid


Here is the GIF file of this code at run-time in LPUART program

Here is the video related to this article :

Thanks for your time to read this. I hope you found this article useful.


You may  find interesting topics at HowTo

. Please provide your feedback regarding this article. Also subscribe Embedkari for other interesting topics. .

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.