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 
00022 typedef struct _timeregs {
00023     u_int counts;
00024     u_int secs;
00025     u_int set;
00026     u_int adj_value;
00027     u_int cmd;
00028 
00029     EdtDev *dev_p;
00030 
00031     /* used to be SS_TimeAdjust */
00032     int check_interval; /* seconds */
00033     int adjust_sample;
00034     int adjustment_scalar;    /* how many times to auto adjust */
00035     int max_error;
00036     int tolerance; /* desired closeness to zero error */
00037     int active;
00038     int done;
00039     int loops;
00040     thread_t thread;
00041 
00042     double err;
00043     int sign;
00044     int loop;
00045     double converge;
00046     double drift;
00047     double adjust_drift;
00048     int iter;
00049 
00050     int ticks;
00051 
00052 } EdtTimeController;
00053 
00054 #define EDT_SSTM_COPY       0x01
00055 #define EDT_SSTM_LATCH      0x02
00056 #define EDT_SSTM_COPY_ADJ         0x04
00057 
00058 #define EDT_SSTM_ADJ_EN   0x10
00059 #define EDT_SSTM_ADJ_PLUS   0x20
00060 #define EDT_SSTM_CMD_LOCK   0x80
00061 
00062 #define EDT_SSTM_ZERO_OFFSET_MAX 100
00063 
00064 #define EDT_SSTM_MAX_ADJ  0xffffff
00065 
00066 #define USECS_FACTOR  (1000000.0 / (double) (1 << 20))
00067 
00068 
00124 EDTAPI void edt_sstm_setup(EdtTimeController *tm, char *bitfile);
00125 
00126 EDTAPI void edt_sstm_strobe(EdtTimeController *tm, unsigned int bits);
00127 
00128 /*
00129  * Latches the current time into registers
00130  */
00131 
00132 EDTAPI void edt_sstm_latch_time(EdtTimeController *tm);
00133 
00134 /*
00135  * returns the value of seconds
00136  */
00137 
00138 EDTAPI u_int edt_sstm_get_seconds(EdtTimeController *tm);
00139 
00140 /*
00141  * returns # of ticks ( = (1<<20)/1000000 microseconds
00142  */
00143 
00144 EDTAPI u_int edt_sstm_get_counts(EdtTimeController *tm);
00145 
00146 /*
00147  * returns # of usecs by multiplying by 
00148  * (1<<20)/1000000)
00149  *
00150  */
00151 
00152 EDTAPI u_int edt_sstm_get_usecs(EdtTimeController *tm);
00153 
00154 /*
00155  * gets both parts (secs/usecs) of time values 
00156  */
00157 
00158 EDTAPI void edt_sstm_get_time_parts(EdtTimeController *tm, u_int *seconds, u_int *usecs);
00159 
00160 /*
00161  * Returns time as double
00162  *
00163  */
00164 
00165 EDTAPI double edt_sstm_timestamp(EdtTimeController *tm);
00166 
00167 /*
00168  * returns 0 or 1 depending on 
00169  * EDT_SSTM_ADJ_enabled bit.
00170  */
00171 
00172 EDTAPI int edt_sstm_get_adjust_enabled(EdtTimeController *tm);
00173 
00174 /*
00175  * returns -1 or 1 depending on 
00176  * EDT_SSTM_ADJ_PLUS bit.
00177  */
00178 
00179 EDTAPI int edt_sstm_get_adjust_sign(EdtTimeController *tm);
00180 
00181 /*
00182  * returns the signed value of the adjustment
00183  */
00184 
00185 EDTAPI u_int edt_sstm_get_adjust_ticks(EdtTimeController *tm);
00186 
00187 
00188 /*
00189  * Set the current seconds value, clears usecs
00190  * Not in synch with system time - to do so use
00191  * edt_sstm_set instead, which waits for system
00192  * zero crossing.
00193  */
00194  
00195 EDTAPI void edt_sstm_set_secs(EdtTimeController *tm, unsigned int second);
00196 
00197 /*
00198  * Set the current seconds value to second + 1, clears usecs
00199  * Calls edt_wait_for_zero before strobing value
00200  */
00201  
00202 EDTAPI void edt_sstm_set(EdtTimeController *tm, unsigned int second);
00203 
00204 
00205 /*
00206  * Sets the time to the current system time, by waiting 
00207  * for zero crossing, then half a second, then calling 
00208  * edt_sstm_set
00209  */
00210 
00211 EDTAPI void edt_sstm_set_to_sys(EdtTimeController *tm);
00212 
00213 /*
00214  * Sets the time to the current system time, adding error ms
00215  */
00216 
00217 EDTAPI void edt_sstm_set_to_sys_error(EdtTimeController *tm, int error);
00218 
00219 /* 
00220 Turn off rate adjustment
00221 */
00222 
00223 EDTAPI void edt_sstm_disable_adjust(EdtTimeController *tm);
00224 
00225 /*
00226 Turn on rate adjustment
00227 */
00228 
00229 EDTAPI void edt_sstm_enable_adjust(EdtTimeController *tm);
00230 
00231 /* 
00232 Set the sign bit for rate adjustment 
00233 */
00234 
00235 EDTAPI void edt_sstm_set_adj_sign(EdtTimeController *tm, int positive);
00236 
00237 
00238 /* 
00239  * sets the time adjustment to ticks
00240  * between delta. Sign of ticks determines whether to add 1
00241  * or not add 1
00242  *
00243  */
00244 
00245 EDTAPI void edt_sstm_set_adj_ticks(EdtTimeController *tm, int ticks, int positive);
00246 
00247 /*
00248  * Return mean error between SS time and sys time
00249  * as a double (in seconds)
00250  */
00251 
00252 
00253 EDTAPI double edt_sstm_sys_error(EdtTimeController *tm);
00254 
00255 /*
00256  *
00257  * Calculate basic error rate between SS clock and sys clock
00258  * Take mean of <samples> over sample_seconds 
00259  * Returns drift as ppm
00260  *
00261  */ 
00262 
00263 EDTAPI double edt_sstm_measure_drift(EdtTimeController *tm);
00264 
00265 /* compute adjustment ticks from drift value in ppm */
00266 
00267 EDTAPI int edt_sstm_ticks_from_drift(double drift);
00268 
00269 /* Take a drift value in ppm and set adjustment ticks
00270     accordingly */
00271 
00272 EDTAPI void edt_sstm_set_adj_from_drift(EdtTimeController *tm, double drift);
00273 
00274 EDTAPI void edt_sstm_set_drift_sampling(int seconds, int samples);
00275 
00276 EDTAPI int edt_sstm_get_adj_sample_secs();
00277 
00278 EDTAPI int edt_sstm_get_adj_samples();
00279 
00280 /*
00281  * Calculate auto adjust values - sample for sample_seconds
00282  * to calculate basic error rate, then find total error and compute
00283  * adjustment to get to zero in revert_secs.
00284  * adjust_to returns the correction for basic error rate.
00285  *
00286  */
00287 
00288 EDTAPI double edt_sstm_calc_convergence(EdtTimeController *tm);
00289 
00290 /*
00291  * Given two drift values, one to converge to zero error and 
00292  * a constant drift, block until finished
00293  * stop value will be checked at 200 ms intervals to 
00294  * see whether to stop 
00295  */
00296 
00297 EDTAPI double edt_sstm_adjust(EdtTimeController *tm);
00298 /*
00299  * Calculate and execute auto adjust
00300  *
00301  */
00302 
00303 EDTAPI void edt_sstm_iterate_adjust(EdtTimeController *tm);
00304 
00305 EDTAPI void
00306 edt_sstm_print_loop(EdtTimeController *tm, int interval, int total_time);
00307 
00308 /*
00309  * Start a thread to check and correct time against 
00310  * system time.
00311  * check - interval between checks
00312  * revert - how long a period for converging to 0 error
00313  * adjustment_scalar - how many times to do the auto adjust if error exceeds
00314  *              max_usec_error.
00315  * 
00316 */
00317 
00318  
00319 EDTAPI EdtTimeController *
00320 edt_sstm_launch_adjuster(EdtTimeController * tm);
00321 
00322 
00323 EDTAPI void
00324 edt_sstm_adjuster_stop(EdtTimeController * tm);
00325 
00326 
00327 EDTAPI void
00328 edt_sstm_adjuster_start(EdtTimeController * tm);
00329 
00335 EDTAPI char * edt_printable_time(double sys_t, char *buf);
00336 
00337 EDTAPI void edt_init_time_controller(EdtTimeController *tm,
00338                                      EdtDev *edt_p,
00339                                      u_int address_base);
00340 
00341 
00342 #ifndef WIN32
00343 
00344 #define edt_sys_timestamp edt_timestamp
00345 
00346 #else
00347 
00348 EDTAPI double edt_sys_timestamp();
00349 
00350 #endif
00351 
00352 
00353 
00354 #ifdef __cplusplus
00355 }
00356 #endif
00357 
00358 
00359 
00360 #endif

Generated on Mon Mar 21 14:14:56 2011 by  doxygen 1.4.7