硬件设计
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]
测试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