Set RTC from 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.
GJS
Posts: 5
Joined: Sat Apr 28, 2018 6:55 pm

Re: Set RTC from GPS

Post by GJS » Tue May 15, 2018 10:08 am

Thanks for your input, Peter. I looked at your code for the DS3231 with interest. Your functions to get milliseconds from the pyboard RTC subseconds value and calculating the cal factor are more efficient than mine so I will plagiarise if you don't mind.

I found that the time between the beginning of the PPS ISR routine and the start of the micropython.schedule() event is around 35us. It takes around 280us to set the RTC with pyb.RTC.datetime(). Hopefully, this operation is atomic and won't get interrupted by garbage collection. I haven't tried setting the cal factor yet but if this is quicker the ultimate accuracy should be better than 280us. Of course, the RTC only has ~4ms resolution so practical use (timestamping log entries in my case) will mean this accuracy is more than adequate.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Set RTC from GPS

Post by pythoncoder » Tue May 15, 2018 11:43 am

The worst-case time from an ISR to running schedule can be on the order of a few ms if the interrupt occurs while a GC is in progress.

You are, of course, welcome to use and copy my code.

I'm developing an asynchronous GPS device driver and one of its functions is to calibrate the Pyboard RTC. It's a work in progress and not yet fully tested but you might like to look at the gps directory in my async repo.

There are one or two potential "gotchas". I do not enable the PPS interrupt until I've received a valid time message from the GPS. I aim to ensure the code will work if the time rolls past midnight: this is why I use μs since Y2K as timing values (thanks to Python support for arbitrary precision integers). To get an accurate calibration factor in as short a time as possible I try to account for the low (4ms) precision of the Pyboard RTC by timing (in μs) a transition of its subsecond value. So I derive the time of the PPS pulse leading edge as you suggest, and also compute the time that the RTC would have read when that edge occurred if it were capable of μs precision. I can then measure the drift between the RTC and the GPS to a higher precision than the RTC actually supports. This method seems to work on my DS3231 and GPS solutions.

Handling GPS outages is something I've yet to investigate: I know messages stop but I'm not sure what happens to the PPS signal during outages.

[EDIT]Before you tear out too much hair you might want to check out this issue. I couldn't figure out why, however I tweaked my code, the RTC was never quite in line with the GPS...
Peter Hinch
Index to my micropython libraries.

Post Reply