Showing results for 
Search instead for 
Did you mean: 

Anyone manage STM32L0 RTC calibration?

Scott Dev
Posted on April 13, 2017 at 19:21


  I am working on using the RTC, and have a project coming up that requires to run from battery for a couple of years. I will be using the RTC for tim stamp information. I know I will have to keep calibrating the RTC occasionally , and looking a the reference manual I see the below code. I dont have any input I can use for calibration (50Hz mains etc), and wondering if anyone has any code that can work out how to do this without any external reference input?

Many Thanks


/* (1) Write access for RTC registers */

/* (2) Enable init phase */

/* (3) Wait until it is allow to modify RTC register values */

/* (4) set prescaler, 40kHz/125 => 320 Hz, 320Hz/320 => 1Hz */

/* (5) New time in TR */

/* (6) Disable init phase */

/* (7) Wait until it's allow to modify calibartion register */

/* (8) Set calibration to around +20ppm, which is a standard value @25°C */

/* Note: the calibration is relevant when LSE is selected for RTC clock */

/* (9) Disable write access for RTC registers */

RTC->WPR = 0xCA; /* (1) */

RTC->WPR = 0x53; /* (1) */

RTC->ISR = RTC_ISR_INIT; /* (2) */

while((RTC->ISR & RTC_ISR_INITF)!=RTC_ISR_INITF) /* (3) */


/* add time out here for a robust application */


RTC->PRER = (124<<16) | 319; /* (4) */

RTC->TR = RTC_TR_PM | Time; /* (5) */

RTC->ISR &=~ RTC_ISR_INIT; /* (6) */

while((RTC->ISR & RTC_ISR_RECALPF) == RTC_ISR_RECALPF) /* (7) */


/* add time out here for a robust application */


RTC->CALR = RTC_CALR_CALP | 482; /* (8) */

RTC->WPR = 0xFE; /* (9) */

RTC->WPR = 0x64; /* (9) */
Posted on April 13, 2017 at 19:46

Are you using a 40 KHz LSE? Or an 32.768 KHz one? Or are you using the LSI?

Short of having some higher precision TCXO you can turn on/off to use as a HSE input, I'm not sure how you're going to be benchmarking anything.

Can your production test measure the frequency as built, at nominal temperature, and burn that in some calibration data structure, along with serial numbers, etc?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 13, 2017 at 20:31

Hi Clive,

  I havent started the design yet, Im still getting used to the STM32. I have come from another processor where I have had many projects using external RTC's, and know how accurate an external XTAL can be. I will be using an external 32.768Khz XTAL. As I will be capturing information for a few years, the accuracy will drift. We have been using 20ppm xtals in previous projects and will use the same. The below shows how accurate it can be.

These watch crystals have +/-20ppm tolerance at 25 degC – so if the device is at constant ambient, one would expect no worse than 1 second in roughly 14 hours (20ppm = 1 second in 50,000 seconds).

My previous projects has shown 1 second gain every 12-14 hours, which is good , but as we plan to run for years then this will be way out by a few years .I have found AN934, which discusses a 512Hz to carry out calibration. Anyone use this method and how successful it is? Even though I havent designed the board yet, I know I will have to carry out calibration .



Posted on April 13, 2017 at 21:49

I'm not using L0 parts. My recollection of the calibration that can be done is that you have course control via the prescaling, and fine control via clock stuffing or removal over a longer count.

On the F2/F4 it is possible to benchmark the LSE cycle, over 1, 2, 4 or 8 periods on TIM5 using the HSE/PLL as the measuring clock. If one took a test station with a GPS sourced 10 MHz (or whatever) one can quantify the accuracy of the LSE. These parts could also be clocked at several hundred MHz, and that clock benchmarked against a 1PPS (input capture on TIM2 counting cycles of 84 MHz). With some sort of established timeline one could also look at the 1Hz out of the RTC, or for that matter hours or minutes, to quantify the performance of the RTC.

I suspect the LSE might be off frequency, but not flailing around all over the place, like say the LSI appears too be.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..