diff --git a/slsDetectorSoftware/gotthardDetectorServer/svnInfoGotthard.h b/slsDetectorSoftware/gotthardDetectorServer/svnInfoGotthard.h index afda5f360..6c9842ea9 100644 --- a/slsDetectorSoftware/gotthardDetectorServer/svnInfoGotthard.h +++ b/slsDetectorSoftware/gotthardDetectorServer/svnInfoGotthard.h @@ -2,10 +2,10 @@ #define SVNURL "file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware/gotthardDetectorServer" //#define SVNREPPATH "" #define SVNREPUUID "951219d9-93cf-4727-9268-0efd64621fa3" -//#define SVNREV 0x491 +//#define SVNREV 0x504 //#define SVNKIND "" //#define SVNSCHED "" -#define SVNAUTH "l_maliakal_d" -#define SVNREV 0x491 -#define SVNDATE 0x20130313 +#define SVNAUTH "bergamaschi" +#define SVNREV 0x504 +#define SVNDATE 0x20130408 // diff --git a/slsDetectorSoftware/moenchDetectorServer/.target-makefrag b/slsDetectorSoftware/moenchDetectorServer/.target-makefrag new file mode 100755 index 000000000..ce093ecac --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/.target-makefrag @@ -0,0 +1 @@ +AXIS_BUILDTYPE ?= cris-axis-linux-gnu diff --git a/slsDetectorSoftware/moenchDetectorServer/Makefile b/slsDetectorSoftware/moenchDetectorServer/Makefile new file mode 100755 index 000000000..75d6800cd --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/Makefile @@ -0,0 +1,48 @@ +# $Id: Makefile,v 1.1.1.1 2006/02/04 03:35:01 freza Exp $ +# first compile +# make cris-axis-linux-gnu + + +CROSS = bfin-uclinux- +CC = $(CROSS)gcc + +CFLAGS += -Wall -DMCB_FUNCS -DDACS_INT -DDEBUG #-DVERBOSE #-DVERYVERBOSE #-DVIRTUAL #-DDACS_INT_CSERVER + + +PROGS= gotthardDetectorServer +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 +OBJS = $(SRCS:%.c=%.o) + + + +all: clean $(PROGS) + +boot: $(OBJS) + +$(PROGS): $(OBJS) + echo $(OBJS) + $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS_$@) $(LDFLAGS_$@) + + +install: $(PROGS) + $(INSTALL) -d $(INSTDIR) + $(INSTALL) -m $(INSTMODE) $(PROGS) $(INSTDIR) + + +romfs: + $(ROMFSINST) /bin/$(PROGS) + +clean: + rm -rf $(PROGS) *.o *.gdb + + + + + + diff --git a/slsDetectorSoftware/moenchDetectorServer/Makefile.virtual b/slsDetectorSoftware/moenchDetectorServer/Makefile.virtual new file mode 100755 index 000000000..9eef2f616 --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/Makefile.virtual @@ -0,0 +1,30 @@ + +DESTDIR ?= ./ + +CC = gcc +CFLAGS += -Wall -DMCB_FUNCS -DDACS_INT -DDEBUG -DVIRTUAL + + +PROGS= $(DESTDIR)/gotthardVirtualServer + + +SRCS = server.c server_funcs.c communication_funcs.c firmware_funcs.c mcb_funcs.c trimming_funcs.c sharedmemory.c +OBJS = $(SRCS:%.c=%.o) + +gotthardVirtualServer = $(PROGS) + +all: clean $(PROGS) + + +$(PROGS): $(OBJS) + $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS_$@) $(LDFLAGS_$@) + + +clean: + rm -rf $(PROGS) *.o *.gdb + + + + + + diff --git a/slsDetectorSoftware/moenchDetectorServer/communication_funcs.c b/slsDetectorSoftware/moenchDetectorServer/communication_funcs.c new file mode 100755 index 000000000..54ad1c8a2 --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/communication_funcs.c @@ -0,0 +1,477 @@ + + +#include "communication_funcs.h" +//#include +#include /* for TCP_NODELAY */ +#include +#include +#include +#include + + + + +//int socketDescriptor, file_des; +const int send_rec_max_size=SEND_REC_MAX_SIZE; +extern int errno; + + +char dummyClientIP[INET_ADDRSTRLEN]; + +//struct sockaddr_in address; +//#define VERBOSE + + +int bindSocket(unsigned short int port_number) { + int i; + + struct sockaddr_in addressS; + int socketDescriptor; + //int file_des; + + //file_des= -1; + socketDescriptor = socket(AF_INET, SOCK_STREAM,0); //tcp + + //socketDescriptor = socket(PF_INET, SOCK_STREAM, 0); + + + + if (socketDescriptor < 0) { + printf("Can not create socket\n"); + } else { + + i = 1; + setsockopt(socketDescriptor, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)); + // setsockopt(socketDescriptor, IPPROTO_TCP, TCP_NODELAY, (char *) &i, sizeof(i)); + // TCP_CORK + + // Set some fields in the serverAddress structure. + addressS.sin_family = AF_INET; + addressS.sin_addr.s_addr = htonl(INADDR_ANY); + addressS.sin_port = htons(port_number); + + // memset(&address.sin_addr, 0, sizeof(address.sin_addr)); + + + if(bind(socketDescriptor,(struct sockaddr *) &addressS,sizeof(addressS))<0){ + + printf("Can not create socket\n"); + + socketDescriptor=-1; + } else { + listen(socketDescriptor, 5); + } + } + + + + + + + //int getrlimit(int resource, struct rlimit *rlim); + + + + return socketDescriptor; + +} + + + + + +int getServerError(int socketDescriptor) +{ + if (socketDescriptor<0) return 1; + else return 0; +}; + + +int acceptConnection(int socketDescriptor) { + struct sockaddr_in addressC; + int file_des=-1; + + //socklen_t address_length; + size_t address_length=sizeof(struct sockaddr_in); + + // if(file_des>0) return file_des; + + //#ifndef C_ONLY + // if(is_a_server){ //server; the server will wait for the clients connection + //#endif + + + if (socketDescriptor>0) { + //if ((file_des = accept(socketDescriptor,(struct sockaddr *) &address, &address_length)) < 0) { + if ((file_des = accept(socketDescriptor,(struct sockaddr *) &addressC, &address_length)) < 0) { + + + printf("Error: with server accept, connection refused %d\n", errno); + + + switch(errno) { + case EWOULDBLOCK: + printf("ewouldblock eagain\n"); + break; + case EBADF: + printf("ebadf\n"); + break; + case ECONNABORTED: + printf("econnaborted\n"); + break; + case EFAULT: + printf("efault\n"); + break; + case EINTR: + printf("eintr\n"); + break; + case EINVAL: + printf("einval\n"); + break; + case EMFILE: + printf("emfile\n"); + break; + case ENFILE: + printf("enfile\n"); + break; + case ENOTSOCK: + printf("enotsock\n"); + break; + case EOPNOTSUPP: + printf("eOPNOTSUPP\n"); + break; + case ENOBUFS: + printf("ENOBUFS\n"); + break; + case ENOMEM: + printf("ENOMEM\n"); + break; + case ENOSR: + printf("ENOSR\n"); + break; + case EPROTO: + printf("EPROTO\n"); + break; + default: + printf("unknown error\n"); + } + + + socketDescriptor=-1; + } + inet_ntop(AF_INET, &(addressC.sin_addr), dummyClientIP, INET_ADDRSTRLEN); + + + + + // struct sockaddr_in + //{ + //short int sin_family; /* Famyly of the address*/ + //unsigned short int sin_port; /* Port */ + //struct in_addr sin_addr; /* Network address */ + //unsigned char sin_zero[8]; /* Same size of struct sockaddr */ + //}; + } + + + return file_des; +} + + + + + + + +void closeConnection(int file_des) { + //fflush(stdout); + //printf("Closing file_des %d\n", file_des); + //sleep(1); +#ifdef VERY_VERBOSE +#endif + if(file_des>=0) + close(file_des); + file_des=-1; +} + +void exitServer(int socketDescriptor) { + if (socketDescriptor>=0) + close(socketDescriptor); +#ifdef VERY_VERBOSE + printf("Closing server\n"); +#endif + socketDescriptor=-1; +} + + + + + int sendDataOnly(int file_des, void* buf,int length) { + + + return write(file_des, buf, length); +} + + + int receiveDataOnly(int file_des, void* buf,int length) { + + int total_received=0; + int nreceiving; + int nreceived; + if (file_des<0) return -1; +#ifdef VERY_VERBOSE + printf("want to receive %d Bytes\n", length); +#endif + + while(length>0){ + nreceiving = (length>send_rec_max_size) ? send_rec_max_size:length; + +#ifdef VERY_VERBOSE + printf("want to receive %d Bytes\n", nreceiving); +#endif + nreceived = read(file_des,(char*)buf+total_received,nreceiving); +#ifdef VERY_VERBOSE + printf("read %d \n", nreceived); +#endif + if(!nreceived) break; + // if(nreceived<0) break; + length-=nreceived; + total_received+=nreceived; + // cout<<"nrec: "<0) + strcpy(thisClientIP,dummyClientIP); + if (strcmp(lastClientIP,thisClientIP)) + differentClients=1; + else + differentClients=0; + + return total_received; +} + + + + + + + + + + + + + + + +int sendChannel(int file_des, sls_detector_channel *myChan) { + return sendDataOnly(file_des,myChan, sizeof(sls_detector_channel)); +} + +int sendChip(int file_des, sls_detector_chip *myChip) { + int ts=0; + int nChans=myChip->nchan; + ts+=sendDataOnly(file_des,myChip,sizeof(sls_detector_chip)); + ts+=sendDataOnly(file_des,myChip->chanregs,nChans*sizeof(int)); + return ts; +} + +int sendModule(int file_des, sls_detector_module *myMod) { + int ts=0; + int idac; + int nChips=myMod->nchip; + int nChans=myMod->nchan; + int nAdcs=myMod->nadc; + int nDacs=myMod->ndac; + ts+= sendDataOnly(file_des,myMod,sizeof(sls_detector_module)); +#ifdef VERBOSE + printf("module %d of size %d sent\n",myMod->module, ts); +#endif + ts+= sendDataOnly(file_des,myMod->dacs,sizeof(dacs_t)*nDacs); +#ifdef VERBOSE + printf("dacs %d of size %d sent\n",myMod->module, ts); + for (idac=0; idac< nDacs; idac++) + printf("dac %d is %d\n",idac,myMod->dacs[idac]); +#endif + ts+= sendDataOnly(file_des,myMod->adcs,sizeof(dacs_t)*nAdcs); +#ifdef VERBOSE + printf("adcs %d of size %d sent\n",myMod->module, ts); +#endif + ts+=sendDataOnly(file_des,myMod->chipregs,sizeof(int)*nChips); +#ifdef VERBOSE + printf("chips %d of size %d sent\n",myMod->module, ts); +#endif + ts+=sendDataOnly(file_des,myMod->chanregs,sizeof(int)*nChans); +#ifdef VERBOSE + 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 + return ts; +} + +int receiveChannel(int file_des, sls_detector_channel *myChan) { + return receiveDataOnly(file_des,myChan,sizeof(sls_detector_channel)); +} + +int receiveChip(int file_des, sls_detector_chip* myChip) { + + int *ptr=myChip->chanregs; + int ts=0; + int nChans, nchanold=myChip->nchan, chdiff; + + ts+= receiveDataOnly(file_des,myChip,sizeof(sls_detector_chip)); + + + myChip->chanregs=ptr; + nChans=myChip->nchan; + chdiff=nChans-nchanold; + if (nchanold!=nChans) { + printf("wrong number of channels received!\n"); + } + + +#ifdef VERBOSE + printf("chip structure received\n"); + printf("now receiving %d channels\n", nChans); +#endif + + if (chdiff<=0) + ts+=receiveDataOnly(file_des,myChip->chanregs, sizeof(int)*nChans); + else { + ptr=malloc(chdiff*sizeof(int)); + myChip->nchan=nchanold; + ts+=receiveDataOnly(file_des,myChip->chanregs, sizeof(int)*nchanold); + ts+=receiveDataOnly(file_des,ptr, sizeof(int)*chdiff); + free(ptr); + return FAIL; + } + +#ifdef VERBOSE + printf("chip's channels received\n"); +#endif + return ts; +} + +int receiveModule(int file_des, sls_detector_module* myMod) { + + + 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; + int nAdcs, naold=myMod->nadc, nadcdiff; + + + ts+= receiveDataOnly(file_des,myMod,sizeof(sls_detector_module)); + + myMod->dacs=dacptr; + myMod->adcs=adcptr; + myMod->chipregs=chipptr; + myMod->chanregs=chanptr; + + nChips=myMod->nchip; + nchipdiff=nChips-nchipold; + if (nchipold!=nChips) { + printf("received wrong number of chips\n"); + } +#ifdef VERBOSE + else + printf("received %d chips\n",nChips); +#endif + + nChans=myMod->nchan; + nchandiff=nChans-nchanold; + if (nchanold!=nChans) { + printf("received wrong number of channels\n"); + } +#ifdef VERBOSE + else + printf("received %d chans\n",nChans); +#endif + + + nDacs=myMod->ndac; + ndacdiff=nDacs-ndold; + if (ndold!=nDacs) { + printf("received wrong number of dacs\n"); + } +#ifdef VERBOSE + else + printf("received %d dacs\n",nDacs); +#endif + + nAdcs=myMod->nadc; + nadcdiff=nAdcs-naold; + if (naold!=nAdcs) { + printf("received wrong number of adcs\n"); + } +#ifdef VERBOSE + else + printf("received %d adcs\n",nAdcs); +#endif + if (ndacdiff<=0) { + ts+=receiveDataOnly(file_des,myMod->dacs, sizeof(dacs_t)*nDacs); +#ifdef VERBOSE + printf("dacs received\n"); +#endif + } else { + dacptr=malloc(ndacdiff*sizeof(dacs_t)); + myMod->ndac=ndold; + ts+=receiveDataOnly(file_des,myMod->dacs, sizeof(dacs_t)*ndold); + ts+=receiveDataOnly(file_des,dacptr, sizeof(dacs_t)*ndacdiff); + free(dacptr); + return FAIL; + } + if (nadcdiff<=0) { + ts+=receiveDataOnly(file_des,myMod->adcs, sizeof(dacs_t)*nAdcs); +#ifdef VERBOSE + printf("adcs received\n"); +#endif + } else { + adcptr=malloc(nadcdiff*sizeof(dacs_t)); + myMod->nadc=naold; + ts+=receiveDataOnly(file_des,myMod->adcs, sizeof(dacs_t)*naold); + ts+=receiveDataOnly(file_des,adcptr, sizeof(dacs_t)*nadcdiff); + free(adcptr); + return FAIL; + } + if (nchipdiff<=0) { + ts+=receiveDataOnly(file_des,myMod->chipregs, sizeof(int)*nChips); +#ifdef VERBOSE + printf("chips received\n"); +#endif + } else { + chipptr=malloc(nchipdiff*sizeof(int)); + myMod->nchip=nchipold; + ts+=receiveDataOnly(file_des,myMod->chipregs, sizeof(int)*nchipold); + ts+=receiveDataOnly(file_des,chipptr, sizeof(int)*nchipdiff); + free(chipptr); + return FAIL; + } + if (nchandiff<=0) { + ts+=receiveDataOnly(file_des,myMod->chanregs, sizeof(int)*nChans); +#ifdef VERBOSE + printf("chans received\n"); +#endif + } else { + chanptr=malloc(nchandiff*sizeof(int)); + myMod->nchan=nchanold; + ts+=receiveDataOnly(file_des,myMod->chanregs, sizeof(int)*nchanold); + ts+=receiveDataOnly(file_des,chanptr, sizeof(int)*nchandiff); + 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/moenchDetectorServer/communication_funcs.h b/slsDetectorSoftware/moenchDetectorServer/communication_funcs.h new file mode 100755 index 000000000..f74b65664 --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/communication_funcs.h @@ -0,0 +1,40 @@ +#ifndef COMMUNICATION_FUNCS_H +#define COMMUNICATION_FUNCS_H + +#include "sls_detector_defs.h" + + +#define SEND_REC_MAX_SIZE 4096 +#define DEFAULT_PORTNO 1952 +#include +#include + + +#include +#include +#include +#include + +#include "sls_detector_defs.h" + +char lastClientIP[INET_ADDRSTRLEN]; +char thisClientIP[INET_ADDRSTRLEN]; +int lockStatus; +int differentClients; + +int bindSocket(unsigned short int port_number); +int acceptConnection(int socketDescriptor); +void closeConnection(int file_Des); +void exitServer(int socketDescriptor); +int sendDataOnly(int file_des, void* buf,int length); +int receiveDataOnly(int file_des, void* buf,int length); + +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 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); + +#endif diff --git a/slsDetectorSoftware/moenchDetectorServer/firmware_funcs.c b/slsDetectorSoftware/moenchDetectorServer/firmware_funcs.c new file mode 100755 index 000000000..e7c178583 --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/firmware_funcs.c @@ -0,0 +1,2416 @@ + +#include "server_defs.h" +#include "firmware_funcs.h" +#include "mcb_funcs.h" +#include "registers_g.h" + +#ifdef SHAREDMEMORY +#include "sharedmemory.h" +#endif + +#include +#include +#include + + +//for memory mapping +u_int32_t CSP0BASE; + +FILE *debugfp, *datafp; + +int fr; +int wait_time; +int *fifocntrl; + +//int *statusreg; commented out by dhanya +const int nModY=1; +int nModBoard; +int nModX=NMAXMOD; +int dynamicRange=16;//32; +int dataBytes=NMAXMOD*NCHIP*NCHAN*2; +int storeInRAM=0; +int ROI_flag=0; +int adcConfigured=-1; +u_int32_t *ram_values=NULL; +volatile char *now_ptr=NULL; +volatile u_int16_t *values; +int ram_size=0; + +int64_t totalTime=1; +u_int32_t progressMask=0; + +int phase_shift=DEFAULT_PHASE_SHIFT; +int ipPacketSize=DEFAULT_IP_PACKETSIZE; +int udpPacketSize=DEFAULT_UDP_PACKETSIZE; + + +int ififostart, ififostop, ififostep, ififo; + +int masterMode=NO_MASTER, syncMode=NO_SYNCHRONIZATION, timingMode=AUTO_TIMING; + +enum externalSignalFlag signals[4]={EXT_SIG_OFF, EXT_SIG_OFF, EXT_SIG_OFF, EXT_SIG_OFF}; + + +#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 + + + + +/** + ENEt conf structs +*/ +typedef struct mac_header_struct{ + u_int8_t mac_dest_mac2; + u_int8_t mac_dest_mac1; + u_int8_t mac_dummy1; + u_int8_t mac_dummy2; + u_int8_t mac_dest_mac6; + u_int8_t mac_dest_mac5; + u_int8_t mac_dest_mac4; + u_int8_t mac_dest_mac3; + u_int8_t mac_src_mac4; + u_int8_t mac_src_mac3; + u_int8_t mac_src_mac2; + u_int8_t mac_src_mac1; + u_int16_t mac_ether_type; + u_int8_t mac_src_mac6; + u_int8_t mac_src_mac5; +} mac_header; + +typedef struct ip_header_struct { + u_int16_t ip_len; + u_int8_t ip_tos; + u_int8_t ip_ihl:4 ,ip_ver:4; + u_int16_t ip_offset:13,ip_flag:3; + u_int16_t ip_ident; + u_int16_t ip_chksum; + u_int8_t ip_protocol; + u_int8_t ip_ttl; + u_int32_t ip_sourceip; + u_int32_t ip_destip; +} ip_header; + +typedef struct udp_header_struct{ + u_int16_t udp_destport; + u_int16_t udp_srcport; + u_int16_t udp_chksum; + u_int16_t udp_len; +} udp_header; + +typedef struct mac_conf_struct{ + mac_header mac; + ip_header ip; + udp_header udp; + u_int32_t npack; + u_int32_t lpack; + u_int32_t npad; + u_int32_t cdone; +} mac_conf; + +typedef struct tse_conf_struct{ + u_int32_t rev; //0x0 + u_int32_t scratch; + u_int32_t command_config; + u_int32_t mac_0; //0x3 + u_int32_t mac_1; + u_int32_t frm_length; + u_int32_t pause_quant; + u_int32_t rx_section_empty; //0x7 + u_int32_t rx_section_full; + u_int32_t tx_section_empty; + u_int32_t tx_section_full; + u_int32_t rx_almost_empty; //0xB + u_int32_t rx_almost_full; + u_int32_t tx_almost_empty; + u_int32_t tx_almost_full; + u_int32_t mdio_addr0; //0xF + u_int32_t mdio_addr1; +}tse_conf; + + + +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"); + +#endif +#ifdef VIRTUAL + CSP0BASE = (u_int32_t)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%x \n",CSP0BASE); + printf("CSPOBASE=from %08x to %x\n",CSP0BASE,CSP0BASE+MEM_SIZE); + + u_int32_t address; + address = FIFO_DATA_REG_OFF; + values=(u_int16_t*)(CSP0BASE+address*2); + printf("statusreg=%08x\n",bus_r(STATUS_REG)); + return OK; +} + +u_int16_t bus_r16(u_int32_t offset){ + volatile u_int16_t *ptr1; + ptr1=(u_int16_t*)(CSP0BASE+offset*2); + return *ptr1; +} + +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; +} + +/** 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); +} + +u_int32_t bus_w(u_int32_t offset, u_int32_t data) { + volatile u_int32_t *ptr1; + + ptr1=(u_int32_t*)(CSP0BASE+offset*2); + *ptr1=data; + + 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; +} + + +int setPhaseShiftOnce(){ + u_int32_t addr, reg; + int i; + addr=MULTI_PURPOSE_REG; + reg=bus_r(addr); +#ifdef VERBOSE + printf("Multipurpose reg:%x\n",reg); +#endif + + //Checking if it is power on(negative number) + // if(((reg&0xFFFF0000)>>16)>0){ + //bus_w(addr,0x0); //clear the reg + + if(reg==0){ + printf("\nImplementing phase shift of %d\n",phase_shift); + for (i=1;i=0 && d<4) { + signals[d]=mode; +#ifdef VERBOSE + printf("settings signal variable number %d to value %04x\n", d, signals[d]); +#endif + + // if output signal, set it! + + switch (mode) { + case GATE_IN_ACTIVE_HIGH: + case GATE_IN_ACTIVE_LOW: + if (timingMode==GATE_FIX_NUMBER || timingMode==GATE_WITH_START_TRIGGER) + setFPGASignal(d,mode); + else + setFPGASignal(d,SIGNAL_OFF); + break; + case TRIGGER_IN_RISING_EDGE: + case TRIGGER_IN_FALLING_EDGE: + if (timingMode==TRIGGER_EXPOSURE || timingMode==GATE_WITH_START_TRIGGER) + setFPGASignal(d,mode); + else + setFPGASignal(d,SIGNAL_OFF); + break; + case RO_TRIGGER_IN_RISING_EDGE: + case RO_TRIGGER_IN_FALLING_EDGE: + if (timingMode==TRIGGER_READOUT) + setFPGASignal(d,mode); + else + setFPGASignal(d,SIGNAL_OFF); + break; + case MASTER_SLAVE_SYNCHRONIZATION: + setSynchronization(syncMode); + break; + default: + setFPGASignal(d,mode); + break; + } + + setTiming(GET_EXTERNAL_COMMUNICATION_MODE); + } + + +// if (mode<=RO_TRIGGER_OUT_FALLING_EDGE && mode>=0) +// bus_w(EXT_SIGNAL_REG,((modes[mode])<=0) { +#ifdef VERBOSE + printf("writing signal register number %d mode %04x\n",d, modes[mode]); +#endif + bus_w(EXT_SIGNAL_REG,((modes[mode])<>off); + + if (mode=0 && d<4) { +#ifdef VERBOSE + printf("gettings signal variable number %d value %04x\n", d, signals[d]); +#endif + return signals[d]; + } else + return -1; + + +} + + +int getFPGASignal(int d) { + + int modes[]={SIGNAL_OFF, GATE_IN_ACTIVE_HIGH, GATE_IN_ACTIVE_LOW,TRIGGER_IN_RISING_EDGE, TRIGGER_IN_FALLING_EDGE,RO_TRIGGER_IN_RISING_EDGE, RO_TRIGGER_IN_FALLING_EDGE, GATE_OUT_ACTIVE_HIGH, GATE_OUT_ACTIVE_LOW, TRIGGER_OUT_RISING_EDGE, TRIGGER_OUT_FALLING_EDGE, RO_TRIGGER_OUT_RISING_EDGE,RO_TRIGGER_OUT_FALLING_EDGE}; + + int off=d*SIGNAL_OFFSET; + int mode=((bus_r(EXT_SIGNAL_REG)&(SIGNAL_MASK<>off); + + if (mode<=RO_TRIGGER_OUT_FALLING_EDGE) { + if (modes[mode]!=SIGNAL_OFF && signals[d]!=MASTER_SLAVE_SYNCHRONIZATION) + signals[d]=modes[mode]; +#ifdef VERYVERBOSE + printf("gettings signal register number %d value %04x\n", d, modes[mode]); +#endif + return modes[mode]; + } else + return -1; + +} + + + + + +/* +enum externalCommunicationMode{ + GET_EXTERNAL_COMMUNICATION_MODE, + AUTO, + TRIGGER_EXPOSURE_SERIES, + TRIGGER_EXPOSURE_BURST, + TRIGGER_READOUT, + TRIGGER_COINCIDENCE_WITH_INTERNAL_ENABLE, + GATE_FIX_NUMBER, + GATE_FIX_DURATION, + GATE_WITH_START_TRIGGER, + GATE_COINCIDENCE_WITH_INTERNAL_ENABLE +}; +*/ + + +int setTiming(int ti) { + + + int ret=GET_EXTERNAL_COMMUNICATION_MODE; + + int g=-1, t=-1, rot=-1; + + int i; + + switch (ti) { + case AUTO_TIMING: + timingMode=ti; + // disable all gates/triggers in except if used for master/slave synchronization + for (i=0; i<4; i++) { + if (getFPGASignal(i)>0 && getFPGASignal(i)=0 && t>=0 && rot<0) { + ret=GATE_WITH_START_TRIGGER; + } else if (g<0 && t>=0 && rot<0) { + ret=TRIGGER_EXPOSURE; + } else if (g>=0 && t<0 && rot<0) { + ret=GATE_FIX_NUMBER; + } else if (g<0 && t<0 && rot>0) { + ret=TRIGGER_READOUT; + } else if (g<0 && t<0 && rot<0) { + ret=AUTO_TIMING; + } + + // timingMode=ret; + + return ret; + +} + + + +int setConfigurationRegister(int d) { +#ifdef VERBOSE + printf("Setting configuration register to %x",d); +#endif + if (d>=0) { + bus_w(CONFIG_REG,d); + } +#ifdef VERBOSE + printf("configuration register is %x", bus_r(CONFIG_REG)); +#endif + return bus_r(CONFIG_REG); +} + +int setToT(int d) { + //int ret=0; + int reg; +#ifdef VERBOSE + printf("Setting ToT to %d\n",d); +#endif + reg=bus_r(CONFIG_REG); +#ifdef VERBOSE + printf("Before: ToT is %x\n", reg); +#endif + if (d>0) { + bus_w(CONFIG_REG,reg|TOT_ENABLE_BIT); + } else if (d==0) { + bus_w(CONFIG_REG,reg&(~TOT_ENABLE_BIT)); + } + reg=bus_r(CONFIG_REG); +#ifdef VERBOSE + printf("ToT is %x\n", reg); +#endif + if (reg&TOT_ENABLE_BIT) + return 1; + else + return 0; +} + +int setContinousReadOut(int d) { + //int ret=0; + int reg; +#ifdef VERBOSE + printf("Setting Continous readout to %d\n",d); +#endif + reg=bus_r(CONFIG_REG); +#ifdef VERBOSE + printf("Before: Continous readout is %x\n", reg); +#endif + if (d>0) { + bus_w(CONFIG_REG,reg|CONT_RO_ENABLE_BIT); + } else if (d==0) { + bus_w(CONFIG_REG,reg&(~CONT_RO_ENABLE_BIT)); + } + reg=bus_r(CONFIG_REG); +#ifdef VERBOSE + printf("Continous readout is %x\n", reg); +#endif + if (reg&CONT_RO_ENABLE_BIT) + return 1; + else + return 0; +} + + +int startReceiver(int start) { + u_int32_t addr=CONFIG_REG; +#ifdef VERBOSE + if(start) + printf("Setting up detector to send to Receiver\n"); + else + printf("Setting up detector to send to CPU\n"); +#endif + int reg=bus_r(addr); + //for start recever, write 0 and for stop, write 1 + if (!start) + bus_w(CONFIG_REG,reg|CPU_OR_RECEIVER_BIT); + else + bus_w(CONFIG_REG,reg&(~CPU_OR_RECEIVER_BIT)); + + reg=bus_r(addr); +//#ifdef VERBOSE + printf("Config Reg %x\n", reg); +//#endif + int d =reg&CPU_OR_RECEIVER_BIT; + if(d!=0) d=1; + if(d!=start) + return OK; + else + return FAIL; +} + + +u_int64_t getDetectorNumber() { + char output[255],mac[255]=""; + u_int64_t res=0; + FILE* sysFile = popen("ifconfig eth0 | grep HWaddr | cut -d \" \" -f 11", "r"); + fgets(output, sizeof(output), sysFile); + pclose(sysFile); + //getting rid of ":" + char * pch; + pch = strtok (output,":"); + while (pch != NULL){ + strcat(mac,pch); + pch = strtok (NULL, ":"); + } + sscanf(mac,"%llx",&res); + return res; +} + +u_int32_t getFirmwareVersion() { + return bus_r(FPGA_VERSION_REG); +} + +u_int32_t getFirmwareSVNVersion(){ + return bus_r(FPGA_SVN_REG); +} + + +// for fpga test +u_int32_t testFpga(void) { + printf("Test FPGA:\n"); + volatile u_int32_t val,addr,val2; + int result=OK,i; + //fixed pattern + val=bus_r(FIX_PATT_REG); + if (val==FIXED_PATT_VAL) { + printf("fixed pattern ok!! %08x\n",val); + } else { + printf("fixed pattern wrong!! %08x\n",val); + result=FAIL; + } + + //dummy register + addr = DUMMY_REG; + for(i=0;i<1000000;i++) + { + val=0x5A5A5A5A-i; + bus_w(addr, val); + val=bus_r(addr); + if (val!=0x5A5A5A5A-i) { + printf("ATTEMPT:%d:\tFPGA dummy register wrong!! %x instead of %x \n",i,val,0x5A5A5A5A-i); + result=FAIL; + } + val=(i+(i<<10)+(i<<20)); + bus_w(addr, val); + val2=bus_r(addr); + if (val2!=val) { + printf("ATTEMPT:%d:\tFPGA dummy register wrong!! read %x instead of %x.\n",i,val2,val); + result=FAIL; + } + val=0x0F0F0F0F; + bus_w(addr, val); + val=bus_r(addr); + if (val!=0x0F0F0F0F) { + printf("ATTEMPT:%d:\tFPGA dummy register wrong!! %x instead of 0x0F0F0F0F \n",i,val); + result=FAIL; + } + val=0xF0F0F0F0; + bus_w(addr, val); + val=bus_r(addr); + if (val!=0xF0F0F0F0) { + printf("ATTEMPT:%d:\tFPGA dummy register wrong!! %x instead of 0xF0F0F0F0 \n\n",i,val); + result=FAIL; + } + } + if(result==OK) + { + printf("----------------------------------------------------------------------------------------------"); + printf("\nATTEMPT 1000000: FPGA DUMMY REGISTER OK!!!\n"); + printf("----------------------------------------------------------------------------------------------\n"); + } + return result; +} + + +// for fpga test +u_int32_t testRAM(void) { + int result=OK; + int i=0; + allocateRAM(); + // while(i<100000) { + memcpy(ram_values, values, dataBytes); + printf ("Test RAM:\t%d: copied fifo %x to memory %x size %d\n",i++, (unsigned int)(values), (unsigned int)(ram_values), dataBytes); + // } + return result; +} + +int getNModBoard() { + return nModX; +} + +int setNMod(int n) { + return nModX; +} + + +// fifo test +int testFifos(void) { + printf("Fifo test not implemented!\n"); + bus_w16(CONTROL_REG, START_FIFOTEST_BIT); + bus_w16(CONTROL_REG, 0x0); + return OK; +} + + + +// program dacq settings + +int64_t set64BitReg(int64_t value, int aLSB, int aMSB){ + int64_t v64; + u_int32_t vLSB,vMSB; + if (value!=-1) { + vLSB=value&(0xffffffff); + bus_w(aLSB,vLSB); + v64=value>> 32; + vMSB=v64&(0xffffffff); + bus_w(aMSB,vMSB); + } + 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; + return v64; +} + +int64_t setFrames(int64_t value){ + return set64BitReg(value, SET_FRAMES_LSB_REG, SET_FRAMES_MSB_REG); +} + +int64_t getFrames(){ + return get64BitReg(GET_FRAMES_LSB_REG, GET_FRAMES_MSB_REG); +} + +int64_t setExposureTime(int64_t value){ + /* time is in ns */ + if (value!=-1) + value*=(1E-9*CLK_FREQ); + return set64BitReg(value,SET_EXPTIME_LSB_REG, SET_EXPTIME_MSB_REG)/(1E-9*CLK_FREQ); +} + +int64_t getExposureTime(){ + return get64BitReg(GET_EXPTIME_LSB_REG, GET_EXPTIME_MSB_REG)/(1E-9*CLK_FREQ); +} + +int64_t setGates(int64_t value){ + return set64BitReg(value, SET_GATES_LSB_REG, SET_GATES_MSB_REG); +} + +int64_t getGates(){ + return get64BitReg(GET_GATES_LSB_REG, GET_GATES_MSB_REG); +} + +int64_t setPeriod(int64_t value){ + /* time is in ns */ + if (value!=-1) { + value*=(1E-9*CLK_FREQ); + } + + + + return set64BitReg(value,SET_PERIOD_LSB_REG, SET_PERIOD_MSB_REG)/(1E-9*CLK_FREQ); +} + +int64_t getPeriod(){ + return get64BitReg(GET_PERIOD_LSB_REG, GET_PERIOD_MSB_REG)/(1E-9*CLK_FREQ); +} + +int64_t setDelay(int64_t value){ + /* time is in ns */ + if (value!=-1) { + value*=(1E-9*CLK_FREQ); + } + return set64BitReg(value,SET_DELAY_LSB_REG, SET_DELAY_MSB_REG)/(1E-9*CLK_FREQ); +} + +int64_t getDelay(){ + return get64BitReg(GET_DELAY_LSB_REG, GET_DELAY_MSB_REG)/(1E-9*CLK_FREQ); +} + +int64_t setTrains(int64_t value){ + return set64BitReg(value, SET_TRAINS_LSB_REG, SET_TRAINS_MSB_REG); +} + +int64_t getTrains(){ + return get64BitReg(GET_TRAINS_LSB_REG, GET_TRAINS_MSB_REG); +} + + +int64_t setProbes(int64_t value){ + return 0; +} + + +int64_t setProgress() { + + //????? eventually call after setting the registers + +return 0; + +} + + +int64_t getProgress() { + + + //should be done in firmware!!!! + + return 0; + +} + +int64_t getActualTime(){ + return get64BitReg(GET_ACTUAL_TIME_LSB_REG, GET_ACTUAL_TIME_MSB_REG)/(1E-9*CLK_FREQ); +} + +int64_t getMeasurementTime(){ + int64_t v=get64BitReg(GET_MEASUREMENT_TIME_LSB_REG, GET_MEASUREMENT_TIME_MSB_REG); + int64_t mask=0x8000000000000000; + if (v & mask ) { +#ifdef VERBOSE + printf("no measurement time left\n"); +#endif + return -1E+9; + } else + return v/(1E-9*CLK_FREQ); +} + + + + +int loadImage(int index, short int ImageVals[]){ + u_int32_t address; + switch (index) { + case DARK_IMAGE : + address = DARK_IMAGE_REG; + break; + case GAIN_IMAGE : + address = GAIN_IMAGE_REG; + break; + } + volatile u_int16_t *ptr; + ptr=(u_int16_t*)(CSP0BASE+address*2); +#ifdef VERBOSE + int i; + for(i=0;i<6;i++) + printf("%d:%d\t",i,ImageVals[i]); +#endif + memcpy(ptr,ImageVals ,dataBytes); +#ifdef VERBOSE + printf("\nLoaded x%08x address with image of index %d\n",(unsigned int)(ptr),index); +#endif + return OK; +} + + + +int64_t getProbes(){ + return 0; +} + + +int setDACRegister(int idac, int val, int imod) { + u_int32_t addr, reg, mask; + int off; +#ifdef VERBOSE + if(val==-1) + printf("Getting dac register%d module %d\n",idac,imod); + else + printf("Setting dac register %d module %d to %d\n",idac,imod,val); +#endif + + switch(idac){ + case 0: + case 1: + case 2: + addr=MOD_DACS1_REG; + break; + case 3: + case 4: + case 5: + addr=MOD_DACS2_REG; + break; + case 6: + case 7: + addr=MOD_DACS3_REG; + break; + default: + printf("weird idac value %d\n",idac); + return -1; + break; + } + //saving only the msb + val=val>>2; + + off=(idac%3)*10; + mask=~((0x3ff)<=0 && val>off)&0x3ff; + //since we saved only the msb + val=val<<2; + + //val=(bus_r(addr)>>off)&0x3ff; + + +#ifdef VERBOSE + printf("Dac %d module %d register is %d\n\n",idac,imod,val); +#endif + return val; +} + + +int getTemperature(int tempSensor, int imod){ + int val; + imod=0;//ignoring more than 1 mod for now + int i,j,repeats=6; + u_int32_t tempVal=0; +#ifdef VERBOSE + char cTempSensor[2][100]={"ADCs/ASICs","VRs/FPGAs"}; + printf("Getting Temperature of module:%d for the %s for tempsensor:%d\n",imod,cTempSensor[tempSensor],tempSensor); +#endif + bus_w(TEMP_IN_REG,(T1_CLK_BIT)|(T1_CS_BIT)|(T2_CLK_BIT)|(T2_CS_BIT));//standby + bus_w(TEMP_IN_REG,((T1_CLK_BIT)&~(T1_CS_BIT))|(T2_CLK_BIT));//high clk low cs + + for(i=0;i<20;i++) { + //repeats is number of register writes for delay + for(j=0;j>1);//fpga + } + } + + bus_w(TEMP_IN_REG,(T1_CLK_BIT)|(T1_CS_BIT)|(T2_CLK_BIT)|(T2_CS_BIT));//standby + val=((int)tempVal)/4.0; + +#ifdef VERBOSE + printf("Temperature of module:%d for the %s is %.2fC\n",imod,cTempSensor[tempSensor],val); +#endif + return val; +} + + + +int initHighVoltage(int val, int imod){ +#ifdef VERBOSE + printf("Setting/Getting High Voltage of module:%d with val:%d\n",imod,val); +#endif + volatile u_int32_t addr=HV_REG; + int writeVal,writeVal2; + switch(val){ + case -1: break; + case 0: writeVal=0x0; writeVal2=0x0; break; + case 90: writeVal=0x0; writeVal2=0x1; break; + case 110:writeVal=0x2; writeVal2=0x3; break; + case 120:writeVal=0x4; writeVal2=0x5; break; + case 150:writeVal=0x6; writeVal2=0x7; break; + case 180:writeVal=0x8; writeVal2=0x9; break; + case 200:writeVal=0xA; writeVal2=0xB; break; + default :printf("Invalid voltage\n");return -2;break; + } + //to set value + if(val!=-1){ + //set value to converted value + bus_w(addr,writeVal); + bus_w(addr,writeVal2); +#ifdef VERBOSE + printf("Value sent is %d and then %d\n",writeVal,writeVal2); +#endif + } + //read value and return the converted value + val=bus_r(addr); +#ifdef VERBOSE + printf("Value read from reg is %d\n",val); +#endif + switch(val){ + case 0x0:val=0;break; + case 0x1:val=90;break; + case 0x3:val=110;break; + case 0x5:val=120;break; + case 0x7:val=150;break; + case 0x9:val=180;break; + case 0xB:val=200;break; + default:printf("Weird value read:%d\n",val);return -3;break; + } +#ifdef VERBOSE + printf("High voltage of module:%d is %d\n",imod,val); +#endif + return val; +} + + + +int initConfGain(int isettings,int val,int imod){ + int retval; + u_int32_t addr=GAIN_REG; + + if(val!=-1){ +#ifdef VERBOSE + printf("Setting Gain of module:%d with val:%d\n",imod,val); +#endif + bus_w(addr,((val<>SETTINGS_OFFSET); +#ifdef VERBOSE + printf("Settings read from reg is %d\n",retval); +#endif + if((isettings!=-1)&&(retval!=isettings)){ + printf("\n\nSettings r\n\n"); + return -1; + } + + return retval; +} + + + +int setADC(int adc){ + int reg,nchips,mask; + + if(adc==-1) ROI_flag=0; + else ROI_flag=1; + + setDAQRegister();//token timing + cleanFifo();//adc sync + + //all adc + if(adc==-1){ + //set packet size + ipPacketSize= DEFAULT_IP_PACKETSIZE; + udpPacketSize=DEFAULT_UDP_PACKETSIZE; + //set channel mask + nchips = NCHIP; + mask = ACTIVE_ADC_MASK; + } + //1 adc + else{ + ipPacketSize= ADC1_IP_PACKETSIZE; + udpPacketSize=ADC1_UDP_PACKETSIZE; + //set channel mask + nchips = NCHIPS_PER_ADC; + mask = 1<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); +#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 getAdcConfigured(){ + return adcConfigured; +} + +u_int32_t runBusy(void) { + u_int32_t s = bus_r(STATUS_REG); + return s; +} + +u_int32_t dataPresent(void) { + return bus_r(LOOK_AT_ME_REG); +} + +u_int32_t runState(void) { + int s=bus_r(STATUS_REG); +#ifdef SHAREDMEMORY + if (s&RUN_BUSY_BIT) + write_status_sm("Running"); + else + write_status_sm("Stopped"); +#endif +#ifdef VERBOSE + printf("status %04x\n",s); +#endif + +/* if (s==0x62001) + exit(-1);*/ + return s; +} + + +// State Machine + +int startStateMachine(){ + +//#ifdef VERBOSE + printf("*******Starting State Machine***************\n"); +//#endif + cleanFifo(); + // fifoReset(); printf("Starting State Machine\n"); + now_ptr=(char*)ram_values; +#ifdef SHAREDMEMORY + write_stop_sm(0); + write_status_sm("Started"); +#endif +/* +#ifdef MCB_FUNCS + setCSregister(ALLMOD); + clearSSregister(ALLMOD); +#endif +*/ + //putout("0000000000000000",ALLMOD); + bus_w16(CONTROL_REG, START_ACQ_BIT | START_EXPOSURE_BIT); + bus_w16(CONTROL_REG, 0x0); + printf("statusreg=%08x\n",bus_r(STATUS_REG)); + return OK; +} + + + + +int stopStateMachine(){ + +#ifdef VERBOSE + printf("Stopping State Machine\n"); +#endif +#ifdef SHAREDMEMORY + write_stop_sm(1); + write_status_sm("Stopped"); +#endif + bus_w16(CONTROL_REG, STOP_ACQ_BIT); + bus_w16(CONTROL_REG, 0x0); + usleep(500); + // if (!runBusy()) + if(!(bus_r(STATUS_REG)&RUNMACHINE_BUSY_BIT)) + return OK; + else + return FAIL; +} + + +int startReadOut(){ + u_int32_t status; +#ifdef VERBOSE + printf("Starting State Machine Readout\n"); +#endif + status=bus_r(STATUS_REG)&RUN_BUSY_BIT; +#ifdef DEBUG + printf("State machine status is %08x\n",bus_r(STATUS_REG)); +#endif + bus_w16(CONTROL_REG, START_ACQ_BIT |START_READOUT_BIT); // start readout + bus_w16(CONTROL_REG, 0x0); + return OK; +} + + +// fifo routines + +u_int32_t fifoReset(void) { + return -1; +} + + +u_int32_t setNBits(u_int32_t n) { + return -1; +} + +u_int32_t getNBits(){ + return -1; +} + + +u_int32_t fifoReadCounter(int fifonum){ + return -1; +} + +u_int32_t fifoReadStatus() +{ + // reads from the global status register + + return bus_r(STATUS_REG)&(SOME_FIFO_FULL_BIT | ALL_FIFO_EMPTY_BIT); +} + +u_int32_t fifo_full(void) +{ + // checks fifo empty flag returns 1 if fifo is empty + // otherwise 0 + return bus_r(STATUS_REG)&SOME_FIFO_FULL_BIT; +} + + +u_int32_t* fifo_read_event() +{ +#ifdef VIRTUAL + return NULL; +#endif + +#ifdef VERBOSE + printf("before looping\n"); +#endif + volatile u_int32_t t = bus_r(LOOK_AT_ME_REG); + +#ifdef VERBOSE + printf("lookatmereg=x%x\n",t); +#endif +/* + while ((t&0x1)==0) + { + t = bus_r(LOOK_AT_ME_REG); + if (!runBusy()){ + return NULL; + } + } +*/ + + while((t&0x1)==0) { + if (runBusy()==0) { + t = bus_r(LOOK_AT_ME_REG); + if ((t&0x1)==0) { +#ifdef VERBOSE + printf("no frame found - exiting "); + printf("%08x %08x\n", runState(), bus_r(LOOK_AT_ME_REG)); +#endif + return NULL; + } else { +#ifdef VERBOSE + printf("no frame found %x status %x\n", bus_r(LOOK_AT_ME_REG),runState()); +#endif + break; + } + } + t = bus_r(LOOK_AT_ME_REG); + } + + +#ifdef VERBOSE + printf("before readout %08x %08x\n", runState(), bus_r(LOOK_AT_ME_REG)); +#endif + + dma_memcpy(now_ptr,values ,dataBytes); + + +#ifdef VERYVERBOSE + int a; + for (a=0;a<8; a=a+2) + printf("\n%d %d: x%04x x%04x ",a+1,a,*(now_ptr+a+1),*(now_ptr+a) ); + for (a=2554;a<2560; a=a+2) + printf("\n%d %d: x%04x x%04x ",a+1,a,*(now_ptr+a+1),*(now_ptr+a) ); + printf("********\n"); + //memcpy(now_ptr, values, dataBytes); +#endif +#ifdef VERBOSE + printf("Copying to ptr %08x %d\n",(unsigned int)(now_ptr), dataBytes); + printf("after readout %08x %08x\n", runState(), bus_r(LOOK_AT_ME_REG)); +#endif + + if (storeInRAM>0) { + now_ptr+=dataBytes; + } + return ram_values; +} + + + +u_int32_t* decode_data(int *datain) +{ + u_int32_t *dataout; + // const char one=1; + const int bytesize=8; + char *ptr=(char*)datain; + //int nbits=dynamicRange; + int ipos=0, ichan=0;; + //int nch, boff=0; + int ibyte;//, ibit; + char iptr; + +#ifdef VERBOSE + printf("Decoding data for DR %d\n",dynamicRange); +#endif + dataout=malloc(nChans*nChips*nModX*4); + ichan=0; + switch (dynamicRange) { + case 1: + for (ibyte=0; ibyte>(ipos))&0x1; + ichan++; + } + } + break; + case 4: + for (ibyte=0; ibyte>(ipos*4))&0xf; + ichan++; + } + } + break; + case 8: + for (ichan=0; ichan0) { + nm=setNMod(-1); + if (dr==1) { + dynamicRange=1; + ow=5; + } else if (dr<=4) { + dynamicRange=4; + ow=4; + } else if (dr<=8) { + dynamicRange=8; + ow=3; + } else if (dr<=16) { + dynamicRange=16; + ow=2; + } else { + dynamicRange=32; + ow=0; //or 1? + } + setCSregister(ALLMOD); + initChipWithProbes(0, ow,np, ALLMOD); + putout("0000000000000000",ALLMOD); + setNMod(nm); + } + */ + + return getDynamicRange(); +} + + + + + + +int getDynamicRange() { + /* + int dr; + u_int32_t shiftin=bus_r(GET_SHIFT_IN_REG); + u_int32_t outmux=(shiftin >> OUTMUX_OFF) & OUTMUX_MASK; + u_int32_t probes=(shiftin >> PROBES_OFF) & PROBES_MASK; +#ifdef VERYVERBOSE + printf("%08x ",shiftin); + printf("outmux=%02x probes=%d\n",outmux,probes); +#endif + + switch (outmux) { + case 2: + dr=16; + break; + case 4: + dr=8; + break; + case 8: + dr=4; + break; + case 16: + dr=1; + break; + default: + dr=32; + } + dynamicRange=dr; + if (probes==0) { + dataBytes=nModX*nModY*NCHIP*NCHAN*dynamicRange/8; + } else { + dataBytes=nModX*nModY*NCHIP*NCHAN*4;/// + } +#ifdef VERBOSE + printf("Number of data bytes %d - probes %d dr %d\n", dataBytes, probes, dr); +#endif + if (allocateRAM()==OK) { + ; + } else + printf("ram not allocated\n"); +*/ + dynamicRange=16; + return dynamicRange; + +} + +int testBus() { + u_int32_t j; + u_int64_t i, n, nt; + // char cmd[100]; + u_int32_t val=0x0; + int ifail=OK; + // printf("%s\n",cmd); + // system(cmd); + i=0; + + n=1000000; + nt=n/100; + printf("testing bus %d times\n",(int)n); + while (i0) + storeInRAM=1; + else + storeInRAM=0; + return allocateRAM(); +} + + +int allocateRAM() { + size_t size; + u_int32_t nt, nf; + nt=setTrains(-1); + nf=setFrames(-1); + if (nt==0) nt=1; + if (nf==0) nf=1; + // ret=clearRAM(); + if (storeInRAM) { + size=dataBytes*nf*nt; + if (size>(23-i))&0x1)<> 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; +} + diff --git a/slsDetectorSoftware/moenchDetectorServer/firmware_funcs.h b/slsDetectorSoftware/moenchDetectorServer/firmware_funcs.h new file mode 100755 index 000000000..975342e92 --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/firmware_funcs.h @@ -0,0 +1,177 @@ +#ifndef FIRMWARE_FUNCS_H +#define FIRMWARE_FUNCS_H + + +#include "sls_detector_defs.h" + + +#include +#include +#include +#include +#include +#include +#include +//#include +#include +#include +#include +#include +#include +#include + + +int mapCSP0(void); + +u_int16_t bus_r16(u_int32_t offset); +u_int16_t bus_w16(u_int32_t offset, u_int16_t data);//aldos function +u_int32_t bus_w(u_int32_t offset, u_int32_t data); +u_int32_t bus_r(u_int32_t offset); + +int setPhaseShiftOnce(); +int cleanFifo(); +int setDAQRegister(); + +u_int32_t putout(char *s, int modnum); +u_int32_t readin(int modnum); +u_int32_t setClockDivider(int d); +u_int32_t getClockDivider(); +u_int32_t setSetLength(int d); +u_int32_t getSetLength(); +u_int32_t setWaitStates(int d); +u_int32_t getWaitStates(); +u_int32_t setTotClockDivider(int d); +u_int32_t getTotClockDivider(); +u_int32_t setTotDutyCycle(int d); +u_int32_t getTotDutyCycle(); + +u_int32_t setExtSignal(int d, enum externalSignalFlag mode); +int getExtSignal(int d); + +u_int32_t setFPGASignal(int d, enum externalSignalFlag mode); +int getFPGASignal(int d); + +int setTiming(int t); + + +int setConfigurationRegister(int d); +int setToT(int d); +int setContinousReadOut(int d); +int startReceiver(int d); + +int setDACRegister(int idac, int val, int imod); + +int getTemperature(int tempSensor,int imod); +int initHighVoltage(int val,int imod); +int initConfGain(int isettings,int val,int imod); + +int setADC(int adc); +int configureMAC(int ipad, long long int macad, long long int detectormacadd, int detipad, int ival, int udpport); +int getAdcConfigured(); + + +u_int64_t getDetectorNumber(); +u_int32_t getFirmwareVersion(); +int testFifos(void); +u_int32_t testFpga(void); +u_int32_t testRAM(void); +int testBus(void); +int setDigitalTestBit(int ival); + +int64_t set64BitReg(int64_t value, int aLSB, int aMSB); +int64_t get64BitReg(int aLSB, int aMSB); + +int64_t setFrames(int64_t value); +int64_t getFrames(); + +int64_t setExposureTime(int64_t value); +int64_t getExposureTime(); + +int64_t setGates(int64_t value); +int64_t getGates(); + +int64_t setDelay(int64_t value); +int64_t getDelay(); + +int64_t setPeriod(int64_t value); +int64_t getPeriod(); + +int64_t setTrains(int64_t value); +int64_t getTrains(); + +int64_t setProbes(int64_t value); +int64_t getProbes(); + +int64_t getProgress(); +int64_t setProgress(); + +int64_t getActualTime(); +int64_t getMeasurementTime(); + + +u_int32_t runBusy(void); +u_int32_t runState(void); +u_int32_t dataPresent(void); + + +int startStateMachine(); +int stopStateMachine(); +int startReadOut(); +u_int32_t fifoReset(void); +u_int32_t fifoReadCounter(int fifonum); +u_int32_t fifoReadStatus(); + + +u_int32_t fifo_full(void); + + + +u_int32_t* fifo_read_event(); +u_int32_t* decode_data(int* datain); +//u_int32_t move_data(u_int64_t* datain, u_int64_t* dataout); +int setDynamicRange(int dr); +int getDynamicRange(); +int getNModBoard(); +int setNMod(int n); +int setStoreInRAM(int b); +int allocateRAM(); +int clearRAM(); + + +int setMaster(int f); +int setSynchronization(int s); + +int loadImage(int index, short int ImageVals[]); +int readCounterBlock(int startACQ, short int CounterVals[]); +int resetCounterBlock(int startACQ); + +int calibratePedestal(int frames); + + + +/* + +u_int32_t setNBits(u_int32_t); +u_int32_t getNBits(); +*/ + +/* +//move to mcb_funcs? + +int readOutChan(int *val); +u_int32_t getModuleNumber(int modnum); +int testShiftIn(int imod); +int testShiftOut(int imod); +int testShiftStSel(int imod); +int testDataInOut(int num, int imod); +int testExtPulse(int imod); +int testExtPulseMux(int imod, int ow); +int testDataInOutMux(int imod, int ow, int num); +int testOutMux(int imod); +int testFpgaMux(int imod); +int calibration_sensor(int num, int *values, int *dacs) ; +int calibration_chip(int num, int *values, int *dacs); +*/ + + +#endif diff --git a/slsDetectorSoftware/moenchDetectorServer/mcb_funcs.c b/slsDetectorSoftware/moenchDetectorServer/mcb_funcs.c new file mode 100755 index 000000000..e9352359f --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/mcb_funcs.c @@ -0,0 +1,2649 @@ +#ifdef MCB_FUNCS + +#include +#include +#include +#include +#include +#include "registers_g.h" + +#ifndef PICASSOD +#include "server_defs.h" +#else +#include "picasso_defs.h" +#endif +#include "firmware_funcs.h" +#include "mcb_funcs.h" + +/* global variables */ +#undef DEBUG +#undef DEBUGOUT + +extern int nModX; +//extern int dataBytes; +extern int dynamicRange; +const int nChans=NCHAN; +const int nChips=NCHIP; +const int nDacs=NDAC; +const int nAdcs=NADC; +const enum detectorType myDetectorType=GOTTHARD; +enum detectorSettings thisSettings; + +int sChan, sChip, sMod, sDac, sAdc; +const int allSelected=-2; +const int noneSelected=-1; + + +sls_detector_module *detectorModules=NULL; +int *detectorChips=NULL; +int *detectorChans=NULL; +int *detectorDacs=NULL; +int *detectorAdcs=NULL; +//int numberOfProbes; + +ROI rois[MAX_ROIS]; +int nROI=0; + + +int initDetector() { + + int imod; + // sls_detector_module *myModule; + int n=getNModBoard(); + nModX=n; + +#ifdef VERBOSE + printf("Board is for %d modules\n",n); +#endif + detectorModules=malloc(n*sizeof(sls_detector_module)); + detectorChips=malloc(n*NCHIP*sizeof(int)); + detectorChans=malloc(n*NCHIP*NCHAN*sizeof(int)); + detectorDacs=malloc(n*NDAC*sizeof(int)); + detectorAdcs=malloc(n*NADC*sizeof(int)); +#ifdef VERBOSE + printf("modules from 0x%x to 0x%x\n",(unsigned int)(detectorModules), (unsigned int)(detectorModules+n)); + printf("chips from 0x%x to 0x%x\n",(unsigned int)(detectorChips), (unsigned int)(detectorChips+n*NCHIP)); + printf("chans from 0x%x to 0x%x\n",(unsigned int)(detectorChans), (unsigned int)(detectorChans+n*NCHIP*NCHAN)); + printf("dacs from 0x%x to 0x%x\n",(unsigned int)(detectorDacs), (unsigned int)(detectorDacs+n*NDAC)); + printf("adcs from 0x%x to 0x%x\n",(unsigned int)(detectorAdcs), (unsigned int)(detectorAdcs+n*NADC)); +#endif + for (imod=0; imoddacs=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; + (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); + */ + + /* initialize dynamic range etc. */ + /* 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; +} + + + + +int copyChannel(sls_detector_channel *destChan, sls_detector_channel *srcChan) { + destChan->chan=srcChan->chan; + destChan->chip=srcChan->chip; + destChan->module=srcChan->module; + destChan->reg=srcChan->reg; + return OK; +} + + +int copyChip(sls_detector_chip *destChip, sls_detector_chip *srcChip) { + + int ichan; + int ret=OK; + if ((srcChip->nchan)>(destChip->nchan)) { + printf("Number of channels of source is larger than number of channels of destination\n"); + return FAIL; + } + + destChip->nchan=srcChip->nchan; + destChip->reg=srcChip->reg; + destChip->chip=srcChip->chip; + destChip->module=srcChip->module; + for (ichan=0; ichan<(srcChip->nchan); ichan++) { + *((destChip->chanregs)+ichan)=*((srcChip->chanregs)+ichan); + } + return ret; +} + + +int copyModule(sls_detector_module *destMod, sls_detector_module *srcMod) { + + int ichip, idac, ichan, iadc; + + int ret=OK; + +#ifdef VERBOSE + printf("Copying module %x to module %x\n",(unsigned int)(srcMod),(unsigned int)(destMod)); +#endif + + if (srcMod->module>=0) { +#ifdef VERBOSE + printf("Copying module number %d to module number %d\n",srcMod->module,destMod->module); +#endif + destMod->module=srcMod->module; + } + if (srcMod->serialnumber>=0){ +/* #ifdef VERBOSE */ +/* printf("Copying module serial number %x to module serial number %x\n",srcMod->serialnumber,destMod->serialnumber); */ +/* #endif */ + destMod->serialnumber=srcMod->serialnumber; + } + if ((srcMod->nchip)>(destMod->nchip)) { + printf("Number of chip of source is larger than number of chips of destination\n"); + return FAIL; + } + if ((srcMod->nchan)>(destMod->nchan)) { + printf("Number of channels of source is larger than number of channels of destination\n"); + return FAIL; + } + if ((srcMod->ndac)>(destMod->ndac)) { + printf("Number of dacs of source is larger than number of dacs of destination\n"); + return FAIL; + } + if ((srcMod->nadc)>(destMod->nadc)) { + printf("Number of dacs of source is larger than number of dacs of destination\n"); + return FAIL; + } + +#ifdef VERBOSE + printf("DACs: src %d, dest %d\n",srcMod->ndac,destMod->ndac); + printf("ADCs: src %d, dest %d\n",srcMod->nadc,destMod->nadc); + printf("Chips: src %d, dest %d\n",srcMod->nchip,destMod->nchip); + printf("Chans: src %d, dest %d\n",srcMod->nchan,destMod->nchan); + +#endif + + + + destMod->ndac=srcMod->ndac; + destMod->nadc=srcMod->nadc; + destMod->nchip=srcMod->nchip; + destMod->nchan=srcMod->nchan; + if (srcMod->reg>=0) + destMod->reg=srcMod->reg; +#ifdef VERBOSE + printf("Copying register %x (%x)\n",destMod->reg,srcMod->reg ); +#endif + if (srcMod->gain>=0) + destMod->gain=srcMod->gain; + if (srcMod->offset>=0) + destMod->offset=srcMod->offset; + + // 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); + } + 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; +} + + + +/* Register commands */ + + +int clearDACSregister(int imod) { + + putout("1111111111111111",imod);//reset + putout("1111111111111110",imod);//cs down + + /* commented out by dhanya + putout("0000000001000000",imod); + putout("0000000101000000",imod); + putout("0000000101000000",imod); + putout("0000000001000000",imod); + */ +#ifdef DEBUG + fprintf(stdout, "Clearing DAC shiftregister\n"); +#endif + // sDac=0; + sMod=imod; + if (imod==ALLMOD) + sMod=allSelected; + return OK; +} + +int nextDAC(int imod) { + + putout("1111111111111011",imod);//cs up + putout("1111111111111001",imod);//clk down + putout("1111111111111111",imod);//reset + + /*commented out by dhanya + putout("0000000001000000",imod); + putout("0000000001001000",imod); + putout("0000000001000000",imod); + */ +#ifdef DEBUG + fprintf(stdout, "Next DAC\n"); +#endif + // sDac++; + sMod=imod; + if (imod==ALLMOD) + sMod=allSelected; + return OK; +} + + +int clearCSregister(int imod) { + + putout("0000000001000000",imod); + putout("0000100001000000",imod); + putout("0000100001000000",imod); + putout("0000000001000000",imod); +#ifdef DEBUG + fprintf(stdout, "Clearing CS shiftregister\n"); +#endif + /* + sChan=noneSelected; + sMod=noneSelected; + sDac=noneSelected; + sAdc=noneSelected; + */ + sChip=noneSelected; + sMod=imod; + if (imod==ALLMOD) + sMod=allSelected; + //putout("0000000000000000",imod); + return 0; +} + +int setCSregister(int imod){ + + putout("0000000001000000",imod); + putout("0001000001000000",imod); + putout("0001000001000000",imod); + putout("0000000001000000",imod); +#ifdef DEBUG + fprintf(stdout, "Setting CS shiftregister\n"); +#endif + putout("0000000000000000",imod); + sChip=allSelected; + sMod=imod; + if (imod==ALLMOD) + sMod=allSelected; + return 0; +} + +int nextChip(int imod){ + + putout("0000000001000000",imod); + putout("0010000001000000",imod); + putout("0000000001000000",imod); +#ifdef DEBUG + fprintf(stdout, "Next Chip\n"); +#endif + sChip++; + sMod=imod; + if (imod==ALLMOD) + sMod=allSelected; + return 0; +} + +int firstChip(int imod){ + + putout("0100000001000000",imod); + putout("0110000001000000",imod); + putout("0100000001000000",imod); +#ifdef DEBUG + fprintf(stdout, "First Chip\n"); +#endif + sChip=0; + sMod=imod; + if (imod==ALLMOD) + sMod=allSelected; + return 0; +} + +int clearSSregister(int imod){ + int i; + putout("0000011000000000",imod); + for (i=0; i<10; i++) + putout("0000111000000000",imod); + putout("0000011000000000",imod); +#ifdef DEBUG + fprintf(stdout,"Clearing SS shiftregister\n"); +#endif + putout("0000000000000000",imod); + sChan=noneSelected; + sMod=imod; + if (imod==ALLMOD) + sMod=allSelected; + return 0; +} + +int setSSregister(int imod){ + int i; + putout("0000011000000000",imod); + for (i=0; i<10; i++) + putout("0001011000000000",imod); + putout("0000011000000000",imod); +#ifdef DEBUG + fprintf(stdout,"Setting SS shiftregister\n"); +#endif + putout("0000000000000000",imod); + sChan=allSelected; + sMod=imod; + if (imod==ALLMOD) + sMod=allSelected; + return 0; +} + +int nextStrip(int imod){ + putout("0000011000000000",imod); + putout("0010011000000000",imod); + putout("0000011000000000",imod); +#ifdef DEBUG + fprintf(stdout,"|-"); +#endif + sChan++; + sMod=imod; + if (imod==ALLMOD) + sMod=allSelected; + return 0; +} + +int selChannel(const int strip,int imod) { + int istrip; + clearSSregister(imod); + nextStrip(imod); + for (istrip=0; istrip=0 && imod=0) + initDAC(ind,val, imod); + + if (imod>=0 && imodgain,(detectorModules+imod)->offset); +#endif + if ((detectorModules+imod)->gain>0) + myg=(detectorModules+imod)->gain; + else { + if (thisSettings>=0 && thisSettings<3) + myg=g[thisSettings]; + // else + //myg=-1; + } + + if ((detectorModules+imod)->offset>0) + myo=(detectorModules+imod)->offset; + else { + if (thisSettings>=0 && thisSettings<3) + myo=o[thisSettings]; + // else + //myo=-1; + } + + if (myg>0 && myo>0) { + //ethr=(myo-detectorDacs[VTHRESH+imod*NDAC])*1000/myg; + + ethr=(myo-setDACRegister(VREF_DS,-1,imod))*1000/myg;//edited by dhanya + // else + // ethr=-1; + + } +#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=%d\n",imod, myg, myo,(int)(setDACRegister(VREF_DS,-1,imod)));//edited by dhanya + printf("Threshold energy of module %d is %d eV\n", imod, ethr); +#endif + + if (imod==0) + ret=ethr; + else { + if (ethr>(ret+100) || ethr<(ret-100)) + return FAIL; + } + } + } + return ret; +} + +int setThresholdEnergy(int ethr) { + double g[3]=DEFAULTGAIN; + double o[3]=DEFAULTOFFSET; + double myg=-1, myo=-1; + int dacu; + int imod; + int ret=ethr; + + setSettings(GET_SETTINGS,-1);//-1 added by dhanya + if (thisSettings>=0 || thisSettings<3){ + myg=g[thisSettings]; + myo=o[thisSettings]; + } + for (imod=0; imodgain>0) + myg=(detectorModules+imod)->gain; + else + if (thisSettings>=0 && thisSettings<3) + myg=g[thisSettings]; + else + myg=-1; + if ((detectorModules+imod)->offset>0) + myo=(detectorModules+imod)->offset; + else + if (thisSettings>=0 && thisSettings<3) + myo=o[thisSettings]; + else + myo=-1; + } else { + if (thisSettings>=0 && thisSettings<3) + myo=o[thisSettings]; + else + myo=-1; + if (thisSettings>=0 && thisSettings<3) + myg=g[thisSettings]; + else + myg=-1; + } + if (myg>0 && myo>0) { + dacu=myo-myg*((double)ethr)/1000.; +#ifdef VERBOSE + printf("module %d (%x): gain %f, off %f, energy %d eV, dac %d\n",imod,(unsigned int)((detectorModules+imod)),(detectorModules+imod)->gain,(detectorModules+imod)->offset, ethr,dacu); +#endif + } else { + dacu=ethr; +#ifdef VERBOSE + printf("could not set threshold energy for module %d, settings %d (offset is %f; gain is %f)\n",imod,thisSettings,myo,myg); +#endif + } + initDACbyIndexDACU(VREF_DS, dacu, imod); ///needs to be fixed dhanya + } + return ret; +} + + + +int getDACbyIndexDACU(int ind, int imod) { + /* + if (detectorDacs) { + if (imodndac) + return (detectorDacs[ind+imod*NDAC]); + } + return FAIL; + */ + return setDACRegister(ind, -1, imod); +} + + +int initDAC(int dac_addr, int value, int imod) { +// int i; +#ifdef VERBOSE + printf("Programming dac %d with value %d\n", dac_addr, value); +#endif + clearDACSregister(imod); + program_one_dac(dac_addr,value,imod); + nextDAC(imod); + clearDACSregister(imod); + + return 0; +} + +int getTemperatureByModule(int tempSensor, int imod) +{ + int im; + //for the particular module + if (imod>=0 && imod=0 && imod=0 && imod=0) { +#ifdef VERBOSE + fprintf(stdout, "voltage %d\n", *(v+iaddr)); +#endif + program_one_dac(iaddr, *(v+iaddr),imod); + } + nextDAC(imod); + } + + + clearDACSregister(imod); + + return 0; + +} + + + + +int setSettings(int i, int imod) { +#ifdef VERBOSE + if(i==-1) + printf("\nReading settings of detector...\n"); + else + printf("\ninside set settings wit settings=%d...\n",i); +#endif + int confgain[] = CONF_GAIN; + int isett=-2,retval; + + //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; +} + + + + +/* Initialization*/ + +int initChannelbyNumber(sls_detector_channel myChan) { + int reg=myChan.reg; + int ft=reg & TRIM_DR; + int cae=(reg>>(NTRIMBITS))&1; + int ae=(reg>>(NTRIMBITS+1))&1; + int coe=(reg>>(NTRIMBITS+2))&1; + int ocoe=(reg>>(NTRIMBITS+3))&1; + int counts=(reg>>(NTRIMBITS+4)); +#ifdef VERBOSE + printf("Initializing channel %d chip %d module %d reg %x\n",myChan.chan,myChan.chip,myChan.module, reg); + printf("trim %d, cae %d, ae %d, coe %d, ocoe %d, counts %d\n",ft, cae, ae, coe, ocoe, counts); +#endif + + if (myChan.chip<0) + setCSregister(myChan.module); + else + selChip(myChan.chip,myChan.module); + + if (myChan.chan<0) + setSSregister(myChan.module); + else + selChannel(myChan.chan,myChan.module); + + initChannel(ft,cae,ae, coe, ocoe, counts,myChan.module); + + setDynamicRange(dynamicRange); + + setCSregister(ALLMOD); + clearSSregister(ALLMOD); + putout("0000000000000000",ALLMOD); + + return myChan.reg; + +} + +int getChannelbyNumber(sls_detector_channel* myChan) { + int imod, ichip, ichan; + imod=myChan->module; + ichip=myChan->chip; + ichan=myChan->chan; + + 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]; + return OK; + } + } + return FAIL; + +} + +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 -1; +} + +int initChannel(int ft,int cae, int ae, int coe, int ocoe, int counts, int imod){ + + int ibit, bit, i, im, ichip, ichan; + int chanmi, chanma, chipmi, chipma, modmi, modma; + + + + sMod=imod; + // printf("initializing module %d\n",sMod); + if (imod==ALLMOD) { + sMod=allSelected; + + // printf("initializing all modules\n"); + } + + 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) { + // printf("initializing no channels ft=%d coe=%d\n",ft,coe); + chanmi=0; + chanma=-1; + } else { + // printf("initializing channel %d ft=%d coe=%d\n",sChan, ft,coe); + chanmi=sChan; + chanma=sChan+1; + } + + if (sChip==allSelected) { + // printf("initializing all chips\n"); + chipmi=0; + chipma=NCHIP; + } else if (sChip==noneSelected || sChip>NCHIP || sChip<0) { + // printf("initializing no chips\n"); + chipmi=0; + chipma=-1; + } else { + // printf("initializing chip %d\n",sChip); + chipmi=sChip; + chipma=sChip+1; + } + + + if (sMod==allSelected) { + modmi=0; + modma=nModX;//getNModBoard(); + } else if (sMod==noneSelected || sMod>nModX || sMod<0) {//(sMod==noneSelected || sMod>getNModBoard() || sMod<0) { + modmi=0; + modma=-1; + return 1; + } else { + modmi=sMod; + modma=sMod+1; + } + + if (detectorChans) { + for (im=modmi; im63 || ft<0) { + fprintf(stdout,"Fine Threshold is %d while should be between 0 and 63!",ft); + return 1; + } + /*cal_enable*/ + if (cae) { + putout("0100000000000000",imod); + putout("0110000000000000",imod); + } else { + putout("0000000000000000",imod); + putout("0010000000000000",imod); + } + /*n_an_enable*/ + if (ae) { + putout("0000000000000000",imod); + putout("0010000000000000",imod); + putout("0000000000000000",imod); + } else { + putout("0100000000000000",imod); + putout("0110000000000000",imod); + putout("0100000000000000",imod); + } + /*trb5*/ + ibit=5; + bit=ft & (1<>1; + int nchan, ichan; + int ft, cae, ae, coe, ocoe, counts, chanreg; + + + + nchan=myChip.nchan; + if (ichip<0) + setCSregister(imod); + else + selChip(ichip,imod); + + clearSSregister(imod); + for (ichan=0; ichan>(NTRIMBITS+1))&1; + ae=(chanreg>>(NTRIMBITS+2))&1; + coe=((chanreg)>>(NTRIMBITS+3))&1; + ocoe=((chanreg)>>(NTRIMBITS+4))&1; + counts=((chanreg)>>(NTRIMBITS+5)); + nextStrip(imod); + initChannel(ft,cae,ae, coe, ocoe, counts,imod); + } + initChip(obe,ow,imod); + return myChip.reg; + +} + +int getChipbyNumber(sls_detector_chip* myChip){ + int imod, ichip; + imod=myChip->module; + ichip=myChip->chip; + + if (detectorChips) { + if (imodnchip) { + myChip->reg=detectorChips[ichip+imod*NCHIP]; + myChip->nchan=NCHAN; + myChip->chanregs=detectorChans+imod*NCHAN*NCHIP+ichip*NCHIP; + return OK; + } + } + return FAIL; + +} + + + +int initChip(int obe, int ow,int imod){ + int i; + int im, ichip; + int chipmi, chipma, modmi, modma; + /* switch (ow) { + case 0:; + case 1: + setDynamicRange(32); + break; + case 2: + setDynamicRange(16); + break; + case 3: + setDynamicRange(8); + break; + case 4: + setDynamicRange(4); + break; + case 5: + setDynamicRange(1); + break; + default: + setDynamicRange(32); + break; + } + */ + +#ifdef DEBUGOUT + printf("Initializing chip\n"); +#endif + putout("0000000000000000",imod); +#ifdef DEBUGOUT + printf("Output mode= %d\n", ow); +#endif + + /* clearing shift in register */ + for (i=0; i<10; i++) + putout("0000100000000000",imod); + putout("0000000000000000",imod); + + if (ow>0) { + putout("0100000000000000",imod); + putout("0110000000000000",imod); + putout("0100000000000000",imod); + for (i=0; i<(OUTMUX_OFFSET-1); i++) { + putout("0000000000000000",imod); + putout("0010000000000000",imod); + putout("0000000000000000",imod); + } + if (ow>1) { + putout("0000000000000000",imod); + putout("0010000000000000",imod); + putout("0000000000000000",imod); + } + if (ow>2) { + putout("0000000000000000",imod); + putout("0010000000000000",imod); + putout("0000000000000000",imod); + } + if (ow>3) { + putout("0000000000000000",imod); + putout("0010000000000000",imod); + putout("0000000000000000",imod); + } + if (ow>4) { + putout("0000000000000000",imod); + putout("0010000000000000",imod); + putout("0000000000000000",imod); + } + } +#ifdef DEBUGOUT + printf("Output buffer enable= %d\n", obe); +#endif + if (obe) { + putout("0100000000000000",imod); + putout("0110000000000000",imod); + putout("0100000000000000",imod); + } else { + putout("0000000000000000",imod); + putout("0010000000000000",imod); + putout("0000000000000000",imod); + } + /*}*/ + putout("0000000000000000",imod); + + + + + + sMod=imod; + if (imod==ALLMOD) + sMod=allSelected; + + + if (sChip==allSelected) { + chipmi=0; + chipma=NCHIP; + } else if (sChip==noneSelected || sChip>NCHIP || sChip<0) { + chipmi=0; + chipma=-1; + } else { + chipmi=sChip; + chipma=sChip+1; + } + + + if (sMod==allSelected) { + modmi=0; + modma=nModX;//getNModBoard(); + } else if (sMod==noneSelected || sMod>nModX || sMod<0) {//(sMod==noneSelected || sMod>getNModBoard() || sMod<0) { + modmi=0; + modma=-1; + } else { + modmi=sMod; + modma=sMod+1; + } + + if (detectorChips) { + for (im=modmi; imNCHIP || sChip<0) { + chipmi=0; + chipma=-1; + } else { + chipmi=sChip; + chipma=sChip+1; + } + + + if (sMod==allSelected) { + modmi=0; + modma=nModX;//getNModBoard(); + } else if (sMod==noneSelected || sMod>nModX || sMod<0) {//(sMod==noneSelected || sMod>getNModBoard() || sMod<0) { + modmi=0; + modma=-1; + } else { + modmi=sMod; + modma=sMod+1; + } + + if (detectorChips) { + for (im=modmi; imnModX || sMod<0) {//(sMod==noneSelected || sMod>getNModBoard() || sMod<0) { + modmi=0; + modma=-1; + } else { + modmi=sMod; + modma=sMod+1; + } + + if (detectorModules) { + for (im=modmi; imreg)=cm; +#ifdef VERBOSE + printf("imod=%d reg=%d (%x)\n",im,(detectorModules+im)->reg,(unsigned int)((detectorModules+im))); +#endif + } + } + return 0; +} + +int initModulebyNumber(sls_detector_module myMod) { + + printf("\ninside initmoduleynumber..\n"); + + int nchip,nchan;//int ichip, nchip, ichan, nchan; + int im, modmi,modma; + // int ft, cae, ae, coe, ocoe, counts, chanreg; + int imod; + // int obe; + // int ow; + int v[NDAC]; + + + nchip=myMod.nchip; + nchan=(myMod.nchan)/nchip; + + imod=myMod.module; + sMod=imod; + + if (sMod==ALLMOD) + sMod=allSelected; + + if (sMod==allSelected) { + modmi=0; + modma=nModX;//getNModBoard(); + } else if (sMod==noneSelected || sMod>nModX || sMod<0) {// (sMod==noneSelected || sMod>getNModBoard() || sMod<0) { + modmi=0; + modma=-1; + } else { + modmi=sMod; + modma=sMod+1; + } + + + /* + for (idac=0; idacmodule; +#ifdef VERBOSE + printf("Getting module %d\n",imod); +#endif + if (detectorModules) { + copyModule(myMod,detectorModules+imod); + ; + } else + return FAIL; + + return OK; +} + +/* To chips */ +int clearCounter(int imod){ + int i; +#ifdef DEBUG + printf("Clearing counter with contclear\n"); +#endif + putout("0000000000000000",imod); + for (i=0; i<10; i++) + putout("0000000000010000",imod); + putout("0000000000000000",imod); + + return 0; +} + +int clearOutReg(int imod){ + int i; +#ifdef DEBUG + printf("Clearing output register\n"); +#endif + putout("0000010000000000",imod); + for (i=0; i<10; i++) + putout("0000110000000000",imod); + putout("0000010000000000",imod); + return 0; +} +int setOutReg(int imod){ + int i; +#ifdef DEBUG + printf("Setting output register\n"); +#endif + putout("0000010000000000",imod); + for (i=0; i<10; i++) + putout("0001010000000000",imod); + putout("0000010000000000",imod); + return 0; +} + + +int extPulse(int ncal, int imod) { + int ical; +#ifdef DEBUG + printf("Giving a clock pulse to the counter\n"); +#endif + for (ical=0; ical0 && i%2==0) { + printf("Shift in: module %d chip %i bit %d read %d instead of %d \n",k,j,i,val & 1<< j, i%2); + result++; + } + if (i%2>0 && (val & 1<0 && (dum & (1<0) { + printf("Shift out: module %d chip %i bit %d read %d instead of %d \n",k,j,i,val & 1<< j, (dum &1<0 && 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++; + } + if (i%2>0 && (val & 1<> 1; + + + putout("0000000000000000",ALLMOD); + putout("0010000000000000",ALLMOD); //change mux setting + putout("0000000000000000",ALLMOD); + } + + printf("Test FpgaMux module %d : %d errors\n", imod,result); + if (result) + return 1; + else + return 0; +} + + + + + + + + + +int calibration_sensor(int num, int *v, int *dacs) { + int ich, ichip, imod; + int val[10]; + + + printf("calibrating sensor..."); + for (imod=0; imod=0){ + + //clear rois + for(i=0;i=0) && (adc<=4)); + else { + printf("warning:adc value greater than 5. deleting roi\n"); + adc=-1; + } + } + } + + + //set rois for just 1 adc - take only 1st roi + if(adc!=-1){ + rois[0].xmin=adc*(NCHAN*NCHIPS_PER_ADC); + rois[0].xmax=(adc+1)*(NCHAN*NCHIPS_PER_ADC)-1; + rois[0].ymin=-1; + rois[0].ymax=-1; + nROI = 1; + }else + nROI = 0; + + if((arg[0].xmin!=rois[0].xmin)||(arg[0].xmax!=rois[0].xmax)||(arg[0].ymin!=rois[0].ymin)||(arg[0].ymax!=rois[0].ymax)) + *ret=FAIL; + if(n!=nROI) + *ret=FAIL; + + //set adc of interest + setADC(adc); + } + +//#ifdef VERBOSE + printf("Rois:\n"); + for( i=0;i + + +extern int sockfd; +extern int phase_shift; + + + +void error(char *msg) +{ + perror(msg); +} + +int main(int argc, char *argv[]) +{ + int portno, b; + char cmd[100]; + int retval=OK; + int sd, fd; + int iarg; + for(iarg=1; iarg + + +// Hardware definitions + +#define NCHAN 128 +#define NCHIP 10 +#define NMAXMODX 1 +#define NMAXMODY 1 +#define NMAXMOD NMAXMODX*NMAXMODY +#define NDAC 8 +#define NADC 5 + +#define NCHANS NCHAN*NCHIP*NMAXMOD +#define NDACS NDAC*NMAXMOD + +#define NTRIMBITS 6 +#define NCOUNTBITS 24 + +#define NCHIPS_PER_ADC 2 + +//#define TRIM_DR ((2**NTRIMBITS)-1) +//#define COUNT_DR ((2**NCOUNTBITS)-1) +#define TRIM_DR (((int)pow(2,NTRIMBITS))-1) +#define COUNT_DR (((int)pow(2,NCOUNTBITS))-1) + + +#define ALLMOD 0xffff +#define ALLFIFO 0xffff + + +#define ADCSYNC_VAL 0x32214 +#define TOKEN_RESTART_DELAY 0x88000000 +#define TOKEN_RESTART_DELAY_ROI 0x1b000000 +#define TOKEN_TIMING_REV1 0x1f16 +#define TOKEN_TIMING_REV2 0x1f0f + +#define DEFAULT_PHASE_SHIFT 120 +#define DEFAULT_IP_PACKETSIZE 0x0522 +#define DEFAULT_UDP_PACKETSIZE 0x050E +#define ADC1_IP_PACKETSIZE 256*2+14+20 +#define ADC1_UDP_PACKETSIZE 256*2+4+8+2 + +#ifdef VIRTUAL +#define DEBUGOUT +#endif + +#define CLK_FREQ 32.1E+6 + + +#endif diff --git a/slsDetectorSoftware/moenchDetectorServer/server_funcs.c b/slsDetectorSoftware/moenchDetectorServer/server_funcs.c new file mode 100755 index 000000000..bba9c94e3 --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/server_funcs.c @@ -0,0 +1,2993 @@ +#include "sls_detector_defs.h" +#include "server_funcs.h" +#ifndef PICASSOD +#include "server_defs.h" +#else +#include "picasso_defs.h" +#endif +#include "firmware_funcs.h" +#include "mcb_funcs.h" +#include "trimming_funcs.h" +#include "registers_g.h" +#include "svnInfoGotthard.h" + +#define FIFO_DATA_REG_OFF 0x50<<11 +#define CONTROL_REG 0x24<<11 +// Global variables + +int (*flist[256])(int); + +#ifdef MCB_FUNCS +extern const enum detectorType myDetectorType; +#endif +#ifndef MCB_FUNCS +const enum detectorType myDetectorType=GOTTHARD; +#endif +extern int nModX; +extern int nModY; +extern int dataBytes; +extern int dynamicRange; +extern int storeInRAM; + +extern int lockStatus; +extern char lastClientIP[INET_ADDRSTRLEN]; +extern char thisClientIP[INET_ADDRSTRLEN]; +extern int differentClients; + +/* global variables for optimized readout */ +extern unsigned int *ram_values; +char *dataretval=NULL; +int nframes, iframes, dataret; +char mess[1000]; + +int digitalTestBit = 0; + + + +int init_detector( int b) { +#ifndef PICASSOD + printf("This is a GOTTHARD detector with %d chips per module\n", NCHIP); +#else + printf("This is a PICASSO detector with %d chips per module\n", NCHIP); +#endif + + if (mapCSP0()==FAIL) { printf("Could not map memory\n"); + exit(1); + } + + //testFpga(); + if (b) { +#ifdef MCB_FUNCS + initDetector(); + printf("\n***initdetector done*** \n\n"); +#endif + testFpga(); + testRAM(); + //gotthard specific + setPhaseShiftOnce(); + + prepareADC(); + setADC(-1); //already does setdaqreg and clean fifo + printf("in chip of interes reg:%d\n",bus_r(CHIP_OF_INTRST_REG)); + int reg = (NCHAN*NCHIP)<255) + fnum=255; + retval=(*flist[fnum])(file_des); + if (retval==FAIL) + printf( "Error executing the function = %d \n",fnum); + return retval; +} + + +int function_table() { + int i; + for (i=0;i<256;i++){ + flist[i]=&M_nofunc; + } + flist[F_EXIT_SERVER]=&exit_server; + flist[F_EXEC_COMMAND]=&exec_command; + flist[F_GET_DETECTOR_TYPE]=&get_detector_type; + flist[F_SET_NUMBER_OF_MODULES]=&set_number_of_modules; + flist[F_GET_MAX_NUMBER_OF_MODULES]=&get_max_number_of_modules; + flist[F_SET_EXTERNAL_SIGNAL_FLAG]=&set_external_signal_flag; + flist[F_SET_EXTERNAL_COMMUNICATION_MODE]=&set_external_communication_mode; + flist[F_GET_ID]=&get_id; + flist[F_DIGITAL_TEST]=&digital_test; + flist[F_WRITE_REGISTER]=&write_register; + flist[F_READ_REGISTER]=&read_register; + flist[F_SET_DAC]=&set_dac; + flist[F_GET_ADC]=&get_adc; + flist[F_SET_CHANNEL]=&set_channel; + flist[F_SET_CHIP]=&set_chip; + flist[F_SET_MODULE]=&set_module; + flist[F_GET_CHANNEL]=&get_channel; + flist[F_GET_CHIP]=&get_chip; + flist[F_GET_MODULE]=&get_module; + flist[F_GET_THRESHOLD_ENERGY]=&get_threshold_energy; + flist[F_SET_THRESHOLD_ENERGY]=&set_threshold_energy; + flist[F_SET_SETTINGS]=&set_settings; + flist[F_START_ACQUISITION]=&start_acquisition; + flist[F_STOP_ACQUISITION]=&stop_acquisition; + flist[F_START_READOUT]=&start_readout; + flist[F_GET_RUN_STATUS]=&get_run_status; + flist[F_READ_FRAME]=&read_frame; + flist[F_READ_ALL]=&read_all; + flist[F_START_AND_READ_ALL]=&start_and_read_all; + flist[F_SET_TIMER]=&set_timer; + flist[F_GET_TIME_LEFT]=&get_time_left; + flist[F_SET_DYNAMIC_RANGE]=&set_dynamic_range; + flist[F_SET_ROI]=&set_roi; + flist[F_SET_SPEED]=&set_speed; + flist[F_SET_READOUT_FLAGS]=&set_readout_flags; + flist[F_EXECUTE_TRIMMING]=&execute_trimming; + flist[F_LOCK_SERVER]=&lock_server; + flist[F_SET_PORT]=&set_port; + flist[F_GET_LAST_CLIENT_IP]=&get_last_client_ip; + flist[F_UPDATE_CLIENT]=&update_client; + flist[F_CONFIGURE_MAC]=&configure_mac; + flist[F_LOAD_IMAGE]=&load_image; + flist[F_SET_MASTER]=&set_master; + flist[F_SET_SYNCHRONIZATION_MODE]=&set_synchronization; + flist[F_READ_COUNTER_BLOCK]=&read_counter_block; + flist[F_RESET_COUNTER_BLOCK]=&reset_counter_block; + flist[F_START_RECEIVER]=&start_receiver; + flist[F_STOP_RECEIVER]=&stop_receiver; + flist[F_CALIBRATE_PEDESTAL]=&calibrate_pedestal; + return OK; +} + + +int M_nofunc(int file_des){ + + int ret=FAIL; + sprintf(mess,"Unrecognized Function\n"); + printf(mess); + + sendDataOnly(file_des,&ret,sizeof(ret)); + sendDataOnly(file_des,mess,sizeof(mess)); + return GOODBYE; +} + + +int exit_server(int file_des) { + int retval=FAIL; + sendDataOnly(file_des,&retval,sizeof(retval)); + printf("closing server."); + sprintf(mess,"closing server"); + sendDataOnly(file_des,mess,sizeof(mess)); + return GOODBYE; +} + +int exec_command(int file_des) { + char cmd[MAX_STR_LENGTH]; + char answer[MAX_STR_LENGTH]; + int retval=OK; + int sysret=0; + int n=0; + + /* receive arguments */ + n = receiveDataOnly(file_des,cmd,MAX_STR_LENGTH); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + retval=FAIL; + } + + /* execute action if the arguments correctly arrived*/ + if (retval==OK) { +#ifdef VERBOSE + printf("executing command %s\n", cmd); +#endif + if (lockStatus==0 || differentClients==0) + sysret=system(cmd); + + //should be replaced by popen + if (sysret==0) { + sprintf(answer,"Succeeded\n"); + if (lockStatus==1 && differentClients==1) + sprintf(answer,"Detector locked by %s\n", lastClientIP); + } else { + sprintf(answer,"Failed\n"); + retval=FAIL; + } + } else { + sprintf(answer,"Could not receive the command\n"); + } + + /* send answer */ + n = sendDataOnly(file_des,&retval,sizeof(retval)); + n = sendDataOnly(file_des,answer,MAX_STR_LENGTH); + if (n < 0) { + sprintf(mess,"Error writing to socket"); + retval=FAIL; + } + + + /*return ok/fail*/ + return retval; + +} + + + +int get_detector_type(int file_des) { + int n=0; + enum detectorType ret; + int retval=OK; + + sprintf(mess,"Can't return detector type\n"); + + + /* receive arguments */ + /* execute action */ + ret=myDetectorType; + +#ifdef VERBOSE + printf("Returning detector type %d\n",ret); +#endif + + /* send answer */ + /* send OK/failed */ + if (differentClients==1) + retval=FORCE_UPDATE; + + n += sendDataOnly(file_des,&retval,sizeof(retval)); + if (retval!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,&ret,sizeof(ret)); + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + /*return ok/fail*/ + return retval; + + +} + + +int set_number_of_modules(int file_des) { + int n; + int arg[2], ret=0; + int retval=OK; + int dim, nm; + + sprintf(mess,"Can't set number of modules\n"); + + /* receive arguments */ + n = receiveDataOnly(file_des,&arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket %d", n); + retval=GOODBYE; + } + if (retval==OK) { + dim=arg[0]; + nm=arg[1]; + + /* execute action */ +#ifdef VERBOSE + printf("Setting the number of modules in dimension %d to %d\n",dim,nm ); +#endif + + //if (nm!=GET_FLAG) { + if (dim!=X && nm!=GET_FLAG) { + retval=FAIL; + sprintf(mess,"Can't change module number in dimension %d\n",dim); + } else { + if (lockStatus==1 && differentClients==1 && nm!=GET_FLAG) { + sprintf(mess,"Detector locked by %s\n", lastClientIP); + retval=FAIL; + } else { + ret=setNMod(nm); + if (nModX==nm || nm==GET_FLAG) { + retval=OK; + if (differentClients==1) + retval=FORCE_UPDATE; + } else + retval=FAIL; + } + } + } + /*} else { + if (dim==Y) { + ret=nModY; + } else if (dim==X) { + ret=setNMod(-1); + } + } + */ + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&retval,sizeof(retval)); + if (retval!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,&ret,sizeof(ret)); + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + /*return ok/fail*/ + return retval; + +} + + +int get_max_number_of_modules(int file_des) { + int n; + int ret; + int retval=OK; + enum dimension arg; + + sprintf(mess,"Can't get max number of modules\n"); + /* receive arguments */ + n = receiveDataOnly(file_des,&arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + retval=FAIL; + } + /* execute action */ +#ifdef VERBOSE + printf("Getting the max number of modules in dimension %d \n",arg); +#endif + + + switch (arg) { + case X: + ret=getNModBoard(); + break; + case Y: + ret=NMAXMODY; + break; + default: + ret=FAIL; + retval=FAIL; + break; + } +#ifdef VERBOSE + printf("Max number of module in dimension %d is %d\n",arg,ret ); +#endif + + + + if (differentClients==1 && retval==OK) { + retval=FORCE_UPDATE; + } + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&retval,sizeof(retval)); + if (retval!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,&ret,sizeof(ret)); + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + + + + /*return ok/fail*/ + return retval; +} + + +//index 0 is in gate +//index 1 is in trigger +//index 2 is out gate +//index 3 is out trigger + +int set_external_signal_flag(int file_des) { + int n; + int arg[2]; + int ret=OK; + int signalindex; + enum externalSignalFlag flag, retval; + + sprintf(mess,"Can't set external signal flag\n"); + + /* receive arguments */ + n = receiveDataOnly(file_des,&arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + retval=SIGNAL_OFF; + if (ret==OK) { + signalindex=arg[0]; + flag=arg[1]; + /* execute action */ + switch (flag) { + case GET_EXTERNAL_SIGNAL_FLAG: + retval=getExtSignal(signalindex); + break; + + default: + if (differentClients==0 || lockStatus==0) { + retval=setExtSignal(signalindex,flag); + } else { + if (lockStatus!=0) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n", lastClientIP); + } + } + + } + +#ifdef VERBOSE + printf("Setting external signal %d to flag %d\n",signalindex,flag ); + printf("Set to flag %d\n",retval); +#endif + + } else { + ret=FAIL; + } + + if (ret==OK && differentClients!=0) + ret=FORCE_UPDATE; + + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,&retval,sizeof(retval)); + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + + + /*return ok/fail*/ + return ret; + +} + + +int set_external_communication_mode(int file_des) { + int n; + enum externalCommunicationMode arg, ret=GET_EXTERNAL_COMMUNICATION_MODE; + int retval=OK; + + sprintf(mess,"Can't set external communication mode\n"); + + + /* receive arguments */ + n = receiveDataOnly(file_des,&arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + retval=FAIL; + } + /* + enum externalCommunicationMode{ + GET_EXTERNAL_COMMUNICATION_MODE, + AUTO, + TRIGGER_EXPOSURE_SERIES, + TRIGGER_EXPOSURE_BURST, + TRIGGER_READOUT, + TRIGGER_COINCIDENCE_WITH_INTERNAL_ENABLE, + GATE_FIX_NUMBER, + GATE_FIX_DURATION, + GATE_WITH_START_TRIGGER, + GATE_COINCIDENCE_WITH_INTERNAL_ENABLE + }; + */ + if (retval==OK) { + /* execute action */ + + ret=setTiming(arg); + + /* switch(arg) { */ + /* default: */ + /* sprintf(mess,"The meaning of single signals should be set\n"); */ + /* retval=FAIL; */ + /* } */ + + +#ifdef VERBOSE + printf("Setting external communication mode to %d\n", arg); +#endif + } else + ret=FAIL; + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&retval,sizeof(retval)); + if (retval!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,&ret,sizeof(ret)); + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + + /*return ok/fail*/ + return retval; +} + + + +int get_id(int file_des) { + // sends back 64 bits! + int64_t retval=-1; + int ret=OK; + int n=0; + enum idMode arg; + + sprintf(mess,"Can't return id\n"); + + /* receive arguments */ + n = receiveDataOnly(file_des,&arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + +#ifdef VERBOSE + printf("Getting id %d\n", arg); +#endif + + switch (arg) { + case DETECTOR_SERIAL_NUMBER: + retval=getDetectorNumber(); + break; + case DETECTOR_FIRMWARE_VERSION: + retval=getFirmwareSVNVersion(); + retval=(retval <<32) | getFirmwareVersion(); + break; + case DETECTOR_SOFTWARE_VERSION: + retval= SVNREV; + retval= (retval <<32) | SVNDATE; + break; +/* case DETECTOR_FIRMWARE_SVN_VERSION: + retval=getFirmwareSVNVersion(); + break;*/ + default: + printf("Required unknown id %d \n", arg); + ret=FAIL; + retval=FAIL; + break; + } + +#ifdef VERBOSE + printf("Id is %llx\n", retval); +#endif + + if (differentClients==1) + ret=FORCE_UPDATE; + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,&retval,sizeof(retval)); + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + + /*return ok/fail*/ + return ret; + +} + +int digital_test(int file_des) { + + int retval; + int ret=OK; + int imod=-1; + int n=0; + int ibit=0; + int ow; + int ival; + enum digitalTestMode arg; + + sprintf(mess,"Can't send digital test\n"); + + n = receiveDataOnly(file_des,&arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + +#ifdef VERBOSE + printf("Digital test mode %d\n",arg ); +#endif + + switch (arg) { + case CHIP_TEST: + n = receiveDataOnly(file_des,&imod,sizeof(imod)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + retval=FAIL; + } +#ifdef VERBOSE + printf("of module %d\n", imod); +#endif + retval=0; +#ifdef MCB_FUNCS + if (differentClients==1 && lockStatus==1) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + break; + } + if (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()) + ret=FAIL; + if (imod<0) + imod=ALLMOD; + +#ifdef MCB_FUNCS + switch (ind) { + case G_VREF_DS : + idac=VREF_DS; + break; + case G_VCASCN_PB: + idac=VCASCN_PB; + break; + case G_VCASCP_PB: + idac=VCASCP_PB; + break; + case G_VOUT_CM: + idac=VOUT_CM; + break; + case G_VCASC_OUT: + idac=VCASC_OUT; + break; + case G_VIN_CM: + idac=VIN_CM; + break; + case G_VREF_COMP: + idac=VREF_COMP; + break; + case G_IB_TESTC: + idac=IB_TESTC; + break; + case HV_POT: + idac=HIGH_VOLTAGE; + break; + + default: + printf("Unknown DAC index %d\n",ind); + sprintf(mess,"Unknown DAC index %d\n",ind); + ret=FAIL; + break; + } + + if (ret==OK) { + if (differentClients==1 && lockStatus==1) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + } else{ + if(idac==HIGH_VOLTAGE) + retval=initHighVoltageByModule(val,imod); + else + retval=initDACbyIndexDACU(idac,val,imod); + } + } + 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; + } +#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{ + if (differentClients) + ret=FORCE_UPDATE; + } + + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,&retval,sizeof(retval)); + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + + + /*return ok/fail*/ + return ret; + +} + + + +int get_adc(int file_des) { + //default: mod 0 + int retval; + int ret=OK; + int arg[2]; + enum dacIndex ind; + int imod; + int n; + int idac=0; + + sprintf(mess,"Can't read ADC\n"); + + + n = receiveDataOnly(file_des,arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + ind=arg[0]; + imod=arg[1]; + +#ifdef VERBOSE + printf("Getting ADC %d of module %d\n", ind, imod); +#endif + + if (imod>=getNModBoard() || imod<0) + ret=FAIL; + +#ifdef MCB_FUNCS + switch (ind) { + case TEMPERATURE_FPGA: + idac=TEMP_FPGA; + break; + case TEMPERATURE_ADC: + idac=TEMP_ADC; + break; + default: + printf("Unknown DAC index %d\n",ind); + sprintf(mess,"Unknown DAC index %d\n",ind); + ret=FAIL; + break; + } + + if (ret==OK) + retval=getTemperatureByModule(idac,imod); +#endif + +#ifdef VERBOSE + printf("ADC is %d V\n", retval); +#endif + if (ret==FAIL) { + printf("Getting adc %d of module %d failed\n", ind, imod); + } + + if (differentClients) + ret=FORCE_UPDATE; + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,&retval,sizeof(retval)); + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + + /*return ok/fail*/ + return ret; + +} + +int set_channel(int file_des) { + int ret=OK; + sls_detector_channel myChan; + int retval; + int n; + + + sprintf(mess,"Can't set channel\n"); + +#ifdef VERBOSE + printf("Setting channel\n"); +#endif + ret=receiveChannel(file_des, &myChan); + if (ret>=0) + ret=OK; + else + ret=FAIL; +#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) + ret=FAIL; + if (myChan.chan>=NCHAN) + ret=FAIL; + if (myChan.module<0) + myChan.module=ALLMOD; + } + + + if (ret==OK) { + if (differentClients==1 && lockStatus==1) { + 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 */ + //copyChannel(detectorChans[myChan.module][myChan.chip]+(myChan.chan), &myChan); + + + + if (differentClients==1 && ret==OK) + ret=FORCE_UPDATE; + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,&retval,sizeof(retval)); + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + + + /*return ok/fail*/ + return ret; + +} + + + + +int get_channel(int file_des) { + + int ret=OK; + sls_detector_channel retval; + + int arg[3]; + int ichan, ichip, imod; + int n; + + sprintf(mess,"Can't get channel\n"); + + + + n = receiveDataOnly(file_des,arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + ichan=arg[0]; + ichip=arg[1]; + imod=arg[2]; + + if (ret==OK) { + ret=FAIL; + if (imod>=0 && imod=0 && ichip=0 && ichan=0) + ret=OK; + else + ret=FAIL; +#ifdef VERBOSE + printf("chip number is %d, module number is %d, register is %d, nchan %d\n",myChip.chip, myChip.module, myChip.reg, myChip.nchan); +#endif + + if (ret==OK) { + if (myChip.module>=getNModBoard()) + ret=FAIL; + if (myChip.module<0) + myChip.module=ALLMOD; + if (myChip.chip>=NCHIP) + ret=FAIL; + } + if (differentClients==1 && lockStatus==1) { + 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); + + if (differentClients && ret==OK) + ret=FORCE_UPDATE; + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,&retval,sizeof(retval)); + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + + + return ret; +} + +int get_chip(int file_des) { + + + int ret=OK; + sls_detector_chip retval; + int arg[2]; + int ichip, imod; + int n; + + + + n = receiveDataOnly(file_des,arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + ichip=arg[0]; + imod=arg[1]; + if (ret==OK) { + ret=FAIL; + if (imod>=0 && imod=0 && ichip=0) + ret=OK; + else + ret=FAIL; + + +#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); +#endif + + if (ret==OK) { + if (myModule.module>=getNModBoard()) { + ret=FAIL; + printf("Module number is too large %d\n",myModule.module); + } + if (myModule.module<0) + myModule.module=ALLMOD; + } + + if (ret==OK) { + if (differentClients==1 && lockStatus==1) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + } else { +#ifdef MCB_FUNCS + retval=initModulebyNumber(myModule); +#endif + } + } + + if (differentClients==1 && ret==OK) + ret=FORCE_UPDATE; + + /* Maybe this is done inside the initialization funcs */ + //copyChip(detectorChips[myChip.module]+(myChip.chip), &myChip); + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,&retval,sizeof(retval)); + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + free(myChip); + free(myChan); + free(myDac); + free(myAdc); + + // setDynamicRange(dr); always 16 commented out + + + return ret; +} + + + + +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*/ + + + if (myDac) + myModule.dacs=myDac; + else { + 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.ndac=NDAC; + myModule.nchip=NCHIP; + myModule.nchan=NCHAN*NCHIP; + myModule.nadc=NADC; + + + + + + n = receiveDataOnly(file_des,&arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + imod=arg; + + if (ret==OK) { + ret=FAIL; + if (imod>=0 && imod-2) { + dataret=FAIL; + sprintf(mess,"no data and run stopped: %d frames left\n",(int)(getFrames()+2)); + printf("%s\n",mess); + } else { + dataret=FINISHED; + sprintf(mess,"acquisition successfully finished\n"); + printf("%s\n",mess); + } +#ifdef VERYVERBOSE + printf("%d %d %x %s\n",(int)(sizeof(mess)),(int)(strlen(mess)),(unsigned int)( mess),mess); +#endif + sendDataOnly(file_des,&dataret,sizeof(dataret)); + sendDataOnly(file_des,mess,sizeof(mess)); +#ifdef VERYVERBOSE + printf("message sent %s\n",mess); +#endif + printf("dataret %d\n",dataret); + return dataret; + } + } else { + nframes=0; + while(fifo_read_event()) { + nframes++; + } + dataretval=(char*)ram_values; + dataret=OK; +#ifdef VERBOSE + printf("sending data of %d frames\n",nframes); +#endif + for (iframes=0; iframes-2) { + dataret=FAIL; + sprintf(mess,"no data and run stopped: %d frames left\n",(int)(getFrames()+2)); + printf("%s\n",mess); + } else { + dataret=FINISHED; + sprintf(mess,"acquisition successfully finished\n"); + printf("%s\n",mess); + if (differentClients) + dataret=FORCE_UPDATE; + } +#ifdef VERBOSE + printf("Frames left %d\n",(int)(getFrames())); +#endif + sendDataOnly(file_des,&dataret,sizeof(dataret)); + sendDataOnly(file_des,mess,sizeof(mess)); + printf("dataret %d\n",dataret); + return dataret; + } + printf("dataret %d\n",dataret); + return dataret; +} + + + + + + + + +int read_all(int file_des) { + +while(read_frame(file_des)==OK) { + +#ifdef VERBOSE + printf("frame read\n"); +#endif + ; + } +#ifdef VERBOSE + printf("Frames finished\n"); +#endif + return OK; + + +} + +int start_and_read_all(int file_des) { + //int dataret=OK; +#ifdef VERBOSE + printf("Starting and reading all frames\n"); +#endif + + if (differentClients==1 && lockStatus==1) { + dataret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + sendDataOnly(file_des,&dataret,sizeof(dataret)); + sendDataOnly(file_des,mess,sizeof(mess)); + return dataret; + + } + + startStateMachine(); + + /* ret=startStateMachine(); + if (ret!=OK) { + sprintf(mess,"could not start state machine\n"); + sendDataOnly(file_des,&ret,sizeof(ret)); + sendDataOnly(file_des,mess,sizeof(mess)); + + #ifdef VERBOSE + printf("could not start state machine\n"); +#endif +} else {*/ + read_all(file_des); +#ifdef VERBOSE + printf("Frames finished\n"); +#endif + //} + + + return OK; + + +} + +int set_timer(int file_des) { + enum timerIndex ind; + int64_t tns; + int n; + int64_t retval; + int ret=OK; + + + sprintf(mess,"can't set timer\n"); + + n = receiveDataOnly(file_des,&ind,sizeof(ind)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + n = receiveDataOnly(file_des,&tns,sizeof(tns)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + if (ret!=OK) { + printf(mess); + } + +#ifdef VERBOSE + printf("setting timer %d to %lld ns\n",ind,tns); +#endif + if (ret==OK) { + + if (differentClients==1 && lockStatus==1 && tns!=-1) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + } else { + switch(ind) { + case FRAME_NUMBER: + retval=setFrames(tns); + break; + case ACQUISITION_TIME: + retval=setExposureTime(tns); + break; + case FRAME_PERIOD: + retval=setPeriod(tns); + break; + case DELAY_AFTER_TRIGGER: + retval=setDelay(tns); + break; + case GATES_NUMBER: + retval=setGates(tns); + break; + case PROBES_NUMBER: + sprintf(mess,"can't set timer for gotthard\n"); + ret=FAIL; + break; + case CYCLES_NUMBER: + retval=setTrains(tns); + break; + default: + ret=FAIL; + sprintf(mess,"timer index unknown %d\n",ind); + break; + } + } + } + if (ret!=OK) { + printf(mess); + if (differentClients) + ret=FORCE_UPDATE; + } + + if (ret!=OK) { + printf(mess); + printf("set timer failed\n"); + } else if (ind==FRAME_NUMBER) { + ret=allocateRAM(); + if (ret!=OK) + sprintf(mess, "could not allocate RAM for %lld frames\n", tns); + } + + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) { + n = sendDataOnly(file_des,mess,sizeof(mess)); + } else { +#ifdef VERBOSE + printf("returning ok %d\n",(int)(sizeof(retval))); +#endif + + n = sendDataOnly(file_des,&retval,sizeof(retval)); + } + + return ret; + +} + + + + + + + + +int get_time_left(int file_des) { + + enum timerIndex ind; + int n; + int64_t retval; + int ret=OK; + + sprintf(mess,"can't get timer\n"); + n = receiveDataOnly(file_des,&ind,sizeof(ind)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + +#ifdef VERBOSE + + printf("getting time left on timer %d \n",ind); +#endif + + if (ret==OK) { + switch(ind) { + case FRAME_NUMBER: + retval=getFrames(); + break; + case ACQUISITION_TIME: + retval=getExposureTime(); + break; + case FRAME_PERIOD: + retval=getPeriod(); + break; + case DELAY_AFTER_TRIGGER: + retval=getDelay(); + break; + case GATES_NUMBER: + retval=getGates(); + break; + case PROBES_NUMBER: + retval=getProbes(); + break; + case CYCLES_NUMBER: + retval=getTrains(); + break; + case PROGRESS: + retval=getProgress(); + break; + case ACTUAL_TIME: + retval=getActualTime(); + break; + case MEASUREMENT_TIME: + retval=getMeasurementTime(); + break; + default: + ret=FAIL; + sprintf(mess,"timer index unknown %d\n",ind); + break; + } + } + + + if (ret!=OK) { + printf("get time left failed\n"); + } else if (differentClients) + ret=FORCE_UPDATE; + +#ifdef VERBOSE + + printf("time left on timer %d is %lld\n",ind, retval); +#endif + + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret!=OK) { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } else { + n = sendDataOnly(file_des,&retval,sizeof(retval)); + } +#ifdef VERBOSE + + printf("data sent\n"); +#endif + + return ret; + + +} + +int set_dynamic_range(int file_des) { + + + + int dr; + int n; + int retval; + int ret=OK; + + + sprintf(mess,"can't set dynamic range\n"); + + + n = receiveDataOnly(file_des,&dr,sizeof(dr)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + + if (differentClients==1 && lockStatus==1 && dr>=0) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + } else { + retval=setDynamicRange(dr); + } + + //if (dr>=0 && retval!=dr) ret=FAIL; + if (ret!=OK) { + sprintf(mess,"set dynamic range failed\n"); + } else { + ret=allocateRAM(); + if (ret!=OK) + sprintf(mess,"Could not allocate RAM for the dynamic range selected\n"); + else if (differentClients) + ret=FORCE_UPDATE; + } + + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) { + n = sendDataOnly(file_des,mess,sizeof(mess)); + } else { + n = sendDataOnly(file_des,&retval,sizeof(retval)); + } + return ret; +} + +int set_roi(int file_des) { + + int i; + int ret=OK; + int nroi=-1; + int n=0; + int retvalsize=0; + ROI arg[MAX_ROIS]; + ROI* retval=0; + + strcpy(mess,"Could not set/get roi\n"); + + + n = receiveDataOnly(file_des,&nroi,sizeof(nroi)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + if(nroi!=-1){ + 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; + } +//#ifdef VERBOSE + printf("Setting ROI to:"); + for( i=0;i=0) { + if (lockStatus==0 || strcmp(lastClientIP,thisClientIP)==0 || strcmp(lastClientIP,"none")==0) + lockStatus=lock; + else { + ret=FAIL; + sprintf(mess,"Server already locked by %s\n", lastClientIP); + } + } + if (differentClients && ret==OK) + ret=FORCE_UPDATE; + + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) { + n = sendDataOnly(file_des,mess,sizeof(mess)); + } else + n = sendDataOnly(file_des,&lockStatus,sizeof(lockStatus)); + + return ret; + +} + +int set_port(int file_des) { + int n; + int ret=OK; + int sd=-1; + + enum portType p_type; /** data? control? stop? Unused! */ + int p_number; /** new port number */ + + n = receiveDataOnly(file_des,&p_type,sizeof(p_type)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + printf("Error reading from socket (ptype)\n"); + ret=FAIL; + } + + n = receiveDataOnly(file_des,&p_number,sizeof(p_number)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + printf("Error reading from socket (pnum)\n"); + ret=FAIL; + } + if (differentClients==1 && lockStatus==1 ) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + } else { + if (p_number<1024) { + sprintf(mess,"Too low port number %d\n", p_number); + printf("\n"); + ret=FAIL; + } + + printf("set port %d to %d\n",p_type, p_number); + + sd=bindSocket(p_number); + } + if (sd>=0) { + ret=OK; + if (differentClients ) + ret=FORCE_UPDATE; + } else { + ret=FAIL; + sprintf(mess,"Could not bind port %d\n", p_number); + printf("Could not bind port %d\n", p_number); + if (sd==-10) { + sprintf(mess,"Port %d already set\n", p_number); + printf("Port %d already set\n", p_number); + + } + } + + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) { + n = sendDataOnly(file_des,mess,sizeof(mess)); + } else { + n = sendDataOnly(file_des,&p_number,sizeof(p_number)); + closeConnection(file_des); + exitServer(sockfd); + sockfd=sd; + + } + + return ret; + +} + +int get_last_client_ip(int file_des) { + int ret=OK; + int n; + if (differentClients ) + ret=FORCE_UPDATE; + n = sendDataOnly(file_des,&ret,sizeof(ret)); + n = sendDataOnly(file_des,lastClientIP,sizeof(lastClientIP)); + + return ret; + +} + + +int send_update(int file_des) { + + int ret=OK; + enum detectorSettings t; + int n;//int thr, n; + //int it; + int64_t retval, tns=-1; + n = sendDataOnly(file_des,lastClientIP,sizeof(lastClientIP)); + n = sendDataOnly(file_des,&nModX,sizeof(nModX)); + 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); + n = sendDataOnly(file_des,&t,sizeof(t)); +/* thr=getThresholdEnergy(); + n = sendDataOnly(file_des,&thr,sizeof(thr));*/ + retval=setFrames(tns); + n = sendDataOnly(file_des,&retval,sizeof(int64_t)); + retval=setExposureTime(tns); + n = sendDataOnly(file_des,&retval,sizeof(int64_t)); + retval=setPeriod(tns); + n = sendDataOnly(file_des,&retval,sizeof(int64_t)); + retval=setDelay(tns); + n = sendDataOnly(file_des,&retval,sizeof(int64_t)); + retval=setGates(tns); + n = sendDataOnly(file_des,&retval,sizeof(int64_t)); +/* retval=setProbes(tns); + n = sendDataOnly(file_des,&retval,sizeof(int64_t));*/ + retval=setTrains(tns); + n = sendDataOnly(file_des,&retval,sizeof(int64_t)); + + if (lockStatus==0) { + strcpy(lastClientIP,thisClientIP); + } + + return ret; + + +} +int update_client(int file_des) { + + int ret=OK; + + sendDataOnly(file_des,&ret,sizeof(ret)); + return send_update(file_des); + + + +} + + +int configure_mac(int file_des) { + + int ret=OK; + char arg[5][50]; + int n; + + int imod=0;//should be in future sent from client as -1, arg[2] + int ipad; + long long int imacadd; + long long int idetectormacadd; + int udpport; + int detipad; + int retval=-100; + + sprintf(mess,"Can't configure MAC\n"); + + + n = receiveDataOnly(file_des,arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + sscanf(arg[0], "%x", &ipad); + sscanf(arg[1], "%llx", &imacadd); + sscanf(arg[2], "%x", &udpport); + sscanf(arg[3], "%llx", &idetectormacadd); + sscanf(arg[4], "%x", &detipad); + +#ifdef VERBOSE + int i; + printf("\ndigital_test_bit in server %d\t",digitalTestBit); + printf("\nipadd %x\t",ipad); + printf("destination ip is %d.%d.%d.%d = 0x%x \n",(ipad>>24)&0xff,(ipad>>16)&0xff,(ipad>>8)&0xff,(ipad)&0xff,ipad); + printf("macad:%llx\n",imacadd); + for (i=0;i<6;i++) + printf("mac adress %d is 0x%x \n",6-i,(unsigned int)(((imacadd>>(8*i))&0xFF))); + printf("udp port:0x%x\n",udpport); + printf("detector macad:%llx\n",idetectormacadd); + for (i=0;i<6;i++) + printf("detector mac adress %d is 0x%x \n",6-i,(unsigned int)(((idetectormacadd>>(8*i))&0xFF))); + printf("detipad %x\n",detipad); + printf("\n"); +#endif + + + + if (imod>=getNModBoard()) + ret=FAIL; + if (imod<0) + imod=ALLMOD; + + //#ifdef VERBOSE + printf("Configuring MAC of module %d at port %x\n", imod, udpport); + //#endif +#ifdef MCB_FUNCS + if (ret==OK){ + if(runBusy()){ + ret=stopStateMachine(); + if(ret==FAIL) + strcpy(mess,"could not stop detector acquisition to configure mac"); + } + + if(ret==OK) + configureMAC(ipad,imacadd,idetectormacadd,detipad,digitalTestBit,udpport); + retval=getAdcConfigured(); + } +#endif + if (ret==FAIL) + printf("configuring MAC of mod %d failed\n", imod); + else + printf("Configuremac successful of mod %d and adc %d\n",imod,retval); + + if (differentClients) + ret=FORCE_UPDATE; + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) + n += sendDataOnly(file_des,mess,sizeof(mess)); + else + n += sendDataOnly(file_des,&retval,sizeof(retval)); + /*return ok/fail*/ + return ret; + +} + + + +int load_image(int file_des) { + int retval; + int ret=OK; + int n; + enum imageType index; + short int ImageVals[NCHAN*NCHIP]; + + sprintf(mess,"Loading image failed\n"); + + n = receiveDataOnly(file_des,&index,sizeof(index)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + n = receiveDataOnly(file_des,ImageVals,dataBytes); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + switch (index) { + case DARK_IMAGE : +#ifdef VERBOSE + printf("Loading Dark image\n"); +#endif + break; + case GAIN_IMAGE : +#ifdef VERBOSE + printf("Loading Gain image\n"); +#endif + break; + default: + printf("Unknown index %d\n",index); + sprintf(mess,"Unknown index %d\n",index); + ret=FAIL; + break; + } + + if (ret==OK) { + if (differentClients==1 && lockStatus==1) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + } else{ + retval=loadImage(index,ImageVals); + if (retval==-1) + ret = FAIL; + } + } + + if(ret==OK){ + if (differentClients) + ret=FORCE_UPDATE; + } + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,&retval,sizeof(retval)); + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + + /*return ok/fail*/ + return ret; +} + + + +int set_master(int file_des) { + + enum masterFlags retval=GET_MASTER; + enum masterFlags arg; + int n; + int ret=OK; + // int regret=OK; + + + sprintf(mess,"can't set master flags\n"); + + + n = receiveDataOnly(file_des,&arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + +#ifdef VERBOSE + printf("setting master flags to %d\n",arg); +#endif + + if (differentClients==1 && lockStatus==1 && arg!=GET_READOUT_FLAGS) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + } else { + retval=setMaster(arg); + + } + if (retval==GET_MASTER) { + ret=FAIL; + } + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) { + n = sendDataOnly(file_des,mess,sizeof(mess)); + } else { + n = sendDataOnly(file_des,&retval,sizeof(retval)); + } + return ret; +} + + + + + + +int set_synchronization(int file_des) { + + enum synchronizationMode retval=GET_MASTER; + enum synchronizationMode arg; + int n; + int ret=OK; + //int regret=OK; + + + sprintf(mess,"can't set synchronization mode\n"); + + + n = receiveDataOnly(file_des,&arg,sizeof(arg)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } +#ifdef VERBOSE + printf("setting master flags to %d\n",arg); +#endif + + if (differentClients==1 && lockStatus==1 && arg!=GET_READOUT_FLAGS) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + } else { + //ret=setStoreInRAM(0); + // initChipWithProbes(0,0,0, ALLMOD); + retval=setSynchronization(arg); + } + if (retval==GET_SYNCHRONIZATION_MODE) { + ret=FAIL; + } + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) { + n = sendDataOnly(file_des,mess,sizeof(mess)); + } else { + n = sendDataOnly(file_des,&retval,sizeof(retval)); + } + return ret; +} + + + + + + +int read_counter_block(int file_des) { + + int ret=OK; + int n; + int startACQ; + //char *retval=NULL; + short int CounterVals[NCHAN*NCHIP]; + + sprintf(mess,"Read counter block failed\n"); + + n = receiveDataOnly(file_des,&startACQ,sizeof(startACQ)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + if (ret==OK) { + if (differentClients==1 && lockStatus==1) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + } else{ + ret=readCounterBlock(startACQ,CounterVals); +#ifdef VERBOSE + int i; + for(i=0;i<6;i++) + printf("%d:%d\t",i,CounterVals[i]); +#endif + } + } + + if(ret!=FAIL){ + if (differentClients) + ret=FORCE_UPDATE; + } + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret!=FAIL) { + /* send return argument */ + n += sendDataOnly(file_des,CounterVals,dataBytes);//1280*2 + } else { + n += sendDataOnly(file_des,mess,sizeof(mess)); + } + + /*return ok/fail*/ + return ret; +} + + + + + +int reset_counter_block(int file_des) { + + int ret=OK; + int n; + int startACQ; + + sprintf(mess,"Reset counter block failed\n"); + + n = receiveDataOnly(file_des,&startACQ,sizeof(startACQ)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + if (ret==OK) { + if (differentClients==1 && lockStatus==1) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + } else + ret=resetCounterBlock(startACQ); + } + + if(ret==OK){ + if (differentClients) + ret=FORCE_UPDATE; + } + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) + n += sendDataOnly(file_des,mess,sizeof(mess)); + + /*return ok/fail*/ + return ret; +} + + + + + + +int start_receiver(int file_des) { + int ret=OK; + int n=0; + strcpy(mess,"Could not start receiver\n"); + + /* execute action if the arguments correctly arrived*/ +#ifdef MCB_FUNCS + if (lockStatus==1 && differentClients==1){//necessary??? + sprintf(mess,"Detector locked by %s\n", lastClientIP); + ret=FAIL; + } + else + ret = startReceiver(1); + +#endif + + + if(ret==OK && differentClients){ + printf("Force update\n"); + ret=FORCE_UPDATE; + } + + /* send answer */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if(ret==FAIL) + n = sendDataOnly(file_des,mess,sizeof(mess)); + /*return ok/fail*/ + return ret; +} + + + + + + +int stop_receiver(int file_des) { + int ret=OK; + int n=0; + + strcpy(mess,"Could not stop receiver\n"); + + /* execute action if the arguments correctly arrived*/ +#ifdef MCB_FUNCS + if (lockStatus==1 && differentClients==1){//necessary??? + sprintf(mess,"Detector locked by %s\n", lastClientIP); + ret=FAIL; + } + else + ret=startReceiver(0); + +#endif + + + if(ret==OK && differentClients){ + printf("Force update\n"); + ret=FORCE_UPDATE; + } + + /* send answer */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if(ret==FAIL) + n = sendDataOnly(file_des,mess,sizeof(mess)); + /*return ok/fail*/ + return ret; +} + + + + + +int calibrate_pedestal(int file_des){ + + int ret=OK; + int retval=-1; + int n; + int frames; + + sprintf(mess,"Could not calibrate pedestal\n"); + + n = receiveDataOnly(file_des,&frames,sizeof(frames)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + ret=FAIL; + } + + if (ret==OK) { + if (differentClients==1 && lockStatus==1) { + ret=FAIL; + sprintf(mess,"Detector locked by %s\n",lastClientIP); + } else + ret=calibratePedestal(frames); + } + + if(ret==OK){ + if (differentClients) + ret=FORCE_UPDATE; + } + + /* send answer */ + /* send OK/failed */ + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret==FAIL) + n += sendDataOnly(file_des,mess,sizeof(mess)); + else + n += sendDataOnly(file_des,&retval,sizeof(retval)); + + /*return ok/fail*/ + return ret; +} + + diff --git a/slsDetectorSoftware/moenchDetectorServer/server_funcs.h b/slsDetectorSoftware/moenchDetectorServer/server_funcs.h new file mode 100755 index 000000000..7dc8da6ea --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/server_funcs.h @@ -0,0 +1,98 @@ +#ifndef SERVER_FUNCS_H +#define SERVER_FUNCS_H + + +#include "sls_detector_defs.h" + + +#include +/* +#include +#include +#include +*/ +#include "communication_funcs.h" + + + + +#define GOODBYE -200 + +int sockfd; + +int function_table(); + +int decode_function(int); + +int init_detector(int); + +int M_nofunc(int); +int exit_server(int); + + + + +// General purpose functions +int get_detector_type(int); +int set_number_of_modules(int); +int get_max_number_of_modules(int); + + +int exec_command(int); +int set_external_signal_flag(int); +int set_external_communication_mode(int); +int get_id(int); +int digital_test(int); +int write_register(int); +int read_register(int); +int set_dac(int); +int get_adc(int); +int set_channel(int); +int set_chip(int); +int set_module(int); +int get_channel(int); +int get_chip(int); +int get_module(int); + +int get_threshold_energy(int); +int set_threshold_energy(int); +int set_settings(int); +int start_acquisition(int); +int stop_acquisition(int); +int start_readout(int); +int get_run_status(int); +int read_frame(int); +int read_all(int); +int start_and_read_all(int); +int set_timer(int); +int get_time_left(int); +int set_dynamic_range(int); +int set_roi(int); +int get_roi(int); +int set_speed(int); +void prepareADC(void); +int set_readout_flags(int); +int execute_trimming(int); +int lock_server(int); +int set_port(int); +int get_last_client_ip(int); +int set_master(int); +int set_synchronization(int); + +int update_client(int); +int send_update(int); +int configure_mac(int); + +int load_image(int); +int read_counter_block(int); +int reset_counter_block(int); + +int start_receiver(int); +int stop_receiver(int); + + +int calibrate_pedestal(int); + +int set_roi(int); + +#endif diff --git a/slsDetectorSoftware/moenchDetectorServer/sharedmemory.c b/slsDetectorSoftware/moenchDetectorServer/sharedmemory.c new file mode 100755 index 000000000..4504cfe05 --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/sharedmemory.c @@ -0,0 +1,39 @@ +#include "sharedmemory.h" + +struct statusdata *stdata; + +int inism(int clsv) { + +static int scansmid; + + if (clsv==SMSV) { + if ( (scansmid=shmget(SMKEY,1024,IPC_CREAT | 0666 ))==-1 ) { + return -1; + } + if ( (stdata=shmat(scansmid,NULL,0))==(void*)-1) { + return -2; + } + } + + if (clsv==SMCL) { + if ( (scansmid=shmget(SMKEY,0,0) )==-1 ) { + return -3; + } + if ( (stdata=shmat(scansmid,NULL,0))==(void*)-1) { + return -4; + } + } + return 1; +} + +void write_status_sm(char *status) { + strcpy(stdata->status,status); +} + +void write_stop_sm(int v) { + stdata->stop=v; +} + +void write_runnumber_sm(int v) { + stdata->runnumber=v; +} diff --git a/slsDetectorSoftware/moenchDetectorServer/sharedmemory.h b/slsDetectorSoftware/moenchDetectorServer/sharedmemory.h new file mode 100755 index 000000000..bdbddf719 --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/sharedmemory.h @@ -0,0 +1,48 @@ +#ifndef SM +#define SM + +#include "sls_detector_defs.h" + +#include +#include +#include +#include +#include +#include +#include +//#include +#include +#include +#include +#include +#include +#include + + +#include + +#include +#include + +/* key for shared memory */ +#define SMKEY 10001 + +#define SMSV 1 +#define SMCL 2 + + +struct statusdata { + int runnumber; + int stop; + char status[20]; +} ; + + +/* for shared memory */ + +int inism(int clsv); +void write_status_sm(char *status); +void write_stop_sm(int v); +void write_runnumber_sm(int v); + +#endif diff --git a/slsDetectorSoftware/moenchDetectorServer/sls_detector_defs.h b/slsDetectorSoftware/moenchDetectorServer/sls_detector_defs.h new file mode 120000 index 000000000..c5062e03f --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/sls_detector_defs.h @@ -0,0 +1 @@ +../commonFiles/sls_detector_defs.h \ No newline at end of file diff --git a/slsDetectorSoftware/moenchDetectorServer/sls_detector_funcs.h b/slsDetectorSoftware/moenchDetectorServer/sls_detector_funcs.h new file mode 120000 index 000000000..844b67129 --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/sls_detector_funcs.h @@ -0,0 +1 @@ +../commonFiles/sls_detector_funcs.h \ No newline at end of file diff --git a/slsDetectorSoftware/moenchDetectorServer/stop_server.c b/slsDetectorSoftware/moenchDetectorServer/stop_server.c new file mode 100755 index 000000000..e3c8ff7e1 --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/stop_server.c @@ -0,0 +1,46 @@ +/* A simple server in the internet domain using TCP + The port number is passed as an argument */ + + +#include "sls_detector_defs.h" + + +#include "communication_funcs.h" +#include "firmware_funcs.h" + + +int sockfd; + +int main(int argc, char *argv[]) +{ + int portno; + int retval=0; + + portno = DEFAULT_PORTNO; + + + bindSocket(portno); + if (getServerError()) + return -1; + + + + /* waits for connection */ + while(retval!=GOODBYE) { +#ifdef VERBOSE + printf("\n"); +#endif +#ifdef VERY_VERBOSE + printf("Stop server: waiting for client call\n"); +#endif + acceptConnection(); + retval=stopStateMachine(); + closeConnection(); + } + + exitServer(); + printf("Goodbye!\n"); + + return 0; +} + diff --git a/slsDetectorSoftware/moenchDetectorServer/svnInfoMoench.h b/slsDetectorSoftware/moenchDetectorServer/svnInfoMoench.h new file mode 100644 index 000000000..e69de29bb diff --git a/slsDetectorSoftware/moenchDetectorServer/svnInfoMoenchTmp.h b/slsDetectorSoftware/moenchDetectorServer/svnInfoMoenchTmp.h new file mode 100644 index 000000000..58e48f497 --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/svnInfoMoenchTmp.h @@ -0,0 +1,11 @@ +//#define SVNPATH "" +#define SVNURL "" +//#define SVNREPPATH "" +#define SVNREPUUID "" +//#define SVNREV "" +//#define SVNKIND "" +//#define SVNSCHED "" +#define SVNAUTH "" +#define SVNREV "" +#define SVNDATE "" +// diff --git a/slsDetectorSoftware/moenchDetectorServer/trimming_funcs.c b/slsDetectorSoftware/moenchDetectorServer/trimming_funcs.c new file mode 100755 index 000000000..9a28b9b4a --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/trimming_funcs.c @@ -0,0 +1,749 @@ +#ifndef PICASSOD +#include "server_defs.h" +#else +#include "picasso_defs.h" +#endif +#include "trimming_funcs.h" +#include "mcb_funcs.h" +#include "firmware_funcs.h" +#include + + + +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/moenchDetectorServer/trimming_funcs.h b/slsDetectorSoftware/moenchDetectorServer/trimming_funcs.h new file mode 100755 index 000000000..42ecea24a --- /dev/null +++ b/slsDetectorSoftware/moenchDetectorServer/trimming_funcs.h @@ -0,0 +1,20 @@ +#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/mythenDetectorServer/svnInfoMythen.h b/slsDetectorSoftware/mythenDetectorServer/svnInfoMythen.h index d225fa860..e71fc46de 100644 --- a/slsDetectorSoftware/mythenDetectorServer/svnInfoMythen.h +++ b/slsDetectorSoftware/mythenDetectorServer/svnInfoMythen.h @@ -2,10 +2,10 @@ #define SVNURL "file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware/mythenDetectorServer" //#define SVNREPPATH "" #define SVNREPUUID "951219d9-93cf-4727-9268-0efd64621fa3" -//#define SVNREV 0x484 +//#define SVNREV 0x504 //#define SVNKIND "" //#define SVNSCHED "" -#define SVNAUTH "l_maliakal_d" -#define SVNREV 0x484 -#define SVNDATE 0x20130306 +#define SVNAUTH "bergamaschi" +#define SVNREV 0x504 +#define SVNDATE 0x20130408 // diff --git a/slsDetectorSoftware/slsDetector/svnInfoLib.h b/slsDetectorSoftware/slsDetector/svnInfoLib.h index 8e3207eeb..94dfac30b 100644 --- a/slsDetectorSoftware/slsDetector/svnInfoLib.h +++ b/slsDetectorSoftware/slsDetector/svnInfoLib.h @@ -2,10 +2,10 @@ #define SVNURLLIB "file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware" //#define SVNREPPATH "" #define SVNREPUUIDLIB "951219d9-93cf-4727-9268-0efd64621fa3" -//#define SVNREV 0x494 +//#define SVNREV 0x506 //#define SVNKIND "" //#define SVNSCHED "" #define SVNAUTHLIB "l_maliakal_d" -#define SVNREVLIB 0x494 -#define SVNDATELIB 0x20130315 +#define SVNREVLIB 0x506 +#define SVNDATELIB 0x20130411 // diff --git a/slsDetectorSoftware/slsReceiver/svnInfoReceiver.h b/slsDetectorSoftware/slsReceiver/svnInfoReceiver.h index 8189948c2..38ccb75bb 100644 --- a/slsDetectorSoftware/slsReceiver/svnInfoReceiver.h +++ b/slsDetectorSoftware/slsReceiver/svnInfoReceiver.h @@ -2,10 +2,10 @@ #define SVNURL "file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware/slsReceiver" //#define SVNREPPATH "" #define SVNREPUUID "951219d9-93cf-4727-9268-0efd64621fa3" -//#define SVNREV 0x491 +//#define SVNREV 0x506 //#define SVNKIND "" //#define SVNSCHED "" #define SVNAUTH "l_maliakal_d" -#define SVNREV 0x491 -#define SVNDATE 0x20130313 +#define SVNREV 0x506 +#define SVNDATE 0x20130411 //