diff --git a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c index 4beb7cbe1..9af050bcc 100644 --- a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c @@ -15,8 +15,11 @@ #include #endif -// Global variable from slsDetectorServer +// Global variable from slsDetectorServer_funcs extern int debugflag; + + +// Global variable from communication_funcs.c extern int isControlServer; int firmware_compatibility = OK; @@ -93,7 +96,7 @@ int getFirmwareCheckResult(char** mess) { return firmware_compatibility; } -void checkFirmwareCompatibility() { +void basictests() { firmware_compatibility = OK; firmware_check_done = 0; memset(firmware_message, 0, MAX_STR_LENGTH); @@ -1231,7 +1234,7 @@ int configureMAC(uint32_t destip, uint64_t destmac, uint64_t sourcemac, uint32_t Beb_SetUpUDPHeader(beb_num,send_to_ten_gig,header_number+i,dst_mac,dst_ip, dst_port)) { FILE_LOG(logDEBUG1, ("\tset up left ok\n")); } else { - return -1; + return FAIL; } /*}*/ @@ -1246,7 +1249,7 @@ int configureMAC(uint32_t destip, uint64_t destmac, uint64_t sourcemac, uint32_t Beb_SetUpUDPHeader(beb_num,send_to_ten_gig,header_number+i,dst_mac,dst_ip, dst_port)) { FILE_LOG(logDEBUG1, (" set up right ok\n")); } else { - return -1; + return FAIL; } /*}*/ @@ -1255,7 +1258,7 @@ int configureMAC(uint32_t destip, uint64_t destmac, uint64_t sourcemac, uint32_t for(i=0;i<32;i++) dst_requested[i] = 0; //clear dst requested nimages_per_request=eiger_nexposures * eiger_ncycles; #endif - return 0; + return OK; } diff --git a/slsDetectorServers/gotthardDetectorServer/Makefile b/slsDetectorServers/gotthardDetectorServer/Makefile index feaa65af0..e02c5c9f3 100755 --- a/slsDetectorServers/gotthardDetectorServer/Makefile +++ b/slsDetectorServers/gotthardDetectorServer/Makefile @@ -1,19 +1,18 @@ -CROSS = bfin-uclinux- -CC = $(CROSS)gcc +CROSS = bfin-uclinux- +CC = $(CROSS)gcc -CFLAGS += -Wall -DGOTTHARDD -DDEBUG # -DVERBOSE #-DVERYVERBOSE #-DVIRTUAL +CFLAGS += -Wall -DGOTTHARDD -DSTOP_SERVER # -DVERBOSE +LDLIBS += -lm -lstdc++ -PROGS= gotthardDetectorServer -INSTDIR= /tftpboot -INSTMODE= 0777 +PROGS = gotthardDetectorServer +DESTDIR ?= bin +INSTMODE = 0777 + +SRCS = communication_funcs.c slsDetectorServer.c slsDetectorServer_funcs.c slsDetectorFunctionList.c +OBJS = $(SRCS:%.c=%.o) -BINS = testlib_sharedlibc -SRCS = server.c firmware_funcs.c server_funcs.c communication_funcs.c -OBJS = $(SRCS:%.c=%.o) - - -all: clean versioning $(PROGS) +all: clean versioning $(PROGS) boot: $(OBJS) @@ -21,21 +20,14 @@ versioning: @echo `tput setaf 6; ./updateGitVersion.sh; tput sgr0;` $(PROGS): $(OBJS) - echo $(OBJS) - $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS_$@) $(LDFLAGS_$@) - rm gotthardDetectorServer.gdb - - -install: $(PROGS) - $(INSTALL) -d $(INSTDIR) - $(INSTALL) -m $(INSTMODE) $(PROGS) $(INSTDIR) - - -romfs: - $(ROMFSINST) /bin/$(PROGS) +# echo $(OBJS) + mkdir -p $(DESTDIR) + $(CC) -o $@ $^ $(CFLAGS) $(LDLIBS) + mv $(PROGS) $(DESTDIR) + rm *.gdb clean: - rm -rf $(PROGS) *.o *.gdb + rm -rf $(DESTDIR)/$(PROGS) *.o *.gdb diff --git a/slsDetectorServers/gotthardDetectorServer/Makefile.virtual b/slsDetectorServers/gotthardDetectorServer/Makefile.virtual index 8574cbec7..65174a82f 100755 --- a/slsDetectorServers/gotthardDetectorServer/Makefile.virtual +++ b/slsDetectorServers/gotthardDetectorServer/Makefile.virtual @@ -1,30 +1,27 @@ +CC = gcc +CFLAGS += -Wall -DGOTTHARDD -DVIRTUAL -DSTOP_SERVER #-DVERBOSEI #-DVERBOSE +LDLIBS += -lm -lstdc++ -pthread -DESTDIR ?= ./ +PROGS = gotthardDetectorServer_virtual +DESTDIR ?= bin +INSTMODE = 0777 -CC = gcc -CFLAGS += -Wall -DGOTTHARDD -DVIRTUAL - - -PROGS= $(DESTDIR)/gotthardDetectorServer_virtual - - -SRCS = server.c server_funcs.c communication_funcs.c firmware_funcs.c -OBJS = $(SRCS:%.c=%.o) - -gotthardVirtualServer = $(PROGS) - -all: clean $(PROGS) +SRC_CLNT = communication_funcs.c slsDetectorServer.c slsDetectorServer_funcs.c slsDetectorFunctionList.c +OBJS = $(SRC_CLNT:.c=.o) + +all: clean versioning $(PROGS) +boot: $(OBJS) +versioning: + @echo `tput setaf 6; ./updateGitVersion.sh; tput sgr0;` + $(PROGS): $(OBJS) - $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS_$@) $(LDFLAGS_$@) - +# echo $(OBJS) + mkdir -p $(DESTDIR) + $(CC) -o $@ $^ $(CFLAGS) $(LDLIBS) + mv $(PROGS) $(DESTDIR) clean: - rm -rf $(PROGS) *.o *.gdb - - - - - - + rm -rf $(DESTDIR)/$(PROGS) *.o + \ No newline at end of file diff --git a/slsDetectorServers/gotthardDetectorServer/RegisterDefs.h b/slsDetectorServers/gotthardDetectorServer/RegisterDefs.h new file mode 100755 index 000000000..74f4883be --- /dev/null +++ b/slsDetectorServers/gotthardDetectorServer/RegisterDefs.h @@ -0,0 +1,328 @@ +#pragma once + +/* Definitions for FPGA*/ +#define MEM_MAP_SHIFT 11 + +/** Gain register */ +#define GAIN_REG (0x10 << MEM_MAP_SHIFT) + +#define GAIN_CONFGAIN_OFST (0) +#define GAIN_CONFGAIN_MSK (0x000000FF << GAIN_CONFGAIN_OFST) +#define GAIN_CONFGAIN_HGH_GAIN_VAL ((0x0 << GAIN_CONFGAIN_OFST) & GAIN_CONFGAIN_MSK) +#define GAIN_CONFGAIN_DYNMC_GAIN_VAL ((0x8 << GAIN_CONFGAIN_OFST) & GAIN_CONFGAIN_MSK) +#define GAIN_CONFGAIN_LW_GAIN_VAL ((0x6 << GAIN_CONFGAIN_OFST) & GAIN_CONFGAIN_MSK) +#define GAIN_CONFGAIN_MDM_GAIN_VAL ((0x2 << GAIN_CONFGAIN_OFST) & GAIN_CONFGAIN_MSK) +#define GAIN_CONFGAIN_VRY_HGH_GAIN_VAL ((0x1 << GAIN_CONFGAIN_OFST) & GAIN_CONFGAIN_MSK) + +/** Multi Purpose register */ +#define MULTI_PURPOSE_REG (0x14 << MEM_MAP_SHIFT) + +#define PHS_STP_OFST (0) +#define PHS_STP_MSK (0x00000001 << PHS_STP_OFST) +#define RST_CNTR_OFST (2) +#define RST_CNTR_MSK (0x00000001 << RST_CNTR_OFST) +#define SW1_OFST (5) +#define SW1_MSK (0x00000001 << SW1_OFST) +#define WRT_BCK_OFST (6) +#define WRT_BCK_MSK (0x00000001 << WRT_BCK_OFST) +#define RST_OFST (7) +#define RST_MSK (0x00000001 << RST_OFST) +#define PLL_CLK_SL_OFST (8) +#define PLL_CLK_SL_MSK (0x00000007 << PLL_CLK_SL_OFST) +#define PLL_CLK_SL_MSTR_VAL ((0x1 << PLL_CLK_SL_OFST) & PLL_CLK_SL_MSK) +#define PLL_CLK_SL_MSTR_ADC_VAL ((0x2 << PLL_CLK_SL_OFST) & PLL_CLK_SL_MSK) +#define PLL_CLK_SL_SLV_VAL ((0x3 << PLL_CLK_SL_OFST) & PLL_CLK_SL_MSK) +#define PLL_CLK_SL_SLV_ADC_VAL ((0x4 << PLL_CLK_SL_OFST) & PLL_CLK_SL_MSK) +#define ENT_RSTN_OFST (11) +#define ENT_RSTN_MSK (0x00000001 << ENT_RSTN_OFST) +#define INT_RSTN_OFST (12) +#define INT_RSTN_MSK (0x00000001 << INT_RSTN_OFST) +#define DGTL_TST_OFST (14) +#define DGTL_TST_MSK (0x00000001 << DGTL_TST_OFST) +#define CHNG_AT_PWR_ON_OFST (15) // Not used in SW +#define CHNG_AT_PWR_ON_MSK (0x00000001 << CHNG_AT_PWR_ON_OFST) // Not used in SW +#define RST_TO_SW1_DLY_OFST (16) +#define RST_TO_SW1_DLY_MSK (0x0000000F << RST_TO_SW1_DLY_OFST) +#define STRT_ACQ_DLY_OFST (20) +#define STRT_ACQ_DLY_MSK (0x0000000F << STRT_ACQ_DLY_OFST) + +/** DAQ register */ +#define DAQ_REG (0x15 << MEM_MAP_SHIFT) + +#define DAQ_TKN_TMNG_OFST (0) +#define DAQ_TKN_TMNG_MSK (0x0000FFFF << DAQ_TKN_TMNG_OFST) +#define DAQ_TKN_TMNG_BRD_RVSN_1_VAL ((0x1f16 << DAQ_TKN_TMNG_OFST) & DAQ_TKN_TMNG_MSK) +#define DAQ_TKN_TMNG_BRD_RVSN_2_VAL ((0x1f0f << DAQ_TKN_TMNG_OFST) & DAQ_TKN_TMNG_MSK) +#define DAQ_PCKT_LNGTH_OFST (16) +#define DAQ_PCKT_LNGTH_MSK (0x0000FFFF << DAQ_PCKT_LNGTH_OFST) +#define DAQ_PCKT_LNGTH_NO_ROI_VAL ((0x0013f << DAQ_PCKT_LNGTH_OFST) & DAQ_PCKT_LNGTH_MSK) +#define DAQ_PCKT_LNGTH_ROI_VAL ((0x0007f << DAQ_PCKT_LNGTH_OFST) & DAQ_PCKT_LNGTH_MSK) + +/** DAC Control register */ +#define DAC_CNTRL_REG (0x17 << MEM_MAP_SHIFT) + +#define DAC_CNTRL_CS_OFST (0) +#define DAC_CNTRL_CS_MSK (0x00000001 << DAC_CNTRL_CS_OFST) +#define DAC_CNTRL_CLK_OFST (1) +#define DAC_CNTRL_CLK_MSK (0x00000001 << DAC_CNTRL_CLK_OFST) +#define DAQ_CNTRL_DGTL_OFST (2) +#define DAQ_CNTRL_DGTL_MSK (0x00000001 << DAQ_CNTRL_DGTL_OFST) + +/** ADC SPI register */ +#define ADC_SPI_REG (0x18 << MEM_MAP_SHIFT) + +#define ADC_SERIAL_CLK_OUT_OFST (0) +#define ADC_SERIAL_CLK_OUT_MSK (0x00000001 << ADC_SERIAL_CLK_OUT_OFST) +#define ADC_SERIAL_DATA_OUT_OFST (1) +#define ADC_SERIAL_DATA_OUT_MSK (0x00000001 << ADC_SERIAL_DATA_OUT_OFST) +#define ADC_SERIAL_CS_OUT_OFST (2) +#define ADC_SERIAL_CS_OUT_MSK (0x0000000F << ADC_SERIAL_CS_OUT_OFST) + +/** ADC Sync register */ +#define ADC_SYNC_REG (0x19 << MEM_MAP_SHIFT) + +#define ADC_SYNC_ENET_STRT_DLY_OFST (0) +#define ADC_SYNC_ENET_STRT_DLY_MSK (0x0000000F << ADC_SYNC_ENET_STRT_DLY_OFST) +#define ADC_SYNC_ENET_STRT_DLY_VAL ((0x4 << ADC_SYNC_ENET_STRT_DLY_OFST) & ADC_SYNC_ENET_STRT_DLY_MSK) +#define ADC_SYNC_TKN1_HGH_DLY_OFST (4) +#define ADC_SYNC_TKN1_HGH_DLY_MSK (0x0000000F << ADC_SYNC_TKN1_HGH_DLY_OFST) +#define ADC_SYNC_TKN1_HGH_DLY_VAL ((0x1 << ADC_SYNC_TKN1_HGH_DLY_OFST) & ADC_SYNC_TKN1_HGH_DLY_MSK) +#define ADC_SYNC_TKN2_HGH_DLY_OFST (8) +#define ADC_SYNC_TKN2_HGH_DLY_MSK (0x0000000F << ADC_SYNC_TKN2_HGH_DLY_OFST) +#define ADC_SYNC_TKN2_HGH_DLY_VAL ((0x2 << ADC_SYNC_TKN2_HGH_DLY_OFST) & ADC_SYNC_TKN2_HGH_DLY_MSK) +#define ADC_SYNC_TKN1_LOW_DLY_OFST (12) +#define ADC_SYNC_TKN1_LOW_DLY_MSK (0x0000000F << ADC_SYNC_TKN1_LOW_DLY_OFST) +#define ADC_SYNC_TKN1_LOW_DLY_VAL ((0x2 << ADC_SYNC_TKN1_LOW_DLY_OFST) & ADC_SYNC_TKN1_LOW_DLY_MSK) +#define ADC_SYNC_TKN2_LOW_DLY_OFST (16) +#define ADC_SYNC_TKN2_LOW_DLY_MSK (0x0000000F << ADC_SYNC_TKN2_LOW_DLY_OFST) +#define ADC_SYNC_TKN2_LOW_DLY_VAL ((0x3 << ADC_SYNC_TKN2_LOW_DLY_OFST) & ADC_SYNC_TKN2_LOW_DLY_MSK) +//0x32214 +#define ADC_SYNC_TKN_VAL (ADC_SYNC_ENET_STRT_DLY_VAL | ADC_SYNC_TKN1_HGH_DLY_VAL | ADC_SYNC_TKN2_HGH_DLY_VAL | ADC_SYNC_TKN1_LOW_DLY_VAL | ADC_SYNC_TKN2_LOW_DLY_VAL) +#define ADC_SYNC_CLEAN_FIFOS_OFST (20) +#define ADC_SYNC_CLEAN_FIFOS_MSK (0x00000001 << ADC_SYNC_CLEAN_FIFOS_OFST)// FIXME: tried with 1, works? else put 0x011 as mask +#define ADC_SYNC_ENET_DELAY_OFST (24) +#define ADC_SYNC_ENET_DELAY_MSK (0x000000FF << ADC_SYNC_ENET_DELAY_OFST) +#define ADC_SYNC_ENET_DELAY_NO_ROI_VAL ((0x88 << ADC_SYNC_ENET_DELAY_OFST) & ADC_SYNC_ENET_DELAY_MSK) +#define ADC_SYNC_ENET_DELAY_ROI_VAL ((0x1b << ADC_SYNC_ENET_DELAY_OFST) & ADC_SYNC_ENET_DELAY_MSK) +//FIXME: try with just 0x8 and 0x1.. it is anded with 0000 in firmware anyway + +/** Temperatre SPI In register */ +#define TEMP_SPI_IN_REG (0x1b << MEM_MAP_SHIFT) + +#define TEMP_SPI_IN_T1_CLK_OFST (0) +#define TEMP_SPI_IN_T1_CLK_MSK (0x00000001 << TEMP_SPI_IN_T1_CLK_OFST) +#define TEMP_SPI_IN_T1_CS_OFST (1) +#define TEMP_SPI_IN_T1_CS_MSK (0x00000001 << TEMP_SPI_IN_T1_CS_OFST) +#define TEMP_SPI_IN_T2_CLK_OFST (2) +#define TEMP_SPI_IN_T2_CLK_MSK (0x00000001 << TEMP_SPI_IN_T2_CLK_OFST) +#define TEMP_SPI_IN_T2_CS_OFST (3) +#define TEMP_SPI_IN_T2_CS_MSK (0x00000001 << TEMP_SPI_IN_T2_CS_OFST) + +/** Temperatre SPI Out register */ +#define TEMP_SPI_OUT_REG (0x1c << MEM_MAP_SHIFT) + +#define TEMP_SPI_OUT_T1_DT_OFST (0) +#define TEMP_SPI_OUT_T1_DT_MSK (0x00000001 << TEMP_SPI_OUT_T1_DT_OFST) +#define TEMP_SPI_OUT_T2_DT_OFST (1) +#define TEMP_SPI_OUT_T2_DT_MSK (0x00000001 << TEMP_SPI_OUT_T2_DT_OFST) + +/** TSE Configure register */ +#define TSE_CONF_REG (0x1d << MEM_MAP_SHIFT) + +/** SPI Configure register */ +#define ENET_CONF_REG (0x1e << MEM_MAP_SHIFT) + +/** High Voltage register */ +#define HV_REG (0x20 << MEM_MAP_SHIFT) + +#define HV_ENBL_OFST (0) +#define HV_ENBL_MSK (0x00000001 << HV_ENBL_OFST) +#define HV_SEL_OFST (1) +#define HV_SEL_MSK (0x00000007 << HV_SEL_OFST) +#define HV_SEL_90_VAL ((0x0 << HV_SEL_OFST) & HV_SEL_MSK) +#define HV_SEL_110_VAL ((0x1 << HV_SEL_OFST) & HV_SEL_MSK) +#define HV_SEL_120_VAL ((0x2 << HV_SEL_OFST) & HV_SEL_MSK) +#define HV_SEL_150_VAL ((0x3 << HV_SEL_OFST) & HV_SEL_MSK) +#define HV_SEL_180_VAL ((0x4 << HV_SEL_OFST) & HV_SEL_MSK) +#define HV_SEL_200_VAL ((0x5 << HV_SEL_OFST) & HV_SEL_MSK) + +/** Dummy register */ +#define DUMMY_REG (0x21 << MEM_MAP_SHIFT) + +/** Firmware Version register */ +#define FPGA_VERSION_REG (0x22 << MEM_MAP_SHIFT) + +#define FPGA_VERSION_OFST (0) +#define FPGA_VERSION_MSK (0x00FFFFFF << FPGA_VERSION_OFST) // to get in format yymmdd + +/* Fix Pattern register */ +#define FIX_PATT_REG (0x23 << MEM_MAP_SHIFT) + +#define FIXED_PATT_VAL (0xACDC1980) + +/** Config register */ +#define Config_REG (0x26 << MEM_MAP_SHIFT) + +#define CONFIG_SLAVE_OFST (0) // Not used in FW & SW +#define CONFIG_SLAVE_MSK (0x00000001 << CONFIG_SLAVE_OFST) +#define CONFIG_MASTER_OFST (1) // Not used in FW & SW +#define CONFIG_MASTER_MSK (0x00000001 << CONFIG_MASTER_OFST) +#define CONFIG_TM_GT_ENBL_OFST (2) // Not used in FW & SW +#define CONFIG_TM_GT_ENBL_MSK (0x00000001 << CONFIG_TM_GT_ENBL_OFST) +#define CONFIG_CPU_RDT_OFST (12) +#define CONFIG_CPU_RDT_MSK (0x00000001 << CONFIG_CPU_RDT_OFST) +#define CONFIG_CNTNS_RDT_OFST (23) // Not used in FW & SW +#define CONFIG_CNTNS_RDT_MSK (0x00000001 << CONFIG_CNTNS_RDT_OFST) +#define CONFIG_ACCMLT_CNTS_OFST (24) // Not used in FW & SW +#define CONFIG_ACCMLT_CNTS_MSK (0x00000001 << CONFIG_ACCMLT_CNTS_OFST) + +/** 16 bit Control register */ +#define CONTROL_REG (0x24 << MEM_MAP_SHIFT) + +#define CONTROL_STRT_ACQ_OFST (0) +#define CONTROL_STRT_ACQ_MSK (0x00000001 << CONTROL_STRT_ACQ_OFST) +#define CONTROL_STP_ACQ_OFST (1) +#define CONTROL_STP_ACQ_MSK (0x00000001 << CONTROL_STP_ACQ_OFST) +#define CONTROL_STRT_FF_TST_OFST (2) // Not used in FW & SW +#define CONTROL_STRT_FF_TST_MSK (0x00000001 << CONTROL_STRT_FF_TST_OFST) +#define CONTROL_STP_FF_TST_OFST (3) // Not used in FW & SW +#define CONTROL_STP_FF_TST_MSK (0x00000001 << CONTROL_STP_FF_TST_OFST) +#define CONTROL_STRT_RDT_OFST (4) +#define CONTROL_STRT_RDT_MSK (0x00000001 << CONTROL_STRT_RDT_OFST) +#define CONTROL_STP_RDT_OFST (5) +#define CONTROL_STP_RDT_MSK (0x00000001 << CONTROL_STP_RDT_OFST) +#define CONTROL_STRT_EXPSR_OFST (6) +#define CONTROL_STRT_EXPSR_MSK (0x00000001 << CONTROL_STRT_EXPSR_OFST) +#define CONTROL_STP_EXPSR_OFST (7) +#define CONTROL_STP_EXPSR_MSK (0x00000001 << CONTROL_STP_EXPSR_OFST) +#define CONTROL_STRT_TRN_OFST (8) +#define CONTROL_STRT_TRN_MSK (0x00000001 << CONTROL_STRT_TRN_OFST) +#define CONTROL_STP_TRN_OFST (9) +#define CONTROL_STP_TRN_MSK (0x00000001 << CONTROL_STP_TRN_OFST) +#define CONTROL_SYNC_RST_OFST (10) +#define CONTROL_SYNC_RST_MSK (0x00000001 << CONTROL_SYNC_RST_OFST) + +/** Status register */ +#define STATUS_REG (0x25 << MEM_MAP_SHIFT) + +#define STATUS_RN_BSY_OFST (0) +#define STATUS_RN_BSY_MSK (0x00000001 << STATUS_RN_BSY_OFST) +#define STATUS_RDT_BSY_OFST (1) +#define STATUS_RDT_BSY_MSK (0x00000001 << STATUS_RDT_BSY_OFST) +#define STATUS_WTNG_FR_TRGGR_OFST (3) +#define STATUS_WTNG_FR_TRGGR_MSK (0x00000001 << STATUS_WTNG_FR_TRGGR_OFST) +#define STATUS_DLY_BFR_OFST (4) +#define STATUS_DLY_BFR_MSK (0x00000001 << STATUS_DLY_BFR_OFST) +#define STATUS_DLY_AFTR_OFST (5) +#define STATUS_DLY_AFTR_MSK (0x00000001 << STATUS_DLY_AFTR_OFST) +#define STATUS_EXPSNG_OFST (6) +#define STATUS_EXPSNG_MSK (0x00000001 << STATUS_EXPSNG_OFST) +#define STATUS_CNT_ENBL_OFST (7) +#define STATUS_CNT_ENBL_MSK (0x00000001 << STATUS_CNT_ENBL_OFST) +#define STATUS_RD_STT_OFST (8) +#define STATUS_RD_STT_MSK (0x00000007 << STATUS_RD_STT_OFST) +#define STATUS_RN_STT_OFST (12) +#define STATUS_RN_STT_MSK (0x00000007 << STATUS_RN_STT_OFST) +#define STATUS_SM_FF_FLL_OFST (15) +#define STATUS_SM_FF_FLL_MSK (0x00000001 << STATUS_SM_FF_FLL_OFST) +#define STATUS_ALL_FF_EMPTY_OFST (11) +#define STATUS_ALL_FF_EMPTY_MSK (0x00000001 << STATUS_ALL_FF_EMPTY_OFST) +#define STATUS_RN_MSHN_BSY_OFST (17) +#define STATUS_RN_MSHN_BSY_MSK (0x00000001 << STATUS_RN_MSHN_BSY_OFST) +#define STATUS_RD_MSHN_BSY_OFST (18) +#define STATUS_RD_MSHN_BSY_MSK (0x00000001 << STATUS_RD_MSHN_BSY_OFST) +#define STATUS_RN_FNSHD_OFST (20) +#define STATUS_RN_FNSHD_MSK (0x00000001 << STATUS_RN_FNSHD_OFST) +#define STATUS_IDLE_MSK (0x0000FFFF << 0) + +/** External Signal register */ +#define EXT_SIGNAL_REG (0x27 << MEM_MAP_SHIFT) + +#define EXT_SIGNAL_OFST (0) +#define EXT_SIGNAL_MSK (0x00000007 << EXT_SIGNAL_OFST) +#define EXT_SIGNAL_OFF_VAL ((0x0 << EXT_SIGNAL_OFST) & EXT_SIGNAL_MSK) +#define EXT_SIGNAL_TRGGR_IN_RSNG_VAL ((0x3 << EXT_SIGNAL_OFST) & EXT_SIGNAL_MSK) +#define EXT_SIGNAL_TRGGR_IN_FLLNG_VAL ((0x4 << EXT_SIGNAL_OFST) & EXT_SIGNAL_MSK) +/* +#define FPGA_SVN_REG 0x29<<11 +#define LOOK_AT_ME_REG 0x28<<11 +*/ +/** Chip of Interest register */ +#define CHIP_OF_INTRST_REG (0x2a << MEM_MAP_SHIFT) + +#define CHIP_OF_INTRST_ADC_SEL_OFST (0) +#define CHIP_OF_INTRST_ADC_SEL_MSK (0x0000001F << CHIP_OF_INTRST_ADC_SEL_OFST) +#define CHIP_OF_INTRST_NUM_CHNNLS_OFST (16) +#define CHIP_OF_INTRST_NUM_CHNNLS_MSK (0x0000FFFF << CHIP_OF_INTRST_NUM_CHNNLS_OFST) + +/** Board Version register */ +#define BOARD_REVISION_REG (0x2c << MEM_MAP_SHIFT) + +#define BOARD_REVISION_OFST (0) +#define BOARD_REVISION_MSK (0x0000FFFF << BOARD_REVISION_OFST) +#define DETECTOR_TYPE_OFST (16) +#define DETECTOR_TYPE_MSK (0x0000000F << DETECTOR_TYPE_OFST) +//#define DETECTOR_TYPE_GOTTHARD_VAL (??) +#define DETECTOR_TYPE_MOENCH_VAL (2) + +/* 16 bit Fifo Data register */ +//#define FIFO_DATA_REG (0x50 << MEM_MAP_SHIFT) // Not used in FW and SW (16bit) + +/* +#define MOD_DACS1_REG 0x65<<11 +#define MOD_DACS2_REG 0x66<<11 +#define MOD_DACS3_REG 0x67<<11 +*/ +/* Set Delay 64 bit register */ +#define SET_DELAY_LSB_REG (0x68 << MEM_MAP_SHIFT) +#define SET_DELAY_MSB_REG (0x69 << MEM_MAP_SHIFT) + +/* Get Delay 64 bit register */ +#define GET_DELAY_LSB_REG (0x6a << MEM_MAP_SHIFT) +#define GET_DELAY_MSB_REG (0x6b << MEM_MAP_SHIFT) + +/* Set Cycles 64 bit register */ +#define SET_TRAINS_LSB_REG (0x6c << MEM_MAP_SHIFT) +#define SET_TRAINS_MSB_REG (0x6d << MEM_MAP_SHIFT) + +/* Get Cycles 64 bit register */ +#define GET_TRAINS_LSB_REG (0x6e << MEM_MAP_SHIFT) +#define GET_TRAINS_MSB_REG (0x6f << MEM_MAP_SHIFT) + +/* Set Frames 64 bit register */ +#define SET_FRAMES_LSB_REG (0x70 << MEM_MAP_SHIFT) +#define SET_FRAMES_MSB_REG (0x71 << MEM_MAP_SHIFT) + +/* Get Frames 64 bit register */ +#define GET_FRAMES_LSB_REG (0x72 << MEM_MAP_SHIFT) +#define GET_FRAMES_MSB_REG (0x73 << MEM_MAP_SHIFT) + +/* Set Period 64 bit register */ +#define SET_PERIOD_LSB_REG (0x74 << MEM_MAP_SHIFT) +#define SET_PERIOD_MSB_REG (0x75 << MEM_MAP_SHIFT) + +/* Get Period 64 bit register */ +#define GET_PERIOD_LSB_REG (0x76 << MEM_MAP_SHIFT) +#define GET_PERIOD_MSB_REG (0x77 << MEM_MAP_SHIFT) + +/* Set Exptime 64 bit register */ +#define SET_EXPTIME_LSB_REG (0x78 << MEM_MAP_SHIFT) +#define SET_EXPTIME_MSB_REG (0x79 << MEM_MAP_SHIFT) + +/* Get Exptime 64 bit register */ +#define GET_EXPTIME_LSB_REG (0x7a << MEM_MAP_SHIFT) +#define GET_EXPTIME_MSB_REG (0x7b << MEM_MAP_SHIFT) + +/* Set Gates 64 bit register */ +//#define SET_GATES_LSB_REG (0x7c << MEM_MAP_SHIFT) // Not used in SW and FW +//#define SET_GATES_MSB_REG (0x7d << MEM_MAP_SHIFT) // Not used in SW and FW + +/* Dark Image starting address */ +#define DARK_IMAGE_REG (0x81 << MEM_MAP_SHIFT) + +/* Gain Image starting address */ +#define GAIN_IMAGE_REG (0x82 << MEM_MAP_SHIFT) + +/* Counter Block Memory starting address */ +#define COUNTER_MEMORY_REG (0x85 << MEM_MAP_SHIFT) + diff --git a/slsDetectorServers/gotthardDetectorServer/blackfin.h b/slsDetectorServers/gotthardDetectorServer/blackfin.h new file mode 120000 index 000000000..2873c7dc6 --- /dev/null +++ b/slsDetectorServers/gotthardDetectorServer/blackfin.h @@ -0,0 +1 @@ +../slsDetectorServer/blackfin.h \ No newline at end of file diff --git a/slsDetectorServers/gotthardDetectorServer/firmware_funcs.c b/slsDetectorServers/gotthardDetectorServer/firmware_funcs.c deleted file mode 100755 index d977b04e4..000000000 --- a/slsDetectorServers/gotthardDetectorServer/firmware_funcs.c +++ /dev/null @@ -1,2098 +0,0 @@ - -#include "server_defs.h" -#include "firmware_funcs.h" -#include "registers_g.h" - -#include -#include -#include - -#include -#include - - -u_int64_t CSP0BASE; -int phase_shift=DEFAULT_PHASE_SHIFT; -int ipPacketSize=DEFAULT_IP_PACKETSIZE; -int udpPacketSize=DEFAULT_UDP_PACKETSIZE; - - -int timingMode=AUTO_TIMING; -enum externalSignalFlag signalMode=EXT_SIG_OFF; -int ROI_flag=0; -int adcConfigured=-1; -int detectorFirstServer = 1; -enum detectorSettings thisSettings; -ROI rois[MAX_ROIS]; -int nROI=0; - -//for the 25um detectors -int masterflags = NO_MASTER; -int masterdefaultdelay = 62; -int patternphase = 0; -int adcphase = 0; -int slavepatternphase = 0; -int slaveadcphase = 0; -int rsttosw1delay = 2; -int startacqdelay = 1; - - -sls_detector_module *detectorModules=NULL; -int *detectorDacs=NULL; -int *detectorAdcs=NULL; - - -/** - 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 = malloc(MEM_SIZE); - printf("memory allocated\n"); -#endif - printf("CSPObase is 0x%llx \n",CSP0BASE); - printf("CSPOBASE=from %llx to %llx\n",CSP0BASE,CSP0BASE+MEM_SIZE); - - printf("statusreg=%08x\n",bus_r(STATUS_REG)); - printf("\n\n"); - 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; -} - - -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 initDetector() { -#ifdef VERBOSE - printf("Board is for 1 module\n"); -#endif - detectorModules=malloc(sizeof(sls_detector_module)); - detectorDacs=malloc(NDAC*sizeof(int)); - detectorAdcs=malloc(NADC*sizeof(int)); -#ifdef VERBOSE - printf("modules from 0x%x to 0x%x\n",(unsigned int)(detectorModules), (unsigned int)(detectorModules)); - printf("dacs from 0x%x to 0x%x\n",(unsigned int)(detectorDacs), (unsigned int)(detectorDacs)); - printf("adcs from 0x%x to 0x%x\n",(unsigned int)(detectorAdcs), (unsigned int)(detectorAdcs)); -#endif - (detectorModules)->dacs=detectorDacs; - (detectorModules)->adcs=detectorAdcs; - (detectorModules)->ndac=NDAC; - (detectorModules)->nadc=NADC; - (detectorModules)->nchip=NCHIP; - (detectorModules)->nchan=NCHIP*NCHAN; - (detectorModules)->gain=0; - (detectorModules)->offset=0; - (detectorModules)->reg=0; - - - thisSettings=UNINITIALIZED; - - testFpga(); - - //gotthard specific - setPhaseShiftOnce(); - configureADC(); - setADC(-1); //already does setdaqreg and clean fifo. - - setSettings(DYNAMICGAIN); - setDefaultDacs(); - - - //Initialization - setFrames(1); - setTrains(1); - setExposureTime(1e6); - setPeriod(1e9); - setDelay(0); - setGates(0); - setTiming(GET_EXTERNAL_COMMUNICATION_MODE); - sendviaUDP(1); - setMasterSlaveConfiguration(); - - return OK; -} - - -int setDefaultDacs() { - printf("Setting Default Dac values\n"); - - int ret = OK; - int i = 0; - int retval[2]={-1,-1}; - const int defaultvals[NDAC] = DEFAULT_DAC_VALS; - - for(i = 0; i < NDAC; ++i) { - // if not already default, set it to default - if (setDACRegister(i, -1) != defaultvals[i]) { - setDAC(i, defaultvals[i], 0, retval); - if (abs(retval[0] - defaultvals[i])<=3) { - cprintf(RED, "Warning: Setting dac %d failed, wrote %d, read %d\n",i ,defaultvals[i], retval[0]); - ret = FAIL; - } - } - } - - return ret; -} - - - - - -void setMasterSlaveConfiguration(){ - - // global master default delay picked from config file - FILE* fd=fopen(CONFIG_FILE,"r"); - if(fd==NULL){ - cprintf(RED,"\nWarning: Could not open file\n"); - return; - } - cprintf(BLUE,"config file %s opened\n", CONFIG_FILE); - - char key[256]; - char value[256]; - char line[256]; - int ival=0; - u_int32_t val=0; - - while (fgets(line, sizeof(line), fd)) { - if(line[0] == '#') - continue; - sscanf(line, "%s %s\n", key, value); - if (!strcasecmp(key,"masterflags")) { - if (!strcasecmp(value,"is_master")) { - masterflags = IS_MASTER; - } - else if (!strcasecmp(value,"is_slave")) { - masterflags = IS_SLAVE; - } - else if (!strcasecmp(value,"no_master")){ - masterflags = NO_MASTER; - } - else { - cprintf(RED,"could not scan masterflags %s value from config file\n",value); - fclose(fd); - exit(EXIT_FAILURE); - } - - if (!detectorFirstServer) { - cprintf(BLUE, "Server has been started up before. Ignoring rest of config file\n"); - fclose(fd); - return; - } - } - else { - if(sscanf(value,"%d",&ival)<=0) { - cprintf(RED,"could not scan patternphase %s value from config file\n",value); - fclose(fd); - exit(EXIT_FAILURE); - } - - if (!strcasecmp(key,"masterdefaultdelay")) - masterdefaultdelay = ival; - else if (!strcasecmp(key,"patternphase")) - patternphase = ival; - else if (!strcasecmp(key,"adcphase")) - adcphase = ival; - else if (!strcasecmp(key,"slavepatternphase")) - slavepatternphase = ival; - else if (!strcasecmp(key,"slaveadcphase")) - slaveadcphase = ival; - else if (!strcasecmp(key,"rsttosw1delay")) - rsttosw1delay = ival; - else if (!strcasecmp(key,"startacqdelay")) - startacqdelay = ival; - else { - cprintf(RED,"could not scan parameter name %s from config file\n",key); - fclose(fd); - exit(EXIT_FAILURE); - } - } - - } - cprintf(BLUE, - "masterflags: %d\n" - "masterdefaultdelay:%d\n" - "patternphase:%d\n" - "adcphase:%d\n" - "slavepatternphase:%d\n" - "slaveadcphase:%d\n" - "rsttosw1delay:%d\n" - "startacqdelay:%d\n", - masterflags, - masterdefaultdelay, - patternphase, - adcphase, - slavepatternphase, - slaveadcphase, - rsttosw1delay, - startacqdelay); - - - - if (masterflags == IS_MASTER) { - // set delay - setDelay(0); - - /* Set pattern phase for the master module */ - val=bus_r(MULTI_PURPOSE_REG); - val = (val & (~(PLL_CLK_SEL_MSK))) | PLL_CLK_SEL_MASTER_VAL; - bus_w(MULTI_PURPOSE_REG,val); - setPhaseShift(patternphase); - /* Set adc phase for the master module */ - val=bus_r(MULTI_PURPOSE_REG); - val = (val & (~(PLL_CLK_SEL_MSK))) | PLL_CLK_SEL_MASTER_ADC_VAL; - bus_w(MULTI_PURPOSE_REG,val); - setPhaseShift(adcphase); - /* Set pattern phase for the slave module */ - val=bus_r(MULTI_PURPOSE_REG); - val = (val & (~(PLL_CLK_SEL_MSK))) | PLL_CLK_SEL_SLAVE_VAL; - bus_w(MULTI_PURPOSE_REG,val); - setPhaseShift(slavepatternphase); - /* Set adc phase for the slave module */ - val=bus_r(MULTI_PURPOSE_REG); - val = (val & (~(PLL_CLK_SEL_MSK))) | PLL_CLK_SEL_SLAVE_ADC_VAL; - bus_w(MULTI_PURPOSE_REG,val); - setPhaseShift(slaveadcphase); - /* Set start acq delay */ - val=bus_r(MULTI_PURPOSE_REG); -#ifdef VERBOSE - printf("Multipurpose reg:0x%x\n",bus_r(MULTI_PURPOSE_REG)); -#endif - val = (val & (~(START_ACQ_DELAY_MSK))) | ((startacqdelay << START_ACQ_DELAY_OFFSET) & (START_ACQ_DELAY_MSK)); - bus_w(MULTI_PURPOSE_REG,val); - printf("Start acq delay set. Multipurpose reg: 0x%x\n",bus_r(MULTI_PURPOSE_REG)); - } - - /* Set RST to SW1 delay */ - val=bus_r(MULTI_PURPOSE_REG); -#ifdef VERBOSE - printf("Multipurpose reg:0x%x\n",bus_r(MULTI_PURPOSE_REG)); -#endif - val = (val & (~(RST_TO_SW1_DELAY_MSK))) | ((rsttosw1delay << RST_TO_SW1_DELAY_OFFSET) & (RST_TO_SW1_DELAY_MSK)); - bus_w(MULTI_PURPOSE_REG,val); - printf("RST to SW1 delay set. Multipurpose reg:0x%x\n",bus_r(MULTI_PURPOSE_REG)); - - fclose(fd); -} - - - - -int configureADC(){ - printf("Preparing ADC\n"); - u_int32_t valw,codata,csmask; - int i,j,cdx,ddx; - cdx=0; ddx=1; - csmask=0x7c; // 1111100 - - for(j=0;j<3;j++){ - //command and value; - codata = 0; - if(j==0) - codata=(0x08<<8)+(0x3);//Power modes(global) //reset - else if(j==1) - codata=(0x08<<8)+(0x0);//Power modes(global) //chip run - else - codata = (0x14<<8)+(0x0);//Output mode //offset binary - - - // start point - valw=0xffffffff; - bus_w(ADC_SPI_REG,(valw)); - - //chip sel bar down - valw=((0xffffffff&(~csmask))); - bus_w(ADC_SPI_REG,valw); - - for (i=0;i<24;i++) { - //cldwn - valw=valw&(~(0x1<>(23-i))&0x1)<>16)>0){ - //bus_w(addr,0x0); //clear the reg - - if(reg==0){ - detectorFirstServer = 1; - printf("\nImplementing phase shift of %d\n",phase_shift); - for (i=1;i=0) { - bus_w(CONFIG_REG,d); - } -#ifdef VERBOSE - printf("configuration register is %x", bus_r(CONFIG_REG)); -#endif - return bus_r(CONFIG_REG); -} - - - - -int sendviaUDP(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; -} - - - - - - -int setDACRegister(int idac, int val) { - u_int32_t addr, reg, mask; - int off; -#ifdef VERBOSE - if(val==-1) - printf("Getting dac register%d \n",idac); - else - printf("Setting dac register %d to %d\n",idac,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 register is %d\n\n",idac,val); -#endif - return val; -} - - - - - - - - - -u_int32_t setExtSignal(enum externalSignalFlag mode) { - - u_int32_t c; - c = bus_r(EXT_SIGNAL_REG); - -#ifdef VERBOSE - printf("settings signal variable number %d to value %04x\n", d, signals[d]); -#endif - - switch (mode) { - case TRIGGER_IN_RISING_EDGE: - case TRIGGER_IN_FALLING_EDGE: - - // set variable - signalMode = mode; - - // set signal only if trigger mode - if (timingMode==TRIGGER_EXPOSURE) - setFPGASignal(mode); - // switch off if not trigger mode, but variable remembers value - else - setFPGASignal(SIGNAL_OFF); - break; - - default: - mode = SIGNAL_OFF; - signalMode = mode; - setFPGASignal(mode); - break; - } - - return getExtSignal(); -} - - -int getExtSignal() { - return signalMode; -} - - -u_int32_t setFPGASignal(enum externalSignalFlag mode) { - - u_int32_t c = bus_r(EXT_SIGNAL_REG); - // offset is 0 as we only deal with the first signal index now. //int off = d * SIGNAL_OFFSET; - - // check and then write - if ((mode == SIGNAL_OFF) || (mode == TRIGGER_IN_RISING_EDGE) || (mode == TRIGGER_IN_FALLING_EDGE)) { -#ifdef VERBOSE - printf("writing signal register number %d mode %04x\n",0, (int) mode); -#endif - bus_w(EXT_SIGNAL_REG,( (c &~ (SIGNAL_MASK)) | ((int)mode) )); - } - return getFPGASignal(); -} - - - - -int getFPGASignal() { - - // offset is 0 as we only deal with the first signal index now. //int off = d * SIGNAL_OFFSET; - int mode = ((bus_r(EXT_SIGNAL_REG)) & SIGNAL_MASK); - - // check and then update variable - if ((mode == SIGNAL_OFF) || (mode == TRIGGER_IN_RISING_EDGE) || (mode == TRIGGER_IN_FALLING_EDGE)) { -#ifdef VERYVERBOSE - printf("gettings signal register number %d value %04x\n", d, (int)mode); -#endif - return (int)mode; - } else - return -1; -} - - - - - -int setTiming(int ti) { - - // set - if (ti != GET_EXTERNAL_COMMUNICATION_MODE) { - - // trigger - if (ti == TRIGGER_EXPOSURE) { - timingMode = ti; - if ((signalMode == TRIGGER_IN_RISING_EDGE) || (signalMode == TRIGGER_IN_FALLING_EDGE)) - setFPGASignal(signalMode); - else - setFPGASignal(SIGNAL_OFF); // only if both (timing & extsig) configured, its set to trigger, else off - } - - // auto - else { - timingMode = AUTO_TIMING; - setFPGASignal(SIGNAL_OFF); - } - } - - // get - int s = getFPGASignal(); - if ((s == TRIGGER_IN_RISING_EDGE) || (s == TRIGGER_IN_FALLING_EDGE)) - return TRIGGER_EXPOSURE; - return AUTO_TIMING; -} - - - - - - -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("Testing 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("----------------------------------------------------------------------------------------------"); - } - printf("\n"); - return result; -} - - -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 (i>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 for the %s is %.2fC\n",cTempSensor[tempSensor],val); -#endif - return val; -} - - - - -int setSettings(int i) { -#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=initConfGain(i,i); - if(retval==i) - isett=UNDEFINED; - } - //writing settings - else{ - retval=initConfGain(i,confgain[i]); - 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; -} - - - - - -int initConfGain(int isettings,int val){ - int retval; - u_int32_t addr=GAIN_REG; - - if(val!=-1){ -#ifdef VERBOSE - printf("Setting Gain with val:%d\n",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; -} - - - -ROI* setROI(int n, ROI arg[], int *retvalsize, int *ret){ - - int i,adc; - ROI temp; - - if(n>=0){ - - //clear rois - for(i=0;i=0) && (adc<=4)); - else { - printf("warning:adc value greater than 5. deleting roi\n"); - adc=-1; - } - } - } - printf("\tGoing to enable adc: %d\n", adc); - - //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; - printf("\tActual xmin:%d xmax:%d\n",rois[0].xmin,rois[0].xmax); - }else - nROI = 0; - - if((n!=0) && ((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 - if (nROI) { - printf("Rois:\n"); - for( i=0;imac.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, bus_r(addrr) | (INT_RSTN_BIT|ENET_RESETN_BIT|WRITE_BACK_BIT|DIGITAL_TEST_BIT)); //0x2840,write shadow regs.. - else - bus_w(addrr, bus_r(addrr) | (INT_RSTN_BIT|ENET_RESETN_BIT|WRITE_BACK_BIT)); //0x2840,write shadow regs.. - -#ifdef VERBOSE - printf("Value read from Multi-purpose Reg:%x\n",bus_r(addrr)); -#endif - // if(val!=0x2840) return -1; - - usleep(100000); - - bus_w(addrr, bus_r(addrr) &(~WRITE_BACK_BIT)); - - if(ival) - bus_w(addrr, bus_r(addrr) | (INT_RSTN_BIT|ENET_RESETN_BIT|SW1_BIT|DIGITAL_TEST_BIT)); //0x2820,write shadow regs.. - else - bus_w(addrr, bus_r(addrr) | (INT_RSTN_BIT|ENET_RESETN_BIT|SW1_BIT)); //0x2820,write shadow regs.. - -#ifdef VERBOSE - printf("Value read from Multi-purpose Reg:%x\n",bus_r(addrr)); -#endif - // if(val!=0x2820) return -1; - - usleep(1000 * 1000); - - return adcConfigured; -} - - -int getAdcConfigured(){ - return adcConfigured; -} - - - - - - - -// 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 = (value * 1E-3 * CLK_FREQ ) + 0.5; - } - return (set64BitReg(value,SET_EXPTIME_LSB_REG, SET_EXPTIME_MSB_REG) / - (1E-3 * CLK_FREQ)) + 0.5; -} - -int64_t getExposureTime(){ - return (get64BitReg(GET_EXPTIME_LSB_REG, GET_EXPTIME_MSB_REG) / - (1E-3 * CLK_FREQ)) + 0.5; -} - -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 = (value * 1E-3 * CLK_FREQ ) + 0.5; - } - return (set64BitReg(value,SET_PERIOD_LSB_REG, SET_PERIOD_MSB_REG) / - (1E-3 * CLK_FREQ)) + 0.5; -} - -int64_t getPeriod(){ - return (get64BitReg(GET_PERIOD_LSB_REG, GET_PERIOD_MSB_REG) / - (1E-3 * CLK_FREQ)) + 0.5; -} - -int64_t setDelay(int64_t value){ - /* time is in ns */ - if (value!=-1) { - if (masterflags == IS_MASTER) { - value += masterdefaultdelay; - cprintf(BLUE,"Actual delay for master: %lld\n", (long long int) value); - } - value = (value * 1E-3 * CLK_FREQ ) + 0.5; - } - int64_t retval = (set64BitReg(value,SET_DELAY_LSB_REG, SET_DELAY_MSB_REG) / - (1E-3 * CLK_FREQ)) + 0.5; - if (masterflags == IS_MASTER) { - cprintf(BLUE,"Actual delay read from master: %lld\n", (long long int) retval); - retval -= masterdefaultdelay; - } - - return retval; -} - -int64_t getDelay(){ - return (get64BitReg(GET_DELAY_LSB_REG, GET_DELAY_MSB_REG) / - (1E-3 * CLK_FREQ)) + 0.5; -} - -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 getActualTime(){ - return (get64BitReg(GET_ACTUAL_TIME_LSB_REG, GET_ACTUAL_TIME_MSB_REG) / - (1E-3 * CLK_FREQ)) + 0.5; -} - -int64_t getMeasurementTime(){ - int64_t v=get64BitReg(GET_MEASUREMENT_TIME_LSB_REG, GET_MEASUREMENT_TIME_MSB_REG); - return (v / (1E-3 * CLK_FREQ)) + 0.5; -} - - - - - - -u_int32_t fifoReadStatus() { - return bus_r(STATUS_REG)&(SOME_FIFO_FULL_BIT | ALL_FIFO_EMPTY_BIT); -} - -u_int32_t fifo_full(void) { - return bus_r(STATUS_REG)&SOME_FIFO_FULL_BIT; -} - - -u_int32_t runBusy(void) { - u_int32_t s = bus_r(STATUS_REG) & RUN_BUSY_BIT; - return s; -} - -u_int32_t runState(void) { - int s=bus_r(STATUS_REG); -#ifdef VERBOSE -printf("status %04x\n",s); -#endif - -return s; -} - - -// State Machine - -int startStateMachine(){ - - //#ifdef VERBOSE - printf("*******Starting State Machine*******\n"); - //#endif - cleanFifo(); - // fifoReset(); - - 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 - cprintf(BG_RED,"*******Stopping State Machine*******\n"); - //#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; - - bus_w16(CONTROL_REG, START_ACQ_BIT |START_READOUT_BIT); // start readout - bus_w16(CONTROL_REG, 0x0); - return OK; -} - - - - -void waitForAcquisitionFinish(){ - volatile u_int32_t t = bus_r(LOOK_AT_ME_REG); -#ifdef VERBOSE - printf("lookatmereg=x%x\n",t); -#endif - 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; - } 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); - } -} - - -int getStatus() { - u_int32_t retval= runState(); - enum runStatus s = IDLE; - int ret = OK; - - printf("\n\nSTATUS=%08x\n",retval); - - //stopped (external stop, also maybe fifo full) - if (runState() & STOPPED_BIT){ - printf("-----------------------------------STOPPED--------------------------------------x%0x\n",retval); - s=STOPPED; - - retval= runState(); - printf("reading again STATUS=%08x\n",retval); - if (runState() & STOPPED_BIT){ - printf("-----------------------------------ERROR--------------------------------------x%0x\n",retval); - s=ERROR; - } - } - - - //error - else if(retval&SOME_FIFO_FULL_BIT){ - printf("-----------------------------------ERROR--------------------------------------x%0x\n",retval); - s=ERROR; - } - - //runbusy=0 - // else if(!(retval&RUNMACHINE_BUSY_BIT)){ //commented by Anna 24.10.2012 - else if(!(retval&RUN_BUSY_BIT)){ // by Anna 24.10.2012 - - //and readbusy=1, its last frame read - if((retval&READMACHINE_BUSY_BIT) ){ // - - - printf("-----------------------------------READ MACHINE BUSY--------------------------\n"); - s=TRANSMITTING; - } else if (retval&ALL_FIFO_EMPTY_BIT) { - printf("-----------------------------------DATA IN FIFO--------------------------\n"); - s=TRANSMITTING; - - } - //and readbusy=0,idle - else if(!(retval&0xffff)){ - //if(!(retval&0x00000001)){ - printf("-----------------------------------IDLE--------------------------------------\n"); - s=IDLE; - } else { - printf("-----------------------------------Unknown status %08x--------------------------------------\n", retval); - s=ERROR; - ret=FAIL; - } - } - //if runbusy=1 - else { - if (retval&WAITING_FOR_TRIGGER_BIT){ - printf("-----------------------------------WAITING-----------------------------------\n"); - s=WAITING; - } - else{ - printf("-----------------------------------RUNNING-----------------------------------\n"); - s=RUNNING; - } - } - - return s; -} - - - -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((char*)ptr,(char*)ImageVals ,DATA_BYTES); -#ifdef VERBOSE -printf("\nLoaded x%08x address with image of index %d\n",(unsigned int)(ptr),index); -#endif -return OK; -} - - - -int readCounterBlock(int startACQ, short int CounterVals[]){ - - - u_int32_t val; - volatile u_int16_t *ptr; - - u_int32_t address = COUNTER_MEMORY_REG; - ptr=(u_int16_t*)(CSP0BASE+address*2); - - - if (runBusy()) { - if(stopStateMachine()==FAIL) - return FAIL; - //waiting for the last frame read to be done - while(runBusy()) usleep(500); -#ifdef VERBOSE - printf("State machine stopped\n"); -#endif - } - - val=bus_r(MULTI_PURPOSE_REG); -#ifdef VERBOSE - printf("Value of multipurpose reg:%d\n",bus_r(MULTI_PURPOSE_REG)); -#endif - - memcpy((char*)CounterVals,(char*)ptr,DATA_BYTES); -#ifdef VERBOSE - int i; - printf("Copied counter memory block with size of %d bytes..\n",DATA_BYTES); - for(i=0;i<6;i++) - printf("%d: %d\t",i,CounterVals[i]); -#endif - - - bus_w(MULTI_PURPOSE_REG,(val&~RESET_COUNTER_BIT)); -#ifdef VERBOSE - printf("\nClearing bit 2 of multipurpose reg:%d\n",bus_r(MULTI_PURPOSE_REG)); -#endif - - if(startACQ==1){ - startStateMachine(); - if(runBusy()) - printf("State machine RUNNING\n"); - else - printf("State machine IDLE\n"); - } - - - return OK; -} - - - - -int resetCounterBlock(int startACQ){ - - char *counterVals=NULL; - counterVals=realloc(counterVals,DATA_BYTES); - - int ret = OK; - u_int32_t val; - volatile u_int16_t *ptr; - - - u_int32_t address = COUNTER_MEMORY_REG; - ptr=(u_int16_t*)(CSP0BASE+address*2); - - - if (runBusy()) { - if(stopStateMachine()==FAIL) - return FAIL; - //waiting for the last frame read to be done - while(runBusy()) usleep(500); -#ifdef VERBOSE - printf("State machine stopped\n"); -#endif - } - - val=bus_r(MULTI_PURPOSE_REG); -#ifdef VERBOSE - printf("Value of multipurpose reg:%d\n",bus_r(MULTI_PURPOSE_REG)); -#endif - - - bus_w(MULTI_PURPOSE_REG,(val|RESET_COUNTER_BIT)); -#ifdef VERBOSE - printf("Setting bit 2 of multipurpose reg:%d\n",bus_r(MULTI_PURPOSE_REG)); -#endif - - - memcpy((char*)counterVals,(char*)ptr,DATA_BYTES); -#ifdef VERBOSE - int i; - printf("Copied counter memory block with size of %d bytes..\n",(int)sizeof(counterVals)); - for(i=0;i<6;i=i+2) - printf("%d: %d\t",i,*(counterVals+i)); -#endif - - - bus_w(MULTI_PURPOSE_REG,(val&~RESET_COUNTER_BIT)); -#ifdef VERBOSE - printf("\nClearing bit 2 of multipurpose reg:%d\n",bus_r(MULTI_PURPOSE_REG)); -#endif - - if(startACQ==1){ - startStateMachine(); - if(runBusy()) - printf("State machine RUNNING\n"); - else - printf("State machine IDLE\n"); - } - - if(sizeof(counterVals)<=0){ - printf("ERROR:size of counterVals=%d\n",(int)sizeof(counterVals)); - ret = FAIL; - } - - return ret; - -} - - - - - - -int copyModule(sls_detector_module *destMod, sls_detector_module *srcMod) { - - int idac, iadc; - - int ret=OK; - -#ifdef VERBOSE - printf("Copying module %x to module %x\n",(unsigned int)(srcMod),(unsigned int)(destMod)); -#endif - - if (srcMod->serialnumber>=0){ - 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; - - 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; -} - - - - - - - -int setDAC(int ind, int val,int mV, int retval[]) { - - if(mV) - val = (val*4096)/2500; - - if (val>=0) - initDAC(ind,val); - clearDACSregister(); - - retval[0] = setDACRegister(ind, -1); - retval[1] = (retval[0]*2500)/4096; - return retval[0]; - -} - - - -int getDAC(int ind) { - return setDACRegister(ind, -1); -} - - - - - - - -int setModule(sls_detector_module myMod) { - - printf("\ninside setmodule..\n"); - - int v[NDAC]; - v[VREF_DS]=(myMod.dacs)[0]; - v[VCASCN_PB]=(myMod.dacs)[1]; - v[VCASCP_PB]=(myMod.dacs)[2]; - v[VOUT_CM]=(myMod.dacs)[3]; - v[VCASC_OUT]=(myMod.dacs)[4]; - v[VIN_CM]=(myMod.dacs)[5]; - v[VREF_COMP]=(myMod.dacs)[6]; - v[IB_TESTC]=(myMod.dacs)[7]; - -#ifdef VERBOSE - printf("vrefds=%d\n",v[VREF_DS]); - printf("vcascn=%d\n",v[VCASCN_PB]); - printf("vcascp=%d\n",v[VCASCP_PB]); - printf("vout=%d\n",v[VOUT_CM]); - printf("vcasc=%d\n",v[VCASC_OUT]); - printf("vin=%d\n",v[VIN_CM]); - printf("vref_comp=%d\n",v[VREF_COMP]); - printf("ib_testc=%d\n",v[IB_TESTC]); -#endif - - initDACs(v); - clearDACSregister(); - - if (detectorModules) { - copyModule(detectorModules,&myMod); - } - - //setting the conf gain and the settings register - setSettings(myMod.reg); - - return myMod.reg; -} - - - -void getModule(sls_detector_module* myMod) { - if (detectorModules) { - copyModule(myMod,detectorModules); - } -} - - - -void initDACs(int* v) { -#ifdef VERBOSE - printf("\n..inside initdacs\n"); -#endif - int iaddr; - for (iaddr=0; iaddr<8; iaddr++) { - initDAC(iaddr, v[iaddr]); - } -} - - - -void initDAC(int dac_addr, int value) { -#ifdef VERBOSE - printf("Programming dac %d with value %d\n", dac_addr, value); -#endif - clearDACSregister(); - if (value >= 0) { - program_one_dac(dac_addr,value); - } - nextDAC(); -} - - - - - -void clearDACSregister() { - putout("1111111111111111");//reset - putout("1111111111111110");//cs down -} - - -void nextDAC() { - putout("1111111111111011");//cs up - putout("1111111111111001");//clk down - putout("1111111111111111");//reset -} - - - -void program_one_dac(int addr, int value) { - -#ifdef VERBOSE - printf("programming dac %d value %d module %d\n",addr, value); -#endif - - - int i,idac,bit, control; - int v=value; - - control=32+addr; - value=(value<<4) | (control<< 16); - - - for (i=0;i<24;i++) { - bit=value & (1<<(23-i)); - if (bit) { - putout("1111111111111100");//clk down - putout("1111111111111100");//write data - putout("1111111111111110");//clk up - } - else - { - putout("1111111111111000");//clk down - putout("1111111111111000");//write data - putout("1111111111111010");//clk up - } - } - - idac=addr; - - - if (detectorDacs) { - detectorDacs[idac]=v; -#ifdef VERBOSE - printf("index=%d, val=%d addr=%x\n", idac, v, (unsigned int)(detectorDacs+idac)); -#endif - setDACRegister(idac,v); - } -} - - - - - - - - - - diff --git a/slsDetectorServers/gotthardDetectorServer/firmware_funcs.h b/slsDetectorServers/gotthardDetectorServer/firmware_funcs.h deleted file mode 100755 index 552d2f6ad..000000000 --- a/slsDetectorServers/gotthardDetectorServer/firmware_funcs.h +++ /dev/null @@ -1,120 +0,0 @@ -#ifndef FIRMWARE_FUNCS_H -#define FIRMWARE_FUNCS_H - - -#include "sls_detector_defs.h" - - -#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 initDetector(); -int setDefaultDacs(); -void setMasterSlaveConfiguration(); -int configureADC(); -int setPhaseShiftOnce(); -int setPhaseShift(int numphaseshift); -int cleanFifo(); -int setDAQRegister(); -u_int32_t putout(char *s); -int setConfigurationRegister(int d); -int sendviaUDP(int d); -int setDACRegister(int idac, int val); - - -u_int32_t setExtSignal(enum externalSignalFlag mode); -int getExtSignal(); -u_int32_t setFPGASignal(enum externalSignalFlag mode); -int getFPGASignal(); - -int setTiming(int t); - -u_int64_t getDetectorNumber(); -u_int32_t getFirmwareVersion(); -u_int32_t getFirmwareSVNVersion(); - -u_int32_t testFpga(void); -int testBus(void); - -int initHighVoltage(int val); -int getTemperature(int tempSensor); -int setSettings(int i); -int initConfGain(int isettings,int val); -ROI* setROI(int n, ROI arg[], int *retvalsize, int *ret); -int setADC(int adc); -int configureMAC(int ipad, long long int macad, long long int detectormacadd, int detipad, int ival, int udpport); -int getAdcConfigured(); - - -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 setPeriod(int64_t value); -int64_t getPeriod(); - -int64_t setDelay(int64_t value); -int64_t getDelay(); - -int64_t setTrains(int64_t value); -int64_t getTrains(); - -int64_t getActualTime(); -int64_t getMeasurementTime(); - -u_int32_t fifoReadStatus(); -u_int32_t fifo_full(void); -u_int32_t runBusy(void); -u_int32_t runState(void); -int startStateMachine(); -int stopStateMachine(); -int startReadOut(); -void waitForAcquisitionFinish(); -int getStatus(); - -int loadImage(int index, short int ImageVals[]); -int readCounterBlock(int startACQ, short int CounterVals[]); -int resetCounterBlock(int startACQ); - -int copyModule(sls_detector_module *destMod, sls_detector_module *srcMod); - -int setDAC(int ind,int val,int mV, int retval[]); -int getDAC(int ind); - -int setModule(sls_detector_module); -void getModule(sls_detector_module*); - -void initDACs(int* v); -void initDAC(int dac_addr, int value); -void clearDACSregister(); -void nextDAC(); -void program_one_dac(int addr, int value); - - - - - - -#endif diff --git a/slsDetectorServers/gotthardDetectorServer/gotthardDetectorServer_refactor b/slsDetectorServers/gotthardDetectorServer/gotthardDetectorServer_refactor deleted file mode 100755 index fdd03de64..000000000 Binary files a/slsDetectorServers/gotthardDetectorServer/gotthardDetectorServer_refactor and /dev/null differ diff --git a/slsDetectorServers/gotthardDetectorServer/gotthardDetectorServer_virtual b/slsDetectorServers/gotthardDetectorServer/gotthardDetectorServer_virtual deleted file mode 100755 index 4f6df8112..000000000 Binary files a/slsDetectorServers/gotthardDetectorServer/gotthardDetectorServer_virtual and /dev/null differ diff --git a/slsDetectorServers/gotthardDetectorServer/logger.h b/slsDetectorServers/gotthardDetectorServer/logger.h new file mode 120000 index 000000000..ff1930ce3 --- /dev/null +++ b/slsDetectorServers/gotthardDetectorServer/logger.h @@ -0,0 +1 @@ +../slsDetectorServer/logger.h \ No newline at end of file diff --git a/slsDetectorServers/gotthardDetectorServer/server.c b/slsDetectorServers/gotthardDetectorServer/server.c deleted file mode 100755 index 2c571d748..000000000 --- a/slsDetectorServers/gotthardDetectorServer/server.c +++ /dev/null @@ -1,103 +0,0 @@ -/* 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 "server_funcs.h" -#include -#include - - -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 NDAC 8 -#define NADC 5 -#define NCHANS NCHAN*NCHIP -#define NDACS NDAC -#define NCHIPS_PER_ADC 2 - -#define DYNAMIC_RANGE 16 -#define DATA_BYTES (NCHIP*NCHAN*2) - -// for 25 um -#define CONFIG_FILE "config.txt" - - - -#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) - -#define CLK_FREQ 32.007729 - -#define DAC_DR 1024 -#define CONF_GAIN { \ - 0, /*standard gain*/ \ - 0, /*fast gain*/ \ - 0, /*high gain*/ \ - 8, /*dynamic gain*/ \ - 6, /*low gain*/ \ - 2, /*medium gain*/ \ - 1 /*very high gain*/ \ -}; -//dynamic gain confgain yet to be figured out-probably 8 or 16 - -// DAC definitions -enum dacsVal{VREF_DS, VCASCN_PB, VCASCP_PB, VOUT_CM, VCASC_OUT, VIN_CM, VREF_COMP, IB_TESTC,HIGH_VOLTAGE, CONFGAIN}; -#define DEFAULT_DAC_VALS { \ - 660, /* VREF_DS */ \ - 650, /* VCASCN_PB */ \ - 1480, /* VCASCP_PB */ \ - 1520, /* VOUT_CM */ \ - 1320, /* VCASC_OUT */ \ - 1350, /* VIN_CM */ \ - 350, /* VREF_COMP */ \ - 2001 /* IB_TESTC */ \ -}; - -//Register Definitions for temp,hv,dac gain -enum adcVals{TEMP_FPGA, TEMP_ADC}; - - -#endif diff --git a/slsDetectorServers/gotthardDetectorServer/server_funcs.c b/slsDetectorServers/gotthardDetectorServer/server_funcs.c deleted file mode 100755 index 0fedaffbe..000000000 --- a/slsDetectorServers/gotthardDetectorServer/server_funcs.c +++ /dev/null @@ -1,2185 +0,0 @@ -#include "sls_detector_defs.h" -#include "server_funcs.h" -#include "server_defs.h" -#include "firmware_funcs.h" -#include "registers_g.h" -#include "gitInfoGotthard.h" -#include "AD9257.h" // include "commonServerFunctions.h" -#include "versionAPI.h" - -#define FIFO_DATA_REG_OFF 0x50<<11 -#define CONTROL_REG 0x24<<11 -// Global variables - -int (*flist[256])(int); - -//defined in the detector specific file -#ifdef GOTTHARDD -const enum detectorType myDetectorType=GOTTHARD; -#else -const enum detectorType myDetectorType=GENERIC; -#endif - - -extern int storeInRAM; - -extern int lockStatus; -extern char lastClientIP[INET_ADDRSTRLEN]; -extern char thisClientIP[INET_ADDRSTRLEN]; -extern int differentClients; - -/* global variables for optimized readout */ - -char mess[MAX_STR_LENGTH]; -int digitalTestBit = 0; - - -int init_detector( int b) { - - if (mapCSP0()==FAIL) { - printf("Could not map memory\n"); - exit(-1); - } - - //confirm if it is really gotthard - if (((bus_r(PCB_REV_REG) & DETECTOR_TYPE_MASK)>> DETECTOR_TYPE_OFFSET) == MOENCH_MODULE){ - printf("This is a MOENCH detector. Exiting Gotthard Server.\n\n"); - exit(-1); - } - - if (b) { - printf("***This is a GOTTHARD detector with %d chips per module***\n", NCHIP); - printf("\nBoard Revision:0x%x\n",(bus_r(PCB_REV_REG)&BOARD_REVISION_MASK)); - initDetector(); - printf("Initializing Detector\n"); - } - strcpy(mess,"dummy message"); - strcpy(lastClientIP,"none"); - strcpy(thisClientIP,"none1"); - lockStatus=0; - return OK; -} - - -int decode_function(int file_des) { - int fnum,n; - int retval=FAIL; -#ifdef VERBOSE - printf( "receive data\n"); -#endif - n = receiveDataOnly(file_des,&fnum,sizeof(fnum)); - if (n <= 0) { -#ifdef VERBOSE - printf("ERROR reading from socket %d, %d %d\n", n, fnum, file_des); -#endif - return FAIL; - } -#ifdef VERBOSE - else - printf("size of data received %d\n",n); -#endif - -#ifdef VERBOSE - printf( "calling function fnum = %d %x\n",fnum,(unsigned int)(flist[fnum])); -#endif - if (fnum<0 || fnum>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_EXEC_COMMAND] = &exec_command; - flist[F_GET_DETECTOR_TYPE] = &get_detector_type; - 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_SET_DAC] = &set_dac; - flist[F_GET_ADC] = &get_adc; - flist[F_WRITE_REGISTER] = &write_register; - flist[F_READ_REGISTER] = &read_register; - flist[F_SET_MODULE] = &set_module; - flist[F_GET_MODULE] = &get_module; - flist[F_SET_SETTINGS] = &set_settings; - flist[F_GET_THRESHOLD_ENERGY] = &M_nofunc; - 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_START_AND_READ_ALL] = &start_and_read_all; - flist[F_READ_ALL] = &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_READOUT_FLAGS] = &set_readout_flags; - flist[F_SET_ROI] = &set_roi; - flist[F_SET_SPEED] = &set_speed; - flist[F_EXIT_SERVER] = &exit_server; - flist[F_LOCK_SERVER] = &lock_server; - flist[F_GET_LAST_CLIENT_IP] = &get_last_client_ip; - flist[F_SET_PORT] = &set_port; - flist[F_UPDATE_CLIENT] = &update_client; - flist[F_CONFIGURE_MAC] = &configure_mac; - flist[F_LOAD_IMAGE] = &load_image; - flist[F_READ_COUNTER_BLOCK] = &read_counter_block; - flist[F_RESET_COUNTER_BLOCK] = &reset_counter_block; - flist[F_CALIBRATE_PEDESTAL] = &M_nofunc; - flist[F_ENABLE_TEN_GIGA] = &M_nofunc; - flist[F_SET_ALL_TRIMBITS] = &M_nofunc; - flist[F_SET_CTB_PATTERN] = &M_nofunc; - flist[F_WRITE_ADC_REG] = &write_adc_register; - flist[F_SET_COUNTER_BIT] = &M_nofunc; - flist[F_PULSE_PIXEL] = &M_nofunc; - flist[F_PULSE_PIXEL_AND_MOVE] = &M_nofunc; - flist[F_PULSE_CHIP] = &M_nofunc; - flist[F_SET_RATE_CORRECT] = &M_nofunc; - flist[F_GET_RATE_CORRECT] = &M_nofunc; - flist[F_SET_NETWORK_PARAMETER] = &M_nofunc; - flist[F_PROGRAM_FPGA] = &M_nofunc; - flist[F_RESET_FPGA] = &M_nofunc; - flist[F_POWER_CHIP] = &M_nofunc; - flist[F_ACTIVATE] = &M_nofunc; - flist[F_PREPARE_ACQUISITION] = &M_nofunc; - flist[F_THRESHOLD_TEMP] = &M_nofunc; - flist[F_TEMP_CONTROL] = &M_nofunc; - flist[F_TEMP_EVENT] = &M_nofunc; - flist[F_AUTO_COMP_DISABLE] = &M_nofunc; - flist[F_STORAGE_CELL_START] = &M_nofunc; - flist[F_CHECK_VERSION] = &check_version; - flist[F_SOFTWARE_TRIGGER] = &M_nofunc; - return OK; -} - - -int M_nofunc(int file_des){ - - int ret=FAIL; - int n = 1; - while (n > 0) - n = receiveData(file_des,mess,MAX_STR_LENGTH,OTHER); - - sprintf(mess,"Unrecognized Function. Please do not proceed.\n"); - cprintf(BG_RED,"Error: %s",mess); - - sendDataOnly(file_des,&ret,sizeof(ret)); - 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 retval; - int ret = OK; - - /* execute action */ - retval=myDetectorType; - -#ifdef VERBOSE - printf("Returning detector type %d\n",ret); -#endif - - if (differentClients==1) - ret=FORCE_UPDATE; - - /* send OK/failed */ - n += sendDataOnly(file_des,&ret,sizeof(ret)); - /* send return argument */ - n += sendDataOnly(file_des,&retval,sizeof(retval)); - - /*return ok/fail*/ - return retval; - - -} - - - -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 (lockStatus && differentClients) { - ret=FAIL; - sprintf(mess,"Detector locked by %s\n", lastClientIP); - } else if (signalindex > 0) { - ret=FAIL; - sprintf(mess,"Signal index %d is reserved. Only index 0 can be configured.\n", signalindex); - } else { - retval=setExtSignal(flag); - } - } -#ifdef VERBOSE - printf("Setting external signal %d to flag %d\n",signalindex,flag ); - printf("Set to flag %d\n",retval); -#endif - - } - - 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; - } - - if (retval==OK) { - /* execute action */ - - ret=setTiming(arg); - - -#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 = (getFirmwareVersion() & 0xFFFFFF); - break; - case DETECTOR_SOFTWARE_VERSION: - retval = (GITDATE & 0xFFFFFF); - break; - case CLIENT_SOFTWARE_API_VERSION: - return APIGOTTHARD; - 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 n=0; - 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 DETECTOR_FIRMWARE_TEST: - retval=testFpga(); - break; - case DETECTOR_BUS_TEST: - retval=testBus(); - break; - case DIGITAL_BIT_TEST: - n = receiveDataOnly(file_des,&ival,sizeof(ival)); - if (n < 0) { - sprintf(mess,"Error reading from socket\n"); - retval=FAIL; - } -#ifdef VERBOSE - printf("with value %d\n", ival); -#endif - if (differentClients==1 && lockStatus==1) { - ret=FAIL; - sprintf(mess,"Detector locked by %s\n",lastClientIP); - break; - } - digitalTestBit = ival; - retval=digitalTestBit; - break; - default: - printf("Unknown digital test required %d\n",arg); - ret=FAIL; - retval=FAIL; - break; - } - -#ifdef VERBOSE - printf("digital test result is 0x%x\n", retval); -#endif - //Always returns force update such that the dynamic range is always updated on the client - - // 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 set_dac(int file_des) { - //default:all mods - int retval[2];retval[1]=-1; - int temp; - int ret=OK; - int arg[2]; - enum dacIndex ind; - int n; - int val; - int mV; - int idac=0; - - sprintf(mess,"Can't set DAC\n"); - - n = receiveDataOnly(file_des,arg,sizeof(arg)); - if (n < 0) { - sprintf(mess,"Error reading from socket\n"); - ret=FAIL; - } - ind=arg[0]; - mV=arg[1]; - - n = receiveDataOnly(file_des,&val,sizeof(val)); - if (n < 0) { - sprintf(mess,"Error reading from socket\n"); - ret=FAIL; - } - -#ifdef VERBOSE - printf("Setting DAC %d to %d V\n", ind, val); -#endif - - 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[0]=initHighVoltage(val); - ret=FAIL; - if(retval[0]==-2) - strcpy(mess,"Invalid Voltage.Valid values are 0,90,110,120,150,180,200"); - else if(retval[0]==-3) - strcpy(mess,"Weird value read back or it has not been set yet\n"); - else - ret=OK; - }else{ - setDAC(idac,val,mV,retval); - ret=FAIL; - if(mV) - temp = retval[1]; - else - temp = retval[0]; - if ((abs(temp-val)<=3) || val==-1) { - ret=OK; -#ifdef VERBOSE - printf("DAC set to %d in dac units and %d mV\n", retval[0],retval[1]); -#endif - } - } - } - } - - - if(ret==FAIL) - printf("Setting dac %d: wrote %d but read %d\n", ind, val, temp); - 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; - enum dacIndex ind; - 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; - -#ifdef VERBOSE - printf("Getting ADC %d\n", ind); -#endif - - 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=getTemperature(idac); - -#ifdef VERBOSE - printf("ADC is %d V\n", retval); -#endif - if (ret==FAIL) { - printf("Getting adc %d failed\n", ind); - } - - 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 write_register(int file_des) { - - int retval; - int ret=OK; - int arg[2]; - int addr, val; - int n; - u_int32_t address; - - sprintf(mess,"Can't write to register\n"); - - n = receiveDataOnly(file_des,arg,sizeof(arg)); - if (n < 0) { - sprintf(mess,"Error reading from socket\n"); - ret=FAIL; - } - addr=arg[0]; - val=arg[1]; - -#ifdef VERBOSE - printf("writing to register 0x%x data 0x%x\n", addr, val); -#endif - - if (differentClients==1 && lockStatus==1) { - ret=FAIL; - sprintf(mess,"Detector locked by %s\n",lastClientIP); - } - - - if(ret!=FAIL){ - address=(addr<<11); - if((address==FIFO_DATA_REG_OFF)||(address==CONTROL_REG)) - ret = bus_w16(address,val); - else - ret=bus_w(address,val); - if(ret==OK){ - if((address==FIFO_DATA_REG_OFF)||(address==CONTROL_REG)) - retval=bus_r16(address); - else - retval=bus_r(address); - } - } - - -#ifdef VERBOSE - printf("Data set to 0x%x\n", retval); -#endif - if (retval==val) { - ret=OK; - if (differentClients) - ret=FORCE_UPDATE; - } else { - ret=FAIL; - sprintf(mess,"Writing to register 0x%x failed: wrote 0x%x but read 0x%x\n", addr, val, retval); - } - - /* 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 read_register(int file_des) { - - int retval; - int ret=OK; - int arg; - int addr; - int n; - u_int32_t address; - - sprintf(mess,"Can't read register\n"); - - n = receiveDataOnly(file_des,&arg,sizeof(arg)); - if (n < 0) { - sprintf(mess,"Error reading from socket\n"); - ret=FAIL; - } - addr=arg; - - - - //#ifdef VERBOSE - printf("reading register 0x%x\n", addr); - //#endif - - if(ret!=FAIL){ - address=(addr<<11); - if((address==FIFO_DATA_REG_OFF)||(address==CONTROL_REG)) - retval=bus_r16(address); - else - retval=bus_r(address); - } - - - -#ifdef VERBOSE - printf("Returned value 0x%x\n", retval); -#endif - if (ret==FAIL) { - ret=FAIL; - printf("Reading register 0x%x failed\n", addr); - } 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 set_module(int file_des) { - sls_detector_module myModule; - int *myChip=malloc(NCHIP*sizeof(int)); - int *myChan=malloc(NCHIP*NCHAN*sizeof(int)); - int *myDac=malloc(NDAC*sizeof(int));/**dhanya*/ - int *myAdc=malloc(NADC*sizeof(int));/**dhanya*/ - int retval, n; - int ret=OK; - int dr;// ow; - - dr=DYNAMIC_RANGE; - - 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; - - -#ifdef VERBOSE - printf("Setting module\n"); -#endif - ret=receiveModule(file_des, &myModule); - - if (ret>=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 (differentClients==1 && lockStatus==1) { - ret=FAIL; - sprintf(mess,"Detector locked by %s\n",lastClientIP); - } else { - retval=setModule(myModule); - } - } - - 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 n; - - - - sls_detector_module myModule; - int *myChip=malloc(NCHIP*sizeof(int)); - int *myChan=malloc(NCHIP*NCHAN*sizeof(int)); - int *myDac=malloc(NDAC*sizeof(int)); - int *myAdc=malloc(NADC*sizeof(int)); - - - 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; - - if (ret==OK) { - getModule(&myModule); - -#ifdef VERBOSE - printf("Returning module register %x\n", myModule.reg); -#endif - - } - - 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 */ - ret=sendModule(file_des, &myModule); - } else { - n += sendDataOnly(file_des,mess,sizeof(mess)); - } - - - - free(myChip); - free(myChan); - free(myDac); - free(myAdc); - - - /*return ok/fail*/ - return ret; - -} - - - -int set_settings(int file_des) { - - int retval; - int ret=OK; - int arg; - int n; - enum detectorSettings isett; - - - n = receiveDataOnly(file_des,&arg,sizeof(arg)); - if (n < 0) { - sprintf(mess,"Error reading from socket\n"); - ret=FAIL; - } - isett=arg; - - -#ifdef VERBOSE - printf("Changing settings to %d\n", isett); -#endif - - if (differentClients==1 && lockStatus==1 && arg!=GET_SETTINGS) { - ret=FAIL; - sprintf(mess,"Detector locked by %s\n",lastClientIP); - } else { - switch(isett) { - case GET_SETTINGS: - case UNINITIALIZED: - case DYNAMICGAIN: - case HIGHGAIN: - case LOWGAIN: - case MEDIUMGAIN: - case VERYHIGHGAIN: - break; - default: - ret = FAIL; - sprintf(mess,"Setting (%d) is not implemented for this detector.\n" - "Options are dynamicgain, highgain, lowgain, mediumgain and " - "veryhighgain.\n", isett); - cprintf(RED, "Warning: %s", mess); - break; - } - if (ret != FAIL) { - retval=setSettings(isett); -#ifdef VERBOSE - printf("Settings changed to %d\n",retval); -#endif - if (retval != isett && isett >= 0) { - ret=FAIL; - sprintf(mess, "Changing settings: wrote %d but read %d\n", isett, retval); - printf("Warning: %s",mess); - } - - else { - ret = setDefaultDacs(); - if (ret == FAIL) { - strcpy(mess,"Could change settings, but could not set to default dacs\n"); - cprintf(RED, "Warning: %s", mess); - } - } - } - } - if (ret==OK && differentClients==1) - ret=FORCE_UPDATE; - - /* send answer */ - 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 start_acquisition(int file_des) { - - int ret=OK; - int n; - - - sprintf(mess,"can't start acquisition\n"); - -#ifdef VERBOSE - printf("Starting acquisition\n"); -#endif - - if (differentClients==1 && lockStatus==1) { - ret=FAIL; - sprintf(mess,"Detector locked by %s\n",lastClientIP); - } else { - ret=startStateMachine(); - } - if (ret==FAIL) - sprintf(mess,"Start acquisition failed\n"); - else if (differentClients) - ret=FORCE_UPDATE; - - n = sendDataOnly(file_des,&ret,sizeof(ret)); - if (ret==FAIL) { - n += sendDataOnly(file_des,mess,sizeof(mess)); - } - return ret; - -} - -int stop_acquisition(int file_des) { - - int ret=OK; - int n; - - sprintf(mess,"can't stop acquisition\n"); - - cprintf(BG_RED,"Client command received to stop acquisition\n"); - - - - if (differentClients==1 && lockStatus==1) { - ret=FAIL; - sprintf(mess,"Detector locked by %s\n",lastClientIP); - } else { - ret=stopStateMachine(); - } - - if (ret==FAIL) - sprintf(mess,"Stop acquisition failed\n"); - else if (differentClients) - ret=FORCE_UPDATE; - - n = sendDataOnly(file_des,&ret,sizeof(ret)); - if (ret==FAIL) { - n += sendDataOnly(file_des,mess,sizeof(mess)); - } - return ret; - - -} - -int start_readout(int file_des) { - - - int ret=OK; - int n; - - - sprintf(mess,"can't start readout\n"); - -#ifdef VERBOSE - printf("Starting readout\n"); -#endif - if (differentClients==1 && lockStatus==1) { - ret=FAIL; - sprintf(mess,"Detector locked by %s\n",lastClientIP); - } else { - ret=startReadOut(); - } - if (ret==FAIL) - sprintf(mess,"Start readout failed\n"); - else if (differentClients) - ret=FORCE_UPDATE; - - n = sendDataOnly(file_des,&ret,sizeof(ret)); - if (ret==FAIL) { - n += sendDataOnly(file_des,mess,sizeof(mess)); - } - return ret; - - - -} - -int get_run_status(int file_des) { - - int ret=OK; - int n; - - - enum runStatus s; - sprintf(mess,"getting run status\n"); - -#ifdef VERBOSE - printf("Getting status\n"); -#endif - - int retval = getStatus(); - - if (ret!=OK) { - printf("get status failed %04x\n",retval); - sprintf(mess, "get status failed %08x\n", retval); - - } 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,&s,sizeof(s)); - } - return ret; - - - -} - - - - - -int start_and_read_all(int file_des) { -#ifdef VERBOSE - printf("Starting and reading all frames\n"); -#endif - int dataret = FAIL; - 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(); - dataret = read_all(file_des); -#ifdef VERBOSE - printf("Frames finished\n"); -#endif - - return dataret; - - -} - - - -int read_all(int file_des) { - int dataret = FAIL; - strcpy(mess,"wait for read frame failed\n"); - - if (differentClients==1 && lockStatus==1) { - dataret=FAIL; - sprintf(mess,"Detector locked by %s\n",lastClientIP); - cprintf(RED,"%s\n",mess); - sendDataOnly(file_des,&dataret,sizeof(dataret)); - sendDataOnly(file_des,mess,sizeof(mess)); - return dataret; - } - - -#ifdef VIRTUAL - dataret = FINISHED; - strcpy(mess,"acquisition successfully finished\n"); -#else - waitForAcquisitionFinish(); - - // set return value and message - if(getFrames()>-2) { - dataret = FAIL; - sprintf(mess,"no data and run stopped: %d frames left\n",(int)(getFrames()+2)); - cprintf(RED,"%s\n",mess); - } else { - dataret = FINISHED; - sprintf(mess,"acquisition successfully finished\n"); - cprintf(GREEN,"%s",mess); - - } -#endif - - if (differentClients) - dataret=FORCE_UPDATE; - - sendDataOnly(file_des,&dataret,sizeof(dataret)); - sendDataOnly(file_des,mess,sizeof(mess)); - return dataret; -} - - - - - -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 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"); - } - - 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 CYCLES_NUMBER: - retval=getTrains(); - 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=DYNAMIC_RANGE; - } - - //if (dr>=0 && retval!=dr) ret=FAIL; - if (ret!=OK) { - sprintf(mess,"set dynamic range failed\n"); - } - - 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_readout_flags(int file_des) { - - enum readOutFlags arg; - int ret=FAIL; - - - receiveDataOnly(file_des,&arg,sizeof(arg)); - -#ifdef PROPIXD - sprintf(mess,"can't set readout flags for propix\n"); -#else - sprintf(mess,"can't set readout flags for gotthard\n"); -#endif - - sendDataOnly(file_des,&ret,sizeof(ret)); - sendDataOnly(file_des,mess,sizeof(mess)); - - 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; - } - -#ifdef PROPIXD - sprintf(mess,"can't set roi for propix\n"); - ret = FAIL; -#endif - if(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("\n\nSetting ROI: nroi=%d\n",nroi); - 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 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 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 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)); - int k = DYNAMIC_RANGE; - n = sendDataOnly(file_des,&k,sizeof(k)); - k = DATA_BYTES; - n = sendDataOnly(file_des,&k,sizeof(k)); - t=setSettings(GET_SETTINGS); - n = sendDataOnly(file_des,&t,sizeof(t)); - 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=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[6][50]; - int n; - - 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); - //arg[5] is udpport2 for eiger -#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 - - - //#ifdef VERBOSE - printf("Configuring MAC at port %x\n", udpport); - //#endif - 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(); - } - if (ret==FAIL) - printf("configuring MAC failed\n"); - else - printf("Configuremac successful and adc %d\n",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,DATA_BYTES); - if (n < 0) { - sprintf(mess,"Error reading from socket\n"); - ret=FAIL; - } - -#ifdef PROPIXD - sprintf(mess,"can't load image for propix\n"); - ret = FAIL; -#endif - - 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 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; - } - - -#ifdef PROPIXD - sprintf(mess,"can't read counter block for propix\n"); - ret = FAIL; -#endif - - 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,DATA_BYTES);//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 write_adc_register(int file_des) { - int ret=OK; - int n=0; - int retval=-1; - sprintf(mess,"write to adc register failed\n"); - - // receive arguments - int arg[2]={-1,-1}; - n = receiveData(file_des,arg,sizeof(arg),INT32); - if (n < 0) { - sprintf(mess,"Error reading from socket\n"); - ret=FAIL; - } - - int addr=arg[0]; - int val=arg[1]; - - // execute action - if (ret == OK) { - if (differentClients && lockStatus) { - ret = FAIL; - sprintf(mess,"Detector locked by %s\n",lastClientIP); - cprintf(RED, "Warning: %s", mess); - } - else { -#ifdef VERBOSE - printf("writing to register 0x%x data 0x%x\n", addr, val); -#endif - setAdc(addr,val); -#ifdef VERBOSE - printf("Data set to 0x%x\n", retval); -#endif - if (ret==OK && differentClients) - ret=FORCE_UPDATE; - } - } - // send ok / fail - n = sendDataOnly(file_des,&ret,sizeof(ret)); - // send return argument - if (ret==FAIL) { - n = sendDataOnly(file_des,mess,sizeof(mess)); - } else - n = sendDataOnly(file_des,&retval,sizeof(retval)); - - // return ok / fail - return ret; -} - - - - -int check_version(int file_des) { - int ret=OK,ret1=OK; - int n=0; - sprintf(mess,"check version failed\n"); - - - - // receive arguments - int64_t arg=-1; - n = receiveData(file_des,&arg,sizeof(arg),INT64); - if (n < 0) { - sprintf(mess,"Error reading from socket\n"); - ret=FAIL; - } - - - // execute action - if (ret == OK) { -#ifdef VERBOSE - printf("Checking versioning compatibility with value %d\n",arg); -#endif - int64_t client_requiredVersion = arg; - int64_t det_apiVersion = APIGOTTHARD; - int64_t det_version = (GITDATE & 0xFFFFFF); - - // old client - if (det_apiVersion > client_requiredVersion) { - ret = FAIL; - sprintf(mess,"Client's detector SW API version: (0x%llx). " - "Detector's SW API Version: (0x%llx). " - "Incompatible, update client!\n", - client_requiredVersion, det_apiVersion); - cprintf(RED, "Warning: %s", mess); - } - - // old software - else if (client_requiredVersion > det_version) { - ret = FAIL; - sprintf(mess,"Detector SW Version: (0x%llx). " - "Client's detector SW API Version: (0x%llx). " - "Incompatible, update detector software!\n", - det_version, client_requiredVersion); - cprintf(RED, "Warning: %s", mess); - } - } - - - - // ret could be swapped during sendData - ret1 = ret; - // send ok / fail - n = sendData(file_des,&ret1,sizeof(ret),INT32); - // send return argument - if (ret==FAIL) { - n += sendData(file_des,mess,sizeof(mess),OTHER); - } - - // return ok / fail - return ret; -} - - diff --git a/slsDetectorServers/gotthardDetectorServer/server_funcs.h b/slsDetectorServers/gotthardDetectorServer/server_funcs.h deleted file mode 100755 index 5c1571241..000000000 --- a/slsDetectorServers/gotthardDetectorServer/server_funcs.h +++ /dev/null @@ -1,70 +0,0 @@ -#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); - - - - - -// General purpose functions -int exec_command(int); -int get_detector_type(int); -int set_external_signal_flag(int); -int set_external_communication_mode(int); -int get_id(int); -int digital_test(int); -int set_dac(int); -int get_adc(int); -int write_register(int); -int read_register(int); -int set_module(int); -int get_module(int); -int set_settings(int); -int start_acquisition(int); -int stop_acquisition(int); -int start_readout(int); -int get_run_status(int); -int start_and_read_all(int); -int read_all(int); -int set_timer(int); -int get_time_left(int); -int set_dynamic_range(int); -int set_readout_flags(int); -int set_roi(int); -int set_speed(int); -int exit_server(int); -int lock_server(int); -int get_last_client_ip(int); -int set_port(int); -int send_update(int); -int update_client(int); -int configure_mac(int); -int load_image(int); -int read_counter_block(int); -int reset_counter_block(int); -int write_adc_register(int); -int check_version(int); -#endif diff --git a/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c new file mode 100644 index 000000000..d23cac83b --- /dev/null +++ b/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c @@ -0,0 +1,1810 @@ +#include "slsDetectorFunctionList.h" +#include "gitInfoGotthard.h" +#include "versionAPI.h" +#include "logger.h" + +#ifndef VIRTUAL +#include "AD9257.h" // commonServerFunctions.h, blackfin.h, ansi.h +#else +#include "blackfin.h" +#include +#include +#endif + +// Variables that will be exported +int phaseShift = DEFAULT_PHASE_SHIFT; + +int firmware_compatibility = OK; +int firmware_check_done = 0; +char firmware_message[MAX_STR_LENGTH]; + +#ifdef VIRTUAL +pthread_t pthread_virtual_tid; +int virtual_status = 0; +int virtual_stop = 0; +int highvoltage = 0; +#endif + +int detectorFirstServer = 1; +int dacValues[NDAC] = {0}; +enum detectorSettings thisSettings = UNITIALIZED; +enum externalSignalFlag signalMode = 0; +int digitalTestBit = 0; + +// roi configuration +int adcConfigured = -1; +ROI rois[MAX_ROIS]; +int nROI = 0; +int ipPacketSize = 0; +int udpPacketSize = 0; + +// master slave configuration (for 25um) +int masterflags = NO_MASTER; +int masterdefaultdelay = 62; +int patternphase = 0; +int adcphase = 0; +int slavepatternphase = 0; +int slaveadcphase = 0; +int rsttosw1delay = 2; +int startacqdelay = 1; + +int isFirmwareCheckDone() { + return firmware_check_done; +} + +int getFirmwareCheckResult(char** mess) { + *mess = firmware_message; + return firmware_compatibility; +} + +void basictests() { + firmware_compatibility = OK; + firmware_check_done = 0; + memset(firmware_message, 0, MAX_STR_LENGTH); +#ifdef VIRTUAL + FILE_LOG(logINFOBLUE, ("******** Gotthard Virtual Server *****************\n")); + if (mapCSP0() == FAIL) { + strcpy(firmware_message, + "Could not map to memory. Dangerous to continue.\n"); + FILE_LOG(logERROR, (firmware_message)); + firmware_compatibility = FAIL; + firmware_check_done = 1; + return; + } + firmware_check_done = 1; + return; +#else + if (mapCSP0() == FAIL) { + strcpy(firmware_message, + "Could not map to memory. Dangerous to continue.\n"); + FILE_LOG(logERROR, ("%s\n\n", firmware_message)); + firmware_compatibility = FAIL; + firmware_check_done = 1; + return; + } + + // does check only if flag is 0 (by default), set by command line + if (((checkType() == FAIL) || (testFpga() == FAIL) || (testBus() == FAIL))) { + strcpy(firmware_message, + "Could not pass basic tests of FPGA and bus. Dangerous to continue.\n"); + FILE_LOG(logERROR, ("%s\n\n", firmware_message)); + firmware_compatibility = FAIL; + firmware_check_done = 1; + return; + } + + uint32_t boardrev = getBoardRevision(); + uint32_t ipadd = getDetectorIP(); + uint64_t macadd = getDetectorMAC(); + int64_t fwversion = getDetectorId(DETECTOR_FIRMWARE_VERSION); + int64_t swversion = getDetectorId(DETECTOR_SOFTWARE_VERSION); + int64_t client_sw_apiversion = getDetectorId(CLIENT_SOFTWARE_API_VERSION); + + FILE_LOG(logINFOBLUE, ("************ Gotthard Server *********************\n" + "Board Revision:\t\t 0x%x\n" + + "Detector IP Addr:\t\t 0x%x\n" + "Detector MAC Addr:\t\t 0x%llx\n\n" + + "Firmware Version:\t\t 0x%llx\n" + "Software Version:\t\t 0x%llx\n" + "F/w-S/w API Version:\t\t 0x%llx\n" + "Required Firmware Version:\t 0x%x\n" + "Client-Software API Version:\t 0x%llx\n" + "********************************************************\n", + boardrev, + ipadd, + (long long unsigned int)macadd, + (long long int)fwversion, + (long long int)swversion, + (long long int)client_sw_apiversion + )); + + FILE_LOG(logINFO, ("Basic Tests - success\n")); + firmware_check_done = 1; +#endif +} + +int checkType() { +#ifdef VIRTUAL + return OK; +#endif + volatile u_int32_t type = ((bus_r(BOARD_REVISION_REG) & DETECTOR_TYPE_MASK) >> DETECTOR_TYPE_OFFSET); + if (type == DETECTOR_TYPE_MOENCH_VAL){ + FILE_LOG(logERROR, ("This is not a Gotthard Server (read %d, expected ?)\n", type)); + return FAIL; + } + return OK; +} + +u_int32_t testFpga(void) { +#ifdef VIRTUAL + return OK; +#endif + FILE_LOG(logINFO, ("Testing FPGA:\n")); + + //fixed pattern + int ret = OK; + u_int32_t val = bus_r(FIX_PATT_REG); + if (val == FIX_PATT_VAL) { + FILE_LOG(logINFO, ("Fixed pattern: successful match 0x%08x\n",val)); + } else { + FILE_LOG(logERROR, ("Fixed pattern does not match! Read 0x%08x, expected 0x%08x\n", val, FIX_PATT_VAL)); + ret = FAIL; + } + + if (ret == OK) { + // dummy reg + FILE_LOG(logINFO, ("\tTesting Dummy Register:\n")); + u_int32_t addr = DUMMY_REG; + volatile u_int32_t val = 0, readval = 0; + int times = 1000 * 1000; + int i = 0; + for (i = 0; i < times; ++i) { + val = 0x5A5A5A5A - i; + bus_w(addr, val); + readval = bus_r(addr); + if (readval != val) { + FILE_LOG(logERROR, ("Mismatch! Loop(%d): Wrote 0x%x, read 0x%x\n", + i, val, readval)); + ret = FAIL; + break; + } + val = (i + (i << 10) + (i << 20)); + bus_w(addr, val); + readval = bus_r(addr); + if (readval != val) { + FILE_LOG(logERROR, ("Mismatch! Loop(%d): Wrote 0x%x, read 0x%x\n", + i, val, readval)); + ret = FAIL; + break; + } + val = 0x0F0F0F0F; + bus_w(addr, val); + readval = bus_r(addr); + if (readval != val) { + FILE_LOG(logERROR, ("Mismatch! Loop(%d): Wrote 0x%x, read 0x%x\n", + i, val, readval)); + ret = FAIL; + break; + } + val = 0xF0F0F0F0; + bus_w(addr, val); + if (readval != val) { + FILE_LOG(logERROR, ("Mismatch! Loop(%d): Wrote 0x%x, read 0x%x\n", + i, val, readval)); + ret = FAIL; + break; + } + } + bus_w(addr, 0); + if (ret == OK) { + FILE_LOG(logINFO, ("Successfully tested FPGA Dummy Register %d times\n", times)); + } + } + + return ret; +} + +int testBus() { +#ifdef VIRTUAL + return OK; +#endif + FILE_LOG(logINFO, ("Testing Bus:\n")); + + int ret = OK; + u_int32_t addr = DUMMY_REG; + volatile u_int32_t val = 0, readval = 0; + int times = 1000 * 1000; + int i = 0; + + for (i = 0; i < times; ++i) { + val += 0xbbbbb; + bus_w(addr, val); + readval = bus_r(addr); + if (readval != val) { + FILE_LOG(logERROR, ("Mismatch! Loop(%d): Wrote 0x%x, read 0x%x\n", + i, val, readval)); + ret = FAIL; + } + } + + bus_w(addr, 0); + + if (ret == OK) { + FILE_LOG(logINFO, ("Successfully tested bus %d times\n", times)); + } + return ret; +} + +int detectorTest( enum digitalTestMode arg, int ival) { +#ifdef VIRTUAL + return OK; +#endif + switch(arg){ + case DIGITAL_BIT_TEST: + if (ival > -1) { + digitalTestBit = (ival == 0) ? 0 : 1; + FILE_LOG(logINFO, ("Digital Test bit set: %d\n", digitalTestBit)); + } + return digitalTestBit; + case DETECTOR_FIRMWARE_TEST: return testFpga(); + case DETECTOR_BUS_TEST: return testBus(); + default: + FILE_LOG(logERROR, ("Test not implemented for this detector %d\n", (int)arg)); + break; + } + return OK; +} + + +/* Ids */ + +int64_t getDetectorId(enum idMode arg){ + int64_t retval = -1; + + switch(arg){ + case DETECTOR_SERIAL_NUMBER: + return getDetectorNumber(); + case DETECTOR_FIRMWARE_VERSION: + return getFirmwareVersion(); + case DETECTOR_SOFTWARE_VERSION: + return (GITDATE & 0xFFFFFF); + case CLIENT_SOFTWARE_API_VERSION: + return APIGOTTHARD; + default: + return retval; + } +} + +u_int64_t getFirmwareVersion() { +#ifdef VIRTUAL + return 0; +#endif + return ((bus_r(FPGA_VERSION_REG) & FPGA_VERSION_MSK) >> FPGA_VERSION_OFST); +} + +u_int32_t getDetectorNumber(){ +#ifdef VIRTUAL + return 0; +#endif + return 0; +} + +u_int64_t getDetectorMAC() { +#ifdef VIRTUAL + return 0; +#else + 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; +#endif +} + +u_int32_t getDetectorIP(){ +#ifdef VIRTUAL + return 0; +#endif + char temp[50]=""; + u_int32_t res=0; + //execute and get address + char output[255]; + FILE* sysFile = popen("ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2", "r"); + fgets(output, sizeof(output), sysFile); + pclose(sysFile); + + //converting IPaddress to hex. + char* pcword = strtok (output,"."); + while (pcword != NULL) { + sprintf(output,"%02x",atoi(pcword)); + strcat(temp,output); + pcword = strtok (NULL, "."); + } + strcpy(output,temp); + sscanf(output, "%x", &res); + //FILE_LOG(logINFO, ("ip:%x\n",res); + + return res; +} + +u_int32_t getBoardRevision() { +#ifdef VIRTUAL + return 0; +#endif + return ((bus_r(BOARD_REVISION_REG) & BOARD_REVISION_MASK) >> BOARD_REVISION_OFST); +} + + +/* initialization */ + +void initControlServer(){ + setupDetector(); +} + +void initStopServer() { + if (mapCSP0() == FAIL) { + FILE_LOG(logERROR, ("Stop Server: Map Fail. Dangerous to continue. Goodbye!\n")); + exit(EXIT_FAILURE); + } +} + + +/* set up detector */ + +void setupDetector() { + FILE_LOG(logINFO, ("This Server is for 1 Gotthard module (1280 channels)\n")); + + // Initialization + setPhaseShiftOnce(); + prepareADC(); /* TODO: check if need to replace with configureADC from firmwarE_funcs.c? */ + setROIADC(-1); // set adcsyncreg, daqreg, chipofinterestreg, cleanfifos, + setGbitReadout(); + //initDac(0); /*FIXME: if it doesnt work, switch to the old dac*/ + + // master, slave (25um) + setMasterSlaveConfiguration(); + + // Default Parameters + FILE_LOG(logINFOBLUE, ("Setting Default parameters\n")); + setDefaultDacs(); + setSettings(DEFAULT_SETTINGS); + setExtSignal(DEFAULT_TRIGGER_MODE); + setTiming(DEFAULT_TIMING_MODE); + setTimer(FRAME_NUMBER, DEFAULT_NUM_FRAMES); + setTimer(CYCLES_NUMBER, DEFAULT_NUM_CYCLES); + setTimer(ACQUISITION_TIME, DEFAULT_EXPTIME); + setTimer(FRAME_PERIOD, DEFAULT_PERIOD); + setTimer(DELAY_AFTER_TRIGGER, DEFAULT_DELAY); + setHighVoltage(DEFAULT_HIGH_VOLTAGE); +} + +int setDefaultDacs() { + int ret = OK; + FILE_LOG(logINFOBLUE, ("Setting Default Dac values\n")); + { + int i = 0; + int retval[2]={-1,-1}; + const int defaultvals[NDAC] = DEFAULT_DAC_VALS; + for(i = 0; i < NDAC; ++i) { + // if not already default, set it to default + if (dacValues[i] != defaultvals[i]) { + setDAC((enum DACINDEX)i,defaultvals[i],0,retval); + if (retval[0] != defaultvals[i]) { + FILE_LOG(logERROR, ("Setting dac %d failed, wrote %d, read %d\n",i ,defaultvals[i], retval[0])); + ret = FAIL; + } + } + } + } + return ret; +} + +uint32_t writeRegister16And32(uint32_t offset, uint32_t data) { + u_int32_t addr = offset << MEM_MAP_SHIFT; + if ((addr == CONTROL_REG) || (addr == FIFO_DATA_REG)) { + bus_w16(addr, (u_int16_t)data); + return (u_int32_t)bus_r16(addr); + } else + return writeRegister(offset, data); + +} + +uint32_t readRegister16And32(uint32_t offset) { + u_int32_t addr = offset << MEM_MAP_SHIFT; + if ((addr == CONTROL_REG) || (addr == FIFO_DATA_REG)) { + return (u_int32_t)bus_r16(addr); + } else + readRegister(offset); +} + +/* firmware functions (resets) */ + +void setPhaseShiftOnce() { + u_int32_t addr = MULTI_PURPOSE_REG; + volatile u_int32_t val = bus_r(addr); + FILE_LOG(logDEBUG1, ("Multipurpose reg: 0x%x\n", val)); + + // first time detector has switched on + if (!val) { + detectorFirstServer = 1; + FILE_LOG(logINFO, ("Implementing the first phase shift of %d\n", phaseShift)); + int times = 0; + for (times = 1; times < phaseShift; ++times) { + bus_w(addr,(INT_RSTN_MSK | ENT_RSTN_MSK | SW1_MSK | PHS_STP_MSK)); //0x1821 + bus_w(addr,(INT_RSTN_MSK | ENT_RSTN_MSK | (SW1_MSK &~ PHS_STP_MSK))); //0x1820 + } + FILE_LOG(logDEBUG1, ("Multipurpose reg: 0x%x\n", val)); + } else + detectorFirstServer = 0; +} + +void setPhaseShift(int numphaseshift) { + FILE_LOG(logINFO, ("Implementing phase shift of %d\n", numphaseshift)); + u_int32_t addr = MULTI_PURPOSE_REG; + + volatile u_int32_t val = bus_r(addr); + FILE_LOG(logDEBUG1, ("Multipurpose reg: 0x%x\n", val)); + int times = 0; + for (times = 1; times < numphaseshift; ++times) { + bus_w(addr, val | PHS_STP_MSK); + bus_w(addr, val & (~PHS_STP_MSK)); + } + FILE_LOG(logDEBUG1, ("Multipurpose reg: 0x%x\n", val)); +} + +void cleanFifos() { + FILE_LOG(logINFO, ("Cleaning FIFOs\n")); + bus_w(ADC_SYNC_REG, bus_r(ADC_SYNC_REG) | ADC_SYNC_CLEAN_FIFOS_MSK); + bus_w(ADC_SYNC_REG, bus_r(ADC_SYNC_REG) & ~ADC_SYNC_CLEAN_FIFOS_MSK); +} + +void setADCSyncRegister() { + FILE_LOG(logINFO, ("\tSetting ADC Sync and Token Delays:\n")); + u_int32_t addr = ADC_SYNC_REG; + + // 0x88(no roi), 0x1b(roi) (MSB) + u_int32_t tokenDelay = ((adcConfigured == -1) ? + ADC_SYNC_ENET_DELAY_NO_ROI_VAL : ADC_SYNC_ENET_DELAY_ROI_VAL); + + // 0x88032214(no roi), 0x1b032214(with roi) + u_int32_t val = (ADC_SYNC_TKN_VAL | tokenDelay); + + bus_w(addr, val); + FILE_LOG(logINFO, ("\tADC Sync Reg: 0x%x\n", bus_r(addr))); +} + +void setDAQRegister() { + FILE_LOG(logINFO, ("\tSetting Packet Length and DAQ Token Timing:\n")); + u_int32_t addr = DAQ_REG; + + // 0x1f16(board rev 1) 0x1f0f(board rev 2) + u_int32_t tokenTiming = ((getBoardRevision() == 1) ? + DAQ_TKN_TMNG_BRD_RVSN_1_VAL : DAQ_TKN_TMNG_BRD_RVSN_2_VAL); + + // 0x13f(no roi), 0x7f(roi) + u_int32_t packetLength = ((adcConfigured == -1) ? + DAQ_PCKT_LNGTH_NO_ROI_VAL : DAQ_PCKT_LNGTH_ROI_VAL); + + // MSB: packetLength LSB: tokenTiming + u_int32_t val = (tokenTiming | packetLength); + + bus_w(addr, val); + FILE_LOG(logINFO, ("\tDAQ Reg: 0x%x\n", bus_r(addr))); +} + +void setChipOfInterestRegister(int adc) { + FILE_LOG(logINFO, ("\tSelecting Chips of Interst:\n")); + u_int32_t addr = CHIP_OF_INTRST_REG; + + // 0x1f(no roi), 0xXX(roi) + u_int32_t adcSelect = ((adcConfigured == -1) ? + CHIP_OF_INTRST_ADC_SEL_MSK : + (((1 << adc) << CHIP_OF_INTRST_ADC_SEL_OFST) & CHIP_OF_INTRST_ADC_SEL_MSK)); + + // 0x0500(no roi), 0x0100(roi) + u_int32_t numChannels = ((adcConfigured == -1) ? NCHIP : NCHIPS_PER_ADC) * NCHAN; + + // 0x500001f(no roi), 0x10000xx(roi) MSB:num channels, LSB: selected ADC + u_int32_t val = (numChannels | adcSelect); + + bus_w(addr, val); + FILE_LOG(logINFO, ("\tChip Of Interest Reg: 0x%x\n", bus_r(addr))); +} + +void setROIADC(int adc) { + FILE_LOG(logINFO, ("\tSetting ROI ADC: %d\n", adc)); + adcConfigured = adc; + + setADCSyncRegister(); // adc sync & token delays + setDAQRegister(); // packet length & token timing + cleanFifos(); // clean fifos + setChipOfInterestRegister(adc); // num channels & select adc + + ipPacketSize = ((adcConfigured == -1) ? IP_PACKET_SIZE_NO_ROI : IP_PACKET_SIZE_ROI); + udpPacketSize = ((adcConfigured == -1) ? UDP_PACKETSIZE_NO_ROI : UDP_PACKETSIZE_ROI); +} + +void setGbitReadout() { + FILE_LOG(logINFO, ("Setting Gbit Readout\n")); + u_int32_t addr = CONFIG_REG; + bus_w(addr, bus_r(addr) & ~CONFIG_CPU_RDT_MSK); + FILE_LOG(logINFO, ("\tConfig Reg 0x%x\n", bus_r(addr))); +} + +int readConfigFile() { + // open config file + FILE* fd = fopen(CONFIG_FILE, "r"); + if(fd == NULL) { + FILE_LOG(logWARNING, ("\tCould not find config file %s\n", CONFIG_FILE)); + return FAIL; + } + FILE_LOG(logINFO, ("\tConfig file %s opened\n", CONFIG_FILE)); + + // Initialization + const size_t lineSize = 256; + char line[lineSize] = {0}; + const size_t keySize = lineSize / 2; + char key[keySize] = {0}; + char value[keySize] = {0}; + int scan = OK; + + // keep reading a line + while (fgets(line, lineSize, fd)) { + // ignore comments + if (line[0] == '#') + continue; + // read key & value + sscanf(line, "%s %s\n", key, value); + + // key is master/ slave flag + if (!strcasecmp(key,"masterflags")) { + if (!strcasecmp(value,"is_master")) { + masterflags = IS_MASTER; + FILE_LOG(logINFOBLUE, ("\tMaster\n")); + } else if (!strcasecmp(value,"is_slave")) { + masterflags = IS_SLAVE; + FILE_LOG(logINFOBLUE, ("\tSlave\n")); + } else if (!strcasecmp(value,"no_master")){ + masterflags = NO_MASTER; + FILE_LOG(logINFOBLUE, ("\tNo Master\n")); + } else { + FILE_LOG(logERROR, ("\tCould not scan masterflags %s value from config file\n", value)); + scan = FAIL; + break; + } + + // not first server since detector power on + if (!detectorFirstServer) { + FILE_LOG(logINFOBLUE, ("\tServer has been started up before. Ignoring rest of config file\n")); + fclose(fd); + return FAIL; + } + } + + // key is not master/ slave flag + else { + // convert value to int + int ival = 0; + if(sscanf(value, "%d", &ival) <= 0) { + FILE_LOG(logERROR, ("\tCould not scan parameter %s value %s from config file\n", key, value)); + scan = FAIL; + break; + } + // set value + if (!strcasecmp(key, "masterdefaultdelay")) + masterdefaultdelay = ival; + else if (!strcasecmp(key, "patternphase")) + patternphase = ival; + else if (!strcasecmp(key, "adcphase")) + adcphase = ival; + else if (!strcasecmp(key, "slavepatternphase")) + slavepatternphase = ival; + else if (!strcasecmp(key, "slaveadcphase")) + slaveadcphase = ival; + else if (!strcasecmp(key, "rsttosw1delay")) + rsttosw1delay = ival; + else if (!strcasecmp(key, "startacqdelay")) + startacqdelay = ival; + else { + FILE_LOG(logERROR, ("\tCould not scan parameter %s from config file\n", key)); + scan = FAIL; + break; + } + } + } + fclose(fd); + if (scan == FAIL) + exit(EXIT_FAILURE); + + FILE_LOG(logINFOBLUE, ( + "\tmasterdefaultdelay:%d\n" + "\tpatternphase:%d\n" + "\tadcphase:%d\n" + "\tslavepatternphase:%d\n" + "\tslaveadcphase:%d\n" + "\trsttosw1delay:%d\n" + "\tstartacqdelay:%d\n", + masterdefaultdelay, + patternphase, + adcphase, + slavepatternphase, + slaveadcphase, + rsttosw1delay, + startacqdelay)); + return OK; +} + +void setMasterSlaveConfiguration() { + FILE_LOG(logINFO, ("Reading Master Slave Configuration\n")); + + // no config file or not first time server + if (readConfigFile() == FAIL) + return; + + // master configuration + if (masterflags == IS_MASTER) { + // master default delay set, so reset delay + setDelay(0); + + // Set pattern phase for the master module + val = (bus_r(MULTI_PURPOSE_REG) & (~(PLL_CLK_SL_MSK))); // unset mask + bus_w(MULTI_PURPOSE_REG, val | PLL_CLK_SL_MSTR_VAL); + setPhaseShift(patternphase); + + // Set adc phase for the master module + val = (bus_r(MULTI_PURPOSE_REG) & (~(PLL_CLK_SL_MSK))); // unset mask + bus_w(MULTI_PURPOSE_REG, val | PLL_CLK_SL_MSTR_ADC_VAL); + setPhaseShift(adcphase); + + // Set pattern phase for the slave module + val = (bus_r(MULTI_PURPOSE_REG) & (~(PLL_CLK_SL_MSK))); // unset mask + bus_w(MULTI_PURPOSE_REG, val | PLL_CLK_SL_SLV_VAL); + setPhaseShift(slavepatternphase); + + // Set adc phase for the slave module + val = (bus_r(MULTI_PURPOSE_REG) & (~(PLL_CLK_SL_MSK))); // unset mask + bus_w(MULTI_PURPOSE_REG, val | PLL_CLK_SL_SLV_ADC_VAL); + setPhaseShift(slaveadcphase); + + // Set start acq delay + val = (bus_r(MULTI_PURPOSE_REG) & (~(STRT_ACQ_DLY_MSK))); // unset mask + val = val | ((startacqdelay << STRT_ACQ_DLY_OFST) & STRT_ACQ_DLY_MSK); // set val + bus_w(MULTI_PURPOSE_REG, val); + FILE_LOG(logDEBUG1, ("\tMultipurpose reg: 0x%x\n", val)); + } + + // all configuration - Set RST to SW1 delay + val = (bus_r(MULTI_PURPOSE_REG) & (~(RST_TO_SW1_DLY_MSK))); // unset mask + val = val | ((rsttosw1delay << RST_TO_SW1_DLY_OFST) & RST_TO_SW1_DLY_MSK); // set val + bus_w(MULTI_PURPOSE_REG, val); + FILE_LOG(logDEBUG1, ("\tMultipurpose reg: 0x%x\n", val)); + + FILE_LOG(logINFO, ("\tMaster Slave Configuration has been set up\n")); +} + + +/* set parameters - dr, roi */ + +int setDynamicRange(int dr){ + return DYNAMIC_RANGE; +} + +ROI* setROI(int n, ROI arg[], int *retvalsize, int *ret) { + + // set ROI + if(n >= 0){ + // print + if (!n) { + FILE_LOG(logINFO, ("Clearing ROI\n")); + } else { + FILE_LOG(logINFO, ("Setting ROI:\n")); + int i = 0; + for (i = 0; i < n; ++i) { + FILE_LOG(logINFO, ("\t(%d, %d)\n", arg[i].xmin, arg[i].xmax)); + + } + } + // only one ROI allowed per module + if (n > 1) { + FILE_LOG(logERROR, ("\tCannot set more than 1 ROI per module\n")); + *ret = FAIL; + *retvalsize = nROI; + return rois; + } + + //clear all rois + nROI = 0; + + // find adc number and recorrect channel limits + int adc = -1; + if (n) { + // all channels + if ((arg[0].xmin <= 0) && (arg[0].xmax >= NCHIP * NCHAN)) + adc = -1; + // single adc + else { + //adc = mid value/numchans + adc = ((((arg[0].xmax) + (arg[0].xmin))/2) / (NCHAN * NCHIPS_PER_ADC)); + // incorrect adc + if((adc < 0) || (adc > 4)) { + FILE_LOG(logERROR, ("\tadc value greater than 5. deleting roi\n")); + adc = -1; + } + // recorrect roi values + else { + 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; + } + } + } + + if (adc == -1) + nROI = 0; + + FILE_LOG(logINFO, ("\tAdc to be configured: %d\n", adc)); + FILE_LOG(logINFO, ("\tROI to be configured: (%d, %d)\n", + (adc == -1) ? 0 : (rois[0].xmin), + (adc == -1) ? (NCHIP * NCHAN - 1) : (rois[0].xmax))); + + // could not set roi + if((n != 0) && ((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; + FILE_LOG(logERROR, ("\tCould not set given ROI\n")); + } + if(n != nROI) { + *ret = FAIL; + FILE_LOG(logERROR, ("\tCould not set or clear ROIs\n")); + } + + //set adc of interest + setROIADC(adc); + } + + // print + if (!nROI) { + FILE_LOG(logINFO, ("\tROI: None\n\n")); + } else { + FILE_LOG(logINFO, ("ROI:\n")); + int i = 0; + for (i = 0; i < nROI; ++i) { + FILE_LOG(logINFO, ("\t(%d, %d)\n\n", rois[i].xmin, rois[i].xmax)); + + } + } + + *retvalsize = nROI; + return rois; +} + + +/* parameters - timer */ + +int64_t setTimer(enum timerIndex ind, int64_t val) { + + int64_t retval = -1; + switch(ind){ + + case FRAME_NUMBER: + if(val >= 0) { + FILE_LOG(logINFO, ("Setting #frames: %lld\n",(long long int)val)); + } + retval = set64BitReg(val, SET_FRAMES_LSB_REG, SET_FRAMES_MSB_REG); + FILE_LOG(logDEBUG1, ("\tGetting #frames: %lld\n", (long long int)retval)); + break; + + case ACQUISITION_TIME: + if(val >= 0){ + FILE_LOG(logINFO, ("Setting exptime: %lldns\n", (long long int)val)); + val = (val * 1E-3 * CLK_FREQ) + 0.5; + } + retval = (set64BitReg(val, SET_EXPTIME_LSB_REG, SET_EXPTIME_MSB_REG) / + (1E-3 * CLK_FREQ)) + 0.5; + FILE_LOG(logDEBUG1, ("\tGetting exptime: %lldns\n", (long long int)retval)); + break; + + case FRAME_PERIOD: + if(val >= 0){ + FILE_LOG(logINFO, ("Setting period: %lldns\n",(long long int)val)); + val = (val * 1E-3 * CLK_FREQ) + 0.5; + } + retval = (set64BitReg(val, SET_PERIOD_LSB_REG, SET_PERIOD_MSB_REG) / + (1E-3 * CLK_FREQ)) + 0.5; + FILE_LOG(logDEBUG1, ("\tGetting period: %lldns\n", (long long int)retval)); + break; + + case DELAY_AFTER_TRIGGER: + if(val >= 0){ + FILE_LOG(logINFO, ("Setting delay: %lldns\n", (long long int)val)); + if (masterflags == IS_MASTER) { + value += masterdefaultdelay; + FILE_LOG(logINFO, ("\tActual delay (master): %lld\n", (long long int) value)); + } + val = (val * 1E-3 * CLK_FREQ) + 0.5; + } + retval = (set64BitReg(val, SET_DELAY_LSB_REG, SET_DELAY_MSB_REG) / + (1E-3 * CLK_FREQ)) + 0.5; + FILE_LOG(logDEBUG1, ("\tGetting delay: %lldns\n", (long long int)retval)); + if (masterflags == IS_MASTER) { + FILE_LOG(logDEBUG1, ("\tActual delay read (master): %lld\n", (long long int) retval)); + retval -= masterdefaultdelay; + } + break; + + case CYCLES_NUMBER: + if(val >= 0) { + FILE_LOG(logINFO, ("Setting #cycles: %lld\n", (long long int)val)); + } + retval = set64BitReg(val, SET_TRAINS_LSB_REG, SET_TRAINS_MSB_REG); + FILE_LOG(logDEBUG1, ("\tGetting #cycles: %lld\n", (long long int)retval)); + break; + + default: + FILE_LOG(logERROR, ("Timer Index not implemented for this detector: %d\n", ind)); + break; + } + + return retval; + +} + + + +int64_t getTimeLeft(enum timerIndex ind){ +#ifdef VIRTUAL + return 0; +#endif + int64_t retval = -1; + switch(ind){ + + case FRAME_NUMBER: + retval = get64BitReg(GET_FRAMES_LSB_REG, GET_FRAMES_MSB_REG); + FILE_LOG(logINFO, ("Getting number of frames left: %lld\n",(long long int)retval)); + break; + + case ACQUISITION_TIME: + retval = (get64BitReg(GET_EXPTIME_LSB_REG, GET_EXPTIME_MSB_REG) / + (1E-3 * CLK_FREQ)) + 0.5; + FILE_LOG(logINFO, ("Getting exptime left: %lldns\n", (long long int)retval)); + break; + + case FRAME_PERIOD: + retval = (get64BitReg(GET_PERIOD_LSB_REG, GET_PERIOD_MSB_REG) / + (1E-3 * CLK_FREQ)) + 0.5; + FILE_LOG(logINFO, ("Getting period left: %lldns\n", (long long int)retval)); + break; + + case DELAY_AFTER_TRIGGER: + retval = (get64BitReg(GET_DELAY_LSB_REG, GET_DELAY_MSB_REG) / + (1E-3 * CLK_FREQ)) + 0.5; + FILE_LOG(logINFO, ("Getting delay left: %lldns\n", (long long int)retval)); + if (masterflags == IS_MASTER) { + FILE_LOG(logDEBUG1, ("\tGetting Actual delay (master): %lld\n", (long long int) retval)); + retval -= masterdefaultdelay; + } + break; + + case CYCLES_NUMBER: + retval = get64BitReg(GET_TRAINS_LSB_REG, GET_TRAINS_MSB_REG); + FILE_LOG(logINFO, ("Getting number of cycles left: %lld\n", (long long int)retval)); + break; + + default: + FILE_LOG(logERROR, ("Remaining Timer index not implemented for this detector: %d\n", ind)); + break; + } + + return retval; +} + + +/* parameters - channel, chip, module, settings */ + +int setModule(sls_detector_module myMod, char* mess){ + + FILE_LOG(logINFO, ("Setting module with settings %d\n",myMod.reg)); + + // settings + setSettings( (enum detectorSettings)myMod.reg); + + //set dac values + { + int i = 0, retval[2] = {-1, -1}; + for(i = 0; i < NDAC; ++i) + setDAC((enum DACINDEX)i, myMod.dacs[i], 0, retval); + } + return OK; +} + +int getModule(sls_detector_module *myMod){ + int idac = 0; + for (idac = 0; idac < NDAC; ++idac) { + if (dacValues[idac] >= 0) + *((myMod->dacs) + idac) = dacValues[idac]; + } + // check if all of them are not initialized + int initialized = 0; + for (idac = 0; idac < NDAC; ++idac) { + if (dacValues[idac] >= 0) + initialized = 1; + } + if (initialized) + return OK; + return FAIL; +} + +enum detectorSettings setSettings(enum detectorSettings sett){ + if(sett == UNINITIALIZED) + return thisSettings; + + // set settings + if(sett != GET_SETTINGS) { + u_int32_t addr = GAIN_REG; + + // find conf gain value + uint32_t confgain = 0x0; + switch (sett) { + case DYNAMICGAIN: + FILE_LOG(logINFO, ("Set settings - Dyanmic Gain\n")); + confgain = GAIN_CONFGAIN_DYNMC_GAIN_VAL; + break; + case HIGHGAIN: + FILE_LOG(logINFO, ("Set settings - High Gain\n")); + confgain = GAIN_CONFGAIN_HGH_GAIN_VAL; + break; + case LOWGAIN: + FILE_LOG(logINFO, ("Set settings - Low Gain\n")); + confgain = GAIN_CONFGAIN_LW_GAIN_VAL; + break; + case MEDIUMGAIN: + FILE_LOG(logINFO, ("Set settings - Medium Gain\n")); + confgain = GAIN_CONFGAIN_MDM_GAIN_VAL; + break; + case VERYHIGHGAIN: + FILE_LOG(logINFO, ("Set settings - Very High Gain\n")); + confgain = GAIN_CONFGAIN_VRY_HGH_GAIN_VAL; + break; + default: + FILE_LOG(logERROR, ("This settings is not defined for this detector %d\n", (int)sett)); + return -1; + } + // set conf gain + bus_w(addr, bus_r(addr) & ~GAIN_CONFGAIN_MSK); + bus_w(addr, bus_r(addr) | GAIN_CONFGAIN_DYNMC_GAIN_VAL); + FILE_LOG(logINFO, ("\Gain Reg: 0x%x\n", bus_r(addr))); + thisSettings = sett; + } + + return getSettings(); +} + +enum detectorSettings getSettings(){ + + uint32_t regval = bus_r(GAIN_REG); + FILE_LOG(logDEBUG1, ("Getting Settings\n Read Gain Register :0x%x\n", val)); + + uint32_t val = regval & GAIN_CONFGAIN_MSK; + switch(val) { + case GAIN_CONFGAIN_DYNMC_GAIN_VAL: + FILE_LOG(logINFO, ("Settings read: Dyanmic Gain. Gain Reg: 0x%x\n", regval)); + thisSettings = DYNAMICGAIN; + break; + case GAIN_CONFGAIN_HGH_GAIN_VAL: + FILE_LOG(logINFO, ("Settings read: High Gain. Gain Reg: 0x%x\n", regval)); + thisSettings = HIGHGAIN; + break; + case GAIN_CONFGAIN_LW_GAIN_VAL: + FILE_LOG(logINFO, ("Settings read: Low Gain. Gain Reg: 0x%x\n", regval)); + thisSettings = LOWGAIN; + break; + case GAIN_CONFGAIN_MDM_GAIN_VAL: + FILE_LOG(logINFO, ("Settings read: Medium Gain. Gain Reg: 0x%x\n", regval)); + thisSettings = MEDIUMGAIN; + break; + case GAIN_CONFGAIN_VRY_HGH_GAIN_VAL: + FILE_LOG(logINFO, ("Settings read: Very High Gain. Gain Reg: 0x%x\n", regval)); + thisSettings = VERYHIGHGAIN; + break; + default: + thisSettings = UNDEFINED; + FILE_LOG(logERROR, ("Settings read: Undefined. Gain Reg: 0x%x\n", regval)); + } + + return thisSettings; +} + + +/* parameters - dac, adc, hv */ + +int voltageToDac(int value){ + int vmin = 0; + int vmax = 2500; + int nsteps = 4096; + if ((value < vmin) || (value > vmax)) { + FILE_LOG(logERROR, ("Voltage value (to convert to dac value) is outside bounds: %d\n", value)); + return -1; + } + return (int)(((value - vmin) / (vmax - vmin)) * (nsteps - 1) + 0.5); +} + +int dacToVoltage(unsigned int digital){ + int vmin = 0; + int vmax = 2500; + int nsteps = 4096; + int v = vmin + (vmax - vmin) * digital / (nsteps - 1); + if((v < 0) || (v > nsteps - 1)) { + FILE_LOG(logERROR, ("Voltage value (converted from dac value) is outside bounds: %d\n", v)); + return -1; + } + return v; +} + +void setDAC(enum DACINDEX ind, int val, int mV, int retval[]){ + int dacmV = val; + + //if set and mv, convert to dac + if (val > 0) { + if (mV) + val = voltageToDac(val); //gives -1 on error + else + dacmV = dacToVoltage(val); //gives -1 on error + } + + if (val >= 0) { + FILE_LOG(logINFO, ("Setting DAC %d: %d dac (%d mV)\n",ind, val, mV)); +#ifndef VIRTUAL + initDAC(ind,val); + clearDACSregister(); +#else + dacValues[ind] = val; +#endif + } + + retval[0] = dacValues[ind]; + retval[1] = dacToVoltage(retval[0]); + FILE_LOG(logDEBUG1, ("Getting DAC %d : %d dac (%d mV)\n",ind, retval[0], retval[1])); +} + +void initDAC(int dac_addr, int value) { + FILE_LOG(logDEBUG1, ("Programming dac %d with value %d\n", dac_addr, value)); + clearDACSregister(); + if (value >= 0) + program_one_dac(dac_addr,value); + nextDAC(); +} + +void clearDACSregister() { + putout("1111111111111111");//reset + putout("1111111111111110");//cs down +} + +void nextDAC() { + putout("1111111111111011");//cs up + putout("1111111111111001");//clk down + putout("1111111111111111");//reset +} + +void program_one_dac(int addr, int value) { + FILE_LOG(logDEBUG1, ("Programming dac %d with value %d\n", addr, value)); + int origValue = value; + int control = 32 + addr; + value = (value << 4) | (control << 16); + + { + int i = 0; + for (i = 0; i < 24; ++i) { + int bit = value & (1 << (23 - i)); + if (bit) { + putout("1111111111111100");//clk down + putout("1111111111111100");//write data + putout("1111111111111110");//clk up + } + else + { + putout("1111111111111000");//clk down + putout("1111111111111000");//write data + putout("1111111111111010");//clk up + } + } + } + dacValues[addr] = origValue; + FILE_LOG(logDEBUG1, ("\tDac %d set to %dn", addr, origValue)); +} + +// direct pattern output +u_int32_t putout(char *s) { + if (strlen(s)<16) { + FILE_LOG(logERROR, ("putout: incorrect pattern length\n")); + return FAIL; + } + + u_int32_t pat=0; + {int i = 0; + for (i = 0; i < 16; ++i) { + if (s[i]=='1') + pat = pat + (1 << (15 - i)); + } + } + bus_w(DAC_CNTRL_REG, pat); + return OK; +} + + +int getADC(enum ADCINDEX ind){ +#ifdef VIRTUAL + return 0; +#endif + char tempnames[2][40]={"VRs/FPGAs Temperature", "ADCs/ASICs Temperature"}; + FILE_LOG(logDEBUG1, ("Getting Temperature for %s\n", tempnames[ind])); + + u_int32_t addr = TEMP_SPI_IN_REG; + uint32_t addrout = TEMP_SPI_OUT_REG; + const int repeats = 6; // number of register writes for delay + const int reads = 20; + u_int32_t value = 0; + + // standby, high clk, high cs + bus_w(addr, (TEMP_SPI_IN_T1_CLK_MSK | TEMP_SPI_IN_T1_CS_MSK | TEMP_SPI_IN_T2_CLK_MSK | TEMP_SPI_IN_T2_CS_MSK)); + + // high clk low cs + bus_w(addr, (TEMP_SPI_IN_T1_CLK_MSK | TEMP_SPI_IN_T2_CLK_MSK)); + + { + int i = 0; + for (i = 0; i < reads; ++i) { + + int j = 0; + // low clk low cs + for (j = 0; j < repeats; ++j) + bus_w(addr, 0x0); + //high clk low cs + for (j = 0; j < repeats; ++j) + bus_w(addr, (TEMP_SPI_IN_T1_CLK_MSK | TEMP_SPI_IN_T2_CLK_MSK)); + + // only the first time + if (i <= 10) { + if (ind == TEMP_ADC) + value = (value << 1) + (bus_r(addrout) & TEMP_SPI_OUT_T1_DT_MSK); + else + value = (value << 1) + (bus_r(addrout) & TEMP_SPI_OUT_T2_DT_MSK); + } + } + } + + // standby high clk, high cs + bus_w(addr, (TEMP_SPI_IN_T1_CLK_MSK | TEMP_SPI_IN_T1_CS_MSK | TEMP_SPI_IN_T2_CLK_MSK | TEMP_SPI_IN_T2_CS_MSK)); + + FILE_LOG(logDEBUG1, ("\tInitial Temperature value: %u\n", value)); + // conversion + value = value/4.0; + FILE_LOG(logINFO, ("\tTemperature %s: %f °C\n",tempnames[ind], value)); + return value; +} + +int setHighVoltage(int val){ +#ifdef VIRTUAL + if (val >= 0) + highvoltage = val; + return highvoltage; +#endif + u_int32_t addr = HV_REG; + u_int32_t sel = 0x0; + + // set + if (val >= 0) { + FILE_LOG(logINFO, ("Setting High Voltage to %d\n", val)); + switch (val) { + case 0: + break; + case 90: + sel = HV_SEL_90_VAL; + break; + case 110: + sel = HV_SEL_110_VAL; + break; + case 120: + sel = HV_SEL_120_VAL; + break; + case 150: + sel = HV_SEL_150_VAL; + break; + case 180: + sel = HV_SEL_180_VAL; + break; + case 200: + sel = HV_SEL_200_VAL; + break; + default: + FILE_LOG(logERROR, ("%d high voltage is not defined for this detector\n", val)); + return setHighVoltage(-1); + } + FILE_LOG(logDEBUG1, ("\tHigh voltage value to be sent: 0x%x\n", sel)); + + // switch off high voltage and set value + bus_w(addr, (bus_r(addr) & ~HV_ENBL_MSK) | sel); + + // switch on high voltage + if (val > 0) + bus_w(addr, bus_r(addr) | HV_ENBL_MSK); + } + + // get + u_int32_t retval = 0; + u_int32_t regval = bus_r(addr); + FILE_LOG(logDEBUG1, ("\tHigh voltage value read: 0x%x\n", regval)); + + // if high voltage was enabled, find value + if (regval & HV_ENBL_MSK) { + switch (regval & HV_SEL_MSK) { + case HV_SEL_90_VAL: + retval = 90; + break; + case HV_SEL_110_VAL: + retval = 110; + break; + case HV_SEL_120_VAL: + retval = 120; + break; + case HV_SEL_150_VAL: + retval = 150; + break; + case HV_SEL_180_VAL: + retval = 180; + break; + default: + retval = 200; + break; + } + } + FILE_LOG(logDEBUG1, ("\tHigh Voltage: %d\n", retval)); + return retval; +} + + +/* parameters - timing, extsig */ + + +void setTiming( enum externalCommunicationMode arg){ + u_int32_t addr = EXT_SIGNAL_REG; + + if (arg != GET_EXTERNAL_COMMUNICATION_MODE){ + switch((int)arg){ + case AUTO_TIMING: + FILE_LOG(logINFO, ("Set Timing: Auto\n")); + bus_w(addr, EXT_SIGNAL_OFF_VAL); + break; + case TRIGGER_EXPOSURE: + if (signalMode == TRIGGER_IN_FALLING_EDGE) { + FILE_LOG(logINFO, ("Set Timing: Trigger (Falling Edge)\n")); + bus_w(addr, EXT_SIGNAL_TRGGR_IN_FLLNG_VAL); + } else { + FILE_LOG(logINFO, ("Set Timing: Trigger (Rising Edge)\n")); + bus_w(addr, EXT_SIGNAL_TRGGR_IN_RSNG_VAL); + } + break; + default: + FILE_LOG(logERROR, ("Unknown timing mode %d for this detector\n", (int)arg)); + return; + } + timingMode = arg; + } +} + +enum externalCommunicationMode getTiming() { + u_int32_t regval = bus_r(EXT_SIGNAL_REG); + switch (regval) { + case EXT_SIGNAL_TRGGR_IN_RSNG_VAL: + case EXT_SIGNAL_TRGGR_IN_FLLNG_VAL: + return TRIGGER_EXPOSURE; + default: + return AUTO_TIMING; + } +} + +void setExtSignal(enum externalSignalFlag mode) { + switch (mode) { + case TRIGGER_IN_RISING_EDGE: + case TRIGGER_IN_FALLING_EDGE: + signalMode = mode; + setTiming(getTiming()); + break; + default: + FILE_LOG(logERROR, ("Extsig (signal mode) %d not defined for this detector\n", mode)); + break; + } +} + +int getExtSignal() { + return signalMode; +} + + +/* configure mac */ + +void calcChecksum(mac_conf* mac, int sourceip, int destip) { + mac->ip.ip_ver = 0x4; + mac->ip.ip_ihl = 0x5; + mac->ip.ip_tos = 0x0; + mac->ip.ip_len = ipPacketSize; + mac->ip.ip_ident = 0x0000; + mac->ip.ip_flag = 0x2; //not nibble aligned (flag& offset + mac->ip.ip_offset = 0x00; + mac->ip.ip_ttl = 0x70; + mac->ip.ip_protocol = 0x11; + mac->ip.ip_chksum = 0x0000 ; // pseudo + mac->ip.ip_sourceip = sourceip; + mac->ip.ip_destip = destip; + FILE_log(logDEBUG1, ("\tIP TTL: 0x%x\n", mac_conf_regs->ip.ip_ttl)); + + int count = sizeof(ip); + unsigned short *addr; + addr = (unsigned short*)(&ip); /* warning: assignment from incompatible pointer type */ + + long int sum = 0; + 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 + long int checksum = (~sum) & 0xffff; + FILE_LOG(logINFO, ("\tIP checksum is 0x%lx\n", checksum)); + mac->ip.ip_chksum = checksum; +} + +int configureMAC(uint32_t destip, uint64_t destmac, uint64_t sourcemac, uint32_t sourceip, uint32_t udpport, uint32_t udpport2, int ival) { +#ifdef VIRTUAL + return OK; +#endif + FILE_LOG(logINFOBLUE, ("Configuring MAC\n")); + u_int32_t addr = MULTI_PURPOSE_REG; + + FILE_LOG(logDEBUG1, ("\tRoi: %d, Ip Packet size: %d UDP Packet size: %d\n", + adcConfigured, ipPacketSize, udpPacketSize)); + + uint32_t sourceport = DEFAULT_TX_UDP_PORT; + FILE_LOG(logINFO, ("\tSource IP : %d.%d.%d.%d \t\t(0x%08x)\n", + (sourceip>>24)&0xff,(sourceip>>16)&0xff,(sourceip>>8)&0xff,(sourceip)&0xff, sourceip)); + FILE_LOG(logINFO, ("\tSource MAC : %02x:%02x:%02x:%02x:%02x:%02x \t(0x%010llx)\n", + (unsigned int)((sourcemac>>40)&0xFF), + (unsigned int)((sourcemac>>32)&0xFF), + (unsigned int)((sourcemac>>24)&0xFF), + (unsigned int)((sourcemac>>16)&0xFF), + (unsigned int)((sourcemac>>8)&0xFF), + (unsigned int)((sourcemac>>0)&0xFF), + (long long unsigned int)sourcemac)); + FILE_LOG(logINFO, ("\tSource Port : %d \t\t\t(0x%08x)\n",sourceport, sourceport)); + FILE_LOG(logINFO, ("\tDest. IP : %d.%d.%d.%d \t\t(0x%08x)\n", + (destip>>24)&0xff,(destip>>16)&0xff,(destip>>8)&0xff,(destip)&0xff, destip)); + FILE_LOG(logINFO, ("\tDest. MAC : %02x:%02x:%02x:%02x:%02x:%02x \t(0x%010llx)\n", + (unsigned int)((destmac>>40)&0xFF), + (unsigned int)((destmac>>32)&0xFF), + (unsigned int)((destmac>>24)&0xFF), + (unsigned int)((destmac>>16)&0xFF), + (unsigned int)((destmac>>8)&0xFF), + (unsigned int)((destmac>>0)&0xFF), + (long long unsigned int)destmac)); + FILE_LOG(logINFO, ("\tDest. Port : %d \t\t\t(0x%08x)\n",udpport, udpport)); + + // set/ unset the digital test bit + if (ival) + bus_w (addr, bus_r(addr) | DGTL_TST_MSK); + else + bus_w (addr, bus_r(addr) & ~DGTL_TST_MSK); + FILE_log(logDEBUG1, ("\tDigital Test Bit. MultiPurpose reg: 0x%x\n", bus_r(addr))); + + //reset mac + bus_w (addr, bus_r(addr) | RST_MSK); + FILE_log(logDEBUG1, ("\tReset Mac. MultiPurpose reg: 0x%x\n", bus_r(addr))); + + usleep(500000); + + // release reset + bus_w(addr, bus_r(addr) &(~ RST_MSK)); + FILE_log(logDEBUG1, ("\tReset released. MultiPurpose reg: 0x%x\n", bus_r(addr))); + + // write shadow regs + bus_w(addr, bus_r(addr) | (ENT_RSTN_MSK | WRT_BCK_MSK)); + FILE_log(logDEBUG1, ("\tWrite shadow regs. MultiPurpose reg: 0x%x\n", bus_r(addr))); + + // release write back + bus_w(addr, bus_r(addr) &(~WRT_BCK_MSK)); + FILE_log(logDEBUG1, ("\tWrite back released. MultiPurpose reg: 0x%x\n", bus_r(addr))); + + // nreset phy /*FIXME: is this needed ?? */ + bus_w(addr, bus_r(addr) | ENT_RSTN_MSK); + FILE_log(logDEBUG1, ("\tNreset phy. MultiPurpose reg: 0x%x\n", bus_r(addr))); + + FILE_log(logDEBUG1, ("\tConfiguring MAC CONF\n")); + mac_conf *mac_conf_regs = (mac_conf*)(CSP0BASE + ENET_CONF_REG * 2); // direct write + mac_conf_regs->mac.mac_dest_mac1 = ((destmac >> (8 * 5)) & 0xFF); + mac_conf_regs->mac.mac_dest_mac2 = ((destmac >> (8 * 4)) & 0xFF); + mac_conf_regs->mac.mac_dest_mac3 = ((destmac >> (8 * 3)) & 0xFF); + mac_conf_regs->mac.mac_dest_mac4 = ((destmac >> (8 * 2)) & 0xFF); + mac_conf_regs->mac.mac_dest_mac5 = ((destmac >> (8 * 1)) & 0xFF); + mac_conf_regs->mac.mac_dest_mac6 = ((destmac >> (8 * 0)) & 0xFF); + FILE_log(logDEBUG1, ("\tDestination Mac: %llx %x:%x:%x:%x:%x:%x\n", + destmac, + 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)); + mac_conf_regs->mac.mac_src_mac1 = ((sourcemac >> (8 * 5)) & 0xFF); + mac_conf_regs->mac.mac_src_mac2 = ((sourcemac >> (8 * 4)) & 0xFF); + mac_conf_regs->mac.mac_src_mac3 = ((sourcemac >> (8 * 3)) & 0xFF); + mac_conf_regs->mac.mac_src_mac4 = ((sourcemac >> (8 * 2)) & 0xFF); + mac_conf_regs->mac.mac_src_mac5 = ((sourcemac >> (8 * 1)) & 0xFF); + mac_conf_regs->mac.mac_src_mac6 = ((sourcemac >> (8 * 0)) & 0xFF); + FILE_log(logDEBUG1, ("\tSource Mac: %llx %x:%x:%x:%x:%x:%x\n", + sourcemac, + 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)); + mac_conf_regs->mac.mac_ether_type = 0x0800; //ipv4 + + calcChecksum(mac_conf_regs, sourceip, destip); + mac_conf_regs->udp.udp_srcport = sourceport; + mac_conf_regs->udp.udp_destport = udpport; + mac_conf_regs->udp.udp_len = udpPacketSize; + mac_conf_regs->udp.udp_chksum = 0x0000; + + FILE_log(logDEBUG1, ("\tConfiguring TSE\n")); + tse_conf *tse_conf_regs = (tse_conf*)(CSP0BASE + TSE_CONF_REG * 2); // direct write + 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; + + FILE_log(logDEBUG1, ("\tConfigure Mac Done")); + mac_conf_regs->cdone = 0xFFFFFFFF; + + // write shadow regs /* FIXME: Only INT_RSTN_MSK | WRT_BCK_MSK */ + bus_w(addr, bus_r(addr) | (INT_RSTN_MSK | ENT_RSTN_MSK| WRT_BCK_MSK)); + FILE_log(logDEBUG1, ("\tWrite shadow regs with int reset. MultiPurpose reg: 0x%x\n", bus_r(addr))); + + usleep(100000); + + // release write back + bus_w(addr, bus_r(addr) &(~WRT_BCK_MSK)); + FILE_log(logDEBUG1, ("\tWrite back released. MultiPurpose reg: 0x%x\n", bus_r(addr))); + + // sw1 /* FIXME: Only SW1_MSK */ + bus_w(addrr, bus_r(addrr) | (INT_RSTN_MSK | ENT_RSTN_MSK | SW1_MSK)); + FILE_log(logDEBUG1, ("\tSw1. MultiPurpose reg: 0x%x\n", bus_r(addr))); + + usleep(1000 * 1000); + return OK; +} + +int getAdcConfigured(){ + return adcConfigured; +} + + +/* gotthard specific - loadimage, read/reset counter block */ + +void loadImage(enum imageType index, short int imageVals[]){ + u_int32_t addr = DARK_IMAGE_REG; + if (index == GAIN_IMAGE) + addr = GAIN_IMAGE_REG; + int dataBytes = calculateDataBytes(); + + volatile u_int16_t *ptr = (u_int16_t*)(CSP0BASE + addr * 2); + memcpy((char*)ptr, (char*)imageVals, dataBytes); + + FILE_log(logINFO, ("Loaded %s image at 0x%p\n", + (index == GAIN_IMAGE) ? "Gain" : "Dark", (void*) ptr)); +} + +int readCounterBlock(int startACQ, short int counterVals[]){ + FILE_log(logINFO, ("Reading Counter Block with start Acq :%d\n", startACQ)); + + // stop any current acquisition + if (runBusy()) { + if (stopStateMachine() == FAIL) + return FAIL; + // waiting for the last frame read to be done + while(runBusy()) + usleep(500); + FILE_log(logDEBUG1, ("State machine stopped\n")); + } + + // copy memory + addr = COUNTER_MEMORY_REG; + volatile u_int16_t *ptr = (u_int16_t*)(CSP0BASE + addr * 2); + int dataBytes = calculateDataBytes(); + memcpy((char*)counterVals, (char*)ptr, dataBytes); + + // unreset counter + addr = MULTI_PURPOSE_REG; + bus_w(addr, (bus_r(addr) &~ RST_CNTR_MSK)); + FILE_LOG(logDEBUG1, ("\tUnsetting reset Counter. Multi Purpose Reg: 0x%x\n", bus_r(addr))); + + // start state machine + if (startACQ == 1){ + startStateMachine(); + if (runBusy()) { + FILE_LOG(logINFO, ("State machine RUNNING\n")); + } else { + FILE_LOG(logINFO, ("State machine IDLE\n")); + } + } + return OK; +} + +int resetCounterBlock(int startACQ){ + FILE_log(logINFO, ("Resetting Counter Block with start Acq :%d\n", startACQ)); + + // stop any current acquisition + if (runBusy()) { + if (stopStateMachine() == FAIL) + return FAIL; + // waiting for the last frame read to be done + while(runBusy()) + usleep(500); + FILE_log(logDEBUG1, ("State machine stopped\n")); + } + + // reset counter + u_int32_t addr = MULTI_PURPOSE_REG; + bus_w(addr, (bus_r(addr) | RST_CNTR_MSK)); + FILE_LOG(logDEBUG1, ("\tResetting Counter. Multi Purpose Reg: 0x%x\n", bus_r(addr))); + + // copy memory + addr = COUNTER_MEMORY_REG; + volatile u_int16_t *ptr = (u_int16_t*)(CSP0BASE + addr * 2); + int dataBytes = calculateDataBytes(); + char *counterVals = NULL; + counterVals = realloc(counterVals, dataBytes); + memcpy((char*)counterVals, (char*)ptr, dataBytes); + + // unreset counter + addr = MULTI_PURPOSE_REG; + bus_w(addr, (bus_r(addr) &~ RST_CNTR_MSK)); + FILE_LOG(logDEBUG1, ("\tUnsetting reset Counter. Multi Purpose Reg: 0x%x\n", bus_r(addr))); + + // start state machine + if (startACQ == 1){ + startStateMachine(); + if (runBusy()) { + FILE_LOG(logINFO, ("State machine RUNNING\n")); + } else { + FILE_LOG(logINFO, ("State machine IDLE\n")); + } + } + + if (sizeof(counterVals) <= 0){ + FILE_LOG(logERROR, ("\tSize of counterVals: %d\n", (int)sizeof(counterVals))); + return FAIL; + } + + return OK; +} + + +/* aquisition */ + +int startStateMachine(){ +#ifdef VIRTUAL + virtual_status = 1; + virtual_stop = 0; + if(pthread_create(&pthread_virtual_tid, NULL, &start_timer, NULL)) { + virtual_status = 0; + FILE_LOG(logERROR, ("Could not start Virtual acquisition thread\n")); + return FAIL; + } + FILE_LOG(logINFOGREEN, ("Virtual Acquisition started\n")); + return OK; +#endif + FILE_LOG(logINFOBLUE, ("Starting State Machine\n")); + + cleanFifos(); + + //start state machine + bus_w16(CONTROL_REG, CONTROL_STRT_ACQ_MSK); + bus_w16(CONTROL_REG, 0x0); + FILE_LOG(logINFO, ("Status Register: %08x\n", runState())); + return OK; +} + +#ifdef VIRTUAL +void* start_timer(void* arg) { + int wait_in_s = (setTimer(FRAME_NUMBER, -1) * + setTimer(CYCLES_NUMBER, -1) * + (setTimer(FRAME_PERIOD, -1)/(1E9))); + FILE_LOG(logDEBUG1, ("going to wait for %d s\n", wait_in_s)); + while(!virtual_stop && (wait_in_s >= 0)) { + usleep(1000 * 1000); + wait_in_s--; + } + FILE_LOG(logINFOGREEN, ("Virtual Timer Done\n")); + + virtual_status = 0; + return NULL; +} +#endif + +int stopStateMachine(){ + FILE_LOG(logINFORED, ("Stopping State Machine\n")); +#ifdef VIRTUAL + virtual_stop = 0; + return OK; +#endif + //stop state machine + bus_w16(CONTROL_REG, CONTROL_STP_ACQ_MSK); + usleep(100);/**FIXME:Needed? not there earlier*/ + bus_w16(CONTROL_REG, 0x0); + + // check + usleep(500); + if ((runState() & STATUS_RN_MSHN_BSY_MSK)) { + FILE_LOG(logERROR, ("\tFailed to stop state machine\n")); + return FAIL; + } + + FILE_LOG(logINFO, ("Status Register: %08x\n", runState())); + return OK; +} + +enum runStatus getRunStatus(){ +#ifdef VIRTUAL + if(virtual_status == 0){ + FILE_LOG(logINFOBLUE, ("Status: IDLE\n")); + return IDLE; + }else{ + FILE_LOG(logINFOBLUE, ("Status: RUNNING\n")); + return RUNNING; + } +#endif + FILE_LOG(logDEBUG1, ("Getting status\n")); + + enum runStatus s = IDLE; + u_int32_t retval = runState(); + FILE_LOG(logINFO, ("Status Register: %08x\n",retval)); + + // finished (external stop or fifo full) + if (retval & STATUS_RN_FNSHD_MSK) { + FILE_LOG(logINFORED, ("Status: Stopped\n")); + s = STOPPED; + + FILE_LOG(logINFO, ("\t Reading status reg again\n")); + retval = runState(); + // fifo full + if (runState() & STATUS_RN_FNSHD_MSK) { + FILE_LOG(logINFORED, ("Status: Error\n")); + s = ERROR; + } + } + + // error (fifo full) + else if (retval & STATUS_SM_FF_FLL_MSK) { + FILE_LOG(logINFORED, ("Status: Error\n")); + s = ERROR; + } + + // not running + else if (!(retval & STATUS_RN_MSHN_BSY_MSK)) { + // read last frames + if (retval & STATUS_RD_MSHN_BSY_MSK) { + FILE_LOG(logINFOBLUE, ("Status: Read Machine Busy\n")); + s = TRANSMITTING; + } + // ??? + else if (retval & STATUS_ALL_FF_EMPTY_MSK) { + FILE_LOG(logINFOBLUE, ("Status: Data in Fifo\n")); + s = TRANSMITTING; + } + // idle, unknown + else if (!(retval & STATUS_IDLE_MSK)) { + FILE_LOG(logINFOBLUE, ("Status: IDLE\n")); + s = IDLE; + } else { + FILE_LOG(logINFORED, ("Status: Unknown Status: 0x%x\n", retval)); + s = ERROR; + } + } + + // running + else { + if (retval & STATUS_WTNG_FR_TRGGR_MSK){ + FILE_LOG(logINFOBLUE, ("Status: Waiting\n")); + s = WAITING; + } + else{ + FILE_LOG(logINFOBLUE, ("Status: Running\n")); + s = RUNNING; + } + } + + return s; +} + +void readFrame(int *ret, char *mess){ +#ifdef VIRTUAL + while(virtual_status) { + //FILE_LOG(logERROR, ("Waiting for finished flag\n"); + usleep(5000); + } + return; +#endif + // wait for status to be done + while(runBusy()){ + usleep(500); + } + + // frames left to give status + int64_t retval = getTimeLeft(FRAME_NUMBER) + 1; + if ( retval > -1) { + *ret = (int)FAIL; + sprintf(mess,"No data and run stopped: %lld frames left\n",(long long int)retval); + FILE_LOG(logERROR, (mess)); + } else { + *ret = (int)OK; + FILE_LOG(logINFOGREEN, ("Acquisition successfully finished\n")); + } +} + +u_int32_t runBusy() { +#ifdef VIRTUAL + return virtual_status; +#endif + return runState() & STATUS_RN_BSY_MSK; +} + +u_int32_t runState() { +#ifdef VIRTUAL + return virtual_status; +#endif + u_int32_t s = bus_r(STATUS_REG); + FILE_LOG(logDEBUG1, ("Status Register: %08x\n", s)); + return s; +} + +/* common */ + +int calculateDataBytes(){ + return DATA_BYTES; +} + +int getTotalNumberOfChannels(){return ((int)getNumberOfChannelsPerChip() * (int)getNumberOfChips());} +int getNumberOfChips(){return NCHIP;} +int getNumberOfDACs(){return NDAC;} +int getNumberOfChannelsPerChip(){return NCHAN;} + + diff --git a/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.h new file mode 120000 index 000000000..345b8c029 --- /dev/null +++ b/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.h @@ -0,0 +1 @@ +../slsDetectorServer/slsDetectorFunctionList.h \ No newline at end of file diff --git a/slsDetectorServers/gotthardDetectorServer/slsDetectorServer.c b/slsDetectorServers/gotthardDetectorServer/slsDetectorServer.c new file mode 120000 index 000000000..a7eb59acb --- /dev/null +++ b/slsDetectorServers/gotthardDetectorServer/slsDetectorServer.c @@ -0,0 +1 @@ +../slsDetectorServer/slsDetectorServer.c \ No newline at end of file diff --git a/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_defs.h b/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_defs.h new file mode 100644 index 000000000..002cd6515 --- /dev/null +++ b/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_defs.h @@ -0,0 +1,121 @@ +#pragma once +#include "sls_detector_defs.h" //default dynamicgain in settings + +#define GOODBYE (-200) + +/* Enums */ +enum ADCINDEX {TEMP_FPGA, TEMP_ADC}; +enum DACINDEX {VREF_DS, VCASCN_PB, VCASCP_PB, VOUT_CM, VCASC_OUT, VIN_CM, VREF_COMP, IB_TESTC}; +#define DEFAULT_DAC_VALS { \ + 660, /* VREF_DS */ \ + 650, /* VCASCN_PB */ \ + 1480, /* VCASCP_PB */ \ + 1520, /* VOUT_CM */ \ + 1320, /* VCASC_OUT */ \ + 1350, /* VIN_CM */ \ + 350, /* VREF_COMP */ \ + 2001 /* IB_TESTC */ \ + }; + +/* for 25 um */ +#define CONFIG_FILE "config.txt" + +/* Hardware Definitions */ +#define NCHAN (128) +#define NCHIP (10) +#define NDAC (8) +#define NCHIPS_PER_ADC (2) +#define DYNAMIC_RANGE (16) +#define NUM_BITS_PER_PIXEL (DYNAMIC_RANGE / 8) +#define DATA_BYTES (NCHIP * NCHAN * NUM_BITS_PER_PIXEL) +#define CLK_FREQ (32.007729) /* MHz */ + +/** Firmware Definitions */ +#define IP_PACKET_SIZE_NO_ROI (NCHIP * (NCHAN / 2) * 2 + 14 + 20) // 2 packets, so divide by 2 +#define IP_PACKET_SIZE_ROI (NCHIPS_PER_ADC * NCHAN * 2 + 14 + 20) + +#define UDP_PACKETSIZE_NO_ROI (NCHIP * (NCHAN / 2) * 2 + 4 + 8 + 2) // 2 packets, so divide by 2 +#define UDP_PACKETSIZE_ROI (NCHIPS_PER_ADC * NCHAN * 2 + 4 + 8 + 2) + +/** Default Parameters */ +#define DEFAULT_NUM_FRAMES (1) +#define DEFAULT_NUM_CYCLES (1) +#define DEFAULT_EXPTIME (1 * 1000 * 1000) // 1 ms +#define DEFAULT_PERIOD (1 * 1000 * 1000 * 1000) // 1 s +#define DEFAULT_DELAY (0) +#define DEFAULT_SETTINGS (DYNAMICGAIN) +#define DEFAULT_TIMING_MODE (AUTO_TIMING) +#define DEFAULT_TRIGGER_MODE (TRIGGER_IN_RISING_EDGE) +#define DEFAULT_HIGH_VOLTAGE (0) +#define DEFAULT_PHASE_SHIFT (120) +#define DEFAULT_TX_UDP_PORT (0xE185) + +/** 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; + diff --git a/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_funcs.c b/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_funcs.c new file mode 120000 index 000000000..a7532ccd4 --- /dev/null +++ b/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_funcs.c @@ -0,0 +1 @@ +../slsDetectorServer/slsDetectorServer_funcs.c \ No newline at end of file diff --git a/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_funcs.h b/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_funcs.h new file mode 120000 index 000000000..7569daf47 --- /dev/null +++ b/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_funcs.h @@ -0,0 +1 @@ +../slsDetectorServer/slsDetectorServer_funcs.h \ No newline at end of file diff --git a/slsDetectorServers/gotthardDetectorServer/stop_server.c b/slsDetectorServers/gotthardDetectorServer/stop_server.c deleted file mode 100755 index e3c8ff7e1..000000000 --- a/slsDetectorServers/gotthardDetectorServer/stop_server.c +++ /dev/null @@ -1,46 +0,0 @@ -/* 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/slsDetectorServers/jungfrauDetectorServer/Makefile b/slsDetectorServers/jungfrauDetectorServer/Makefile index c887965e5..850cbf826 100755 --- a/slsDetectorServers/jungfrauDetectorServer/Makefile +++ b/slsDetectorServers/jungfrauDetectorServer/Makefile @@ -25,7 +25,7 @@ $(PROGS): $(OBJS) rm *.gdb clean: - rm -rf $(DESTDIR)/$(PROGS) *.o + rm -rf $(DESTDIR)/$(PROGS) *.o *.gdb \ No newline at end of file diff --git a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c index 978660497..a03d5ae16 100644 --- a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c @@ -14,23 +14,20 @@ #include #endif -// Global variable from slsDetectorServer +// Global variable from slsDetectorServer_funcs extern int debugflag; int firmware_compatibility = OK; int firmware_check_done = 0; char firmware_message[MAX_STR_LENGTH]; -sls_detector_module *detectorModules = NULL; -int *detectorDacs = NULL; - #ifdef VIRTUAL pthread_t pthread_virtual_tid; int virtual_status = 0; int virtual_stop = 0; #endif -enum detectorSettings thisSettings; +enum detectorSettings thisSettings = UNINITIALIZED; int highvoltage = 0; int dacValues[NDAC] = {0}; int32_t clkPhase[2] = {0, 0}; @@ -45,7 +42,7 @@ int getFirmwareCheckResult(char** mess) { return firmware_compatibility; } -void checkFirmwareCompatibility() { +void basictests() { firmware_compatibility = OK; firmware_check_done = 0; memset(firmware_message, 0, MAX_STR_LENGTH); @@ -242,7 +239,7 @@ int detectorTest( enum digitalTestMode arg){ //DETECTOR_MEMORY_TEST:testRAM //DETECTOR_SOFTWARE_TEST: default: - FILE_LOG(logERROR, ("Test not implemented for this detector %d\n", (int)arg)); + FILE_LOG(logERROR, ("Test %s not implemented for this detector\n", (int)arg)); break; } return OK; @@ -387,39 +384,11 @@ void initStopServer() { /* set up detector */ -void allocateDetectorStructureMemory(){ - FILE_LOG(logINFO, ("This Server is for 1 Jungfrau module (500k)\n")); - - //Allocation of memory - if (detectorModules != NULL) free(detectorModules); - if (detectorDacs != NULL) free(detectorDacs); - detectorModules = malloc(sizeof(sls_detector_module)); - detectorDacs = malloc(NDAC*sizeof(int)); - FILE_LOG(logDEBUG1, ("modules from 0x%x to 0x%x\n",detectorModules, detectorModules)); - FILE_LOG(logDEBUG1, ("dacs from 0x%x to 0x%x\n",detectorDacs, detectorDacs)); - (detectorModules)->dacs = detectorDacs; - (detectorModules)->ndac = NDAC; - (detectorModules)->nchip = NCHIP; - (detectorModules)->nchan = NCHIP * NCHAN; - (detectorModules)->reg = 0; - (detectorModules)->iodelay = 0; - (detectorModules)->tau = 0; - (detectorModules)->eV = 0; - thisSettings = UNINITIALIZED; - - { // initialize to -1 - int i = 0; - for (i = 0; i < NDAC; ++i) { - dacValues[i] = -1; - } - } -} void setupDetector() { - - allocateDetectorStructureMemory(); + FILE_LOG(logINFO, ("This Server is for 1 Jungfrau module (500k)\n")); resetPLL(); resetCore(); @@ -797,12 +766,12 @@ int64_t getTimeLeft(enum timerIndex ind){ break; case ACTUAL_TIME: - retval = get64BitReg(TIME_FROM_START_LSB_REG, TIME_FROM_START_MSB_REG) / (1E-9 * CLK_SYNC); + retval = get64BitReg(TIME_FROM_START_LSB_REG, TIME_FROM_START_MSB_REG) / (1E-3 * CLK_SYNC); FILE_LOG(logINFO, ("Getting actual time (time from start): %lld\n", (long long int)retval)); break; case MEASUREMENT_TIME: - retval = get64BitReg(START_FRAME_TIME_LSB_REG, START_FRAME_TIME_MSB_REG) / (1E-9 * CLK_SYNC); + retval = get64BitReg(START_FRAME_TIME_LSB_REG, START_FRAME_TIME_MSB_REG) / (1E-3 * CLK_SYNC); FILE_LOG(logINFO, ("Getting measurement time (timestamp/ start frame time): %lld\n", (long long int)retval)); break; @@ -835,19 +804,10 @@ int setModule(sls_detector_module myMod, char* mess){ // settings setSettings( (enum detectorSettings)myMod.reg); - //copy module locally - if (detectorModules) { - if (copyModule(detectorModules, &myMod) == FAIL) { - sprintf(mess, "Could not copy module\n"); - FILE_LOG(logERROR, (mess)); - return FAIL; - } - } - //set dac values { int i = 0, retval[2] = {-1, -1}; - for(i = 0; i < myMod.ndac; ++i) + for(i = 0; i < NDAC; ++i) setDAC((enum DACINDEX)i, myMod.dacs[i], 0, retval); } return OK; @@ -855,19 +815,20 @@ int setModule(sls_detector_module myMod, char* mess){ int getModule(sls_detector_module *myMod){ - int i; - int retval[2]; - - //dacs - for(i=0;i= 0) + *((myMod->dacs) + idac) = dacValues[idac]; + } + // check if all of them are not initialized + int initialized = 0; + for (idac = 0; idac < NDAC; ++idac) { + if (dacValues[idac] >= 0) + initialized = 1; + } + if (initialized) + return OK; + return FAIL; } @@ -1028,22 +989,25 @@ int dacToVoltage(unsigned int digital){ void setDAC(enum DACINDEX ind, int val, int mV, int retval[]){ - int dacval = val; + int dacmV = val; - //if set and mv, convert to dac - if (val > 0 && mV) { - val = voltageToDac(val); //gives -1 on error - } + //if set and mv, convert to dac + if (val > 0) { + if (mV) + val = voltageToDac(val); //gives -1 on error + else + dacmV = dacToVoltage(val); //gives -1 on error + } if ( (val >= 0) || (val == -100)) { #ifdef VIRTUAL - dacValues[ind] = dacval; + dacValues[ind] = val; #else u_int32_t codata; int csdx = ind / NDAC + DAC_SERIAL_CS_OUT_OFST; // old board (16 dacs),so can be DAC_SERIAL_CS_OUT_OFST or +1 int dacchannel = ind % NDAC; // 0-8, dac channel number (also for dacnum 9-15 in old board) - FILE_LOG(logINFO, ("Setting DAC %d: %d dac (%d mV)\n",ind, dacval, val)); + FILE_LOG(logINFO, ("Setting DAC %d: %d dac (%d mV)\n",ind, val, dacmV)); // command if (val >= 0) { FILE_LOG(logDEBUG1,("\tWrite to Input Register and Update\n")); @@ -1064,7 +1028,7 @@ void setDAC(enum DACINDEX ind, int val, int mV, int retval[]){ serializeToSPI(SPI_REG, codata, (0x1 << csdx), LTC2620_DAC_NUMBITS, DAC_SERIAL_CLK_OUT_MSK, DAC_SERIAL_DIGITAL_OUT_MSK, DAC_SERIAL_DIGITAL_OUT_OFST); - dacValues[ind] = dacval; + dacValues[ind] = val; if (ind == VREF_COMP) { bus_w (VREF_COMP_MOD_REG, (bus_r(VREF_COMP_MOD_REG) &~ (VREF_COMP_MOD_MSK)) // reset @@ -1177,12 +1141,7 @@ enum externalCommunicationMode getTiming() { long int calcChecksum(int sourceip, int destip) { - ip_header ip; - int count; - unsigned short *addr; - long int sum = 0; - long int checksum; ip.ip_ver = 0x4; ip.ip_ihl = 0x5; ip.ip_tos = 0x0; @@ -1196,18 +1155,22 @@ long int calcChecksum(int sourceip, int destip) { ip.ip_sourceip = sourceip; ip.ip_destip = destip; - count = sizeof(ip); - addr = (unsigned short*) &(ip); /* warning: assignment from incompatible pointer type */ - while( count > 1 ) { + int count = sizeof(ip); + + unsigned short *addr; + addr = (unsigned short*) &(ip); /* warning: assignment from incompatible pointer type */ + + long int sum = 0; + 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; - + 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 + long int checksum = (~sum) & 0xffff; FILE_LOG(logINFO, ("IP checksum is 0x%lx\n",checksum)); - return checksum; } @@ -1215,7 +1178,7 @@ long int calcChecksum(int sourceip, int destip) { int configureMAC(uint32_t destip, uint64_t destmac, uint64_t sourcemac, uint32_t sourceip, uint32_t udpport, uint32_t udpport2){ #ifdef VIRTUAL - return 0; + return OK; #endif FILE_LOG(logINFOBLUE, ("Configuring MAC\n")); uint32_t sourceport = DEFAULT_TX_UDP_PORT; @@ -1277,7 +1240,7 @@ int configureMAC(uint32_t destip, uint64_t destmac, uint64_t sourcemac, uint32_t resetCore(); usleep(500 * 1000); /* todo maybe without */ - return 0; + return OK; } @@ -1602,11 +1565,11 @@ void readFrame(int *ret, char *mess){ -u_int32_t runBusy(void) { +u_int32_t runBusy() { #ifdef VIRTUAL return virtual_status; #endif - u_int32_t s = ((bus_r(STATUS_REG) & RUN_BUSY_MSK) >> RUN_BUSY_OFST); + u_int32_t s = (bus_r(STATUS_REG) & RUN_BUSY_MSK); FILE_LOG(logDEBUG1, ("Status Register: %08x\n", s)); return s; } @@ -1620,45 +1583,6 @@ u_int32_t runBusy(void) { /* common */ -//jungfrau doesnt require chips and chans (save memory) -int copyModule(sls_detector_module *destMod, sls_detector_module *srcMod){ - - int idac; - int ret=OK; - - FILE_LOG(logDEBUG1, ("Copying module\n")); - - if (srcMod->serialnumber>=0){ - destMod->serialnumber=srcMod->serialnumber; - } - if ((srcMod->nchip)>(destMod->nchip)) { - FILE_LOG(logERROR, ("Number of chip of source is larger than number of chips of destination\n")); - return FAIL; - } - if ((srcMod->nchan)>(destMod->nchan)) { - FILE_LOG(logERROR, ("Number of channels of source is larger than number of channels of destination\n")); - return FAIL; - } - if ((srcMod->ndac)>(destMod->ndac)) { - FILE_LOG(logERROR, ("Number of dacs of source is larger than number of dacs of destination\n")); - return FAIL; - } - FILE_LOG(logDEBUG1, ("DACs: src %d, dest %d\n",srcMod->ndac,destMod->ndac)); - FILE_LOG(logDEBUG1, ("Chips: src %d, dest %d\n",srcMod->nchip,destMod->nchip)); - FILE_LOG(logDEBUG1, ("Chans: src %d, dest %d\n",srcMod->nchan,destMod->nchan)); - - if (srcMod->reg>=0) - destMod->reg=srcMod->reg; - FILE_LOG(logDEBUG1, ("Copying register %x (%x)\n",destMod->reg,srcMod->reg )); - - for (idac=0; idac<(srcMod->ndac); idac++) { - if (*((srcMod->dacs)+idac)>=0) - *((destMod->dacs)+idac)=*((srcMod->dacs)+idac); - } - return ret; -} - - int calculateDataBytes(){ return DATA_BYTES; } diff --git a/slsDetectorServers/jungfrauDetectorServer/slsDetectorServer_defs.h b/slsDetectorServers/jungfrauDetectorServer/slsDetectorServer_defs.h index d897cbc42..7f3fe3893 100644 --- a/slsDetectorServers/jungfrauDetectorServer/slsDetectorServer_defs.h +++ b/slsDetectorServers/jungfrauDetectorServer/slsDetectorServer_defs.h @@ -38,11 +38,8 @@ enum DACINDEX {VB_COMP, VDD_PROT, VIN_COM, VREF_PRECH, VB_PIXBUF, VB_DS, VREF 480, /* VREF_DS */ \ 420 /* VREF_COMP */ \ }; - enum NETWORKINDEX { TXN_FRAME }; - - /* Hardware Definitions */ #define NCHAN (256 * 256) #define NCHIP (8) @@ -55,7 +52,6 @@ enum NETWORKINDEX { TXN_FRAME }; #define CLK_RUN (40) /* MHz */ #define CLK_SYNC (20) /* MHz */ - /** Default Parameters */ #define DEFAULT_NUM_FRAMES (100*1000*1000) #define DEFAULT_NUM_CYCLES (1) @@ -78,7 +74,6 @@ enum NETWORKINDEX { TXN_FRAME }; #define MAX_STORAGE_CELL_VAL (15) //0xF #define ACQ_TIME_MIN_CLOCK (2) - #define SAMPLE_ADC_HALF_SPEED (SAMPLE_DECMT_FACTOR_2_VAL + SAMPLE_DGTL_SAMPLE_0_VAL + SAMPLE_ADC_DECMT_FACTOR_0_VAL + SAMPLE_ADC_SAMPLE_0_VAL) /* 0x1000 */ #define SAMPLE_ADC_QUARTER_SPEED (SAMPLE_DECMT_FACTOR_4_VAL + SAMPLE_DGTL_SAMPLE_8_VAL + SAMPLE_ADC_DECMT_FACTOR_1_VAL + SAMPLE_ADC_SAMPLE_0_VAL) /* 0x2810 */ #define CONFIG_HALF_SPEED (CONFIG_TDMA_DISABLE_VAL + CONFIG_HALF_SPEED_20MHZ_VAL + CONFIG_MODE_1_X_10GBE_VAL) @@ -89,15 +84,6 @@ enum NETWORKINDEX { TXN_FRAME }; #define ADC_PHASE_QUARTER_SPEED (0x2D) //45 #define ADC_PORT_INVERT_VAL (0x453b2a9c) -/* Maybe not required for jungfrau */ -#define NTRIMBITS (6) -#define NCOUNTBITS (24) -#define NCHIPS_PER_ADC (2) -#define TRIM_DR (((int)pow(2,NTRIMBITS))-1) -#define COUNT_DR (((int)pow(2,NCOUNTBITS))-1) -#define ALLMOD (0xffff) -#define ALLFIFO (0xffff) - /* MSB & LSB DEFINES */ #define MSB_OF_64_BIT_REG_OFST (32) #define LSB_OF_64_BIT_REG_OFST (0) @@ -116,14 +102,11 @@ enum NETWORKINDEX { TXN_FRAME }; #define LTC2620_DAC_CMD_POWER_DOWN (0x00000004 << LTC2620_DAC_CMD_OFST) #define LTC2620_DAC_NUMBITS (24) - /* MAX1932 HV DEFINES */ #define MAX1932_HV_NUMBITS (8) #define MAX1932_HV_DATA_OFST (0) #define MAX1932_HV_DATA_MSK (0x000000FF << MAX1932_HV_DATA_OFST) - - /** PLL Reconfiguration Registers */ //https://www.altera.com/documentation/mcn1424769382940.html #define PLL_MODE_REG (0x00) diff --git a/slsDetectorServers/slsDetectorServer/AD9257.h b/slsDetectorServers/slsDetectorServer/AD9257.h index 615fdb3c0..9b86a60a7 100755 --- a/slsDetectorServers/slsDetectorServer/AD9257.h +++ b/slsDetectorServers/slsDetectorServer/AD9257.h @@ -1,5 +1,4 @@ -#ifndef AD9257_H -#define AD9257_H +#pragma once #include "commonServerFunctions.h" // blackfin.h, ansi.h @@ -90,12 +89,24 @@ void prepareADC(){ FILE_LOG(logINFO, ("\tPower mode reset\n")); setAdc(AD9257_POWER_MODE_REG, (AD9257_INT_RESET_VAL << AD9257_POWER_INTERNAL_OFST) & AD9257_POWER_INTERNAL_MSK); +#ifdef GOTTHARDD + usleep(50000); +#endif //power mode chip run FILE_LOG(logINFO, ("\tPower mode chip run\n")); setAdc(AD9257_POWER_MODE_REG, (AD9257_INT_CHIP_RUN_VAL << AD9257_POWER_INTERNAL_OFST) & AD9257_POWER_INTERNAL_MSK); +#ifdef GOTTHARDD + usleep(50000); + // binary offset + FILE_LOG(logINFO, ("\tBinary offset\n")); + setAdc(AD9257_OUT_MODE_REG, + (AD9257_OUT_BINARY_OFST_VAL << AD9257_OUT_FORMAT_OFST) & AD9257_OUT_FORMAT_MSK); + usleep(50000); + return; +#endif //output clock phase FILE_LOG(logINFO, ("\tOutput clock phase\n")); setAdc(AD9257_OUT_PHASE_REG, @@ -132,5 +143,3 @@ void prepareADC(){ (AD9257_MIXED_BIT_FREQ_VAL << AD9257_OUT_TEST_OFST) & AD9257_OUT_TEST_MSK); #endif } - -#endif //AD9257_H diff --git a/slsDetectorServers/slsDetectorServer/commonServerFunctions.h b/slsDetectorServers/slsDetectorServer/commonServerFunctions.h index 67651e487..cf62d90af 100755 --- a/slsDetectorServers/slsDetectorServer/commonServerFunctions.h +++ b/slsDetectorServers/slsDetectorServer/commonServerFunctions.h @@ -1,13 +1,6 @@ -#ifndef COMMON_SERVER_FUNCTIONS_H -#define COMMON_SERVER_FUNCTIONS_H +#pragma once -#ifndef GOTTHARDD //gotthard already had bus_w etc defined in its firmware_funcs.c (not yet made with common files) #include "blackfin.h" -#else -#include "ansi.h" -#endif - -/* global variables */ void SPIChipSelect (u_int32_t* valw, u_int32_t addr, u_int32_t csmask) { @@ -69,5 +62,3 @@ void serializeToSPI(u_int32_t addr, u_int32_t val, u_int32_t csmask, int numbits SPIChipDeselect(&valw, addr, csmask, clkmask); } - -#endif //COMMON_SERVER_FUNCTIONS_H diff --git a/slsDetectorServers/slsDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/slsDetectorServer/slsDetectorFunctionList.h index d161a7877..34bce6973 100644 --- a/slsDetectorServers/slsDetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/slsDetectorServer/slsDetectorFunctionList.h @@ -13,20 +13,21 @@ Here are the definitions, but the actual implementation should be done for each // basic tests -#if defined(EIGERD) || defined(JUNGFRAUD) || defined(GOTTHARD) +#if defined(EIGERD) || defined(JUNGFRAUD) || defined(GOTTHARDD) int isFirmwareCheckDone(); int getFirmwareCheckResult(char** mess); #endif - -void checkFirmwareCompatibility(); +void basictests(); #ifdef JUNGFRAUD int checkType(); u_int32_t testFpga(void); int testBus(void); #endif -#ifdef JUNGFRAUD -int detectorTest( enum digitalTestMode arg); +#ifdef GOTTHARDD +int detectorTest(enum digitalTestMode arg, int ival); +#elif JUNGFRAUD +int detectorTest(enum digitalTestMode arg); #endif // Ids @@ -40,6 +41,9 @@ u_int16_t getHardwareSerialNumber(); u_int32_t getDetectorNumber(); u_int64_t getDetectorMAC(); u_int32_t getDetectorIP(); +#ifdef GOTTHARDD +u_int32_t getBoardRevision(); +#endif // initialization @@ -50,9 +54,11 @@ void getModuleConfiguration(); #endif // set up detector +#ifdef EIGERD void allocateDetectorStructureMemory(); +#endif void setupDetector(); -#ifdef JUNGFRAUD +#if defined(GOTTHARDD) || defined(JUNGFRAUD) int setDefaultDacs(); #endif @@ -61,9 +67,12 @@ int setDefaultDacs(); #ifndef EIGERD extern u_int32_t writeRegister(u_int32_t offset, u_int32_t data); // blackfin.h extern u_int32_t readRegister(u_int32_t offset); // blackfin.h -#else +#elif JUNGFRAUD uint32_t writeRegister(uint32_t offset, uint32_t data); uint32_t readRegister(uint32_t offset); +#else +uint32_t writeRegister16And32(uint32_t offset, uint32_t data); +uint32_t readRegister16And32(uint32_t offset); #endif @@ -77,16 +86,29 @@ int autoCompDisable(int on); int adcPhase(int st); int getPhase(); void configureASICTimer(); +#elif GOTTHARDD +void setPhaseShiftOnce(); +void setPhaseShift(int numphaseshift); +void cleanFifos(); +void setADCSyncRegister(); +void setDAQRegister(); +void setChipOfInterestRegister(int adc); +void setROIADC(int adc); +void setGbitReadout(); +int readConfigFile(); +void setMasterSlaveConfiguration(); #endif // parameters - dr, roi int setDynamicRange(int dr); #ifdef GOTTHARD -int setROI(int n, ROI arg[], int *retvalsize, int *ret); +ROI* setROI(int n, ROI arg[], int *retvalsize, int *ret); #endif // parameters - readout +#ifndef GOTTHARDD enum speedVariable setSpeed(int val); +#endif #ifdef EIGERD enum readOutFlags setReadOutFlags(enum readOutFlags val); #endif @@ -114,17 +136,23 @@ int setThresholdEnergy(int ev); // parameters - dac, adc, hv #ifdef JUNGFRAUD -void serializeToSPI(u_int32_t addr, u_int32_t val, u_int32_t csmask, int numbitstosend, u_int32_t clkmask, u_int32_t digoutmask, int digofset); +void serializeToSPI(u_int32_t addr, u_int32_t val, u_int32_t csmask, int numbitstosend, u_int32_t clkmask, u_int32_t digoutmask, int digofset); //commonServerFunction.h void initDac(int dacnum); +#endif +#if defined(GOTTHARDD) || defined(JUNGFRAUD) int voltageToDac(int value); int dacToVoltage(unsigned int digital); -#endif - -#ifdef JUNGFRAUD extern void setAdc(int addr, int val); // AD9257.h #endif void setDAC(enum DACINDEX ind, int val, int mV, int retval[]); +#ifdef GOTTHARDD +void initDAC(int dac_addr, int value); +void clearDACSregister(); +void nextDAC(); +void program_one_dac(int addr, int value); +u_int32_t putout(char *s); +#endif int getADC(enum ADCINDEX ind); int setHighVoltage(int val); @@ -134,16 +162,21 @@ int setHighVoltage(int val); // parameters - timing, extsig void setTiming( enum externalCommunicationMode arg); enum externalCommunicationMode getTiming(); +#ifdef GOTTHARDD +void setExtSignal(enum externalSignalFlag mode); +int getExtSignal(); +#endif // configure mac -#ifdef JUNGFRAUD +#ifdef GOTTHARDD +void calcChecksum(mac_conf* mac, int sourceip, int destip); +#elif JUNGFRAUD long int calcChecksum(int sourceip, int destip); #endif #ifdef GOTTHARDD -int configureMAC(uint32_t destip, uint64_t destmac, uint64_t sourcemac, uint32_t sourceip, uint32_t udpport, uint32_t udpport2, int ival); -#else -int configureMAC(uint32_t destip, uint64_t destmac, uint64_t sourcemac, uint32_t sourceip, uint32_t udpport, uint32_t udpport2); +int getAdcConfigured(); #endif +int configureMAC(uint32_t destip, uint64_t destmac, uint64_t sourcemac, uint32_t sourceip, uint32_t udpport, uint32_t udpport2); #if defined(JUNGFRAUD) || defined(EIGERD) int setDetectorPosition(int pos[]); #endif @@ -153,10 +186,9 @@ int setDetectorPosition(int pos[]); // gotthard specific - image, pedestal #ifdef GOTTHARDD -int loadImage(enum imageType index, char *imageVals); +void loadImage(enum imageType index, short int imageVals[]); int readCounterBlock(int startACQ, char *counterVals); int resetCounterBlock(int startACQ); -int calibratePedestal(int frames); // jungfrau specific - pll, flashing firmware #elif JUNGFRAUD @@ -198,7 +230,7 @@ int setNetworkParameter(enum NETWORKINDEX mode, int value); // aquisition -#if defined(EIGERD) || defined(GOTTHARD) +#ifdef EIGERD int prepareAcquisition(); #endif int startStateMachine(); @@ -209,18 +241,23 @@ int stopStateMachine(); #ifdef EIGERD int softwareTrigger(); #endif -#ifndef JUNGFRAUD +#ifdef EIGERD int startReadOut(); #endif enum runStatus getRunStatus(); void readFrame(int *ret, char *mess); -#ifdef JUNGFRAUD -u_int32_t runBusy(void); +#if defined(GOTTHARDD) || defined(JUNGFRAUD) +u_int32_t runBusy(); +#endif +#ifdef GOTTHARDD +u_int32_t runState(); #endif //common +#ifdef EIGERD int copyModule(sls_detector_module *destMod, sls_detector_module *srcMod); +#endif int calculateDataBytes(); int getTotalNumberOfChannels(); int getNumberOfChips(); diff --git a/slsDetectorServers/slsDetectorServer/slsDetectorServer.c b/slsDetectorServers/slsDetectorServer/slsDetectorServer.c index 9758aee4e..365c35069 100755 --- a/slsDetectorServers/slsDetectorServer/slsDetectorServer.c +++ b/slsDetectorServers/slsDetectorServer/slsDetectorServer.c @@ -19,6 +19,11 @@ extern int ret; extern int sockfd; extern int debugflag; +// Global variables from slsDetectorFunctionList +#ifdef GOTTHARDD +extern int phaseShift; +#endif + void error(char *msg){ perror(msg); } @@ -41,10 +46,23 @@ int main(int argc, char *argv[]){ FILE_LOG(logINFO, ("Detected stop server\n")); isControlServer = 0; } - else if(!strcasecmp(argv[i],"-devel")){ - FILE_LOG(logINFO, ("Detected developer mode\n")); - debugflag = 1; + else if(!strcasecmp(argv[i],"-devel")){ + FILE_LOG(logINFO, ("Detected developer mode\n")); + debugflag = DEVELOPER_MODE; + } +#ifdef GOTTHARDD + else if(!strcasecmp(argv[i],"-phaseshift")){ + if ((i + 1) >= argc) { + FILE_LOG(logERROR, ("no phase shift value given. Exiting.\n")); + return -1; + } + if (sscanf(argv[i + 1], "%d", &phaseShift) == 0) { + FILE_LOG(logERROR, ("cannot decode phase shift value %s. Exiting.\n", argv[i + 1])); + return -1; + } + FILE_LOG(logINFO, ("Detected phase shift of %d\n", phaseShift)); } +#endif #ifdef JUNGFRAUD else if(!strcasecmp(argv[i],"-update")){ FILE_LOG(logINFO, ("Detected update mode\n")); diff --git a/slsDetectorServers/slsDetectorServer/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/slsDetectorServer_funcs.c index 45196226f..8e91f935d 100755 --- a/slsDetectorServers/slsDetectorServer/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/slsDetectorServer_funcs.c @@ -34,15 +34,12 @@ int debugflag = 0; // Local variables int (*flist[NUM_DET_FUNCTIONS])(int); -int dataBytes = 10; #ifdef EIGERD uint32_t dhcpipad = 0; #endif -#ifdef GOTTHARDD -int digitalTestBit = 0; -#endif - +enum numberMode {DEC, HEX}; +enum updateRet {NO_UPDATE, UPDATE}; /* initialization functions */ @@ -52,11 +49,6 @@ int printSocketReadError() { } -void basictests() { - checkFirmwareCompatibility(); -} - - void init_detector() { #ifdef VIRTUAL FILE_LOG(logINFO, ("This is a VIRTUAL detector\n")); @@ -271,10 +263,10 @@ void modeNotImplemented(char* modename, int mode) { FILE_LOG(logERROR,(mess)); } -void validate(int arg, int retval, char* modename, int hex) { +void validate(int arg, int retval, char* modename, enum numberMode nummode) { if (ret == OK && arg != -1 && retval != arg) { ret = FAIL; - if (hex) + if (nummode == HEX) sprintf(mess, "Could not %s. Set 0x%x, but read 0x%x\n", modename, arg, retval); else @@ -284,10 +276,10 @@ void validate(int arg, int retval, char* modename, int hex) { } } -void validate64(int64_t arg, int64_t retval, char* modename, int hex) { +void validate64(int64_t arg, int64_t retval, char* modename, enum numberMode nummode) { if (ret == OK && arg != -1 && retval != arg) { ret = FAIL; - if (hex) + if (nummode == HEX) sprintf(mess, "Could not %s. Set 0x%llx, but read 0x%llx\n", modename, (long long unsigned int)arg, (long long unsigned int)retval); else @@ -309,7 +301,7 @@ int M_nofunc(int file_des) { sprintf(mess,"Unrecognized Function enum %d. Please do not proceed.\n", fnum); FILE_LOG(logERROR, (mess)); - return Server_SendResult(file_des, OTHER, 0, NULL, 0); + return Server_SendResult(file_des, OTHER, NO_UPDATE, NULL, 0); } @@ -328,7 +320,7 @@ int M_nofuncMode(int file_des) { "Restart detector server in normal mode (without any arguments) to continue.\n", getFunctionName((enum detFuncs)fnum)); FILE_LOG(logERROR, (mess)); - return Server_SendResult(file_des, OTHER, 0, NULL, 0); + return Server_SendResult(file_des, OTHER, NO_UPDATE, NULL, 0); } @@ -364,7 +356,7 @@ int exec_command(int file_des) { pclose(sysFile); FILE_LOG(logINFO, ("Result of cmd (%s):\n%s\n", cmd, retval)); } - return Server_SendResult(file_des, OTHER, 0, retval, sizeof(retval)); + return Server_SendResult(file_des, OTHER, NO_UPDATE, retval, sizeof(retval)); } @@ -375,7 +367,7 @@ int get_detector_type(int file_des) { memset(mess, 0, sizeof(mess)); enum detectorType retval = myDetectorType; FILE_LOG(logDEBUG1,("Returning detector type %d\n", retval)); - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -404,11 +396,11 @@ int set_external_signal_flag(int file_des) { } // get retval = getExtSignal(signalindex); - validate((int)flag, (int)retval, "set external signal flag", 1); + validate((int)flag, (int)retval, "set external signal flag", DEC); FILE_LOG(logDEBUG1, ("External Signal Flag: %d\n", retval)); } #endif - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -442,9 +434,9 @@ int set_external_communication_mode(int file_des) { } // get retval = getTiming(); - validate((int)arg, (int)retval, "set timing mode", 0); + validate((int)arg, (int)retval, "set timing mode", DEC); FILE_LOG(logDEBUG1, ("Timing Mode: %d\n",retval)); - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -464,8 +456,8 @@ int get_id(int file_des) { switch (arg) { #if defined(EIGERD) || defined(JUNGFRAUD) case SOFTWARE_FIRMWARE_API_VERSION: -#endif case DETECTOR_SERIAL_NUMBER: +#endif case DETECTOR_FIRMWARE_VERSION: case DETECTOR_SOFTWARE_VERSION: retval = getDetectorId(arg); @@ -475,7 +467,7 @@ int get_id(int file_des) { modeNotImplemented("ID Index", (int)arg); break; } - return Server_SendResult(file_des, INT64, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT64, UPDATE, &retval, sizeof(retval)); } @@ -504,17 +496,16 @@ int digital_test(int file_des) { // only set if (Server_VerifyLock() == OK) { switch (mode) { -#ifdef GOTTHARDD - case DIGITAL_BIT_TEST: - FILE_LOG(logDEBUG1, ("Setting digital test bit: %d\n", ival)); - if (ival >= 0) - digitalTestBit = (ival > 0) ? 1 : 0; - retval = digitalTestBit; - break; -#endif + case DETECTOR_FIRMWARE_TEST: case DETECTOR_BUS_TEST: - retval = detectorTest(mode); +#ifdef GOTTHARDD + case DIGITAL_BIT_TEST: + retval = detectorTest(mode, ival); + break; +#else + retval = detectorTest(mode); +#endif FILE_LOG(logDEBUG1, ("Digital Test (%d): %d\n", mode, retval)); break; default: @@ -523,7 +514,7 @@ int digital_test(int file_des) { } } #endif - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -554,13 +545,29 @@ int set_dac(int file_des) { switch (ind) { #ifdef GOTTHARDD case G_VREF_DS : + serverDacIndex = VREF_DS; + break; case G_VCASCN_PB: + serverDacIndex = VCASCN_PB; + break; case G_VCASCP_PB: + serverDacIndex = VCASCP_PB; + break; case G_VOUT_CM: + serverDacIndex = VOUT_CM; + break; case G_VCASC_OUT: + serverDacIndex = VCASC_OUT; + break; case G_VIN_CM: + serverDacIndex = VIN_CM; + break; case G_VREF_COMP: + serverDacIndex = VREF_COMP; + break; case G_IB_TESTC: + serverDacIndex = IB_TESTC; + break; case HV_POT: break; #elif EIGERD @@ -643,6 +650,7 @@ int set_dac(int file_des) { case IO_DELAY: retval[0] = setIODelay(val); FILE_LOG(logDEBUG1, ("IODelay: %d\n", retval[0])); + validate(val, retval[0], "set iodelay", DEC); break; #endif @@ -651,7 +659,14 @@ int set_dac(int file_des) { case HV_NEW: retval[0] = setHighVoltage(val); FILE_LOG(logDEBUG1, ("High Voltage: %d\n", retval[0])); -#ifdef EIGERD +#ifdef GOTTHARDD + if (retval[0] == -1) { + ret = FAIL; + strcpy(mess,"Invalid Voltage. Valid values are 0, 90, 110, 120, 150, 180, 200\n"); + FILE_LOG(logERROR,(mess)); + } else + validate(val, retval[0], "set high voltage", DEC); +#elif EIGERD if ((retval[0] != SLAVE_HIGH_VOLTAGE_READ_VAL) && (retval[0] < 0)) { ret = FAIL; if (retval[0] == -1) @@ -710,7 +725,7 @@ int set_dac(int file_des) { } } } - return Server_SendResult(file_des, INT32, 1, retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, retval, sizeof(retval)); } @@ -774,7 +789,7 @@ int get_adc(int file_des) { retval = getADC(serverAdcIndex); FILE_LOG(logDEBUG1, ("ADC(%d): %d\n", retval)); } - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -782,29 +797,33 @@ int get_adc(int file_des) { int write_register(int file_des) { - ret = OK; - memset(mess, 0, sizeof(mess)); - uint32_t args[2] = {-1, -1}; - uint32_t retval = -1; + ret = OK; + memset(mess, 0, sizeof(mess)); + uint32_t args[2] = {-1, -1}; + uint32_t retval = -1; - if (receiveData(file_des, args, sizeof(args), INT32) < 0) - return printSocketReadError(); - uint32_t addr = args[0]; - uint32_t val = args[1]; - FILE_LOG(logDEBUG1, ("Writing to register 0x%x, data 0x%x\n", addr, val)); + if (receiveData(file_des, args, sizeof(args), INT32) < 0) + return printSocketReadError(); + uint32_t addr = args[0]; + uint32_t val = args[1]; + FILE_LOG(logDEBUG1, ("Writing to register 0x%x, data 0x%x\n", addr, val)); - // only set - if (Server_VerifyLock() == OK) { - retval = writeRegister(addr, val); - // validate - if (retval != val) { - ret = FAIL; - sprintf(mess,"Could not write to register 0x%x. Wrote 0x%x but read 0x%x\n", addr, val, retval); - FILE_LOG(logERROR,(mess)); - } - FILE_LOG(logDEBUG1, ("Write register (0x%x): 0x%x\n", retval)); - } - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + // only set + if (Server_VerifyLock() == OK) { +#ifdef GOTTHARDD + retval = writeRegister16And32(addr, val); +#else + retval = writeRegister(addr, val); +#endif + // validate + if (retval != val) { + ret = FAIL; + sprintf(mess,"Could not write to register 0x%x. Wrote 0x%x but read 0x%x\n", addr, val, retval); + FILE_LOG(logERROR,(mess)); + } + FILE_LOG(logDEBUG1, ("Write register (0x%x): 0x%x\n", retval)); + } + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -823,10 +842,14 @@ int read_register(int file_des) { FILE_LOG(logDEBUG1, ("Reading from register 0x%x\n", addr)); // get +#ifdef GOTTHARDD + retval = readRegister16And32(addr); +#else retval = readRegister(addr); +#endif FILE_LOG(logDEBUG1, ("Read register (0x%x): 0x%x\n", retval)); - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -854,8 +877,8 @@ int set_module(int file_des) { } else module.dacs = myDac; - // allocate chans #ifdef EIGERD + // allocate chans if (ret == OK) { myChan = (int*)malloc(getTotalNumberOfChannels() * sizeof(int)); if (getTotalNumberOfChannels() > 0 && myChan == NULL) { @@ -929,12 +952,12 @@ int set_module(int file_des) { ret = setModule(module, mess); retval = getSettings(); - validate(module.reg, (int)retval, "set module (settings)", 0); + validate(module.reg, (int)retval, "set module (settings)", DEC); FILE_LOG(logDEBUG1, ("Settings: %d\n", retval)); } if (myChan != NULL) free(myChan); if (myDac != NULL) free(myDac); - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -960,8 +983,8 @@ int get_module(int file_des) { } else module.dacs = myDac; - // allocate chans #ifdef EIGERD + // allocate chans if (ret == OK) { myChan = (int*)malloc(getTotalNumberOfChannels() * sizeof(int)); if (getTotalNumberOfChannels() > 0 && myChan == NULL) { @@ -985,7 +1008,7 @@ int get_module(int file_des) { FILE_LOG(logDEBUG1, ("Getting module. Settings:%d\n", module.reg)); } - Server_SendResult(file_des, INT32, 1, NULL, 0); + Server_SendResult(file_des, INT32, UPDATE, NULL, 0); // send module, 0 is to receive partially (without trimbits etc) if (ret != FAIL) { @@ -1046,7 +1069,7 @@ int set_settings(int file_des) { if (ret == OK) { retval = setSettings(isett); FILE_LOG(logDEBUG1, ("Settings: %d\n", retval)); - validate((int)isett, (int)retval, "set settings", 0); + validate((int)isett, (int)retval, "set settings", DEC); #if defined(JUNGFRAUD) || defined (GOTTHARDD) if (ret == OK && isett >= 0) { ret = setDefaultDacs(); @@ -1058,7 +1081,7 @@ int set_settings(int file_des) { #endif } } - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -1078,7 +1101,7 @@ int get_threshold_energy(int file_des) { retval = getThresholdEnergy(); FILE_LOG(logDEBUG1, ("Threshold energy: %d eV\n", retval)); #endif - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -1100,7 +1123,7 @@ int start_acquisition(int file_des) { } FILE_LOG(logDEBUG1, ("Starting Acquisition ret: %d\n", ret)); } - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -1119,7 +1142,7 @@ int stop_acquisition(int file_des) { } FILE_LOG(logDEBUG1, ("Stopping Acquisition ret: %d\n", ret)); } - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -1131,7 +1154,7 @@ int start_readout(int file_des) { memset(mess, 0, sizeof(mess)); FILE_LOG(logDEBUG1, ("Starting readout\n")); -#ifdef JUNGFRAUD +#ifndef EIGERD functionNotImplemented(); #else // only set @@ -1144,7 +1167,7 @@ int start_readout(int file_des) { FILE_LOG(logDEBUG1, ("Starting readout ret: %d\n", ret)); } #endif - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -1161,7 +1184,7 @@ int get_run_status(int file_des) { // only get retval = getRunStatus(); FILE_LOG(logDEBUG1, ("Status: %d\n", retval)); - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -1188,7 +1211,7 @@ int start_and_read_all(int file_des) { // lock or acquisition start error if (ret == FAIL) - return Server_SendResult(file_des, INT32, 1 , NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); // read all (again validate lock, but should pass and not fail) return read_all(file_des); @@ -1206,7 +1229,7 @@ int read_all(int file_des) { if (Server_VerifyLock() == OK) { readFrame(&ret, mess); } - return Server_SendResult(file_des, INT32, 1 , NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -1238,7 +1261,7 @@ int set_timer(int file_des) { case ACQUISITION_TIME: case FRAME_PERIOD: case CYCLES_NUMBER: -#ifdef JUNGFRAUD +#if defined(GOTTHARDD) || defined(JUNGFRAUD) case DELAY_AFTER_TRIGGER: #endif retval = setTimer(ind, tns); @@ -1284,12 +1307,12 @@ int set_timer(int file_des) { break; } // validate - validate64(tns, retval, "set timer", 0); + validate64(tns, retval, "set timer", DEC); } if (ret != FAIL) { FILE_LOG(logDEBUG1, ("Timer index %d: %lld\n", ind, retval)); } - return Server_SendResult(file_des, INT64, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT64, UPDATE, &retval, sizeof(retval)); } @@ -1325,18 +1348,16 @@ int get_time_left(int file_des) { #elif JUNGFRAUD case FRAMES_FROM_START: case FRAMES_FROM_START_PG: + case ACTUAL_TIME: + case MEASUREMENT_TIME: #elif GOTTHARDD - case GATES_NUMBER: + case ACQUISITION_TIME: #endif #if defined(GOTTHARDD) || defined(JUNGFRAUD) case FRAME_NUMBER: - case ACQUISITION_TIME: case FRAME_PERIOD: case DELAY_AFTER_TRIGGER: case CYCLES_NUMBER: - case PROGRESS: - case ACTUAL_TIME: - case MEASUREMENT_TIME: #endif retval = getTimeLeft(ind); FILE_LOG(logDEBUG1, ("Timer left index %d: %lld\n", ind, retval)); @@ -1346,7 +1367,7 @@ int get_time_left(int file_des) { break; } } - return Server_SendResult(file_des, INT64, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT64, UPDATE, &retval, sizeof(retval)); } @@ -1379,9 +1400,7 @@ int set_dynamic_range(int file_des) { #endif retval = setDynamicRange(dr); FILE_LOG(logDEBUG1, ("Dynamic range: %d\n", retval)); - validate(dr, retval, "set dynamic range", 0); - if (dr >= 0) - dataBytes=calculateDataBytes(); + validate(dr, retval, "set dynamic range", DEC); break; default: modeNotImplemented("Dynamic range", dr); @@ -1411,7 +1430,7 @@ int set_dynamic_range(int file_des) { } #endif } - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -1445,7 +1464,7 @@ int set_readout_flags(int file_des) { case NOOVERFLOW: retval = setReadOutFlags(arg); FILE_LOG(logDEBUG1, ("Read out flags: 0x%x\n", retval)); - validate((int)arg, (int)(retval & arg), "set readout flag", 1); + validate((int)arg, (int)(retval & arg), "set readout flag", HEX); break; default: modeNotImplemented("Read out flag index", (int)arg); @@ -1453,7 +1472,7 @@ int set_readout_flags(int file_des) { } } #endif - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -1499,18 +1518,24 @@ int set_roi(int file_des) { functionNotImplemented(); #else // set & get - if ((arg == GET_READOUT_FLAGS) || ((arg != GET_READOUT_FLAGS) && (Server_VerifyLock() == OK))) { - retval = setROI(narg, arg, &nretval, &ret); - if (ret == FAIL) { - sprintf(mess,"Could not set all roi. " - "Set %d rois, but read %d rois\n", narg, nretval); - FILE_LOG(logERROR,(mess)); - } - FILE_LOG(logDEBUG1, ("nRois: %d\n", nretval)); + if ((narg == GET_READOUT_FLAGS) || ((narg != GET_READOUT_FLAGS) && (Server_VerifyLock() == OK))) { + if (narg > 1) { + ret = FAIL; + strcpy(mess,"Can not set more than one ROI per module.\n"); + FILE_LOG(logERROR,(mess)); + } else { + retval = setROI(narg, arg, &nretval, &ret); + if (ret == FAIL) { + sprintf(mess,"Could not set all roi. " + "Set %d rois, but read %d rois\n", narg, nretval); + FILE_LOG(logERROR,(mess)); + } + FILE_LOG(logDEBUG1, ("nRois: %d\n", nretval)); + } } #endif - Server_SendResult(file_des, INT32, 1, NULL, 0); + Server_SendResult(file_des, INT32, UPDATE, NULL, 0); if (ret != FAIL) { //retvalsize could be swapped during sendData @@ -1539,6 +1564,10 @@ int set_speed(int file_des) { if (receiveData(file_des, args, sizeof(args), INT32) < 0) return printSocketReadError(); + +#ifdef GOTTHARDD + functionNotImplemented(); +#else enum speedVariable ind = args[0]; int val = args[1]; FILE_LOG(logDEBUG1, ("Setting speed index %d to %d\n", ind, val)); @@ -1552,21 +1581,23 @@ int set_speed(int file_des) { retval = adcPhase(val); FILE_LOG(logDEBUG1, ("ADc Phase: %d\n", retval)); if (val != 100000) { - validate(val, retval, "set adc phase ", 0); + validate(val, retval, "set adc phase ", DEC); } break; #endif case CLOCK_DIVIDER: retval = setSpeed(val); FILE_LOG(logDEBUG1, ("Clock: %d\n", retval)); - validate(val, retval, "set clock ", 0); + validate(val, retval, "set clock ", DEC); break; default: modeNotImplemented("Speed index", (int)ind); break; } } - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); +#endif + + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -1578,7 +1609,7 @@ int exit_server(int file_des) { cprintf(BG_RED, "Closing Server\n"); ret = OK; memset(mess, 0, sizeof(mess)); - Server_SendResult(file_des, INT32, 0, NULL, 0); + Server_SendResult(file_des, INT32, NO_UPDATE, NULL, 0); return GOODBYE; } @@ -1605,7 +1636,7 @@ int lock_server(int file_des) { Server_LockedError(); } } - return Server_SendResult(file_des, INT32, 1, &lockStatus, sizeof(lockStatus)); + return Server_SendResult(file_des, INT32, UPDATE, &lockStatus, sizeof(lockStatus)); } @@ -1614,7 +1645,7 @@ int lock_server(int file_des) { int get_last_client_ip(int file_des) { ret = OK; memset(mess, 0, sizeof(mess)); - return Server_SendResult(file_des, INT32, 1, lastClientIP, sizeof(lastClientIP)); + return Server_SendResult(file_des, INT32, UPDATE, lastClientIP, sizeof(lastClientIP)); } @@ -1646,7 +1677,7 @@ int set_port(int file_des) { } } - Server_SendResult(file_des, INT32, 1, &p_number, sizeof(p_number)); + Server_SendResult(file_des, INT32, UPDATE, &p_number, sizeof(p_number)); // delete old socket if (ret != FAIL) { closeConnection(file_des); @@ -1668,7 +1699,7 @@ int update_client(int file_des) { ret = OK; } #endif - Server_SendResult(file_des, INT32, 0, NULL, 0); + Server_SendResult(file_des, INT32, NO_UPDATE, NULL, 0); if (ret == OK) return ret; return send_update(file_des); @@ -1690,8 +1721,8 @@ int send_update(int file_des) { n = sendData(file_des,&i32,sizeof(i32),INT32); if (n < 0) return printSocketReadError(); - dataBytes = calculateDataBytes(); - n = sendData(file_des,&dataBytes,sizeof(dataBytes),INT32); + i32 = calculateDataBytes(); + n = sendData(file_des,&i32,sizeof(i32),INT32); if (n < 0) return printSocketReadError(); i32 = (int)setSettings(GET_SETTINGS); @@ -1732,12 +1763,6 @@ int send_update(int file_des) { if (n < 0) return printSocketReadError(); #endif -#if !defined(EIGERD) && !defined(JUNGFRAUD) - i64 = setTimer(GATES_NUMBER,GET_FLAG); - n = sendData(file_des,&i64,sizeof(i64),INT64); - if (n < 0) return printSocketReadError(); -#endif - i64 = setTimer(CYCLES_NUMBER,GET_FLAG); n = sendData(file_des,&i64,sizeof(i64),INT64); if (n < 0) return printSocketReadError(); @@ -1759,16 +1784,12 @@ int configure_mac(int file_des) { memset(mess, 0, sizeof(mess)); char args[9][50]; memset(args, 0, sizeof(args)); - char retvals[3][50]; + char retvals[2][50]; memset(retvals, 0, sizeof(retvals)); - int iretval = -1; if (receiveData(file_des, args, sizeof(args), OTHER) < 0) return printSocketReadError(); FILE_LOG(logDEBUG1, ("\n Configuring UDP Destination\n")); -#ifdef GOTTHARDD - FILE_LOG(logDEBUG1, ("Digital Test Bit %d\t", digitalTestBit); -#endif uint32_t dstIp = 0; sscanf(args[0], "%x", &dstIp); FILE_LOG(logDEBUG1, ("Dst Ip Addr: %d.%d.%d.%d = 0x%x \n", @@ -1863,20 +1884,14 @@ int configure_mac(int file_des) { srcIp = dhcpipad; #endif -#ifdef GOTTHARDD - iretval = configureMAC(dstIp, dstMac, srcMac, srcIp, dstPort, dstPort2, digitalTestBit); - FILE_LOG(logDEBUG1, ("Configure mac retval: %d\n", iretval)); - FILE_LOG(logINFO, ("Configure MAC successful: %d\n", iretval)); -#else - iretval = configureMAC(dstIp, dstMac, srcMac, srcIp, dstPort, dstPort2); - FILE_LOG(logDEBUG1, ("Configure mac retval: %d\n", iretval)); - - if (iretval == -1) { - ret = FAIL; + ret = configureMAC(dstIp, dstMac, srcMac, srcIp, dstPort, dstPort2); + if (ret == FAIL) { sprintf(mess,"Configure Mac failed\n"); FILE_LOG(logERROR,(mess)); - } else { - FILE_LOG(logINFO, ("Configure MAC successful\n")); + } +#if defined(EIGERD) || defined (JUNGFRAUD) + else { + FILE_LOG(logINFO, ("Configure MAC successful: %d\n", ret)); ret = setDetectorPosition(pos); if (ret == FAIL) { sprintf(mess, "Could not set detector position\n"); @@ -1886,14 +1901,13 @@ int configure_mac(int file_des) { #endif // set retval vals if (ret != FAIL) { - sprintf(retvals[0],"%x", iretval); - sprintf(retvals[1],"%llx", (long long unsigned int)srcMac); - sprintf(retvals[2],"%x", srcIp); + sprintf(retvals[0],"%llx", (long long unsigned int)srcMac); + sprintf(retvals[1],"%x", srcIp); } } } - return Server_SendResult(file_des, OTHER, 1, retvals, sizeof(retvals)); + return Server_SendResult(file_des, OTHER, UPDATE, retvals, sizeof(retvals)); } @@ -1907,12 +1921,12 @@ int load_image(int file_des) { if (receiveData(file_des, args, sizeof(args), INT32) < 0) return printSocketReadError(); + enum imageType index = args[0]; - char* ImageVals = NULL; - if (args[1] > 0) { - ImageVals = (char*)malloc(args[1]); - if (receiveData(file_des, ImageVals, args[1], OTHER) < 0) { - free(ImageVals); + int numChannels = args[1]; + short int imageVals[numChannels] = {0}; + if (numChannels > 0) { + if (receiveData(file_des, imageVals, numChannels * sizeof(short int), OTHER) < 0) { return printSocketReadError(); } } @@ -1928,12 +1942,15 @@ int load_image(int file_des) { switch (index) { case DARK_IMAGE : case GAIN_IMAGE : - ret = loadImage(index, ImageVals); - FILE_LOG(logDEBUG1, ("Loading image ret: %d\n", ret)); - if (ret == FAIL) { - sprintf(mess, "Could not load image\n"); - FILE_LOG(logERROR,(mess)); - } + // size of image does not match expected size + if (numChannels != (calculateDataBytes()/sizeof(short int))) { + ret = FAIL; + sprintf(mess, "Could not load image. " + "Number of Channels do not match. Expected %d, got %d\n", + calculateDataBytes(), numChannels); + FILE_LOG(logERROR,(mess)); + } else + loadImage(index, imageVals); break; default: modeNotImplemented("Image index", (int)index); @@ -1941,7 +1958,7 @@ int load_image(int file_des) { } } #endif - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -1952,12 +1969,13 @@ int load_image(int file_des) { int read_counter_block(int file_des) { ret = OK; memset(mess, 0, sizeof(mess)); - int startACQ = -1; - char retval[dataBytes]; - memset(retval, 0, dataBytes); + int args[2] = {-1, -1}; - if (receiveData(file_des, &startACQ, sizeof(startACQ), INT32) < 0) + if (receiveData(file_des, args, sizeof(args), INT32) < 0) return printSocketReadError(); + int startACQ = args[0]; + int numChannels = args[1]; + short int retval[numChannels] = {0}; FILE_LOG(logDEBUG1, ("Read counter block with start acq bit: %d\n", startACQ)); #ifndef GOTTHARDD @@ -1966,14 +1984,23 @@ int read_counter_block(int file_des) { // only set if (Server_VerifyLock() == OK) { - ret = readCounterBlock(startACQ, retval); - if (ret == FAIL) { - strcpy(mess, "Could not read counter block\n"); - FILE_LOG(logERROR,(mess)); - } + // size of image does not match expected size + if (numChannels != (calculateDataBytes()/sizeof(short int))) { + ret = FAIL; + sprintf(mess, "Could not load image. " + "Number of Channels do not match. Expected %d, got %d\n", + calculateDataBytes(), numChannels); + FILE_LOG(logERROR,(mess)); + } else { + ret = readCounterBlock(startACQ, retval); + if (ret == FAIL) { + strcpy(mess, "Could not read counter block\n"); + FILE_LOG(logERROR,(mess)); + } + } } #endif - return Server_SendResult(file_des, OTHER, 1, retval, dataBytes); + return Server_SendResult(file_des, OTHER, UPDATE, retval, numChannels * sizeof(short int)); } @@ -1992,7 +2019,6 @@ int reset_counter_block(int file_des) { #ifndef GOTTHARDD functionNotImplemented(); #else - // only set if (Server_VerifyLock() == OK) { ret = resetCounterBlock(startACQ); @@ -2002,7 +2028,7 @@ int reset_counter_block(int file_des) { } } #endif - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -2026,10 +2052,10 @@ int enable_ten_giga(int file_des) { if ((arg == -1) || ((arg != -1) && (Server_VerifyLock() == OK))) { retval = enableTenGigabitEthernet(arg); FILE_LOG(logDEBUG1, ("10GbE: %d\n", retval)); - validate(arg, retval, "enable/disable 10GbE", 0); + validate(arg, retval, "enable/disable 10GbE", DEC); } #endif - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -2059,9 +2085,9 @@ int set_all_trimbits(int file_des) { // get retval = getAllTrimbits(); FILE_LOG(logDEBUG1, ("All trimbits: %d\n", retval)); - validate(arg, retval, "set all trimbits", 0); + validate(arg, retval, "set all trimbits", DEC); #endif - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -2071,7 +2097,7 @@ int set_ctb_pattern(int file_des) { ret = OK; memset(mess, 0, sizeof(mess)); functionNotImplemented(); - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -2088,7 +2114,7 @@ int write_adc_register(int file_des) { uint32_t val = args[1]; FILE_LOG(logDEBUG1, ("Writing 0x%x to ADC Register 0x%x\n", val, addr)); -#ifndef JUNGFRAUD +#if defined(GOTTHARDD) || defined(JUNGFRAUD) functionNotImplemented(); #else #ifndef VIRTUAL @@ -2097,7 +2123,7 @@ int write_adc_register(int file_des) { setAdc(addr, val); #endif #endif - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -2124,9 +2150,9 @@ int set_counter_bit(int file_des) { // get retval = setCounterBit(-1); FILE_LOG(logDEBUG1, ("Set counter bit retval: %d\n", retval)); - validate(arg, retval, "set counter bit", 0); + validate(arg, retval, "set counter bit", DEC); #endif - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -2154,7 +2180,7 @@ int pulse_pixel(int file_des) { } } #endif - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -2182,7 +2208,7 @@ int pulse_pixel_and_move(int file_des) { } } #endif - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -2211,7 +2237,7 @@ int pulse_chip(int file_des) { } } #endif - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -2252,11 +2278,11 @@ int set_rate_correct(int file_des) { FILE_LOG(logERROR, ("Settings has been changed to undefined (tau changed)\n")); } int64_t retval = setRateCorrection(tau_ns); - validate64(tau_ns, retval, "set rate correction", 0); + validate64(tau_ns, retval, "set rate correction", DEC); } } #endif - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -2275,7 +2301,7 @@ int get_rate_correct(int file_des) { retval = getCurrentTau(); FILE_LOG(logDEBUG1, ("Tau: %lld\n", (long long int)retval)); #endif - return Server_SendResult(file_des, INT64, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT64, UPDATE, &retval, sizeof(retval)); } @@ -2332,11 +2358,11 @@ int set_network_parameter(int file_des) { if (ret == OK) { retval = setNetworkParameter(serverIndex, value); FILE_LOG(logDEBUG1, ("Network Parameter index %d: %d\n", serverIndex, retval)); - validate(value, retval, "set network parameter", 0); + validate(value, retval, "set network parameter", DEC); } } #endif - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -2394,7 +2420,7 @@ int program_fpga(int file_des) { } //---------------- first ret ---------------- - Server_SendResult(file_des, INT32, 0, NULL, 0); + Server_SendResult(file_des, INT32, NO_UPDATE, NULL, 0); if (ret != FAIL) { //erasing flash @@ -2427,7 +2453,7 @@ int program_fpga(int file_des) { ret = writeFPGAProgram(fpgasrc, unitprogramsize, fp); //---------------- middle rets ---------------- - Server_SendResult(file_des, INT32, 0, NULL, 0); + Server_SendResult(file_des, INT32, NO_UPDATE, NULL, 0); @@ -2455,14 +2481,14 @@ int program_fpga(int file_des) { FILE_LOG(logDEBUG1, ("Done with program receiving command\n")); if (isControlServer) { - basictests(debugflag); + basictests(); initControlServer(); } } } #endif #endif - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -2480,7 +2506,7 @@ int reset_fpga(int file_des) { // only set if (Server_VerifyLock() == OK) { if (isControlServer) { - basictests(debugflag); // mapping of control server at least + basictests(); // mapping of control server at least if (debugflag != PROGRAMMING_MODE) initControlServer(); } @@ -2488,7 +2514,7 @@ int reset_fpga(int file_des) { ret = FORCE_UPDATE; } #endif - return Server_SendResult(file_des, INT32, 1, NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -2510,7 +2536,7 @@ int power_chip(int file_des) { if ((arg == -1) || ((arg != -1) && (Server_VerifyLock() == OK))) { retval = powerChip(arg); FILE_LOG(logDEBUG1, ("Power chip: %d\n", retval)); - validate(arg, retval, "power on/off chip", 0); + validate(arg, retval, "power on/off chip", DEC); // narrow down error when powering on if (ret == FAIL && arg > 0) { if (setTemperatureEvent(-1) == 1) @@ -2520,7 +2546,7 @@ int power_chip(int file_des) { } } #endif - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -2543,10 +2569,10 @@ int set_activate(int file_des) { if ((arg == -1) || ((arg != -1) && (Server_VerifyLock() == OK))) { retval = activate(arg); FILE_LOG(logDEBUG1, ("Activate: %d\n", retval)); - validate(arg, retval, "set activate", 0); + validate(arg, retval, "set activate", DEC); } #endif - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -2569,7 +2595,7 @@ int prepare_acquisition(int file_des) { } } #endif - return Server_SendResult(file_des, INT32, 1 , NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -2601,11 +2627,11 @@ int threshold_temp(int file_des) { else { retval = setThresholdTemperature(arg); FILE_LOG(logDEBUG1, ("Threshold temperature: %d\n", retval)); - validate(arg, retval, "set threshold temperature", 0); + validate(arg, retval, "set threshold temperature", DEC); } } #endif - return Server_SendResult(file_des, INT32, 0, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, NO_UPDATE, &retval, sizeof(retval)); } @@ -2627,10 +2653,10 @@ int temp_control(int file_des) { if ((arg == -1) || ((arg != -1) && (Server_VerifyLock() == OK))) { retval = setTemperatureControl(arg); FILE_LOG(logDEBUG1, ("Temperature control: %d\n", retval)); - validate(arg, retval, "set temperature control", 0); + validate(arg, retval, "set temperature control", DEC); } #endif - return Server_SendResult(file_des, INT32, 0, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, NO_UPDATE, &retval, sizeof(retval)); } @@ -2653,10 +2679,10 @@ int temp_event(int file_des) { if ((arg == -1) || ((arg != -1) && (Server_VerifyLock() == OK))) { retval = setTemperatureEvent(arg); FILE_LOG(logDEBUG1, ("Temperature event: %d\n", retval)); - validate(arg, retval, "set temperature event", 0); + validate(arg, retval, "set temperature event", DEC); } #endif - return Server_SendResult(file_des, INT32, 0, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, NO_UPDATE, &retval, sizeof(retval)); } @@ -2680,10 +2706,10 @@ int auto_comp_disable(int file_des) { if ((arg == -1) || ((arg != -1) && (Server_VerifyLock() == OK))) { retval = autoCompDisable(arg); FILE_LOG(logDEBUG1, ("Auto comp disable: %d\n", retval)); - validate(arg, retval, "set auto comp disable", 0); + validate(arg, retval, "set auto comp disable", DEC); } #endif - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -2712,11 +2738,11 @@ int storage_cell_start(int file_des) { } else { retval = selectStoragecellStart(arg); FILE_LOG(logDEBUG1, ("Storage cell start: %d\n", retval)); - validate(arg, retval, "set storage cell start", 0); + validate(arg, retval, "set storage cell start", DEC); } } #endif - return Server_SendResult(file_des, INT32, 1, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -2782,7 +2808,7 @@ int check_version(int file_des) { FILE_LOG(logERROR,(mess)); } } - return Server_SendResult(file_des, INT32, 1 , NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } @@ -2806,6 +2832,6 @@ int software_trigger(int file_des) { FILE_LOG(logDEBUG1, ("Software trigger ret: %d\n", ret)); } #endif - return Server_SendResult(file_des, INT32, 1 , NULL, 0); + return Server_SendResult(file_des, INT32, UPDATE, NULL, 0); } diff --git a/slsDetectorServers/slsDetectorServer/slsDetectorServer_funcs.h b/slsDetectorServers/slsDetectorServer/slsDetectorServer_funcs.h index b37bf5aa7..9fcbc99e0 100755 --- a/slsDetectorServers/slsDetectorServer/slsDetectorServer_funcs.h +++ b/slsDetectorServers/slsDetectorServer/slsDetectorServer_funcs.h @@ -3,7 +3,6 @@ // initialization functions int printSocketReadError(); -void basictests(); void init_detector(); int decode_function(int); const char* getFunctionName(enum detFuncs func); diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 4d503d95e..e2a1b4f35 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -42,7 +42,7 @@ slsDetector::slsDetector(detectorType type, int multiId, int id, bool verify) // ensure shared memory was not created before auto shm = SharedMemory(multiId, id); if (shm.IsExisting()) { - FILE_LOG(logWARNING) << "Weird, this shared memory should have been " + FILE_LOG(logWARNING) << "This shared memory should have been " "deleted before! " << shm.GetName() << ". Freeing it again"; freeSharedMemory(multiId, id); } @@ -1402,12 +1402,6 @@ int slsDetector::updateDetectorNoWait() { thisDetector->timerValue[DELAY_AFTER_TRIGGER] = i64; } - if ((thisDetector->myDetectorType != JUNGFRAU) && - (thisDetector->myDetectorType != EIGER)) { - n += controlSocket->ReceiveDataOnly(&i64, sizeof(i64)); - thisDetector->timerValue[GATES_NUMBER] = i64; - } - n += controlSocket->ReceiveDataOnly(&i64, sizeof(i64)); thisDetector->timerValue[CYCLES_NUMBER] = i64; @@ -2060,7 +2054,7 @@ int slsDetector::configureMAC() { int ret = FAIL; char args[9][50]; memset(args, 0, sizeof(args)); - char retvals[3][50]; + char retvals[2][50]; memset(retvals, 0, sizeof(retvals)); FILE_LOG(logDEBUG1) << "Configuring MAC"; @@ -2186,30 +2180,30 @@ int slsDetector::configureMAC() { // get detectormac, detector ip uint64_t idetectormac = 0; uint32_t idetectorip = 0; - sscanf(retvals[1], "%lx", &idetectormac); - sscanf(retvals[2], "%x", &idetectorip); - sprintf(retvals[1],"%02x:%02x:%02x:%02x:%02x:%02x", + sscanf(retvals[0], "%lx", &idetectormac); + sscanf(retvals[1], "%x", &idetectorip); + sprintf(retvals[0],"%02x:%02x:%02x:%02x:%02x:%02x", (unsigned int)((idetectormac>>40)&0xFF), (unsigned int)((idetectormac>>32)&0xFF), (unsigned int)((idetectormac>>24)&0xFF), (unsigned int)((idetectormac>>16)&0xFF), (unsigned int)((idetectormac>>8)&0xFF), (unsigned int)((idetectormac>>0)&0xFF)); - sprintf(retvals[2],"%d.%d.%d.%d", + sprintf(retvals[1],"%d.%d.%d.%d", (idetectorip>>24)&0xff, (idetectorip>>16)&0xff, (idetectorip>>8)&0xff, (idetectorip)&0xff); // update if different - if (strcasecmp(retvals[1],thisDetector->detectorMAC)) { + if (strcasecmp(retvals[0],thisDetector->detectorMAC)) { memset(thisDetector->detectorMAC, 0, MAX_STR_LENGTH); - strcpy(thisDetector->detectorMAC, retvals[1]); + strcpy(thisDetector->detectorMAC, retvals[0]); FILE_LOG(logINFO) << detId << ": Detector MAC updated to " << thisDetector->detectorMAC; } - if (strcasecmp(retvals[2],thisDetector->detectorIP)) { + if (strcasecmp(retvals[1],thisDetector->detectorIP)) { memset(thisDetector->detectorIP, 0, MAX_STR_LENGTH); - strcpy(thisDetector->detectorIP, retvals[2]); + strcpy(thisDetector->detectorIP, retvals[1]); FILE_LOG(logINFO) << detId << ": Detector IP updated to " << thisDetector->detectorIP; } @@ -3364,11 +3358,12 @@ int slsDetector::digitalTest( digitalTestMode mode, int ival) { int slsDetector::loadImageToDetector(imageType index,std::string const fname) { int ret = FAIL; - short int args[thisDetector->nChans * thisDetector->nChips]; + int nChan = getTotalNumberOfChannels(); + short int args[nChan]; FILE_LOG(logDEBUG1) << "Loading " << (!index ? "Dark" : "Gain") << "image from file " << fname; - if (readDataFile(fname, args, getTotalNumberOfChannels())) { - ret = sendImageToDetector(index,args); + if (readDataFile(fname, args, nChan)) { + ret = sendImageToDetector(index, args); return ret; } @@ -3381,18 +3376,24 @@ int slsDetector::loadImageToDetector(imageType index,std::string const fname) { int slsDetector::sendImageToDetector(imageType index,short int imageVals[]) { int fnum = F_LOAD_IMAGE; int ret = FAIL; + int nChan = getTotalNumberOfChannels(); + int args[2] = {(int)index, nChan}; FILE_LOG(logDEBUG1) << "Sending image to detector"; if (thisDetector->onlineFlag == ONLINE_FLAG && connectControl() == OK) { - ret = thisDetectorControl->Client_Send(fnum, - imageVals, thisDetector->dataBytes, NULL, 0); - disconnectControl(); - - // handle ret - if (ret == FAIL) { - setErrorMask((getErrorMask())|(OTHER_ERROR_CODE)); - } else if (ret == FORCE_UPDATE) - ret = updateDetector(); + controlSocket->SendDataOnly(&fnum, sizeof(fnum)); + controlSocket->SendDataOnly(args, sizeof(args)); + controlSocket->SendDataOnly(imageVals, nChan * sizeof(short int)); + controlSocket->ReceiveDataOnly(&ret, sizeof(ret)); + if (ret == FAIL) { + char mess[MAX_STR_LENGTH] = {0}; + setErrorMask((getErrorMask())|(OTHER_ERROR_CODE)); + controlSocket->ReceiveDataOnly(mess, MAX_STR_LENGTH); + FILE_LOG(logERROR) << "Detector " << detId << " returned error: " << mess; + } + disconnectControl(); + if (ret == FORCE_UPDATE) + ret = updateDetector(); } return ret; } @@ -3400,7 +3401,8 @@ int slsDetector::sendImageToDetector(imageType index,short int imageVals[]) { int slsDetector::writeCounterBlockFile(std::string const fname,int startACQ) { int ret = FAIL; - short int retvals[thisDetector->nChans * thisDetector->nChips]; + int nChan = getTotalNumberOfChannels(); + short int retvals[nChan]; FILE_LOG(logDEBUG1) << "Reading Counter to " << fname << (startACQ ? " and Restarting Acquisition" : "\n"); @@ -3408,20 +3410,22 @@ int slsDetector::writeCounterBlockFile(std::string const fname,int startACQ) { if (ret == FAIL) setErrorMask((getErrorMask())|(OTHER_ERROR_CODE)); else - ret=writeDataFile(fname, getTotalNumberOfChannels(), retvals); + ret = writeDataFile(fname, nChan, retvals); return ret; } int slsDetector::getCounterBlock(short int image[],int startACQ) { - int fnum = F_READ_COUNTER_BLOCK; + int nChan = getTotalNumberOfChannels(); + int fnum = F_READ_COUNTER_BLOCK; int ret = FAIL; - int arg = startACQ; + int nChan = getTotalNumberOfChannels(); + int args[2] = {startACQ, nChan}; FILE_LOG(logDEBUG1) << "Reading Counter block with startacq: " << startACQ; if (thisDetector->onlineFlag == ONLINE_FLAG && connectControl() == OK) { ret = thisDetectorControl->Client_Send(fnum, - &arg, sizeof(arg), image, thisDetector->dataBytes); + args, sizeof(args), image, nChan * sizeof(short int)); disconnectControl(); // handle ret