measuring crystal ppm with GPS

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

measuring crystal ppm with GPS

Post by manitou » Fri Mar 13, 2015 3:49 pm

I measured the frequency difference of the pyboard main (8MHz) crystal against a GPS pps on X1.

Code: Select all

# GPS pps interrupt on X1
import pyb
tick =0
us = 0

def callback(line):
	global tick,us
	us=pyb.micros()
	tick=1

pin = pyb.Pin('X1')
pyb.ExtInt(pin, pyb.ExtInt.IRQ_RISING, pyb.Pin.PULL_NONE, callback)

prev=0
while(1):
	if (tick) :
		t=us-prev
		print (t, t-1000000, ' ppm')
		tick = 0
		prev = us

GPS is EM-406A (sparkfun or adafruit). Connect with 5v/grnd and pps to X1. The GPS requires 5v, but the TX and pps signals are only 2.85v so you don't need levelers. The pyboard crystal (I believe) is spec'd at 30ppm. I measured 31 ppm with the program above.

Using a long running simple python program to print pyb.millis() every 10 seconds to a host serial port that was running NTP, i measured pyboard frequency offset of 32 ppm.

Using the same NTP test harness but converting the RTC output to "milliseconds" (it has a 1/256 subsecond resolution), I measured the frequency offset of the 32khz crystal at 170 ppm. I think the 32khz pyboard crystal is spec'd at 50ppm. (The RTC hardware has the ability to prescale and calibrate the 32khz time soure, so the RTC frequency could be "adjusted".) Voltage, capacitance, age, and temperature can effect crystal frequency.

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: measuring crystal ppm with GPS

Post by blmorris » Fri Mar 13, 2015 4:27 pm

There was a pretty long-winded discussion on this topic here back in October: http://forum.micropython.org/viewtopic.php?f=2&t=326

Your measurement of RTC drift matches pretty closely what some of us have found, IIRC. It isn't clear to me why it should be so far off, I will need to read the thread again to remember if anyone had any good suggestions.

Your post does remind me that I never did figure out how to use the RTC calibration mechanism - something to revisit when I get a little time.

-Bryan

manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

Re: measuring crystal ppm with GPS

Post by manitou » Fri Mar 13, 2015 4:46 pm

Obviously I hadn't seen the earlier thread, thanks for pointing that out. There have been lots of discussions on teensy forum (user constatin) on the effect of temperature on RTC frequency, and he has developed code to measure temperature and adjust RTC frequency accordingly (teensy ARM has same sort of prescale/calibration registers and you can alter the capacitance). Over the years I have measured ppm on various MCU's, RTC's, and TCXOs, anecdotal trivia here
https://github.com/manitou48/crystals/b ... ystals.txt

I wonder if the pyboard 32khz capacitors (2.2pf) are undersized, that might account for the high frequency...

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: measuring crystal ppm with GPS

Post by blmorris » Mon Mar 16, 2015 2:40 am

manitou wrote:Over the years I have measured ppm on various MCU's, RTC's, and TCXOs, anecdotal trivia here https://github.com/manitou48/crystals/b ... ystals.txt

I wonder if the pyboard 32khz capacitors (2.2pf) are undersized, that might account for the high frequency...
Thanks for the link - you have done quite a bit of testing on different RTC's, thanks for taking an interest in the pyboard!
I need to take another look at the RTC system on a board I designed which runs MicroPython - the crystal and capacitors are different, but I don't recall if the discrepancy was similar.

You mentioned the discussions on the teensy forum - https://forum.pjrc.com/threads/24628-In ... ature-Data
That is some really cool work that constantin has done - I wonder if it would be possible to use the STM32F405's onboard temperature sensor to do similar temperature compensation for the pyboard RTC. In my experience with the pyboard temperature measurement, it usually only reads a few degrees above ambient, since the processor itself produces so little heat in most cases.

manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

Re: measuring crystal ppm with GPS

Post by manitou » Mon Mar 16, 2015 10:14 am

I wonder if it would be possible to use the STM32F405's onboard temperature sensor to do similar temperature compensation for the pyboard RTC. In my experience with the pyboard temperature measurement
I think one should be able to use the internal temp sensor. in my experience, you sometimes need to calibrate the parameters of the temp sensor. And you would probably need to derive the quadratic coefficients of the temperature response of the RTC crystal if your operational environment spans a wide temperature range.

manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

Re: measuring crystal ppm with GPS

Post by manitou » Fri Mar 20, 2015 4:21 pm

Re: RTC drift

seeing there was recent update to RTC in firmware, I verified that LSE is enabled for RTC

Code: Select all

>>> print(hex(stm.mem32[stm.RCC + stm.RCC_BDCR]))
0x8103
Looks good. RTC is running from LSE/32khz crystal.

crystal's capacitors are still the primary suspect in 170 ppm frequency error.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: measuring crystal ppm with GPS

Post by Damien » Sat Mar 21, 2015 12:27 am

The crystal is a st3215sb32768b0hpwb3 which has a rated frequency tolerance of 20ppm.

@manitou are you able to post your "long running NTP" code so I can have a go at running it? I can try different capacitor values to see if that is the problem. I can also try different boards to see what the variation is.

manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

Re: measuring crystal ppm with GPS

Post by manitou » Sat Mar 21, 2015 1:23 am

Damien wrote: @manitou are you able to post your "long running NTP" code so I can have a go at running it? I can try different capacitor values to see if that is the problem. I can also try different boards to see what the variation is.
take a look in https://github.com/manitou48/pyboard

if the RTC crystal is really off systemically by 170 ppm, you also could probably observe the drift by comparing long running difference between pyboard micros and RTC "millis" (1/256 seconds) -- a self contained test, duelling crystals. The accuracy of the drift estimate improves with the duration of the test. Typically, the drift will settle within 15 to 30 minutes.

The NTP host I use for the hostdrift.c program actually has a stratum 0 NTP clock (cellular based), but a long running NTP host typically disciplines its frequency to within 1 ppm

A GPS breakout with a pps signal works nicely too -- (gpsrtc.py)
https://github.com/manitou48/pyboard/bl ... /gpsrtc.py


Edit: code to use pyb.millis() to check RTC frequency, see
https://github.com/manitou48/pyboard/bl ... ftrtcms.py

and another variation, using RTC callback and pyb.micros() (NOTE: MCU crystal will have its own drift afffecting micros())
https://github.com/manitou48/pyboard/bl ... ftrtccb.py
Last edited by manitou on Wed Nov 18, 2015 1:05 pm, edited 3 times in total.

manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

Re: measuring crystal ppm with GPS

Post by manitou » Sat Mar 21, 2015 1:34 am

Damien wrote:The crystal is a st3215sb32768b0hpwb3 which has a rated frequency tolerance of 20ppm.
From the pyboard schematic PYBv10b.pdf , the 32khz crystal was listed as
9HT10-32.768KBZF-T 6pf (2.2pf) 50ppm ?

manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

Re: measuring crystal ppm with GPS

Post by manitou » Sat Mar 21, 2015 1:41 am

Damien wrote: I can also try different boards to see what the variation is.
in earlier thread, another anecdotal data point, bimorris measured his RTC crystal at 135 ppm

http://forum.micropython.org/viewtopic. ... t=20#p1803

Post Reply