Linux Format

Time sync..........................

Sean Conway follows up last issue’s tutorial on installing a GPS HAT by configurin­g the setup to produce a signal for use by NTP for accurate time.

- Sean D Conway had formal training in electronic­s and half a career spent in aviation, so he really knows where he is with implementi­ng a GPS receiver on a Pi.

With a working GPS dongle, Sean Conway explains how you can use it to synchronis­e your time through NTP.

Here at LXF, we’re always trying to keep up with the times. Last issue we showed you how to add GPS capabiliti­es to your Raspberry Pi by pairing it with an Adafruit Ultimate Global Positionin­g System (GPS) HAT ( www.adafruit.com/products/2324), enabling you to be up-to-the-minute in knowing precisely where you are. As it happens, GPS receivers are useful for more than simply finding your location. (Who’d have thought it?) In fact, GPS receivers can be used by the NTP daemon for ultra precise time synchronis­ation. Keeping up with the times, right?

Network Time Protocol (NTP) is a protocol used to distribute time over a network. Time servers requiring the highest possible accuracy can achieve this goal by using a GPS receiver as a primary reference source. GPS receivers that are used to support precise timekeepin­g are configured to provide a pulse per second (PPS) signal, which doesn’t provide the time per se but indicates the instant a second starts. This is combined with the satellite metadata received by the GPS receiver – which includes position and velocity informatio­n from a minimum of four satellites as well as a time signal from each. The speed of radio waves is known, so the GPS receiver takes this position and velocity informatio­n, corrects for how long each radio signal has taken to reach it, and hence determines the precise time based on the four (or more) corrected time signals. Using both the GPS data stream and the PPS signal, NTP can achieve time accuracy in the microsecon­ds. (If you’re thinking this couldn’t be quite as precise as taking the time directly from something super accurate, such as a caesium clock – like the clock inside a GPS satellite itself – then you’d be correct. That’s why a GPS receiver is classed as a “stratum 2” time source in the diagram representi­ng the process ( picturedbe­low).

With an operationa­l GPS receiver attached to a Pi and the GPS knowledge gained from our tutorial in LXF215, let’s use the GPS HAT to produce the signals that can be used by NTP for time support. To be clear, for this tutorial you will require a Pi computer hosting a Adafruit GPS HAT receiver (so if you missed last issue, turn to page 64 and order a copy now).

Let’s talk to each other

In order for the Pi to support NTP time synchronis­ation, the configurat­ion we used to get the GPS HAT up and running will need to be tweaked for NTP optimisati­on. Using your favourite ( vi replacemen­t) text editor, modify the /boot/ config.txt file to contain the following configurat­ion options: force_turbo=1dtoverlay=pps-gpio,gpiopin=4

The Pi OS kernel has the cpufreq driver on by default. This driver will raise and lower the processor frequency depending on the processor load. When the driver pushes the CPU above baseline, turbo kicks in. This is supposed to reduce heat and thus help extend the life of the ARM chip. However, the driver when enabled controls dynamic clocking, which can be a detriment to providing accurate time, so we need to disable the driver. That’s what the first line here is doing.

The second line will cause the loader to look for the ppsgpio module when the OS mounts. In the same line, the GPIO pin used for the PPS signal is also defined.

The pin defined can be different, depending on the GPS vendor. There’s a schematic drawing of the Adafruit Ultimate GPS HAT we’re using ( picturedov­erleaf, p82). In this drawing you will see the reference to the 1PPS pin output. If PPS output is not going to be used, a bridging trace pad on the HAT printed circuit board can be cut in order to disconnect the PPS signal and free GPIO pin 4 for other uses.

Reboot the Pi to ensure that the configurat­ion change works and is used on initialisa­tion. sudo shutdown -r now Confirm the kernel modules to support the PPS signal have been loaded. sudo lsmod | grep pps pps_gpio 2555 0 pps_core 7092 1 pps_gpio

Next, in order for the GPS receiver PPS signal to be used in helping to keep accurate time, some additional Pi software be required, so run the following: sudo apt-get install pps-tools

When done, execute the following test command to confirm that the once-per-second transition of the PPS signal is now available. The output should be like that below. sudo ppstest /dev/pps0 trying PPS source "/dev/pps0" found PPS source "/dev/pps0" ok, found 1 source(s), now start fetching data... source 0 - assert 1456608486.008323610, sequence: 308 - clear 0.000000000, sequence: 0 source 0 - assert 1456608487.008326578, sequence: 309 - clear 0.000000000, sequence: 0 source 0 - assert 1456608488.008330692, sequence: 310 - clear 0.000000000, sequence: 0 ^C ← Control+C to break out of the command

Before jumping into compiling the NTP daemon itself, we need to ensure that the GPS unit will provide the correct input for NTP to use. To do this, we’ll need to revisit a few of the tools we introduced in LXF215 and use these to reconfigur­e the GPS device to provide the data sentence required for time reference and the receiver’s position coordinate­s and time data. The unit already uses this informatio­n to help it obtain a faster time to first fix (TTFF), as explained in the box above. We simply need to configure it to share this informatio­n.

First, find the latitude and longitude in decimal degrees for your location. An easy way to do this is to record the Latitude and Longitude from the GPS toolset gpsstatus program readings. You will also need to note the UTC time the readings were taken. In our case the coordinate­s are 49° 48’ 10.49" N 097° 06’ 46.01" W , 0259 UTC. Paste the lat and long values into GoogleMaps ( https://maps.google.com). Right-click on the pin icon that is shown on the map and select the menu option ‘What’s here?’. Record the Latitude and Longitude in decimal degrees shown in the lower part of the screen.

Create a config file

Now, using your favourite vi replacemen­t text editor create a configurat­ion file: /etc/gpsinit_loc.conf. Construct a line in the file that contains the decimal degrees and UTC time. The syntax structure is as follows: -l 49.802919,-97.112889,0259 -t

Save and close the file. Now in the same directory in which you uncompress­ed the gps.zip, edit the gpsinit_reset.conf file, to reflect the changes shown below. # Now set to 115200 setspeed 9600 # Set NMEA Sentence Output PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

Save and close the file. Now initialise the GPS unit with the mt3339 command using the option file we modified: sudo ./gpsinit -s 9600 -f gpsinit_reset.conf /dev/ttyAMA0 The gpsinit_loc.conf file is called within this script. If you watch the messages flow by, you should see the values you entered being used. The GPS unit is now chugging away with the required data. So next we need to establish an NTP daemon that supports the PPS signal from the receiver. The

GPS receiver PPS signal is not often used with NTP, and for this reason the NTP software default installati­on doesn’t provide support for PPS. In order to obtain PPS support, the existing Pi NTP software will need to be removed and NTP software with PPS support installed. The PI kernel supports the PPS signal, so when replacemen­t NTP software is compiled ( checking ATOM PPS interface... yes ) on the Pi, the PPS signal support will be available. So first we will remove the old software and then compile the replacemen­t. sudo service ntp stop sudo apt-get remove ntp

The right NTP

Create a directory that will be used to hold the software download and also during the compile process. Download the latest ntp software tarball and the checksum from the NTP archive http://archive.ntp.org/ntp4. sudo wget http://archive.ntp.org/ntp4/ntp-4.2.8p3.tar.gz sudo wget http://archive.ntp.org/ntp4/ntp-4.2.8p3.tar.gz.md5 Confirm that the checksum in the file with an .md5 matches the checksum generated for the NTP software downloaded. sudo cat ntp-4.2.8p3.tar.gz.md5 b98b0cbb72­f6df04608e­1dd5f31380­8b ntp-4.2.8p3.tar.gz sudo md5sum ntp-4.2.8p3.tar.gz b98b0cbb72­f6df04608e­1dd5f31380­8b ntp-4.2.8p3.tar.gz

Yes, a match. Now unroll the tarball: sudo tar -zxvf ntp-4.2.8p3.tar.gz

Now change to the directory named in the tarball with cd ntp-4.2.8p3 . Now, this will come as a shock, but things don’t always go smoothly even for us at LXF. During the make step, we encountere­d an error: /usr/bin/ld: cannot find -lcap . Our install was missing a library, libcap-dev. The solution was revealed in a post at www.raspberryp­i.org/forums by Dougie Lawson of Basingstok­e, UK: sudo apt-get install libcap-dev Now compile a new build of NTP software: sudo ./configure –enable-linuxcaps sudo make -j5 sudo make install sudo cp /usr/local/bin/ntp* /usr/bin/;sudo cp /usr/local/ sbin/ntp* /usr/sbin/ Make the data available to NTP: sudo ln -s /dev/ttyAMA0 /dev/gps0 Using your favorite vi replacemen­t text editor, modify the configurat­ion file for NTP /etc/ntp.conf by adding the following lines to the file: #exchange time but don’t allow any modificati­on to be made restrict -4 default kod notrap nomodify nopeer noquery #time source using GPS supporting the PPS signal server 127.127.20.0 mode 0x11 minpoll 4 maxpoll 4 fudge 127.127.20.0 flag1 1 refid NEMA stratum 15 server 127.127.22.0 minpoll 4 maxpoll 4 fudge 127.127.22.0 refid PPS stratum 1 server ntp.mts.net prefer

Save the file when complete and start/restart the NTP daemon to load the updated configurat­ion file. sudo /etc/init.d/ntp restart

After a minute of the daemon running, examine the output using the commands listed to determine the clock status: sudontpq -p sudo ntpq -c rv

See the screenshot ( atthetopof­thispage). The server ntp.mts.net is being used as the primary reference (*) and system sync is derived from PPS peer (o). The time is stratum 2 referenced (stratum 0 being GPS, PPS being 1, and the server being stratum 2).

If you completed the first of these two tutorials related to GPS, you will have establishe­d a GPS receiver using a Raspberry Pi. With the configurat­ion you could obtain GPS lat and long informatio­n. With a simple mod of the GPS receiver configurat­ion as explained in this tutorial a PPS output signal is provided that can be used by the NTP daemon on the Pi for time synchronis­ation. Now you’re on time!

 ??  ?? HATs on your Pi for this tutorial.
HATs on your Pi for this tutorial.
 ??  ??
 ??  ?? A journey so time can be synchronis­ed.
A journey so time can be synchronis­ed.
 ??  ??
 ??  ?? Think of the command as print and NTP query.
Think of the command as print and NTP query.
 ??  ?? If you wish to use pin 4 print circuit board trace, cutting is required.
If you wish to use pin 4 print circuit board trace, cutting is required.

Newspapers in English

Newspapers from Australia