Raspberry Pi + GPS ModuleでStratum1なNTPサーバをつくる


  • あとでかくぞ.


  • raspberry piのserial consoleをdisableすること.
  • serialがttyAMA0に割り当てられること
  • gpsmon/cgpsを利用してppsのoffsetを掌握し,/etc/chrony/chrony.confのoffsetを調整すること.
    • これをやらないと処理遅延によるoffsetがかかってしまって絶対時間が合わない.
    • いくらgpsのppsを利用しているとはいえ,offsetは避けられない.設定しない場合でもあるoffsetをもつjitter/err marginの少ないNTPとしては成立すると思う.
    • refclock SHM 1 lock NMEA refid PPS precision 1e-9 offset 0.100500


jp7fkf@lab1:~ $ uname -a
Linux rasp03r.live 4.19.97+ #1294 Thu Jan 30 13:10:54 GMT 2020 armv6l GNU/Linux
jp7fkf@lab1:~ $ sudo systemctl stop [email protected]
jp7fkf@lab1:~ $ sudo systemctl disable [email protected]
jp7fkf@lab1:~ $ sudo apt install gpsd gpsd-clients pps-tools
jp7fkf@lab1:~ $
jp7fkf@lab1:~ $
jp7fkf@lab1:~ $ sudo timedatectl status
               Local time: Fri 2020-02-28 19:14:50 JST
           Universal time: Fri 2020-02-28 10:14:50 UTC
                 RTC time: n/a
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
jp7fkf@lab1:~ $
jp7fkf@lab1:~ $ cat /boot/cmdline.txt
console=tty1 root=PARTUUID=738a4d67-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
jp7fkf@lab1:~ $ cat /boot/config.txt
# For more options and information see
# http://rpf.io/configtxt
# Some settings may impact device functionality. See link above for details

# uncomment if you get no picture on HDMI for a default "safe" mode

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border

# uncomment to force a console size. By default it will be display's size minus
# overscan.

# uncomment if hdmi display is not detected and composite is being output

# uncomment to force a specific HDMI mode (this will force VGA)

# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes

# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display

# uncomment for composite PAL

#uncomment to overclock the arm. 700 MHz is the default.

# Uncomment some or all of these to enable the optional hardware interfaces

# Uncomment this to enable infrared communication.

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)

# Enable DRM VC4 V3D driver on top of the dispmanx display stack


core_freq=250                        #added!
dtoverlay=pps-gpio,gpiopin=18        #added!
jp7fkf@lab1:~ $ cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
jp7fkf@lab1:~ $ cat
# Default settings for the gpsd init script and the hotplug wrapper.

# Start the gpsd daemon automatically at boot time

# Use USB hotplugging to add new USB devices automatically to the daemon

# Devices gpsd should collect to at boot time.
# They need to be read/writeable, either by user gpsd or the group dialout.
DEVICES="/dev/ttyAMA0 /dev/pps0"

# Other options you want to pass to gpsd
jp7fkf@lab1:~ $ cgps -s
jp7fkf@lab1:~ $ gpsmon
jp7fkf@lab1:~ $ sudo systemctl disable systemd-timesyncd
Removed /etc/systemd/system/sysinit.target.wants/systemd-timesyncd.service.
Removed /etc/systemd/system/dbus-org.freedesktop.timesync1.service.
jp7fkf@lab1:~ $ sudo systemctl stop systemd-timesyncd
jp7fkf@lab1:~ $
jp7fkf@lab1:~ $ sudo apt install chrony
# Welcome to the chrony configuration file. See chrony.conf(5) for more
# information about usuable directives.
# pool 2.debian.pool.ntp.org iburst
pool ntp.nict.jp maxsources 4 noselect
pool ntp.jst.mfeed.ad.jp maxsources 4 noselect

refclock SHM 0 refid NMEA precision 1e-1 noselect
refclock SHM 1 lock NMEA refid PPS precision 1e-9 offset 0.100500

# This directive specify the location of the file containing ID/key pairs for
# NTP authentication.
keyfile /etc/chrony/chrony.keys

# This directive specify the file into which chronyd will store the rate
# information.
driftfile /var/lib/chrony/chrony.drift

# Uncomment the following line to turn logging on.
#log tracking measurements statistics

# Log files location.
logdir /var/log/chrony

# Stop bad estimates upsetting machine clock.
maxupdateskew 100.0

# This directive enables kernel synchronisation (every 11 minutes) of the
# real-time clock. Note that it can’t be used along with the 'rtcfile' directive.

# Step the system clock instead of slewing it if the adjustment is larger than
# one second, but only in the first three clock updates.
makestep 1 3

# Allow NTP client access from local network.
jp7fkf@lab1:~ $ chronyc sources -v
210 Number of sources = 2

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample
#? NMEA                          0   4   377    23   +301ms[ +301ms] +/-  100ms
#* PPS                           0   4   377    21  +1232ns[+1929ns] +/- 1000ns


  • ppstest /dev/pps0