时间同步方案
名词缩写及解释
缩写 | 解释 |
---|---|
GPS | Global Positioning System |
RTC | Real_Time Clock |
PHC | PTP hardware clock |
PTP | precision time protocol |
gPTP | precision time protocol, extension of PTP Protocol |
MCU | Microcontroller Unit |
UART | Universal Asynchronous Receiver/Transmitter |
CAN | Controller Area Network |
PPS | Pulse Per Second |
NMEA | National Marine Electronics Association |
NTP | Network Time Protocol |
NIC | Network Interface Card |
ptp时间同步
功能
该软件包含两个程序:ptp4l和phc2sys。这两个软件结合使用,就可以实现从master获取时间,同步S100的PHC时间和Linux系统时间。
ptp4l使用方法
命令行参数
ptp4l支持gptp功能,可以作为master,也可以作为slave;如果作为slave,可以从master获取时间,同步S100的PHC时间和RTC时间。
可以通过ptp4l -h查看帮助信息:
root@ubuntu:/userdata# ptp4l -h
usage: ptp4l [options]
Delay Mechanism
-A Auto, starting with E2E
-E E2E, delay request-response (default)
-P P2P, peer delay mechanism
Network Transport
-2 IEEE 802.3
-4 UDP IPV4 (default)
-6 UDP IPV6
Time Stamping
-H HARDWARE (default)
-S SOFTWARE
-L LEGACY HW
Other Options
-f [file] read configuration from 'file'
-i [dev] interface device to use, for example 'eth0'
(may be specified multiple times)
-p [dev] Clock device to use, default auto
(ignored for SOFTWARE/LEGACY HW time stamping)
-s slave only mode (overrides configuration file)
-l [num] set the logging level to 'num'
-m print messages to stdout
-q do not print messages to the syslog
-v prints the software version and exits
-h prints this message and exits
配置文件的使用
ptp4l可以通过-f参数指定配置文件。
配置文件按段划分,空行和#开头的行会被忽略。
有三种段类型:
[global]段,用来配置program选项,clock选项,默认port选项。
port段使用被配置的网口的名字,如[eth0]段,其配置的选项会覆盖[global]段中默认port选项。port段可以为空内容,作用只是指定网口,这样命令行中不必使用-i选项。
[unicast_master_table]段,配置单播table。
配置文件的详细信息可以参考:https://linuxptp.nwtime.org/documentation/ptp4l/
automotive配置示例
automotive-master.cfg:
#
# Automotive Profile example configuration for master containing those
# attributes which differ from the defaults. See the file, default.cfg, for
# the complete list of available options.
#
[global]
# Options carried over from gPTP.
gmCapable 1
priority1 248
priority2 248
logSyncInterval -3
syncReceiptTimeout 3
neighborPropDelayThresh 800
min_neighbor_prop_delay -20000000
assume_two_step 1
path_trace_enabled 1
follow_up_info 1
transportSpecific 0x1
ptp_dst_mac 01:80:C2:00:00:0E
network_transport L2
delay_mechanism P2P
#
# Automotive Profile specific options
#
BMCA noop
masterOnly 1
inhibit_announce 1
asCapable true
inhibit_delay_req 1
automotive-slave.cfg:
#
# Automotive Profile example configuration for slaves containing those
# attributes which differ from the defaults. See the file, default.cfg, for
# the complete list of available options.
#
[global]
#
# Options carried over from gPTP.
#
gmCapable 1
priority1 248
priority2 248
logSyncInterval -3
syncReceiptTimeout 3
neighborPropDelayThresh 800
min_neighbor_prop_delay -20000000
assume_two_step 1
path_trace_enabled 1
follow_up_info 1
transportSpecific 0x1
ptp_dst_mac 01:80:C2:00:00:0E
network_transport L2
delay_mechanism P2P
#
# Automotive Profile specific options
#
BMCA noop
slaveOnly 1
inhibit_announce 1
asCapable true
ignore_source_id 1
# Required to quickly correct Time Jumps in master
step_threshold 1
operLogSyncInterval 0
operLogPdelayReqInterval 2
msg_interval_request 1
servo_offset_threshold 30
servo_num_offset_values 10
phc2sys使用方法
phc2sys用于将linux系统时间同步到phc时间,或者将phc时间同步到linux系统时间。
可以通过phc2sys --h查看使用说明:
root@ubuntu:/userdata# phc2sys -h
usage: phc2sys [options]
automatic configuration:
-a turn on autoconfiguration
-r synchronize system (realtime) clock
repeat -r to consider it also as a time source
manual configuration:
-c [dev|name] slave clock (CLOCK_REALTIME)
-d [dev] master PPS device
-s [dev|name] master clock
-O [offset] slave-master time offset (0)
-w wait for ptp4l
common options:
-f [file] configuration file
-E [pi|linreg] clock servo (pi)
-P [kp] proportional constant (0.7)
-I [ki] integration constant (0.3)
-S [step] step threshold (disabled)
-F [step] step threshold only on start (0.00002)
-R [rate] slave clock update rate in HZ (1.0)
-N [num] number of master clock readings per update (5)
-L [limit] sanity frequency limit in ppb (200000000)
-M [num] NTP SHM segment number (0)
-u [num] number of clock updates in summary stats (0)
-n [num] domain number (0)
-x apply leap seconds by servo instead of kernel
-z [path] server address for UDS (/var/run/ptp4l)
-l [num] set the logging level to 'num' (6)
-t [tag] add tag to log messages
-m print messages to stdout
-q do not print messages to the syslog
-v prints the software version and exits
-h prints this message and exits
综合示例
命令行参数
下面示范如何通过ptp4l和phc2sys,将master的网卡时间同步到slave的网卡时间和系统时间。用户需要保证master设备和slave设备之间网络联通。
Master端执行如下命令:
ptp4l -i eth0 -f /usr/hobot/lib/pkgconfig/automotive-master.cfg -m -l 7
Slave端执行如下命令:
ptp4l -i eth0 -f /usr/hobot/lib/pkgconfig/automotive-slave.cfg -m -l 7 > ptp4l.log &
phc2sys -s eth0 -c CLOCK_REALTIME --transportSpecific=1 -m --step_threshold=1000 -w > phc2sys.log &
Log示例
Slave端log:
ptp4l[8330.884]: PI servo: sync interval 1.000 kp 0.700 ki 0.300000
ptp4l[8330.885]: master offset 21 s3 freq -391 path delay 690
ptp4l[8330.998]: port 1: delay timeout
ptp4l[8330.999]: delay filtered 689 raw 687
ptp4l[8331.884]: master offset 35 s3 freq -371 path delay 689
ptp4l[8332.885]: master offset 47 s3 freq -349 path delay 689
ptp4l[8333.885]: master offset 50 s3 freq -332 path delay 689
ptp4l[8334.885]: master offset 22 s3 freq -345 path delay 689
Master端log:
ptp4l[3469.136]: config item /var/run/ptp4l.inhibit_delay_req is 1
ptp4l[3469.136]: config item (null).uds_address is '/var/run/ptp4l'
ptp4l[3469.136]: port 0: INITIALIZING to LISTENING on INIT_COMPLETE
ptp4l[3469.136]: config item (null).slaveOnly is 0
ptp4l[3469.136]: port 1: received link status notification
ptp4l[3469.136]: interface index 2 is up
ptp4l[3469.261]: port 1: master sync timeout
ptp4l[3469.386]: port 1: master sync timeout
ptp4l[3469.511]: port 1: master sync timeout
ptp4l[3469.636]: port 1: master sync timeout
ptp4l[3469.761]: port 1: master sync timeout
ptp4l[3469.886]: port 1: master sync timeout
全局时间源配置
由于当前系统中存在多个timeline,比如systime、phc、rtc等,而各个模块支持选择不同的timeline来打印各自的时间戳。为了统一各个模块的日志中时间戳的timeline,添加了一个hb_systime的模块,来统一系统各个模块的timeline的选择。该模块实 现的具体功能如下:
解析dts里默认的timeline的配置,如下:
globaltime: globaltime {
compatible = "hobot,globaltime";
globaltime = <2>; /* 0-systime, 1-phc, 2-rtc */
phc-index = <0>; /* phc index */
status = "okay";
};
其中dts中 globaltime 属性表示系统默认用的全局timeline,S100默认使用RTC时间。对应关系如下:
0:systime
1:phc
2:rtc
如果timeline选中网卡phc时间, 则通过phc-index属 性进一步判断当前使用哪个网卡phc的时间。对应关系如下:
0:phc0
1:phc1
向应用层提供/sys接口用来设置或者获取当前系统全局的timeline的选择,命令如下:
查看当前系统全局的timeline的配置:
cat /sys/devices/platform/soc/soc:globaltime/globaltime
设置系统全局timeline的选项:
echo 0 >/sys/devices/platform/soc/soc:globaltime/globaltime
或
echo 1 >/sys/devices/platform/soc/soc:globaltime/globaltime
或
echo 2 >/sys/devices/platform/soc/soc:globaltime/globaltime
向应用层提供/sys接口用来设置或者获取当前选中的phc编号
查看当前选中的phc编号:
cat /sys/devices/platform/soc/soc:globaltime/phcindex
设置phc编号:
echo 0 >/sys/devices/platform/soc/soc:globaltime/phcindex
或
echo 1 >/sys/devices/platform/soc/soc:globaltime/phcindex
📍 注意: phcindex设置不要超过网卡实际数量。
向内核其他模块提供接口,查看当前系统用的哪个timeline,以及使用的是哪个phc,如下:
int32_t hobot_get_global_time_type(uint32_t *global_time_type);
int32_t hobot_get_phc_index(uint32_t *phc_index);
MCU时间同步说明
MCU支持的时间类型
PHC时间:这个是网卡内部的一个计时器;
RTC时间:这个是S100 MCU侧带的一个实时时钟,不支持通过纽扣电池供电;
MCU支持的时间同步方式
MCU支持下面的时间同步方式:
基于PPS的Timesync:在秒脉冲上升沿捕捉到两个时间的snapshot,根据snapshot计算误差,根据误差进行时间同步 ;