From 1a75170eed9ab9e92c3098df60161b69bf661eaa Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Thu, 7 May 2020 16:04:30 +0200 Subject: [PATCH] mythen3: set trimbits (not settings, threshold yet), set all trimbits --- .../slsDetectorFunctionList.c | 352 +++++++++++++++++- .../slsDetectorServer_defs.h | 31 +- .../include/slsDetectorFunctionList.h | 14 +- .../src/communication_funcs.c | 6 +- .../src/slsDetectorServer_funcs.c | 26 +- slsDetectorSoftware/include/Detector.h | 18 +- slsDetectorSoftware/src/CmdProxy.h | 22 +- slsDetectorSoftware/src/Detector.cpp | 24 +- slsDetectorSoftware/src/Module.cpp | 53 ++- slsDetectorSoftware/src/Module.h | 13 +- slsSupportLib/include/sls_detector_defs.h | 4 +- 11 files changed, 474 insertions(+), 89 deletions(-) diff --git a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c index 06fbb3d5c..564e90935 100644 --- a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c @@ -39,11 +39,14 @@ int virtual_status = 0; int virtual_stop = 0; #endif +sls_detector_module *detectorModules = NULL; +int *detectorChans = NULL; +int *detectorDacs = NULL; + 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 @@ -340,9 +343,44 @@ 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 + int idac = 0; + for (idac = 0; idac < (detectorModules)->ndac; ++idac) { + detectorDacs[idac] = 0; + } + + // if trimval requested, should return -1 to acknowledge unknown + int ichan = 0; + for (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; @@ -355,9 +393,6 @@ void setupDetector() { for (i = 0; i < NUM_CLOCKS; ++i) { clkPhase[i] = 0; } - for (i = 0; i < NDAC; ++i) { - dacValues[i] = 0; - } } #ifdef VIRTUAL virtual_status = 0; @@ -408,6 +443,10 @@ int setDefaultDacs() { const int defaultvals[NDAC] = DEFAULT_DAC_VALS; for (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 +523,238 @@ 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 + { + int i = 0; + for (i = 0; i < NDAC; ++i) { + 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 + int ichan = 0; + for (ichan = 0; ichan < 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")); + + uint64_t patword = 0; + int iaddr = 0; + int ichip = 0; + for (ichip = 0; ichip < NCHIP; ichip++) { + if (iaddr >= MAX_PATTERN_LENGTH) { + LOG(logERROR, ("Addr 0x%x is past max_address_length 0x%x!\n", + iaddr, MAX_PATTERN_LENGTH)); + return FAIL; + } + LOG(logINFOBLUE, (" Chip %d\n", ichip)); + iaddr = 0; + patword = 0; + writePatternWord(iaddr++, patword); + + // chip select + patword = SetBit(SIGNAL_TBLoad_1 + ichip, 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++) { + if (iaddr >= MAX_PATTERN_LENGTH) { + LOG(logERROR, ("Addr 0x%x is past max_address_length 0x%x!\n", + iaddr, MAX_PATTERN_LENGTH)); + return FAIL; + } + LOG(logINFOBLUE, (" 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); + + // 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 + ichan = 0; + for (ichan = 0; ichan < NCHAN; ++ichan) { + detectorChans[ichan] = trimbits[ichan]; + } + return OK; +} + +int setAllTrimbits(int val) { + int trimbits[NCHAN]; + int ichan = 0; + for (ichan = 0; ichan < NCHAN; ++ichan) { + trimbits[ichan] = val; + } + if (setTrimbits(trimbits) == FAIL) { + LOG(logERROR, ("Could not set all trimbits to %d\n", val)); + return FAIL; + } + + LOG(logINFO, ("All trimbits have been set to %d\n", val)); + return OK; +} + +int getAllTrimbits() { + int ichan = 0; + int value = detectorChans[0]; + if (detectorModules) { + for (ichan = 0; ichan < 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 +956,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; } @@ -1059,6 +1329,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; @@ -1655,6 +1929,60 @@ u_int32_t runBusy() { /* common */ +int copyModule(sls_detector_module *destMod, sls_detector_module *srcMod) { + + int idac, ichan; + int ret = OK; + + 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 (ichan = 0; ichan < (srcMod->nchan); ichan++) { + *((destMod->chanregs) + ichan) = *((srcMod->chanregs) + ichan); + } + } else + LOG(logINFO, ("Not Copying trimbits\n")); + + for (idac = 0; idac < (srcMod->ndac); idac++) { + if (*((srcMod->dacs) + idac) >= 0) { + *((destMod->dacs) + idac) = *((srcMod->dacs) + idac); + } + } + return ret; +} + 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..ca2391163 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..945ab766e 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 @@ -1745,7 +1743,7 @@ 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)); @@ -1815,6 +1813,8 @@ int set_module(int file_des) { case LOWGAIN: case MEDIUMGAIN: case VERYHIGHGAIN: +#elif MYTHEN3D + case GET_SETTINGS: #endif break; default: @@ -1823,9 +1823,11 @@ 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); @@ -1833,7 +1835,7 @@ int set_module(int file_des) { 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) { @@ -1856,12 +1858,11 @@ 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)); @@ -1882,8 +1883,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)); @@ -3141,7 +3141,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 +3154,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 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..de833162e 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); @@ -3600,6 +3604,29 @@ sls_detector_module Module::readSettingsFile(const std::string &fname, int tb) { LOG(logDEBUG1) << "tau:" << myMod.tau; } + // mythen3 (dacs, trimbits) + else if (shm()->myDetectorType == MYTHEN3) { + bool allread = false; + infile.read(reinterpret_cast(myMod.dacs), + sizeof(int) * (myMod.ndac)); + if (infile.good()) { + infile.read(reinterpret_cast(myMod.chanregs), + sizeof(int) * (myMod.nchan)); + if (infile) { + allread = true; + } + } + if (!allread) { + infile.close(); + 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; @@ -3694,6 +3721,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() {