kern_ntptime.c (44776) | kern_ntptime.c (44794) |
---|---|
1/*********************************************************************** 2 * * 3 * Copyright (c) David L. Mills 1993-1998 * 4 * * 5 * Permission to use, copy, modify, and distribute this software and * 6 * its documentation for any purpose and without fee is hereby * 7 * granted, provided that the above copyright notice appears in all * 8 * copies and that both the copyright notice and this permission * --- 138 unchanged lines hidden (view full) --- 147#ifdef PPS_SYNC 148/* 149 * The following variables are used when a pulse-per-second (PPS) signal 150 * is available and connected via a modem control lead. They establish 151 * the engineering parameters of the clock discipline loop when 152 * controlled by the PPS signal. 153 */ 154#define PPS_FAVG 2 /* min freq avg interval (s) (shift) */ | 1/*********************************************************************** 2 * * 3 * Copyright (c) David L. Mills 1993-1998 * 4 * * 5 * Permission to use, copy, modify, and distribute this software and * 6 * its documentation for any purpose and without fee is hereby * 7 * granted, provided that the above copyright notice appears in all * 8 * copies and that both the copyright notice and this permission * --- 138 unchanged lines hidden (view full) --- 147#ifdef PPS_SYNC 148/* 149 * The following variables are used when a pulse-per-second (PPS) signal 150 * is available and connected via a modem control lead. They establish 151 * the engineering parameters of the clock discipline loop when 152 * controlled by the PPS signal. 153 */ 154#define PPS_FAVG 2 /* min freq avg interval (s) (shift) */ |
155#define PPS_FAVGMAX 8 /* max freq avg interval (s) (shift) */ | 155#define PPS_FAVGMAX 7 /* max freq avg interval (s) (shift) */ |
156#define PPS_PAVG 4 /* phase avg interval (s) (shift) */ 157#define PPS_VALID 120 /* PPS signal watchdog max (s) */ 158#define MAXTIME 500000 /* max PPS error (jitter) (ns) */ 159#define MAXWANDER 500000 /* max PPS wander (ns/s/s) */ 160 161struct ppstime { 162 long sec; /* PPS seconds */ 163 long nsec; /* PPS nanoseconds */ | 156#define PPS_PAVG 4 /* phase avg interval (s) (shift) */ 157#define PPS_VALID 120 /* PPS signal watchdog max (s) */ 158#define MAXTIME 500000 /* max PPS error (jitter) (ns) */ 159#define MAXWANDER 500000 /* max PPS wander (ns/s/s) */ 160 161struct ppstime { 162 long sec; /* PPS seconds */ 163 long nsec; /* PPS nanoseconds */ |
164 long count; /* PPS nanosecond counter */ | |
165}; 166static struct ppstime pps_tf[3]; /* phase median filter */ 167static struct ppstime pps_filt; /* phase offset */ | 164}; 165static struct ppstime pps_tf[3]; /* phase median filter */ 166static struct ppstime pps_filt; /* phase offset */ |
167static long pps_fcount; /* frequency accumulator */ |
|
168static l_fp pps_freq; /* scaled frequency offset (ns/s) */ 169static long pps_offacc; /* offset accumulator */ 170static long pps_jitter; /* scaled time dispersion (ns) */ 171static long pps_stabil; /* scaled frequency dispersion (ns/s) */ | 168static l_fp pps_freq; /* scaled frequency offset (ns/s) */ 169static long pps_offacc; /* offset accumulator */ 170static long pps_jitter; /* scaled time dispersion (ns) */ 171static long pps_stabil; /* scaled frequency dispersion (ns/s) */ |
172static long pps_lastcount; /* last counter offset */ | |
173static long pps_lastsec; /* time at last calibration (s) */ 174static int pps_valid; /* signal watchdog counter */ 175static int pps_shift = PPS_FAVG; /* interval duration (s) (shift) */ 176static int pps_intcnt; /* wander counter */ 177static int pps_offcnt; /* offset accumulator counter */ 178 179/* 180 * PPS signal quality monitors --- 133 unchanged lines hidden (view full) --- 314 315 /* 316 * Retrieve all clock variables 317 */ 318 if (time_status & STA_NANO) 319 ntv.offset = L_GINT(time_offset); 320 else 321 ntv.offset = L_GINT(time_offset) / 1000; | 172static long pps_lastsec; /* time at last calibration (s) */ 173static int pps_valid; /* signal watchdog counter */ 174static int pps_shift = PPS_FAVG; /* interval duration (s) (shift) */ 175static int pps_intcnt; /* wander counter */ 176static int pps_offcnt; /* offset accumulator counter */ 177 178/* 179 * PPS signal quality monitors --- 133 unchanged lines hidden (view full) --- 313 314 /* 315 * Retrieve all clock variables 316 */ 317 if (time_status & STA_NANO) 318 ntv.offset = L_GINT(time_offset); 319 else 320 ntv.offset = L_GINT(time_offset) / 1000; |
322 ntv.freq = L_GINT(time_freq) * SCALE_PPM; | 321 ntv.freq = L_GINT((time_freq / 1000) * 65536); |
323 ntv.maxerror = time_maxerror; 324 ntv.esterror = time_esterror; 325 ntv.status = time_status; 326 if (ntv.constant < 0) 327 time_constant = 0; 328 else if (ntv.constant > MAXTC) 329 time_constant = MAXTC; 330 else 331 time_constant = ntv.constant; 332 if (time_status & STA_NANO) 333 ntv.precision = time_precision; 334 else 335 ntv.precision = time_precision / 1000; 336 ntv.tolerance = MAXFREQ * SCALE_PPM; 337#ifdef PPS_SYNC 338 ntv.shift = pps_shift; | 322 ntv.maxerror = time_maxerror; 323 ntv.esterror = time_esterror; 324 ntv.status = time_status; 325 if (ntv.constant < 0) 326 time_constant = 0; 327 else if (ntv.constant > MAXTC) 328 time_constant = MAXTC; 329 else 330 time_constant = ntv.constant; 331 if (time_status & STA_NANO) 332 ntv.precision = time_precision; 333 else 334 ntv.precision = time_precision / 1000; 335 ntv.tolerance = MAXFREQ * SCALE_PPM; 336#ifdef PPS_SYNC 337 ntv.shift = pps_shift; |
339 ntv.ppsfreq = L_GINT(pps_freq) * SCALE_PPM; | 338 ntv.ppsfreq = L_GINT((pps_freq / 1000) * 65536); |
340 ntv.jitter = pps_jitter; 341 if (time_status & STA_NANO) 342 ntv.jitter = pps_jitter; 343 else 344 ntv.jitter = pps_jitter / 1000; 345 ntv.stabil = pps_stabil; 346 ntv.calcnt = pps_calcnt; 347 ntv.errcnt = pps_errcnt; --- 155 unchanged lines hidden (view full) --- 503 * The following variables are initialized only at startup. Only 504 * those structures not cleared by the compiler need to be 505 * initialized, and these only in the simulator. In the actual 506 * kernel, any nonzero values here will quickly evaporate. 507 */ 508 L_CLR(time_offset); 509 L_CLR(time_freq); 510#ifdef PPS_SYNC | 339 ntv.jitter = pps_jitter; 340 if (time_status & STA_NANO) 341 ntv.jitter = pps_jitter; 342 else 343 ntv.jitter = pps_jitter / 1000; 344 ntv.stabil = pps_stabil; 345 ntv.calcnt = pps_calcnt; 346 ntv.errcnt = pps_errcnt; --- 155 unchanged lines hidden (view full) --- 502 * The following variables are initialized only at startup. Only 503 * those structures not cleared by the compiler need to be 504 * initialized, and these only in the simulator. In the actual 505 * kernel, any nonzero values here will quickly evaporate. 506 */ 507 L_CLR(time_offset); 508 L_CLR(time_freq); 509#ifdef PPS_SYNC |
511 pps_filt.sec = pps_filt.nsec = pps_filt.count = 0; | 510 pps_filt.sec = pps_filt.nsec = 0; 511 pps_fcount = 0; |
512 pps_tf[0] = pps_tf[1] = pps_tf[2] = pps_filt; 513 L_CLR(pps_freq); 514#endif /* PPS_SYNC */ 515} 516 517SYSINIT(ntpclocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, ntp_init, NULL) 518 519/* --- 133 unchanged lines hidden (view full) --- 653 * boundary during the last second, so correct the tick. Very 654 * intricate. 655 */ 656 u_nsec = nsec; 657 if (u_nsec > (NANOSECOND >> 1)) 658 u_nsec -= NANOSECOND; 659 else if (u_nsec < -(NANOSECOND >> 1)) 660 u_nsec += NANOSECOND; | 512 pps_tf[0] = pps_tf[1] = pps_tf[2] = pps_filt; 513 L_CLR(pps_freq); 514#endif /* PPS_SYNC */ 515} 516 517SYSINIT(ntpclocks, SI_SUB_CLOCKS, SI_ORDER_FIRST, ntp_init, NULL) 518 519/* --- 133 unchanged lines hidden (view full) --- 653 * boundary during the last second, so correct the tick. Very 654 * intricate. 655 */ 656 u_nsec = nsec; 657 if (u_nsec > (NANOSECOND >> 1)) 658 u_nsec -= NANOSECOND; 659 else if (u_nsec < -(NANOSECOND >> 1)) 660 u_nsec += NANOSECOND; |
661#if 0 662 if (u_nsec > (time_tick >> 1)) 663 u_nsec -= time_tick; 664 else if (u_nsec < -(time_tick >> 1)) 665 u_nsec += time_tick; 666#endif 667 pps_tf[0].count = pps_tf[1].count + u_nsec; | 661 pps_fcount += u_nsec; |
668 if (v_nsec > MAXFREQ) { 669 return; 670 } 671 time_status &= ~STA_PPSJITTER; 672 673 /* 674 * A three-stage median filter is used to help denoise the PPS 675 * time. The median sample becomes the time offset estimate; the --- 60 unchanged lines hidden (view full) --- 736 * frequency. It will later be divided by the length of the 737 * interval to determine the frequency update. If the frequency 738 * exceeds a sanity threshold, or if the actual calibration 739 * interval is not equal to the expected length, the data are 740 * discarded. We can tolerate a modest loss of data here without 741 * degrading frequency ccuracy. 742 */ 743 pps_calcnt++; | 662 if (v_nsec > MAXFREQ) { 663 return; 664 } 665 time_status &= ~STA_PPSJITTER; 666 667 /* 668 * A three-stage median filter is used to help denoise the PPS 669 * time. The median sample becomes the time offset estimate; the --- 60 unchanged lines hidden (view full) --- 730 * frequency. It will later be divided by the length of the 731 * interval to determine the frequency update. If the frequency 732 * exceeds a sanity threshold, or if the actual calibration 733 * interval is not equal to the expected length, the data are 734 * discarded. We can tolerate a modest loss of data here without 735 * degrading frequency ccuracy. 736 */ 737 pps_calcnt++; |
744 v_nsec = -pps_filt.count; | 738 v_nsec = -pps_fcount; |
745 pps_lastsec = pps_tf[0].sec; | 739 pps_lastsec = pps_tf[0].sec; |
746 pps_tf[0].count = 0; | 740 pps_fcount = 0; |
747 u_nsec = MAXFREQ << pps_shift; 748 if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 << 749 pps_shift)) { 750 time_status |= STA_PPSERROR; 751 pps_errcnt++; 752 return; 753 } 754 --- 41 unchanged lines hidden (view full) --- 796 if (u_nsec < 0) 797 u_nsec = -u_nsec; 798 pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG; 799 800 /* 801 * The frequency offset is averaged into the PPS frequency. If 802 * enabled, the system clock frequency is updated as well. 803 */ | 741 u_nsec = MAXFREQ << pps_shift; 742 if (v_nsec > u_nsec || v_nsec < -u_nsec || u_sec != (1 << 743 pps_shift)) { 744 time_status |= STA_PPSERROR; 745 pps_errcnt++; 746 return; 747 } 748 --- 41 unchanged lines hidden (view full) --- 790 if (u_nsec < 0) 791 u_nsec = -u_nsec; 792 pps_stabil += (u_nsec * SCALE_PPM - pps_stabil) >> PPS_FAVG; 793 794 /* 795 * The frequency offset is averaged into the PPS frequency. If 796 * enabled, the system clock frequency is updated as well. 797 */ |
804 L_RSHIFT(ftemp, PPS_FAVG); | 798 L_RSHIFT(ftemp, 1); |
805 L_ADD(pps_freq, ftemp); 806 u_nsec = L_GINT(pps_freq); 807 if (u_nsec > MAXFREQ) 808 L_LINT(pps_freq, MAXFREQ); 809 else if (u_nsec < -MAXFREQ) 810 L_LINT(pps_freq, -MAXFREQ); 811 if (time_status & STA_PPSFREQ) 812 time_freq = pps_freq; 813} 814#endif /* PPS_SYNC */ | 799 L_ADD(pps_freq, ftemp); 800 u_nsec = L_GINT(pps_freq); 801 if (u_nsec > MAXFREQ) 802 L_LINT(pps_freq, MAXFREQ); 803 else if (u_nsec < -MAXFREQ) 804 L_LINT(pps_freq, -MAXFREQ); 805 if (time_status & STA_PPSFREQ) 806 time_freq = pps_freq; 807} 808#endif /* PPS_SYNC */ |