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

Generated on 19 Jun 2015 by  doxygen 1.4.7