From 971cc458aea21832a20b1b087185659d8e9ec2b3 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 19 Feb 2019 23:00:23 +0000 Subject: fix offsets --- app/dcf77.c | 19 ++++++++++++------- app/gps.c | 27 +++++++++++++-------------- app/msf.c | 5 ++--- app/ntp.c | 5 +++++ app/pll.c | 14 ++++++++++++-- app/util.c | 29 ++++++++++------------------- 6 files changed, 54 insertions(+), 45 deletions(-) diff --git a/app/dcf77.c b/app/dcf77.c index 361e4ca..2e9a9a4 100644 --- a/app/dcf77.c +++ b/app/dcf77.c @@ -54,19 +54,24 @@ static void process_bits (uint64_t abs) u.jday = 0; u.year = le_bcd (bits, 50, 57); u.month = le_bcd (bits, 45, 49); - u.mday = bcd (bits, 36, 41); - u.hour = bcd (bits, 29, 34); - u.minute = bcd (bits, 21, 27); - u.second = 58; + u.mday = le_bcd (bits, 36, 41); + u.hour = le_bcd (bits, 29, 34); + u.minute = le_bcd (bits, 21, 27); + u.second = 0; u.nanosecond = 0; + dcf77_time = time_utc_to_epoch (u); - dcf77_time.s -= 58; -#if 0 + + dcf77_time.s -= 2; /*Message arrives 2s early*/ + + dcf77_time.s -= 3600; /*CET*/ + + if (bits[17]) dcf77_time.s -= 3600; /*CEST*/ + pll_set_offset (dcf77_time, abs); -#endif printf ("DCF77: Next minute is: %02d-%02d-%02d %02d:%02d\r\n", u.year, u.month, u.mday, u.hour, u.minute); time_print_epoch ("DCF77: ", dcf77_time); diff --git a/app/gps.c b/app/gps.c index 7e2fd1b..5fc9994 100644 --- a/app/gps.c +++ b/app/gps.c @@ -185,9 +185,16 @@ ubx_recv_utc (uint8_t *ptr, unsigned len) ptr += ubx_get_u8 (ptr, &min); ptr += ubx_get_u8 (ptr, &sec); - // printf ("gps %02d:%02d:%02d %09d\r\n", (int) hour, (int) min, (int) sec, (int) nano); + printf ("GPS META-DATA %04d-%02d-%02d %02d:%02d:%02d Fix:%c%c TXCO %+8dE-12\r\n", + year, + month, + day, + hour, + min, + sec, + fix, fix2, (int) freq); - { + if (gps_happy > 3) { UTC u; EPOCH gps_time; uint32_t now; @@ -425,12 +432,12 @@ ubx_dispatch_search (int s_class, int s_id, unsigned *len_ptr) } static void gps_pps_dispatch (void) { - char buf[80]; + //char buf[80]; uint32_t now; uint64_t abs; int v; EPOCH e; - UTC u; + //UTC u; if (gps_ring.rx_ptr == gps_ring.tx_ptr) return; @@ -451,15 +458,7 @@ static void gps_pps_dispatch (void) e = pll_decompose (abs); - u = time_epoch_to_utc (e); - - printf (" GPS %02d:%02d:%02d.%09d Fix:%c%c TXCO %+8dE-12\r\n", - u.hour, - u.minute, - u.second, - u.nanosecond, - fix, fix2, (int) freq); - + //u = time_epoch_to_utc (e); time_print_epoch ("GPS : ", e); } @@ -669,7 +668,7 @@ gps_init (void) { uint8_t buf[80], *ptr; unsigned len; - uint16_t u2; + // uint16_t u2; usart3_drain(); diff --git a/app/msf.c b/app/msf.c index 69790ff..96c3afe 100644 --- a/app/msf.c +++ b/app/msf.c @@ -82,15 +82,14 @@ static void process_bits (uint64_t abs) u.mday = bcd (bitsa, 30, 35); u.hour = bcd (bitsa, 39, 44); u.minute = bcd (bitsa, 45, 51); - u.second = 59; + u.second = 0; u.nanosecond = 0; /* This is always valid a check_min_ident will fail for leap seconds*/ msf_time = time_utc_to_epoch (u); - msf_time.s -= 59; - // pll_set_offset (msf_time, abs); + pll_set_offset (msf_time, abs); stats_stamp (u, abs); dump_bits ("msfa", bitsa); diff --git a/app/ntp.c b/app/ntp.c index 2aa33e4..f8ca357 100644 --- a/app/ntp.c +++ b/app/ntp.c @@ -78,6 +78,11 @@ 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 (!time_known) break; + + memcpy (&pkt, p->payload, sizeof (ntp_packet_t)); pbuf_realloc (p, sizeof (ntp_packet_t)); diff --git a/app/pll.c b/app/pll.c index 0c9ed80..43fb9ad 100644 --- a/app/pll.c +++ b/app/pll.c @@ -105,16 +105,26 @@ void pll_dispatch (uint64_t edge) void pll_set_offset (EPOCH epoch, uint64_t abs) { + int64_t new_offset; + int diff; + /* Find nearest second to abs*/ abs += pll_freq >> 2; abs -= phase; abs /= pll_freq; - offset = epoch.s - abs; + new_offset = epoch.s - abs; + + if (new_offset != offset) { + diff = (int) (new_offset - offset); + + + printf ("PLL wallclock offset moved by %d\r\n", diff); + offset = new_offset; + } pll_valid = 1; time_known = 1; - } diff --git a/app/util.c b/app/util.c index ff35288..7f05a60 100644 --- a/app/util.c +++ b/app/util.c @@ -13,21 +13,16 @@ int check_parity (uint8_t *d, unsigned s, unsigned e, uint8_t p) unsigned bcd (uint8_t *d, unsigned s, unsigned e) { - unsigned ret = 0, c, b, i; + unsigned ret = 0, c, i; - for (i = e, c = 1, b = 0; i >= s; --i, b++) { + for (i = e, c = 1 ; i >= s; --i) { if (d[i]) ret += c; - switch (b & 3) { - case 0: - case 1: - case 2: + if (c & 0x77777777) c <<= 1; - break; - - default: + else { c >>= 3; c *= 10; } @@ -40,24 +35,20 @@ unsigned bcd (uint8_t *d, unsigned s, unsigned e) unsigned le_bcd (uint8_t *d, unsigned s, unsigned e) { - unsigned ret = 0, c, b, i; + unsigned ret = 0, c, i; - for (i = s, c = 1, b = 0; i <= e; ++i, b++) { + for (i = s, c = 1 ; i <= e; ++i) { if (d[i]) ret += c; - - switch (b & 3) { - case 0: - case 1: - case 2: + if (c & 0x77777777) c <<= 1; - break; - - default: + else { c >>= 3; c *= 10; } + + } return ret; -- cgit v1.2.3