【电路笔记 通信】IEEE 1588精密时间协议(PTP):时间戳格式+精确到ns的时间表示与处理
时间戳格式项目IEEE 1588 TruncatedNTP 64‑bitEpoch1970‑01‑01TAI1900‑01‑01UTC秒字段32‑bit 秒32‑bit 秒小数部分32‑bit纳秒(0–10⁹‑1)32‑bit秒分数(1/2³² s)闰秒不考虑TAI考虑UTC由 NTP精度纳秒~233 ps理论常见场景硬件/PTP/NSHNTP / 传统网络协议字段位数说明secondsField整秒部分32 bit4 字节自 PTP Epoch1970-01-01 00:00:00TAI起的秒数无闰秒nanosecondsField纳秒部分32 bit4 字节0 ~ 999,999,999 ns表示不足一秒的小数部分The two timestamp formats that can be usedinthe timestamp field are: o IEEE1588Truncated Timestamp Format: theformatof this field uses the64least significant bits of the IEEE1588-2008 Precision Time Protocolformat[IEEE1588]. This truncatedformatconsists of a32-bit seconds field followed by a32-bit nanoseconds field. As definedin[IEEE1588], the timestamp specifies the number of seconds elapsed since1January197000:00:00 according to the International Atomic Time(TAI). Mizrahi, et al. Expires February21,2018[Page4]Internet-Draft NSH Timestamp August2017012301234567890123456789012345678901--------------------------------|Seconds|--------------------------------|Nanoseconds|-------------------------------- Figure2: IEEE1588Truncated Timestamp Format[IEEE1588]. o NTP64-bit Timestamp Format: thisformatconsists of a32-bit seconds field followed by a32-bit fractional second field. As definedin[RFC5905], the timestamp specifies the number of seconds elapsed since1January190000:00:00 according to the Coordinated Universal Time(UTC).012301234567890123456789012345678901--------------------------------|Seconds|--------------------------------|Fraction|-------------------------------- Figure3: NTP[RFC5905]64-bit Timestamp Format - https://datatracker.ietf.org/doc/html/draft-mymb-sfc-nsh-allocation-timestamp-02?精确到ns的时间表示与处理// 基本数据结构推荐 reg [31:0] sec; // 秒低32位 reg [31:0] nsec; // 纳秒0–999_999_999 经过了多少个ns自增时间递增维护最常见:每周期加固定纳秒如 8 nsalways (posedge clk) begin if (nsec 32d1_000_000_000 - 32d8) begin // 进位条件是 1e9不是 防止亚稳态和边界 bug nsec nsec 32d8 - 32d1_000_000_000; sec sec 1b1; end else begin nsec nsec 32d8; end end相减(使用高频mcu处理比较容易)两个PTP时间戳相减Δt在同一 ToDTime of Day源、单调递增计数器、无软件干预可以假设 a_sec ≥ b_sec甚至可以 assert(a_sec b_sec)不需要显式处理 a_sec b_sec 的情况。// ts_a ts_b reg [31:0] delta_sec; reg [31:0] delta_nsec; always (*) begin if (a_nsec b_nsec) begin delta_sec a_sec - b_sec; delta_nsec a_nsec - b_nsec; end else begin delta_sec a_sec - b_sec - 1b1; delta_nsec a_nsec 32d1_000_000_000 - b_nsec; end end减法中考虑a_sec b_sec的情况有符号数正确姿势二进制补码Two’s Complement是计算机内部表示和存储有符号整数的标准方法。它将符号位与数值位统一处理使加减法硬件电路大大简化。正数的补码就是其本身负数的补码则是将其原码除符号位外按位取反末位再加 1。reg signed [31:0] a, b; wire signed [31:0] diff; assign diff a - b;采用有符号数标志位reg [31:0] a_sec, a_nsec; reg [31:0] b_sec, b_nsec; reg valid; reg [31:0] delta_sec; reg [31:0] delta_nsec; always (*) begin if (a_sec b_sec || (a_sec b_sec a_nsec b_nsec)) begin // 小数减大数 → valid 0 valid 1b0; delta_sec 32d0; delta_nsec 32d0; end else begin valid 1b1; if (a_nsec b_nsec) begin delta_sec a_sec - b_sec; delta_nsec a_nsec - b_nsec; end else begin delta_sec a_sec - b_sec - 1b1; delta_nsec a_nsec 32d1_000_000_000 - b_nsec; end end end