Compensate for LSE error

Post here first, or if you can't find a relevant section!
dolfandringa
Posts: 20
Joined: Wed Jun 03, 2020 3:47 am

Compensate for LSE error

Post by dolfandringa »

Hey all, I am using a custom board with an STM32F103C8 with an LSE and HSE clock using the STM32 core and the stm32duino RTC and LowPower libraries. I am putting my MCU to sleep using stop mode (deepsleep in stm32duino Low Power lingo) and wake up from an RTC alarm. Although the sensor says it will sleep for 600 seconds, it sleeps for 630 seconds. So it looks like my LSE clock is off by 5%. The RTC is also updated as if only 600 seconds passed (instead of the 630 seconds). I am guessing (but haven't spent time investigating yet) that my LSE clock is somewhat off, which I will investigate further and fix. But in the mean time, is there a way I can compensate for this in software? Just sleeping for less time doesn't help since the RTC clock is still off. Is there a register or something where I can modify how many clock ticks there are per second for the LSE clock?
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Compensate for LSE error

Post by dannyf »

unless you are using something truly bad, those crystals are spec'd to 20ppm and usually within 5ppm. so 5% is a little extraordinary.

I would first verify the cause. if it is indeed in the crystal, the hardware does allow LSE compensation - look to the reference manual. but unlikely to the extent of 5%. so you may have to use software means instead. to do that will require further understanding of your code structure.
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: Compensate for LSE error

Post by stevestrong »

On the blupill board one had to remove the pins (if soldered on PC14/15) in order to have correct speed. So take care of short lines to LSE crystal.
Does HSE have the same drift as LSE?
Last edited by stevestrong on Fri Dec 25, 2020 1:18 pm, edited 1 time in total.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Compensate for LSE error

Post by ag123 »

in case you are using libmaple core, you may want to check out RTCAdjust in the examples
https://github.com/rogerclarkmelbourne/ ... /RTCAdjust
dolfandringa
Posts: 20
Joined: Wed Jun 03, 2020 3:47 am

Re: Compensate for LSE error

Post by dolfandringa »

dannyf wrote: Fri Dec 25, 2020 2:18 am unless you are using something truly bad, those crystals are spec'd to 20ppm and usually within 5ppm. so 5% is a little extraordinary.

I would first verify the cause. if it is indeed in the crystal, the hardware does allow LSE compensation - look to the reference manual. but unlikely to the extent of 5%. so you may have to use software means instead. to do that will require further understanding of your code structure.
Yeah, 5% is very large indeed. I don't get it either. It seems to only happen when the MCU is sleeping. When I tell the MCU to sleep for 10 minutes, it sleeps for 10 minutes and 30 seconds. The RTC is also off by 30 seconds at this time, and that RTC error is accumulating across sleep cycles.
I am using a Seiko SC32S 7pF 20ppm crystal, with 8pF capacitors (assuming a 3pF stray capacitance). The OSC32_OUT trace (the longer one) is 4mm in length.
STM32 clock.png
STM32 clock.png (12.44 KiB) Viewed 5162 times
You say I can compensate for an LSE error in software, but I can't find that in the datasheet. it's a bit hard to find this info if you don't know exactly what to look for. Do you have any clues as to what function or register(s) I need to use?
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: Compensate for LSE error

Post by mlundin »

I dont have the hardware to test but if the LSE runs correctly when not sleeping then my wild guess would be that the core somehow switches to LSI when sleeping.
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: Compensate for LSE error

Post by ozcar »

dolfandringa wrote: Mon Jan 04, 2021 5:54 am ...
You say I can compensate for an LSE error in software, but I can't find that in the datasheet. it's a bit hard to find this info if you don't know exactly what to look for. Do you have any clues as to what function or register(s) I need to use?
I have never tried to use the RTC on any STM32, so I did not even know it was possible to compensate for crystal frequency.

However, by taking a quick look at the link you were given above by ag123, and the AN2604 note that is mentioned there, it seems the register you seek is BKP_RTCCR. Due to the way that works, it looks like you might also have to change the RTC prescaler to allow for both "faster" or "slower" adjustments.

Given you are using the official STM32 core, you can't just pick up the code given there, but before attacking the registers directly, you could maybe look to see what HAL functions are provided.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Compensate for LSE error

Post by dannyf »

i would first make sure that the hardware works - the right crystal is used, and the LSE oscillates in run mode correctly, ...

I would then try to find out if the code is wrong -> it would be unthinkable if this is a (chip) hardware problem.

all else fails, I would try to correct the problem. the simplest would be to scale down the time. rather than setting the clock up to expire in 10 minutes (600 seconds), set it to expire in 540 seconds...
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Compensate for LSE error

Post by ag123 »

oh and did you connect VBAT to a 3v e.g. cr3032 coin cell? that would probably keep the rtc running on its own, otherwise you may have found a bug in code or on the chip somewhere

normally the trouble with crystals is those 'cheap' boards with cheap 32k crystals, for reasons i'm unsure, those can vary quite a bit
https://www.aliexpress.com/af/32768.htm ... 68&catId=0
i've a crystal where the error is 1/3000, that makes the drift way out from those ppm specs it is well more than the 5 minutes which the onchip registers can compensate for on stm32f103. hence, for that i used my rtcadjust method to make the adjustments. that makes it rather usable if you can make do with perhaps seconds of drift each month. the idea is to call synctime(time_t time_now) with accurate clock time to set the epoch, and calling void calibratertc(time_t time_now) with accurate clock time again after a long time lapse, so that it examine the difference between the rtc time and your accurate clock time input to compute the 1/N error. in the most recent update to rtcadjust, i made it possible to call rtccalibrate() more than once. the computed error is stored in a backup register. and you sketch needs to call bkp_init(); adjtime(); in setup() to apply the drift adjustments.

technically i think it is quite possible to measure the rtc drift, e.g. by comparing it against the 8mhz HSE crystal to estimate the drift. takes a bit of work and i'd think it may be more direct than using my rtcadjust method to estimate the drift
ozcar
Posts: 143
Joined: Wed Apr 29, 2020 9:07 pm
Answers: 5

Re: Compensate for LSE error

Post by ozcar »

In my travels through RM008 and AN2604 yesterday, I noticed that you can get it to output RTC clock divided by 64 on the "TAMPER" pin (PC13?). If that pin is available, you would not need a very accurate counter to see if the frequency is out by as much as it appears to be.
Post Reply

Return to “General discussion”