From c93861c733ced9c6659241cea69c7feed56afcce Mon Sep 17 00:00:00 2001 From: fishsoupisgood Date: Wed, 20 Feb 2019 02:39:38 +0000 Subject: use OCXO, and auto fail-over between different clock sources --- app/Makefile | 2 +- app/dcf77.c | 9 +++++++-- app/gps.c | 31 ++++++++++++++++++++----------- app/gps.h | 4 ++-- app/main.c | 19 ++++++++++++++++++- app/msf.c | 22 +++++++++++++++++++--- app/pll.c | 35 ++++++++++++++++++++++++++++++----- app/project.h | 5 +++-- app/prototypes.h | 22 ++++------------------ app/stats.c | 2 ++ app/steth.c | 6 +++--- 11 files changed, 109 insertions(+), 48 deletions(-) diff --git a/app/Makefile b/app/Makefile index c325681..e8ae217 100644 --- a/app/Makefile +++ b/app/Makefile @@ -109,4 +109,4 @@ tidy: astyle -A3 -s2 --attach-extern-c -L -c -w -Y -m0 -f -p -H -U -k3 -xj -xd ${CSRCS} ${HSRCS} slog: - sympathy -d /dev/ttyUSB5 -b 38400 -L ../log -w 132x59 -s + sympathy -d /dev/ttyUSB6 -b 38400 -L ../log -w 132x59 -s diff --git a/app/dcf77.c b/app/dcf77.c index 2e9a9a4..909188b 100644 --- a/app/dcf77.c +++ b/app/dcf77.c @@ -10,7 +10,8 @@ static Event_ring dcf77_ring; -uint64_t dcf77_last_second; +static uint64_t dcf77_last_second; +uint64_t dcf77_last_happy; void exti15_10_isr (void) { @@ -71,8 +72,12 @@ static void process_bits (uint64_t abs) if (bits[17]) dcf77_time.s -= 3600; /*CEST*/ + dcf77_last_happy = abs - (int64_t) 60 * (int64_t) HZ; + pll_set_offset (dcf77_time, abs); + + 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); @@ -161,7 +166,7 @@ void dcf77_dispatch (void) abs = abs_extend (now); dcf77_last_second = abs; - //pll_dispatch (abs); + pll_dispatch (dcf77_last_happy, abs, "DCF77"); if (time_known) report_time (abs); diff --git a/app/gps.c b/app/gps.c index 5fc9994..b3c322d 100644 --- a/app/gps.c +++ b/app/gps.c @@ -12,8 +12,10 @@ static int ubx_ack_xfer = 0; static int current_ref_hz = 1; -int gps_locked; -int gps_happy; +static int gps_locked; +static int gps_happy; + +uint64_t gps_last_happy; static char fix, fix2; static int32_t freq = 0; @@ -214,7 +216,11 @@ ubx_recv_utc (uint8_t *ptr, unsigned len) now = SCS_DWT_CYCCNT; abs = abs_extend (now); + + gps_last_happy = abs + (int64_t) HZ * (int64_t) 60; + pll_set_offset (gps_time, abs); + } @@ -453,7 +459,8 @@ static void gps_pps_dispatch (void) - pll_dispatch (abs); + if (gps_happy) + pll_dispatch (gps_last_happy, abs, "GPS"); e = pll_decompose (abs); @@ -471,7 +478,7 @@ gps_dispatch (void) gps_pps_dispatch(); } -void +static void ubx_send (uint8_t class, uint8_t id, const void *_payload, unsigned len) { uint8_t ck_a = 0, ck_b = 0; @@ -502,7 +509,7 @@ ubx_send (uint8_t class, uint8_t id, const void *_payload, unsigned len) } -int +static int ubx_handshake (uint8_t class, uint8_t id, const void *payload, unsigned len) { uint32_t timeout; @@ -532,7 +539,7 @@ ubx_handshake (uint8_t class, uint8_t id, const void *payload, unsigned len) return 0; } -int +static int ubx_handshake_xfer (uint8_t class, uint8_t id, const void *payload, unsigned len) { @@ -567,7 +574,7 @@ ubx_handshake_xfer (uint8_t class, uint8_t id, const void *payload, -uint8_t * +static uint8_t * ubx_fetch (uint8_t class, uint8_t id, void *payload, unsigned len, unsigned *len_ptr) { @@ -584,7 +591,7 @@ ubx_fetch (uint8_t class, uint8_t id, void *payload, unsigned len, return ret; } -int +static int ubx_set_message_rate_port1 (uint8_t class, uint8_t id, uint8_t rate) { uint8_t buf[8], *ptr; @@ -604,7 +611,7 @@ ubx_set_message_rate_port1 (uint8_t class, uint8_t id, uint8_t rate) } -int +static int ubx_cfg_rst (uint16_t flags) { uint8_t buf[8], *ptr; @@ -620,7 +627,7 @@ ubx_cfg_rst (uint16_t flags) } -int +static int gps_set_ref (int ref_hz) { uint8_t buf[80], *ptr; @@ -852,7 +859,8 @@ gps_reset (void) } -int +#if 0 +static int ubx_get_clock_stats (void) { uint8_t *ptr; @@ -864,3 +872,4 @@ ubx_get_clock_stats (void) //return ubx_recv_clock_stats(ptr,len); return ptr ? 0 : -1; } +#endif diff --git a/app/gps.h b/app/gps.h index ce6ee14..07933cf 100644 --- a/app/gps.h +++ b/app/gps.h @@ -1,5 +1,4 @@ -extern int gps_happy; - +#if 0 static inline int gps_is_happy (void) { return gps_happy > 15; @@ -9,3 +8,4 @@ static inline int gps_is_stable (void) return gps_happy > 60; } +#endif diff --git a/app/main.c b/app/main.c index 229674e..6116a5b 100644 --- a/app/main.c +++ b/app/main.c @@ -28,11 +28,28 @@ static void cmd_dispatch() } } +static const clock_scale_t hse_10mhz_3v3_168 = { + /* 168MHz */ + .pllm = 10, + .plln = 336, + .pllp = 2, + .pllq = 7, + .hpre = RCC_CFGR_HPRE_DIV_NONE, + .ppre1 = RCC_CFGR_PPRE_DIV_4, + .ppre2 = RCC_CFGR_PPRE_DIV_2, + .flash_config = FLASH_ACR_ICE | FLASH_ACR_DCE | + FLASH_ACR_LATENCY_5WS, + .apb1_frequency = 42000000, + .apb2_frequency = 84000000, +}; + + static void board_setup (void) { - rcc_clock_setup_hse_3v3 (&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); + rcc_osc_bypass_enable (HSE); + rcc_clock_setup_hse_3v3 (&hse_10mhz_3v3_168); rcc_periph_clock_enable (RCC_SYSCFG); rcc_periph_clock_enable (RCC_GPIOA); diff --git a/app/msf.c b/app/msf.c index 96c3afe..4b3c350 100644 --- a/app/msf.c +++ b/app/msf.c @@ -8,7 +8,9 @@ static Event_ring msf_ring; -uint64_t msf_last_second; +static uint64_t msf_last_second; +uint64_t msf_last_happy; + void exti0_isr (void) @@ -25,6 +27,19 @@ void exti0_isr (void) msf_ring.events[msf_ring.tx_ptr].value = v; msf_ring.tx_ptr = (msf_ring.tx_ptr + 1) & ERING_MASK; +#if 0 + + if (time_known) { + uint64_t abs = abs_extend (now); + EPOCH e = pll_decompose (abs); + UTC u = time_epoch_to_utc (e); + + printf ("QRR %d.%09d %d\r\n", (u.minute * 60) + u.second, u.nanosecond, (int) !v); + printf ("QRR %d.%09d %d\r\n", (u.minute * 60) + u.second, u.nanosecond, (int) v); + } + +#endif + nvic_enable_irq (NVIC_EXTI0_IRQ); } @@ -89,9 +104,10 @@ static void process_bits (uint64_t abs) msf_time = time_utc_to_epoch (u); + msf_last_happy = abs - (int64_t) 120 * (int64_t) HZ; + pll_set_offset (msf_time, abs); - stats_stamp (u, abs); dump_bits ("msfa", bitsa); dump_bits ("msfb", bitsb); @@ -170,7 +186,7 @@ void msf_dispatch (void) abs = abs_extend (now); msf_last_second = abs; - //pll_dispatch (abs); + pll_dispatch (msf_last_happy, abs, "MSF"); diff --git a/app/pll.c b/app/pll.c index 43fb9ad..5fa370e 100644 --- a/app/pll.c +++ b/app/pll.c @@ -2,7 +2,7 @@ #define JUMP_THRESH 0.1 #define JUMP_TICKS 30 -#define FEEDBACK 0.01 +#define FEEDBACK 0.001 #define WARM_UP 3 @@ -47,7 +47,7 @@ static void modify_pll_freq (uint64_t now, int d) phase = now - pd2; } -void pll_dispatch (uint64_t edge) +void pll_dispatch (uint64_t happy, uint64_t edge, const char *src) { double f; int64_t pd; @@ -58,6 +58,32 @@ void pll_dispatch (uint64_t edge) } +#if 0 + { + int h1, h2, h3, h4; + EPOCH e; + + + e = pll_decompose (happy); + h1 = e.s; + e = pll_decompose (gps_last_happy); + h2 = e.s; + e = pll_decompose (dcf77_last_happy); + h3 = e.s; + e = pll_decompose (msf_last_happy); + h4 = e.s; + + printf ("H %d %d %d %d\r\n", h1 - h2, h2 - h2, h3 - h2, h4 - h2); + } +#endif + + + if (happy < gps_last_happy) return; + + if (happy < dcf77_last_happy) return; + + if (happy < msf_last_happy) return; + { int diff, hf; @@ -81,9 +107,7 @@ void pll_dispatch (uint64_t edge) else if (out_of_lock <= JUMP_TICKS) out_of_lock = 0; -#if 1 - printf ("PLL pd %.3f pll_freq %d phase %d\r\n", (float) f, (int) pll_freq, (int) phase); -#endif + printf ("PLL pd %.3f pll_freq %d phase %d %s\r\n", (float) f, (int) pll_freq, (int) phase, src); if (out_of_lock > JUMP_TICKS) { phase += pd; @@ -108,6 +132,7 @@ 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; diff --git a/app/project.h b/app/project.h index f3a49f1..fa8d5b2 100644 --- a/app/project.h +++ b/app/project.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -41,10 +42,10 @@ #include "prototypes.h" -//#define HZ 168000000 +#define HZ 168000000 //#define HZ 167968615 //#define HZ 165925490 -#define HZ 167996682 +//#define HZ 167996682 #define TRACE do { stdio_drain(); printf("%s:%d\r\n",__FUNCTION__,__LINE__); } while (0) diff --git a/app/prototypes.h b/app/prototypes.h index 22a7334..44cc1c8 100644 --- a/app/prototypes.h +++ b/app/prototypes.h @@ -44,16 +44,13 @@ extern uint32_t sys_now(void); extern void dispatch_lwip(void); extern void start_lwip(void); /* steth.c */ -extern _Bool phy_link_an_done(uint8_t phy); extern void eth_isr(void); extern void steth_isr(void); extern err_t steth_lwip_init(struct netif *netif); -extern void eth_reset(void); -extern void eth_start_an(void); extern void steth_init(void); extern void steth_slow_tick(void); /* msf.c */ -extern uint64_t msf_last_second; +extern uint64_t msf_last_happy; extern void exti0_isr(void); extern void msf_dispatch(void); extern void msf_init(void); @@ -67,7 +64,7 @@ extern int64_t pll_freq; extern uint64_t pll_last_update; extern int pll_valid; extern void pll_meh(void); -extern void pll_dispatch(uint64_t edge); +extern void pll_dispatch(uint64_t happy, uint64_t edge, const char *src); extern void pll_set_offset(EPOCH epoch, uint64_t abs); extern EPOCH _pll_decompose(uint64_t abs); extern EPOCH pll_decompose_diff(int64_t diff); @@ -83,7 +80,7 @@ extern void time_print_epoch(const char *p, EPOCH e); /* ntp.c */ extern void ntp_init(void); /* dcf77.c */ -extern uint64_t dcf77_last_second; +extern uint64_t dcf77_last_happy; extern void exti15_10_isr(void); extern void dcf77_dispatch(void); extern void dcf77_init(void); @@ -92,24 +89,13 @@ extern int check_parity(uint8_t *d, unsigned s, unsigned e, uint8_t p); extern unsigned bcd(uint8_t *d, unsigned s, unsigned e); extern unsigned le_bcd(uint8_t *d, unsigned s, unsigned e); /* stats.c */ -extern void stats(void); -extern void stats_stamp(UTC m, uint64_t abs); /* gps.c */ -extern int gps_locked; -extern int gps_happy; +extern uint64_t gps_last_happy; extern void exti2_isr(void); extern void gps_dispatch(void); -extern void ubx_send(uint8_t class, uint8_t id, const void *_payload, unsigned len); -extern int ubx_handshake(uint8_t class, uint8_t id, const void *payload, unsigned len); -extern int ubx_handshake_xfer(uint8_t class, uint8_t id, const void *payload, unsigned len); -extern uint8_t *ubx_fetch(uint8_t class, uint8_t id, void *payload, unsigned len, unsigned *len_ptr); -extern int ubx_set_message_rate_port1(uint8_t class, uint8_t id, uint8_t rate); -extern int ubx_cfg_rst(uint16_t flags); -extern int gps_set_ref(int ref_hz); extern int gps_init(void); extern int gps_almanac(void); extern void gps_reset(void); -extern int ubx_get_clock_stats(void); /* hexdump.c */ extern void hexdump(void *_d, int len); /* bits.c */ diff --git a/app/stats.c b/app/stats.c index dfed017..1764b70 100644 --- a/app/stats.c +++ b/app/stats.c @@ -1,6 +1,7 @@ #include "project.h" +#if 0 void stats (void) { int64_t d; @@ -42,3 +43,4 @@ void stats_stamp (UTC m, uint64_t abs) } +#endif diff --git a/app/steth.c b/app/steth.c index 5ed11c7..2603725 100644 --- a/app/steth.c +++ b/app/steth.c @@ -101,7 +101,7 @@ static void phy_stat (void) phy_stat_reg (i); } -bool +static bool phy_link_an_done (uint8_t phy) { return eth_smi_read (PHY, PHY_REG_BSR) & PHY_REG_BSR_ANDONE; @@ -230,7 +230,7 @@ static void fucked_eth_init (uint8_t phy, enum eth_clk clock) #endif -void eth_reset (void) +static void eth_reset (void) { unsigned i; @@ -295,7 +295,7 @@ void eth_reset (void) } -void eth_start_an (void) +static void eth_start_an (void) { printf ("starting autonegociation\r\n"); eth_smi_write (PHY, PHY_REG_ANTX, 0x1e1); -- cgit v1.2.3