diff --git a/slsDetectorSoftware/commonFiles/communication_funcs.c b/slsDetectorSoftware/commonFiles/communication_funcs.c index 48aae9d30..c72722c92 100755 --- a/slsDetectorSoftware/commonFiles/communication_funcs.c +++ b/slsDetectorSoftware/commonFiles/communication_funcs.c @@ -328,9 +328,9 @@ int receiveData(int file_des, void* buf,int length, intType itype){ int nreceiving; int nreceived; if (file_des<0) return -1; - //#ifdef VERY_VERBOSE + #ifdef VERY_VERBOSE printf("want to receive %d Bytes\n", length); - //#endif + #endif while(length>0){ nreceiving = (length>send_rec_max_size) ? send_rec_max_size:length; diff --git a/slsDetectorSoftware/commonFiles/sls_detector_defs.h b/slsDetectorSoftware/commonFiles/sls_detector_defs.h index 6c44b47e5..faa425780 100755 --- a/slsDetectorSoftware/commonFiles/sls_detector_defs.h +++ b/slsDetectorSoftware/commonFiles/sls_detector_defs.h @@ -29,6 +29,9 @@ #define MAX_FPGAPROGRAMSIZE (2 * 1024 *1024) + +#define MAX_SCAN_LEVELS 2 + typedef char mystring[MAX_STR_LENGTH]; typedef double mysteps[MAX_SCAN_STEPS]; @@ -373,7 +376,12 @@ enum dacIndex { V_POWER_D = 103, /**new chiptest board */ V_POWER_IO =104, /**new chiptest board */ V_POWER_CHIP=105 ,/**new chiptest board */ - V_LIMIT=106 /**new chiptest board */ + I_POWER_A=106 , /**new chiptest board */ + I_POWER_B=107 , /**new chiptest board */ + I_POWER_C=108 , /**new chiptest board */ + I_POWER_D=109 , /**new chiptest board */ + I_POWER_IO=110 , /**new chiptest board */ + V_LIMIT=111 /**new chiptest board */ }; /** diff --git a/slsDetectorSoftware/jctbDetectorServer/Makefile.ctb b/slsDetectorSoftware/jctbDetectorServer/Makefile.ctb index ca00d71b9..52b5bb8df 100644 --- a/slsDetectorSoftware/jctbDetectorServer/Makefile.ctb +++ b/slsDetectorSoftware/jctbDetectorServer/Makefile.ctb @@ -16,7 +16,8 @@ INSTMODE= 0777 BINS = testlib_sharedlibc -SRCS = server.c server_funcs.c communication_funcs.c firmware_funcs.c mcb_funcs.c sharedmemory.c +SRCS = server.c server_funcs.c communication_funcs.c firmware_funcs.c slow_adc.c blackfin.c +#mcb_funcs.c sharedmemory.c OBJS = $(SRCS:%.c=%.o) diff --git a/slsDetectorSoftware/jctbDetectorServer/firmware_funcs.c b/slsDetectorSoftware/jctbDetectorServer/firmware_funcs.c index a95eed4e5..2fd223172 100755 --- a/slsDetectorSoftware/jctbDetectorServer/firmware_funcs.c +++ b/slsDetectorSoftware/jctbDetectorServer/firmware_funcs.c @@ -6,6 +6,7 @@ #include "server_defs.h" #include "firmware_funcs.h" #include "mcb_funcs.h" +#include "slow_adc.h" #include "registers_m.h" //#define VERBOSE @@ -47,6 +48,7 @@ #include #include +#include "blackfin.h" typedef struct ip_header_struct { u_int16_t ip_len; u_int8_t ip_tos; @@ -64,9 +66,6 @@ typedef struct ip_header_struct { struct timeval tss,tse,tsss; //for timing -//for memory mapping -u_int32_t CSP0BASE; - FILE *debugfp, *datafp; @@ -89,7 +88,7 @@ int adcConfigured=-1; u_int16_t *ram_values=NULL; char volatile *now_ptr=NULL; //u_int32_t volatile *values; -u_int16_t volatile *values; +extern u_int16_t volatile *values; int ram_size=0; int64_t totalTime=1; @@ -139,93 +138,96 @@ int digitalEnable=0; int vLimit=-100; +int nDacs; +int nAdcs; +char mtdvalue[10]; +int initDetector() { -int mapCSP0(void) { - printf("Mapping memory\n"); -#ifndef VIRTUAL - int fd; - fd = open("/dev/mem", O_RDWR | O_SYNC, 0); - if (fd == -1) { - printf("\nCan't find /dev/mem!\n"); - return FAIL; - } - printf("/dev/mem opened\n"); - - CSP0BASE = (u_int32_t)mmap(0, MEM_SIZE, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, fd, CSP0); - if (CSP0BASE == (u_int32_t)MAP_FAILED) { - printf("\nCan't map memmory area!!\n"); - return FAIL; - } - printf("CSP0 mapped\n"); + int imod; + // sls_detector_module *myModule; + int n=getNModBoard(); + nModX=n; +#ifdef VERBOSE + printf("Board is for %d modules\n",n); #endif -#ifdef VIRTUAL - CSP0BASE = malloc(MEM_SIZE); - printf("memory allocated\n"); -#endif -#ifdef SHAREDMEMORY - if ( (res=inism(SMSV))<0) { - printf("error attaching shared memory! %i",res); - return FAIL; - } -#endif - 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; - //values=(u_int32_t*)(CSP0BASE+address*2); - values=(u_int16_t*)(CSP0BASE+address*2); - printf("statusreg=%08x\n",bus_r(STATUS_REG)); - printf("\n\n"); - return OK; -} + + // nChans=N_CHAN; + // nChips=N_CHIP; + nDacs=N_DAC; + // nAdcs=N_ADC; -u_int16_t bus_r16(u_int32_t offset){ - volatile u_int16_t *ptr1; - ptr1=(u_int16_t*)(CSP0BASE+offset*2); - return *ptr1; -} +/* detectorModules=malloc(n*sizeof(sls_detector_module)); */ +/* detectorDacs=malloc(n*N_DAC*sizeof(int)); */ +/* detectorAdcs=malloc(n*N_ADC*sizeof(int)); */ +/* detectorChips=NULL; */ +/* detectorChans=NULL; */ +/* detectorAdcs=NULL; */ +/* if(myDetectorType != JUNGFRAU){ */ +/* detectorChips=malloc(n*N_CHIP*sizeof(int)); */ +/* detectorChans=malloc(n*N_CHIP*N_CHAN*sizeof(int)); */ +/* } */ -u_int16_t bus_w16(u_int32_t offset, u_int16_t data) { - volatile u_int16_t *ptr1; - ptr1=(u_int16_t*)(CSP0BASE+offset*2); - *ptr1=data; - return OK; -} +/* #ifdef VERBOSE */ +/* printf("modules from 0x%x to 0x%x\n",(unsigned int)(detectorModules), (unsigned int)(detectorModules+n)); */ +/* printf("dacs from 0x%x to 0x%x\n",(unsigned int)(detectorDacs), (unsigned int)(detectorDacs+n*N_DAC)); */ +/* printf("adcs from 0x%x to 0x%x\n",(unsigned int)(detectorAdcs), (unsigned int)(detectorAdcs+n*N_ADC)); */ +/* if(myDetectorType != JUNGFRAU){ */ +/* printf("chips from 0x%x to 0x%x\n",(unsigned int)(detectorChips), (unsigned int)(detectorChips+n*N_CHIP)); */ +/* printf("chans from 0x%x to 0x%x\n",(unsigned int)(detectorChans), (unsigned int)(detectorChans+n*N_CHIP*N_CHAN)); */ +/* } */ +/* #endif */ -/** ramType is DARK_IMAGE_REG or GAIN_IMAGE_REG */ -u_int16_t ram_w16(u_int32_t ramType, int adc, int adcCh, int Ch, u_int16_t data) { - unsigned int adr = (ramType | adc << 8 | adcCh << 5 | Ch ); - // printf("Writing to addr:%x\n",adr); - return bus_w16(adr,data); -} -/** ramType is DARK_IMAGE_REG or GAIN_IMAGE_REG */ -u_int16_t ram_r16(u_int32_t ramType, int adc, int adcCh, int Ch){ - unsigned int adr = (ramType | adc << 8 | adcCh << 5 | Ch ); - // printf("Reading from addr:%x\n",adr); - return bus_r16(adr); -} +/* 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; */ +/* (detectorModules+imod)->reg=0; */ +/* /\* initialize registers, dacs, retrieve sn, adc values etc *\/ */ +/* } */ +/* thisSettings=UNINITIALIZED; */ +/* sChan=noneSelected; */ +/* sChip=noneSelected; */ +/* sMod=noneSelected; */ +/* sDac=noneSelected; */ +/* sAdc=noneSelected; */ + +/* /\* */ +/* setCSregister(ALLMOD); //commented out by dhanya */ +/* setSSregister(ALLMOD); */ +/* counterClear(ALLMOD); */ +/* clearSSregister(ALLMOD); */ +/* putout("0000000000000000",ALLMOD); */ +/* *\/ */ -u_int32_t bus_w(u_int32_t offset, u_int32_t data) { - volatile u_int32_t *ptr1; +/* /\* initialize dynamic range etc. *\/ */ +/* /\* dynamicRange=getDynamicRange(); //always 16 not required commented out */ +/* nModX=setNMod(-1);*\/ */ - ptr1=(u_int32_t*)(CSP0BASE+offset*2); - *ptr1=data; +/* // dynamicRange=32; */ +/* // initChip(0, 0,ALLMOD); */ +/* //nModX=n; */ +/* // */ + allocateRAM(); return OK; } -u_int32_t bus_r(u_int32_t offset) { - volatile u_int32_t *ptr1; - ptr1=(u_int32_t*)(CSP0BASE+offset*2); - return *ptr1; -} - @@ -470,16 +472,24 @@ int configureFrequency(int val, int i) { u_int32_t tot; u_int32_t odd=1;//0; - + printf("Want to configure frequency of counter %d to %d\n",i,val); // printf("PLL reconfig reset\N"); bus_w(PLL_CNTRL_REG,(1<3) + if (i<0 || i>3) { + printf("wrong counter number %d\n",i); return -1; + } - if (val<=0) + if (val<=0) { + + printf("get value %d %d \n",i,clkDivider[i]); return clkDivider[i]; - - if (i==1 || i==2){ - if (val>40) printf("Too high frequency %d MHz for these ADCs!\n", val); + } + if (i==adc_clk_c){ + if (val>40) + { + printf("Too high frequency %d MHz for these ADCs!\n", val); + return clkDivider[i]; + } } tot= PLL_VCO_FREQ_MHZ/val; @@ -489,10 +499,10 @@ int configureFrequency(int val, int i) { h=l+1; odd=1; } - else - { - odd=0; - } + else + { + odd=0; + } printf("Counter %d: Low is %d, High is %d\n",i, l,h); @@ -518,7 +528,11 @@ int configureFrequency(int val, int i) { bus_w(PLL_CNTRL_REG,((1<> 32; - vMSB=v64&(0xffffffff); - bus_w(aMSB,vMSB); - // printf("Wreg64(%x,%x) %08x %08x %016llx\n", aLSB>>11, aMSB>>11, vLSB, vMSB, value); - } - return get64BitReg(aLSB, aMSB); - -} - -int64_t get64BitReg(int aLSB, int aMSB){ - int64_t v64; - u_int32_t vLSB,vMSB; - vLSB=bus_r(aLSB); - vMSB=bus_r(aMSB); - v64=vMSB; - v64=(v64<<32) | vLSB; - - // printf("reg64(%x,%x) %x %x %llx\n", aLSB, aMSB, vLSB, vMSB, v64); - - return v64; -} - int64_t setFrames(int64_t value){ return set64BitReg(value, SET_FRAMES_LSB_REG, SET_FRAMES_MSB_REG); } @@ -1599,11 +1583,11 @@ int setDACRegister(int idac, int val, int imod) { /* #endif */ /* return val; */ } + - -int getTemperature(int tempSensor, int imod){ +int getTemperature(int tempSensor){ int val; - imod=0;//ignoring more than 1 mod for now + int imod=0;//ignoring more than 1 mod for now int i,j,repeats=6; u_int32_t tempVal=0; #ifdef VERBOSE @@ -1643,13 +1627,21 @@ int initHighVoltage(int val, int imod){ u_int32_t offw,codata; - u_int16_t valw, dacvalue; + u_int16_t valw, dacvalue=-1; int i,ddx,csdx,cdx;//iru, float alpha=0.55;//, fval=val; + + offw=DAC_REG; - if (val!=-1) { - - if (val<60) { + if (val!=-1) { + if (val<0) { + printf("val is %d: should switch the relais!\n", val); + val=-100; + dacvalue=0; + } else if (val==0) { + dacvalue=0; + val=0; + } else if (val<60) { dacvalue=0; val=60; } else if (val>=200) { @@ -1661,9 +1653,8 @@ int initHighVoltage(int val, int imod){ } printf ("****************************** setting val %d, dacval %d\n",val, dacvalue); - if (val>=0) { + // if (dacvalue>=0) { - offw=DAC_REG; ddx=8; csdx=10; cdx=9; codata=((dacvalue)&0xff); @@ -1681,24 +1672,24 @@ int initHighVoltage(int val, int imod){ valw=(valw&(~(0x1<=0) { + valw=bus_r(offw)|0xff00;; //switch on HV + } else { + valw=bus_r(offw)&0x7fff;//switch off HV + } + + bus_w(offw,(valw)); // stop point =start point of course */ - printf("Writing %d in HVDAC \n",dacvalue); bus_w(HV_REG,val); - } else { - valw=bus_r(offw)&0x7fff; - bus_w(offw,(valw)); - bus_w(HV_REG,0); - } - } + // } + } - return bus_r(HV_REG); + return (int16_t)bus_r(HV_REG); @@ -1709,18 +1700,18 @@ int initHighVoltage(int val, int imod){ int initConfGain(int isettings,int val,int imod){ int retval; - u_int32_t addr=CONFGAIN_REG; +/* u_int32_t addr=CONFGAIN_REG; */ - if(isettings!=-1){ -#ifdef VERBOSE - printf("Setting Gain of module:%d with val:%d\n",imod,val); -#endif - bus_w(addr,val); - } - retval=(bus_r(addr)); -#ifdef VERBOSE - printf("Value read from Gain reg is %d\n",retval); -#endif +/* if(isettings!=-1){ */ +/* #ifdef VERBOSE */ +/* printf("Setting Gain of module:%d with val:%d\n",imod,val); */ +/* #endif */ +/* bus_w(addr,val); */ +/* } */ +/* retval=(bus_r(addr)); */ +/* #ifdef VERBOSE */ +/* printf("Value read from Gain reg is %d\n",retval); */ +/* #endif */ return retval; } @@ -2163,7 +2154,7 @@ u_int16_t* fifo_read_event(int ns) if (i!=0 || ns!=0) { a=0; while (*((u_int16_t*)now_ptr)==*((u_int16_t*)(now_ptr)-1) && a++<10) { - *((u_int16_t*)now_ptr)=*values; + *((u_int16_t*)now_ptr)=*values; } } now_ptr+=2; @@ -2174,13 +2165,13 @@ u_int16_t* fifo_read_event(int ns) } } if (digitalEnable) { - // printf("+"); + printf("+"); bus_w16(DUMMY_REG,1<<9); // read strobe to digital fifo bus_w16(DUMMY_REG,0<<9); // read strobe to digital fifo *((u_int64_t*)now_ptr)=get64BitReg(FIFO_DIGITAL_DATA_LSB_REG,FIFO_DIGITAL_DATA_MSB_REG); //bit17 is clkout - printf("%d",(*((u_int64_t*)now_ptr)>>17)&1); + // printf("%d",(*((u_int64_t*)now_ptr)>>17)&1); now_ptr+=8; } @@ -2412,16 +2403,17 @@ int allocateRAM() { #ifdef VERBOSE printf("\nnmodx=%d nmody=%d dynamicRange=%d dataBytes=%d nFrames=%d nTrains=%d, size=%d\n",nModX,nModY,dynamicRange,dataBytes,nf,nt,(int)size ); #endif + if (size<=0) return FAIL; - if (size==ram_size) { + if (size==ram_size) { //#ifdef VERBOSE - printf("RAM of size %d already allocated: nothing to be done\n",(int) size); + printf("RAM of size %d already allocated: nothing to be done\n",(int) size); //#endif - return OK; - } - + return OK; + } + //#ifdef VERBOSE printf("reallocating ram %x, size %d\n",(unsigned int)ram_values, (int)size); @@ -2505,243 +2497,6 @@ int writeADC(int addr, int val) { return OK; } -int prepareSlowADCSeq() { - - // u_int16_t vv=0x3c40; - u_int16_t codata=( 1<<13) | (7<<10) | (7<<7) | (1<<6) | (0<<3) | (2<<1) | 1; - - u_int32_t valw; - int obit, ibit; - - int cnv_bit=16, sdi_bit=17, sck_bit=18; - - - // int oval=0; - - - printf("Codata is %04x\n",codata); - - /* //convert */ - valw=(1<> (13-ibit)) & 1); - // printf("%d",obit); - valw = obit << sdi_bit; - - bus_w(ADC_WRITE_REG,valw); - - usleep(20); - - bus_w(ADC_WRITE_REG,valw|(1<> (13-ibit)) & 1); - // printf("%d",obit); - valw = obit << sdi_bit; - - bus_w(ADC_WRITE_REG,valw); - - usleep(20); - - bus_w(ADC_WRITE_REG,valw|(1<7) - return -1; - - - prepareSlowADC(ichan); - - - /* printf("Codata is %04x\n",codata); */ - - /* /\* //convert *\/ */ - /* valw=(1<> (13-ibit)) & 1); */ - /* // printf("%d",obit); */ - /* valw = obit << sdi_bit; */ - - /* bus_w(ADC_WRITE_REG,valw); */ - - /* usleep(20); */ - - /* bus_w(ADC_WRITE_REG,valw|(1<> 8); - // printf("%i: %i %i\n",a, frame[a],v); - avg[a] += ((double)frame[a])/(double)frames; - //if(frame[a] == 8191) - // printf("ch %i: %u\n",a,frame[a]); - } - // printf("********\n"); - numberFrames++; - } - - //no more data or no data - else { - if(getFrames()>-2) { - dataret=FAIL; - printf("no data and run stopped: %d frames left\n",(int)(getFrames()+2)); - - } else { - dataret=FINISHED; - printf("acquisition successfully finished\n"); - - } - printf("dataret %d\n",dataret); - } - } - - - - //double nf = (double)numberFrames; - for(i =0; i < 1280; i++){ - adc = i / 256; - adcCh = (i - adc * 256) / 32; - Ch = i - adc * 256 - adcCh * 32; - adc--; - double v2 = avg[i]; - avg[i] = avg[i]/ ((double)numberFrames/(double)frames); - unsigned short v = (unsigned short)avg[i]; - printf("setting avg for channel %i(%i,%i,%i): %i (double= %f (%f))\t", i,adc,adcCh,Ch, v,avg[i],v2); - v=i*100; - ram_w16(DARK_IMAGE_REG,adc,adcCh,Ch,v-4096); - if(ram_r16(DARK_IMAGE_REG,adc,adcCh,Ch) != v-4096){ - printf("value is wrong (%i,%i,%i): %i \n",adc,adcCh,Ch, ram_r16(DARK_IMAGE_REG,adc,adcCh,Ch)); - } - } - - /*for(adc = 1; adc < 5; adc++){ - for(adcCh = 0; adcCh < 8; adcCh++){ - for(Ch=0 ; Ch < 32; Ch++){ - int channel = (adc+1) * 32 * 8 + adcCh * 32 + Ch; - double v2 = avg[channel]; - avg[channel] = avg[channel]/ ((double)numberFrames/(double)frames); - unsigned short v = (unsigned short)avg[channel]; - printf("setting avg for channel %i: %i (double= %f (%f))\t", channel, v,avg[channel],v2); - ram_w16(DARK_IMAGE_REG,adc,adcCh,Ch,v-4096); - if(ram_r16(DARK_IMAGE_REG,adc,adcCh,Ch) != v-4096){ - printf("value is wrong (%i,%i,%i): %i \n",adc,adcCh,Ch, ram_r16(DARK_IMAGE_REG,adc,adcCh,Ch)); - } - } - } - }*/ - - - - printf("frames: %i\n",numberFrames); - printf("corrected avg by: %f\n",(double)numberFrames/(double)frames); - - printf("restoring previous condition\n"); - setFrames(framesBefore); - setPeriod(periodBefore); - - printf("---------------------------\n"); - return 0; -} uint64_t readPatternWord(int addr) { uint64_t word=0; int cntrl=0; @@ -3586,8 +3223,8 @@ int getDacRegister(int dacnum) { bus_w(DAC_NUM_REG, dacnum); - printf("READ dac register value %d address %d\n",bus_r(DAC_VAL_OUT_REG),bus_r(DAC_NUM_REG)) ; - return bus_r(DAC_VAL_OUT_REG); + printf("READ dac register value %d address %d\n",(int16_t)bus_r(DAC_VAL_OUT_REG),bus_r(DAC_NUM_REG)) ; + return (int16_t)bus_r(DAC_VAL_OUT_REG); /* #define DAC_VAL_REG 121<<11 */ /* #define DAC_NUM_REG 122<<11 */ /* #define DAC_VAL_OUT_REG 42<<11 */ @@ -3757,6 +3394,136 @@ int setPower(int ind, int val) { } +void defineGPIOpins(){ + //define the gpio pins + system("echo 7 > /sys/class/gpio/export"); + system("echo 9 > /sys/class/gpio/export"); + //define their direction + system("echo in > /sys/class/gpio/gpio7/direction"); + system("echo out > /sys/class/gpio/gpio9/direction"); +} + +void resetFPGA(){ + cprintf(BLUE,"\n*** Reseting FPGA ***\n"); + FPGAdontTouchFlash(); + FPGATouchFlash(); + usleep(250*1000); +} + +void FPGAdontTouchFlash(){ + //tell FPGA to not touch flash + system("echo 0 > /sys/class/gpio/gpio9/value"); + //usleep(100*1000); +} + +void FPGATouchFlash(){ + //tell FPGA to touch flash to program itself + system("echo 1 > /sys/class/gpio/gpio9/value"); +} + + +int startWritingFPGAprogram(FILE** filefp){ +#ifdef VERY_VERBOSE + printf("\n at startWritingFPGAprogram \n"); +#endif + + //getting the drive + char output[255]; + FILE* fp = popen("awk \'$4== \"\\\"bitfile(spi)\\\"\" {print $1}\' /proc/mtd", "r"); + fgets(output, sizeof(output), fp); + pclose(fp); + strcpy(mtdvalue,"/dev/"); + char* pch = strtok(output,":"); + if(pch == NULL){ + cprintf(RED,"Could not get mtd value\n"); + return FAIL; + } + strcat(mtdvalue,pch); + printf ("\nFlash drive found: %s\n",mtdvalue); + + + FPGAdontTouchFlash(); + + //writing the program to flash + *filefp = fopen(mtdvalue, "w"); + if(*filefp == NULL){ + cprintf(RED,"Unable to open %s in write mode\n",mtdvalue); + return FAIL; + } + printf("flash ready for writing\n"); + + return OK; +} + + +void eraseFlash(){ +#ifdef VERY_VERBOSE + printf("\n at eraseFlash \n"); +#endif + + char command[255]; + sprintf(command,"flash_eraseall %s",mtdvalue); + system(command); + printf("flash erased\n"); +} + +int stopWritingFPGAprogram(FILE* filefp){ +#ifdef VERY_VERBOSE + printf("\n at stopWritingFPGAprogram \n"); +#endif + + int wait = 0; + if(filefp!= NULL){ + fclose(filefp); + wait = 1; + } + + //touch and program + FPGATouchFlash(); + + if(wait){ +#ifdef VERY_VERBOSE + printf("Waiting for FPGA to program from flash\n"); +#endif + //waiting for success or done + char output[255]; + int res=0; + while(res == 0){ + FILE* sysFile = popen("cat /sys/class/gpio/gpio7/value", "r"); + fgets(output, sizeof(output), sysFile); + pclose(sysFile); + sscanf(output,"%d",&res); +#ifdef VERY_VERBOSE + printf("gpi07 returned %d\n",res); +#endif + } + } + printf("FPGA has picked up the program from flash\n\n"); + + + return OK; +} + +int writeFPGAProgram(char* fpgasrc, size_t fsize, FILE* filefp){ +#ifdef VERY_VERBOSE + printf("\n at writeFPGAProgram \n"); + cprintf(BLUE,"address of fpgasrc:%p\n",(void *)fpgasrc); + cprintf(BLUE,"fsize:%d\n",fsize); + cprintf(BLUE,"pointer:%p\n",(void*)filefp); +#endif + + if(fwrite((void*)fpgasrc , sizeof(char) , fsize , filefp )!= fsize){ + cprintf(RED,"Could not write FPGA source to flash\n"); + return FAIL; + } +#ifdef VERY_VERBOSE + cprintf(BLUE,"program written to flash\n"); +#endif + return OK; +} + + + int powerChip(int arg) { //#ifndef CTB @@ -4050,3 +3817,249 @@ int setReadOutMode(int arg) { return v1; } + + +int writePowerI2C(int val, int nbit) { + + int nc=nbit/8; + int ic, ib, ii; + int ack; + int bsd=PWR_I2C_SDA_BIT, bsc=PWR_I2C_SCL_BIT,esd=PWR_I2C_SDA_EN_BIT, esc=PWR_I2C_SCL_EN_BIT; + + u_int16_t co; + + printf("Write power I2C\n"); + co=(1<>ib)&1)<>ib)&1)); + } + printf("\n"); + co=co&(~(1<>ib)&1)<>ib)&1)); + } + printf("\n"); + co=co&(~(1<=0 && imod=0 && imod= nModX) { - ret=FAIL; - sprintf(mess,"Module %d disabled\n",imod); - break; - } - if (testShiftIn(imod)) retval|=(1<<(ibit)); - ibit++; - if (testShiftOut(imod)) retval|=(1<<(ibit)); - ibit++; - if (testShiftStSel(imod)) retval|=(1<<(ibit)); - ibit++; - //if ( testDataInOut(0x123456, imod)) retval|=(1<<(ibit++)); - //if ( testExtPulse(imod)) retval|=(1<<(ibit++)); - // for (ow=0; ow<6; ow++) - // ow=1; - //#ifndef PICASSOD - for (ow=0; ow<5; ow++) { - //#endif - if (testDataInOutMux(imod, ow, 0x789abc)) retval|=(1<=getNModBoard() || imod<0) ret=FAIL; -#ifdef MCB_FUNCS + //#ifdef MCB_FUNCS switch (ind) { case TEMPERATURE_FPGA: idac=TEMP_FPGA; @@ -1180,16 +1097,51 @@ int get_adc(int file_des) { case TEMPERATURE_ADC: idac=TEMP_ADC; break; + + case V_POWER_D: + idac++; + case V_POWER_C: + idac++; + case V_POWER_B: + idac++; + case V_POWER_A: + idac++; + case V_POWER_IO: + idac+=100; + break; + + case I_POWER_D: + idac++; + case I_POWER_C: + idac++; + case I_POWER_B: + idac++; + case I_POWER_A: + idac++; + case I_POWER_IO: + idac+=200; + break; + default: - printf("Unknown DAC index %d\n",ind); + // printf("Unknown DAC index %d\n",ind); sprintf(mess,"Unknown DAC index %d\n",ind); ret=FAIL; break; } + printf("DAC index %d (%d)\n",idac,ind); - if (ret==OK) - retval=getTemperatureByModule(idac,imod); - else { + if (ret==OK) { + + + if (idac>=200) + retval=getCurrent(idac-200); + else if (idac>=100) + retval=getVoltage(idac-100); + else + retval=getTemperature(idac); + + + }else if (ind>=1000) { retval=readSlowADC(ind-1000); if (retval>=0) { ret=OK; @@ -1198,7 +1150,7 @@ int get_adc(int file_des) { } -#endif + //#endif #ifdef VERBOSE printf("ADC is %d V\n", retval); @@ -1263,9 +1215,7 @@ int set_channel(int file_des) { ret=FAIL; sprintf(mess,"Detector locked by %s\n",lastClientIP); } else { -#ifdef MCB_FUNCS - retval=initChannelbyNumber(myChan); -#endif + } } /* Maybe this is done inside the initialization funcs */ @@ -1338,9 +1288,6 @@ int get_channel(int file_des) { if (ret==OK) { -#ifdef MCB_FUNCS - ret=getChannelbyNumber(&retval); -#endif if (differentClients && ret==OK) ret=FORCE_UPDATE; } @@ -1410,9 +1357,7 @@ int set_chip(int file_des) { ret=FAIL; sprintf(mess,"Detector locked by %s\n",lastClientIP); } else { -#ifdef MCB_FUNCS - retval=initChipbyNumber(myChip); -#endif + ; } /* Maybe this is done inside the initialization funcs */ //copyChip(detectorChips[myChip.module]+(myChip.chip), &myChip); @@ -1467,9 +1412,6 @@ int get_chip(int file_des) { if (ret==OK) { -#ifdef MCB_FUNCS - ret=getChipbyNumber(&retval); -#endif if (differentClients && ret==OK) ret=FORCE_UPDATE; } @@ -1584,11 +1526,6 @@ int set_module(int file_des) { ret=FAIL; sprintf(mess,"Detector locked by %s\n",lastClientIP); } else { -#ifdef MCB_FUNCS - retval=initModulebyNumber(myModule); - if(retval != myModule.reg) - ret = FAIL; -#endif } } @@ -1694,9 +1631,6 @@ int get_module(int file_des) { if (imod>=0 && imodretval) { + configureFrequency(retval,sync_clk_c); + printf("--Configuring sync clk to %d MHz\n",val); + } else if (configureFrequency(-1,dbit_clk_c)>val && configureFrequency(-1,adc_clk_c)>retval) { + printf("++Configuring sync clk to %d MHz\n",val); + configureFrequency(retval,sync_clk_c); + } break; /* case PHASE_SHIFT: */ @@ -2535,11 +2446,25 @@ int set_speed(int file_des) { case ADC_CLOCK: retval=configureFrequency(val,adc_clk_c);//setClockDivider(val,1); - configureFrequency(val,sync_clk_c); + if (configureFrequency(-1,sync_clk_c)>val) { + configureFrequency(retval,sync_clk_c); + printf("--Configuring sync clk to %d MHz\n",val); + } else if (configureFrequency(-1,dbit_clk_c)>val && configureFrequency(-1,run_clk_c)>retval) { + printf("++Configuring sync clk to %d MHz\n",val); + configureFrequency(retval,sync_clk_c); + } break; case DBIT_CLOCK: retval=configureFrequency(val,dbit_clk_c);//setClockDivider(val,2); + if (configureFrequency(-1,sync_clk_c)>retval){ + configureFrequency(retval,sync_clk_c); + printf("--Configuring sync clk to %d MHz\n",val); + } else if (configureFrequency(-1,adc_clk_c)>retval && configureFrequency(-1,run_clk_c)>retval) { + printf("++Configuring sync clk to %d MHz\n",val); + configureFrequency(retval,sync_clk_c); + } + break; @@ -2760,7 +2685,7 @@ int send_update(int file_des) { n = sendDataOnly(file_des,&nModY,sizeof(nModY)); n = sendDataOnly(file_des,&dynamicRange,sizeof(dynamicRange)); n = sendDataOnly(file_des,&dataBytes,sizeof(dataBytes)); - t=setSettings(GET_SETTINGS,-1); + t=GET_SETTINGS;//setSettings(GET_SETTINGS,-1); n = sendDataOnly(file_des,&t,sizeof(t)); /* thr=getThresholdEnergy(); n = sendDataOnly(file_des,&thr,sizeof(thr));*/ @@ -2859,7 +2784,7 @@ int configure_mac(int file_des) { //#ifdef VERBOSE printf("Configuring MAC of module %d at port %x\n", imod, udpport); //#endif -#ifdef MCB_FUNCS + //#ifdef MCB_FUNCS if (ret==OK){ if(runBusy()){ ret=stopStateMachine(); @@ -2871,7 +2796,7 @@ int configure_mac(int file_des) { configureMAC(ipad,imacadd,idetectormacadd,detipad,digitalTestBit,udpport); retval=getAdcConfigured(); } -#endif + //#endif if (ret==FAIL) printf("configuring MAC of mod %d failed\n", imod); else @@ -3196,7 +3121,7 @@ int stop_receiver(int file_des) { strcpy(mess,"Could not stop receiver\n"); /* execute action if the arguments correctly arrived*/ -#ifdef MCB_FUNCS + //#ifdef MCB_FUNCS if (lockStatus==1 && differentClients==1){//necessary??? sprintf(mess,"Detector locked by %s\n", lastClientIP); ret=FAIL; @@ -3204,7 +3129,7 @@ int stop_receiver(int file_des) { else ret=startReceiver(0); -#endif + //#endif if(ret==OK && differentClients){ @@ -3536,3 +3461,160 @@ int power_chip(int file_des) { return ret; } + + +int reset_fpga(int file_des) { + int ret=OK; + int n; + sprintf(mess,"Reset FPGA unsuccessful\n"); + + resetFPGA(); + initializeDetector(); + + ret = FORCE_UPDATE; + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) + n += sendDataOnly(file_des,mess,sizeof(mess)); + + /*return ok/fail*/ + return ret; +} + + +int program_fpga(int file_des) { + int ret=OK; + int n; + sprintf(mess,"Program FPGA unsuccessful\n"); + char* fpgasrc = NULL; + FILE* fp = NULL; + size_t filesize = 0; + size_t unitprogramsize = 0; + size_t totalsize = 0; + + + //filesize + n = receiveDataOnly(file_des,&filesize,sizeof(filesize)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + totalsize = filesize; +#ifdef VERY_VERBOSE + printf("\n\n Total size is:%d\n",totalsize); +#endif + + //lock + if (ret==OK && differentClients==1 && lockStatus==1) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + filesize = 0; + } + + //opening file pointer to flash and telling FPGA to not touch flash + if(ret == OK && startWritingFPGAprogram(&fp) != OK){ + sprintf(mess,"Could not write to flash. Error at startup.\n"); + cprintf(RED,"%s",mess); + ret=FAIL; + filesize = 0; + } + + //---------------- first ret ---------------- + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) + n += sendDataOnly(file_des,mess,sizeof(mess)); + //---------------- first ret ---------------- + + + //erasing flash + if(ret != FAIL){ + eraseFlash(); + fpgasrc = (char*)malloc(MAX_FPGAPROGRAMSIZE); + } + + + + //writing to flash part by part + while(ret != FAIL && filesize){ + + unitprogramsize = MAX_FPGAPROGRAMSIZE; //2mb + if(unitprogramsize > filesize) //less than 2mb + unitprogramsize = filesize; +#ifdef VERY_VERBOSE + printf("unit size to receive is:%d\n",unitprogramsize); + printf("filesize:%d currentpointer:%d\n",filesize,currentPointer); +#endif + + + //receive + n = receiveDataOnly(file_des,fpgasrc,unitprogramsize); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + + if (ret==OK) { + if(!(unitprogramsize - filesize)){ + fpgasrc[unitprogramsize]='\0'; + filesize-=unitprogramsize; + unitprogramsize++; + }else + filesize-=unitprogramsize; + + ret = writeFPGAProgram(fpgasrc,unitprogramsize,fp); + } + + + //---------------- middle rets ---------------- + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) { + n += sendDataOnly(file_des,mess,sizeof(mess)); + cprintf(RED,"Failure: Breaking out of program receiving\n"); + } + //---------------- middle rets ---------------- + + + if(ret != FAIL){ + //print progress + printf("Writing to Flash:%d%%\r",(int) (((double)(totalsize-filesize)/totalsize)*100) ); + fflush(stdout); + } + + } + + + + printf("\n"); + + //closing file pointer to flash and informing FPGA + if(stopWritingFPGAprogram(fp) == FAIL){ + sprintf(mess,"Could not write to flash. Error at end.\n"); + cprintf(RED,"%s",mess); + ret=FAIL; + } + + if(ret!=FAIL){ + ret=FORCE_UPDATE; + } + + + //---------------- last ret ---------------- + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) + n += sendDataOnly(file_des,mess,sizeof(mess)); + //---------------- last ret ---------------- + + + //free resources + if(fpgasrc != NULL) + free(fpgasrc); + if(fp!=NULL) + fclose(fp); +#ifdef VERY_VERBOSE + printf("Done with program receiving command\n"); +#endif + /*return ok/fail*/ + return ret; +} + + diff --git a/slsDetectorSoftware/jctbDetectorServer/server_funcs.h b/slsDetectorSoftware/jctbDetectorServer/server_funcs.h index 8d131d83b..eac4fedfa 100755 --- a/slsDetectorSoftware/jctbDetectorServer/server_funcs.h +++ b/slsDetectorSoftware/jctbDetectorServer/server_funcs.h @@ -96,4 +96,7 @@ int set_ctb_pattern(int); int write_adc_register(int); int power_chip(int); +int reset_fpga(int); +int program_fpga(int); + #endif diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index db2b3cbe6..7f6a26e20 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -6625,7 +6625,7 @@ int slsDetector::programFPGA(string fname){ size_t filesize=0; char* fpgasrc = NULL; - if(thisDetector->myDetectorType != JUNGFRAU){ + if(thisDetector->myDetectorType != JUNGFRAU && thisDetector->myDetectorType != JUNGFRAUCTB){ std::cout << "Not implemented for this detector" << std::endl; return FAIL; } diff --git a/slsDetectorSoftware/slsDetector/slsDetectorActions.h b/slsDetectorSoftware/slsDetector/slsDetectorActions.h index 5aff24f79..a034e6bf0 100644 --- a/slsDetectorSoftware/slsDetector/slsDetectorActions.h +++ b/slsDetectorSoftware/slsDetector/slsDetectorActions.h @@ -8,9 +8,6 @@ #include - -#define MAX_SCAN_LEVELS 2 - using namespace std; /** diff --git a/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp b/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp index 14d17fe1d..873cfd70c 100644 --- a/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp @@ -467,6 +467,15 @@ slsDetectorCommand::slsDetectorCommand(slsDetectorUtils *det) { descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdAdvanced; i++; + + + /* chip */ + descrToFuncMap[i].m_pFuncName="led"; + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdAdvanced; + i++; + + + /* versions/ serial numbers getId */ descrToFuncMap[i].m_pFuncName="moduleversion"; // @@ -835,6 +844,46 @@ slsDetectorCommand::slsDetectorCommand(slsDetectorUtils *det) { i++; + descrToFuncMap[i].m_pFuncName="i_a"; // + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdADC; + i++; + + descrToFuncMap[i].m_pFuncName="i_b"; // + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdADC; + i++; + + descrToFuncMap[i].m_pFuncName="i_c"; // + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdADC; + i++; + + descrToFuncMap[i].m_pFuncName="i_d"; // + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdADC; + i++; + + descrToFuncMap[i].m_pFuncName="i_io"; // + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdADC; + i++; + + descrToFuncMap[i].m_pFuncName="vm_a"; // + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdADC; + i++; + + descrToFuncMap[i].m_pFuncName="vm_b"; // + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdADC; + i++; + + descrToFuncMap[i].m_pFuncName="vm_c"; // + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdADC; + i++; + + descrToFuncMap[i].m_pFuncName="vm_d"; // + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdADC; + i++; + + descrToFuncMap[i].m_pFuncName="vm_io"; // + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdADC; + i++; + /* r/w timers */ descrToFuncMap[i].m_pFuncName="timing"; // @@ -4016,7 +4065,7 @@ string slsDetectorCommand::cmdADC(int narg, char *args[], int action) { return helpADC(narg, args, action); else if (action==PUT_ACTION) return string("cannot set ")+cmd; - + if (sscanf(args[0],"adc:%d",&idac)==1) { // printf("chiptestboard!\n"); adc=(dacIndex)(idac+1000); @@ -4038,6 +4087,26 @@ string slsDetectorCommand::cmdADC(int narg, char *args[], int action) { adc=TEMPERATURE_FPGA2; else if (cmd=="temp_fpgafr") adc=TEMPERATURE_FPGA3; + else if (cmd=="i_a") + adc=I_POWER_A; + else if (cmd=="i_b") + adc=I_POWER_B; + else if (cmd=="i_c") + adc=I_POWER_C; + else if (cmd=="i_d") + adc=I_POWER_D; + else if (cmd=="vm_a") + adc=V_POWER_A; + else if (cmd=="vm_b") + adc=V_POWER_B; + else if (cmd=="vm_c") + adc=V_POWER_C; + else if (cmd=="vm_d") + adc=V_POWER_D; + else if (cmd=="vm_io") + adc=V_POWER_IO; + else if (cmd=="i_io") + adc=I_POWER_IO; else return string("cannot decode adc ")+cmd; @@ -4546,6 +4615,19 @@ string slsDetectorCommand::cmdAdvanced(int narg, char *args[], int action) { } sprintf(ans,"%d",myDet->powerChip()); return string(ans); + } else if (cmd=="led") { + char ans[100]; + int val=0; + myDet->setOnline(ONLINE_FLAG); + if (action==PUT_ACTION){ + int ival = -1; + if (!sscanf(args[1],"%d",&ival)) + return string("could not scan powerchip parameter " + string(args[1])); + val=myDet->readRegister(0x4d); + myDet->writeRegister(0x4d,(val&(~1))|((~ival)&1));//config register + } + sprintf(ans,"%d",~(myDet->readRegister(0x4d))&1); + return string(ans); } else return string("unknown command ")+cmd; @@ -4563,11 +4645,13 @@ string slsDetectorCommand::helpAdvanced(int narg, char *args[], int action) { os << "programfpga f \t programs the fpga with file f (with .pof extension)." << std::endl; os << "resetfpga f \t resets fpga, f can be any value" << std::endl; + os << "led s \t sets led status (0 off, 1 on)" << std::endl; } if (action==GET_ACTION || action==HELP_ACTION) { os << "extsig:i \t gets the mode of the external signal i. can be \n \t \t \t off, \n \t \t \t gate_in_active_high, \n \t \t \t gate_in_active_low, \n \t \t \t trigger_in_rising_edge, \n \t \t \t trigger_in_falling_edge, \n \t \t \t ro_trigger_in_rising_edge, \n \t \t \t ro_trigger_in_falling_edge, \n \t \t \t gate_out_active_high, \n \t \t \t gate_out_active_low, \n \t \t \t trigger_out_rising_edge, \n \t \t \t trigger_out_falling_edge, \n \t \t \t ro_trigger_out_rising_edge, \n \t \t \t ro_trigger_out_falling_edge" << std::endl; os << "flags \t gets the readout flags. can be none, storeinram, tot, continous, parallel, nonparallel, safe, digital, analog_digital, unknown" << std::endl; + os << "led \t returns led status (0 off, 1 on)" << std::endl; } return os.str();