Teensy 3.0 RTC drift

Status
Not open for further replies.
Hello,

I purchased a 32.768kHz crystal (http://www.dipmicro.com/store/XC4-32768) and soldered it to the board. It seems to work as expected but has a HUGE amount of drift. Is it the crystal, temperature, or both? Over the course of 30 minutes it has drifted about 40 seconds slower - that's even after using Teensy3Clock.compensate(2000); in my code! I'm setting it using the same code in the Time example and getting my unix timestamp on my mac by running:
Code:
date +%s

Thoughts? Is this normal? The room I am testing this in is unfortunately in the upper 80s Fahrenheit so that could be contributing but I feel like compensating 250ppm should make this better...

Thanks in advance!
 
Last edited:
Could some I/O support software be disabling interrupts too long?
If it were in the baseline code, it would affect everyone.
(of course, it could simply be an out-of-spec crystal or capacitors that are needed at the crystal's leads).
 
For what it's worth I've had a crystal on my Teensy 3.0 since early July. I synced it when I got it running and haven't updated it or used rtc.compensate(). The board's been in a car on drive through the Nevada desert twice (Southern California to Idaho & back) and mostly lives in my un-airconditioned shop (currently 80 degrees). I just checked and it's lost about 28 seconds. Not stellar, but certainly within the 20ppm spec for running 2 months. So I agree with stevech -- I think your crystal is bad.

[edit - I'm using the crystal Paul recommends - http://www.digikey.com/product-detail/en/CFS206-32.768KDZF-UB/300-8303-ND/862580]
 
Sounds like a bad crystal.

Or....

Could some I/O support software be disabling interrupts too long?

The RTC does not use interrupts to keep time. The normal millis() count does, but the RTC increments completely in hardware.

(of course, it could simply be an out-of-spec crystal or capacitors that are needed at the crystal's leads).

The capacitors are build inside the chip. They are software adjustable in 2 pF steps. The default setting assumes a 12.5 pF crystal.

Even if this is a 6 pF crystal, it still seems pretty far outside of spec. But maybe editing the code to reduce the capacitance on the pins might help?
 
The capacitors are built inside the MK20 chip. You can configure the capacitance using the CTR_CR register. Look for this code in hardware/teensy/cores/teensy3/mk20dx128.c

Code:
        // if the RTC oscillator isn't enabled, get it started early
        if (!(RTC_CR & RTC_CR_OSCE)) {
                RTC_SR = 0;
                RTC_CR = RTC_CR_SC16P | RTC_CR_SC4P | RTC_CR_OSCE;
        }

This RTC oscillator is documented in the reference manual, in chapter 26. The actual hardware configuration info regarding the capacitance is in chapter 29, on page 870.
 
I have some results where I varied RTC_CR (capacitance) and RTC_TCR (compensation) or Paul's rtc_compensate() for a 32khz crystal. The sketch commnunicated with an NTP host to check the frequency of the crystal (or you can use a GPS or TCXO or even micros()). Some results are in crystals.txt at
https://github.com/manitou48/crystals
Also there is a simple sketch teensyRTC.pde

You should be able to discipline your crystal to a suitable frequency unless it's is badly out of spec.
 
Depends also a bit on the local temperature. You can certainly help compensate the temperature drift using a NTP, GPS, or similar signal, but only for a given crystal temperature.

If the temperature of the RTC crystal is going up and down more than 5*C, you will likely have measure the local temperature and then compensate with an open-loop control based on previous measurements across a wide range of temperatures. I used a PPS signal from a GPS receiver for my solution. Using dry ice, I got a wide range of temperatures and ended up with a compensation function with a very high R^2 factor. The key thing is to vary the temperature slowly so the sensor and crystal can keep up.

If you don't want to fiddle with open-loop control and temperature sensors despite changing ambient conditions, I highly recommend the DS3231 real time clock, which is temperature compensated. Macetech makes the excellent Chronodot using that chip and it's wonderful and easy to use. The accuracy is around 2PPM, which is more than good enough for most folk.
 
I have to say I love using the Teensy3 because it does so many things well and there is great info in this forum to help figure things out. I was just trying to determine how the RTC oscillator capacitance load should be set and found this post. It covers the exact issue I'm interested in and has detailed info from Paul in this post I am responding to plus an amazing study referenced by @manitou further down. That study shows that the capacitance load setting Paul used in the BSP code results in the lowest measured clock drift. I'm just a bit confused because the recommended Digikey 300-8303-ND 32KHz oscillator that everyone (me included) seems to use has a stated load of 12.5pF. So I'm wondering why the load setting of 16+4=20pF that is used in the BSP code works best. Why wouldn't RTC_CR_SC8P | RTC_CR_SC4P be the best? We have other Teensy 3 boards that we want to put other crystals we have on that have different loading specs and I wanted to set the right load for them too.
 
Isn't this dependent on the specific crystal part number and perhaps lot number (assuming room temperature as the long term average)?
 
So, I actually sort of found my own answer to my question. The post at:
http://www.lpcware.com/content/faq/how-calculate-value-crystal-load-capacitors
Talks about this exact issue. In Table 16 of the data sheet for the processor used on the Teensy3 it says that the "typical" stray capacitance is 5pF. Using the equation that is referenced from the AVR app note that C=2*CL-Cstray then the setting of 20pF Paul uses is "right on". However, using the equation C = 2(CL - Cstray) that does seem to be referenced on more/most web sites then a C value of 15pF would seem to be correct. But as @manitou measured in his results, a 20pF setting like Paul uses works best in the real world. So I guess that's the right setting to use.

But then there is the issue of setting the load for the 16MHz main oscillator. @manitou indicates that on the Teensy3 the main oscillator is an NDK NX3225SA-16 chip. That oscillator has a CL value of 8pF. And the MCU data sheet doesn't specify a Cstray value for the "high frequency" oscillator. But the Teensy3 BSP code sets the C value for that 16MHz oscillator to:
Code:
	// enable capacitors for crystal
	OSC0_CR = OSC_SC8P | OSC_SC2P;
So I guess that if Cstray for the main oscillator is 6pF then the setting is optimum based on the AVR C=2CL-Cstray equation that worked for the RTC. I'm not sure that setting is "right" but that's how it's set for what that's worth.
 
Last edited:
the recommended Digikey 300-8303-ND 32KHz oscillator that everyone (me included) seems to use has a stated load of 12.5pF. So I'm wondering why the load setting of 16+4=20pF that is used in the BSP code works best. Why wouldn't RTC_CR_SC8P | RTC_CR_SC4P be the best?

The capacitive load at each pin needs to be double the crystal's specified capacitive load.

The two capacitors both connect to ground. To the crystal's point of view, they're connected in series (the crystal doesn't "know" about ground). To get 12.5 pF in parallel with the crystal, the capacitance at each pin needs to be 25 pF.

The pins have approx 5 pF capacitance, due to their shape, pcb trace, bond wire, but mostly from the on-chip metal pad for the bond wire, which is very close to the chip's substrate. This 5 pF adds to the 20 pF from the on-chip capacitors to give the required 25 pF.
 
Last edited:
The capacitive load at each pin needs to be double the crystal's specified capacitive load.

The two capacitors both connect to ground. To the crystal's point of view, they're connected in series (the crystal doesn't "know" about ground). To get 12.5 pF in parallel with the crystal, the capacitance at each pin needs to be 25 pF.

The pins have approx 5 pF capacitance, due to their shape, pcb trace, bond wire, but mostly from the on-chip metal pad for the bond wire, which is very close to the chip's substrate that, which is connected to ground. This 5 pF adds to the 20 pF from the on-chip capacitors to give the required 25 pF.

Hey Paul, Thanks for the quick answer. And as I would expect from all the great design in your products it matches the other information I had found. So I now know that if we use other RTC oscillators on our other boards I'll set the capacitance setting in RTC_CR to 2*CL-5. Great to have that resolved. Thanks!
 
Status
Not open for further replies.
Back
Top