STM32 gotchas
200. LSI is not a good clock to keep real time in RTC

STM32 are equipped by several oscillators, among others all STM32 contain a low-frequency low-power RC oscillator marked LSI. LSI is one of the possible input clocks to the RTC.

However, as is the case with RC oscillators, LSI is not precise. In STM32Fxxx, its frequency is given as nominally 40kHz, but with min/max range 30kHz..50kHz. That is 25% error, or 250'000ppm 1, and if an LSI running at some of the extremes would be used for timekeeping, the clock would be off by 15 minutes per hour.

LSI in STM32Lxxx and STM32Gxxx are better off - they have a nominal frequency of 32kHz and at ideal conditions (room temperature and nominal supply voltage) deviate at most 0.96kHz from it, i.e. 3% or 30'000ppm (in worst case across whole temperature and voltage range, the maximum deviation is 2.5kHz, 7.8%, 78'000ppm). While this is way better, it's still completely useless for real time keeping, as clock could be off around 2 minutes per hour.

While other RC oscillators (HSI, MSI) in STM32 do have some means of adjustment (albeit quite crude), there is no such option for LSI, so it can't be made "better" by calibrating/comparing it to a known precise clock source.

Also, LSI is in the VDD power domain, not the VBAT domain 2; so when VDD is removed, it stops working, and the clock "freezes" even if VBAT is supplied.

So, why is LSI one of the RTC inputs? Well, in some applications, there is no need for precise timekeeping, but the application has to be very low power, spending most of the time in sleep, waking up from time to time for a burst of activity. For wakeup, a very low consumption clock source is needed, and also a very low power consumption counter driven by that clock and capable of waking up the processor in adjustable time periods. And the LSI+RTC combination is exactly this: LSI has an extremely low power consumption, and so is RTC (partially thanks to its asynchronous prescaler).

And is there any possiblity to maintain real time without having a 32.768kHz crystal or oscillator on LSE?

One option is to drive RTC from the HSE/n input, if a crystal or oscillator is present on HSE. Another option is to use some external precise clock source (e.g. 1Hz from GPS receivers) and feed it into a timer used to count real time (and calculating calendar e.g. using facilities of <time.h>). These solution don't work in VBAT mode, though.


1. To put this into perspective, LSE crystal oscillator can with moderate effort be made accurate to 20ppm, resulting in an error of 12 seconds per week.

2. Except in the rather new 'U5 family.