如何读取 PTP 硬件时钟 PHC 与 GPS 误差?

解读

国内主流基站、电网、自动驾驶域控制器都要求 时间同步精度 ≤ 1 μs,因此面试官想知道候选人是否真正“摸过”PTP 硬件时钟(PHC),而不是只会 ntpq -p。
核心考点:

  1. 能否在 Linux C 用户态 拿到 PHC 时间;
  2. 能否把 GPS 1 PPS 转化为可比较的时间戳;
  3. 能否计算 本地 PHC – GPS 误差 并处理纳秒回绕;
  4. 是否了解国内常见 SOC(如海思 Hi3559、瑞芯微 RK3588)的 pps-gpio 驱动限制。

知识点

  1. PHC 字符设备:/dev/ptpN,通过 ioctl 取时间,内核配置 CONFIG_PTP_1588_CLOCK。
  2. 关键 ioctl:
    PTP_CLOCK_GETTIME 返回 struct timespec64,精度 1 ns。
  3. GPS 1 PPS 输入:
    国内项目普遍把 GPS 模块的 PPS 接 SOC 的 GPIO,内核用 pps-gpio 驱动生成 /dev/ppsN。
  4. PPS 内核 API:
    ioctl(fd_pps, PPS_FETCH, &pps_info) 拿到 内核时间戳,该时间戳已转换到 CLOCK_REALTIME 域。
  5. 误差计算:
    在同一 1 PPS 周期内,
    Δt = PHC(ns) – GPS(ns),需处理 uint64 回绕。
  6. 国内踩坑点:
    • 海思 SDK 默认 关闭 ptp 设备节点,需 echo 1 > /sys/class/ptp/ptp0/enable;
    • 瑞芯微 GPIO 触发延迟 ±300 ns,必须做 DMA 时间戳校准;
    • 基站项目要求 锁相后 |Δt| < 100 ns,面试时要提到 低通滤波 + 环形缓冲 抑制抖动。

答案

下面给出一份可直接在 RK3588 板卡上跑通的 C 语言最小示例,演示“读取 PHC 与 GPS 误差”的完整流程,代码 不超过 120 行,符合国内面试官“能当场手写”的期望。

/* phc_gps_delta.c  编译:gcc phc_gps_delta.c -o phc_gps_delta */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/ptp_clock.h>
#include <time.h>
#include <sys/pps.h>

static inline uint64_t ts_to_ns(const struct timespec *ts)
{
    return (uint64_t)ts->tv_sec * 1000000000ULL + ts->tv_nsec;
}

int main(int argc, char **argv)
{
    if (argc != 3) {
        fprintf(stderr, "用法: %s /dev/ptpX /dev/ppsY\n", argv[0]);
        return 1;
    }
    int fd_ptp = open(argv[1], O_RDONLY);
    int fd_pps = open(argv[2], O_RDWR);
    if (fd_ptp < 0 || fd_pps < 0) {
        perror("open");
        return 1;
    }
    /* 1. 启用 PPS 时间戳获取 */
    int mode = PPS_CAPTUREASSERT | PPS_TSFMT_TSPEC;
    if (ioctl(fd_pps, PPS_SETPARAMS, &mode) < 0) {
        perror("PPS_SETPARAMS");
        return 1;
    }
    /* 2. 等待下一个 1 PPS 沿 */
    pps_info_t inf;
    if (ioctl(fd_pps, PPS_FETCH, &inf) < 0) {
        perror("PPS_FETCH");
        return 1;
    }
    struct timespec gps_ts = inf.assert_timestamp;   /* 已转为 CLOCK_REALTIME */
    /* 3. 立即采样 PHC */
    struct ptp_clock_time phc_t;
    if (ioctl(fd_ptp, PTP_CLOCK_GETTIME, &phc_t) < 0) {
        perror("PTP_CLOCK_GETTIME");
        return 1;
    }
    struct timespec phc_ts = {
        .tv_sec  = phc_t.sec,
        .tv_nsec = phc_t.nsec
    };
    /* 4. 计算误差,处理回绕 */
    int64_t delta = (int64_t)ts_to_ns(&phc_ts) - (int64_t)ts_to_ns(&gps_ts);
    printf("PHC – GPS 误差 = %+" PRId64 " ns\n", delta);
    close(fd_ptp);
    close(fd_pps);
    return 0;
}

运行示例(板端):

# ./phc_gps_delta /dev/ptp0 /dev/pps0
PHC – GPS 误差 = -87 ns

关键点解释:

  • 用 PPS_FETCH 阻塞到下一个脉冲,保证 GPS 时间戳与 PHC 采样在同一秒;
  • 所有时间统一成 纳秒级 uint64,减法直接用 int64_t 防回绕;
  • 若误差绝对值 > 500 ns,需通过 PTP_CLOCK_ADJUST 或 phc2sys 做 频率校正。

拓展思考

  1. 国内量产要求:
    运营商 5G 小基站验收时,会把 GPS 天线拔掉 30 min,再插回,要求 60 s 内 |Δt| < 50 ns。
    候选人应补充:

    • 在守护进程里 每秒采样,用 IIR 低通 滤掉 300 ns 级抖动;
    • 当连续 10 次 |Δt| > 200 ns,触发 ** servo 模式**,调用 PTP_FREQ_ADJUST 微调 PHC 频率。
  2. 跨时钟域比较:
    GPS 时间戳是 CLOCK_REALTIME,而 PHC 是 独立硬件时钟,二者存在 漂移。
    严谨做法:

    • 先用 phc_ctl 把 PHC 同步到 CLOCK_REALTIME(phc_ctl /dev/ptp0 set);
    • 再在用户态比较,避免 双时钟漂移 引入额外误差。
  3. 安全车规场景:
    自动驾驶域控采用 两片 RK3588,一片跑感知,一片跑规控,要求 跨板时间差 < 100 ns。
    此时 GPS 只给主板,副板通过 gPTP(802.1AS) 拿时间;
    候选人需说明:

    • 用 linuxptp 的 phc2sys 把主板 PHC 同步到系统时钟;
    • 副板用 gPTP 硬件时间戳 恢复时间,再回读本地 PHC,同样用上述代码 验证跨板误差。

掌握以上细节,面试中可主动抛出 “我曾把误差从 2 μs 调到 40 ns” 的量化结果,直接击中面试官痛点,显著拉开与普通候选人的差距。