硬件设计
GNSS模块输出pps到CPU,1PPS_1.8V用于V2X的pps,GPIO35_1.8V用于pps-gpio。这里做了pps一分二,由于距离太近不影响功能,焊接R35 0欧电阻。
驱动配置
gps_pps_pins:ublox_gps@2 {
leadcore,pins = "gpio34";
leadcore,function = "gpio";
leadcore,pull = <MUXPIN_PULL_DISABLE>;
};
gps_pps2_pins:pps_gpio@0 {
leadcore,pins = "gpio35";
leadcore,function = "gpio";
leadcore,pull = <MUXPIN_PULL_DISABLE>;
};
ublox_gps {
compatible = "morningcore,ublox-gps";
//gps_d1v8_ctrl = <&gpio 255 0>;
gps_rst = <&gpio 255 0>;
gps_pps = <&gpio 34 0>;
pinctrl-names = "d1v8_ctrl_pins", "rst_pin", "pps_pins";
pinctrl-0 = <&gps_d1v8_ctrl_pins>;
pinctrl-1 = <&gps_rst_pins>;
pinctrl-2 = <&gps_pps_pins>;
};
pps_gpio {
compatible = "morningcore,pps-gpio";
gps_pps2 = <&gpio 35 0>;
pinctrl-names = "pps2_pins";
pinctrl-0 = <&gps_pps2_pins>;
};
drivers/comip/wireless/ublox-gps.c
{ .compatible = "morningcore,ublox-gps", },
comip_mfp_config(MFP_PIN_GPIO(34), MFP_PIN_MODE_GPIO);
pdata->gpio_pps = of_get_named_gpio(pdev->dev.of_node, "gps_pps", 0);
if (pdata->gpio_pps < 0) {
dev_err(&pdev->dev, "get gps_pps failed\n");
return -EINVAL;
}
gpio_request(pdata->gpio_pps, "GPS pps GPIO");
gpio_direction_input(pdata->gpio_pps);
comip_mfp_config_pull(pdata->gpio_pps, MFP_PULL_DISABLE);
gpio_free(pdata->gpio_pps);
调试发现gpio34如果没有配置不响应v2x功能,这是为何?
drivers/pps/clients/pps-gpio.c
{ .compatible = "morningcore,pps-gpio", },
pdata->gpio_pin = of_get_named_gpio(pdev->dev.of_node, "gps_pps2", 0);
//printk("wzl-debug pdata->gpio_pin=%d\n", pdata->gpio_pin);
if (pdata->gpio_pin < 0) {
dev_err(&pdev->dev, "get gps_pps2 failed\n");
return -EINVAL;
}
comip_mfp_config1(pdata->gpio_pin, MFP_PIN_MODE_GPIO);
/* IRQ setup */
irq = gpio_to_irq(pdata->gpio_pin);
if (irq < 0) {
pr_err("failed to map GPIO to IRQ: %d\n", irq);
err = -EINVAL;
goto return_error;
}
ret = gpio_request(pdata->gpio_pin, "GPS pps2 GPIO");
if (ret) {
pr_warning("failed to request GPIO %u\n", pdata->gpio_pin);
return -EINVAL;
}
ret = gpio_direction_input(pdata->gpio_pin);
if (ret) {
pr_warning("failed to set pin direction\n");
gpio_free(pdata->gpio_pin);
return -EINVAL;
}
comip_mfp_config_pull(pdata->gpio_pin, MFP_PULL_DISABLE);
gpio_free(pdata->gpio_pin);
/* register IRQ interrupt handler */
ret = request_irq(irq, pps_gpio_irq_handler,
IRQF_TRIGGER_RISING, data->info.name, data);
if (ret) {
pps_unregister_source(data->pps);
pr_err("failed to acquire IRQ %d\n", irq);
err = -EINVAL;
goto return_error;
}
static irqreturn_t pps_gpio_irq_handler(int irq, void *data)
{
const struct pps_gpio_device_data *info;
struct pps_event_time ts;
int rising_edge;
disable_irq_nosync(irq);
/* Get the time stamp first */
pps_get_ts(&ts);
info = data;
rising_edge = gpio_get_value(info->pdata->gpio_pin);
//printk("wzl-debug:pps_gpio_irq_handler rising_edge = %d\n", rising_edge);
if ((rising_edge && !info->pdata->assert_falling_edge) ||
(!rising_edge && info->pdata->assert_falling_edge))
pps_event(info->pps, &ts, PPS_CAPTUREASSERT, NULL);
else if (info->pdata->capture_clear &&
((rising_edge && info->pdata->assert_falling_edge) ||
(!rising_edge && !info->pdata->assert_falling_edge)))
pps_event(info->pps, &ts, PPS_CAPTURECLEAR, NULL);
enable_irq(irq);
return IRQ_HANDLED;
}
打开驱动配置:
#
# PPS support
#
CONFIG_PPS=y
CONFIG_PPS_DEBUG=y
#
# PPS clients support
#
# CONFIG_PPS_CLIENT_KTIMER is not set
# CONFIG_PPS_CLIENT_LDISC is not set
CONFIG_PPS_CLIENT_GPIO=y
启动时内核打印和pps设备节点信息:
linux_genvict:~# dmesg | grep pps
[ 0.303184]{0} pps_core: LinuxPPS API ver. 1 registered
[ 0.303191]{0} pps_core: Software ver. 5.3.6 - Copyright 2005-2007 Rodolfo Giometti <giometti@linux.it>
[ 3.534376]{1} pps_core: source pps_gpio.7.-1 got cdev (252:0)
[ 3.534387]{1} pps pps0: new PPS source pps_gpio.7.-1
[ 3.540090]{1} pps pps0: Registered IRQ 195 as PPS source
linux_genvict:~# ls /dev/pps0 -l
crw-rw---- 1 root 0 252, 0 Jan 1 00:08 /dev/pps0
应用编译
使用Buildroot 2017.02.3源码,选择外部编译链,编译gpsd、pps和chrony工具:
Target packages --->
Hardware handling --->
[*] gpsd ----
(/dev/ttyS1) Where to look for GPSes (NEW)
[*] client debugging support (NEW)
[*] profiling support (NEW)
[*] NTP time hinting support (NEW)
[*] PPS time syncing support (NEW)
[*] allow gpsd to change device settings (NEW)
[*] allow gpsctl/gpsmon to change device settings (NEW)
[*] NMEA (NEW)
[*] pps-tools
Networking applications --->
[*] chrony
[wangyb@wangyb-VirtualBox:target]$ find ./ -name gps*
./usr/sbin/gpsd
./usr/sbin/gpsdctl
./usr/bin/gpspipe
./usr/bin/gpsdecode
./usr/bin/gpsctl
./usr/bin/gpsmon
./usr/bin/gps2udp
[wangyb@wangyb-VirtualBox:target]$ find ./ -name pps*
./usr/bin/ppstest
./usr/bin/ppsfind
./usr/bin/ppswatch
./usr/bin/ppsctl
[wangyb@wangyb-VirtualBox:target]$ find ./ -name chrony*
./var/lib/chrony
./usr/sbin/chronyd
./usr/bin/chronyc
[wangyb@wangyb-VirtualBox:target]$ readelf -d ./usr/sbin/gpsd
Dynamic section at offset 0x25820 contains 29 entries:
标记 类型 名称/值
0x00000001 (NEEDED) 共享库:[libusb-1.0.so.0]
0x00000001 (NEEDED) 共享库:[libm.so.6]
0x00000001 (NEEDED) 共享库:[librt.so.1]
0x00000001 (NEEDED) 共享库:[libdbus-1.so.3]
0x00000001 (NEEDED) 共享库:[libpthread.so.0]
0x00000001 (NEEDED) 共享库:[libc.so.6]
如果没有编译到gpsmon cgps,缺失ncurses库:
Target packages --->
Libraries --->
Text and terminal handling --->
-*- ncurses
[*] pcre
[ ] 16-bit pcre
[ ] 32-bit pcre
-*- UTF-8/16/32 support in pcre
[*] Unicode properties support in pcre
[*] readline
测试pps
R35电路没有焊接的情况下:
linux_genvict:/data/upgrade# ./ppstest /dev/pps0
trying PPS source "/dev/pps0"
found PPS source "/dev/pps0"
ok, found 1 source(s), now start fetching data...
time_pps_fetch() error -1 (Connection timed out)
time_pps_fetch() error -1 (Connection timed out)
time_pps_fetch() error -1 (Connection timed out)
time_pps_fetch() error -1 (Connection timed out)
短接R35电阻,pps接在gpio35:
linux_genvict:/data/upgrade# ./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 1686797890.917627372, sequence: 88 - clear 0.000000000, sequence: 0
source 0 - assert 1686797891.917628603, sequence: 89 - clear 0.000000000, sequence: 0
source 0 - assert 1686797892.917629680, sequence: 90 - clear 0.000000000, sequence: 0
source 0 - assert 1686797893.917603988, sequence: 91 - clear 0.000000000, sequence: 0
source 0 - assert 1686797894.917600834, sequence: 92 - clear 0.000000000, sequence: 0
source 0 - assert 1686797895.917604911, sequence: 93 - clear 0.000000000, sequence: 0
source 0 - assert 1686797896.917607372, sequence: 94 - clear 0.000000000, sequence: 0
gpsd不使用pps:./gpsd -n -G /dev/ttygnss1
gpsd使用pps:./gpsd -n -G /dev/ttygnss1 /dev/pps0
chrony校时
添加chrony配置文件:/system/etc/chrony.conf
makestep 0.1 3
rtcsync
allow
leapsectz right/UTC
driftfile /var/lib/chrony/drift
dumpdir /var/run/chrony
refclock PPS /dev/pps0 refid PPS lock GPSD prefer precision 1e-7 poll 2
refclock SHM 0 refid GPSD precision 1e-1 offset 0.9999 delay 0.2 poll 2
参考博客:嵌入式Linux 时间同步 gpsd+chrony_gpsd chrony_sep4075的博客-CSDN博客
先运行gpsd,然后运行./chronyd -f /system/etc/chrony.conf:
使用套接字方法让 chronyd 连接到 gpsd,添加配置:
refclock SOCK /var/run/chrony.ttygnss1.sock refid GPS precision 1e-1 offset 0.9999
refclock SOCK /var/run/chrony.pps0.sock refid PPS precision 1e-7
1、多久自动更新系统时间
GPS定位成功A状态后,大概40秒左右更新系统时间,应该是PPS输出成功后才更新:
===============================================================================
#? GPS 0 4 3 23 -20021d[-20021d] +/- 250ms
#? PPS 0 4 3 23 -20021d[-20021d] +/- 364us
Thu Jan 1 08:01:55 CST 1970
linux_genvict:~# chronyc sources;date
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
#? GPS 0 4 7 10 +42ms[-20021d] +/- 250ms
#* PPS 0 4 7 8 -3561us[-20021d] +/- 250us
Fri Oct 25 15:09:45 CST 2024
2、开机时设置系统时间后,chrony更新系统时间吗
开机时通过命令date手动设置系统时间后,chrony会在GPS定位成功A状态后,大概40秒左右更新系统时间。
3、chrony更新系统时间后,手动设置系统时间后会再自动更新吗
目前测试来看,系统时间更新后,手动设置时间权限更高,后面不会再更新时间。
4、怎么手动触发chrony更新系统时间
使用chronyc makestep命令可以手动触发自动更新系统时间,成功后返回200 OK,但有时没有真正成功需要等一会再设置一次。
问题1:gpsd 3.25版本支持设置波特率参数-s 460800,加参数后pps时间不能同步
使用gpsd 3.23和3.25版本,加参数-s 460800后chronyc pps不能同步:
linux_genvict:~# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
#? PPS 0 4 0 - +0ns[ +0ns] +/- 0ns
#? GPS 0 4 0 - +0ns[ +0ns] +/- 0ns
之前使用gpsd 3.16版本,不支持-s参数所以没有出现这个问题,发现把GPS同步注释,只使用PPS同步能成功:
#refclock SHM 0 refid GPS offset 0.0 delay 0.5 noselect
refclock PPS /dev/pps0 refid PPS lock GPS prefer precision 1e-9
这是为什么呢?各版本参数如下:
gpsd-3.25 Options include:
-?, -h, --help = help message
-b, --readonly = bluetooth-safe: open data sources read-only
-D, --debug integer = set debug level, default 0
-F, --sockfile sockfile = specify control socket location, default none
-f, --framing FRAMING = fix device framing to FRAMING (8N1, 8O1, etc.)
-G, --listenany = make gpsd listen on INADDR_ANY
-l, --drivers = list compiled in drivers, and exit.
-n, --nowait = don't wait for client connects to poll GPS
-N, --foreground = don't go into background
-P, --pidfile pidfile = set file to record process ID
-p, --passive = do not reconfigure the receiver automatically
-r, --badtime = use GPS time even if no fix
-S, --port PORT = set port for daemon, default 2947
-s, --speed SPEED = fix device speed to SPEED, default none
-V, --version = emit version and exit.
gpsd-3.16 Options include:
-b = bluetooth-safe: open data sources read-only
-n = don't wait for client connects to poll GPS
-N = don't go into background
-F sockfile = specify control socket location
-G = make gpsd listen on INADDR_ANY
-P pidfile = set file to record process ID
-D integer (default 0) = set debug level
-S integer (default 2947) = set port for daemon
-h = help message
-V = emit version and exit.
这样会引起另一个问题,不能自动设置和同步系统时间;正常情况下,chronyc开启后,gps定位成功大概1分钟左右会进行设置和同步系统时间,也会查询到pps时间同步的精度,这样系统时间不会自动设置和同步导致了问题。date查看是1970年,需要自己手动设置时间,那么这样就不准了。