ss_time_lib.h

00001 
00002 
00003 #ifndef SS_TIME_LIB_H
00004 #define SS_TIME_LIB_H
00005 
00006 #ifdef __cplusplus
00007 extern "C" {
00008 #endif
00009 
00010 #define EDT_SSTM_COUNTS 0x04010080
00011 #define EDT_SSTM_SECS    0x04010084
00012 #define EDT_SSTM_SET     0x04010088
00013 #define EDT_SSTM_ADJ_VALUE  0x0301008C
00014 #define EDT_SSTM_CMD     0x0101008F
00015 
00016 #define EDT_SSTM_COPY       0x01
00017 #define EDT_SSTM_LATCH      0x02
00018 #define EDT_SSTM_COPY_ADJ         0x04
00019 
00020 #define EDT_SSTM_ADJ_EN   0x10
00021 #define EDT_SSTM_ADJ_PLUS   0x20
00022 #define EDT_SSTM_CMD_LOCK   0x80
00023 
00024 #define EDT_SSTM_ZERO_OFFSET_MAX 100
00025 
00026 #define EDT_SSTM_MAX_ADJ  0xffffff
00027 
00028 #define USECS_FACTOR  (1000000.0 / (double) (1 << 20))
00029 
00030 
00086 EDTAPI void edt_sstm_setup(EdtDev *edt_p, char *bitfile);
00087 
00088 EDTAPI void edt_sstm_strobe(EdtDev *edt_p, unsigned int bits);
00089 
00090 /*
00091  * Latches the current time into registers
00092  */
00093 
00094 EDTAPI void edt_sstm_latch_time(EdtDev *edt_p);
00095 
00096 /*
00097  * returns the value of seconds
00098  */
00099 
00100 EDTAPI u_int edt_sstm_get_seconds(EdtDev *edt_p);
00101 
00102 /*
00103  * returns # of ticks ( = (1<<20)/1000000 microseconds
00104  */
00105 
00106 EDTAPI u_int edt_sstm_get_counts(EdtDev *edt_p);
00107 
00108 /*
00109  * returns # of usecs by multiplying by 
00110  * (1<<20)/1000000)
00111  *
00112  */
00113 
00114 EDTAPI u_int edt_sstm_get_usecs(EdtDev *edt_p);
00115 
00116 /*
00117  * gets both parts (secs/usecs) of time values 
00118  */
00119 
00120 EDTAPI void edt_sstm_get_time_parts(EdtDev *edt_p, u_int *seconds, u_int *usecs);
00121 
00122 /*
00123  * Returns time as double
00124  *
00125  */
00126 
00127 EDTAPI double edt_sstm_timestamp(EdtDev *edt_p);
00128 
00129 /*
00130  * returns 0 or 1 depending on 
00131  * EDT_SSTM_ADJ_enabled bit.
00132  */
00133 
00134 EDTAPI int edt_sstm_get_adjust_enabled(EdtDev *edt_p);
00135 
00136 /*
00137  * returns -1 or 1 depending on 
00138  * EDT_SSTM_ADJ_PLUS bit.
00139  */
00140 
00141 EDTAPI int edt_sstm_get_adjust_sign(EdtDev *edt_p);
00142 
00143 /*
00144  * returns the signed value of the adjustment
00145  */
00146 
00147 EDTAPI u_int edt_sstm_get_adjust_ticks(EdtDev *edt_p);
00148 
00149 
00150 /*
00151  * Set the current seconds value, clears usecs
00152  * Not in synch with system time - to do so use
00153  * edt_sstm_set instead, which waits for system
00154  * zero crossing.
00155  */
00156  
00157 EDTAPI void edt_sstm_set_secs(EdtDev *edt_p, unsigned int second);
00158 
00159 /*
00160  * Set the current seconds value to second + 1, clears usecs
00161  * Calls edt_wait_for_zero before strobing value
00162  */
00163  
00164 EDTAPI void edt_sstm_set(EdtDev *edt_p, unsigned int second);
00165 
00166 
00167 /*
00168  * Sets the time to the current system time, by waiting 
00169  * for zero crossing, then half a second, then calling 
00170  * edt_sstm_set
00171  */
00172 
00173 EDTAPI void edt_sstm_set_to_sys(EdtDev *edt_p);
00174 
00175 /*
00176  * Sets the time to the current system time, adding error ms
00177  */
00178 
00179 EDTAPI void edt_sstm_set_to_sys_error(EdtDev *edt_p, int error);
00180 
00181 /* 
00182 Turn off rate adjustment
00183 */
00184 
00185 EDTAPI void edt_sstm_disable_adjust(EdtDev *edt_p);
00186 
00187 /*
00188 Turn on rate adjustment
00189 */
00190 
00191 EDTAPI void edt_sstm_enable_adjust(EdtDev *edt_p);
00192 
00193 /* 
00194 Set the sign bit for rate adjustment 
00195 */
00196 
00197 EDTAPI void edt_sstm_set_adj_sign(EdtDev *edt_p, int positive);
00198 
00199 
00200 /* 
00201  * sets the time adjustment to ticks
00202  * between delta. Sign of ticks determines whether to add 1
00203  * or not add 1
00204  *
00205  */
00206 
00207 EDTAPI void edt_sstm_set_adj_ticks(EdtDev *edt_p, int ticks, int positive);
00208 
00209 /*
00210  * Return mean error between SS time and sys time
00211  * as a double (in seconds)
00212  */
00213 
00214 
00215 EDTAPI double edt_sstm_sys_error(EdtDev *edt_p);
00216 
00217 /*
00218  *
00219  * Calculate basic error rate between SS clock and sys clock
00220  * Take mean of <samples> over sample_seconds 
00221  * Returns drift as ppm
00222  *
00223  */ 
00224 
00225 EDTAPI double edt_sstm_measure_drift(EdtDev *edt_p);
00226 
00227 /* compute adjustment ticks from drift value in ppm */
00228 
00229 EDTAPI int edt_sstm_ticks_from_drift(double drift);
00230 
00231 /* Take a drift value in ppm and set adjustment ticks
00232     accordingly */
00233 
00234 EDTAPI void edt_sstm_set_adj_from_drift(EdtDev *edt_p, double drift);
00235 
00236 EDTAPI void edt_sstm_set_drift_sampling(int seconds, int samples);
00237 
00238 EDTAPI int edt_sstm_get_adj_sample_secs();
00239 
00240 EDTAPI int edt_sstm_get_adj_samples();
00241 
00242 /*
00243  * Calculate auto adjust values - sample for sample_seconds
00244  * to calculate basic error rate, then find total error and compute
00245  * adjustment to get to zero in revert_secs.
00246  * adjust_to returns the correction for basic error rate.
00247  *
00248  */
00249 
00250 EDTAPI double edt_sstm_calc_convergence(EdtDev *edt_p, 
00251                              int revert_secs,
00252                              double drift_ticks);
00253 
00254 /*
00255  * Given two drift values, one to converge to zero error and 
00256  * a constant drift, block until finished
00257  * stop value will be checked at 200 ms intervals to 
00258  * see whether to stop 
00259  */
00260 
00261 EDTAPI double edt_sstm_adjust(EdtDev *edt_p,
00262                               double converge, 
00263                               double drift,
00264                               int *active);
00265 /*
00266  * Calculate and execute auto adjust
00267  *
00268  */
00269 
00270 EDTAPI void edt_sstm_iterate_adjust(EdtDev *edt_p, 
00271                              int revert_secs,
00272                              int maxiterations,
00273                              int tolerance,
00274                              int *active);
00275 
00276 EDTAPI void
00277 edt_sstm_print_loop(EdtDev *edt_p, int interval, int total_time);
00278 
00279 
00280 /* 
00281  * Structure used by edt_sstm_launch_adjuster to
00282  * have a thread running in the background 
00283  * which periodically checks the clock error and does
00284  * correction. Set active to 0 to stop the thread.
00285  */
00286 
00287 
00288 typedef struct {
00289     EdtDev *edt_p;
00290     int check_interval; /* seconds */
00291     int revert_seconds;
00292     int iterations;    /* how many times to auto adjust */
00293     int max_error;
00294     int tolerance; /* desired closeness to zero error */
00295     int active;
00296     int done;
00297     int loops;
00298     thread_t thread;
00299 } EdtSSTimeAdjuster;
00300 
00301 /*
00302  * Start a thread to check and correct time against 
00303  * system time.
00304  * check - interval between checks
00305  * revert - how long a period for converging to 0 error
00306  * iterations - how many times to do the auto adjust if error exceeds
00307  *              max_usec_error.
00308  * 
00309 */
00310 
00311  
00312 EDTAPI EdtSSTimeAdjuster *
00313 edt_sstm_launch_adjuster(EdtDev *edt_p, 
00314                             int check, 
00315                             int revert, 
00316                             int iterations, 
00317                             int max_usec_error,
00318                             int tolerance,
00319                             int loops);
00320 
00321 
00322 EDTAPI void
00323 edt_sstm_adjuster_stop(EdtSSTimeAdjuster *adj_p);
00324 
00325 
00326 EDTAPI void
00327 edt_sstm_adjuster_start(EdtSSTimeAdjuster *adj_p);
00328 
00334 EDTAPI char * edt_printable_time(double sys_t, char *buf);
00335 
00336 #ifndef WIN32
00337 
00338 #define edt_sys_timestamp edt_timestamp
00339 
00340 #else
00341 
00342 EDTAPI double edt_sys_timestamp();
00343 
00344 #endif
00345 
00346 
00347 
00348 #ifdef __cplusplus
00349 }
00350 #endif
00351 
00352 
00353 
00354 #endif

Generated on Mon May 12 16:38:59 2008 by  doxygen 1.5.1