summaryrefslogtreecommitdiffstats
path: root/app/ntp.c
diff options
context:
space:
mode:
Diffstat (limited to 'app/ntp.c')
-rw-r--r--app/ntp.c40
1 files changed, 31 insertions, 9 deletions
diff --git a/app/ntp.c b/app/ntp.c
index 0bdda4d..2b9e6f3 100644
--- a/app/ntp.c
+++ b/app/ntp.c
@@ -30,7 +30,7 @@ typedef struct {
#if 0
packet (uint8_t *_p)
{
- EPOCH reftime = pll_decompose (pll_last_update);
+ EPOCH reftime = ref_decompose (pll_last_update);
ntp_packet_t rx = (uint8_t *)_p;
ntp_packet_t tx;
@@ -52,10 +52,26 @@ packet (uint8_t *_p)
}
#endif
-static uint64_t ntp_ts (uint64_t v)
+static uint64_t ntp_ts_ref (uint64_t v)
{
uint64_t ret;
- EPOCH e = pll_decompose (v);
+ EPOCH e = ref_decompose (v);
+
+ ret = e.ns;
+ ret <<= 32;
+ ret = ret / 1000000000;
+ ret = htonl (ret);
+ ret <<= 32;
+ ret |= htonl (2208988800UL + e.s);
+
+ return ret;
+}
+
+
+static uint64_t ntp_ts_ptp (uint64_t v)
+{
+ uint64_t ret;
+ EPOCH e = ptp_decompose (v);
ret = e.ns;
ret <<= 32;
@@ -69,7 +85,7 @@ static uint64_t ntp_ts (uint64_t v)
static void ntp_rx (void *arg, struct udp_pcb *s, struct pbuf *p, struct ip_addr *src_addr, u16_t port)
{
- uint32_t now = HW_CLOCK_REG;
+ uint64_t ref = ref_get_irq();
struct ip_addr dst_addr = *src_addr;
ntp_packet_t pkt;
@@ -78,7 +94,7 @@ static void ntp_rx (void *arg, struct udp_pcb *s, struct pbuf *p, struct ip_addr
if (p->len < sizeof (ntp_packet_t)) break;
- if (!pll_valid) break;
+ if (!ref_valid) break;
if (!time_known) break;
@@ -99,13 +115,19 @@ static void ntp_rx (void *arg, struct udp_pcb *s, struct pbuf *p, struct ip_addr
pkt.reference_id = htonl (0x47505300);
pkt.origin_ts = pkt.transmit_ts;
- pkt.receive_ts = ntp_ts (abs_extend (now));
- pkt.reference_ts = ntp_ts (pll_last_update);
+#if 0
+ pkt.receive_ts = ntp_ts_ref (ref);
+#else
+ pkt.receive_ts = ntp_ts_ptp (p->ptp_timestamp);
+#endif
- now = HW_CLOCK_REG;
- pkt.transmit_ts = ntp_ts (abs_extend (now));
+ pkt.receive_ts = ntp_ts_ref (ref);
+ pkt.reference_ts = ntp_ts_ref (ref_last_update);
memcpy (p->payload, &pkt, sizeof (ntp_packet_t));
+ compiler_mb();
+ ref = ref_get_irq();
+ pkt.transmit_ts = ntp_ts_ref (ref);
udp_sendto (s, p, &dst_addr, port);
} while (0);