diff --git a/slsDetectorServers/ctbDetectorServer/bin/ctbDetectorServer_developer b/slsDetectorServers/ctbDetectorServer/bin/ctbDetectorServer_developer index 875b82d1c..eee4c2dbe 100755 Binary files a/slsDetectorServers/ctbDetectorServer/bin/ctbDetectorServer_developer and b/slsDetectorServers/ctbDetectorServer/bin/ctbDetectorServer_developer differ diff --git a/slsDetectorServers/eigerDetectorServer/Beb.h b/slsDetectorServers/eigerDetectorServer/Beb.h index 4cb84b807..ea376facf 100644 --- a/slsDetectorServers/eigerDetectorServer/Beb.h +++ b/slsDetectorServers/eigerDetectorServer/Beb.h @@ -1,5 +1,6 @@ #pragma once +#include #include "LocalLinkInterface.h" #include "slsDetectorServer_defs.h" diff --git a/slsDetectorServers/eigerDetectorServer/bin/eigerDetectorServer_developer b/slsDetectorServers/eigerDetectorServer/bin/eigerDetectorServer_developer index 133044600..81b3189c1 100755 Binary files a/slsDetectorServers/eigerDetectorServer/bin/eigerDetectorServer_developer and b/slsDetectorServers/eigerDetectorServer/bin/eigerDetectorServer_developer differ diff --git a/slsDetectorServers/gotthard2DetectorServer/bin/gotthard2DetectorServer_developer b/slsDetectorServers/gotthard2DetectorServer/bin/gotthard2DetectorServer_developer index 746942b14..18247efe2 100755 Binary files a/slsDetectorServers/gotthard2DetectorServer/bin/gotthard2DetectorServer_developer and b/slsDetectorServers/gotthard2DetectorServer/bin/gotthard2DetectorServer_developer differ diff --git a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c index 89d21c85e..eec0389c1 100644 --- a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c @@ -217,9 +217,8 @@ int testBus() { int ret = OK; u_int32_t addr = DTA_OFFSET_REG; u_int32_t times = 1000 * 1000; - u_int32_t i = 0; - for (i = 0; i < times; ++i) { + for (u_int32_t i = 0; i < times; ++i) { bus_w(addr, i * 100); if (i * 100 != bus_r(addr)) { LOG(logERROR, @@ -375,26 +374,23 @@ void setupDetector() { delayReg = 0; numBurstsReg = 1; burstPeriodReg = 0; - { - int i, j; - for (i = 0; i < NUM_CLOCKS; ++i) { - clkPhase[i] = 0; + for (int i = 0; i < NUM_CLOCKS; ++i) { + clkPhase[i] = 0; + } + for (int i = 0; i < NDAC; ++i) { + dacValues[i] = 0; + } + for (int i = 0; i < ONCHIP_NDAC; ++i) { + for (int j = 0; j < NCHIP; ++j) { + onChipdacValues[i][j] = -1; } - for (i = 0; i < NDAC; ++i) { - dacValues[i] = 0; + } + for (int i = 0; i < NCHIP; ++i) { + for (int j = 0; j < NCHAN; ++j) { + vetoReference[i][j] = 0; } - for (i = 0; i < ONCHIP_NDAC; ++i) { - for (j = 0; j < NCHIP; ++j) { - onChipdacValues[i][j] = -1; - } - } - for (i = 0; i < NCHIP; ++i) { - for (j = 0; j < NCHAN; ++j) { - vetoReference[i][j] = 0; - } - for (j = 0; j < NADC; ++j) { - adcConfiguration[i][j] = 0; - } + for (int j = 0; j < NADC; ++j) { + adcConfiguration[i][j] = 0; } } #ifdef VIRTUAL @@ -647,9 +643,8 @@ int readConfigFile() { adcmax = iadc + 1; } - int i, j; - for (i = chipmin; i < chipmax; ++i) { - for (j = adcmin; j < adcmax; ++j) { + for (int i = chipmin; i < chipmax; ++i) { + for (int j = adcmin; j < adcmax; ++j) { adcConfiguration[i][j] = (uint8_t)value; ++nadcRead; } @@ -776,13 +771,10 @@ int readConfigFile() { nadcRead, NADC * NCHIP); } } - { - int i = 0, j = 0; - for (i = 0; i < NCHIP; ++i) { - for (j = 0; j < NADC; ++j) { - LOG(logDEBUG2, ("adc read %d %d: 0x%02hhx\n", i, j, - adcConfiguration[i][j])); - } + for (int i = 0; i < NCHIP; ++i) { + for (int j = 0; j < NADC; ++j) { + LOG(logDEBUG2, + ("adc read %d %d: 0x%02hhx\n", i, j, adcConfiguration[i][j])); } } @@ -1143,8 +1135,7 @@ int setOnChipDAC(enum ONCHIP_DACINDEX ind, int chipIndex, int val) { } // all chips if (chipIndex == -1) { - int ichip = 0; - for (ichip = 0; ichip < NCHIP; ++ichip) { + for (int ichip = 0; ichip < NCHIP; ++ichip) { onChipdacValues[ind][ichip] = val; } } @@ -1160,9 +1151,8 @@ int getOnChipDAC(enum ONCHIP_DACINDEX ind, int chipIndex) { // all chips if (chipIndex == -1) { int retval = onChipdacValues[ind][0]; - int ichip = 0; // check if same value for remaining chips - for (ichip = 1; ichip < NCHIP; ++ichip) { + for (int ichip = 1; ichip < NCHIP; ++ichip) { if (onChipdacValues[ind][ichip] != retval) { return -1; } @@ -1641,8 +1631,7 @@ int setClockDivider(enum CLKINDEX ind, int val) { // Remembering old phases in degrees int oldPhases[NUM_CLOCKS]; { - int i = 0; - for (i = 0; i < NUM_CLOCKS; ++i) { + for (int i = 0; i < NUM_CLOCKS; ++i) { oldPhases[i] = getPhase(i, 1); LOG(logDEBUG1, ("\tRemembering %s clock (%d) phase: %d degrees\n", clock_names[ind], ind, oldPhases[i])); @@ -1673,16 +1662,13 @@ int setClockDivider(enum CLKINDEX ind, int val) { } // set the phase in degrees (reset by pll) - { - int i = 0; - for (i = 0; i < NUM_CLOCKS; ++i) { - int currPhaseDeg = getPhase(i, 1); - if (oldPhases[i] != currPhaseDeg) { - LOG(logINFO, - ("\tCorrecting %s clock (%d) phase from %d to %d degrees\n", - clock_names[i], i, currPhaseDeg, oldPhases[i])); - setPhase(i, oldPhases[i], 1); - } + for (int i = 0; i < NUM_CLOCKS; ++i) { + int currPhaseDeg = getPhase(i, 1); + if (oldPhases[i] != currPhaseDeg) { + LOG(logINFO, + ("\tCorrecting %s clock (%d) phase from %d to %d degrees\n", + clock_names[i], i, currPhaseDeg, oldPhases[i])); + setPhase(i, oldPhases[i], 1); } } return OK; @@ -1711,8 +1697,8 @@ int setInjectChannel(int offset, int increment) { char buffer[17]; memset(buffer, 0, sizeof(buffer)); int startCh = 4; // 4 due to padding - int ich = 0; - for (ich = startCh + offset; ich < startCh + NCHAN; ich = ich + increment) { + for (int ich = startCh + offset; ich < startCh + NCHAN; + ich = ich + increment) { int byteIndex = ich / 8; int bitIndex = ich % 8; buffer[byteIndex] |= (1 << (8 - 1 - bitIndex)); @@ -1741,8 +1727,7 @@ int setVetoReference(int gainIndex, int value) { gainIndex, value)); int vals[NCHAN]; memset(vals, 0, sizeof(vals)); - int ich = 0; - for (ich = 0; ich < NCHAN; ++ich) { + for (int ich = 0; ich < NCHAN; ++ich) { vals[ich] = value; } return setVetoPhoton(-1, gainIndex, vals); @@ -1770,8 +1755,7 @@ int setVetoPhoton(int chipIndex, int gainIndex, int *values) { return FAIL; } LOG(logDEBUG2, ("Adding gain bits\n")); - int i = 0; - for (i = 0; i < NCHAN; ++i) { + for (int i = 0; i < NCHAN; ++i) { values[i] |= gainValue; LOG(logDEBUG2, ("Value %d: 0x%x\n", i, values[i])); } @@ -1787,8 +1771,7 @@ int setVetoPhoton(int chipIndex, int gainIndex, int *values) { uint8_t commandBytes[lenTotalBits]; memset(commandBytes, 0, sizeof(commandBytes)); int offset = padding; // bit offset for commandbytes - int ich = 0; - for (ich = 0; ich < NCHAN; ++ich) { + for (int ich = 0; ich < NCHAN; ++ich) { // loop through all bits in a value int iBit = 0; for (iBit = 0; iBit < lenDataBitsPerchannel; ++iBit) { @@ -1803,10 +1786,9 @@ int setVetoPhoton(int chipIndex, int gainIndex, int *values) { memset(buffer, 0, len); offset = 0; // loop through buffer elements - for (ich = 0; ich < len; ++ich) { + for (int ich = 0; ich < len; ++ich) { // loop through each bit in buffer element - int iBit = 0; - for (iBit = 0; iBit < 8; ++iBit) { + for (int iBit = 0; iBit < 8; ++iBit) { buffer[ich] |= (commandBytes[offset++] << (8 - 1 - iBit)); } } @@ -1820,10 +1802,8 @@ int setVetoPhoton(int chipIndex, int gainIndex, int *values) { // all chips if (chipIndex == -1) { - int ichip = 0; - int ichan = 0; - for (ichan = 0; ichan < NCHAN; ++ichan) { - for (ichip = 0; ichip < NCHIP; ++ichip) { + for (int ichan = 0; ichan < NCHAN; ++ichan) { + for (int ichip = 0; ichip < NCHIP; ++ichip) { vetoReference[ichip][ichan] = values[ichan]; } } @@ -1831,8 +1811,7 @@ int setVetoPhoton(int chipIndex, int gainIndex, int *values) { // specific chip else { - int ichan = 0; - for (ichan = 0; ichan < NCHAN; ++ichan) { + for (int ichan = 0; ichan < NCHAN; ++ichan) { vetoReference[chipIndex][chipIndex] = values[ichan]; ; } @@ -1842,10 +1821,9 @@ int setVetoPhoton(int chipIndex, int gainIndex, int *values) { int getVetoPhoton(int chipIndex, int *retvals) { if (chipIndex == -1) { - int i = 0, j = 0; - for (i = 0; i < NCHAN; ++i) { + for (int i = 0; i < NCHAN; ++i) { int val = vetoReference[0][i]; - for (j = 1; j < NCHIP; ++j) { + for (int j = 1; j < NCHIP; ++j) { if (vetoReference[j][i] != val) { LOG(logERROR, ("Get vet photon fail for chipIndex:%d. Different " @@ -1876,14 +1854,11 @@ int configureSingleADCDriver(int chipIndex) { memcpy(values, adcConfiguration + ind * NADC, NADC); // change adc values if continuous mode - { - int i = 0; - for (i = 0; i < NADC; ++i) { - if (burstMode == BURST_OFF) { - values[i] |= ASIC_CONTINUOUS_MODE_MSK; - } - LOG(logDEBUG2, ("Value %d: 0x%02hhx\n", i, values[i])); + for (int i = 0; i < NADC; ++i) { + if (burstMode == BURST_OFF) { + values[i] |= ASIC_CONTINUOUS_MODE_MSK; } + LOG(logDEBUG2, ("Value %d: 0x%02hhx\n", i, values[i])); } const int lenDataBitsPerADC = ASIC_ADC_MAX_BITS; // 7 @@ -1896,11 +1871,9 @@ int configureSingleADCDriver(int chipIndex) { uint8_t commandBytes[lenTotalBits]; memset(commandBytes, 0, sizeof(commandBytes)); int offset = padding; // bit offset for commandbytes - int ich = 0; - for (ich = 0; ich < NADC; ++ich) { + for (int ich = 0; ich < NADC; ++ich) { // loop through all bits in a value - int iBit = 0; - for (iBit = 0; iBit < lenDataBitsPerADC; ++iBit) { + for (int iBit = 0; iBit < lenDataBitsPerADC; ++iBit) { commandBytes[offset++] = ((values[ich] >> (lenDataBitsPerADC - 1 - iBit)) & 0x1); } @@ -1912,10 +1885,9 @@ int configureSingleADCDriver(int chipIndex) { memset(buffer, 0, len); offset = 0; // loop through buffer elements - for (ich = 0; ich < len; ++ich) { + for (int ich = 0; ich < len; ++ich) { // loop through each bit in buffer element - int iBit = 0; - for (iBit = 0; iBit < 8; ++iBit) { + for (int iBit = 0; iBit < 8; ++iBit) { buffer[ich] |= (commandBytes[offset++] << (8 - 1 - iBit)); } } @@ -1934,23 +1906,19 @@ int configureADC() { LOG(logINFO, ("Configuring ADC \n")); int equal = 1; - { - int i = 0, j = 0; - for (i = 0; i < NADC; ++i) { - int val = adcConfiguration[0][i]; - for (j = 1; j < NCHIP; ++j) { - if (adcConfiguration[j][i] != val) { - equal = 0; - break; - } + for (int i = 0; i < NADC; ++i) { + int val = adcConfiguration[0][i]; + for (int j = 1; j < NCHIP; ++j) { + if (adcConfiguration[j][i] != val) { + equal = 0; + break; } } } if (equal) { return configureSingleADCDriver(-1); } else { - int i = 0; - for (i = 0; i < NCHIP; ++i) { + for (int i = 0; i < NCHIP; ++i) { if (configureSingleADCDriver(i) == FAIL) { return FAIL; } @@ -2075,10 +2043,8 @@ int setBurstMode(enum burstMode burst) { uint8_t commandBytes[lenTotalBits]; memset(commandBytes, 0, sizeof(commandBytes)); int offset = padding; // bit offset for commandbytes - int ich = 0; // loop through all bits in a value - int iBit = 0; - for (iBit = 0; iBit < ASIC_GLOBAL_SETT_MAX_BITS; ++iBit) { + for (int iBit = 0; iBit < ASIC_GLOBAL_SETT_MAX_BITS; ++iBit) { commandBytes[offset++] = ((value >> (ASIC_GLOBAL_SETT_MAX_BITS - 1 - iBit)) & 0x1); } @@ -2089,10 +2055,9 @@ int setBurstMode(enum burstMode burst) { memset(buffer, 0, len); offset = 0; // loop through buffer elements - for (ich = 0; ich < len; ++ich) { + for (int ich = 0; ich < len; ++ich) { // loop through each bit in buffer element - int iBit = 0; - for (iBit = 0; iBit < 8; ++iBit) { + for (int iBit = 0; iBit < 8; ++iBit) { buffer[ich] |= (commandBytes[offset++] << (8 - 1 - iBit)); } } @@ -2235,25 +2200,20 @@ void *start_timer(void *arg) { // Generate data char imageData[imagesize]; memset(imageData, 0, imagesize); - { - int i = 0; - for (i = 0; i < imagesize; i += sizeof(uint16_t)) { - *((uint16_t *)(imageData + i)) = i; - } + for (int i = 0; i < imagesize; i += sizeof(uint16_t)) { + *((uint16_t *)(imageData + i)) = i; } { - int repeatNr = 0; int frameHeaderNr = 0; // loop over number of repeats - for (repeatNr = 0; repeatNr != numRepeats; ++repeatNr) { + for (int repeatNr = 0; repeatNr != numRepeats; ++repeatNr) { struct timespec rbegin, rend; clock_gettime(CLOCK_REALTIME, &rbegin); - int frameNr = 0; // loop over number of frames - for (frameNr = 0; frameNr != numFrames; ++frameNr) { + for (int frameNr = 0; frameNr != numFrames; ++frameNr) { // update the virtual stop from stop server virtual_stop = ComVirtual_getStop(); diff --git a/slsDetectorServers/gotthardDetectorServer/bin/gotthardDetectorServer_developer b/slsDetectorServers/gotthardDetectorServer/bin/gotthardDetectorServer_developer index 051b2278e..d0a15d1ff 100755 Binary files a/slsDetectorServers/gotthardDetectorServer/bin/gotthardDetectorServer_developer and b/slsDetectorServers/gotthardDetectorServer/bin/gotthardDetectorServer_developer differ diff --git a/slsDetectorServers/jungfrauDetectorServer/bin/jungfrauDetectorServer_developer b/slsDetectorServers/jungfrauDetectorServer/bin/jungfrauDetectorServer_developer index 15ace9d7d..c0bc273bc 100755 Binary files a/slsDetectorServers/jungfrauDetectorServer/bin/jungfrauDetectorServer_developer and b/slsDetectorServers/jungfrauDetectorServer/bin/jungfrauDetectorServer_developer differ diff --git a/slsDetectorServers/moenchDetectorServer/bin/moenchDetectorServer_developer b/slsDetectorServers/moenchDetectorServer/bin/moenchDetectorServer_developer index ea88e3726..5aa70142d 100755 Binary files a/slsDetectorServers/moenchDetectorServer/bin/moenchDetectorServer_developer and b/slsDetectorServers/moenchDetectorServer/bin/moenchDetectorServer_developer differ diff --git a/slsDetectorServers/mythen3DetectorServer/Makefile b/slsDetectorServers/mythen3DetectorServer/Makefile index de03fb554..34184c7f7 100755 --- a/slsDetectorServers/mythen3DetectorServer/Makefile +++ b/slsDetectorServers/mythen3DetectorServer/Makefile @@ -5,7 +5,7 @@ support_lib = ../../slsSupportLib/include/ CROSS = nios2-buildroot-linux-gnu- CC = $(CROSS)gcc -CFLAGS += -Wall -DMYTHEN3D -DSTOP_SERVER -I$(main_inc) -I$(support_lib) -I$(current_dir) -DDEBUG1 #-DVERBOSEI #-DVERBOSE +CFLAGS += -Wall -DMYTHEN3D -DSTOP_SERVER -I$(main_inc) -I$(support_lib) -I$(current_dir) #-DDEBUG1 #-DVERBOSEI #-DVERBOSE LDLIBS += -lm PROGS = mythen3DetectorServer DESTDIR ?= bin diff --git a/slsDetectorServers/mythen3DetectorServer/bin/mythen3DetectorServer_developer b/slsDetectorServers/mythen3DetectorServer/bin/mythen3DetectorServer_developer index 9fd407e77..7c51e2460 100755 Binary files a/slsDetectorServers/mythen3DetectorServer/bin/mythen3DetectorServer_developer and b/slsDetectorServers/mythen3DetectorServer/bin/mythen3DetectorServer_developer differ diff --git a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c index 06fbb3d5c..80675cf22 100644 --- a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c @@ -39,11 +39,15 @@ int virtual_status = 0; int virtual_stop = 0; #endif +sls_detector_module *detectorModules = NULL; +int *detectorChans = NULL; +int *detectorDacs = NULL; + +enum TLogLevel trimmingPrint = logINFO; int32_t clkPhase[NUM_CLOCKS] = {}; uint32_t clkDivider[NUM_CLOCKS] = {}; int highvoltage = 0; -int dacValues[NDAC] = {}; int detPos[2] = {}; uint32_t countermask = 0; // will be removed later when in firmware converted to mask @@ -204,9 +208,8 @@ int testBus() { int ret = OK; u_int32_t addr = DTA_OFFSET_REG; u_int32_t times = 1000 * 1000; - u_int32_t i = 0; - for (i = 0; i < times; ++i) { + for (u_int32_t i = 0; i < times; ++i) { bus_w(addr, i * 100); if (i * 100 != bus_r(addr)) { LOG(logERROR, @@ -340,9 +343,42 @@ void initStopServer() { /* set up detector */ +void allocateDetectorStructureMemory() { + // Allocation of memory + detectorModules = malloc(sizeof(sls_detector_module)); + detectorChans = malloc(NCHIP * NCHAN * sizeof(int)); + detectorDacs = malloc(NDAC * sizeof(int)); + LOG(logDEBUG1, + ("modules from 0x%x to 0x%x\n", detectorModules, detectorModules)); + LOG(logDEBUG1, ("chans from 0x%x to 0x%x\n", detectorChans, detectorChans)); + LOG(logDEBUG1, ("dacs from 0x%x to 0x%x\n", detectorDacs, detectorDacs)); + (detectorModules)->dacs = detectorDacs; + (detectorModules)->chanregs = detectorChans; + (detectorModules)->ndac = NDAC; + (detectorModules)->nchip = NCHIP; + (detectorModules)->nchan = NCHIP * NCHAN; + (detectorModules)->reg = UNINITIALIZED; + (detectorModules)->iodelay = 0; + (detectorModules)->tau = 0; + (detectorModules)->eV = 0; + // thisSettings = UNINITIALIZED; + + // initialize dacs + for (int idac = 0; idac < (detectorModules)->ndac; ++idac) { + detectorDacs[idac] = 0; + } + + // if trimval requested, should return -1 to acknowledge unknown + for (int ichan = 0; ichan < (detectorModules->nchan); ichan++) { + *((detectorModules->chanregs) + ichan) = -1; + } +} + void setupDetector() { LOG(logINFO, ("This Server is for 1 Mythen3 module \n")); + allocateDetectorStructureMemory(); + clkDivider[READOUT_C0] = DEFAULT_READOUT_C0; clkDivider[READOUT_C1] = DEFAULT_READOUT_C1; clkDivider[SYSTEM_C0] = DEFAULT_SYSTEM_C0; @@ -350,14 +386,9 @@ void setupDetector() { clkDivider[SYSTEM_C2] = DEFAULT_SYSTEM_C2; highvoltage = 0; - { - int i; - for (i = 0; i < NUM_CLOCKS; ++i) { - clkPhase[i] = 0; - } - for (i = 0; i < NDAC; ++i) { - dacValues[i] = 0; - } + trimmingPrint = logINFO; + for (int i = 0; i < NUM_CLOCKS; ++i) { + clkPhase[i] = 0; } #ifdef VIRTUAL virtual_status = 0; @@ -404,10 +435,13 @@ int setDefaultDacs() { int ret = OK; LOG(logINFOBLUE, ("Setting Default Dac values\n")); { - int i = 0; const int defaultvals[NDAC] = DEFAULT_DAC_VALS; - for (i = 0; i < NDAC; ++i) { + for (int i = 0; i < NDAC; ++i) { setDAC((enum DACINDEX)i, defaultvals[i], 0); + if (detectorDacs[i] != defaultvals[i]) { + LOG(logERROR, ("Setting dac %d failed, wrote %d, read %d\n", i, + defaultvals[i], detectorDacs[i])); + } } } return ret; @@ -484,7 +518,251 @@ int setDynamicRange(int dr) { } } -/* parameters - speed, readout */ +/* parameters - module, speed, readout */ + +int setModule(sls_detector_module myMod, char *mess) { + + LOG(logINFO, ("Setting module\n")); + + /* future implementation + // settings (not yet implemented) + setSettings((enum detectorSettings)myMod.reg); + if (myMod.reg >= 0) { + detectorModules->reg = myMod.reg; + } + + // threshold + if (myMod.eV >= 0) + setThresholdEnergy(myMod.eV); + else { + // (loading a random trim file) (dont return fail) + setSettings(UNDEFINED); + LOG(logERROR, + ("Settings has been changed to undefined (random trim + file)\n")); + } + */ + + // dacs + for (int i = 0; i < NDAC; ++i) { + // ignore dacs with -1 + if (myMod.dacs[i] != -1) { + setDAC((enum DACINDEX)i, myMod.dacs[i], 0); + if (myMod.dacs[i] != detectorDacs[i]) { + sprintf(mess, "Could not set module. Could not set dac %d\n", + i); + LOG(logERROR, (mess)); + // setSettings(UNDEFINED); + // LOG(logERROR, ("Settings has been changed to undefined\n")); + return FAIL; + } + } + } + + // trimbits + if (myMod.nchan == 0) { + LOG(logINFO, ("Setting module without trimbits\n")); + } else { + // set trimbits + if (setTrimbits(myMod.chanregs) == FAIL) { + sprintf(mess, "Could not set module. Could not set trimbits\n"); + LOG(logERROR, (mess)); + // setSettings(UNDEFINED); + // LOG(logERROR, ("Settings has been changed to undefined (random " + // "trim file)\n")); + return FAIL; + } + } + + return OK; +} + +int getModule(sls_detector_module *myMod) { + // copy local module to myMod + if (detectorModules) { + if (copyModule(myMod, detectorModules) == FAIL) + return FAIL; + } else + return FAIL; + return OK; +} + +int setBit(int ibit, int patword) { return patword |= (1 << ibit); } + +int clearBit(int ibit, int patword) { return patword &= ~(1 << ibit); } + +int setTrimbits(int *trimbits) { + LOG(logINFOBLUE, ("Setting trimbits\n")); + + // validate + for (int ichan = 0; ichan < ((detectorModules)->nchan); ++ichan) { + if (trimbits[ichan] < 0 || trimbits[ichan] > 63) { + LOG(logERROR, ("Trimbit value (%d) for channel %d is invalid\n", + trimbits[ichan], ichan)); + return FAIL; + } + } + LOG(logINFO, ("Trimbits validated\n")); + trimmingPrint = logDEBUG5; + + uint64_t patword = 0; + int iaddr = 0; + for (int ichip = 0; ichip < NCHIP; ichip++) { + LOG(logDEBUG1, (" Chip %d\n", ichip)); + iaddr = 0; + patword = 0; + writePatternWord(iaddr++, patword); + + // chip select + patword = setBit(SIGNAL_TBLoad_1 + ichip, patword); + writePatternWord(iaddr++, patword); + + // reset trimbits + patword = setBit(SIGNAL_resStorage, patword); + patword = setBit(SIGNAL_resCounter, patword); + writePatternWord(iaddr++, patword); + writePatternWord(iaddr++, patword); + patword = clearBit(SIGNAL_resStorage, patword); + patword = clearBit(SIGNAL_resCounter, patword); + writePatternWord(iaddr++, patword); + writePatternWord(iaddr++, patword); + + // select first channel + patword = setBit(SIGNAL_CHSserialIN, patword); + writePatternWord(iaddr++, patword); + // 1 clk pulse + patword = setBit(SIGNAL_CHSclk, patword); + writePatternWord(iaddr++, patword); + patword = clearBit(SIGNAL_CHSclk, patword); + // clear 1st channel + writePatternWord(iaddr++, patword); + patword = clearBit(SIGNAL_CHSserialIN, patword); + // 2 clk pulses + for (int i = 0; i < 2; i++) { + patword = setBit(SIGNAL_CHSclk, patword); + writePatternWord(iaddr++, patword); + patword = clearBit(SIGNAL_CHSclk, patword); + writePatternWord(iaddr++, patword); + } + + // for each channel (all chips) + for (int ich = 0; ich < NCHAN_1_COUNTER; ich++) { + LOG(logDEBUG1, (" Chip %d, Channel %d\n", ichip, ich)); + int val = trimbits[ichip * NCHAN_1_COUNTER * NCOUNTERS + + NCOUNTERS * ich] + + trimbits[ichip * NCHAN_1_COUNTER * NCOUNTERS + + NCOUNTERS * ich + 1] * + 64 + + trimbits[ichip * NCHAN_1_COUNTER * NCOUNTERS + + NCOUNTERS * ich + 2] * + 64 * 64; + + // push 6 0 bits + for (int i = 0; i < 6; i++) { + patword = clearBit(SIGNAL_serialIN, patword); + patword = clearBit(SIGNAL_clk, patword); + writePatternWord(iaddr++, patword); + patword = setBit(SIGNAL_clk, patword); + writePatternWord(iaddr++, patword); + } + + // deserialize + for (int i = 0; i < 18; i++) { + if (val & (1 << i)) { + patword = setBit(SIGNAL_serialIN, patword); + } else { + patword = clearBit(SIGNAL_serialIN, patword); + } + patword = clearBit(SIGNAL_clk, patword); + writePatternWord(iaddr++, patword); + + patword = setBit(SIGNAL_clk, patword); + writePatternWord(iaddr++, patword); + } + writePatternWord(iaddr++, patword); + writePatternWord(iaddr++, patword); + + // move to next channel + for (int i = 0; i < 3; i++) { + patword = setBit(SIGNAL_CHSclk, patword); + writePatternWord(iaddr++, patword); + patword = clearBit(SIGNAL_CHSclk, patword); + writePatternWord(iaddr++, patword); + } + } + // chip unselect + patword = clearBit(SIGNAL_TBLoad_1 + ichip, patword); + writePatternWord(iaddr++, patword); + + // last iaddr check + if (iaddr >= MAX_PATTERN_LENGTH) { + LOG(logERROR, ("Addr 0x%x is past max_address_length 0x%x!\n", + iaddr, MAX_PATTERN_LENGTH)); + trimmingPrint = logINFO; + return FAIL; + } + + // set pattern wait address + for (int i = 0; i <= 2; i++) + setPatternWaitAddress(i, MAX_PATTERN_LENGTH - 1); + + // pattern loop + for (int i = 0; i <= 2; i++) { + int stop = MAX_PATTERN_LENGTH - 1, nloop = 0; + setPatternLoop(i, &stop, &stop, &nloop); + } + + // pattern limits + { + int start = 0, nloop = 0; + setPatternLoop(-1, &start, &iaddr, &nloop); + } + // load the trimbits +#ifndef VIRTUAL + startStateMachine(); +#endif + } + + // copy trimbits locally + for (int ichan = 0; ichan < ((detectorModules)->nchan); ++ichan) { + detectorChans[ichan] = trimbits[ichan]; + } + trimmingPrint = logINFO; + LOG(logINFO, ("All trimbits have been loaded\n")); + return OK; +} + +int setAllTrimbits(int val) { + int *trimbits = malloc(sizeof(int) * ((detectorModules)->nchan)); + for (int ichan = 0; ichan < ((detectorModules)->nchan); ++ichan) { + trimbits[ichan] = val; + } + if (setTrimbits(trimbits) == FAIL) { + LOG(logERROR, ("Could not set all trimbits to %d\n", val)); + free(trimbits); + return FAIL; + } + // setSettings(UNDEFINED); + // LOG(logERROR, ("Settings has been changed to undefined (random " + // "trim file)\n")); + LOG(logINFO, ("All trimbits have been set to %d\n", val)); + free(trimbits); + return OK; +} + +int getAllTrimbits() { + int value = detectorChans[0]; + if (detectorModules) { + for (int ichan = 0; ichan < ((detectorModules)->nchan); ichan++) { + if (detectorChans[ichan] != value) { + value = -1; + break; + } + } + } + LOG(logINFO, ("Value of all Trimbits: %d\n", value)); + return value; +} void setNumFrames(int64_t val) { if (val > 0) { @@ -686,29 +964,29 @@ void setDAC(enum DACINDEX ind, int val, int mV) { LOG(logINFO, ("Setting dac[%d - %s]: %d %s \n", (int)ind, dac_names[ind], val, (mV ? "mV" : "dac units"))); if (!mV) { - dacValues[ind] = val; + detectorDacs[ind] = val; } // convert to dac units else if (LTC2620_D_VoltageToDac(val, &dacval) == OK) { - dacValues[ind] = dacval; + detectorDacs[ind] = dacval; } #else if (LTC2620_D_SetDACValue((int)ind, val, mV, dac_names[ind], &dacval) == OK) { - dacValues[ind] = dacval; + detectorDacs[ind] = dacval; } #endif } int getDAC(enum DACINDEX ind, int mV) { if (!mV) { - LOG(logDEBUG1, ("Getting DAC %d : %d dac\n", ind, dacValues[ind])); - return dacValues[ind]; + LOG(logDEBUG1, ("Getting DAC %d : %d dac\n", ind, detectorDacs[ind])); + return detectorDacs[ind]; } int voltage = -1; - LTC2620_D_DacToVoltage(dacValues[ind], &voltage); + LTC2620_D_DacToVoltage(detectorDacs[ind], &voltage); LOG(logDEBUG1, - ("Getting DAC %d : %d dac (%d mV)\n", ind, dacValues[ind], voltage)); + ("Getting DAC %d : %d dac (%d mV)\n", ind, detectorDacs[ind], voltage)); return voltage; } @@ -918,7 +1196,7 @@ uint64_t readPatternWord(int addr) { return -1; } - LOG(logINFO, (" Reading Pattern Word (addr:0x%x)\n", addr)); + LOG(trimmingPrint, (" Reading Pattern Word (addr:0x%x)\n", addr)); uint32_t reg_lsb = PATTERN_STEP0_LSB_REG + addr * REG_OFFSET * 2; // the first word in RAM as base plus the offset @@ -934,6 +1212,7 @@ uint64_t readPatternWord(int addr) { } uint64_t writePatternWord(int addr, uint64_t word) { + // get if ((int64_t)word == -1) return readPatternWord(addr); @@ -945,25 +1224,23 @@ uint64_t writePatternWord(int addr, uint64_t word) { addr, MAX_PATTERN_LENGTH)); return -1; } + LOG(trimmingPrint, ("Setting Pattern Word (addr:0x%x, word:0x%llx)\n", addr, + (long long int)word)); - LOG(logINFO, ("Setting Pattern Word (addr:0x%x, word:0x%llx)\n", addr, - (long long int)word)); + // write word uint32_t reg_lsb = PATTERN_STEP0_LSB_REG + addr * REG_OFFSET * 2; // the first word in RAM as base plus the offset // of the word to write (addr) uint32_t reg_msb = PATTERN_STEP0_MSB_REG + addr * REG_OFFSET * 2; - - // write word set64BitReg(word, reg_lsb, reg_msb); + LOG(logDEBUG1, (" Wrote word. PatternIn Reg: 0x%llx\n", get64BitReg(reg_lsb, reg_msb))); - return readPatternWord(addr); } int setPatternWaitAddress(int level, int addr) { - // error (handled in tcp) if (addr >= MAX_PATTERN_LENGTH) { LOG(logERROR, ("Cannot set Pattern Wait Address. Invalid addr 0x%x. " @@ -1001,8 +1278,9 @@ int setPatternWaitAddress(int level, int addr) { // set if (addr >= 0) { - LOG(logINFO, ("Setting Pattern Wait Address (level:%d, addr:0x%x)\n", - level, addr)); + LOG(trimmingPrint, + ("Setting Pattern Wait Address (level:%d, addr:0x%x)\n", level, + addr)); bus_w(reg, ((addr << offset) & mask)); } @@ -1039,8 +1317,8 @@ uint64_t setPatternWaitTime(int level, uint64_t t) { // set if ((int64_t)t >= 0) { - LOG(logINFO, ("Setting Pattern Wait Time (level:%d, t:%lld)\n", level, - (long long int)t)); + LOG(trimmingPrint, ("Setting Pattern Wait Time (level:%d, t:%lld)\n", + level, (long long int)t)); set64BitReg(t, regl, regm); } @@ -1059,6 +1337,10 @@ void setPatternLoop(int level, int *startAddr, int *stopAddr, int *nLoop) { "stopaddr:0x%x) must be " "less than 0x%x\n", *startAddr, *stopAddr, MAX_PATTERN_LENGTH)); + *startAddr = -1; + *stopAddr = -1; + *nLoop = -1; + return; } uint32_t addr = 0; @@ -1116,7 +1398,7 @@ void setPatternLoop(int level, int *startAddr, int *stopAddr, int *nLoop) { if (level >= 0) { // set iteration if (*nLoop >= 0) { - LOG(logINFO, + LOG(trimmingPrint, ("Setting Pattern Loop (level:%d, nLoop:%d)\n", level, *nLoop)); bus_w(nLoopReg, *nLoop); } @@ -1126,12 +1408,11 @@ void setPatternLoop(int level, int *startAddr, int *stopAddr, int *nLoop) { // set if (*startAddr >= 0 && *stopAddr >= 0) { // writing start and stop addr - LOG(logINFO, - ("Setting Pattern Loop (level:%d, startaddr:0x%x, stopaddr:0x%x)\n", - level, *startAddr, *stopAddr)); + LOG(trimmingPrint, ("Setting Pattern Loop (level:%d, startaddr:0x%x, " + "stopaddr:0x%x)\n", + level, *startAddr, *stopAddr)); bus_w(addr, ((*startAddr << startOffset) & startMask) | ((*stopAddr << stopOffset) & stopMask)); - LOG(logDEBUG1, ("Addr:0x%x, val:0x%x\n", addr, bus_r(addr))); } // get @@ -1352,13 +1633,10 @@ int setClockDivider(enum CLKINDEX ind, int val) { // Remembering old phases in degrees int oldPhases[NUM_CLOCKS]; - { - int i = 0; - for (i = 0; i < NUM_CLOCKS; ++i) { - oldPhases[i] = getPhase(i, 1); - LOG(logDEBUG1, ("\tRemembering %s clock (%d) phase: %d degrees\n", - clock_names[ind], ind, oldPhases[i])); - } + for (int i = 0; i < NUM_CLOCKS; ++i) { + oldPhases[i] = getPhase(i, 1); + LOG(logDEBUG1, ("\tRemembering %s clock (%d) phase: %d degrees\n", + clock_names[ind], ind, oldPhases[i])); } // Calculate and set output frequency @@ -1380,16 +1658,13 @@ int setClockDivider(enum CLKINDEX ind, int val) { } // set the phase in degrees (reset by pll) - { - int i = 0; - for (i = 0; i < NUM_CLOCKS; ++i) { - int currPhaseDeg = getPhase(i, 1); - if (oldPhases[i] != currPhaseDeg) { - LOG(logINFO, - ("\tCorrecting %s clock (%d) phase from %d to %d degrees\n", - clock_names[i], i, currPhaseDeg, oldPhases[i])); - setPhase(i, oldPhases[i], 1); - } + for (int i = 0; i < NUM_CLOCKS; ++i) { + int currPhaseDeg = getPhase(i, 1); + if (oldPhases[i] != currPhaseDeg) { + LOG(logINFO, + ("\tCorrecting %s clock (%d) phase from %d to %d degrees\n", + clock_names[i], i, currPhaseDeg, oldPhases[i])); + setPhase(i, oldPhases[i], 1); } } return OK; @@ -1462,68 +1737,58 @@ void *start_timer(void *arg) { // Generate data char imageData[imagesize]; memset(imageData, 0, imagesize); - { - int i = 0; - for (i = 0; i < imagesize; i += sizeof(uint8_t)) { - *((uint8_t *)(imageData + i)) = i; - } + for (int i = 0; i < imagesize; i += sizeof(uint8_t)) { + *((uint8_t *)(imageData + i)) = i; } // Send data - { - int frameNr = 1; - // loop over number of frames - for (frameNr = 0; frameNr != numFrames; ++frameNr) { + // loop over number of frames + for (int frameNr = 0; frameNr != numFrames; ++frameNr) { - // update the virtual stop from stop server - virtual_stop = ComVirtual_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { - break; - } + // update the virtual stop from stop server + virtual_stop = ComVirtual_getStop(); + // check if virtual_stop is high + if (virtual_stop == 1) { + break; + } - // sleep for exposure time - struct timespec begin, end; - clock_gettime(CLOCK_REALTIME, &begin); - usleep(expUs); + // sleep for exposure time + struct timespec begin, end; + clock_gettime(CLOCK_REALTIME, &begin); + usleep(expUs); - int srcOffset = 0; - // loop packet - { - int i = 0; - for (i = 0; i != PACKETS_PER_FRAME; ++i) { - char packetData[packetSize]; - memset(packetData, 0, packetSize); + int srcOffset = 0; + // loop packet + for (int i = 0; i != PACKETS_PER_FRAME; ++i) { + char packetData[packetSize]; + memset(packetData, 0, packetSize); - // set header - sls_detector_header *header = - (sls_detector_header *)(packetData); - header->detType = (uint16_t)myDetectorType; - header->version = SLS_DETECTOR_HEADER_VERSION - 1; - header->frameNumber = frameNr + 1; - header->packetNumber = i; - header->modId = 0; - header->row = detPos[X]; - header->column = detPos[Y]; + // set header + sls_detector_header *header = (sls_detector_header *)(packetData); + header->detType = (uint16_t)myDetectorType; + header->version = SLS_DETECTOR_HEADER_VERSION - 1; + header->frameNumber = frameNr + 1; + header->packetNumber = i; + header->modId = 0; + header->row = detPos[X]; + header->column = detPos[Y]; - // fill data - memcpy(packetData + sizeof(sls_detector_header), - imageData + srcOffset, dataSize); - srcOffset += dataSize; + // fill data + memcpy(packetData + sizeof(sls_detector_header), + imageData + srcOffset, dataSize); + srcOffset += dataSize; - sendUDPPacket(0, packetData, packetSize); - } - } - LOG(logINFO, ("Sent frame: %d\n", frameNr)); - clock_gettime(CLOCK_REALTIME, &end); - int64_t timeNs = ((end.tv_sec - begin.tv_sec) * 1E9 + - (end.tv_nsec - begin.tv_nsec)); + sendUDPPacket(0, packetData, packetSize); + } + LOG(logINFO, ("Sent frame: %d\n", frameNr)); + clock_gettime(CLOCK_REALTIME, &end); + int64_t timeNs = + ((end.tv_sec - begin.tv_sec) * 1E9 + (end.tv_nsec - begin.tv_nsec)); - // sleep for (period - exptime) - if (frameNr < numFrames) { // if there is a next frame - if (periodNs > timeNs) { - usleep((periodNs - timeNs) / 1000); - } + // sleep for (period - exptime) + if (frameNr < numFrames) { // if there is a next frame + if (periodNs > timeNs) { + usleep((periodNs - timeNs) / 1000); } } } @@ -1655,6 +1920,56 @@ u_int32_t runBusy() { /* common */ +int copyModule(sls_detector_module *destMod, sls_detector_module *srcMod) { + LOG(logDEBUG1, ("Copying module\n")); + + if (srcMod->serialnumber >= 0) { + destMod->serialnumber = srcMod->serialnumber; + } + // no trimbit feature + if (destMod->nchan && ((srcMod->nchan) > (destMod->nchan))) { + LOG(logINFO, ("Number of channels of source is larger than number of " + "channels of destination\n")); + return FAIL; + } + if ((srcMod->ndac) > (destMod->ndac)) { + LOG(logINFO, ("Number of dacs of source is larger than number of dacs " + "of destination\n")); + return FAIL; + } + + LOG(logDEBUG1, ("DACs: src %d, dest %d\n", srcMod->ndac, destMod->ndac)); + LOG(logDEBUG1, ("Chans: src %d, dest %d\n", srcMod->nchan, destMod->nchan)); + destMod->ndac = srcMod->ndac; + destMod->nchip = srcMod->nchip; + destMod->nchan = srcMod->nchan; + if (srcMod->reg >= 0) + destMod->reg = srcMod->reg; + /* + if (srcMod->iodelay >= 0) + destMod->iodelay = srcMod->iodelay; + if (srcMod->tau >= 0) + destMod->tau = srcMod->tau; + if (srcMod->eV >= 0) + destMod->eV = srcMod->eV; + */ + LOG(logDEBUG1, ("Copying register %x (%x)\n", destMod->reg, srcMod->reg)); + + if (destMod->nchan != 0) { + for (int ichan = 0; ichan < (srcMod->nchan); ichan++) { + *((destMod->chanregs) + ichan) = *((srcMod->chanregs) + ichan); + } + } else + LOG(logINFO, ("Not Copying trimbits\n")); + + for (int idac = 0; idac < (srcMod->ndac); idac++) { + if (*((srcMod->dacs) + idac) >= 0) { + *((destMod->dacs) + idac) = *((srcMod->dacs) + idac); + } + } + return OK; +} + int calculateDataBytes() { int numCounters = __builtin_popcount(getCounterMask()); int dr = setDynamicRange(-1); diff --git a/slsDetectorServers/mythen3DetectorServer/slsDetectorServer_defs.h b/slsDetectorServers/mythen3DetectorServer/slsDetectorServer_defs.h index d11fb35f3..c99d35629 100644 --- a/slsDetectorServers/mythen3DetectorServer/slsDetectorServer_defs.h +++ b/slsDetectorServers/mythen3DetectorServer/slsDetectorServer_defs.h @@ -45,7 +45,8 @@ #define MAX_PATTERN_LENGTH (0x2000) // maximum number of words (64bit) /** Other Definitions */ -#define BIT16_MASK (0xFFFF) +#define BIT16_MASK (0xFFFF) +#define MAX_TRIMBITS_VALUE (63) /* Enums */ enum DACINDEX { @@ -126,3 +127,31 @@ typedef struct udp_header_struct { } udp_header; #define UDP_IP_HEADER_LENGTH_BYTES (28) #define PACKETS_PER_FRAME (2) + +/** Signal Definitions */ +#define SIGNAL_TBLoad_1 (0) +#define SIGNAL_TBLoad_2 (1) +#define SIGNAL_TBLoad_3 (2) +#define SIGNAL_TBLoad_4 (3) +#define SIGNAL_TBLoad_5 (4) +#define SIGNAL_TBLoad_6 (5) +#define SIGNAL_TBLoad_7 (6) +#define SIGNAL_TBLoad_8 (7) +#define SIGNAL_TBLoad_9 (8) +#define SIGNAL_TBLoad_10 (9) +#define SIGNAL_AnaMode (10) +#define SIGNAL_CHSserialIN (11) +#define SIGNAL_READOUT (12) +#define SIGNAL_pulse (13) +#define SIGNAL_EN1 (14) +#define SIGNAL_EN2 (15) +#define SIGNAL_EN3 (16) +#define SIGNAL_clk (17) +#define SIGNAL_SRmode (18) +#define SIGNAL_serialIN (19) +#define SIGNAL_STO (20) +#define SIGNAL_STATLOAD (21) +#define SIGNAL_resStorage (22) +#define SIGNAL_resCounter (23) +#define SIGNAL_CHSclk (24) +#define SIGNAL_exposing (25) \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/include/slsDetectorFunctionList.h b/slsDetectorServers/slsDetectorServer/include/slsDetectorFunctionList.h index 006ce81d7..b9893d62b 100644 --- a/slsDetectorServers/slsDetectorServer/include/slsDetectorFunctionList.h +++ b/slsDetectorServers/slsDetectorServer/include/slsDetectorFunctionList.h @@ -99,7 +99,7 @@ void getModuleConfiguration(); #endif // set up detector -#ifdef EIGERD +#if defined(EIGERD) || defined(MYTHEN3D) void allocateDetectorStructureMemory(); #endif void setupDetector(); @@ -252,11 +252,17 @@ int64_t getMeasurementTime(); #endif // parameters - module, settings -#if (!defined(CHIPTESTBOARDD)) && (!defined(MOENCHD)) && \ - (!defined(MYTHEN3D)) && (!defined(GOTTHARD2D)) +#if (!defined(CHIPTESTBOARDD)) && (!defined(MOENCHD)) && (!defined(GOTTHARD2D)) int setModule(sls_detector_module myMod, char *mess); int getModule(sls_detector_module *myMod); #endif +#ifdef MYTHEN3D +int setTrimbits(int *trimbits); +int setBit(int ibit, int patword); +int clearBit(int ibit, int patword); +int setAllTrimbits(int val); +int getAllTrimbits(); +#endif #if (!defined(CHIPTESTBOARDD)) && (!defined(MYTHEN3D)) enum detectorSettings setSettings(enum detectorSettings sett); #endif @@ -534,7 +540,7 @@ u_int32_t runState(enum TLogLevel lev); #endif // common -#ifdef EIGERD +#if defined(EIGERD) || defined(MYTHEN3D) int copyModule(sls_detector_module *destMod, sls_detector_module *srcMod); #endif int calculateDataBytes(); diff --git a/slsDetectorServers/slsDetectorServer/src/communication_funcs.c b/slsDetectorServers/slsDetectorServer/src/communication_funcs.c index ed73b66e1..e4265ac25 100644 --- a/slsDetectorServers/slsDetectorServer/src/communication_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/communication_funcs.c @@ -456,7 +456,7 @@ int sendModule(int file_des, sls_detector_module *myMod) { } ts += n; // channels -#ifdef EIGERD +#if defined(EIGERD) || defined(MYTHEN3D) n = sendData(file_des, myMod->chanregs, sizeof(int) * (myMod->nchan), INT32); if (!n) { @@ -473,7 +473,7 @@ int receiveModule(int file_des, sls_detector_module *myMod) { LOG(level, ("Receiving Module\n")); int ts = 0, n = 0; int nDacs = myMod->ndac; -#ifdef EIGERD +#if defined(EIGERD) || defined(MYTHEN3D) int nChans = myMod->nchan; // can be zero for no trimbits LOG(level, ("nChans: %d\n", nChans)); #endif @@ -542,7 +542,7 @@ int receiveModule(int file_des, sls_detector_module *myMod) { ts += n; LOG(level, ("dacs received. %d bytes.\n", n)); // channels -#ifdef EIGERD +#if defined(EIGERD) || defined(MYTHEN3D) if (((myMod->nchan) != 0) && // no trimbits (nChans != (myMod->nchan))) { // with trimbits LOG(logERROR, ("received wrong number of channels. " diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index 55e398ce5..fba68c070 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -1721,10 +1721,8 @@ int read_register(int file_des) { int set_module(int file_des) { ret = OK; memset(mess, 0, sizeof(mess)); - enum detectorSettings retval = -1; -#if defined(CHIPTESTBOARDD) || defined(MOENCHD) || defined(MYTHEN3D) || \ - defined(GOTTHARD2D) +#if defined(CHIPTESTBOARDD) || defined(MOENCHD) || defined(GOTTHARD2D) functionNotImplemented(); #else @@ -1736,7 +1734,7 @@ int set_module(int file_des) { // allocate to receive arguments // allocate dacs - myDac = (int *)malloc(getNumberOfDACs() * sizeof(int)); + myDac = malloc(getNumberOfDACs() * sizeof(int)); // error if (getNumberOfDACs() > 0 && myDac == NULL) { ret = FAIL; @@ -1745,10 +1743,10 @@ int set_module(int file_des) { } else module.dacs = myDac; -#ifdef EIGERD +#if defined(EIGERD) || defined(MYTHEN3D) // allocate chans if (ret == OK) { - myChan = (int *)malloc(getTotalNumberOfChannels() * sizeof(int)); + myChan = malloc(getTotalNumberOfChannels() * sizeof(int)); if (getTotalNumberOfChannels() > 0 && myChan == NULL) { ret = FAIL; sprintf(mess, "Could not allocate chans\n"); @@ -1764,10 +1762,8 @@ int set_module(int file_des) { module.ndac = getNumberOfDACs(); int ts = receiveModule(file_des, &module); if (ts < 0) { - if (myChan != NULL) - free(myChan); - if (myDac != NULL) - free(myDac); + free(myChan); + free(myDac); return printSocketReadError(); } LOG(logDEBUG1, ("module register is %d, nchan %d, nchip %d, " @@ -1815,6 +1811,8 @@ int set_module(int file_des) { case LOWGAIN: case MEDIUMGAIN: case VERYHIGHGAIN: +#elif MYTHEN3D + case GET_SETTINGS: #endif break; default: @@ -1823,17 +1821,17 @@ int set_module(int file_des) { } ret = setModule(module, mess); - retval = getSettings(); +#ifndef MYTHEN3D + enum detectorSettings retval = getSettings(); validate(module.reg, (int)retval, "set module (settings)", DEC); LOG(logDEBUG1, ("Settings: %d\n", retval)); +#endif } - if (myChan != NULL) - free(myChan); - if (myDac != NULL) - free(myDac); + free(myChan); + free(myDac); #endif - return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, NULL, 0); } int get_module(int file_des) { @@ -1847,7 +1845,7 @@ int get_module(int file_des) { // allocate to send arguments // allocate dacs - myDac = (int *)malloc(getNumberOfDACs() * sizeof(int)); + myDac = malloc(getNumberOfDACs() * sizeof(int)); // error if (getNumberOfDACs() > 0 && myDac == NULL) { ret = FAIL; @@ -1856,15 +1854,14 @@ int get_module(int file_des) { } else module.dacs = myDac; -#if defined(CHIPTESTBOARDD) || defined(MOENCHD) || defined(MYTHEN3D) || \ - defined(GOTTHARD2D) +#if defined(CHIPTESTBOARDD) || defined(MOENCHD) || defined(GOTTHARD2D) functionNotImplemented(); #endif -#ifdef EIGERD +#if defined(EIGERD) || defined(MYTHEN3D) // allocate chans if (ret == OK) { - myChan = (int *)malloc(getTotalNumberOfChannels() * sizeof(int)); + myChan = malloc(getTotalNumberOfChannels() * sizeof(int)); if (getTotalNumberOfChannels() > 0 && myChan == NULL) { ret = FAIL; sprintf(mess, "Could not allocate chans\n"); @@ -1882,8 +1879,7 @@ int get_module(int file_des) { // only get LOG(logDEBUG1, ("Getting module\n")); -#if !defined(CHIPTESTBOARDD) && !defined(MOENCHD) && !defined(MYTHEN3D) && \ - !defined(GOTTHARD2D) +#if !defined(CHIPTESTBOARDD) && !defined(MOENCHD) && !defined(GOTTHARD2D) getModule(&module); #endif LOG(logDEBUG1, ("Getting module. Settings:%d\n", module.reg)); @@ -1895,10 +1891,8 @@ int get_module(int file_des) { if (ret != FAIL) { ret = sendModule(file_des, &module); } - if (myChan != NULL) - free(myChan); - if (myDac != NULL) - free(myDac); + free(myChan); + free(myDac); return ret; } @@ -3141,7 +3135,7 @@ int set_all_trimbits(int file_des) { return printSocketReadError(); LOG(logDEBUG1, ("Set all trmbits to %d\n", arg)); -#ifndef EIGERD +#if !defined(EIGERD) && !defined(MYTHEN3D) functionNotImplemented(); #else @@ -3154,10 +3148,12 @@ int set_all_trimbits(int file_des) { LOG(logERROR, (mess)); } else { ret = setAllTrimbits(arg); +#ifdef EIGERD // changes settings to undefined setSettings(UNDEFINED); LOG(logERROR, ("Settings has been changed to undefined (change all " "trimbits)\n")); +#endif } } // get @@ -3924,7 +3920,7 @@ int program_fpga(int file_des) { // receive program if (ret == OK) { - char *fpgasrc = (char *)malloc(filesize); + char *fpgasrc = malloc(filesize); if (receiveData(file_des, fpgasrc, filesize, OTHER) < 0) return printSocketReadError(); @@ -3932,8 +3928,7 @@ int program_fpga(int file_des) { Server_SendResult(file_des, INT32, NULL, 0); // free resources - if (fpgasrc != NULL) - free(fpgasrc); + free(fpgasrc); } #else // jungfrau, ctb, moench @@ -3961,7 +3956,7 @@ int program_fpga(int file_des) { // erasing flash if (ret != FAIL) { eraseFlash(); - fpgasrc = (char *)malloc(MAX_FPGAPROGRAMSIZE); + fpgasrc = malloc(MAX_FPGAPROGRAMSIZE); } // writing to flash part by part @@ -4007,8 +4002,7 @@ int program_fpga(int file_des) { stopWritingFPGAprogram(fp); // free resources - if (fpgasrc != NULL) - free(fpgasrc); + free(fpgasrc); if (fp != NULL) fclose(fp); diff --git a/slsDetectorSoftware/include/Detector.h b/slsDetectorSoftware/include/Detector.h index 364c8a8b4..cc3601d2e 100644 --- a/slsDetectorSoftware/include/Detector.h +++ b/slsDetectorSoftware/include/Detector.h @@ -108,6 +108,15 @@ class Detector { */ void setSettings(defs::detectorSettings value, Positions pos = {}); + /** [Eiger][Mythen3] */ + void loadTrimbits(const std::string &fname, Positions pos = {}); + + /** [Eiger][Mythen3] -1 if they are all different */ + Result getAllTrimbits(Positions pos = {}) const; + + /**[Eiger][Mythen3] */ + void setAllTrimbits(int value, Positions pos = {}); + /************************************************** * * * Callbacks * @@ -715,9 +724,6 @@ class Detector { /** [Eiger] */ void setSettingsPath(const std::string &value, Positions pos = {}); - /** [Eiger] */ - void loadTrimbits(const std::string &fname, Positions pos = {}); - /** [Eiger] */ Result getParallelMode(Positions pos = {}) const; @@ -742,12 +748,6 @@ class Detector { /** [Eiger] for client call back (gui) purposes */ void setBottom(bool value, Positions pos = {}); - /** [Eiger] -1 if they are all different */ - Result getAllTrimbits(Positions pos = {}) const; - - /**[Eiger] */ - void setAllTrimbits(int value, Positions pos = {}); - /**[Eiger] Returns energies in eV where the module is trimmed */ Result> getTrimEnergies(Positions pos = {}) const; diff --git a/slsDetectorSoftware/src/CmdProxy.h b/slsDetectorSoftware/src/CmdProxy.h index 4cc4f5673..6bd00844b 100644 --- a/slsDetectorSoftware/src/CmdProxy.h +++ b/slsDetectorSoftware/src/CmdProxy.h @@ -563,6 +563,8 @@ class CmdProxy { {"type", &CmdProxy::type}, {"detsize", &CmdProxy::DetectorSize}, {"settings", &CmdProxy::settings}, + {"trimbits", &CmdProxy::trimbits}, + {"trimval", &CmdProxy::trimval}, /* acquisition parameters */ {"acquire", &CmdProxy::acquire}, @@ -751,13 +753,11 @@ class CmdProxy { {"threshold", &CmdProxy::Threshold}, {"thresholdnotb", &CmdProxy::ThresholdNoTb}, {"settingspath", &CmdProxy::settingspath}, - {"trimbits", &CmdProxy::trimbits}, {"gappixels", &CmdProxy::GapPixels}, {"parallel", &CmdProxy::parallel}, {"overflow", &CmdProxy::overflow}, {"storeinram", &CmdProxy::storeinram}, {"flippeddatax", &CmdProxy::flippeddatax}, - {"trimval", &CmdProxy::trimval}, {"trimen", &CmdProxy::TrimEnergies}, {"ratecorr", &CmdProxy::RateCorrection}, {"readnlines", &CmdProxy::readnlines}, @@ -1032,6 +1032,15 @@ class CmdProxy { "g2_lc_hg | g2_lc_lg | g4_hg | g4_lg]" "\n\t[Eiger] Use threshold or thresholdnotb."); + EXECUTE_SET_COMMAND_NOID_1ARG( + trimbits, loadTrimbits, + "[fname]\n\t[Eiger][Mythen3] Loads the trimbit file to detector. If no " + "extension specified, serial number of each module is attached."); + + INTEGER_COMMAND(trimval, getAllTrimbits, setAllTrimbits, StringTo, + "[n_trimval]\n\t[Eiger][Mythen3] All trimbits set to this " + "value. Returns -1 if all trimbits are different values."); + /* acquisition parameters */ INTEGER_COMMAND_NOID( @@ -1740,11 +1749,6 @@ class CmdProxy { settingspath, getSettingsPath, setSettingsPath, "[path]\n\t[Eiger] Directory where settings files are loaded from/to."); - EXECUTE_SET_COMMAND_NOID_1ARG( - trimbits, loadTrimbits, - "[fname]\n\t[Eiger] Loads the trimbit file to detector. If no " - "extension specified, serial number of each module is attached."); - INTEGER_COMMAND(parallel, getParallelMode, setParallelMode, StringTo, "[0, 1]\n\t[Eiger] Enable or disable parallel mode."); @@ -1762,10 +1766,6 @@ class CmdProxy { "is top. Used to let Receivers and Gui know to flip the bottom image " "over the x axis. Files are not written without the flip however."); - INTEGER_COMMAND(trimval, getAllTrimbits, setAllTrimbits, StringTo, - "[n_trimval]\n\t[Eiger] All trimbits set to this value. A " - "get returns -1 if all trimbits are different values."); - INTEGER_COMMAND( readnlines, getPartialReadout, setPartialReadout, StringTo, "[1 - 256]\n\t[Eiger] Number of rows to readout per half module " diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index 3a4c3eb21..5faf17696 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -149,6 +149,18 @@ void Detector::setSettings(const defs::detectorSettings value, Positions pos) { pimpl->Parallel(&Module::setSettings, pos, value); } +void Detector::loadTrimbits(const std::string &fname, Positions pos) { + pimpl->Parallel(&Module::loadSettingsFile, pos, fname); +} + +Result Detector::getAllTrimbits(Positions pos) const { + return pimpl->Parallel(&Module::getAllTrimbits, pos); +} + +void Detector::setAllTrimbits(int value, Positions pos) { + pimpl->Parallel(&Module::setAllTrimbits, pos, value); +} + // Callback void Detector::registerAcquisitionFinishedCallback(void (*func)(double, int, @@ -942,10 +954,6 @@ void Detector::setSettingsPath(const std::string &value, Positions pos) { pimpl->Parallel(&Module::setSettingsDir, pos, value); } -void Detector::loadTrimbits(const std::string &fname, Positions pos) { - pimpl->Parallel(&Module::loadSettingsFile, pos, fname); -} - Result Detector::getParallelMode(Positions pos) const { return pimpl->Parallel(&Module::getParallelMode, pos); } @@ -978,14 +986,6 @@ void Detector::setBottom(bool value, Positions pos) { pimpl->Parallel(&Module::setFlippedDataX, pos, value); } -Result Detector::getAllTrimbits(Positions pos) const { - return pimpl->Parallel(&Module::setAllTrimbits, pos, -1); -} - -void Detector::setAllTrimbits(int value, Positions pos) { - pimpl->Parallel(&Module::setAllTrimbits, pos, value); -} - Result> Detector::getTrimEnergies(Positions pos) const { return pimpl->Parallel(&Module::getTrimEn, pos); } diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index 14d96240a..e47b0728f 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -110,19 +110,20 @@ int64_t Module::getFirmwareVersion() { int64_t Module::getDetectorServerVersion() { int64_t retval = -1; sendToDetector(F_GET_SERVER_VERSION, nullptr, retval); - LOG(logDEBUG1) << "firmware version: 0x" << std::hex << retval << std::dec; + LOG(logDEBUG1) << "detector server version: 0x" << std::hex << retval + << std::dec; return retval; } int64_t Module::getSerialNumber() { int64_t retval = -1; sendToDetector(F_GET_SERIAL_NUMBER, nullptr, retval); - LOG(logDEBUG1) << "firmware version: 0x" << std::hex << retval << std::dec; + LOG(logDEBUG1) << "serial number: 0x" << std::hex << retval << std::dec; return retval; } int64_t Module::getReceiverSoftwareVersion() const { - LOG(logDEBUG1) << "Getting receiver software version"; + LOG(logDEBUG1) << "Getting receiver version"; int64_t retval = -1; if (shm()->useReceiverFlag) { sendToReceiver(F_GET_RECEIVER_VERSION, nullptr, retval); @@ -450,7 +451,7 @@ int Module::sendModule(sls_detector_module *myMod, sls::ClientSocket &client) { ts += n; LOG(level) << "dacs sent. " << n << " bytes"; - if (shm()->myDetectorType == EIGER) { + if (shm()->myDetectorType == EIGER || shm()->myDetectorType == MYTHEN3) { n = client.Send(myMod->chanregs, sizeof(int) * (myMod->nchan)); ts += n; LOG(level) << "channels sent. " << n << " bytes"; @@ -472,7 +473,7 @@ int Module::receiveModule(sls_detector_module *myMod, ts += client.Receive(myMod->dacs, sizeof(int) * (myMod->ndac)); LOG(logDEBUG1) << "received dacs of size " << ts; - if (shm()->myDetectorType == EIGER) { + if (shm()->myDetectorType == EIGER || shm()->myDetectorType == MYTHEN3) { ts += client.Receive(myMod->chanregs, sizeof(int) * (myMod->nchan)); LOG(logDEBUG1) << " nchan= " << myMod->nchan << " nchip= " << myMod->nchip @@ -907,7 +908,7 @@ void Module::loadSettingsFile(const std::string &fname) { ostfn << fname; // find specific file if it has detid in file name (.snxxx) - if (shm()->myDetectorType == EIGER) { + if (shm()->myDetectorType == EIGER || shm()->myDetectorType == MYTHEN3) { if (fname.find(".sn") == std::string::npos && fname.find(".trim") == std::string::npos && fname.find(".settings") == std::string::npos) { @@ -2509,14 +2510,20 @@ void Module::setFlippedDataX(bool value) { sendToReceiver(F_SET_FLIPPED_DATA_RECEIVER, arg, retval); } -int Module::setAllTrimbits(int val) { +int Module::getAllTrimbits() { int retval = -1; - LOG(logDEBUG1) << "Setting all trimbits to " << val; + int val = -1; sendToDetector(F_SET_ALL_TRIMBITS, val, retval); LOG(logDEBUG1) << "All trimbit value: " << retval; return retval; } +void Module::setAllTrimbits(int val) { + int retval = -1; + LOG(logDEBUG1) << "Setting all trimbits to " << val; + sendToDetector(F_SET_ALL_TRIMBITS, val, retval); +} + int Module::setTrimEn(const std::vector &energies) { if (shm()->myDetectorType != EIGER) { throw RuntimeError("setTrimEn not implemented for this detector."); @@ -2766,7 +2773,6 @@ int Module::setAutoComparatorDisableMode(int ival) { void Module::setModule(sls_detector_module &module, int tb) { int fnum = F_SET_MODULE; int ret = FAIL; - int retval = -1; LOG(logDEBUG1) << "Setting module with tb:" << tb; // to exclude trimbits if (tb == 0) { @@ -2783,8 +2789,6 @@ void Module::setModule(sls_detector_module &module, int tb) { throw RuntimeError("Detector " + std::to_string(detId) + " returned error: " + mess); } - client.Receive(&retval, sizeof(retval)); - LOG(logDEBUG1) << "Set Module returned: " << retval; } sls_detector_module Module::getModule() { @@ -3554,7 +3558,7 @@ sls_detector_module Module::readSettingsFile(const std::string &fname, int tb) { auto names = getSettingsFileDacNames(); // open file std::ifstream infile; - if (shm()->myDetectorType == EIGER) { + if (shm()->myDetectorType == EIGER || shm()->myDetectorType == MYTHEN3) { infile.open(fname.c_str(), std::ifstream::binary); } else { infile.open(fname.c_str(), std::ios_base::in); @@ -3565,30 +3569,16 @@ sls_detector_module Module::readSettingsFile(const std::string &fname, int tb) { // eiger if (shm()->myDetectorType == EIGER) { - bool allread = false; infile.read(reinterpret_cast(myMod.dacs), sizeof(int) * (myMod.ndac)); - if (infile.good()) { - infile.read(reinterpret_cast(&myMod.iodelay), - sizeof(myMod.iodelay)); - if (infile.good()) { - infile.read(reinterpret_cast(&myMod.tau), - sizeof(myMod.tau)); - if (tb != 0) { - if (infile.good()) { - infile.read(reinterpret_cast(myMod.chanregs), - sizeof(int) * (myMod.nchan)); - if (infile) { - allread = true; - } - } - } else if (infile) { - allread = true; - } - } + infile.read(reinterpret_cast(&myMod.iodelay), + sizeof(myMod.iodelay)); + infile.read(reinterpret_cast(&myMod.tau), sizeof(myMod.tau)); + if (tb != 0) { + infile.read(reinterpret_cast(myMod.chanregs), + sizeof(int) * (myMod.nchan)); } - if (!allread) { - infile.close(); + if (!infile) { throw RuntimeError("readSettingsFile: Could not load all values " "for settings for " + fname); @@ -3600,6 +3590,23 @@ sls_detector_module Module::readSettingsFile(const std::string &fname, int tb) { LOG(logDEBUG1) << "tau:" << myMod.tau; } + // mythen3 (dacs, trimbits) + else if (shm()->myDetectorType == MYTHEN3) { + infile.read(reinterpret_cast(myMod.dacs), + sizeof(int) * (myMod.ndac)); + infile.read(reinterpret_cast(myMod.chanregs), + sizeof(int) * (myMod.nchan)); + + if (!infile) { + throw RuntimeError("readSettingsFile: Could not load all values " + "for settings for " + + fname); + } + for (int i = 0; i < myMod.ndac; ++i) { + LOG(logDEBUG1) << "dac " << i << ":" << myMod.dacs[i]; + } + } + // gotthard, jungfrau else { size_t idac = 0; @@ -3626,18 +3633,15 @@ sls_detector_module Module::readSettingsFile(const std::string &fname, int tb) { if (!found) { throw RuntimeError("readSettingsFile: Unknown dac: " + sargname); - infile.close(); } } // not all read if (idac != names.size()) { - infile.close(); throw RuntimeError("Could read only " + std::to_string(idac) + " dacs. Expected " + std::to_string(names.size()) + " dacs"); } } - infile.close(); LOG(logINFO) << "Settings file loaded: " << fname.c_str(); return myMod; } @@ -3678,7 +3682,6 @@ void Module::writeSettingsFile(const std::string &fname, outfile << names[i] << " " << mod.dacs[i] << std::endl; } } - outfile.close(); } std::vector Module::getSettingsFileDacNames() { @@ -3694,6 +3697,8 @@ std::vector Module::getSettingsFileDacNames() { "VDAC6", "VDAC7", "VDAC8", "VDAC9", "VDAC10", "VDAC11", "VDAC12", "VDAC13", "VDAC14", "VDAC15"}; break; + case MYTHEN3: + break; default: throw RuntimeError( "Unknown detector type - unknown format for settings file"); diff --git a/slsDetectorSoftware/src/Module.h b/slsDetectorSoftware/src/Module.h index f91a40de9..77f672099 100644 --- a/slsDetectorSoftware/src/Module.h +++ b/slsDetectorSoftware/src/Module.h @@ -318,11 +318,6 @@ class Module : public virtual slsDetectorDefs { */ std::string setSettingsDir(const std::string &dir); - /** - * Loads the modules settings/trimbits reading from a specific file - * file name extension is automatically generated. - * @param fname specific settings/trimbits file - */ void loadSettingsFile(const std::string &fname); /** @@ -1127,12 +1122,8 @@ class Module : public virtual slsDetectorDefs { */ void setFlippedDataX(bool value); - /** - * Sets all the trimbits to a particular value (Eiger) - * @param val trimbit value - * @returns OK or FAIL - */ - int setAllTrimbits(int val); + int getAllTrimbits(); + void setAllTrimbits(int val); /** * Sets the number of trim energies and their value (Eiger) diff --git a/slsSupportLib/include/sls_detector_defs.h b/slsSupportLib/include/sls_detector_defs.h index 8acee1a56..55b579afd 100644 --- a/slsSupportLib/include/sls_detector_defs.h +++ b/slsSupportLib/include/sls_detector_defs.h @@ -593,8 +593,8 @@ typedef struct { #ifdef __cplusplus sls_detector_module() - : serialnumber(0), nchan(0), nchip(0), ndac(0), reg(0), iodelay(0), - tau(0), eV(0), dacs(nullptr), chanregs(nullptr) {} + : serialnumber(0), nchan(0), nchip(0), ndac(0), reg(-1), iodelay(0), + tau(0), eV(-1), dacs(nullptr), chanregs(nullptr) {} explicit sls_detector_module(slsDetectorDefs::detectorType type) : sls_detector_module() { diff --git a/slsSupportLib/include/versionAPI.h b/slsSupportLib/include/versionAPI.h index a33ce6990..4ba6d6993 100644 --- a/slsSupportLib/include/versionAPI.h +++ b/slsSupportLib/include/versionAPI.h @@ -3,11 +3,11 @@ #define APILIB 0x200409 #define APIRECEIVER 0x200409 #define APIGUI 0x200409 -#define APIEIGER 0x200409 -#define APICTB 0x200409 -#define APIGOTTHARD 0x200409 -#define APIJUNGFRAU 0x200409 -#define APIMYTHEN3 0x200428 -#define APIGOTTHARD2 0x200429 -#define APIMOENCH 0x200429 +#define APICTB 0x200508 +#define APIGOTTHARD 0x200508 +#define APIGOTTHARD2 0x200508 +#define APIJUNGFRAU 0x200508 +#define APIMOENCH 0x200508 +#define APIEIGER 0x200508 +#define APIMYTHEN3 0x200508