diff --git a/slsDetectorSoftware/README b/slsDetectorSoftware/README index 83e019581..2cbe78380 100644 --- a/slsDetectorSoftware/README +++ b/slsDetectorSoftware/README @@ -1,4 +1,4 @@ -This subversion repository contains +This subversion repository contains Common directories Please inform Anna, Beat and Ian if you make modifications to these directories @@ -18,4 +18,4 @@ Please inform Beat and Ian if you make modifications to these directories - eigerDetector : eiger specific funcs (mainly files I/O and ..) - eigerDetectorServer : eiger server -make doc \ No newline at end of file +make doc diff --git a/slsDetectorSoftware/commonFiles/communication_funcs.c b/slsDetectorSoftware/commonFiles/communication_funcs.c index 0c13acd4c..a42371570 100755 --- a/slsDetectorSoftware/commonFiles/communication_funcs.c +++ b/slsDetectorSoftware/commonFiles/communication_funcs.c @@ -403,7 +403,13 @@ int sendChip(int file_des, sls_detector_chip *myChip) { return ts; } + int sendModule(int file_des, sls_detector_module *myMod) { + return sendModuleGeneral(file_des, myMod, 1); +} + + +int sendModuleGeneral(int file_des, sls_detector_module *myMod, int sendAll) { int ts=0; #ifdef VERBOSE int idac; @@ -422,8 +428,11 @@ int sendModule(int file_des, sls_detector_module *myMod) { ts+=sendData(file_des,&(myMod->reg),sizeof(myMod->reg),INT32); ts+=sendData(file_des,myMod->dacs,sizeof(myMod->ndac),OTHER); ts+=sendData(file_des,myMod->adcs,sizeof(myMod->nadc),OTHER); - ts+=sendData(file_des,myMod->chipregs,sizeof(myMod->nchip),OTHER); - ts+=sendData(file_des,myMod->chanregs,sizeof(myMod->nchan),OTHER); + /*some detectors dont require sending all trimbits etc.*/ + if(sendAll){ + ts+=sendData(file_des,myMod->chipregs,sizeof(myMod->nchip),OTHER); + ts+=sendData(file_des,myMod->chanregs,sizeof(myMod->nchan),OTHER); + } ts+=sendData(file_des,&(myMod->gain), sizeof(myMod->gain),OTHER); ts+=sendData(file_des,&(myMod->offset), sizeof(myMod->offset),OTHER); @@ -440,14 +449,19 @@ int sendModule(int file_des, sls_detector_module *myMod) { #ifdef VERBOSE printf("adcs %d of size %d sent\n",myMod->module, ts); #endif - ts+=sendData(file_des,myMod->chipregs,sizeof(int)*nChips,INT32); + + /*some detectors dont require sending all trimbits etc.*/ + if(sendAll){ + ts+=sendData(file_des,myMod->chipregs,sizeof(int)*nChips,INT32); #ifdef VERBOSE - printf("chips %d of size %d sent\n",myMod->module, ts); + printf("chips %d of size %d sent\n",myMod->module, ts); #endif - ts+=sendData(file_des,myMod->chanregs,sizeof(int)*nChans,INT32); + ts+=sendData(file_des,myMod->chanregs,sizeof(int)*nChans,INT32); #ifdef VERBOSE - printf("chans %d of size %d sent - %d\n",myMod->module, ts, myMod->nchan); + printf("chans %d of size %d sent - %d\n",myMod->module, ts, myMod->nchan); #endif + } + #ifdef VERBOSE printf("module %d of size %d sent register %x\n",myMod->module, ts, myMod->reg); #endif @@ -507,13 +521,16 @@ int receiveChip(int file_des, sls_detector_chip* myChip) { return ts; } -int receiveModule(int file_des, sls_detector_module* myMod) { - +int receiveModule(int file_des, sls_detector_module* myMod) { + return receiveModuleGeneral(file_des,myMod,1); +} + +int receiveModuleGeneral(int file_des, sls_detector_module* myMod, int receiveAll) { + int ts=0; dacs_t *dacptr=myMod->dacs; dacs_t *adcptr=myMod->adcs; int *chipptr=myMod->chipregs, *chanptr=myMod->chanregs; - int ts=0; int nChips, nchipold=myMod->nchip, nchipdiff; int nChans, nchanold=myMod->nchan, nchandiff; int nDacs, ndold=myMod->ndac, ndacdiff; @@ -531,8 +548,11 @@ int receiveModule(int file_des, sls_detector_module* myMod) { ts+=receiveData(file_des,&(myMod->reg),sizeof(myMod->reg),INT32); ts+=receiveData(file_des,myMod->dacs,sizeof(myMod->ndac),INT32); ts+=receiveData(file_des,myMod->adcs,sizeof(myMod->nadc),INT32); - ts+=receiveData(file_des,myMod->chipregs,sizeof(myMod->nchip),INT32); - ts+=receiveData(file_des,myMod->chanregs,sizeof(myMod->nchan),INT32); + /*some detectors dont require sending all trimbits etc.*/ + if(receiveAll){ + ts+=receiveData(file_des,myMod->chipregs,sizeof(myMod->nchip),INT32); + ts+=receiveData(file_des,myMod->chanregs,sizeof(myMod->nchan),INT32); + } ts+=receiveData(file_des,&(myMod->gain), sizeof(myMod->gain),OTHER); ts+=receiveData(file_des,&(myMod->offset), sizeof(myMod->offset),OTHER); @@ -540,7 +560,7 @@ int receiveModule(int file_des, sls_detector_module* myMod) { myMod->adcs=adcptr; myMod->chipregs=chipptr; myMod->chanregs=chanptr; - + nChips=myMod->nchip; nchipdiff=nChips-nchipold; if (nchipold!=nChips) { @@ -585,6 +605,7 @@ int receiveModule(int file_des, sls_detector_module* myMod) { ts+=receiveData(file_des,myMod->dacs, sizeof(dacs_t)*nDacs,INT32); #ifdef VERBOSE printf("dacs received\n"); + int id; for (id=0; iddacs[id]); @@ -601,7 +622,7 @@ int receiveModule(int file_des, sls_detector_module* myMod) { if (nadcdiff<=0) { ts+=receiveData(file_des,myMod->adcs, sizeof(dacs_t)*nAdcs,INT32); -#ifdef VERBOSE +#ifdef VERBOSE printf("adcs received\n"); #endif } else { @@ -613,35 +634,41 @@ int receiveModule(int file_des, sls_detector_module* myMod) { return FAIL; } - if (nchipdiff<=0) { - ts+=receiveData(file_des,myMod->chipregs, sizeof(int)*nChips,INT32); -#ifdef VERBOSE - printf("chips received\n"); -#endif - } else { - chipptr=(int*)malloc(nchipdiff*sizeof(int)); - myMod->nchip=nchipold; - ts+=receiveData(file_des,myMod->chipregs, sizeof(int)*nchipold,INT32); - ts+=receiveData(file_des,chipptr, sizeof(int)*nchipdiff,INT32); - free(chipptr); - return FAIL; - } - if (nchandiff<=0) { - ts+=receiveData(file_des,myMod->chanregs, sizeof(int)*nChans,INT32); -#ifdef VERBOSE - printf("chans received\n"); + /*some detectors dont require sending all trimbits etc.*/ + if(receiveAll){ + + if (nchipdiff<=0) { + ts+=receiveData(file_des,myMod->chipregs, sizeof(int)*nChips,INT32); +#ifdef VERBOSE + printf("chips received\n"); #endif - } else { - chanptr=(int*)malloc(nchandiff*sizeof(int)); - myMod->nchan=nchanold; - ts+=receiveData(file_des,myMod->chanregs, sizeof(int)*nchanold,INT32); - ts+=receiveData(file_des,chanptr, sizeof(int)*nchandiff,INT32); - free(chanptr); - return FAIL; + } else { + chipptr=(int*)malloc(nchipdiff*sizeof(int)); + myMod->nchip=nchipold; + ts+=receiveData(file_des,myMod->chipregs, sizeof(int)*nchipold,INT32); + ts+=receiveData(file_des,chipptr, sizeof(int)*nchipdiff,INT32); + free(chipptr); + return FAIL; + } + + if (nchandiff<=0) { + ts+=receiveData(file_des,myMod->chanregs, sizeof(int)*nChans,INT32); +#ifdef VERBOSE + printf("chans received\n"); +#endif + } else { + chanptr=(int*)malloc(nchandiff*sizeof(int)); + myMod->nchan=nchanold; + ts+=receiveData(file_des,myMod->chanregs, sizeof(int)*nchanold,INT32); + ts+=receiveData(file_des,chanptr, sizeof(int)*nchandiff,INT32); + free(chanptr); + return FAIL; + } } #ifdef VERBOSE printf("received module %d of size %d register %x\n",myMod->module,ts,myMod->reg); #endif + return ts; } diff --git a/slsDetectorSoftware/commonFiles/communication_funcs.h b/slsDetectorSoftware/commonFiles/communication_funcs.h index 7ed8bf28b..e4e3fac27 100755 --- a/slsDetectorSoftware/commonFiles/communication_funcs.h +++ b/slsDetectorSoftware/commonFiles/communication_funcs.h @@ -42,9 +42,10 @@ int getServerError(int socketDescriptor); int sendChannel(int file_des, sls_detector_channel *myChan); int sendChip(int file_des, sls_detector_chip *myChip); int sendModule(int file_des, sls_detector_module *myMod); +int sendModuleGeneral(int file_des, sls_detector_module *myMod, int sendAll); int receiveChannel(int file_des, sls_detector_channel *myChan); int receiveChip(int file_des, sls_detector_chip* myChip); -int receiveModule(int file_des, sls_detector_module* myMod); - +int receiveModule(int file_des, sls_detector_module* myMod); +int receiveModuleGeneral(int file_des, sls_detector_module* myMod, int receiveAll); #endif diff --git a/slsDetectorSoftware/commonFiles/error_defs.h b/slsDetectorSoftware/commonFiles/error_defs.h index c3509a44a..2ad16c404 100644 --- a/slsDetectorSoftware/commonFiles/error_defs.h +++ b/slsDetectorSoftware/commonFiles/error_defs.h @@ -56,6 +56,8 @@ using namespace std; #define COULD_NOT_PULSE_PIXEL 0x0000000000040000ULL #define COULD_NOT_PULSE_PIXEL_NMOVE 0x0000000000080000ULL #define COULD_NOT_PULSE_CHIP 0x0000000000100000ULL +#define COULD_NOT_SET_RATE_CORRECTION 0x0000000000200000ULL +#define RATE_CORRECTION_TAU_SUBEXPOSURE 0x0000000000400000ULL // 0x00000000FFFFFFFFULL /** @short class returning all error messages for error mask */ @@ -177,6 +179,16 @@ public: if(slsErrorMask&COULD_NOT_PULSE_CHIP) retval.append("Could not pulse chip\n"); + if(slsErrorMask&COULD_NOT_SET_RATE_CORRECTION) + retval.append("Could not set rate correction\n"); + + if(slsErrorMask&RATE_CORRECTION_TAU_SUBEXPOSURE) + retval.append("Rate Correction Deactivated: (tau/subexptime) must be less than 0.0015\n"); + + + //------------------------------------------------------ length of message + + return retval; } diff --git a/slsDetectorSoftware/commonFiles/sls_detector_defs.h b/slsDetectorSoftware/commonFiles/sls_detector_defs.h index 5dc57e871..4d55f5b6b 100755 --- a/slsDetectorSoftware/commonFiles/sls_detector_defs.h +++ b/slsDetectorSoftware/commonFiles/sls_detector_defs.h @@ -22,7 +22,7 @@ #define HEADERLENGTH 12 #define DEFAULT_SUBFRAME_EXPOSURE_VAL 2621440 /** default value for sub frame value 2.6ms*/ -#define MAX_SUBFRAME_EXPOSURE_VAL 0x1FFFFFFF /** 29 bit register for max subframe exposure value */ +#define MAX_SUBFRAME_EXPOSURE_VAL_IN_10NS 0x1FFFFFFF /** 29 bit register for max subframe exposure value */ /** maximum rois */ #define MAX_ROIS 100 @@ -348,7 +348,8 @@ enum dacIndex { E_Vcn, /**< eiger */ E_Vis, /**< eiger */ IO_DELAY, /**< eiger io delay */ - ADC_VPP /**< adc vpp for jctb */ + ADC_VPP, /**< adc vpp for jctb */ + HV_NEW /**< new hv index for jungfrau & c */ }; /** @@ -364,6 +365,11 @@ enum detectorSettings{ MEDIUMGAIN, /**< medium gain settings */ VERYHIGHGAIN, /**< very high gain settings */ LOWNOISE, /**< low noise settings */ + DYNAMICHG0, /**< dynamic high gain 0 */ + FIXGAIN1, /**< fix gain 1 */ + FIXGAIN2, /**< fix gain 2 */ + FORCESWITCHG1, /**< force switch gain 1 */ + FORCESWITCHG2, /**< force switch gain 2 */ UNDEFINED, /**< undefined or custom settings */ UNINITIALIZED /**< uninitialiazed (status at startup) */ }; diff --git a/slsDetectorSoftware/commonFiles/sls_detector_funcs.h b/slsDetectorSoftware/commonFiles/sls_detector_funcs.h index e61b5cf9c..8c4612339 100644 --- a/slsDetectorSoftware/commonFiles/sls_detector_funcs.h +++ b/slsDetectorSoftware/commonFiles/sls_detector_funcs.h @@ -98,7 +98,9 @@ enum { F_SET_COUNTER_BIT, /** < set/reset counter bit in detector for eiger */ F_PULSE_PIXEL, /** < pulse pixel n number of times in eiger at (x,y) */ F_PULSE_PIXEL_AND_MOVE, /** < pulse pixel n number of times and move relatively by x and y */ - F_PULSE_CHIP /** < pulse chip n number of times */ + F_PULSE_CHIP, /** < pulse chip n number of times */ + + F_SET_RATE_CORRECT /** < set/reset rate correction tau */ /* Always append functions hereafter!!! */ diff --git a/slsDetectorSoftware/eigerDetectorServer/FebControl.c b/slsDetectorSoftware/eigerDetectorServer/FebControl.c index b4f505486..54ea06559 100644 --- a/slsDetectorSoftware/eigerDetectorServer/FebControl.c +++ b/slsDetectorSoftware/eigerDetectorServer/FebControl.c @@ -43,11 +43,15 @@ unsigned int Feb_Control_triggerMode; //internal timer, external start, unsigned int Feb_Control_externalEnableMode; //external enabling engaged and it's polarity unsigned int Feb_Control_subFrameMode; + unsigned int Feb_Control_nimages; double Feb_Control_exposure_time_in_sec; int Feb_Control_subframe_exposure_time_in_10nsec; double Feb_Control_exposure_period_in_sec; +int64_t Feb_Control_RateTable_Tau_in_nsec = -1; +int64_t Feb_Control_RateTable_Subexptime_in_nsec = -1; + unsigned int Feb_Control_trimbit_size; unsigned int* Feb_Control_last_downloaded_trimbits; @@ -55,7 +59,11 @@ unsigned int* Feb_Control_last_downloaded_trimbits; int Feb_Control_module_number; int Feb_Control_current_index; -int counter_bit = 1; +int Feb_Control_counter_bit = 1; +int Feb_control_master = 0; + +unsigned int Feb_Control_rate_correction_table[1024]; +double Feb_Control_rate_meas[16384]; void Module_Module(struct Module* mod,unsigned int number, unsigned int address_top){ @@ -130,17 +138,17 @@ unsigned int Module_GetBottomLeftAddress(struct Module* mod) {return mod->botto unsigned int Module_GetBottomRightAddress(struct Module* mod) {return mod->bottom_right_address;} unsigned int Module_SetTopIDelay(struct Module* mod,unsigned int chip,unsigned int value) { return Module_TopAddressIsValid(mod) &&chip<4 ? (mod->idelay_top[chip]=value) : 0;} //chip 0=ll,1=lr,0=rl,1=rr -unsigned int Module_GetTopIDelay(struct Module* mod,unsigned int chip) { return chip<4 ? mod->idelay_top[chip] : 0;} //chip 0=ll,1=lr,0=rl,1=rr +unsigned int Module_GetTopIDelay(struct Module* mod,unsigned int chip) { return chip<4 ? mod->idelay_top[chip] : 0;} //chip 0=ll,1=lr,0=rl,1=rr unsigned int Module_SetBottomIDelay(struct Module* mod,unsigned int chip,unsigned int value) { return Module_BottomAddressIsValid(mod) &&chip<4 ? (mod->idelay_bottom[chip]=value) : 0;} //chip 0=ll,1=lr,0=rl,1=rr -unsigned int Module_GetBottomIDelay(struct Module* mod,unsigned int chip) { return chip<4 ? mod->idelay_bottom[chip] : 0;} //chip 0=ll,1=lr,0=rl,1=rr +unsigned int Module_GetBottomIDelay(struct Module* mod,unsigned int chip) { return chip<4 ? mod->idelay_bottom[chip] : 0;} //chip 0=ll,1=lr,0=rl,1=rr -float Module_SetHighVoltage(struct Module* mod,float value) { return Module_TopAddressIsValid(mod) ? (mod->high_voltage=value) : -1;} -float Module_GetHighVoltage(struct Module* mod) { return mod->high_voltage;} +float Module_SetHighVoltage(struct Module* mod,float value) { return Feb_control_master ? (mod->high_voltage=value) : -1;}// Module_TopAddressIsValid(mod) ? (mod->high_voltage=value) : -1;} +float Module_GetHighVoltage(struct Module* mod) { return mod->high_voltage;} -int Module_SetTopDACValue(struct Module* mod,unsigned int i, int value) { return (itop_dac[i]=value) : -1;} -int Module_GetTopDACValue(struct Module* mod,unsigned int i) { return (itop_dac[i]:-1;} -int Module_SetBottomDACValue(struct Module* mod,unsigned int i, int value) { return (ibottom_dac[i]=value) : -1;} -int Module_GetBottomDACValue(struct Module* mod,unsigned int i) { return (ibottom_dac[i]:-1;} +int Module_SetTopDACValue(struct Module* mod,unsigned int i, int value) { return (itop_dac[i]=value) : -1;} +int Module_GetTopDACValue(struct Module* mod,unsigned int i) { return (itop_dac[i] : -1;} +int Module_SetBottomDACValue(struct Module* mod,unsigned int i, int value) { return (ibottom_dac[i]=value): -1;} +int Module_GetBottomDACValue(struct Module* mod,unsigned int i) { return (ibottom_dac[i] : -1;} @@ -166,27 +174,16 @@ void Feb_Control_FebControl(){ -int Feb_Control_Init(int master, int top){ +int Feb_Control_Init(int master, int top, int module_num){ unsigned int i; Feb_Control_module_number = 0; Feb_Control_current_index = 0; + Feb_control_master = master; //global send Feb_Control_AddModule1(0,1,0xff,0,1); Feb_Control_PrintModuleList(); - - //get module nummber - int res=0; - char hostname[100]; - if (gethostname(hostname, sizeof hostname) == 0) - puts(hostname); - else - perror("gethostname"); - char *pch; - pch = strtok(hostname,"0"); - pch = strtok(NULL,"0"); - sscanf(pch,"%d",&res); - Feb_Control_module_number = (res & 0xFF); + Feb_Control_module_number = (module_num & 0xFF); int serial = !top; @@ -538,7 +535,7 @@ int Feb_Control_ReadSetUpFile(unsigned int module_num, char* file_name){ } -int Feb_Control_CheckSetup(){ +int Feb_Control_CheckSetup(int master){ printf("Checking Set up\n"); unsigned int i,j; int ok = 1; @@ -556,7 +553,7 @@ int Feb_Control_CheckSetup(){ ok=0; } } - if(Module_GetHighVoltage(&modules[i])<0){ + if((Feb_control_master) &&(Module_GetHighVoltage(&modules[i])<0)){ cprintf(RED,"Warning: module %d's high voltage not set.\n",Module_GetModuleNumber(&modules[i])); ok=0; } @@ -709,7 +706,7 @@ int Feb_Control_SetHighVoltage1(unsigned int module_num,float value){ unsigned int module_index=0; unsigned int i; - if(Module_TopAddressIsValid(&modules[module_index])){ + if(Feb_control_master){//if(Module_TopAddressIsValid(&modules[module_index])){ if(!Feb_Control_GetModuleIndex(module_num,&module_index)){/*||!Module_TopAddressIsValid(&modules[module_index])){*/ cprintf(RED,"Error could not set high voltage module number %d invalid.\n",module_num); return 0; @@ -962,7 +959,9 @@ int Feb_Control_SetTrimbits(unsigned int module_num, unsigned int *trimbits){ for(l_r=0;l_r<2;l_r++){ // l_r loop //printf("\nl_r:%d\t\t",l_r); unsigned int disable_chip_mask = l_r ? DAQ_CS_BAR_LEFT : DAQ_CS_BAR_RIGHT; - if(!(Feb_Interface_WriteRegister(0xfff,DAQ_REG_STATIC_BITS,disable_chip_mask|DAQ_STATIC_BIT_PROGRAM|DAQ_STATIC_BIT_M8,0,0)&&Feb_Control_SetCommandRegister(DAQ_SET_STATIC_BIT)&&Feb_Control_StartDAQOnlyNWaitForFinish(5000))){ + if(!(Feb_Interface_WriteRegister(0xfff,DAQ_REG_STATIC_BITS,disable_chip_mask|DAQ_STATIC_BIT_PROGRAM|DAQ_STATIC_BIT_M8,0,0) + &&Feb_Control_SetCommandRegister(DAQ_SET_STATIC_BIT) + &&Feb_Control_StartDAQOnlyNWaitForFinish(5000))){ printf("Could not select chips\n"); return 0; } @@ -1187,7 +1186,7 @@ int Feb_Control_WaitForStartedFlag(int sleep_time_us, int prev_flag){ } -int Feb_Control_Reset(){ +int Feb_Control_Reset(){printf("Reset daq\n"); if(!Feb_Interface_WriteRegister(Feb_Control_AddressToAll(),DAQ_REG_CTRL,0,0,0) || !Feb_Interface_WriteRegister(Feb_Control_AddressToAll(),DAQ_REG_CTRL,DAQ_CTRL_RESET,0,0) || !Feb_Interface_WriteRegister(Feb_Control_AddressToAll(),DAQ_REG_CTRL,0,0,0)){ cprintf(RED,"Warning: Could not reset daq, no response.\n"); return 0; @@ -1378,7 +1377,7 @@ int Feb_Control_SetSubFrameExposureTime(int the_subframe_exposure_time_in_10nsec printf("Sub Frame Exposure time set to: %d\n",Feb_Control_subframe_exposure_time_in_10nsec); return 1; } -int Feb_Control_GetSubFrameExposureTime(){return Feb_Control_subframe_exposure_time_in_10nsec;} +int Feb_Control_GetSubFrameExposureTime(){return Feb_Control_subframe_exposure_time_in_10nsec*10;} int Feb_Control_SetExposurePeriod(double the_exposure_period_in_sec){ Feb_Control_exposure_period_in_sec = the_exposure_period_in_sec; @@ -1545,7 +1544,7 @@ int Feb_Control_PrepareForAcquisition(){//return 1; } int ret=0; - if(counter_bit) + if(Feb_Control_counter_bit) ret = Feb_Control_ResetChipCompletely(); else ret = Feb_Control_ResetChipPartially(); @@ -1648,11 +1647,11 @@ int Feb_Control_SaveAllTrimbitsTo(int value){ void Feb_Control_Set_Counter_Bit(int value){ - counter_bit = value; + Feb_Control_counter_bit = value; } int Feb_Control_Get_Counter_Bit(){ - return counter_bit; + return Feb_Control_counter_bit; } int Feb_Control_Pulse_Pixel(int npulses, int x, int y){ @@ -1777,8 +1776,8 @@ int Feb_Control_PulseChip(int npulses){ cprintf(RED,"some wait error\n"); } Feb_Control_SetExternalEnableMode(on,1); - counter_bit = (on?0:1); - printf("counter_bit:%d\n",counter_bit); + Feb_Control_counter_bit = (on?0:1); + printf("Feb_Control_counter_bit:%d\n",Feb_Control_counter_bit); if(on) printf("Pulse chip success\n\n"); @@ -1789,10 +1788,174 @@ int Feb_Control_PulseChip(int npulses){ +int64_t Feb_Control_Get_RateTable_Tau_in_nsec(){ return Feb_Control_RateTable_Tau_in_nsec;} +int64_t Feb_Control_Get_RateTable_Subexptime_in_nsec(){ return Feb_Control_RateTable_Subexptime_in_nsec;} + +//returns -1 if slope is too high +int Feb_Control_SetRateCorrectionTau(int64_t tau_in_Nsec){ + + double sub_expure_time_in_sec = (double)(Feb_Control_GetSubFrameExposureTime())/(double)1e9; + double tau_in_sec = (double)tau_in_Nsec/(double)1e9; + unsigned int np = 16384; //max slope 16 * 1024 + double b0[1024]; + double m[1024]; + + + if(tau_in_sec<0||sub_expure_time_in_sec<0){ + printf("Error tau %f and sub_expure_time %f must be greater than 0.\n", tau_in_sec, sub_expure_time_in_sec); + return 0; + } + + printf("\tCalculating table for tau of %lld ns.\n", tau_in_Nsec); + int i; + for(i=0;i memory address --> memory + data_in( 1..0) -> lsb + + mem_data_out(13.. 0) -> base + mem_data_out(17..14) -> slope + + delta = slope*lsb + corr = base+delta + */ + + int next_i=0; + + b0[0] = 0; + m[0] = 1; + int b; + for(b=1;b<1024;b++){ + if(m[b-1]<14.5){ + double s=0,sx=0,sy=0,sxx=0,sxy=0; + for(;;next_i++){ + if(next_i>=np){ + cprintf(RED,"Error: (tau/subexptime) must be < 0.0015 \n"); + return -1; + } + + double x = Feb_Control_rate_meas[next_i] - b*4; + double y = next_i; + /*printf("Start Loop x: %f,\t y: %f,\t s: %f,\t sx: %f,\t sy: %f,\t sxx: %f,\t sxy: %f,\t " + "next_i: %d,\t b: %d,\t Feb_Control_rate_meas[next_i]: %f\n", + x, y, s, sx, sy, sxx, sxy, next_i, b, Feb_Control_rate_meas[next_i]);*/ + + if(x < -0.5) continue; + if(x > 3.5) break; + s += 1; + sx += x; + sy += y; + sxx += x*x; + sxy += x*y; + /*printf("End Loop x: %f,\t y: %f,\t s: %f,\t sx: %f,\t sy: %f,\t sxx: %f,\t sxy: %f,\t " + "next_i: %d,\t b: %d,\t Feb_Control_rate_meas[next_i]: %f\n", + x, y, s, sx, sy, sxx, sxy, next_i, b, Feb_Control_rate_meas[next_i]);*/ + } + double delta = s*sxx - sx*sx; + b0[b] = (sxx*sy - sx*sxy)/delta; + m[b] = (s*sxy - sx*sy) /delta; + + if(m[b]<0||m[b]>15) + m[b]=15; + /*printf("After Loop s: %f,\t sx: %f,\t sy: %f,\t sxx: %f,\t sxy: %f,\t " + "next_i: %d,\t b: %d,\t Feb_Control_rate_meas[next_i]: %f\n", + s, sx, sy, sxx, sxy, next_i, b, Feb_Control_rate_meas[next_i]);*/ + // cout<>2] & 0x3fff; + slope = ((Feb_Control_rate_correction_table[i>>2] & 0x3c000) >> 14); + delta = slope*lsb; + corr = delta+base; + printf("Readout Input: %d,\tBase:%d,\tSlope:%d,\tLSB:%d,\tDelta:%d\tResult:%d\tReal:%f\n", + i, base, slope, lsb, delta, corr, Feb_Control_rate_meas[i]); + } + return 1; +} diff --git a/slsDetectorSoftware/eigerDetectorServer/FebControl.h b/slsDetectorSoftware/eigerDetectorServer/FebControl.h index dfaf15969..eca0dd48f 100644 --- a/slsDetectorSoftware/eigerDetectorServer/FebControl.h +++ b/slsDetectorSoftware/eigerDetectorServer/FebControl.h @@ -111,7 +111,7 @@ int Feb_Control_GetModuleNumber(); void Feb_Control_FebControl(); - int Feb_Control_Init(int master, int top); + int Feb_Control_Init(int master, int top, int module_num); int Feb_Control_ReadSetUpFileToAddModules(char* file_name); int Feb_Control_ReadSetUpFile(unsigned int module_num, char* file_name); int Feb_Control_CheckSetup(); @@ -184,5 +184,12 @@ int Feb_Control_GetModuleNumber(); int Feb_Control_ClockRowClock(unsigned int ntimes); int Feb_Control_PulseChip(int npulses); + int64_t Feb_Control_Get_RateTable_Tau_in_nsec(); + int64_t Feb_Control_Get_RateTable_Subexptime_in_nsec(); + int Feb_Control_SetRateCorrectionTau(int64_t tau_in_Nsec); + int Feb_Control_SetRateCorrectionTable(unsigned int *table); + int Feb_Control_GetRateCorrectionVariable(); + void Feb_Control_SetRateCorrectionVariable(int activate_rate_correction); + int Feb_Control_PrintCorrectedValues(); -#endif +#endif diff --git a/slsDetectorSoftware/eigerDetectorServer/FebInterface.h b/slsDetectorSoftware/eigerDetectorServer/FebInterface.h index df96910ff..f05b88b9a 100644 --- a/slsDetectorSoftware/eigerDetectorServer/FebInterface.h +++ b/slsDetectorSoftware/eigerDetectorServer/FebInterface.h @@ -32,7 +32,7 @@ /*int Feb_Interface_WriteRegisters(unsigned int sub_num, unsigned int nwrites, unsigned int* reg_nums, unsigned int* values, int* wait_ons=0, unsigned int* wait_on_addresses=0);*/ int Feb_Interface_WriteRegisters(unsigned int sub_num, unsigned int nwrites, unsigned int* reg_nums, unsigned int* values, int* wait_ons, unsigned int* wait_on_addresses); - + //mem_num is 0 for trimbit BRAM and 1 for rate correction BRAM int Feb_Interface_WriteMemoryInLoops(unsigned int sub_num, unsigned int mem_num, unsigned int start_address, unsigned int nwrites, unsigned int *values); int Feb_Interface_WriteMemory(unsigned int sub_num, unsigned int mem_num, unsigned int start_address, unsigned int nwrites, unsigned int *values); diff --git a/slsDetectorSoftware/eigerDetectorServer/bin/beb_debug b/slsDetectorSoftware/eigerDetectorServer/bin/beb_debug deleted file mode 100755 index 81a3ceee0..000000000 Binary files a/slsDetectorSoftware/eigerDetectorServer/bin/beb_debug and /dev/null differ diff --git a/slsDetectorSoftware/eigerDetectorServer/bin/eigerDetectorServer_gemma b/slsDetectorSoftware/eigerDetectorServer/bin/eigerDetectorServer_gemma deleted file mode 100755 index 0a25b82e3..000000000 Binary files a/slsDetectorSoftware/eigerDetectorServer/bin/eigerDetectorServer_gemma and /dev/null differ diff --git a/slsDetectorSoftware/eigerDetectorServer/bin/eigerDetectorServer b/slsDetectorSoftware/eigerDetectorServer/bin/eigerDetectorServerv1.11.18 similarity index 50% rename from slsDetectorSoftware/eigerDetectorServer/bin/eigerDetectorServer rename to slsDetectorSoftware/eigerDetectorServer/bin/eigerDetectorServerv1.11.18 index 651eb76d7..8612c3cb5 100755 Binary files a/slsDetectorSoftware/eigerDetectorServer/bin/eigerDetectorServer and b/slsDetectorSoftware/eigerDetectorServer/bin/eigerDetectorServerv1.11.18 differ diff --git a/slsDetectorSoftware/eigerDetectorServer/bin/feb_debug b/slsDetectorSoftware/eigerDetectorServer/bin/feb_debug deleted file mode 100755 index 022341fb2..000000000 Binary files a/slsDetectorSoftware/eigerDetectorServer/bin/feb_debug and /dev/null differ diff --git a/slsDetectorSoftware/eigerDetectorServer/renameServer.sh b/slsDetectorSoftware/eigerDetectorServer/renameServer.sh new file mode 100644 index 000000000..a3745d4a3 --- /dev/null +++ b/slsDetectorSoftware/eigerDetectorServer/renameServer.sh @@ -0,0 +1,3 @@ +mv bin/eigerDetectorServer bin/$2 +git rm bin/$1 +git add bin/$2 diff --git a/slsDetectorSoftware/eigerDetectorServer/slsDetectorFunctionList.c b/slsDetectorSoftware/eigerDetectorServer/slsDetectorFunctionList.c index 96cadc708..a10cd7e01 100644 --- a/slsDetectorSoftware/eigerDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorSoftware/eigerDetectorServer/slsDetectorFunctionList.c @@ -51,9 +51,24 @@ unsigned int nimages_per_request=1; int on_dst=0; int dst_requested[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; -//char Module_dac_names[16][10]= {"SvP","Vtr","Vrf","Vrs","SvN","Vtgstv","Vcmp_ll","Vcmp_lr","cal","Vcmp_rl","rxb_rb","rxb_lb","Vcmp_rr","Vcp","Vcn","Vis"};; - -int default_dac_values[16] = {0,2480,3300,1400,4000,2556,1000,1000,4000,1000,1000,1000,1000,200,2000,1550}; +int default_dac_values[16] = { + 0, //SvP + 2480, //Vtr + 3300, //Vrf + 1400, //Vrs + 4000, //SvN + 2556, //Vtgstv + 1000, //Vcmp_ll + 1000, //Vcmp_lr + 4000, //cal + 1000, //Vcmp_rl + 1100, //rxb_rb + 1100, //rxb_lb + 1000, //Vcmp_rr + 200, //Vcp + 2000, //Vcn + 1550 //Vis +}; int default_gain_values[3] = {517000,517000,517000}; int default_offset_values[3] = {3851000,3851000,3851000}; @@ -125,7 +140,7 @@ int initDetector(){ getModuleConfiguration(); Feb_Interface_FebInterface(); Feb_Control_FebControl(); - Feb_Control_Init(master,top); + Feb_Control_Init(master,top,getDetectorNumber()); printf("FEB Initialization done\n"); Beb_Beb(); printf("BEB Initialization done\n"); @@ -145,10 +160,10 @@ int initDetector(){ setReadOutFlags(NONPARALLEL); setSpeed(0,1);//clk_devider,half speed setHighVolage(0,0); - setIODelay(675,0); + setIODelay(650,0); setTiming(AUTO_TIMING); //SetPhotonEnergyCalibrationParameters(-5.8381e-5,1.838515,5.09948e-7,-4.32390e-11,1.32527e-15); - //SetRateCorrection(0); //deactivate rate correction + setRateCorrection(0); //deactivate rate correction int enable[2] = {0,1}; setExternalGating(enable);//disable external gating Feb_Control_SetInTestModeVariable(0); @@ -166,7 +181,7 @@ int initDetectorStop(){ getModuleConfiguration(); Feb_Interface_FebInterface(); Feb_Control_FebControl(); - Feb_Control_Init(master,top); + Feb_Control_Init(master,top,getDetectorNumber()); printf("FEB Initialization done\n"); /* Beb_Beb(-1); printf("BEB constructor done\n");*/ @@ -279,6 +294,9 @@ u_int64_t getDetectorMAC() { pch = strtok (NULL, ":"); } sscanf(mac,"%llx",&res); + //increment by 1 for 10g + if(send_to_ten_gig) + res++; //printf("mac:%llx\n",res); return res; @@ -471,9 +489,54 @@ int pulseChip(int n){ return OK; } +int setRateCorrection(int64_t custom_tau_in_nsec){//in nanosec (will never be -1) + + //deactivating rate correction + if(custom_tau_in_nsec==0){ + Feb_Control_SetRateCorrectionVariable(0); + return 0; + } + + int64_t tau_in_nsec = Feb_Control_Get_RateTable_Tau_in_nsec(); + int64_t subexp_in_nsec = Feb_Control_Get_RateTable_Subexptime_in_nsec(); + //same setting + if((tau_in_nsec == custom_tau_in_nsec) && (subexp_in_nsec == Feb_Control_GetSubFrameExposureTime())){ + printf("Rate Table already created before: Same Tau %lldns, Same subexptime %lldns\n", + tau_in_nsec,subexp_in_nsec); + } + //different setting, calculate table + else{ + int ret = Feb_Control_SetRateCorrectionTau(custom_tau_in_nsec); + if(ret<=0){ + cprintf(RED,"Rate correction failed. Deactivating rate correction\n"); + Feb_Control_SetRateCorrectionVariable(0); + return ret;//-1 is for tau/subexptime error, 0 for all other errors + } + } + //activating rate correction + Feb_Control_SetRateCorrectionVariable(1); +#ifdef VERBOSE + Feb_Control_PrintCorrectedValues(); +#endif + + return Feb_Control_Get_RateTable_Tau_in_nsec(); +} + +int getRateCorrectionEnable(){ + return Feb_Control_GetRateCorrectionVariable(); +} + +int getDefaultSettingsTau_in_nsec(){ + switch(thisSettings){ + case STANDARD: return STANDARD_TAU; + case HIGHGAIN: return HIGHGAIN_TAU; + case LOWGAIN: return LOWGAIN_TAU; + default: return -1; + } +} -int setModule(sls_detector_module myMod, int* gain, int* offset){ +int setModule(sls_detector_module myMod, int* gain, int* offset,int* delay){ int retval[2]; int i; @@ -497,6 +560,12 @@ int setModule(sls_detector_module myMod, int* gain, int* offset){ printf("offset[%d]:%d\n",i,detectorOffset[i]); }else cprintf(RED,"offset not set\n"); } + + if(setIODelay(*delay, -1)!= (*delay)){ + cprintf(RED,"could not set iodelay %d\n",*delay); + return FAIL; + } + //copy module locally if (detectorModules) copyModule(detectorModules,&myMod); @@ -761,7 +830,7 @@ int64_t setTimer(enum timerIndex ind, int64_t val){ printf(" Setting sub exp time: %dns\n",(int)val/10); Feb_Control_SetSubFrameExposureTime(val/10); } - return (Feb_Control_GetSubFrameExposureTime()*10); + return (Feb_Control_GetSubFrameExposureTime()); case FRAME_PERIOD: @@ -911,15 +980,15 @@ int executeTrimming(enum trimMode mode, int par1, int par2, int imod){ int configureMAC(int ipad, long long int macad, long long int detectormacadd, int detipad, int udpport, int udpport2, int ival){ + if (detectormacadd != getDetectorMAC()){ + printf("*************************************************\n"); + printf("WARNING: actual detector mac address %llx does not match the one from client %llx\n",getDetectorMAC(),detectormacadd); + detectormacadd = getDetectorMAC(); + printf("WARNING: Matched detectormac to the hardware mac now\n"); + printf("*************************************************\n"); + } //only for 1Gbe if(!send_to_ten_gig){ - if (detectormacadd != getDetectorMAC()){ - printf("*************************************************\n"); - printf("WARNING: actual detector mac address %llx does not match the one from client %llx\n",getDetectorMAC(),detectormacadd); - detectormacadd = getDetectorMAC(); - printf("WARNING: Matched detectormac to the hardware mac now\n"); - printf("*************************************************\n"); - } if (detipad != getDetectorIP()){ printf("*************************************************\n"); printf("WARNING: actual detector ip address %x does not match the one from client %x\n",getDetectorIP(),detipad); diff --git a/slsDetectorSoftware/eigerDetectorServer/slsDetectorServer_defs.h b/slsDetectorSoftware/eigerDetectorServer/slsDetectorServer_defs.h index efed927c6..c41e5cdbf 100644 --- a/slsDetectorSoftware/eigerDetectorServer/slsDetectorServer_defs.h +++ b/slsDetectorSoftware/eigerDetectorServer/slsDetectorServer_defs.h @@ -40,4 +40,12 @@ enum detDacIndex{SVP,VTR,VRF,VRS,SVN,VTGSTV,VCMP_LL,VCMP_LR,CAL,VCMP_RL,RXB_RB,RXB_LB,VCMP_RR,VCP,VCN,VIS,VTHRESHOLD}; +enum defaultTauValues{ + HIGHGAIN_TAU=410, + STANDARD_TAU=290, + LOWGAIN_TAU=180}; + + + + #endif /* SLSDETECTORSERVER_DEFS_H_ */ diff --git a/slsDetectorSoftware/gotthardDetectorServer/gotthardDetectorServer b/slsDetectorSoftware/gotthardDetectorServer/gotthardDetectorServer deleted file mode 100755 index 3bdafcdf3..000000000 Binary files a/slsDetectorSoftware/gotthardDetectorServer/gotthardDetectorServer and /dev/null differ diff --git a/slsDetectorSoftware/gotthardDetectorServer/gotthardDetectorServerv2.0 b/slsDetectorSoftware/gotthardDetectorServer/gotthardDetectorServerv2.0 new file mode 100755 index 000000000..64cc73931 Binary files /dev/null and b/slsDetectorSoftware/gotthardDetectorServer/gotthardDetectorServerv2.0 differ diff --git a/slsDetectorSoftware/gotthardDetectorServer/mcb_funcs.c b/slsDetectorSoftware/gotthardDetectorServer/mcb_funcs.c index 577b740a1..4670e4968 100755 --- a/slsDetectorSoftware/gotthardDetectorServer/mcb_funcs.c +++ b/slsDetectorSoftware/gotthardDetectorServer/mcb_funcs.c @@ -725,9 +725,9 @@ int setSettings(int i, int imod) { } } thisSettings=isett; -#ifdef VERBOSE +//#ifdef VERBOSE printf("detector settings are %d\n",thisSettings); -#endif +//#endif return thisSettings; } diff --git a/slsDetectorSoftware/jungfrauDetectorServer/Makefile b/slsDetectorSoftware/jungfrauDetectorServer/Makefile index 6333c7b88..6b3fc4bd4 100755 --- a/slsDetectorSoftware/jungfrauDetectorServer/Makefile +++ b/slsDetectorSoftware/jungfrauDetectorServer/Makefile @@ -6,17 +6,17 @@ CROSS = bfin-uclinux- CC = $(CROSS)gcc -CFLAGS += -Wall -DMOENCHD -DMCB_FUNCS -DDACS_INT -DDEBUG -DV1#-DVERBOSE #-DVERYVERBOSE #-DVIRTUAL #-DDACS_INT_CSERVER +CFLAGS += -Wall -DMOENCHD -DMCB_FUNCS -DDACS_INT -DDEBUG -DV1 #-DVERBOSE #-DVERYVERBOSE #-DVIRTUAL #-DDACS_INT_CSERVER -PROGS= jungfrauDetectorServer +PROGS= jungfrauDetectorServerTest INSTDIR= /tftpboot INSTMODE= 0777 BINS = testlib_sharedlibc -SRCS = server.c server_funcs.c communication_funcs.c firmware_funcs.c mcb_funcs.c trimming_funcs.c sharedmemory.c +SRCS = server.c server_funcs.c communication_funcs.c firmware_funcs.c mcb_funcs.c sharedmemory.c OBJS = $(SRCS:%.c=%.o) @@ -27,6 +27,11 @@ test: clean jungfrauADCTEst boot: $(OBJS) +jungfrauDetectorServerTest: $(OBJS) + echo $(OBJS) + $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS_$@) $(LDFLAGS_$@) + + jungfrauDetectorServer: $(OBJS) echo $(OBJS) $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS_$@) $(LDFLAGS_$@) diff --git a/slsDetectorSoftware/jungfrauDetectorServer/ansi.h b/slsDetectorSoftware/jungfrauDetectorServer/ansi.h new file mode 120000 index 000000000..c8131b6ec --- /dev/null +++ b/slsDetectorSoftware/jungfrauDetectorServer/ansi.h @@ -0,0 +1 @@ +/afs/psi.ch/project/sls_det_software/dhanya_softwareDevelopment/mySoft/slsDetectorsPackage/slsReceiverSoftware/include/ansi.h \ No newline at end of file diff --git a/slsDetectorSoftware/jungfrauDetectorServer/firmware_funcs.c b/slsDetectorSoftware/jungfrauDetectorServer/firmware_funcs.c index c2fcde194..1765f4662 100755 --- a/slsDetectorSoftware/jungfrauDetectorServer/firmware_funcs.c +++ b/slsDetectorSoftware/jungfrauDetectorServer/firmware_funcs.c @@ -1,6 +1,7 @@ //#define TESTADC #define TESTADC1 + //#define TIMEDBG #include "server_defs.h" #include "firmware_funcs.h" @@ -64,7 +65,8 @@ struct timeval tss,tse,tsss; //for timing //for memory mapping -u_int64_t CSP0BASE; +u_int32_t CSP0BASE; + FILE *debugfp, *datafp; @@ -96,7 +98,11 @@ int phase_shift=0;//DEFAULT_PHASE_SHIFT; int ipPacketSize=DEFAULT_IP_PACKETSIZE; int udpPacketSize=DEFAULT_UDP_PACKETSIZE; +#ifndef NEW_PLL_RECONFIG u_int32_t clkDivider[2]={32,16}; +#else +u_int32_t clkDivider[2]={40,20}; +#endif int32_t clkPhase[2]={0,0}; u_int32_t adcDisableMask=0; @@ -109,22 +115,20 @@ enum externalSignalFlag signals[4]={EXT_SIG_OFF, EXT_SIG_OFF, EXT_SIG_OFF, EXT_ int withGotthard = 0; -#ifdef MCB_FUNCS -extern const int nChans; -extern const int nChips; -//extern const int nDacs; -//extern const int nAdcs; -#endif -#ifndef MCB_FUNCS - -const int nChans=NCHAN; -const int nChips=NCHIP; -const int nDacs=NDAC; -const int nAdcs=NADC; -#endif - - +/**is not const because this value will change after initDetector, is removed from mcb_funcs.c cuz its not used anywhere + * why is this used anywhere instead of macro*/ +int nChans=NCHAN; +int nChips=NCHIP; +int nDacs=NDAC; +int nAdcs=NADC; +extern enum detectorType myDetectorType; +/** for jungfrau reinitializing macro later in server_funcs.c in initDetector*/ +extern int N_CHAN; +extern int N_CHIP; +extern int N_DAC; +extern int N_ADC; +extern int N_CHANS; int mapCSP0(void) { printf("Mapping memory\n"); @@ -155,8 +159,8 @@ int mapCSP0(void) { return FAIL; } #endif - printf("CSPObase is 0x%x \n",CSP0BASE); - printf("CSPOBASE=from %08x to %x\n",CSP0BASE,CSP0BASE+MEM_SIZE); + printf("CSPObase is 0x%08x \n",CSP0BASE); + printf("CSPOBASE=from %08x to %08x\n",CSP0BASE,CSP0BASE+MEM_SIZE); u_int32_t address; address = FIFO_DATA_REG;//_OFF; @@ -383,7 +387,6 @@ u_int32_t setPllReconfigReg(u_int32_t reg, u_int32_t val, int trig) { u_int32_t getPllReconfigReg(u_int32_t reg, int trig) { - int i; u_int32_t val=reg<65535 || st<-65535) return clkPhase[0]; - - printf("phase %d\n", st); - - clkPhase[1]=st-clkPhase[0]; - +#ifdef NEW_PLL_RECONFIG + printf("reset pll\n"); + bus_w(PLL_CNTRL_REG,((1<=0) { if (nroi==0) { @@ -1350,7 +1377,7 @@ ROI *setROI(int nroi,ROI* arg,int *retvalsize, int *ret) { for (i=0; i=0 && ich=0 && ich=0) { + + if (val<60) { + dacvalue=0; + val=60; + } else if (val>=200) { + dacvalue=0x1; + val=200; + } else { + dacvalue=1.+(200.-val)/alpha; + val=200.-(dacvalue-1)*alpha; + } + printf ("****************************** setting val %d, dacval %d\n",val, dacvalue); + offw=DAC_REG; + + ddx=8; csdx=10; cdx=9; + codata=((dacvalue)&0xff); + + + + + valw=0xffff; bus_w(offw,(valw)); // start point + valw=((valw&(~(0x1<>(7-i))&0x1)<>SETTINGS_OFFSET); + retval=(bus_r(addr)); #ifdef VERBOSE - printf("Settings read from reg is %d\n",retval); + printf("Value read from Gain reg is %d\n",retval); #endif - if((isettings!=-1)&&(retval!=isettings)){ - printf("\n\nSettings r\n\n"); - return -1; - } - - return retval; + return retval; } @@ -1637,8 +1661,8 @@ int setADC(int adc){ ipPacketSize= DEFAULT_IP_PACKETSIZE; udpPacketSize=DEFAULT_UDP_PACKETSIZE; //set channel mask - nchips = NCHIP; - nchans = NCHANS; + nchips = N_CHIP; + nchans = N_CHANS; mask = ACTIVE_ADC_MASK; }/* //with moench module 1 adc -- NOT IMPLEMENTED @@ -1667,21 +1691,15 @@ int setADC(int adc){ } -int configureMAC(uint32_t destip,uint64_t destmac,uint64_t sourcemac,int sourceip,int ival,uint32_t destport) { -//int configureMAC(int ipad,long long int macad,long long int detectormacad, int detipad, int ival, int udpport){ - uint32_t sourceport = 0x7e9a; // 0xE185; - //setheader(0xF452142F3200,0x00ad29ae39fd,0x0a000264,0x0A00020d ,0x8436, 0x7e9a); +long int calcChecksum(int sourceip, int destip) { -/* void setheader(uint64_t destmac, uint64_t sourcemac, uint32_t destip, uint32_t sourceip, uint32_t destport, */ -/* uint32_t sourceport){ */ ip_header ip; int count; unsigned short *addr; long int sum = 0; long int checksum; - volatile u_int32_t conf= bus_r(CONFIG_REG); ip.ip_ver = 0x4; ip.ip_ihl = 0x5; @@ -1698,7 +1716,7 @@ ip.ip_destip = destip; count=sizeof(ip); - addr=&(ip); + addr=&(ip); /* warning: assignment from incompatible pointer type */ while( count > 1 ) { sum += *addr++; count -= 2; @@ -1707,9 +1725,63 @@ ip.ip_destip = destip; while (sum>>16) sum = (sum & 0xffff) + (sum >> 16);// Fold 32-bit sum to 16 bits checksum = (~sum)&0xffff; - printf("IP checksum is 0x%x\n",checksum); + printf("IP checksum is 0x%lx\n",checksum); + + return checksum; +} + +#ifdef NEW_GBE_INTERFACE +int writeGbeReg(int ivar, uint32_t val, int addr, int interface) { +/* #define GBE_CTRL_WSTROBE 0 */ +/* #define GBE_CTRL_VAR_OFFSET 16 */ +/* #define GBE_CTRL_VAR_MASK 0XF */ +/* #define GBE_CTRL_RAMADDR_OFFSET 24 */ +/* #define GBE_CTRL_RAMADDR_MASK 0X3F */ +/* #define GBE_CTRL_INTERFACE 23 */ + uint32_t ctrl=((ivar&GBE_CTRL_VAR_MASK)<>32)&0xFFFFFFFF; + vals[IPCHECKSUM_ADDR]=checksum; + vals[GBE_DELAY_ADDR]=0; + vals[GBE_RESERVED1_ADDR]=sourceport; + vals[GBE_RESERVED2_ADDR]=interface; + vals[DETECTOR_MAC_L_ADDR]=(sourcemac)&0xFFFFFFFF; + vals[DETECTOR_MAC_H_ADDR]=(sourcemac>>32)&0xFFFFFFFF; + vals[DETECTOR_IP_ADDR]=sourceip; + + for (ivar=0; ivar>32)&0xFFFFFFFF);//detectormacH_AReg_c bus_w(DETECTORMACL_AREG,(sourcemac)&0xFFFFFFFF);//detectormacL_AReg_c - bus_w(UDPPORTS_AREG,((destport&0xFFFF)<<16)+(sourceport&0xFFFF));//udpports_AReg_c + bus_w(UDPPORTS_AREG,((sourceport&0xFFFF)<<16)+(destport&0xFFFF));//udpports_AReg_c bus_w(IPCHKSUM_AREG,(checksum&0xFFFF));//ipchksum_AReg_c + +#endif + bus_w(CONTROL_REG,GB10_RESET_BIT); sleep(1); bus_w(CONTROL_REG,0); @@ -1727,198 +1802,51 @@ ip.ip_destip = destip; bus_w(CONFIG_REG,conf | GB10_NOT_CPU_BIT); printf("System status register is %08x\n",bus_r(SYSTEM_STATUS_REG)); -return; -/* } */ +return 0; //any value doesnt matter - dhanya -/* #ifdef DDEBUG */ -/* printf("Chip of Intrst Reg:%x\n",bus_r(CHIP_OF_INTRST_REG)); */ -/* printf("IP Packet Size:%d\n",ipPacketSize); */ -/* printf("UDP Packet Size:%d\n",udpPacketSize); */ -/* #endif */ - -/* //configuring mac */ -/* u_int32_t addrr=MULTI_PURPOSE_REG; */ -/* u_int32_t offset=ENET_CONF_REG, offset2=TSE_CONF_REG; */ -/* mac_conf *mac_conf_regs; */ -/* tse_conf *tse_conf_regs; */ -/* long int sum = 0; */ -/* long int checksum; */ -/* int count,val; */ -/* unsigned short *addr; */ - -/* mac_conf_regs=(mac_conf*)(CSP0BASE+offset*2); */ -/* tse_conf_regs=(tse_conf*)(CSP0BASE+offset2*2); */ - -/* #ifdef DDEBUG */ -/* printf("***Configuring MAC*** \n"); */ -/* #endif */ - -/* if(ival) */ -/* bus_w(addrr,(RESET_BIT|DIGITAL_TEST_BIT)); //0x080,reset mac (reset) */ -/* else */ -/* bus_w(addrr,RESET_BIT); //0x080,reset mac (reset) */ -/* val=bus_r(addrr); */ -/* #ifdef VERBOSE */ -/* printf("Value read from Multi-purpose Reg:%x\n",val); */ -/* #endif */ -/* // if(val!=0x080) return -1; */ - -/* usleep(500000); */ - -/* if(ival) */ -/* bus_w(addrr,(ENET_RESETN_BIT|WRITE_BACK_BIT|DIGITAL_TEST_BIT)); //0x840,write shadow regs(enet reset,write bak) */ -/* else */ -/* bus_w(addrr,(ENET_RESETN_BIT|WRITE_BACK_BIT)); //0x840,write shadow regs(enet reset,write bak) */ -/* val=bus_r(addrr); */ -/* #ifdef VERBOSE */ -/* printf("Value read from Multi-purpose Reg:%x\n",val); */ -/* #endif */ -/* // if(val!=0x840) return -1; */ - -/* if(ival) */ -/* bus_w(addrr,(ENET_RESETN_BIT|DIGITAL_TEST_BIT)); //0x800,nreset phy(enet reset) */ -/* else */ -/* bus_w(addrr,ENET_RESETN_BIT); //0x800,nreset phy(enet reset) */ -/* val=bus_r(addrr); */ -/* #ifdef VERBOSE */ -/* printf("Value read from Multi-purpose Reg:%x\n",val); */ -/* #endif */ -/* // if(val!=0x800) return -1; */ - - -/* mac_conf_regs->mac.mac_dest_mac1 =((macad>>(8*5))&0xFF);// 0x00; //pc7060 */ -/* mac_conf_regs->mac.mac_dest_mac2 =((macad>>(8*4))&0xFF);// 0x19; //pc7060 */ -/* mac_conf_regs->mac.mac_dest_mac3 =((macad>>(8*3))&0xFF);// 0x99; //pc7060 */ -/* mac_conf_regs->mac.mac_dest_mac4 =((macad>>(8*2))&0xFF);// 0x24; //pc7060 */ -/* mac_conf_regs->mac.mac_dest_mac5 =((macad>>(8*1))&0xFF);// 0xEB; //pc7060 */ -/* mac_conf_regs->mac.mac_dest_mac6 =((macad>>(8*0))&0xFF);// 0xEE; //pc7060 */ - -/* /\* */ -/* mac_conf_regs->mac.mac_src_mac1 = 0x00; */ -/* mac_conf_regs->mac.mac_src_mac2 = 0xAA; */ -/* mac_conf_regs->mac.mac_src_mac3 = 0xBB; */ -/* mac_conf_regs->mac.mac_src_mac4 = 0xCC; */ -/* mac_conf_regs->mac.mac_src_mac5 = 0xDD; */ -/* mac_conf_regs->mac.mac_src_mac6 = 0xEE; */ -/* *\/ */ -/* mac_conf_regs->mac.mac_src_mac1 =((detectormacad>>(8*5))&0xFF); */ -/* mac_conf_regs->mac.mac_src_mac2 =((detectormacad>>(8*4))&0xFF); */ -/* mac_conf_regs->mac.mac_src_mac3 =((detectormacad>>(8*3))&0xFF); */ -/* mac_conf_regs->mac.mac_src_mac4 =((detectormacad>>(8*2))&0xFF); */ -/* mac_conf_regs->mac.mac_src_mac5 =((detectormacad>>(8*1))&0xFF); */ -/* mac_conf_regs->mac.mac_src_mac6 =((detectormacad>>(8*0))&0xFF); */ -/* mac_conf_regs->mac.mac_ether_type = 0x0800; //ipv4 */ +} -/* mac_conf_regs->ip.ip_ver = 0x4; */ -/* mac_conf_regs->ip.ip_ihl = 0x5; */ -/* mac_conf_regs->ip.ip_tos = 0x0; */ -/* mac_conf_regs->ip.ip_len = ipPacketSize;//0x0522; // was 0x0526; */ -/* mac_conf_regs->ip.ip_ident = 0x0000; */ -/* mac_conf_regs->ip.ip_flag = 0x2; */ -/* mac_conf_regs->ip.ip_offset = 0x00; */ -/* mac_conf_regs->ip.ip_ttl = 0x70; */ -/* mac_conf_regs->ip.ip_protocol = 0x11; */ -/* mac_conf_regs->ip.ip_chksum = 0x0000 ; //6E42 now is automatically computed */ -/* mac_conf_regs->ip.ip_sourceip = detipad; //0x8181CA2E;129.129.202.46 */ -/* mac_conf_regs->ip.ip_destip = ipad; //CA57 */ - -/* //#ifdef VERBOSE */ -/* printf("mac_dest:%llx %x:%x:%x:%x:%x:%x\n", */ -/* macad, */ -/* mac_conf_regs->mac.mac_dest_mac1, */ -/* mac_conf_regs->mac.mac_dest_mac2, */ -/* mac_conf_regs->mac.mac_dest_mac3, */ -/* mac_conf_regs->mac.mac_dest_mac4, */ -/* mac_conf_regs->mac.mac_dest_mac5, */ -/* mac_conf_regs->mac.mac_dest_mac6); */ -/* printf("mac_src:%llx %x:%x:%x:%x:%x:%x\n", */ -/* detectormacad, */ -/* mac_conf_regs->mac.mac_src_mac1, */ -/* mac_conf_regs->mac.mac_src_mac2, */ -/* mac_conf_regs->mac.mac_src_mac3, */ -/* mac_conf_regs->mac.mac_src_mac4, */ -/* mac_conf_regs->mac.mac_src_mac5, */ -/* mac_conf_regs->mac.mac_src_mac6); */ -/* printf("ip_ttl:%x\n",mac_conf_regs->ip.ip_ttl); */ -/* printf("det_ip: %x %x\n",detipad, mac_conf_regs->ip.ip_sourceip); */ -/* printf("dest_ip: %x %x\n",ipad, mac_conf_regs->ip.ip_destip); */ - -/* //#endif */ - -/* //checksum */ -/* count=sizeof(mac_conf_regs->ip); */ -/* addr=&(mac_conf_regs->ip); */ -/* while( count > 1 ) { */ -/* sum += *addr++; */ -/* count -= 2; */ -/* } */ -/* if( count > 0 ) sum += *addr; // Add left-over byte, if any */ -/* while (sum>>16) sum = (sum & 0xffff) + (sum >> 16);// Fold 32-bit sum to 16 bits */ -/* checksum = (~sum)&0xffff; */ -/* mac_conf_regs->ip.ip_chksum = checksum; */ -/* //#ifdef VERBOSE */ -/* printf("IP header checksum is 0x%x s\n",(unsigned int)(checksum)); */ -/* //#endif */ - -/* mac_conf_regs->udp.udp_srcport = 0xE185; */ -/* mac_conf_regs->udp.udp_destport = udpport;//0xC351; */ -/* mac_conf_regs->udp.udp_len = udpPacketSize;//0x050E; //was 0x0512; */ -/* mac_conf_regs->udp.udp_chksum = 0x0000; */ - -/* #ifdef VERBOSE */ -/* printf("Configuring TSE\n"); */ -/* #endif */ -/* tse_conf_regs->rev = 0xA00; */ -/* tse_conf_regs->scratch = 0xCCCCCCCC; */ -/* tse_conf_regs->command_config = 0xB; */ -/* tse_conf_regs->mac_0 = 0x17231C00; */ -/* tse_conf_regs->mac_1 = 0xCB4A; */ -/* tse_conf_regs->frm_length = 0x5DC; //max frame length (1500 bytes) (was 0x41C) */ -/* tse_conf_regs->pause_quant = 0x0; */ -/* tse_conf_regs->rx_section_empty = 0x7F0; */ -/* tse_conf_regs->rx_section_full = 0x10; */ -/* tse_conf_regs->tx_section_empty = 0x3F8; //was 0x7F0; */ -/* tse_conf_regs->tx_section_full = 0x16; */ -/* tse_conf_regs->rx_almost_empty = 0x8; */ -/* tse_conf_regs->rx_almost_full = 0x8; */ -/* tse_conf_regs->tx_almost_empty = 0x8; */ -/* tse_conf_regs->tx_almost_full = 0x3; */ -/* tse_conf_regs->mdio_addr0 = 0x12; */ -/* tse_conf_regs->mdio_addr1 = 0x0; */ - -/* mac_conf_regs->cdone = 0xFFFFFFFF; */ -/* if(ival) */ -/* bus_w(addrr,(INT_RSTN_BIT|ENET_RESETN_BIT|WRITE_BACK_BIT|DIGITAL_TEST_BIT)); //0x2840,write shadow regs.. */ -/* else */ -/* bus_w(addrr,(INT_RSTN_BIT|ENET_RESETN_BIT|WRITE_BACK_BIT)); //0x2840,write shadow regs.. */ - -/* val=bus_r(addrr); */ -/* #ifdef VERBOSE */ -/* printf("Value read from Multi-purpose Reg:%x\n",val); */ -/* #endif */ -/* // if(val!=0x2840) return -1; */ - -/* usleep(100000); */ - -/* if(ival) */ -/* bus_w(addrr,(INT_RSTN_BIT|ENET_RESETN_BIT|SW1_BIT|DIGITAL_TEST_BIT)); //0x2820,write shadow regs.. */ -/* else */ -/* bus_w(addrr,(INT_RSTN_BIT|ENET_RESETN_BIT|SW1_BIT)); //0x2820,write shadow regs.. */ - -/* val=bus_r(addrr); */ -/* #ifdef VERBOSE */ -/* printf("Value read from Multi-purpose Reg:%x\n",val); */ -/* #endif */ -/* // if(val!=0x2820) return -1; */ - -/* return adcConfigured; */ + + + +int configureMAC(uint32_t destip,uint64_t destmac,uint64_t sourcemac,int sourceip,int ival,uint32_t destport) { +//int configureMAC(int ipad,long long int macad,long long int detectormacad, int detipad, int ival, int udpport){ + + uint32_t sourceport = 0x7e9a; // 0xE185; + int interface=0; + int ngb; +volatile u_int32_t conf= bus_r(CONFIG_REG); + + + + + +#ifdef NEW_GBE_INTERFACE + ngb=2; + printf("--------- New XGB interface\n"); +#else + ngb=1; + printf("********* Old XGB interface\n"); +#endif + + for (interface=0; interface >((i)*2))==3) { *\/ */ /* /\* i++; *\/ */ /* /\* if (i>15) *\/ */ @@ -2158,12 +2086,12 @@ u_int16_t* fifo_read_event(int ns) /* bus_w16(DUMMY_REG,i); */ /* } */ /* val=*values; */ - - // bus_w16(DUMMY_REG,0); // - for (i=0; i<16; i++) { - + // bus_w16(DUMMY_REG,0); // + for (i=0; i<16; i++) { + + // bus_w16(DUMMY_REG,i); // bus_r16(DUMMY_REG); /* dum=(((u_int16_t*)(now_ptr))+i); */ @@ -2174,36 +2102,36 @@ u_int16_t* fifo_read_event(int ns) // a=*values;//bus_r(FIFO_DATA_REG); // if ((adcDisableMask&(3<<(i*2)))==0) { *((u_int32_t*)now_ptr)=*values;//bus_r(FIFO_DATA_REG); - - + + if (i!=0 || ns!=0) { a=0; while (*((u_int32_t*)now_ptr)==*((u_int32_t*)(now_ptr)-1) && a++<10) { - + // printf("******************** %d: fifo %d: new %08x old %08x\n ",ns, i, *((u_int32_t*)now_ptr),*((u_int32_t*)(now_ptr)-1)); *((u_int32_t*)now_ptr)=*values; // printf("%d-",i); - + } } now_ptr+=4; - // } + // } /* while (((adcDisableMask&(3<<((i+1)*2)))>>((i+1)*2))==3) { */ /* i++; */ /* } */ // if (((adcDisableMask&(3<<((i+1)*2)))>>((i+1)*2))!=3) { - + bus_w16(DUMMY_REG,i+1); // } // *(((u_int16_t*)(now_ptr))+i)=bus_r16(FIFO_DATA_REG); - } - // bus_w16(DUMMY_REG,0); // + } + // bus_w16(DUMMY_REG,0); // /* #ifdef TIMEDBG */ - + /* gettimeofday(&tss,NULL); */ /* printf("read data loop = %ld usec\n",(tss.tv_usec) - (tse.tv_usec)); */ - + /* #endif */ #ifdef VERBOSE printf("*"); @@ -2329,7 +2257,6 @@ u_int32_t* decode_data(int *datain) int setDynamicRange(int dr) { - if (dr%16==0 && dr>0) { dynamicRange=16; nSamples=dr/16; @@ -2347,12 +2274,15 @@ int setDynamicRange(int dr) { int getDynamicRange() { - // dynamicRange=16; + if(myDetectorType == JUNGFRAU){ + dynamicRange=16; + return dynamicRange; + } + nSamples=bus_r(NSAMPLES_REG); getChannels(); - dataBytes=nModX*NCHIP*getChannels()*2; + dataBytes=nModX*N_CHIP*getChannels()*2; return dynamicRange*bus_r(NSAMPLES_REG);//nSamples; - } int testBus() { @@ -2400,7 +2330,7 @@ int setStoreInRAM(int b) { int getChannels() { int nch=32; int i; - for (i=0; i> 8); + //unsigned short v = (frame[a] << 8) + (frame[a] >> 8); // printf("%i: %i %i\n",a, frame[a],v); avg[a] += ((double)frame[a])/(double)frames; //if(frame[a] == 8191) @@ -3030,7 +2961,7 @@ int calibratePedestal(int frames){ - double nf = (double)numberFrames; + //double nf = (double)numberFrames; for(i =0; i < 1280; i++){ adc = i / 256; adcCh = (i - adc * 256) / 32; @@ -3123,7 +3054,7 @@ uint64_t writePatternWord(int addr, uint64_t word) { return readPatternWord(addr); } uint64_t writePatternIOControl(uint64_t word) { - if (word!=0xffffffffffffffff) { + if (word!=0xffffffffffffffff) { /*warning: integer constant is too large for ‘long’ type*/ // printf("%llx %llx %lld",get64BitReg(PATTERN_IOCTRL_REG_LSB,PATTERN_IOCTRL_REG_MSB),word); set64BitReg(word,PATTERN_IOCTRL_REG_LSB,PATTERN_IOCTRL_REG_MSB); // printf("************ write IOCTRL (%x)\n",PATTERN_IOCTRL_REG_MSB); @@ -3132,7 +3063,7 @@ uint64_t writePatternIOControl(uint64_t word) { } uint64_t writePatternClkControl(uint64_t word) { - if (word!=0xffffffffffffffff) set64BitReg(word,PATTERN_IOCLKCTRL_REG_LSB,PATTERN_IOCLKCTRL_REG_MSB); + if (word!=0xffffffffffffffff) set64BitReg(word,PATTERN_IOCLKCTRL_REG_LSB,PATTERN_IOCLKCTRL_REG_MSB);/*warning: integer constant is too large for ‘long’ type*/ return get64BitReg(PATTERN_IOCLKCTRL_REG_LSB,PATTERN_IOCLKCTRL_REG_MSB); } @@ -3203,7 +3134,6 @@ int setPatternLoop(int level, int *start, int *stop, int *n) { int setPatternWaitAddress(int level, int addr) { - int ret=-1; int reg; switch (level) { @@ -3232,7 +3162,6 @@ int setPatternWaitAddress(int level, int addr) { uint64_t setPatternWaitTime(int level, uint64_t t) { - uint64_t ret=-1; int reglsb; int regmsb; @@ -3266,7 +3195,7 @@ void initDac(int dacnum) { u_int32_t offw,codata; u_int16_t valw; - int iru,i,ddx,csdx,cdx; + int i,ddx,csdx,cdx; @@ -3279,7 +3208,7 @@ void initDac(int dacnum) { printf("data bit=%d, clkbit=%d, csbit=%d",ddx,cdx,csdx); - codata=((((0x6)<<4)+((0xf))<<16)+((0x0<<4)&0xfff0)); + codata=((((0x6)<<4)+((0xf))<<16)+((0x0<<4)&0xfff0)); /*warning: suggest parentheses around + or - inside shift*/ valw=0xffff; bus_w(offw,(valw)); // start point valw=((valw&(~(0x1<dacs=detectorDacs+imod*NDAC; - (detectorModules+imod)->adcs=detectorAdcs+imod*NADC; - (detectorModules+imod)->chipregs=detectorChips+imod*NCHIP; - (detectorModules+imod)->chanregs=detectorChans+imod*NCHIP*NCHAN; - (detectorModules+imod)->ndac=NDAC; - (detectorModules+imod)->nadc=NADC; - (detectorModules+imod)->nchip=NCHIP; - (detectorModules+imod)->nchan=NCHIP*NCHAN; + + for (imod=0; imoddacs=detectorDacs+imod*N_DAC; + (detectorModules+imod)->adcs=detectorAdcs+imod*N_ADC; + if(myDetectorType != JUNGFRAU){ + (detectorModules+imod)->chipregs=detectorChips+imod*N_CHIP; + (detectorModules+imod)->chanregs=detectorChans+imod*N_CHIP*N_CHAN; + } + (detectorModules+imod)->ndac=N_DAC; + (detectorModules+imod)->nadc=N_ADC; + (detectorModules+imod)->nchip=N_CHIP; + (detectorModules+imod)->nchan=N_CHIP*N_CHAN; (detectorModules+imod)->module=imod; (detectorModules+imod)->gain=0; (detectorModules+imod)->offset=0; @@ -103,14 +135,12 @@ int initDetector() { /* dynamicRange=getDynamicRange(); //always 16 not required commented out nModX=setNMod(-1);*/ - //dataBytes=nModX*NCHIP*NCHAN*4; // dynamicRange=32; // initChip(0, 0,ALLMOD); //nModX=n; // allocateRAM(); - return OK; } @@ -211,22 +241,27 @@ int copyModule(sls_detector_module *destMod, sls_detector_module *srcMod) { // printf("copying gain and offset %f %f to %f %f\n",srcMod->gain,srcMod->offset,destMod->gain,destMod->offset); - for (ichip=0; ichip<(srcMod->nchip); ichip++) { - if (*((srcMod->chipregs)+ichip)>=0) - *((destMod->chipregs)+ichip)=*((srcMod->chipregs)+ichip); - } - for (ichan=0; ichan<(srcMod->nchan); ichan++) { - if (*((srcMod->chanregs)+ichan)>=0) - *((destMod->chanregs)+ichan)=*((srcMod->chanregs)+ichan); + if(myDetectorType != JUNGFRAU){ + for (ichip=0; ichip<(srcMod->nchip); ichip++) { + if (*((srcMod->chipregs)+ichip)>=0) + *((destMod->chipregs)+ichip)=*((srcMod->chipregs)+ichip); + } + for (ichan=0; ichan<(srcMod->nchan); ichan++) { + if (*((srcMod->chanregs)+ichan)>=0) + *((destMod->chanregs)+ichan)=*((srcMod->chanregs)+ichan); + } } + for (idac=0; idac<(srcMod->ndac); idac++) { if (*((srcMod->dacs)+idac)>=0) *((destMod->dacs)+idac)=*((srcMod->dacs)+idac); } + for (iadc=0; iadc<(srcMod->nadc); iadc++) { if (*((srcMod->adcs)+iadc)>=0) *((destMod->adcs)+iadc)=*((srcMod->adcs)+iadc); } + return ret; } @@ -480,15 +515,15 @@ int selChip(const int chip,int imod) { /* sMod=allSelected; */ /* if (imod>=0 && imod=0 && imod0 && myo>0) { */ -/* //ethr=(myo-detectorDacs[VTHRESH+imod*NDAC])*1000/myg; */ +/* //ethr=(myo-detectorDacs[VTHRESH+imod*N_DAC])*1000/myg; */ /* ethr=(myo-setDACRegister(VDAC0,-1,imod))*1000/myg;//edited by dhanya */ /* // else */ @@ -644,7 +679,7 @@ int selChip(const int chip,int imod) { /* } */ /* #ifdef VERBOSE */ -/* //printf("module=%d gain=%f, offset=%f, dacu=%f\n",imod, myg, myo, detectorDacs[VTHRESH+imod*NDAC]); */ +/* //printf("module=%d gain=%f, offset=%f, dacu=%f\n",imod, myg, myo, detectorDacs[VTHRESH+imod*N_DAC]); */ /* printf("module=%d gain=%f, offset=%f, dacu=%d\n",imod, myg, myo,(int)(setDACRegister(VDAC0,-1,imod)));//edited by dhanya */ /* printf("Threshold energy of module %d is %d eV\n", imod, ethr); */ /* #endif */ @@ -722,7 +757,7 @@ int selChip(const int chip,int imod) { /* if (detectorDacs) { */ /* if (imodndac) */ -/* return (detectorDacs[ind+imod*NDAC]); */ +/* return (detectorDacs[ind+imod*N_DAC]); */ /* } */ /* return FAIL; */ /* *\/ */ @@ -847,38 +882,51 @@ int setSettings(int i, int imod) { else printf("\ninside set settings wit settings=%d...\n",i); #endif - int confgain[] = CONF_GAIN; - int isett=-2,retval; + int isett=-1,val=-1,retval=-1; + enum conf_gain { + dynamic = 0x0f00, //dynamic + dynamichighgain0 = 0x0f01, //dynamichighgain0 + fixgain1 = 0x0f02, //fixgain1 + fixgain2 = 0x0f06, //fixgain2 + forceswitchgain1 = 0x1f00, //forceswitchgain1 + forceswitchgain2 = 0x3f00 //forceswitchgain2 + }; - //reading settings - if(i==GET_SETTINGS){ - retval=initConfGainByModule(i,i,imod); - if(retval==i) - isett=UNDEFINED; - } - //writing settings - else{ - retval=initConfGainByModule(i,confgain[i],imod); - if(retval!=i) - isett=UNDEFINED; - } - //if error while read/writing - if(isett==UNDEFINED) - printf("Error:Weird Value read back from the Gain/Settings Reg\n"); - else{ - //validating the settings read back - if((retval>=HIGHGAIN)&&(retval<=VERYHIGHGAIN)) - isett=retval; - else{ - isett=UNDEFINED; - printf("Error:Wrong Settings Read out:%d\n",retval); - } - } - thisSettings=isett; -#ifdef VERBOSE - printf("detector settings are %d\n",thisSettings); -#endif - return thisSettings; + //determine conf value to write + if(i!=GET_SETTINGS){ + switch(i){ + case DYNAMICGAIN: val = dynamic;break; + case DYNAMICHG0: val = dynamichighgain0;break; + case FIXGAIN1: val = fixgain1;break; + case FIXGAIN2: val = fixgain2;break; + case FORCESWITCHG1: val = forceswitchgain1;break; + case FORCESWITCHG2: val = forceswitchgain2;break; + default: + printf("Error: This settings is not defined for this detector %d\n",i); + return GET_SETTINGS; + } + } + + retval=initConfGainByModule(i,val,imod); + + switch(retval){ + case dynamic: isett=DYNAMICGAIN; break; + case dynamichighgain0: isett=DYNAMICHG0; break; + case fixgain1: isett=FIXGAIN1; break; + case fixgain2: isett=FIXGAIN2; break; + case forceswitchgain1: isett=FORCESWITCHG1; break; + case forceswitchgain2: isett=FORCESWITCHG2; break; + default: + isett=UNDEFINED; + printf("Error:Wrong settings read out from Gain Reg 0x%x\n",retval); + break; + } + + thisSettings=isett; +//#ifdef VERBOSE + printf("detector settings are %d\n",thisSettings); +//#endif + return thisSettings; } @@ -886,7 +934,7 @@ int setSettings(int i, int imod) { /* Initialization*/ -int initChannelbyNumber(sls_detector_channel myChan) { +int initChannelbyNumber(sls_detector_channel myChan) {printf("in init channel by number\n"); int reg=myChan.reg; int ft=reg & TRIM_DR; int cae=(reg>>(NTRIMBITS))&1; @@ -930,7 +978,7 @@ int getChannelbyNumber(sls_detector_channel* myChan) { if (detectorChans) { if (imod=0) { if (ichip<(detectorModules+imod)->nchip && ichan<(detectorModules+imod)->nchan/(detectorModules+imod)->nchip) - myChan->reg=detectorChans[imod*NCHAN*NCHIP+ichip*NCHAN+ichan]; + myChan->reg=detectorChans[imod*N_CHAN*N_CHIP+ichip*N_CHAN+ichan]; return OK; } } @@ -942,7 +990,7 @@ int getTrimbit(int imod, int ichip, int ichan) { if (detectorChans) { if (imod=0) if (ichip<(detectorModules+imod)->nchip && ichan<(detectorModules+imod)->nchan/(detectorModules+imod)->nchip) - return (detectorChans[imod*NCHAN*NCHIP+ichip*NCHAN+ichan] & TRIM_DR); + return (detectorChans[imod*N_CHAN*N_CHIP+ichip*N_CHAN+ichan] & TRIM_DR); } return -1; @@ -966,8 +1014,8 @@ int initChannel(int ft,int cae, int ae, int coe, int ocoe, int counts, int imod) if (sChan==allSelected) { // printf("initializing all channels ft=%d coe=%d\n",ft,coe); chanmi=0; - chanma=NCHAN; - } else if (sChan==noneSelected || sChan>NCHAN || sChan<0) { + chanma=N_CHAN; + } else if (sChan==noneSelected || sChan>N_CHAN || sChan<0) { // printf("initializing no channels ft=%d coe=%d\n",ft,coe); chanmi=0; chanma=-1; @@ -980,8 +1028,8 @@ int initChannel(int ft,int cae, int ae, int coe, int ocoe, int counts, int imod) if (sChip==allSelected) { // printf("initializing all chips\n"); chipmi=0; - chipma=NCHIP; - } else if (sChip==noneSelected || sChip>NCHIP || sChip<0) { + chipma=N_CHIP; + } else if (sChip==noneSelected || sChip>N_CHIP || sChip<0) { // printf("initializing no chips\n"); chipmi=0; chipma=-1; @@ -1009,12 +1057,12 @@ int initChannel(int ft,int cae, int ae, int coe, int ocoe, int counts, int imod) for (ichip=chipmi; ichipnchip) { - myChip->reg=detectorChips[ichip+imod*NCHIP]; - myChip->nchan=NCHAN; - myChip->chanregs=detectorChans+imod*NCHAN*NCHIP+ichip*NCHIP; + myChip->reg=detectorChips[ichip+imod*N_CHIP]; + myChip->nchan=N_CHAN; + myChip->chanregs=detectorChans+imod*N_CHAN*N_CHIP+ichip*N_CHIP; return OK; } } @@ -1322,8 +1370,8 @@ int initChip(int obe, int ow,int imod){ if (sChip==allSelected) { chipmi=0; - chipma=NCHIP; - } else if (sChip==noneSelected || sChip>NCHIP || sChip<0) { + chipma=N_CHIP; + } else if (sChip==noneSelected || sChip>N_CHIP || sChip<0) { chipmi=0; chipma=-1; } else { @@ -1347,9 +1395,9 @@ int initChip(int obe, int ow,int imod){ for (im=modmi; imNCHIP || sChip<0) { + chipma=N_CHIP; + } else if (sChip==noneSelected || sChip>N_CHIP || sChip<0) { chipmi=0; chipma=-1; } else { @@ -1447,9 +1495,9 @@ int initChipWithProbes(int obe, int ow,int nprobes, int imod){ for (im=modmi; im0 && i%2==0) { printf("Shift stsel: module %d chip %i bit %d read %d instead of %d \n",k,j,i,val & 1<< j, i%2); result++; @@ -1926,7 +1978,7 @@ int testShiftStSel(int imod) { int testDataInOut(int num, int imod) { - int val[NCHIP*nModX], result=OK; + int val[N_CHIP*nModX], result=OK; int ich, ichip; setCSregister(ALLMOD); printf("Testing data in out for module %d pattern 0x%x\n", imod, num); @@ -1936,14 +1988,14 @@ int testDataInOut(int num, int imod) { setCSregister(ALLMOD); initChip(0, 0,ALLMOD); clearSSregister(ALLMOD); - for (ich=0; ich #include "communication_funcs.h" #include "server_funcs.h" -#include +#include + extern int sockfd; diff --git a/slsDetectorSoftware/jungfrauDetectorServer/server_defs.h b/slsDetectorSoftware/jungfrauDetectorServer/server_defs.h index fe2019c51..6a1f8eb23 100755 --- a/slsDetectorSoftware/jungfrauDetectorServer/server_defs.h +++ b/slsDetectorSoftware/jungfrauDetectorServer/server_defs.h @@ -7,25 +7,30 @@ // Hardware definitions +#define NMAXMODY 1 +#define NMAXMOD (NMAXMODX*NMAXMODY) #define NCHAN 32 #define NCHIP 1 -#define NMAXMODX 1 -#define NMAXMODY 1 -#define NMAXMOD (NMAXMODX*NMAXMODY) -#define NDAC 16 #define NADC 1 +#define NDAC 16 +#define NMAXMODX 1 +#define NCHANS (NCHAN*NCHIP*NMAXMOD) +#define NDACS (NDAC*NMAXMOD) + +#define JUNGFRAU_NCHAN (256*256) +#define JUNGFRAU_NCHIP 8 +#define JUNGFRAU_NADC 0 +#define JUNGFRAU_NDAC 16 +#define JUNGFRAU_NCHANS (JUNGFRAU_NCHAN*JUNGFRAU_NCHIP*NMAXMOD) + + /**when moench readout tested with gotthard module*/ #define GOTTHARDNCHAN 128 #define GOTTHARDNCHIP 10 - - -#define NCHANS (NCHAN*NCHIP*NMAXMOD) -#define NDACS (NDAC*NMAXMOD) - #define NTRIMBITS 6 #define NCOUNTBITS 24 diff --git a/slsDetectorSoftware/jungfrauDetectorServer/server_funcs.c b/slsDetectorSoftware/jungfrauDetectorServer/server_funcs.c index 30afd1dbf..5b0be78cd 100755 --- a/slsDetectorSoftware/jungfrauDetectorServer/server_funcs.c +++ b/slsDetectorSoftware/jungfrauDetectorServer/server_funcs.c @@ -4,7 +4,6 @@ #include "server_defs.h" #include "firmware_funcs.h" #include "mcb_funcs.h" -#include "trimming_funcs.h" #include "registers_m.h" #include "gitInfoMoench.h" @@ -56,19 +55,26 @@ extern int withGotthard; int adcvpp=0x4; +/** for jungfrau reinitializing macro later */ +int N_CHAN=NCHAN; +int N_CHIP=NCHIP; +int N_DAC=NDAC; +int N_ADC=NADC; +int N_CHANS=NCHANS; + + int init_detector(int b, int checkType) { int i; - int retvalsize,ret; if (mapCSP0()==FAIL) { printf("Could not map memory\n"); exit(1); } - // - + //print version printf("v: 0x%x\n",bus_r(FPGA_VERSION_REG)); printf("fp: 0x%x\n",bus_r(FIX_PATT_REG)); + //checktype if (checkType) { printf("Bus test... (checktype is %d; b is %d)",checkType,b ); for (i=0; i<1000000; i++) { @@ -80,7 +86,9 @@ int init_detector(int b, int checkType) { printf("Finished\n"); }else printf("(checktype is %d; b is %d)",checkType,b ); - //confirm if it is really moench + + + //confirm the detector type switch ((bus_r(PCB_REV_REG) & DETECTOR_TYPE_MASK)>>DETECTOR_TYPE_OFFSET) { case MOENCH03_MODULE_ID: myDetectorType=MOENCH; @@ -100,20 +108,15 @@ int init_detector(int b, int checkType) { default: myDetectorType=GENERIC; printf("Unknown detector type %02x\n",(bus_r(PCB_REV_REG) & DETECTOR_TYPE_MASK)>>DETECTOR_TYPE_OFFSET); - + break; } - printf("Detector type is %d\n", myDetectorType); - - // return OK; + //control server only-- if (b) { - - - resetPLL(); - + resetPLL(); bus_w16(CONTROL_REG, SYNC_RESET); bus_w16(CONTROL_REG, 0); bus_w16(CONTROL_REG, GB10_RESET_BIT); @@ -121,26 +124,94 @@ int init_detector(int b, int checkType) { #ifdef MCB_FUNCS printf("\nBoard Revision:0x%x\n",(bus_r(PCB_REV_REG)&BOARD_REVISION_MASK)); - // initDetector(); + if(myDetectorType == JUNGFRAU) + initDetector(); /*allocating detectorModules, detectorsDacs etc for "settings", also does allocate RAM*/ + dataBytes=NMAXMOD*N_CHIP*N_CHAN*2; /**Nchip and Nchan real values get assigned in initDetector()*/ printf("Initializing Detector\n"); //bus_w16(CONTROL_REG, SYNC_RESET); // reset registers #endif - // testFpga(); // testRAM(); // printf("ADC_SYNC_REG:%x\n",bus_r(ADC_SYNC_REG)); //moench specific - - // setPhaseShiftOnce(); //firmware.h - - prepareADC(); // server_funcs + // setPhaseShiftOnce(); + /*some registers set, which is in common with jungfrau, please check */ + prepareADC(); //setADC(-1); //already does setdaqreg and clean fifo // setSettings(GET_SETTINGS,-1); - + /*some registers set, which is in common with jungfrau, please check */ initDac(0); initDac(8); //initializes the two dacs - - //Initialization + + if(myDetectorType==JUNGFRAU){ + /** for jungfrau reinitializing macro */ + N_CHAN=JUNGFRAU_NCHAN; + N_CHIP=JUNGFRAU_NCHIP; + N_DAC=JUNGFRAU_NDAC; + N_ADC=JUNGFRAU_NADC; + N_CHANS=JUNGFRAU_NCHANS; + + + //set dacs + int retval = -1; + int dacvalues[14][2]={ + {0, 1250}, //vout_cm + {10, 1053}, //vin_com + {1, 600}, //vb_sda + {11, 1000}, //vb_colbuf + {2, 3000}, //vb_test_cur + {3, 830}, //vcascp_pixbuf + {4, 1630}, //vcascn_pixbuf + {12, 750}, //vb_pixbuf + {6, 480}, //vref_ds + {5, 1000}, //vb_ds + {7, 400}, //vref_comp + {13, 1220}, //vb_comp + {8, 1500}, //vref_prech + {9, 3000}, //vdd_prot + }; + for(i=0;i<14;++i){ + retval=setDac(dacvalues[i][0], dacvalues[i][1]); + if(retval!=dacvalues[i][1]) + printf("Error: Setting dac %d failed, wrote %d, read %d\n",dacvalues[i][0],dacvalues[i][1],retval); + } + + //power on the chips + bus_w(POWER_ON_REG,0x1); + + //reset adc + writeADC(ADCREG1,0x3); writeADC(ADCREG1,0x0); + writeADC(ADCREG2,0x40); + writeADC(ADCREG3,0xf); + writeADC(ADCREG4,0x3f); + //vrefs - configurable? + writeADC(ADCREG_VREFS,0x2); + + + //set ADCINVERSionreg (by trial and error) + bus_w(ADC_INVERSION_REG,0x453b2a9c); + + //set adc_pipeline + bus_w(ADC_PIPELINE_REG,0x20); //same as ADC_OFFSET_REG + + //set dbit_pipeline + bus_w(DBIT_PIPELINE_REG,0x100e); + usleep(1000000);//1s + + //reset mem machine fifos fifos + bus_w(MEM_MACHINE_FIFOS_REG,0x4000); + bus_w(MEM_MACHINE_FIFOS_REG,0x0); + + //reset run control + bus_w(MEM_MACHINE_FIFOS_REG,0x0400); + bus_w(MEM_MACHINE_FIFOS_REG,0x0); + + //set default setting + setSettings(DYNAMICGAIN,-1); + } + + + //Initialization of acquistion parameters setFrames(-1); setTrains(-1); setExposureTime(-1); @@ -152,14 +223,24 @@ int init_detector(int b, int checkType) { setMaster(GET_MASTER); setSynchronization(GET_SYNCHRONIZATION_MODE); startReceiver(0); //firmware - } + }//end of control server only-- + else printf("\n\n"); + + + //common for both control and stop server strcpy(mess,"dummy message"); strcpy(lastClientIP,"none"); strcpy(thisClientIP,"none1"); lockStatus=0; // getDynamicRange(); - setROI(-1,NULL,&retvalsize,&ret); - allocateRAM(); + + /* both these functions setROI and allocateRAM should go into the control server part. */ + if(myDetectorType!=JUNGFRAU){ + int retvalsize,ret; + setROI(-1,NULL,&retvalsize,&ret); + allocateRAM(); + } + return OK; } @@ -169,7 +250,7 @@ int decode_function(int file_des) { int retval=FAIL; #ifdef VERBOSE printf( "receive data\n"); -#endif +#endif n = receiveDataOnly(file_des,&fnum,sizeof(fnum)); if (n <= 0) { #ifdef VERBOSE @@ -182,9 +263,9 @@ int decode_function(int file_des) { printf("size of data received %d\n",n); #endif - //#ifdef VERBOSE +#ifdef VERBOSE printf( "calling function fnum = %d %x %x %x\n",fnum,(unsigned int)(flist[fnum]), (unsigned int)(flist[F_READ_REGISTER]),(unsigned int)(&read_register)); - //#endif +#endif if (fnum<0 || fnum>255) fnum=255; retval=(*flist[fnum])(file_des); @@ -955,14 +1036,13 @@ int read_register(int file_des) { int set_dac(int file_des) { //default:all mods - int retval, retval1; + int retval, retval1; int ret=OK; int arg[3]; enum dacIndex ind; int imod; int n; int val; - int idac=0; int mV=0; sprintf(mess,"Can't set DAC\n"); @@ -989,8 +1069,8 @@ int set_dac(int file_des) { if (imod<0) imod=ALLMOD; - - + + #ifdef MCB_FUNCS @@ -998,8 +1078,7 @@ int set_dac(int file_des) { if (differentClients==1 && lockStatus==1) { ret=FAIL; sprintf(mess,"Detector locked by %s\n",lastClientIP); - } else{ - + } else{ if (ind<16) { if (mV) { @@ -1029,36 +1108,30 @@ int set_dac(int file_des) { } retval=adcvpp;; - } + } else if (ind==HV_NEW ) + retval=initHighVoltageByModule(val,imod); + else + printf("**********No dac with index %d\n",ind); } } if(ret==OK){ - /* ret=FAIL; */ -/* if(idac==HIGH_VOLTAGE){ */ -/* if(retval==-2) */ -/* strcpy(mess,"Invalid Voltage.Valid values are 0,90,110,120,150,180,200"); */ -/* else if(retval==-3) */ -/* strcpy(mess,"Weird value read back or it has not been set yet\n"); */ -/* else */ -/* ret=OK; */ -/* }//since v r saving only msb */ -/* else if ((retval-val)<=3 || val==-1) */ -/* ret=OK; */ - if (ind<16) { - if (mV) { + if (ind<16) { + if (mV) { - printf("%d DACu is ",retval); - retval1=2500*retval/16535; - printf("%d mV \n",retval1); - } else + printf("%d DACu is ",retval); + retval1=2500*retval/16535; + printf("%d mV \n",retval1); + } else + retval1=retval; + } else retval1=retval; - } + #endif #ifdef VERBOSE printf("DAC set to %d V\n", retval); #endif - } + if(ret==FAIL) printf("Setting dac %d of module %d: wrote %d but read %d\n", ind, imod, val, retval); else{ @@ -1178,13 +1251,13 @@ int set_channel(int file_des) { #ifdef VERBOSE printf("channel number is %d, chip number is %d, module number is %d, register is %lld\n", myChan.chan,myChan.chip, myChan.module, myChan.reg); #endif - + if (ret==OK) { if (myChan.module>=getNModBoard()) ret=FAIL; - if (myChan.chip>=NCHIP) + if (myChan.chip>=N_CHIP) ret=FAIL; - if (myChan.chan>=NCHAN) + if (myChan.chan>=N_CHAN) ret=FAIL; if (myChan.module<0) myChan.module=ALLMOD; @@ -1258,13 +1331,13 @@ int get_channel(int file_des) { } if (ret==OK) { ret=FAIL; - if (ichip>=0 && ichip=0 && ichip=0 && ichan=0 && ichan=NCHIP) + if (myChip.chip>=N_CHIP) ret=FAIL; } if (differentClients==1 && lockStatus==1) { @@ -1392,7 +1465,7 @@ int get_chip(int file_des) { } if (ret==OK) { ret=FAIL; - if (ichip>=0 && ichip=0 && ichip=0) ret=OK; @@ -1503,6 +1592,8 @@ int set_module(int file_des) { } else { #ifdef MCB_FUNCS retval=initModulebyNumber(myModule); + if(retval != myModule.reg) + ret = FAIL; #endif } } @@ -1522,14 +1613,15 @@ int set_module(int file_des) { } else { n += sendDataOnly(file_des,mess,sizeof(mess)); } - free(myChip); - free(myChan); + free(myDac); - free(myAdc); + if(myAdc != NULL) free(myAdc); + if(myChip != NULL) free(myChip); + if(myChan != NULL) free(myChan); - // setDynamicRange(dr); always 16 commented out - + //setDynamicRange(dr); always 16 commented out + return ret; } @@ -1538,21 +1630,22 @@ int set_module(int file_des) { int get_module(int file_des) { - int ret=OK; - - int arg; int imod; int n; - - - sls_detector_module myModule; - int *myChip=malloc(NCHIP*sizeof(int)); - int *myChan=malloc(NCHIP*NCHAN*sizeof(int)); - int *myDac=malloc(NDAC*sizeof(int));/**dhanya*/ - int *myAdc=malloc(NADC*sizeof(int));/**dhanya*/ + int *myDac=malloc(N_DAC*sizeof(int)); + int *myChip=NULL; + int *myChan=NULL; + int *myAdc=NULL; + + /*not required for jungfrau. so save memory*/ + if(myDetectorType != JUNGFRAU){ + myChip=malloc(N_CHIP*sizeof(int)); + myChan=malloc(N_CHIP*N_CHAN*sizeof(int)); + myAdc=malloc(N_ADC*sizeof(int)); + } if (myDac) @@ -1561,34 +1654,40 @@ int get_module(int file_des) { sprintf(mess,"could not allocate dacs\n"); ret=FAIL; } - if (myAdc) - myModule.adcs=myAdc; - else { - sprintf(mess,"could not allocate adcs\n"); - ret=FAIL; - } - if (myChip) - myModule.chipregs=myChip; - else { - sprintf(mess,"could not allocate chips\n"); - ret=FAIL; - } - if (myChan) - myModule.chanregs=myChan; - else { - sprintf(mess,"could not allocate chans\n"); - ret=FAIL; + + + myModule.adcs=NULL; + myModule.chipregs=NULL; + myModule.chanregs=NULL; + /*not required for jungfrau. so save memory*/ + if(myDetectorType != JUNGFRAU){ + if (myAdc) + myModule.adcs=myAdc; + else { + sprintf(mess,"could not allocate adcs\n"); + ret=FAIL; + } + if (myChip) + myModule.chipregs=myChip; + else { + sprintf(mess,"could not allocate chips\n"); + ret=FAIL; + } + if (myChan) + myModule.chanregs=myChan; + else { + sprintf(mess,"could not allocate chans\n"); + ret=FAIL; + } } - myModule.ndac=NDAC; - myModule.nchip=NCHIP; - myModule.nchan=NCHAN*NCHIP; - myModule.nadc=NADC; + myModule.ndac=N_DAC; + myModule.nchip=N_CHIP; + myModule.nchan=N_CHAN*N_CHIP; + myModule.nadc=N_ADC; - - n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); @@ -1619,17 +1718,18 @@ int get_module(int file_des) { n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=FAIL) { /* send return argument */ - ret=sendModule(file_des, &myModule); + if(myDetectorType != JUNGFRAU) + ret=sendModuleGeneral(file_des, &myModule,1); //1 is to send everything + else + ret=sendModuleGeneral(file_des, &myModule,0); //0 is to send partially (without trimbits etc.) } else { n += sendDataOnly(file_des,mess,sizeof(mess)); } - - - free(myChip); - free(myChan); free(myDac); - free(myAdc); + if(myChip != NULL) free(myChip); + if(myChan != NULL) free(myChan); + if(myAdc != NULL) free(myAdc); /*return ok/fail*/ @@ -1854,7 +1954,35 @@ int get_run_status(int file_des) { retval= runState(); printf("\n\nSTATUS=%08x\n",retval); + if(myDetectorType == JUNGFRAU){ + if(!(retval&RUN_BUSY_BIT)){ + if((retval&READMACHINE_BUSY_BIT) ){ + printf("-----------------------------------READ MACHINE BUSY--------------------------\n"); + s=TRANSMITTING; + } + //and readbusy=0,idle + else if((!(retval&0xffff))||(retval==SOME_FIFO_FULL_BIT)){ + printf("-----------------------------------IDLE--------------------------------------\n"); + s=IDLE; + } else { + printf("-----------------------------------Unknown status %08x--------------------------------------\n", retval); + s=ERROR; + ret=FAIL; + } + } + //if runbusy=1 + else { + if (retval&WAITING_FOR_TRIGGER_BIT){ + printf("-----------------------------------WAITING-----------------------------------\n"); + s=WAITING; + } + else{ + printf("-----------------------------------RUNNING-----------------------------------\n"); + s=RUNNING; + } + } +}else{ //error if(retval&SOME_FIFO_FULL_BIT){ printf("-----------------------------------ERROR--------------------------------------x%0x\n",retval); @@ -1864,10 +1992,13 @@ int get_run_status(int file_des) { // else if(!(retval&RUNMACHINE_BUSY_BIT)){ //commented by Anna 24.10.2012 else if(!(retval&RUN_BUSY_BIT)){ // by Anna 24.10.2012 + if((retval&STOPPED_BIT) ){ // - //and readbusy=1, its last frame read - if((retval&READMACHINE_BUSY_BIT) ){ // + printf("-----------------------------------STOPPED--------------------------\n"); + s=STOPPED; + } else if((retval&READMACHINE_BUSY_BIT) ){ // ///and readbusy=1, its last frame read + printf("-----------------------------------READ MACHINE BUSY--------------------------\n"); @@ -1898,9 +2029,9 @@ int get_run_status(int file_des) { printf("-----------------------------------RUNNING-----------------------------------\n"); s=RUNNING; } - } - + } +} if (ret!=OK) { @@ -1924,14 +2055,12 @@ int get_run_status(int file_des) { int read_frame(int file_des) { - - int ns=0; u_int16_t* p=NULL; - if (differentClients==1 && lockStatus==1) { dataret=FAIL; - sprintf(mess,"Detector locked by %s\n",lastClientIP); + sprintf(mess,"Detector locked by %s\n",lastClientIP); + printf("Warning: %s\n",mess); sendDataOnly(file_des,&dataret,sizeof(dataret)); sendDataOnly(file_des,mess,sizeof(mess)); #ifdef VERBOSE @@ -1940,6 +2069,7 @@ int read_frame(int file_des) { return dataret; } + p=fifo_read_frame(); if (p) { nframes++; @@ -1971,6 +2101,10 @@ int read_frame(int file_des) { sendDataOnly(file_des,&dataret,sizeof(dataret)); sendDataOnly(file_des,mess,sizeof(mess)); } + + + + return dataret; } @@ -2288,53 +2422,48 @@ int set_roi(int file_des) { int retvalsize=0; ROI arg[MAX_ROIS]; ROI* retval=0; - int iroi; strcpy(mess,"Could not set/get roi\n"); - - // u_int32_t disable_reg=0; - n = receiveDataOnly(file_des,&nroi,sizeof(nroi)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; } - if(nroi>=0){ - n = receiveDataOnly(file_des,arg,nroi*sizeof(ROI)); - if (n != (nroi*sizeof(ROI))) { - sprintf(mess,"Received wrong number of bytes for ROI\n"); + if(myDetectorType == JUNGFRAU){ + ret = FAIL; + strcpy(mess,"Not applicable/implemented for this detector\n"); + printf("Error:Set ROI-%s",mess); + } + + else{ + + if(nroi>=0){ + n = receiveDataOnly(file_des,arg,nroi*sizeof(ROI)); + if (n != (nroi*sizeof(ROI))) { + sprintf(mess,"Received wrong number of bytes for ROI\n"); + ret=FAIL; + } + + printf("Setting ROI to:"); + for( i=0;i - - - -extern int nModX; -//extern int *values; - -extern const int nChans; -extern const int nChips; -extern const int nDacs; -extern const int nAdcs; - - -int trim_fixed_settings(int countlim, int par2, int im) -{ - - int retval=OK; -#ifdef VERBOSE - printf("Trimming with fixed settings\n"); -#endif -#ifdef VIRTUAL - return OK; -#endif - - if (par2<=0) - retval=trim_with_level(countlim, im); - else - retval=trim_with_median(countlim,im); - - - return retval; -} - - -int trim_with_noise(int countlim, int nsigma, int im) -{ - - - int retval=OK, retval1=OK, retval2=OK; -#ifdef VERBOSE - printf("Trimming using noise\n"); -#endif -#ifdef VIRTUAL - return OK; -#endif - - /* threshold scan */ - -#ifdef VERBOSE - printf("chosing vthresh and vtrim....."); -#endif - retval1=choose_vthresh_and_vtrim(countlim,nsigma, im); - -#ifdef VERBOSE - printf("trimming with noise.....\n"); -#endif - retval2=trim_with_level(countlim, im); - -#ifdef DEBUGOUT - printf("done\n"); -#endif - if (retval1==OK && retval2==OK) - retval=OK; - else - retval=FAIL; - - return retval; - -} - -int trim_with_beam(int countlim, int nsigma, int im) //rpc -{ - - - int retval=OK, retval1=OK, retval2=OK; - - printf("Trimming using beam\n"); - //return OK; -#ifdef VIRTUAL - printf("Trimming using beam\n"); - return OK; -#endif - /* threshold scan */ -#ifdef DEBUGOUT - printf("chosing vthresh and vtrim....."); -#endif - - retval1=choose_vthresh_and_vtrim(countlim,nsigma,im); - retval2=trim_with_median(TRIM_DR, im); - -#ifdef DEBUGOUT - printf("done\n"); -#endif - - if (retval1==OK && retval2==OK) - retval=OK; - else - retval=FAIL; - - return retval; - -} - - -int trim_improve(int maxit, int par2, int im) //rpc -{ - - int retval=OK, retval1=OK, retval2=OK; - - -#ifdef VERBOSE - printf("Improve the trimming\n"); -#endif -#ifdef VIRTUAL - return OK; -#endif - - - if (par2!=0 && im==ALLMOD) - retval1=choose_vthresh(); - - retval2=trim_with_median(2*maxit+1, im); -#ifdef DEBUGOUT - printf("done\n"); -#endif - if (retval1==OK && retval2==OK) - retval=OK; - else - retval=FAIL; - - return retval; - -} - -int calcthr_from_vcal(int vcal) { - int thrmin; - //thrmin=140+3*vcal/5; - thrmin=180+3*vcal/5; - return thrmin; -} - -int calccal_from_vthr(int vthr) { - int vcal; - vcal=5*(vthr-140)/3; - return vcal; -} - -int choose_vthresh_and_vtrim(int countlim, int nsigma, int im) { - int retval=OK; -#ifdef MCB_FUNCS - int modma, modmi, nm; - int thr, thrstep=5, nthr=31; - - int *fifodata; - - double vthreshmean, vthreshSTDev; - int *thrmi, *thrma; - double c; - double b=BVTRIM; - double a=AVTRIM; - int *trim; - int ich, imod, ichan; - int nvalid=0; - u_int32_t *scan; - int ithr; - sls_detector_channel myChan; - - - - setFrames(1); - // setNMod(getNModBoard()); - - if (im==ALLMOD){ - modmi=0; - modma=nModX; - } else { - modmi=im; - modma=im+1; - } - nm=modma-modmi; - - trim=malloc(sizeof(int)*nChans*nChips*nModX); - thrmi=malloc(sizeof(int)*nModX); - thrma=malloc(sizeof(int)*nModX); - - - for (ich=0; ichcountlim && trim[ich]==-1) { -//commented out by dhanya trim[ich]=getDACbyIndexDACU(VTHRESH,imod); -#ifdef VERBOSE - // printf("yes: %d %d %d\n",ich,ithr,scan[ich]); -#endif - } -#ifdef VERBOSE - /* else { - printf("no: %d %d %d\n",ich,ithr,scan[ich]); - }*/ -#endif - } - } - free(scan); - } - - for (imod=modmi; imodthrmi[imod] && trim[ich]0) { - vthreshmean=vthreshmean/nvalid; - //commented out by dhanya vthreshSTDev=sqrt((vthreshSTDev/nvalid)-vthreshmean*vthreshmean); - } else { - vthreshmean=thrmi[imod]; - vthreshSTDev=nthr*thrstep; - printf("No valid channel for module %d\n",imod); - retval=FAIL; - } - -#ifdef DEBUGOUT - printf("module= %d nvalid = %d mean=%f RMS=%f\n",imod, nvalid, vthreshmean,vthreshSTDev); -#endif - // *vthresh=round(vthreshmean-nsigma*vthreshSTDev); - thr=(int)(vthreshmean-nsigma*vthreshSTDev); - if (thr<0 || thr>(DAC_DR-1)) { - thr=thrmi[imod]/2; - printf("Can't find correct threshold for module %d\n",imod); - retval=FAIL; - } -//commented out by dhanya initDACbyIndexDACU(VTHRESH,thr,imod); -#ifdef VERBOSE - printf("vthresh=%d \n",thr); -#endif - c=CVTRIM-2.*nsigma*vthreshSTDev/63.; - //commented out by dhanya thr=(int)((-b-sqrt(b*b-4*a*c))/(2*a)); - if (thr<500 || thr>(DAC_DR-1)) { - thr=750; - printf("Can't find correct trimbit size for module %d\n",imod); - retval=FAIL; - } - - //commented out by dhanya initDACbyIndexDACU(VTRIM,thr,imod); - -#ifdef VERBOSE - printf("vtrim=%d \n",thr); -#endif - - } - free(trim); - free(thrmi); - free(thrma); - -#endif - return retval; -} - - - - - -int trim_with_level(int countlim, int im) { - int ich, itrim, ichan, ichip, imod; - u_int32_t *scan; - int *inttrim; - int modma, modmi, nm; - int retval=OK; - int *fifodata; - sls_detector_channel myChan; - printf("trimming module number %d", im); - - -#ifdef MCB_FUNCS - setFrames(1); - // setNMod(getNModBoard()); - - if (im==ALLMOD){ - modmi=0; - modma=nModX; - } else { - modmi=im; - modma=im+1; - } - nm=modma-modmi; - - inttrim=malloc(sizeof(int)*nChips*nChans*nModX); - printf("countlim=%d\n",countlim); - for (ich=0; ichcountlim){ - inttrim[ich]=itrim; - if (scan[ich]>2*countlim && itrim>0) { - //if (scan[ich]>2*countlim || itrim==0) { - inttrim[ich]=itrim-1; - } -#ifdef VERBOSE - printf("Channel %d trimbit %d counted %d (%08x) countlim %d\n",ich,itrim,scan[ich],fifodata[ich],countlim); -#endif - } - } -#ifdef VERBOSE - /* else - printf("Channel %d trimbit %d counted %d countlim %d\n",ich,itrim,scan[ich],countlim);*/ -#endif - } - } - free(scan); - } - - for (imod=modmi; imod0) - direction[ichan]=1; - else - direction[ichan]=-1; - } - //commented out by dhanya vthresh=getDACbyIndexDACU(VTHRESH,imod); - if ( direction[ichan]!=-3) { - if (abs(diff)>abs(olddiff[ichan])) { - vthresh=vthresh-direction[ichan]; - if (vthresh>(DAC_DR-1)) { - vthresh=(DAC_DR-1); - printf("can't equalize threshold for module %d\n", ichan); - retval=FAIL; - } - if (vthresh<0) { - vthresh=0; - printf("can't equalize threshold for module %d\n", ichan); - retval=FAIL; - } - direction[ichan]=-3; - } else { - vthresh=vthresh+direction[ichan]; - olddiff[ichan]=diff; - change_flag=1; - } -//commented out by dhanya initDACbyIndex(VTHRESH,vthresh, ichan); - } - } - iteration++; - free(scan); - free(scan1); - } -#endif - return retval; -} - - - - - -int trim_with_median(int stop, int im) { - - - int retval=OK; - -#ifdef MCB_FUNCS - int ichan, imod, ichip, ich; - u_int32_t *scan, *scan1; - int *olddiff, *direction; - int med, diff; - int change_flag=1; - int iteration=0; - int me[nModX], me1[nModX]; - int modma, modmi, nm; - int trim; - int *fifodata; - - setFrames(1); - // setNMod(getNModBoard()); - - if (im==ALLMOD){ - modmi=0; - modma=nModX; - } else { - modmi=im; - modma=im+1; - } - nm=modma-modmi; - - olddiff=malloc(4*nModX*nChips*nChans); - direction=malloc(4*nModX*nChips*nChans); - for (imod=modmi; imod0) { - direction[ichan]=1; - } else { - direction[ichan]=-1; - } - } - if ( direction[ichan]!=-3) { - if (abs(diff)>abs(olddiff[ichan])) { - trim=getTrimbit(imod,ichip,ich)+direction[ichan]; - printf("%d old diff %d < new diff %d %d - trimbit %d\n",ichan, olddiff[ichan], diff, direction[ichan], trim); - direction[ichan]=-3; - } else { - trim=getTrimbit(imod,ichip,ich)-direction[ichan]; - olddiff[ichan]=diff; - change_flag=1; - } - if (trim>TRIM_DR) { - trim=63; - printf("can't trim channel %d chip %d module %d to trim %d\n",ich, ichip, imod, trim); - retval=FAIL; - } - if (trim<0) { - printf("can't trim channel %d chip %d module %d to trim %d\n",ich, ichip, imod, trim); - trim=0; - retval=FAIL; - } - initChannel(trim,0,0,1,0,0,imod); - } - } - } - } - iteration++; - free(scan); - free(scan1); - } - free(olddiff); - free(direction); -#endif - return retval; -} diff --git a/slsDetectorSoftware/jungfrauDetectorServer/trimming_funcs.h b/slsDetectorSoftware/jungfrauDetectorServer/trimming_funcs.h deleted file mode 100755 index 42ecea24a..000000000 --- a/slsDetectorSoftware/jungfrauDetectorServer/trimming_funcs.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef TRIMMING_FUNCS_H -#define TRIMMING_FUNCS_H - -#include "sls_detector_defs.h" - -int trim_fixed_settings(int countlim, int par2, int imod); -int trim_with_noise(int countlim, int nsigma, int imod); -int trim_with_beam(int countlim, int nsigma, int imod); -int trim_improve(int maxit, int par2, int imod); -int calcthr_from_vcal(int vcal); -int calccal_from_vthr(int vthr); -int choose_vthresh_and_vtrim(int countlim, int nsigma, int imod); - -int choose_vthresh(); -int trim_with_level(int countlim, int imod); -int trim_with_median(int stop, int imod); -int calcthr_from_vcal(int vcal); -int calccal_from_vthr(int vthr); - -#endif diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp index 580b0c359..1a98955c0 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp @@ -1143,6 +1143,7 @@ slsDetectorDefs::detectorSettings multiSlsDetector::setSettings(detectorSettings } thisMultiDetector->currentSettings=ret1; + return ret1; } @@ -1255,6 +1256,7 @@ int multiSlsDetector::stopAcquisition(){ } } *stoppedFlag=1; + setAcquiringFlag(false); return ret1; @@ -1628,6 +1630,15 @@ int64_t multiSlsDetector::setTimer(timerIndex index, int64_t t){ thisMultiDetector->timerValue[index]=ret1; + + if (getDetectorsType() == EIGER) { + //if subexptime set, update rate correction in server and update result in multi client + double r; + if((index == SUBFRAME_ACQUISITION_TIME) && (t>=0) && getRateCorrection(r)){ + setRateCorrection(r); + } + } + return ret1; }; @@ -2413,36 +2424,55 @@ int multiSlsDetector::flatFieldCorrect(double* datain, double *errin, double* da int multiSlsDetector::setRateCorrection(double t){ - // double tdead[]=defaultTDead; + // double tdead[]=defaultTDead; - if (t==0) { - thisMultiDetector->correctionMask&=~(1<correctionMask|=(1<numberOfDetectors; idet++) { - - if (detectors[idet]) { - detectors[idet]->setRateCorrection(t); - if(detectors[idet]->getErrorMask()) - setErrorMask(getErrorMask()|(1<tDead << std::endl; + std::cout<< "Setting rate correction with dead time "<< thisMultiDetector->tDead << std::endl; #endif - } - return thisMultiDetector->correctionMask&(1<correctionMask&=~(1<correctionMask&(1<correctionMask|=(1<numberOfDetectors; idet++) { + if (detectors[idet]) { + ret=detectors[idet]->setRateCorrection(t); + if(detectors[idet]->getErrorMask()) + setErrorMask(getErrorMask()|(1<correctionMask&=~(1<correctionMask|=(1<correctionMask&(1<correctionMask&(1<tDead << std::endl; #endif - //which t should we return if they are all different? return 1; } else t=0; @@ -2455,21 +2485,35 @@ int multiSlsDetector::getRateCorrection(double &t){ double multiSlsDetector::getRateCorrectionTau(){ double ret1=-100,ret; + for (int idet=0; idetnumberOfDetectors; idet++) { + if (detectors[idet]) { + ret=detectors[idet]->getRateCorrectionTau(); + if (ret1==-100) + ret1=ret; + else if (ret!=ret1){ + std::cout<< "Rate correction is different for different readouts " << std::endl; + if(getDetectorsType() == EIGER) + ret1=-2; + else + ret1=-1; //same settings also return -1 + } + } + } + + if (getDetectorsType() != MYTHEN){ + //if set by the slsDetector + if(ret1 == 0) + thisMultiDetector->correctionMask&=~(1<correctionMask|=(1<correctionMask&(1<tDead << std::endl; #endif - //which t should we return if they are all different? - - for (int idet=0; idetnumberOfDetectors; idet++) { - if (detectors[idet]) { - ret=detectors[idet]->getRateCorrectionTau(); - if (ret1==-100) - ret1=ret; - else if (ret!=ret1) - ret1=-1; - } - } } else { #ifdef VERBOSE std::cout<< "Rate correction is disabled " << std::endl; @@ -4617,7 +4661,6 @@ int* multiSlsDetector::readFrameFromReceiver(char* fName, int &acquisitionIndex acquisitionIndex = -1; return NULL; } - int n,complete=OK; int i,k,offsetX, offsetY, maxX, maxY; double dr; int* retval=new int[nel]; diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 8b0581c8b..641520b18 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -72,6 +72,14 @@ int slsDetector::initSharedMemory(detectorType type, int id) { ng=0; no=0; break; + case JUNGFRAU: + nch=256*256; + nm=1; //modules/detector + nc=8; //chips + nd=16; //dacs+adcs + ng=0; + no=0; + break; case JUNGFRAUCTB: nch=32; nm=1; //modules/detector @@ -612,6 +620,19 @@ int slsDetector::initializeDetectorSize(detectorType type) { thisDetector->nModMax[Y]=1; thisDetector->dynamicRange=16; break; + case JUNGFRAU: + thisDetector->nChan[X]=256; + thisDetector->nChan[Y]=256; + thisDetector->nChip[X]=4; + thisDetector->nChip[Y]=2; + thisDetector->nDacs=16; + thisDetector->nAdcs=0; + thisDetector->nGain=0; + thisDetector->nOffset=0; + thisDetector->nModMax[X]=1; + thisDetector->nModMax[Y]=1; + thisDetector->dynamicRange=16; + break; case JUNGFRAUCTB: thisDetector->nChan[X]=32; thisDetector->nChan[Y]=1; @@ -837,6 +858,8 @@ int slsDetector::initializeDetectorSize(detectorType type) { setFramesPerFile(MAX_FRAMES_PER_FILE); if (thisDetector->myDetectorType==MOENCH) setFramesPerFile(MOENCH_MAX_FRAMES_PER_FILE); + if (thisDetector->myDetectorType==JUNGFRAU) + setFramesPerFile(JFRAU_MAX_FRAMES_PER_FILE); if (thisDetector->myDetectorType==JUNGFRAUCTB) setFramesPerFile(JFCTB_MAX_FRAMES_PER_FILE); thisReceiver = new receiverInterface(dataSocket); @@ -972,6 +995,13 @@ slsDetectorDefs::sls_detector_module* slsDetector::createModule(detectorType t) nd=8; //dacs na=1; break; + case JUNGFRAU: + nch=256*256;//32; + nm=1; + nc=4*2; + nd=16; // dacs+adcs + na=0; + break; case JUNGFRAUCTB: nch=32;//32; nm=1; @@ -1054,16 +1084,20 @@ int slsDetector::sendModule(sls_detector_module *myMod) { ts+=controlSocket->SendDataOnly(&(myMod->reg),sizeof(myMod->reg)); ts+=controlSocket->SendDataOnly(myMod->dacs,sizeof(myMod->ndac)); ts+=controlSocket->SendDataOnly(myMod->adcs,sizeof(myMod->nadc)); - ts+=controlSocket->SendDataOnly(myMod->chipregs,sizeof(myMod->nchip)); - ts+=controlSocket->SendDataOnly(myMod->chanregs,sizeof(myMod->nchan)); - ts+=controlSocket->SendDataOnly(&(myMod->gain), sizeof(myMod->gain)); - ts+=controlSocket->SendDataOnly(&(myMod->offset), sizeof(myMod->offset)); + if(thisDetector->myDetectorType != JUNGFRAU){ + ts+=controlSocket->SendDataOnly(myMod->chipregs,sizeof(myMod->nchip)); + ts+=controlSocket->SendDataOnly(myMod->chanregs,sizeof(myMod->nchan)); + } + ts+=controlSocket->SendDataOnly(&(myMod->gain),sizeof(myMod->gain)); + ts+=controlSocket->SendDataOnly(&(myMod->offset), sizeof(myMod->offset)); ts+=controlSocket->SendDataOnly(myMod->dacs,sizeof(dacs_t)*(myMod->ndac)); ts+=controlSocket->SendDataOnly(myMod->adcs,sizeof(dacs_t)*(myMod->nadc)); - ts+=controlSocket->SendDataOnly(myMod->chipregs,sizeof(int)*(myMod->nchip)); - ts+=controlSocket->SendDataOnly(myMod->chanregs,sizeof(int)*(myMod->nchan)); + if(thisDetector->myDetectorType != JUNGFRAU){ + ts+=controlSocket->SendDataOnly(myMod->chipregs,sizeof(int)*(myMod->nchip)); + ts+=controlSocket->SendDataOnly(myMod->chanregs,sizeof(int)*(myMod->nchan)); + } return ts; } @@ -1118,16 +1152,19 @@ int slsDetector::receiveModule(sls_detector_module* myMod) { ts+=controlSocket->ReceiveDataOnly(&(myMod->reg),sizeof(myMod->reg)); ts+=controlSocket->ReceiveDataOnly(myMod->dacs,sizeof(myMod->ndac)); ts+=controlSocket->ReceiveDataOnly(myMod->adcs,sizeof(myMod->nadc)); - ts+=controlSocket->ReceiveDataOnly(myMod->chipregs,sizeof(myMod->nchip)); - ts+=controlSocket->ReceiveDataOnly(myMod->chanregs,sizeof(myMod->nchan)); + + if(thisDetector->myDetectorType != JUNGFRAU){ + ts+=controlSocket->ReceiveDataOnly(myMod->chipregs,sizeof(myMod->nchip)); + ts+=controlSocket->ReceiveDataOnly(myMod->chanregs,sizeof(myMod->nchan)); + } ts+=controlSocket->ReceiveDataOnly(&(myMod->gain), sizeof(myMod->gain)); ts+=controlSocket->ReceiveDataOnly(&(myMod->offset), sizeof(myMod->offset)); - myMod->dacs=dacptr; - myMod->adcs=adcptr; - myMod->chipregs=chipptr; - myMod->chanregs=chanptr; + myMod->dacs=dacptr; + myMod->adcs=adcptr; + myMod->chipregs=chipptr; + myMod->chanregs=chanptr; #ifdef VERBOSE std::cout<< "received module " << myMod->module << " of size "<< ts << " register " << myMod->reg << std::endl; @@ -1140,17 +1177,20 @@ int slsDetector::receiveModule(sls_detector_module* myMod) { #ifdef VERBOSE std::cout<< "received adcs " << myMod->module << " of size "<< ts << std::endl; #endif - ts+=controlSocket->ReceiveDataOnly(myMod->chipregs,sizeof(int)*(myMod->nchip)); -#ifdef VERBOSE - std::cout<< "received chips " << myMod->module << " of size "<< ts << std::endl; -#endif - ts+=controlSocket->ReceiveDataOnly(myMod->chanregs,sizeof(int)*(myMod->nchan)); -#ifdef VERBOSE - std::cout<< "nchans= " << thisDetector->nChans << " nchips= " << thisDetector->nChips; - std::cout<< "mod - nchans= " << myMod->nchan << " nchips= " <nchip; - std::cout<< "received chans " << myMod->module << " of size "<< ts << std::endl; + if(thisDetector->myDetectorType != JUNGFRAU){ + ts+=controlSocket->ReceiveDataOnly(myMod->chipregs,sizeof(int)*(myMod->nchip)); +#ifdef VERBOSE + std::cout<< "received chips " << myMod->module << " of size "<< ts << std::endl; #endif + ts+=controlSocket->ReceiveDataOnly(myMod->chanregs,sizeof(int)*(myMod->nchan)); +#ifdef VERBOSE + std::cout<< "nchans= " << thisDetector->nChans << " nchips= " << thisDetector->nChips; + std::cout<< "mod - nchans= " << myMod->nchan << " nchips= " <nchip; + std::cout<< "received chans " << myMod->module << " of size "<< ts << std::endl; +#endif + } + #ifdef VERBOSE std::cout<< "received module " << myMod->module << " of size "<< ts << " register " << myMod->reg << std::endl; #endif @@ -2176,6 +2216,11 @@ dacs_t slsDetector::setDAC(dacs_t val, dacIndex index, int mV, int imod){ int ret=FAIL; char mess[100]; int arg[3]; + + + if ( (thisDetector->myDetectorType != GOTTHARD) && (thisDetector->myDetectorType != PROPIX) && index==HV_POT) + index=HV_NEW; + arg[0]=index; arg[1]=imod; arg[2]=mV; @@ -2618,6 +2663,7 @@ int slsDetector::setModule(int reg, int imod){ sls_detector_module myModule; int* g=0; int* o=0; + int* iod=0; #ifdef VERBOSE std::cout << "slsDetector set module " << std::endl; @@ -2686,14 +2732,14 @@ int slsDetector::setModule(int reg, int imod){ ads[i]=-1; myModule.adcs=ads; } - ret=setModule(myModule,g,o); + ret=setModule(myModule,g,o,iod); } return ret; }; -int slsDetector::setModule(sls_detector_module module, int* gainval, int* offsetval){ +int slsDetector::setModule(sls_detector_module module, int* gainval, int* offsetval, int* iodelay){ int fnum=F_SET_MODULE; int retval; @@ -2717,6 +2763,8 @@ int slsDetector::setModule(sls_detector_module module, int* gainval, int* offset controlSocket->SendDataOnly(gainval,sizeof(int)*thisDetector->nGain); if((thisDetector->nOffset) && (offsetval)) controlSocket->SendDataOnly(offsetval,sizeof(int)*thisDetector->nOffset); + if(thisDetector->myDetectorType == EIGER) + controlSocket->SendDataOnly(iodelay,sizeof(int)); controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); if (ret!=FAIL) { @@ -2731,6 +2779,13 @@ int slsDetector::setModule(sls_detector_module module, int* gainval, int* offset } } + if(ret == FAIL && thisDetector->myDetectorType == EIGER && strcasestr(mess,"Rate")){ + setErrorMask((getErrorMask())|(COULD_NOT_SET_RATE_CORRECTION)); + if(strcasestr(mess,"tau/subexptime")) + setErrorMask((getErrorMask())|(RATE_CORRECTION_TAU_SUBEXPOSURE)); + thisDetector->correctionMask&=~(1<tDead = 0; + } if (ret!=FAIL) { if (detectorModules) { @@ -2744,24 +2799,27 @@ int slsDetector::setModule(sls_detector_module module, int* gainval, int* offset thisDetector->nDacs=module.ndac; thisDetector->nAdcs=module.nadc; - for (int ichip=0; ichipnChips; ichip++) { - if (chipregs) - chipregs[ichip+thisDetector->nChips*imod]=module.chipregs[ichip]; + if(thisDetector->myDetectorType != JUNGFRAU){ + for (int ichip=0; ichipnChips; ichip++) { + if (chipregs) + chipregs[ichip+thisDetector->nChips*imod]=module.chipregs[ichip]; - if (chanregs) { - for (int i=0; inChans; i++) { - chanregs[i+ichip*thisDetector->nChans+thisDetector->nChips*thisDetector->nChans*imod]=module.chanregs[ichip*thisDetector->nChans+i]; - } - } + if (chanregs) { + for (int i=0; inChans; i++) { + chanregs[i+ichip*thisDetector->nChans+thisDetector->nChips*thisDetector->nChans*imod]=module.chanregs[ichip*thisDetector->nChans+i]; + } + } + } + if (adcs) { + for (int i=0; inAdcs; i++) + adcs[i+imod*thisDetector->nAdcs]=module.adcs[i]; + } } + if (dacs) { for (int i=0; inDacs; i++) dacs[i+imod*thisDetector->nDacs]=module.dacs[i]; } - if (adcs) { - for (int i=0; inAdcs; i++) - adcs[i+imod*thisDetector->nAdcs]=module.adcs[i]; - } (detectorModules+imod)->gain=module.gain; (detectorModules+imod)->offset=module.offset; @@ -2869,25 +2927,28 @@ slsDetectorDefs::sls_detector_module *slsDetector::getModule(int imod){ thisDetector->nDacs=myMod->ndac; thisDetector->nAdcs=myMod->nadc; - for (int ichip=0; ichipnChips; ichip++) { - if (chipregs) - chipregs[ichip+thisDetector->nChips*imod]=myMod->chipregs[ichip]; + if(thisDetector->myDetectorType != JUNGFRAU){ + for (int ichip=0; ichipnChips; ichip++) { + if (chipregs) + chipregs[ichip+thisDetector->nChips*imod]=myMod->chipregs[ichip]; - if (chanregs) { - for (int i=0; inChans; i++) { - chanregs[i+ichip*thisDetector->nChans+thisDetector->nChips*thisDetector->nChans*imod]=myMod->chanregs[ichip*thisDetector->nChans+i]; + if (chanregs) { + for (int i=0; inChans; i++) { + chanregs[i+ichip*thisDetector->nChans+thisDetector->nChips*thisDetector->nChans*imod]=myMod->chanregs[ichip*thisDetector->nChans+i]; + } } } + + if (adcs) { + for (int i=0; inAdcs; i++) + adcs[i+imod*thisDetector->nAdcs]=myMod->adcs[i]; + } } + if (dacs) { for (int i=0; inDacs; i++) dacs[i+imod*thisDetector->nDacs]=myMod->dacs[i]; } - if (adcs) { - for (int i=0; inAdcs; i++) - adcs[i+imod*thisDetector->nAdcs]=myMod->adcs[i]; - } - (detectorModules+imod)->gain=myMod->gain; (detectorModules+imod)->offset=myMod->offset; (detectorModules+imod)->serialnumber=myMod->serialnumber; @@ -3063,10 +3124,13 @@ slsDetectorDefs::detectorSettings slsDetector::setSettings( detectorSettings ise string ssettings; int* gainval=0, *offsetval=0; + int* iodelay=0; if(thisDetector->nGain) gainval=new int[thisDetector->nGain]; if(thisDetector->nOffset) offsetval=new int[thisDetector->nOffset]; + if(thisDetector->myDetectorType == EIGER) + iodelay = new int; int ret=0; @@ -3098,6 +3162,7 @@ slsDetectorDefs::detectorSettings slsDetector::setSettings( detectorSettings ise case DYNAMICGAIN: if ((thisDetector->myDetectorType == GOTTHARD) || (thisDetector->myDetectorType == PROPIX) || + (thisDetector->myDetectorType == JUNGFRAU) || (thisDetector->myDetectorType == MOENCH)) { ssettings="/dynamicgain"; thisDetector->currentSettings=DYNAMICGAIN; @@ -3130,6 +3195,36 @@ slsDetectorDefs::detectorSettings slsDetector::setSettings( detectorSettings ise break; case LOWNOISE: break; + case DYNAMICHG0: + if (thisDetector->myDetectorType == JUNGFRAU) { + ssettings="/dynamichg0"; + thisDetector->currentSettings=DYNAMICHG0; + } + break; + case FIXGAIN1: + if (thisDetector->myDetectorType == JUNGFRAU) { + ssettings="/fixgain1"; + thisDetector->currentSettings=FIXGAIN1; + } + break; + case FIXGAIN2: + if (thisDetector->myDetectorType == JUNGFRAU) { + ssettings="/fixgain2"; + thisDetector->currentSettings=FIXGAIN2; + } + break; + case FORCESWITCHG1: + if (thisDetector->myDetectorType == JUNGFRAU) { + ssettings="/forceswitchg1"; + thisDetector->currentSettings=FORCESWITCHG1; + } + break; + case FORCESWITCHG2: + if (thisDetector->myDetectorType == JUNGFRAU) { + ssettings="/forceswitchg2"; + thisDetector->currentSettings=FORCESWITCHG2; + } + break; default: break; } @@ -3164,6 +3259,7 @@ slsDetectorDefs::detectorSettings slsDetector::setSettings( detectorSettings ise case MOENCH: case GOTTHARD: case PROPIX: + case JUNGFRAU: case JUNGFRAUCTB: //settings is saved in myMod.reg myMod->reg=thisDetector->currentSettings; @@ -3184,13 +3280,14 @@ slsDetectorDefs::detectorSettings slsDetector::setSettings( detectorSettings ise #ifdef VERBOSE cout << "the settings file name is "<myDetectorType, myMod)) { + if (!readSettingsFile(settingsfname,thisDetector->myDetectorType, myMod,iodelay)) { //if it didnt open, try default settings file ostringstream ostfn_default; switch(thisDetector->myDetectorType){ case MOENCH: case GOTTHARD: case PROPIX: + case JUNGFRAU: case JUNGFRAUCTB: ostfn_default << thisDetector->settingsDir << ssettings << ssettings << ".settings"; break; @@ -3203,7 +3300,7 @@ slsDetectorDefs::detectorSettings slsDetector::setSettings( detectorSettings ise #ifdef VERBOSE cout << settingsfname << endl; #endif - if (!readSettingsFile(settingsfname,thisDetector->myDetectorType, myMod)) { + if (!readSettingsFile(settingsfname,thisDetector->myDetectorType, myMod,iodelay)) { //if default doesnt work, return error std::cout << "Could not open settings file" << endl; setErrorMask((getErrorMask())|(SETTINGS_FILE_NOT_OPEN)); @@ -3248,7 +3345,7 @@ slsDetectorDefs::detectorSettings slsDetector::setSettings( detectorSettings ise } //if everything worked, set module**** - setModule(*myMod,gainval,offsetval); + setModule(*myMod,gainval,offsetval,iodelay); } } @@ -3342,6 +3439,7 @@ int slsDetector::updateDetectorNoWait() { if((thisDetector->myDetectorType!= GOTTHARD)&& (thisDetector->myDetectorType!= PROPIX)&& + (thisDetector->myDetectorType!= JUNGFRAU)&& (thisDetector->myDetectorType!= MOENCH)){ //thr=getThresholdEnergy(); n = controlSocket->ReceiveDataOnly( &thr,sizeof(thr)); @@ -3600,7 +3698,7 @@ int* slsDetector::getDataFromDetector(int *retval){ std::cout<< "Received "<< n << " data bytes" << std::endl; #endif if (n!=thisDetector->dataBytes) { - std::cout<< "wrong data size received: received " << n << " but expected " << thisDetector->dataBytes << std::endl; + std::cout<< "wrong data size received from detector: received " << n << " but expected " << thisDetector->dataBytes << std::endl; thisDetector->stoppedFlag=1; ret=FAIL; if (r==NULL) { @@ -3779,123 +3877,120 @@ int slsDetector::startAndReadAllNoWait(){ int64_t slsDetector::setTimer(timerIndex index, int64_t t){ - int fnum=F_SET_TIMER,fnum2=F_SET_RECEIVER_TIMER; - int64_t retval = -1; - int64_t ut = -2; - char mess[100]; - int ret=OK; - int n=0; + int fnum=F_SET_TIMER,fnum2=F_SET_RECEIVER_TIMER; + int64_t retval = -1; + int64_t ut = -2; + char mess[100]; + int ret=OK; + int n=0; - if (index!=MEASUREMENTS_NUMBER) { + if (index!=MEASUREMENTS_NUMBER) { #ifdef VERBOSE - std::cout<< "Setting timer "<< index << " to " << t << "ns/value" << std::endl; + std::cout<< "Setting timer "<< index << " to " << t << "ns/value" << std::endl; #endif - if (thisDetector->onlineFlag==ONLINE_FLAG) { - if (connectControl() == OK){ - controlSocket->SendDataOnly(&fnum,sizeof(fnum)); - controlSocket->SendDataOnly(&index,sizeof(index)); - n=controlSocket->SendDataOnly(&t,sizeof(t)); - controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); - if (ret==FAIL) { - controlSocket->ReceiveDataOnly(mess,sizeof(mess)); - std::cout<< "Detector returned error: " << mess << std::endl; - setErrorMask((getErrorMask())|(DETECTOR_TIMER_VALUE_NOT_SET)); + if (thisDetector->onlineFlag==ONLINE_FLAG) { + if (connectControl() == OK){ + controlSocket->SendDataOnly(&fnum,sizeof(fnum)); + controlSocket->SendDataOnly(&index,sizeof(index)); + n=controlSocket->SendDataOnly(&t,sizeof(t)); + controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); + if (ret==FAIL) { + controlSocket->ReceiveDataOnly(mess,sizeof(mess)); + std::cout<< "Detector returned error: " << mess << std::endl; + setErrorMask((getErrorMask())|(DETECTOR_TIMER_VALUE_NOT_SET)); + } else { + controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); + thisDetector->timerValue[index]=retval; + } + disconnectControl(); + if (ret==FORCE_UPDATE) { + updateDetector(); +#ifdef VERBOSE + std::cout<< "Updated!" << std::endl; +#endif + + } + } + } else { + //std::cout<< "offline " << std::endl; + if (t>=0) + thisDetector->timerValue[index]=t; + if((thisDetector->myDetectorType==GOTTHARD)|| + (thisDetector->myDetectorType==PROPIX)|| + (thisDetector->myDetectorType==JUNGFRAU)|| + (thisDetector->myDetectorType==MOENCH)) + thisDetector->timerValue[PROBES_NUMBER]=0; + } } else { - controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); - thisDetector->timerValue[index]=retval; + if (t>=0) + thisDetector->timerValue[index]=t; } - disconnectControl(); - if (ret==FORCE_UPDATE) { - updateDetector(); #ifdef VERBOSE - std::cout<< "Updated!" << std::endl; + std::cout<< "Timer " << index << " set to "<< thisDetector->timerValue[index] << "ns" << std::endl; #endif + if ((thisDetector->myDetectorType==MYTHEN)&&(index==PROBES_NUMBER)) { + setDynamicRange(); + //cout << "Changing probes: data size = " << thisDetector->dataBytes <=0) - thisDetector->timerValue[index]=t; - if((thisDetector->myDetectorType==GOTTHARD)|| - (thisDetector->myDetectorType==PROPIX)|| - (thisDetector->myDetectorType==MOENCH)) - thisDetector->timerValue[PROBES_NUMBER]=0; - } - } else { - if (t>=0) - thisDetector->timerValue[index]=t; - } + + /* set progress */ + if ((index==FRAME_NUMBER) || (index==CYCLES_NUMBER)) { + setTotalProgress(); + } + + + + //send acquisiton period/frame number to receiver + if((index==FRAME_NUMBER)||(index==FRAME_PERIOD)||(index==CYCLES_NUMBER)){ + if(ret != FAIL){ + if(setReceiverOnline(ONLINE_FLAG)==ONLINE_FLAG){ + int64_t args[2]; + args[1] = retval; + if(t == -1) args[1] = -1; + + if((index==FRAME_NUMBER)||(index==CYCLES_NUMBER)){ #ifdef VERBOSE - std::cout<< "Timer " << index << " set to "<< thisDetector->timerValue[index] << "ns" << std::endl; + std::cout << "Setting/Getting number of frames " << index <<" to/from receiver " << args[1] << std::endl; #endif - - if ((thisDetector->myDetectorType==MYTHEN)&&(index==PROBES_NUMBER)) { - setDynamicRange(); - //cout << "Changing probes: data size = " << thisDetector->dataBytes <timerValue[FRAME_NUMBER]*thisDetector->timerValue[CYCLES_NUMBER]); + if(args[1] != -1) args[1]=retval; + }else{ #ifdef VERBOSE - - std::cout << "Setting/Getting number of frames " << index <<" to/from receiver " << args[1] << std::endl; + std::cout << "Setting/Getting acquisition period " << index << " to/from receiver " << args[1] << std::endl; #endif - args[0] = FRAME_NUMBER; - }else{ -#ifdef VERBOSE - std::cout << "Setting/Getting acquisition period " << index << " to/from receiver " << args[1] << std::endl; -#endif - args[0] = FRAME_PERIOD; - //if acquisition period is zero, then #frames/buffer depends on exposure time and not acq period - if(!retval) - args[1] = timerValue[ACQUISITION_TIME]; - } + args[0] = FRAME_PERIOD; + //if acquisition period is zero, then #frames/buffer depends on exposure time and not acq period + if(!retval) args[1] = timerValue[ACQUISITION_TIME]; + } - if (connectData() == OK) - ret=thisReceiver->sendIntArray(fnum2,ut,args); - disconnectData(); - if((ut != retval)|| (ret==FAIL)){ - ret = FAIL; - if(index==FRAME_PERIOD){ - //exptime sent if acq period = 0 - if(retval){ - cout << "ERROR:Acquisition Period in receiver set incorrectly to " << ut << " instead of " << retval << endl; - setErrorMask((getErrorMask())|(RECEIVER_ACQ_PERIOD_NOT_SET)); - } - }else{ - cout << "ERROR:Number of Frames in receiver set incorrectly to " << ut << " instead of " << retval << endl; - setErrorMask((getErrorMask())|(RECEIVER_FRAME_NUM_NOT_SET)); - } - } - - if(ret==FORCE_UPDATE) - updateReceiver(); - } - } -} - return thisDetector->timerValue[index]; + if (connectData() == OK) + ret=thisReceiver->sendIntArray(fnum2,ut,args); + disconnectData(); + if((ut != retval)|| (ret==FAIL)){ + ret = FAIL; + if(index==FRAME_PERIOD){ + //exptime sent if acq period = 0 + if(retval){ + cout << "ERROR:Acquisition Period in receiver set incorrectly to " << ut << " instead of " << retval << endl; + setErrorMask((getErrorMask())|(RECEIVER_ACQ_PERIOD_NOT_SET)); + } + }else{ + cout << "ERROR:Number of Frames (* Number of cycles) in receiver set incorrectly to " << ut << " instead of " << retval << endl; + setErrorMask((getErrorMask())|(RECEIVER_FRAME_NUM_NOT_SET)); + } + } + if(ret==FORCE_UPDATE) + updateReceiver(); + } + } + } + return thisDetector->timerValue[index]; }; int slsDetector::lockServer(int lock) { @@ -4890,28 +4985,79 @@ int slsDetector::flatFieldCorrect(double* datain, double *errin, double* dataout }; int slsDetector::setRateCorrection(double t){ - double tdead[]=defaultTDead; - if (t==0) { + if (getDetectorsType() == MYTHEN){ + double tdead[]=defaultTDead; + if (t==0) { #ifdef VERBOSE - std::cout<< "unsetting rate correction" << std::endl; + std::cout<< "unsetting rate correction" << std::endl; #endif - thisDetector->correctionMask&=~(1<correctionMask|=(1<0) - thisDetector->tDead=t; - else { - if (thisDetector->currentSettings<3 && thisDetector->currentSettings>-1) - thisDetector->tDead=tdead[thisDetector->currentSettings]; - else - thisDetector->tDead=0; - } + thisDetector->correctionMask&=~(1<correctionMask|=(1<0) + thisDetector->tDead=t; + else { + if (thisDetector->currentSettings<3 && thisDetector->currentSettings>-1) + thisDetector->tDead=tdead[thisDetector->currentSettings]; + else + thisDetector->tDead=0; + } #ifdef VERBOSE - std::cout<< "Setting rate correction with dead time "<< thisDetector->tDead << std::endl; + std::cout<< "Setting rate correction with dead time "<< thisDetector->tDead << std::endl; #endif - } - return thisDetector->correctionMask&(1<correctionMask&(1<SendDataOnly(&fnum,sizeof(fnum)); + controlSocket->SendDataOnly(&arg,sizeof(arg)); + controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); + if (ret!=FAIL) { + controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); + if(retval<0){ + thisDetector->correctionMask&=~(1<tDead = 0; + }else{ + if(retval>0){ + thisDetector->correctionMask|=(1<tDead = -1; + else + thisDetector->tDead = (double)retval/(double)1e9; + } + else{ + thisDetector->correctionMask&=~(1<tDead = (double)retval/(double)1e9; + } + } + } else { + controlSocket->ReceiveDataOnly(mess,sizeof(mess)); + std::cout<< "Detector returned error: " << mess << std::endl; + setErrorMask((getErrorMask())|(COULD_NOT_SET_RATE_CORRECTION)); + if(strcasestr(mess,"tau/subexptime")) + setErrorMask((getErrorMask())|(RATE_CORRECTION_TAU_SUBEXPOSURE)); + thisDetector->correctionMask&=~(1<tDead = 0; + } + disconnectControl(); + if (ret==FORCE_UPDATE) + updateDetector(); + } + } + + return thisDetector->correctionMask&(1<myDetectorType==GOTTHARD)|| (thisDetector->myDetectorType==PROPIX)|| + (thisDetector->myDetectorType==JUNGFRAU)|| (thisDetector->myDetectorType==MOENCH)) { names[0]= "hostname"; names[1]= "port"; @@ -6019,9 +6166,9 @@ int slsDetector::writeConfigurationFile(ofstream &outfile, int id){ -int slsDetector::writeSettingsFile(string fname, int imod){ +int slsDetector::writeSettingsFile(string fname, int imod, int* iodelay){ - return writeSettingsFile(fname,thisDetector->myDetectorType, detectorModules[imod]); + return writeSettingsFile(fname,thisDetector->myDetectorType, detectorModules[imod], iodelay); }; @@ -6032,6 +6179,7 @@ int slsDetector::loadSettingsFile(string fname, int imod) { sls_detector_module *myMod=NULL; int* gainval=0; int* offsetval=0; + int *iodelay=0; if(thisDetector->nGain){ gainval=new int[thisDetector->nGain]; for(int i=0;inGain;i++) @@ -6042,6 +6190,8 @@ int slsDetector::loadSettingsFile(string fname, int imod) { for(int i=0;inOffset;i++) offsetval[i] = -1; } + if(thisDetector->myDetectorType == EIGER) + iodelay = new int;*iodelay=0; string fn=fname; fn=fname; int mmin=0, mmax=setNumberOfModules(); @@ -6061,13 +6211,14 @@ int slsDetector::loadSettingsFile(string fname, int imod) { ostfn << ".sn" << setfill('0') << setw(3) << dec << getId(DETECTOR_SERIAL_NUMBER, im); fn=ostfn.str(); } - myMod=readSettingsFile(fn, thisDetector->myDetectorType); + myMod=readSettingsFile(fn, thisDetector->myDetectorType,myMod,iodelay); + if (myMod) { myMod->module=im; //settings is saved in myMod.reg for all except mythen if(thisDetector->myDetectorType!=MYTHEN) myMod->reg=thisDetector->currentSettings; - setModule(*myMod,gainval,offsetval); + setModule(*myMod,gainval,offsetval,iodelay); deleteModule(myMod); if(gainval) delete[] gainval; if(offsetval) delete[] offsetval; @@ -6080,9 +6231,9 @@ int slsDetector::loadSettingsFile(string fname, int imod) { int slsDetector::saveSettingsFile(string fname, int imod) { - sls_detector_module *myMod=NULL; int ret=FAIL; + int *iod = 0; int mmin=0, mmax=setNumberOfModules(); if (imod>=0) { @@ -6091,13 +6242,18 @@ int slsDetector::saveSettingsFile(string fname, int imod) { } for (int im=mmin; immyDetectorType == EIGER) + if(thisDetector->myDetectorType == EIGER){ ostfn << fname << ".sn" << setfill('0') << setw(3) << dec << getId(DETECTOR_SERIAL_NUMBER); - else + } else ostfn << fname << ".sn" << setfill('0') << setw(3) << hex << getId(MODULE_SERIAL_NUMBER,im); if ((myMod=getModule(im))) { - ret=writeSettingsFile(ostfn.str(), thisDetector->myDetectorType, *myMod); - deleteModule(myMod); + + if(thisDetector->myDetectorType == EIGER){ + iod = new int; + *iod = (int)setDAC((dacs_t)-1,IO_DELAY,0,-1); + } + ret=writeSettingsFile(ostfn.str(), thisDetector->myDetectorType, *myMod,iod); + deleteModule(myMod); } } return ret; @@ -6148,6 +6304,7 @@ int slsDetector::loadCalibrationFile(string fname, int imod) { string fn=fname; int* gainval=0; int* offsetval=0; + int* iodelay=0; if(thisDetector->nGain){ gainval=new int[thisDetector->nGain]; for(int i=0;inGain;i++) @@ -6179,7 +6336,8 @@ int slsDetector::loadCalibrationFile(string fname, int imod) { } fn=ostfn.str(); if((myMod=getModule(im))){ - + iodelay = new int; + *iodelay = (int)setDAC(-1,IO_DELAY,0); //extra gain and offset if(thisDetector->nGain){ if(readCalibrationFile(fn,gainval, offsetval,thisDetector->myDetectorType)==FAIL) @@ -6189,7 +6347,7 @@ int slsDetector::loadCalibrationFile(string fname, int imod) { if(readCalibrationFile(fn,myMod->gain, myMod->offset)==FAIL) return FAIL; } - setModule(*myMod,gainval,offsetval); + setModule(*myMod,gainval,offsetval,iodelay); deleteModule(myMod); if(gainval) delete[]gainval; @@ -6589,7 +6747,7 @@ int slsDetector::startReceiver(){ setErrorMask((getErrorMask())|(COULDNOT_START_RECEIVER)); } } - if((ret==OK))//&& (thisDetector->myDetectorType != EIGER)) + if((ret==OK) && (thisDetector->myDetectorType != JUNGFRAU)) ret=detectorSendToReceiver(true); return ret; @@ -6603,7 +6761,7 @@ int slsDetector::stopReceiver(){ int ret = FAIL; char mess[] = ""; - if(thisDetector->myDetectorType != EIGER) + if(thisDetector->myDetectorType != EIGER && thisDetector->myDetectorType != JUNGFRAU) detectorSendToReceiver(false); if (setReceiverOnline(ONLINE_FLAG)==ONLINE_FLAG) { @@ -6781,7 +6939,6 @@ int* slsDetector::readFrameFromReceiver(char* fName, int &acquisitionIndex, int int n; char mess[100]="Nothing"; - if (setReceiverOnline(ONLINE_FLAG)==ONLINE_FLAG) { #ifdef VERBOSE std::cout<< "slsDetector: Reading frame from receiver "<< thisDetector->dataBytes << " " <myDetectorType == JUNGFRAU){ + for(unsigned int i=0;igetRunStatus(); return myDet->runStatusType(s); @@ -1941,7 +1941,7 @@ string slsDetectorCommand::cmdRateCorr(int narg, char *args[], int action){ } double t; if (myDet->getRateCorrection(t)) { - sprintf(answer,"%f",t); + sprintf(answer,"%0.9f",t); } else { sprintf(answer,"%f",0.); } @@ -1954,7 +1954,7 @@ string slsDetectorCommand::helpRateCorr(int narg, char *args[], int action){ if (action==GET_ACTION || action==HELP_ACTION) os << string("ratecorr \t returns the dead time used for rate correections in ns \n"); if (action==PUT_ACTION || action==HELP_ACTION) - os << string("ratecorr ns \t sets the deadtime correction constant in ns\n"); + os << string("ratecorr ns \t sets the deadtime correction constant in ns, -1 in Eiger will set it to default tau of settings\n"); return os.str(); } @@ -2247,10 +2247,10 @@ string slsDetectorCommand::cmdCounter(int narg, char *args[], int action){ retval=myDet->resetCounterBlock(ival); } - else if (string(args[0])==string("setctrbit")){ + else if (string(args[0])==string("resmat")){ if (action==PUT_ACTION){ if (!sscanf(args[1],"%d",&ival)) - return string("Could not scan resetctrbit input ")+string(args[1]); + return string("Could not scan resmat input ")+string(args[1]); if(ival>=0) sprintf(answer,"%d",myDet->setCounterBit(ival)); }else @@ -2271,12 +2271,12 @@ string slsDetectorCommand::helpCounter(int narg, char *args[], int action){ if (action==PUT_ACTION || action==HELP_ACTION){ os << "readctr \t Cannot put"<< std::endl; os << "resetctr i \t resets counter in detector, restarts acquisition if i=1"<< std::endl; - os << "setctrbit i \t sets/resets counter bit in detector"<< std::endl; + os << "resmat i \t sets/resets counter bit in detector"<< std::endl; } if (action==GET_ACTION || action==HELP_ACTION){ os << "readctr i fname\t reads counter in detector to file fname, restarts acquisition if i=1"<< std::endl; os << "resetctr \t Cannot get"<< std::endl; - os << "setctrbit i \t gets the counter bit in detector"<< std::endl; + os << "resmat i \t gets the counter bit in detector"<< std::endl; } return os.str(); } @@ -3053,7 +3053,7 @@ string slsDetectorCommand::cmdSettings(int narg, char *args[], int action) { myDet->setOnline(ONLINE_FLAG); if (cmd=="settings") { - if (action==PUT_ACTION) + if (action==PUT_ACTION) myDet->setSettings(myDet->getDetectorSettings(string(args[1]))); return myDet->getDetectorSettings(myDet->getSettings()); } else if (cmd=="threshold") { @@ -3070,7 +3070,7 @@ string slsDetectorCommand::cmdSettings(int narg, char *args[], int action) { string sval=string(args[1]); #ifdef VERBOSE std::cout<< " trimfile " << sval << std::endl; -#endif +#endif if (action==GET_ACTION) { //create file names myDet->saveSettingsFile(sval, -1); @@ -3145,7 +3145,8 @@ string slsDetectorCommand::helpSettings(int narg, char *args[], int action) { ostringstream os; if (action==PUT_ACTION || action==HELP_ACTION) { - os << "settings s \n sets the settings of the detector - can be standard, fast, highgain, dynamicgain, lowgain, mediumgain, veryhighgain"<< std::endl; + os << "settings s \n sets the settings of the detector - can be standard, fast, highgain, dynamicgain, lowgain, mediumgain, veryhighgain" + "lownoise, dynamichg0,fixgain1,fixgain2,forceswitchg1, forceswitchg2"<< std::endl; os << "threshold eV\n sets the detector threshold in eV"<< std::endl; os << "trimbits fname\n loads the trimfile fname to the detector. If no extension is specified, the serial number of each module will be attached."<< std::endl; os << "trim:mode fname\n trims the detector according to mode (can be noise, beam, improve, fix) and saves the resulting trimbits to file fname."<< std::endl; diff --git a/slsDetectorSoftware/slsDetector/slsDetectorUsers.cpp b/slsDetectorSoftware/slsDetector/slsDetectorUsers.cpp index 9cd9e9936..4e554750f 100644 --- a/slsDetectorSoftware/slsDetector/slsDetectorUsers.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetectorUsers.cpp @@ -110,22 +110,24 @@ int slsDetectorUsers::getPositions(double *pos){ } int slsDetectorUsers::setDetectorSize(int x0, int y0, int nx, int ny){ + if(myDetector->getTotalNumberOfChannels(slsDetectorDefs::Y)>1) + return 1; int nmod=nx/(myDetector->getChansPerMod(0)); cout << myDetector->getChansPerMod(0) << " " << nx << " " << nmod << endl; return myDetector->setNumberOfModules(nmod)*myDetector->getChansPerMod(0);} int slsDetectorUsers::getDetectorSize(int &x0, int &y0, int &nx, int &ny){ y0=0; - ny=1; x0=0; - nx=myDetector->setNumberOfModules()*myDetector->getChansPerMod(0); - return nx; + nx=myDetector->getTotalNumberOfChannels(slsDetectorDefs::X); + ny=myDetector->getTotalNumberOfChannels(slsDetectorDefs::Y); + return nx*ny; } int slsDetectorUsers::getMaximumDetectorSize(int &nx, int &ny){ - ny=1; - nx=myDetector->getMaxNumberOfModules()*myDetector->getChansPerMod(0); - return nx; + nx=myDetector->getMaxNumberOfChannelsPerDetector(slsDetectorDefs::X); + ny=myDetector->getMaxNumberOfChannelsPerDetector(slsDetectorDefs::Y); + return nx*ny; } int slsDetectorUsers::setBitDepth(int i){ diff --git a/slsDetectorSoftware/slsDetector/slsDetectorUtils.cpp b/slsDetectorSoftware/slsDetector/slsDetectorUtils.cpp index 3b546efb1..d390c8398 100644 --- a/slsDetectorSoftware/slsDetector/slsDetectorUtils.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetectorUtils.cpp @@ -44,14 +44,12 @@ slsDetectorUtils::slsDetectorUtils() { int slsDetectorUtils::acquire(int delflag){ //ensure acquire isnt started multiple times by same client - pthread_mutex_lock(&mp); if(getAcquiringFlag() == false) setAcquiringFlag(true); else{ std::cout << "Error: Acquire has already been started." << std::endl; return FAIL; } - pthread_mutex_unlock(&mp); bool receiver = (setReceiverOnline()==ONLINE_FLAG); @@ -157,10 +155,6 @@ int slsDetectorUtils::acquire(int delflag){ *stoppedFlag=1; } - //resets frames caught in receiver - resetFramesCaught(); - - if(setReceiverOnline()==OFFLINE_FLAG) *stoppedFlag=1; } @@ -175,7 +169,10 @@ int slsDetectorUtils::acquire(int delflag){ cout << " starting thread " << endl; #endif - + //resets frames caught in receiver + if(receiver){ + resetFramesCaught(); + } for(int im=0;imdacs,sizeof(dacs_t)*(myMod->ndac)); + infile.read((char*) iodelay,sizeof(iodelay)); infile.read((char*) myMod->chanregs,sizeof(int)*(myMod->nchan)); #ifdef VERBOSE for(int i=0;indac;i++) std::cout << "dac " << i << ":" << myMod->dacs[i] << std::endl; + std::cout << "iodelay:" << *iodelay << std::endl; #endif if(infile.eof()){ cout< If it is NULL a new module structure will be created + \param iodelay io delay (detector specific) \returns the pointer to myMod or NULL if reading the file failed */ - sls_detector_module* readSettingsFile(string fname, detectorType myDetectorType, sls_detector_module* myMod=NULL); + sls_detector_module* readSettingsFile(string fname, detectorType myDetectorType, sls_detector_module* myMod=NULL, int* iodelay=0); /** writes a trim/settings file \param fname name of the file to be written \param myDetectorType detector type (needed for number of channels, chips, dacs etc.) \param mod module structure which has to be written to file + \param iodelay io delay (detector specific) \returns OK or FAIL if the file could not be written \sa ::sls_detector_module mythenDetector::writeSettingsFile(string, sls_detector_module) */ - int writeSettingsFile(string fname, detectorType myDetectorType, sls_detector_module mod); + int writeSettingsFile(string fname, detectorType myDetectorType, sls_detector_module mod, int* iodelay=0); /** allocates the momery for a detector module structure \param myDetectorType detector type (needed for number of channels, chips, dacs etc.) diff --git a/slsDetectorSoftware/slsDetectorAnalysis/postProcessing.cpp b/slsDetectorSoftware/slsDetectorAnalysis/postProcessing.cpp index a174bc637..8dbc0195e 100644 --- a/slsDetectorSoftware/slsDetectorAnalysis/postProcessing.cpp +++ b/slsDetectorSoftware/slsDetectorAnalysis/postProcessing.cpp @@ -137,7 +137,7 @@ void postProcessing::processFrame(int *myData, int delflag, int jctb) { // cout << "callback arg "<< getCurrentProgress()<< " " << (fname+string(".raw")).c_str() << " " << getTotalNumberOfChannels() << endl; // cout << "DATAREADY 1" <=0) ret=OK; @@ -1861,15 +1870,29 @@ int set_module(int file_des) { ret=FAIL; -//#ifdef VERBOSE +#ifdef VERBOSE printf("module number is %d,register is %d, nchan %d, nchip %d, ndac %d, nadc %d, gain %f, offset %f\n",myModule.module, myModule.reg, myModule.nchan, myModule.nchip, myModule.ndac, myModule.nadc, myModule.gain,myModule.offset); #ifdef EIGERD + int i; for(i=0;i MAX_SUBFRAME_EXPOSURE_VAL ){ + if (tns > (MAX_SUBFRAME_EXPOSURE_VAL_IN_10NS*10) ){ ret=FAIL; strcpy(mess,"Sub Frame exposure time should not exceed 5.368 seconds\n"); break; @@ -3815,3 +3857,72 @@ int pulse_chip(int file_des) { } + + + +int rate_correct(int file_des) { + int64_t tau_ns=-1; + int n; + int64_t retval=-1; + int ret=OK,ret1=OK; + + sprintf(mess,"can't set/unset rate correction\n"); + + n = receiveData(file_des,&tau_ns,sizeof(tau_ns),INT64); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + cprintf(RED,"%s",mess); + ret=FAIL; + } + +#ifndef EIGERD + sprintf(mess,"Rate Correction not implemented for this detector\n"); + cprintf(RED,"%s",mess); + ret=FAIL; +#endif + +#ifdef SLS_DETECTOR_FUNCTION_LIST + + //tau = -1, use default tau of settings + if((ret==OK)&&(tau_ns<0)){ + tau_ns = getDefaultSettingsTau_in_nsec(); + if(tau_ns < 0){ + ret = FAIL; + sprintf(mess,"Cannot set rate correction. Settings %d not recognized by detector\n",thisSettings); + cprintf(RED,"%s",mess); + } + } + + + if (ret==OK) { + printf("Setting rate correction to %lld ns\n",tau_ns); + + if (differentClients==1 && lockStatus==1 && tau_ns!=-1) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + } else { + retval = setRateCorrection(tau_ns); //tau_ns will not be -1 here + if(tau_ns != retval){ + if(retval == -1) + strcpy(mess,"Rate correction Deactivated, (tau/subexptime) must be < 0.0015\n"); + cprintf(RED,"%s",mess); + ret=FAIL; + } + } + } +#endif + if ((ret==OK) && (differentClients)) + ret=FORCE_UPDATE; + + + //ret could be swapped during sendData + ret1 = ret; + n = sendData(file_des,&ret1,sizeof(ret),INT32); + if (ret==FAIL) { + n = sendData(file_des,mess,sizeof(mess),OTHER); + } else { + n = sendData(file_des,&retval,sizeof(retval),INT64); + } + + return ret; +} diff --git a/slsDetectorSoftware/slsDetectorServer/slsDetectorServer_funcs.h b/slsDetectorSoftware/slsDetectorServer/slsDetectorServer_funcs.h index c45da8b56..c2fa38438 100755 --- a/slsDetectorSoftware/slsDetectorServer/slsDetectorServer_funcs.h +++ b/slsDetectorSoftware/slsDetectorServer/slsDetectorServer_funcs.h @@ -87,6 +87,7 @@ int set_counter_bit(int); int pulse_pixel(int); int pulse_pixel_and_move(int); int pulse_chip(int); +int rate_correct(int); #endif