/* ts2config.c - Trigger Supervisor 2 configuration/status */ #include #include #include #include #include #include #include #include #include #include typedef struct{ unsigned short tir_csr; unsigned short tir_vec; unsigned short tir_dat; unsigned short tir_oport; unsigned short tir_iport; }vme_tir; int SetTimeOffset(unsigned long current_time); int PrintTimeOffset(); void Setv792Thresholds(int id, const char *filename); unsigned long time_offset; int PULSER_ON = 0; unsigned int PULSER_BITS = 0xf; unsigned long PULSER_DELAY = 1000; /* in microseconds */ /*---------------- /* SetTimeOffset /*----------------*/ int SetTimeOffset(unsigned long current_time) { /* Since the system clock is reset on re-boot, we have to */ /* add an offset to the time returned by time(NULL) if we */ /* want the correct time to show up in the EventID bank */ /* This routine should be called with the current time in */ /* seconds as returned by time(NULL) on a system with the */ /* correct system clock. */ time_offset = current_time - time(NULL); PrintTimeOffset(); return 0; } /*---------------- /* PrintTimeOffset /*----------------*/ int PrintTimeOffset() { printf("time_offset set to %d\n",time_offset); return 0; } /*------------------ /* Setv792Thresholds /*------------------*/ void Setv792Thresholds(int id, const char *filename) { FILE *f; int thresh[32]; int i; /* Open file */ f = fopen(filename,"r"); if(!f){ printf("%s:%d error opening file \"%s\"\n",__FILE__,__LINE__,filename); } printf("Reading sparsification thresholds for module %d \n",id); printf(" from \"%s\"\n", filename); /* Read in values. There should be exactly 32 */ for(i=0; i<32; i++){ int channel, n; n=fscanf(f,"%d %d", &channel, &thresh[i]); if(channel != i || n<2){ printf("%s:%d Bad channel number or format in \"%s\" line %d (should be %d)\n",__FILE__,__LINE__,filename,i,i); } printf("channel %d thresh %d\n",channel, thresh[i]); } fclose(f); if(i==32){ /* Found 32 values. Program them in */ int avg = 0; for(i=0; i<32; i++){ c792SetThresh(id, i, thresh[i]); avg += thresh[i]; } c792Sparse(id, 0, 1); printf("Average threshold: %3.1f\n\n",(float)avg/32.0); }else{ printf("%s:%d Problem with format of \"%s\". Thresholds unchanged\n", __FILE__,__LINE__,filename); } } /*------------------ /* StartTIRPulser /*------------------*/ void StartTIRPulser(unsigned int bits, unsigned long sleep_delay_us) { /* Start an infinite loop where the bit pattern specified */ /* in "bits" is strobed on the TIR board's output register */ /* with a delay of "sleep_delay_us" microseconds between */ /* each pulse. This should be spawned as a separate task. */ /* e.g. /* sp StartTIRPulser(0x80, 1000) /* /* which starts pulsing the 3rd output bit at about 1kHz */ float freq; vme_tir *tir=NULL; int stat = 0; unsigned long laddr; struct timespec ts; unsigned long last_delay; /* Initialize globals with parameters passed as arguments */ PULSER_DELAY = sleep_delay_us; PULSER_BITS = bits; last_delay = PULSER_DELAY+1; /* guarantee timespec is filled below */ /* Get TIR address */ stat = sysBusToLocalAdrs(0x29,0x0ed0,&laddr); if (stat != 0) { printf("StartTIRPulser: ERROR: Error in sysBusToLocalAdrs res=%d \n",stat); } tir = (vme_tir*)laddr; /* Print informative message and start loop */ freq = 1.0/((float)sleep_delay_us*1.0E-6); PULSER_ON = 1; printf("TIR pulsar started at %4.0f Hz\n",freq); while(PULSER_ON){ unsigned int outval = tir->tir_oport; tir->tir_oport = ((PULSER_BITS)|outval); tir->tir_oport = ((~PULSER_BITS)&outval); if(PULSER_DELAY != last_delay){ /* Convert delay into seconds and nanoseconds */ unsigned long delay = PULSER_DELAY; ts.tv_sec = delay/1000000; delay -= ts.tv_sec*1000000; ts.tv_nsec = delay*1000; last_delay = PULSER_DELAY; } nanosleep(&ts, NULL); } printf("TIR pulser stopped.\n"); } /*------------------ /* SetTIRPulser /*------------------*/ void SetTIRPulser(unsigned int bits, unsigned long sleep_delay_us) { PULSER_BITS = bits; PULSER_DELAY = sleep_delay_us; } /*------------------ /* StopTIRPulser /*------------------*/ void StopTIRPulser(void) { PULSER_ON = 0; }