0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 ======================
0004 PPS - Pulse Per Second
0005 ======================
0006
0007 Copyright (C) 2007 Rodolfo Giometti <giometti@enneenne.com>
0008
0009 This program is free software; you can redistribute it and/or modify
0010 it under the terms of the GNU General Public License as published by
0011 the Free Software Foundation; either version 2 of the License, or
0012 (at your option) any later version.
0013
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0017 GNU General Public License for more details.
0018
0019
0020
0021 Overview
0022 --------
0023
0024 LinuxPPS provides a programming interface (API) to define in the
0025 system several PPS sources.
0026
0027 PPS means "pulse per second" and a PPS source is just a device which
0028 provides a high precision signal each second so that an application
0029 can use it to adjust system clock time.
0030
0031 A PPS source can be connected to a serial port (usually to the Data
0032 Carrier Detect pin) or to a parallel port (ACK-pin) or to a special
0033 CPU's GPIOs (this is the common case in embedded systems) but in each
0034 case when a new pulse arrives the system must apply to it a timestamp
0035 and record it for userland.
0036
0037 Common use is the combination of the NTPD as userland program, with a
0038 GPS receiver as PPS source, to obtain a wallclock-time with
0039 sub-millisecond synchronisation to UTC.
0040
0041
0042 RFC considerations
0043 ------------------
0044
0045 While implementing a PPS API as RFC 2783 defines and using an embedded
0046 CPU GPIO-Pin as physical link to the signal, I encountered a deeper
0047 problem:
0048
0049 At startup it needs a file descriptor as argument for the function
0050 time_pps_create().
0051
0052 This implies that the source has a /dev/... entry. This assumption is
0053 OK for the serial and parallel port, where you can do something
0054 useful besides(!) the gathering of timestamps as it is the central
0055 task for a PPS API. But this assumption does not work for a single
0056 purpose GPIO line. In this case even basic file-related functionality
0057 (like read() and write()) makes no sense at all and should not be a
0058 precondition for the use of a PPS API.
0059
0060 The problem can be simply solved if you consider that a PPS source is
0061 not always connected with a GPS data source.
0062
0063 So your programs should check if the GPS data source (the serial port
0064 for instance) is a PPS source too, and if not they should provide the
0065 possibility to open another device as PPS source.
0066
0067 In LinuxPPS the PPS sources are simply char devices usually mapped
0068 into files /dev/pps0, /dev/pps1, etc.
0069
0070
0071 PPS with USB to serial devices
0072 ------------------------------
0073
0074 It is possible to grab the PPS from an USB to serial device. However,
0075 you should take into account the latencies and jitter introduced by
0076 the USB stack. Users have reported clock instability around +-1ms when
0077 synchronized with PPS through USB. With USB 2.0, jitter may decrease
0078 down to the order of 125 microseconds.
0079
0080 This may be suitable for time server synchronization with NTP because
0081 of its undersampling and algorithms.
0082
0083 If your device doesn't report PPS, you can check that the feature is
0084 supported by its driver. Most of the time, you only need to add a call
0085 to usb_serial_handle_dcd_change after checking the DCD status (see
0086 ch341 and pl2303 examples).
0087
0088
0089 Coding example
0090 --------------
0091
0092 To register a PPS source into the kernel you should define a struct
0093 pps_source_info as follows::
0094
0095 static struct pps_source_info pps_ktimer_info = {
0096 .name = "ktimer",
0097 .path = "",
0098 .mode = PPS_CAPTUREASSERT | PPS_OFFSETASSERT |
0099 PPS_ECHOASSERT |
0100 PPS_CANWAIT | PPS_TSFMT_TSPEC,
0101 .echo = pps_ktimer_echo,
0102 .owner = THIS_MODULE,
0103 };
0104
0105 and then calling the function pps_register_source() in your
0106 initialization routine as follows::
0107
0108 source = pps_register_source(&pps_ktimer_info,
0109 PPS_CAPTUREASSERT | PPS_OFFSETASSERT);
0110
0111 The pps_register_source() prototype is::
0112
0113 int pps_register_source(struct pps_source_info *info, int default_params)
0114
0115 where "info" is a pointer to a structure that describes a particular
0116 PPS source, "default_params" tells the system what the initial default
0117 parameters for the device should be (it is obvious that these parameters
0118 must be a subset of ones defined in the struct
0119 pps_source_info which describe the capabilities of the driver).
0120
0121 Once you have registered a new PPS source into the system you can
0122 signal an assert event (for example in the interrupt handler routine)
0123 just using::
0124
0125 pps_event(source, &ts, PPS_CAPTUREASSERT, ptr)
0126
0127 where "ts" is the event's timestamp.
0128
0129 The same function may also run the defined echo function
0130 (pps_ktimer_echo(), passing to it the "ptr" pointer) if the user
0131 asked for that... etc..
0132
0133 Please see the file drivers/pps/clients/pps-ktimer.c for example code.
0134
0135
0136 SYSFS support
0137 -------------
0138
0139 If the SYSFS filesystem is enabled in the kernel it provides a new class::
0140
0141 $ ls /sys/class/pps/
0142 pps0/ pps1/ pps2/
0143
0144 Every directory is the ID of a PPS sources defined in the system and
0145 inside you find several files::
0146
0147 $ ls -F /sys/class/pps/pps0/
0148 assert dev mode path subsystem@
0149 clear echo name power/ uevent
0150
0151
0152 Inside each "assert" and "clear" file you can find the timestamp and a
0153 sequence number::
0154
0155 $ cat /sys/class/pps/pps0/assert
0156 1170026870.983207967#8
0157
0158 Where before the "#" is the timestamp in seconds; after it is the
0159 sequence number. Other files are:
0160
0161 * echo: reports if the PPS source has an echo function or not;
0162
0163 * mode: reports available PPS functioning modes;
0164
0165 * name: reports the PPS source's name;
0166
0167 * path: reports the PPS source's device path, that is the device the
0168 PPS source is connected to (if it exists).
0169
0170
0171 Testing the PPS support
0172 -----------------------
0173
0174 In order to test the PPS support even without specific hardware you can use
0175 the pps-ktimer driver (see the client subsection in the PPS configuration menu)
0176 and the userland tools available in your distribution's pps-tools package,
0177 http://linuxpps.org , or https://github.com/redlab-i/pps-tools.
0178
0179 Once you have enabled the compilation of pps-ktimer just modprobe it (if
0180 not statically compiled)::
0181
0182 # modprobe pps-ktimer
0183
0184 and the run ppstest as follow::
0185
0186 $ ./ppstest /dev/pps1
0187 trying PPS source "/dev/pps1"
0188 found PPS source "/dev/pps1"
0189 ok, found 1 source(s), now start fetching data...
0190 source 0 - assert 1186592699.388832443, sequence: 364 - clear 0.000000000, sequence: 0
0191 source 0 - assert 1186592700.388931295, sequence: 365 - clear 0.000000000, sequence: 0
0192 source 0 - assert 1186592701.389032765, sequence: 366 - clear 0.000000000, sequence: 0
0193
0194 Please note that to compile userland programs, you need the file timepps.h.
0195 This is available in the pps-tools repository mentioned above.
0196
0197
0198 Generators
0199 ----------
0200
0201 Sometimes one needs to be able not only to catch PPS signals but to produce
0202 them also. For example, running a distributed simulation, which requires
0203 computers' clock to be synchronized very tightly. One way to do this is to
0204 invent some complicated hardware solutions but it may be neither necessary
0205 nor affordable. The cheap way is to load a PPS generator on one of the
0206 computers (master) and PPS clients on others (slaves), and use very simple
0207 cables to deliver signals using parallel ports, for example.
0208
0209 Parallel port cable pinout::
0210
0211 pin name master slave
0212 1 STROBE *------ *
0213 2 D0 * | *
0214 3 D1 * | *
0215 4 D2 * | *
0216 5 D3 * | *
0217 6 D4 * | *
0218 7 D5 * | *
0219 8 D6 * | *
0220 9 D7 * | *
0221 10 ACK * ------*
0222 11 BUSY * *
0223 12 PE * *
0224 13 SEL * *
0225 14 AUTOFD * *
0226 15 ERROR * *
0227 16 INIT * *
0228 17 SELIN * *
0229 18-25 GND *-----------*
0230
0231 Please note that parallel port interrupt occurs only on high->low transition,
0232 so it is used for PPS assert edge. PPS clear edge can be determined only
0233 using polling in the interrupt handler which actually can be done way more
0234 precisely because interrupt handling delays can be quite big and random. So
0235 current parport PPS generator implementation (pps_gen_parport module) is
0236 geared towards using the clear edge for time synchronization.
0237
0238 Clear edge polling is done with disabled interrupts so it's better to select
0239 delay between assert and clear edge as small as possible to reduce system
0240 latencies. But if it is too small slave won't be able to capture clear edge
0241 transition. The default of 30us should be good enough in most situations.
0242 The delay can be selected using 'delay' pps_gen_parport module parameter.