Nanosecond clock sync with a Raspberry Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is a second to most people it's 186 thousand four hundredths of a day but if you ask someone at the nist us's national institute of standards and technology you'll get a different answer they define a second as the amount of time it takes to measure 430 trillion oscillations of strontium atoms when they're in an optical lattice but what does that matter to you how important can a second be if the only way to get a true measurement is to have millions of dollars of lab equipment well for many years large companies could buy things like these these are master clocks and these things can cost thousands of dollars they take in a super accurate time source like a gps radio signal that comes from the nist's fancy strontium clocks then they distribute it over a network using protocols like ptp or the precision time protocol using ptp also requires special network adapters and those can also cost thousands of dollars but what if i told you i can replicate that setup we can make time more accurate in networks all over the world using one of these a tiny raspberry pi compute module 4. well you can guess from the fact that i'm making this video i can thanks to the amazing work of people in the open source community this compute module has two time travel tricks up its sleeve it can send out a super accurate pulse per second and it can do something called hardware ptp timestamping for the most accurate network time you can get first let's talk about pulse per second a few months ago tom scott posted a whole video about how radio clocks work in britain the national physical laboratory or npl sends out radio signals to coordinate clocks the video has a great overview of how the npl synchronizes the second between different locations and it even talks about utc or coordinated universal time don't ask about why the acronym doesn't match the words in the u.s we also have a radio time signal that signal comes from a tower in colorado run by the nist time and frequency service the radio station is called wwv and you can even listen to it online this is what it sounds like at the tone 3 hours 54 minutes coordinated universal time [Applause] it's pretty trippy i have to admit i've had that on the background sometimes and it's definitely a blast from the past when you hear the recorded voices but here's the cool thing if you have any radio controlled clocks in your house maybe an alarm clock or a wall clock all those clocks use this signal at least in north america you put it in time zone and the clock will sync up after a few minutes it takes a minute or two because those beeps and ticks actually store all the data for a clock to know what year day and time it is in utc or at least nist's version of the utc the pps is easy to hear in that radio signal but you can't really hear it in a wire but thanks to some help from the team working on the time appliances project at meta i can show you what a pulse per second looks like on this oscilloscope the latest pios kernel has pps built in now and you can enable it on the compute module 4's built-in network interface that's this tiny little broadcom chip it'll output a signal precisely once every second over the compute module 4's sync out pin just as an aside since i know you'll ask the chip on the regular pi 4 model b and the pi 400 don't support this they're actually different than the one used on the cm4 but getting back to pps it's used in all kinds of timekeeping devices to synchronize them heck even aliens see the value of having a signal to coordinate time do you remember in the movie independence day that part in the beginning where the researchers hear the repeating signal after studying that signal a while jeff goldblum or whatever his character's name was in the movie figured out the signal was coordinating an attack they're positioning themselves all over the world using this one signal to synchronize their efforts in approximately six hours the signal is going to disappear and the countdown is going to be over well pps isn't going to bring about armageddon but the principle is the same but pps by itself isn't super helpful we also need to know what the actual time is and that's where ptp comes in like those series of boops and beeps in the wwv signal ptp has a lot more information so computers can sync up over a lan or local area network you might have already heard of ntp or network time protocol most computers use it nowadays in my max date and time preferences you can see it's using this apple time server to set the max clock but ntp has one major flaw it was built for a less stable network path since it's mainly used over the internet usually ntp can be accurate to under 50 milliseconds heck some specialized ntp apps can get accuracy down to one millisecond on a local network but that's still not accurate enough for many applications especially in finance manufacturing and science and that's where ptp comes in ptp stands for precision time protocol and it's useful for keeping time in sync over a lan but not on the internet anything that adds random latency like network switches that buffer packets or routers between networks will reduce ptp's effectiveness so it works best over simpler networks and you might ask why not just use gps well you can do that if you just have one computer but it gets expensive if you have a lot plus all those devices could still drift a tiny bit especially if the gps signal is lost so ptp keeps them in sync ptp sends a bunch of packets out and the slave devices can calculate network latency with extreme accuracy without any interference from the host operating system that's because ptp lives in hardware on the network chip itself and that's why only the cm4 can do it other pies don't have chips with ptp built in since ntp runs in software the kernel also adds a tiny bit of latency that ptp doesn't have to deal with but this is just a lot of explanation let's see exactly what's happening with this oscilloscope for this setup there are two raspberry pi's i call them time pi 1 here on the left and time pi 2 on the right both of them have pin 9 here as the signal that's this the black wire on j2 and pin 8 is the ground the yellow wire and those are going into channel 1 and channel 2 in the oscilloscope and right now what it's doing is it's measuring when a pulse per second is triggered so it's you can see it's off by a bit here the scope is on a 100 millisecond time scale and right now they're probably off by like half a second maybe you know 300 400 500 milliseconds uh from from the time that both signals rise and the goal with pps and ptp is to use this network connection the pi is connected straight through to this other pi and then that network connection goes through the pi's built-in mix that are made by broadcom and it'll send a signal between these two pies from the master to the slave clock i installed ptp for linux and set up the first pi as a master clock and the second pi is a slave and right now on the scope that time scale is still a hundred milliseconds so the pies are still half a second off but once i start ptp for linux the second slave pi jumps very close to the same time right away and when i zoom in you can see the accuracy is improving on the microsecond scale and if i zoom into 20 microseconds you can start seeing how the software learns over time the exact offset it needs for the network path it's using i'll speed things up a bit since it takes a couple minutes to get beyond microsecond accuracy and now i'm on a 100 nanosecond scale so we're already just a few hundred nanoseconds off and if i speed up again you can see it down to the 10 nanosecond scale so after just a couple minutes of calibration my two raspberry pi's clocks are within 20 nanoseconds of synchronization at some point you get diminishing returns so the software just kind of keeps a lock around 10 to 20 nanoseconds at least on my two pies but some hardware can get even more accurate with ptp if i put the two lines on top of each other here you can see that they even get within a couple nanoseconds of each other from time to time so ptp provides synchronization but there's no guarantee the time will be accurate we need to sync up to utc so we know what time it is outside of our local network for that most people just use gps but gps by itself isn't bulletproof and that's where the time card comes in this is an open source hardware design that incorporates gps and a high quality oscillator in this case a bit of slightly radioactive rubidium i spoke with ahmad by agawi who leads the time appliances project and does timing research at meta and he describes the time card design as a central part of getting more accurate time on every device everywhere my goal now is like i want to have every computer in the world precision time synchronized and this goes like from mobile phones like like uh like all the way to computers laptops everything and uh that's why we like okay let's put it as part of the time appliances project so then a lot of people will work on this contribute on this and that's why everything about the time card is open source i gave an overview of the time card in a video last year and linus over on ltt even did a whole video on it though their thumbnail is a little misleading well i mean it does work by exciting a slightly radioactive element but the key word here is slightly there is this laser that basically uh beams into this chamber inside the chamber you have uh the cavity and then you have let's see if i can open it so inside the chamber you have a little vial that has the rubidium gas this one is a little bit radioactive but as you can see i'm not dying so you know yeah the dose is very low probably only one banana right anyways the time card is a completely open hardware design and after my last video some people complained there was still a little bit of software that was closed source well that's being addressed the early version i tested needed some software from net time logic to get it working but ahmad said they're in the middle of rewriting it to be completely open source so there won't be any aspect of the card that requires closed source software at all i'll get back to the time card someday but i want to focus back on the compute module 4. with the gps hat the compute module 4 is an honest-to-goodness stratum 1 ntp and ptp grandmaster clock for less than 100 bucks well a bit more if you want to use an atomic clock for times when gps goes out it can't replace high-end network cards for every use case especially when you need lots of bandwidth or have thousands of slave devices but lassie johnson from timebeat was pretty upbeat on the prospects it rivals uh network cards uh which are a thousand dollars in terms of uh performance in terms of accuracy obviously you can't have the same sort of uh bandwidth throughput as you can on those high-end adapters but specifically for sync the result is is genuinely excellent and what if the time card in its atomic clock is overkill well time beat and the time appliances project are working on a new pi hat called the time for pi it uses pps and gps and will make an extremely affordable tiny time server this thing could replace multi thousand dollar grand master clocks and everything about it will be open source lassie from timebeat also worked on the linux kernel patches for ptp on the cm4 and here's what he had to say okay i have this view that um you know these purpose-built appliances that exist as a grandmaster clocks from the traditional vendors um you know i think in in 2022 they're about as relevant as the appliance web server from uh you know the rec cube was in 1998 right bringing costs down and making it easier means more people can have super accurate timing but heck what does any of this mean for you i use this material to pick up girls you know high accuracy time synchronization it's a crowd please all right well yeah it's not as impressive as some things but if we could guarantee nanosecond level accuracy in all our devices it could improve things like citizen science projects pc gaming and communication so this is in one aspect but let's say when you go for these competitive games i can actually control what you're seeing at the same time based on the latency put everyone basically at the worst case scenario and then run everything at the worst case and do the judgment based on when the clicks happen by the people because they're seeing things at the same time ptp isn't all perfect of course no standard is but that's why companies like timebeat are building functionality to monitor and self-heal timing traffic and that's why the time appliances project is completely open i mean seriously go check out their wiki if you ever wanted to nerd out about time check out some of the presentations like this one about optical timekeeping at nist so today nanoseconds matter for finance for massive databases for 5g and factory automation but once more of us have devices that can keep time accurately maybe we'll see even more improvements in things like game design and communications i know for me i plan on setting this thing up as a stratum 1 ptp time server in my home lab soon maybe i'll be the first home labor with a time server in my rack until next time i'm jeff gearling
Info
Channel: Jeff Geerling
Views: 170,296
Rating: undefined out of 5
Keywords: raspberry pi, ptp, time, card, clock, meta, ntp, quantum, physics, standard, wwv, wwvb, broadcast, aes67, ptpv2, pps, gps, global, positioning, system, satellite, radio, rubidium, linux, kernel, patch, lasse, johnsen, ahmad, byagowi, interview, meinberg, master, grandmaster, server, atomic, optical, lattice, tweezer, nist, oscilloscope, sync, synchronization, nic, ethernet, utc, universal, coordinated, cm4, compute module, date, precision, protocol, network, ieee1588-2008, BCM54210PE
Id: RvnG-ywF6_s
Channel Id: undefined
Length: 13min 55sec (835 seconds)
Published: Wed Jul 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.