From 326a8efad6f6f7401e0d78efec8291b5077253bf Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Wed, 4 Feb 2026 17:52:20 +0100 Subject: [PATCH] wip. xilinx left --- .../slsDetectorFunctionList.c | 220 ++++++++------ .../slsDetectorFunctionList.h | 11 +- .../slsDetectorFunctionList.c | 217 ++++++++------ .../slsDetectorFunctionList.h | 10 +- .../slsDetectorFunctionList.c | 136 +++++---- .../slsDetectorFunctionList.h | 12 +- .../slsDetectorFunctionList.c | 169 +++++++---- .../slsDetectorFunctionList.h | 11 +- .../slsDetectorFunctionList.c | 145 +++++++--- .../slsDetectorFunctionList.h | 10 +- .../slsDetectorFunctionList.c | 268 +++++++++++------- .../slsDetectorFunctionList.h | 14 +- .../slsDetectorServer/include/LTC2620.h | 11 +- .../include/LTC2620_Driver.h | 12 +- .../slsDetectorServer/src/LTC2620.c | 41 +-- .../slsDetectorServer/src/LTC2620_Driver.c | 84 ++---- .../src/slsDetectorServer_funcs.c | 155 ++++------ 17 files changed, 828 insertions(+), 698 deletions(-) diff --git a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c index a6b014b94..7fe88116e 100644 --- a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c @@ -589,15 +589,22 @@ void setupDetector() { DAC_MAX_MV); // has to be before setvchip LTC2620_Disable(); LTC2620_Configure(); - // switch off dacs (power regulators most likely only sets to minimum (if - // power enable on)) LOG(logINFOBLUE, ("Powering down all dacs\n")); - for (int idac = 0; idac < NDAC; ++idac) { - setDAC(idac, LTC2620_GetPowerDownValue(), - 0); // has to be before setvchip + for (int idac = 0; idac < NDAC_ONLY; ++idac) { + initError = setDAC(idac, LTC2620_GetPowerDownValue(), 0, initErrorMessage); + if (initError == FAIL) + return; } - // power regulators + // power regulators (set min dac values, still power disabled) + LOG(logINFOBLUE, ("Setting power dacs to minimum (power disabled)\n")); + for (int idac = NDAC_ONLY; idac < NDAC; ++idac) { + initError = initPower(idac, initErrorMessage); + if (initError == FAIL) + return; + } + + // I2C INA226_ConfigureI2CCore(I2C_SHUNT_RESISTER_OHMS, I2C_CONTROL_REG, I2C_STATUS_REG, I2C_RX_DATA_FIFO_REG, @@ -611,7 +618,9 @@ void setupDetector() { INA226_CalibrateCurrentRegister(I2C_POWER_VD_DEVICE_ID); initError = setVchip(VCHIP_MIN_MV, initErrorMessage); if (initError == FAIL) - return; + return; + + setADCInvertRegister(0); // depends on chip @@ -1248,13 +1257,26 @@ int getADCVpp(int mV, int* retval, char* mess) { return OK; } -int setDAC(enum DACINDEX ind, int val, int mV, char* mess) { +int validateDAC(enum DACINDEX ind, int val, int mV, char* mess) { // validate index if (ind < D0 || ind > D_PWR_IO) { sprintf(mess, "Could not set DAC %d. Invalid index.\n", ind); LOG(logERROR, (mess)); return FAIL; } + // validate mV + if (mV) { + if (val == LTC2620_GetPowerDownValue()) { + sprintf(mess, "Could not set DAC %d. Cannot use power down value and use 'mV'\n", ind); + LOG(logERROR, (mess)); + return FAIL; + } + if (ind >= D_PWR_D) { + sprintf(mess, "Could not set DAC %d. Cannot convert to dac units for power regulator at this stage. Should have been earlier.\n"); + LOG(logERROR, (mess)); + return FAIL; + } + } // validate min value if (val < 0) { // dacs only: allow power down value (-100) @@ -1265,18 +1287,65 @@ int setDAC(enum DACINDEX ind, int val, int mV, char* mess) { LOG(logERROR, (mess)); return FAIL; } + } + // validate max value + if (val != LTC2620_GetPowerDownValue()) { + if (!mV) { + // dacs and power regs + if (val > LTC2620_GetMaxInput()) { + sprintf("Could not set DAC %d. Input %d exceeds max dac value %d\n", ind, val, LTC2620_GetMaxInput()); + LOG(logERROR, (mess)) + return FAIL; + } + // check vLimit - only dacs, convert to mV + if (vLimit > 0 && ind < PWR_D) { + int dacmV = 0; + if (LTC2620_DacToVoltage(val, &dacmV) == FAIL) { + sprintf(mess, "Could not set DAC %d. Could not convert input %d to mV\n", ind, val); + log(logERROR, (mess)); + return FAIL; + } + if (dacmV > vLimit) { + sprintf(mess, "Could not set DAC %d. Input %d (%d mV) exceeds vLimit value %d\n", ind, val, vLimit); + LOG(logERROR, (mess)); + return FAIL; + } + } + } + // only dacs in mV here + else { + if (val > DAC_MAX_MV) { + sprintf("Could not set DAC %d. Input %d mV exceeds max dac value %d mV\n", ind, val, DAC_MAX_MV); + LOG(logERROR, (mess)) + return FAIL; + } + if (vLimit > 0 && val > vLimit) { + sprintf("Could not set DAC %d. Input %d mV exceeds vLimit %d mV\n", ind, val, vLimit); + LOG(logERROR, (mess)) + return FAIL; + } + } } - // validate mV - if (ind >= D_PWR_D && mV) { - sprintf(mess, "Could not set DAC %d. Cannot convert to dac units for power regulator at this stage. Should have been earlier.\n"); - LOG(logERROR, (mess)); + return OK; +} + +int setDAC(enum DACINDEX ind, int val, int mV, char* mess) { + if (validateDAC(ind, val, mV, mess) == FAIL) return FAIL; - } - - LOG(logDEBUG1, ("Setting dac[%d]: %d %s \n", (int)ind, val, - (mV ? "mV" : "dac units"))); + + LOG(logINFO, ("Setting dac[%d]: %d %s \n", (int)ind, val, (mV ? "mV" : "dac units"))); + + // dacs only, mV, convert to dac value int dacval = val; - if (LTC2620_SetDACValue((int)ind, val, mV, &dacval) == FAIL) { + if (mV && ind < PWR_D) { + if (LTC2620_VoltageToDac(val, &dacval) == FAIL) { + sprintf(mess, "Could not set DAC %d. Could not convert %d mV to dac units.\n", ind, val); + LOG(logERROR, (mess)); + return FAIL; + } + } + + if (LTC2620_SetDACValue((int)ind, dacval) == FAIL) { sprintf(mess, "Could not set DAC %d. Failed to convert\n", ind); LOG(logERROR, (mess)); return FAIL; @@ -1289,13 +1358,13 @@ int setDAC(enum DACINDEX ind, int val, int mV, char* mess) { int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess) { // validate index if (ind < D0 || ind > D_PWR_IO) { - sprintf(mess, "Could not set DAC %d. Invalid index.\n", ind); + sprintf(mess, "Could not get DAC %d. Invalid index.\n", ind); LOG(logERROR, (mess)); return FAIL; } // validate mV - if (ind >= D_PWR_D && mV) { - sprintf(mess, "Could not set DAC %d. Cannot convert to dac units for power regulator at this stage. Should have been earlier.\n"); + if (mV && ind >= D_PWR_D) { + sprintf(mess, "Could not get DAC %d. Cannot convert to dac units for power regulator at this stage. Should have been earlier.\n"); LOG(logERROR, (mess)); return FAIL; } @@ -1317,31 +1386,6 @@ int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess) { return OK; } -int getMaxDacSteps() { return LTC2620_GetMaxNumSteps(); } - -int dacToVoltage(int dac) { - int val; - LTC2620_DacToVoltage(dac, &val); - return val; -} - -int checkVLimitCompliant(int mV) { - if (vLimit > 0 && mV > vLimit) - return FAIL; - return OK; -} - -int checkVLimitDacCompliant(int dac) { - if (vLimit > 0 && dac != -1 && dac != LTC2620_GetPowerDownValue()) { - int mv = 0; - // could not convert - if (LTC2620_DacToVoltage(dac, &mv) == FAIL) - return FAIL; - if (mv > vLimit) - return FAIL; - } - return OK; -} int getVLimit() { return vLimit; } @@ -1354,12 +1398,6 @@ int setVLimit(int val, char* mess) { vLimit = val; } -int isVchipValid(int val) { - if (val < VCHIP_MIN_MV || val > VCHIP_MAX_MV) { - return 0; - } - return 1; -} int getVchip(int* retval, char* mess) { *retval = -1; @@ -1457,42 +1495,6 @@ int getVChipToSet(enum DACINDEX ind, int val, int* vchip, char* mess) { return OK; } -int getDACIndexFromADCIndex(enum ADCINDEX ind) { - switch (ind) { - case V_PWR_IO: - return D_PWR_IO; - case V_PWR_A: - return D_PWR_A; - case V_PWR_B: - return D_PWR_B; - case V_PWR_C: - return D_PWR_C; - case V_PWR_D: - return D_PWR_D; - default: - LOG(logERROR, ("ADC index %d is not defined to get DAC index\n", ind)); - return -1; - } -} - -int getADCIndexFromDACIndex(enum DACINDEX ind) { - switch (ind) { - case D_PWR_IO: - return V_PWR_IO; - case D_PWR_A: - return V_PWR_A; - case D_PWR_B: - return V_PWR_B; - case D_PWR_C: - return V_PWR_C; - case D_PWR_D: - return V_PWR_D; - default: - LOG(logERROR, ("DAC index %d is not defined to get ADC index\n", ind)); - return -1; - } -} - int isPowerValid(enum PWRINDEX ind, int val, char* mess) { char *powerNames[] = {PWR_NAMES}; @@ -1653,13 +1655,46 @@ int getPower(enum DACINDEX ind, int* retval, char* mess) { return FAIL; } - // convert dac to voltage - if (ConvertToDifferentRange(LTC2620_GetMaxInput(), LTC2620_GetMinInput(), POWER_RGLTR_MIN, POWER_RGLTR_MAX, dacValues[ind], retval) == FAIL) { - sprintf(mess, "Could not convert to get %s value\n", powerNames[pwrIndex]); + if (convertPowerRegDACtoVoltage(pwrIndex, dacValues[ind], retval, mess) == FAIL) + return FAIL; + + return OK; +} + +int convertPowerRegDACtoVoltage(enum PWRINDEX ind, int val, int* retval, char* mess) { + char *powerNames[] = {PWR_NAMES}; + if (ConvertToDifferentRange(LTC2620_GetMaxInput(), LTC2620_GetMinInput(), POWER_RGLTR_MIN, POWER_RGLTR_MAX, val, retval) == FAIL) { + sprintf(mess, "Could not get %s. Could not convert %d dac to mV\n", powerNames[ind], val); LOG(logERROR, (mess)); return FAIL; } +} +int convertPowerRegVoltagetoDAC(enum PWRINDEX ind, int val, int* retval, char* mess) { + char *powerNames[] = {PWR_NAMES}; + if (ConvertToDifferentRange(POWER_RGLTR_MIN, POWER_RGLTR_MAX, LTC2620_GetMaxInput(), LTC2620_GetMinInput(), val, retval) == FAIL) { + sprintf(mess, "Could not set %s. Invalid value %d mV to convert to dac units.\n", powerNames[ind], val); + LOG(logERROR, (mess)); + return FAIL; + } +} + +int initPower(enum DACINDEX ind, char* mess) { + if (ind == D_PWR_CHIP) + return OK; + + enum PWRINDEX pwrIndex = 0; + if (getPowerIndex(ind, pwrIndex, initErrorMessage) == FAIL) + return FAIL; + + int dacval = 0; + int min = (ind == D_PWR_IO) ? VIO_MIN_MV : POWER_RGLTR_MIN; + if (convertPowerRegVoltagetoDAC(pwrIndex, min, &dacval, initErrorMessage) == FAIL) + return FAIL; + + if (setDAC(ind, dacval, 0, initErrorMessage) == FAIL) + return FAIL; + return OK; } @@ -1692,11 +1727,8 @@ int setPower(enum DACINDEX ind, int val, char* mess) { // convert to dac units int dacval = val; if (val != LTC2620_GetPowerDownValue()) { - if (ConvertToDifferentRange(POWER_RGLTR_MIN, POWER_RGLTR_MAX, LTC2620_GetMaxInput(), LTC2620_GetMinInput(), val, &dacval) == FAIL) { - sprintf(mess, "Could not set %s. Invalid value %d mV to convert to dac units.\n", powerNames[pwrIndex], val); - LOG(logERROR, (mess)); + if (convertPowerRegVoltagetoDAC(pwrIndex, val, &dacval, mess) == FAIL) return FAIL; - } } LOG(logINFO, ("Setting %s (DAC %d): %d dac (%d mV)\n", powerNames[pwrIndex], dacval, val)); diff --git a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.h index 6cab2faea..0ea71d3e8 100644 --- a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.h @@ -128,26 +128,23 @@ int getADCVpp(int mV, int* retval, char* mess); int setDAC(enum DACINDEX ind, int val, int mV, char* mess); int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess); -int getMaxDacSteps(); -int dacToVoltage(int dac); -int checkVLimitCompliant(int mV); -int checkVLimitDacCompliant(int dac); int getVLimit(); int setVLimit(int val, char* mess); -int isVchipValid(int val); int getVchip(int* retval, char* mess); int setVchip(int val, char* mess); int getVChipToSet(enum DACINDEX ind, int val, int* vchip, char* mess); -int getDACIndexFromADCIndex(enum ADCINDEX ind); -int getADCIndexFromDACIndex(enum DACINDEX ind); int getPowerRailMask(enum PWRINDEX ind, uint32_t* mask, char* mess); int EnablePowerRail(enum PWRINDEX ind, char* mess); int DisablePowerRail(enum PWRINDEX ind, char* mess); int getPowerRail(enum PWRINDEX ind, int* retval, char* mess); + int isPowerValid(enum PWRINDEX ind, int val, char* mess); int getPowerIndex(enum DACINDEX ind, enum PWRINDEX* pwrIndex, char* mess); + +int convertPowertoDACUnits(enum PWRINDEX ind, int val, int* retval, char* mess); +int initPower(enum DACINDEX ind, char* mess); int getPower(enum DACINDEX ind, int* retval, char* mess); int setPower(enum DACINDEX ind, int val, char* mess); void powerOff(); diff --git a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c index dc7a1f0dc..644103dba 100644 --- a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c @@ -800,7 +800,9 @@ void setupDetector() { return; LOG(logINFOBLUE, ("Setting Default Parameters\n")); - resetToDefaultDacs(0); + initError = resetToDefaultDacs(0, initErrorMessage); + if (initError == FAIL) + return; #ifdef VIRTUAL if (isControlServer) { sharedMemory_setStatus(IDLE); @@ -866,7 +868,7 @@ void setupDetector() { LOG(logDEBUG1, ("Setup detector done\n\n")); } -int resetToDefaultDacs(int hardReset) { +int resetToDefaultDacs(int hardReset, char* mess) { // reset defaults to hardcoded defaults if (hardReset) { const int vals[] = DEFAULT_DAC_VALS; @@ -878,12 +880,8 @@ int resetToDefaultDacs(int hardReset) { int ret = OK; LOG(logINFOBLUE, ("Setting Default Dac values\n")); for (int i = 0; i < NDAC; ++i) { - setDAC((enum DACINDEX)i, defaultDacValues[i], 0); - if ((detectorModules)->dacs[i] != defaultDacValues[i]) { - ret = FAIL; - LOG(logERROR, ("Setting dac %d failed, wrote %d, read %d\n", i, - defaultDacValues[i], (detectorModules)->dacs[i])); - } + if (setDAC((enum DACINDEX)i, defaultDacValues[i], 0, mess) == FAIL) + return FAIL; } return ret; } @@ -1307,14 +1305,16 @@ int setModule(sls_detector_module myMod, char *mess) { detectorModules->serialnumber = myMod.serialnumber; // settings - setSettings((enum detectorSettings)myMod.reg); + if (setSettings((enum detectorSettings)myMod.reg, mess) == FAIL) + return FAIL; // iodelay if (setIODelay(myMod.iodelay) != myMod.iodelay) { sprintf(mess, "Could not set module. Could not set iodelay %d\n", myMod.iodelay); LOG(logERROR, (mess)); - setSettings(UNDEFINED); + if (setSettings(UNDEFINED, mess) == FAIL) + return FAIL; LOG(logERROR, ("Settings has been changed to undefined\n")); return FAIL; } @@ -1324,18 +1324,21 @@ int setModule(sls_detector_module myMod, char *mess) { setThresholdEnergy(myMod.eV[0]); else { // (loading a random trim file) (dont return fail) - setSettings(UNDEFINED); + if (setSettings(UNDEFINED, mess) == FAIL) + return FAIL; LOG(logERROR, ("Settings has been changed to undefined (random trim file)\n")); } // dacs for (int i = 0; i < NDAC; ++i) { - setDAC((enum DACINDEX)i, myMod.dacs[i], 0); + if (setDAC((enum DACINDEX)i, myMod.dacs[i], 0, mess) == FAIL) + return FAIL; if (myMod.dacs[i] != (detectorModules)->dacs[i]) { sprintf(mess, "Could not set module. Could not set dac %d\n", i); LOG(logERROR, (mess)); - setSettings(UNDEFINED); + if (setSettings(UNDEFINED, mess) == FAIL) + return FAIL; LOG(logERROR, ("Settings has been changed to undefined\n")); return FAIL; } @@ -1362,7 +1365,8 @@ int setModule(sls_detector_module myMod, char *mess) { "Cannot set module. Cannot set Rate correction. " "No default tau provided. Deactivating Rate Correction\n"); LOG(logERROR, (mess)); - setSettings(UNDEFINED); + if (setSettings(UNDEFINED, mess) == FAIL) + return FAIL; LOG(logERROR, ("Settings has been changed to undefined (random " "trim file)\n")); return FAIL; @@ -1375,7 +1379,8 @@ int setModule(sls_detector_module myMod, char *mess) { if (setRateCorrection(myMod.tau) == FAIL) { sprintf(mess, "Cannot set module. Rate correction failed.\n"); LOG(logERROR, (mess)); - setSettings(UNDEFINED); + if (setSettings(UNDEFINED, mess) == FAIL) + return FAIL; LOG(logERROR, ("Settings has been changed to undefined (random " "trim file)\n")); return FAIL; @@ -1386,7 +1391,8 @@ int setModule(sls_detector_module myMod, char *mess) { mess, "Cannot set module. Could not set rate correction\n"); LOG(logERROR, (mess)); - setSettings(UNDEFINED); + if (setSettings(UNDEFINED, mess) == FAIL) + return FAIL; LOG(logERROR, ("Settings has been changed to undefined " "(random trim file)\n")); return FAIL; @@ -1397,14 +1403,16 @@ int setModule(sls_detector_module myMod, char *mess) { return OK; } -enum detectorSettings setSettings(enum detectorSettings sett) { +int setSettings(enum detectorSettings sett, char* mess) { if (sett == UNINITIALIZED) { - return thisSettings; + sprintf(mess, "Cannot set settings to uninitialized\n"); + LOG(logERROR, (mess)); + return FAIL; } thisSettings = sett; detectorModules->reg = sett; LOG(logINFO, ("Settings: %d\n", thisSettings)); - return thisSettings; + return OK; } enum detectorSettings getSettings() { return thisSettings; } @@ -1425,100 +1433,131 @@ int setThresholdEnergy(int ev) { } /* parameters - dac, adc, hv */ +int validateDAC(enum DACINDEX ind, int val, int mV, char* mess) { + char *dacNames[] = {DAC_NAMES}; + + // validate index (including E_VTHRESHOLD) + if (ind < 0 || ind >= NDAC + 1) { + sprintf(mess, "Could not set DAC. Invalid index %d\n", ind); + LOG(logERROR, (mess)); + return FAIL; + } + // validate min value + if (val < 0) { + sprintf(mess, "Could not set DAC %s. Input value %d cannot be negative\n", dacNames[ind], val); + LOG(logERROR, (mess)); + return FAIL; + } + // validate max value + if (mV && val > DAC_MAX_MV) { + sprintf(mess, "Could not set DAC %s. Input value %d exceed maximum %d mV\n", dacNames[ind], val, DAC_MAX_MV); + LOG(logERROR, (mess)); + return FAIL; + } + else if (!mV && val > LTC2620_MAX_VAL) { + sprintf(mess, "Could not set DAC %s. Input value %d exceed maximum %d \n", dacNames[ind], val, LTC2620_MAX_VAL); + LOG(logERROR, (mess)); + return FAIL; + } + return OK; +} + // uses LTC2620 with 2.048V (implementation different to others not bit banging) -void setDAC(enum DACINDEX ind, int val, int mV) { - if (val < 0) - return; +int setDAC(enum DACINDEX ind, int val, int mV, char* mess) { + if (validateDAC(ind, val, mV, mess) == FAIL) + return FAIL; - LOG(logDEBUG1, ("Setting dac[%d]: %d %s \n", (int)ind, val, - (mV ? "mV" : "dac units"))); + char *dacNames[] = {DAC_NAMES}; + LOG(logINFO, ("Setting DAC %s: %d %s \n", dacNames[ind], val, (mV ? "mV" : "dac units"))); if (ind == E_VTHRESHOLD) { - setDAC(E_VCMP_LL, val, mV); - setDAC(E_VCMP_LR, val, mV); - setDAC(E_VCMP_RL, val, mV); - setDAC(E_VCMP_RR, val, mV); - setDAC(E_VCP, val, mV); - return; + if (setDAC(E_VCMP_LL, val, mV, mess) == FAIL) + return FAIL; + if (setDAC(E_VCMP_LR, val, mV, mess) == FAIL) + return FAIL; + if (setDAC(E_VCMP_RL, val, mV, mess) == FAIL) + return FAIL; + if (setDAC(E_VCMP_RR, val, mV, mess) == FAIL) + return FAIL; + if (setDAC(E_VCP, val, mV, mess) == FAIL) + return FAIL; + return OK; } - // validate index - if (ind < 0 || ind >= NDAC) { - LOG(logERROR, - ("\tDac index %d is out of bounds (0 to %d)\n", ind, NDAC - 1)); - return; - } - - char *dac_names[] = {DAC_NAMES}; - LOG(logINFO, ("Setting dac[%d - %s]: %d %s \n", (int)ind, dac_names[ind], - val, (mV ? "mV" : "dac units"))); - -#ifdef VIRTUAL - int dacval = 0; - if (!mV) { - (detectorModules)->dacs[ind] = val; - } - // convert to dac units - else if (ConvertToDifferentRange(DAC_MIN_MV, DAC_MAX_MV, LTC2620_MIN_VAL, - LTC2620_MAX_VAL, val, &dacval) == OK) { - (detectorModules)->dacs[ind] = dacval; - } -#else + // mV: convert to dac value int dacval = val; if (mV) { - // convert to dac units - if (ConvertToDifferentRange(DAC_MIN_MV, DAC_MAX_MV, LTC2620_MIN_VAL, - LTC2620_MAX_VAL, val, &dacval) == FAIL) { - LOG(logERROR, - ("Could not convert %d mV for dac to dac units\n", val)); - return; + if (ConvertToDifferentRange(DAC_MIN_MV, DAC_MAX_MV, LTC2620_MIN_VAL, LTC2620_MAX_VAL, val, &dacval) == FAIL) { + sprintf(mess, "Could not set DAC %s. Could not convert %d mV to dac units.\n", dacNames[ind], val); + LOG(logERROR, (mess)); + return FAIL; } } + +#ifdef VIRTUAL + (detectorModules)->dacs[ind] = dacval; +#else sharedMemory_lockLocalLink(); if (Feb_Control_SetDAC(ind, dacval)) { (detectorModules)->dacs[ind] = dacval; } sharedMemory_unlockLocalLink(); #endif + return OK; } -int getDAC(enum DACINDEX ind, int mV) { +int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess) { + // validate index (including E_VTHRESHOLD) + if (ind < 0 || ind >= NDAC + 1) { + sprintf(mess, "Could not set DAC. Invalid index %d\n", ind); + LOG(logERROR, (mess)); + return FAIL; + } + + + if (ind == E_VTHRESHOLD) { - int ret[5] = {0}; - ret[0] = getDAC(E_VCMP_LL, mV); - ret[1] = getDAC(E_VCMP_LR, mV); - ret[2] = getDAC(E_VCMP_RL, mV); - ret[3] = getDAC(E_VCMP_RR, mV); - ret[4] = getDAC(E_VCP, mV); + int retval[5] = {0}; + if (getDAC(E_VCMP_LL, mV, &retval[0], mess) == FAIL) + return FAIL; + if (getDAC(E_VCMP_LR, mV, &retval[1], mess) == FAIL) + return FAIL; + if (getDAC(E_VCMP_RL, mV, &retval[2], mess) == FAIL) + return FAIL; + if (getDAC(E_VCMP_RR, mV, &retval[3], mess) == FAIL) + return FAIL; + if (getDAC(E_VCP, mV, &retval[4], mess) == FAIL) + return FAIL; - if ((ret[0] == ret[1]) && (ret[1] == ret[2]) && (ret[2] == ret[3]) && - (ret[3] == ret[4])) { - LOG(logINFO, ("\tvthreshold match\n")); - return ret[0]; - } else { - LOG(logERROR, ("\tvthreshold mismatch vcmp_ll:%d vcmp_lr:%d " - "vcmp_rl:%d vcmp_rr:%d vcp:%d\n", - ret[0], ret[1], ret[2], ret[3], ret[4])); - return -1; + if ((retval[0] != retval[1]) || (retval[1] != retval[2]) || (retval[2] != retval[3]) || (retval[3] != retval[4])) { + sprintf(mess, "Vthreshold mismatch. vcmp_ll:%d vcmp_lr:%d vcmp_rl:%d vcmp_rr:%d vcp:%d\n", retval[0], retval[1], retval[2], retval[3], retval[4]); + LOG(logERROR, (mess)); + return FAIL; } - } + LOG(logINFO, ("\tvthreshold match\n")); + *retval = retval[0]; + return OK; + } + char *dacNames[] = {DAC_NAMES}; if (!mV) { - LOG(logDEBUG1, - ("Getting DAC %d : %d dac\n", ind, (detectorModules)->dacs[ind])); - return (detectorModules)->dacs[ind]; + LOG(logDEBUG1, ("Getting DAC %s : %d dac\n", dacNames[ind], (detectorModules)->dacs[ind])); + *retval = (detectorModules)->dacs[ind]; + return OK; } - int voltage = -1; - // dac units to voltage - ConvertToDifferentRange(LTC2620_MIN_VAL, LTC2620_MAX_VAL, DAC_MIN_MV, - DAC_MAX_MV, (detectorModules)->dacs[ind], &voltage); - LOG(logDEBUG1, ("Getting DAC %d : %d dac (%d mV)\n", ind, - (detectorModules)->dacs[ind], voltage)); - return voltage; + // convert to mV + *retval = -1; + if (ConvertToDifferentRange(LTC2620_MIN_VAL, LTC2620_MAX_VAL, DAC_MIN_MV, DAC_MAX_MV, (detectorModules)->dacs[ind], retval) == FAIL) { + sprintf(mess, "Could not set DAC %s. Could not convert %d mV to dac units.\n", dacNames[ind], (detectorModules)->dacs[ind]); + LOG(logERROR, (mess)); + return FAIL; + } + LOG(logDEBUG1, + ("Getting DAC %s : %d dac (%d mV)\n", dacNames[ind], (detectorModules)->dacs[ind], *retval)); + return OK; } -int getMaxDacSteps() { return DAC_MAX_STEPS; } int getADC(enum ADCINDEX ind) { #ifdef VIRTUAL @@ -2157,7 +2196,8 @@ int validateAndSetRateCorrection(int64_t tau_ns, char *mess) { } // user defined value (settings become undefined) else if (tau_ns > 0) { - setSettings(UNDEFINED); + if (setSettings(UNDEFINED, mess) == FAIL) + return FAIL; LOG(logERROR, ("Settings has been changed to undefined (tau changed)\n")); eiger_tau_ns = tau_ns; @@ -2360,7 +2400,8 @@ int setTrimbits(int *chanregs, char *mess) { if (!Feb_Control_SetTrimbits(tt, top)) { sprintf(mess, "Could not set module. Could not set trimbits\n"); LOG(logERROR, (mess)); - setSettings(UNDEFINED); + if (setSettings(UNDEFINED, mess) == FAIL) + return FAIL; LOG(logERROR, ("Settings has been changed to undefined (random " "trim file)\n")); diff --git a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.h index 55c07ecfa..6b7efe09a 100644 --- a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.h @@ -69,7 +69,7 @@ void setupFebBeb(); int allocateDetectorStructureMemory(); void setupDetector(); -int resetToDefaultDacs(int hardReset); +int resetToDefaultDacs(int hardReset, char* mess); int getDefaultDac(enum DACINDEX index, enum detectorSettings sett, int *retval); int setDefaultDac(enum DACINDEX index, enum detectorSettings sett, int value); int readConfigFile(); @@ -115,7 +115,7 @@ int64_t getMeasuredSubPeriod(); void getModule(sls_detector_module *myMod); int setModule(sls_detector_module myMod, char *mess); int setTrimbits(int *chanregs, char *mess); -enum detectorSettings setSettings(enum detectorSettings sett); +int setSettings(enum detectorSettings sett, char* mess); enum detectorSettings getSettings(); // parameters - threshold @@ -123,9 +123,9 @@ int getThresholdEnergy(); int setThresholdEnergy(int ev); // parameters - dac, adc, hv -void setDAC(enum DACINDEX ind, int val, int mV); -int getDAC(enum DACINDEX ind, int mV); -int getMaxDacSteps(); +int validateDAC(enum DACINDEX ind, int val, int mV, char* mess); +int setDAC(enum DACINDEX ind, int val, int mV, char* mess); +int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess); int getADC(enum ADCINDEX ind); int setHighVoltage(int val, char* mess); diff --git a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c index 97dcb5f46..0145bb0bb 100644 --- a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c @@ -522,7 +522,9 @@ void setupDetector() { setBurstMode(DEFAULT_BURST_MODE); setFilterResistor(DEFAULT_FILTER_RESISTOR); setCDSGain(DEFAILT_CDS_GAIN); - setSettings(DEFAULT_SETTINGS); + initError = setSettings(DEFAULT_SETTINGS, initErrorMessage); + if (initError == FAIL) + return; // Initialization of acquistion parameters setNextFrameNumber(DEFAULT_FRAME_NUMBER); @@ -558,7 +560,7 @@ void setASICDefaults() { LOG(logINFO, ("Setting ASIC Defaults (0x%x)\n", bus_r(addr))); } -int resetToDefaultDacs(int hardReset) { +int resetToDefaultDacs(int hardReset, char* mess) { // reset defaults to hardcoded defaults if (hardReset) { for (int i = 0; i < NDAC; ++i) { @@ -570,12 +572,8 @@ int resetToDefaultDacs(int hardReset) { LOG(logINFOBLUE, ("Setting Default Dac values\n")); for (int i = 0; i < NDAC; ++i) { if (defaultDacValues[i] != -1) { - setDAC((enum DACINDEX)i, defaultDacValues[i], 0); - if (dacValues[i] != defaultDacValues[i]) { - ret = FAIL; - LOG(logERROR, ("Setting dac %d failed, wrote %d, read %d\n", i, - defaultDacValues[i], dacValues[i])); - } + if (setDAC((enum DACINDEX)i, defaultDacValues[i], 0, mess) == FAIL) + return FAIL; } } LOG(logINFOBLUE, ("Setting Default On-chip Dac values\n")); @@ -586,17 +584,17 @@ int resetToDefaultDacs(int hardReset) { defaultOnChipdacValues[idac][ichip]); if (onChipdacValues[idac][ichip] != defaultOnChipdacValues[idac][ichip]) { - ret = FAIL; - LOG(logERROR, - ("Setting on-chip dac %d (ichip:%d) failed, " + sprintf(mess, "Setting on-chip dac %d (ichip:%d) failed, " "wrote %d, read %d\n", idac, ichip, defaultOnChipdacValues[idac][ichip], - onChipdacValues[idac][ichip])); + onChipdacValues[idac][ichip]); + LOG(logERROR, (mess)); + return FAIL; } } } } - return ret; + return OK; } int getDefaultDac(enum DACINDEX index, enum detectorSettings sett, @@ -950,13 +948,11 @@ int readConfigFile() { hardCodedDefaultDacValues[idac] = value; // set dac - setDAC(idac, value, 0); - int retval = getDAC(idac, 0); - if (retval != value) { + if (setDAC(idac, value, 0, initErrorMessage) == FAIL) {; sprintf(initErrorMessage, "Set dac %s failed from on-board server config file. " - "Set %d, got %d.\n", - command, value, retval); + "Could not set %d.\n", + command, value); break; } } @@ -1399,9 +1395,12 @@ int64_t getMeasurementTime() { } /* parameters - module, settings */ -enum detectorSettings setSettings(enum detectorSettings sett) { - if (sett == UNINITIALIZED) - return thisSettings; +int setSettings(enum detectorSettings sett, char* mess) { + if (sett == UNINITIALIZED) { + sprintf(mess, "Cannot set settings to uninitialized\n"); + LOG(logERROR, (mess)); + return FAIL; + } // set settings uint32_t addr = ASIC_CONFIG_REG; @@ -1426,13 +1425,13 @@ enum detectorSettings setSettings(enum detectorSettings sett) { ("Set settings - Fix Gain 2, val: 0x%x\n", bus_r(addr) & mask)); break; default: - LOG(logERROR, - ("This settings is not defined for this detector %d\n", (int)sett)); - return -1; + sprintf(mess, "Undefined settings %d\n", (int)sett); + LOG(logERROR, (mess)); + return FAIL; } thisSettings = sett; - return getSettings(); + return OK; } enum detectorSettings getSettings() { @@ -1520,47 +1519,80 @@ int getOnChipDAC(enum ONCHIP_DACINDEX ind, int chipIndex) { // specific chip return onChipdacValues[ind][chipIndex]; } +int validateDAC(enum DACINDEX ind, int val, int mV, char* mess) { + char *dacNames[] = {DAC_NAMES}; -void setDAC(enum DACINDEX ind, int val, int mV) { + // validate index + if (ind < 0 || ind >= NDAC) { + sprintf(mess, "Could not set DAC. Invalid index %d\n", ind); + LOG(logERROR, (mess)); + return FAIL; + } + // validate min value if (val < 0) { - return; + sprintf(mess, "Could not set DAC %s. Input value %d cannot be negative\n", dacNames[ind], val); + LOG(logERROR, (mess)); + return FAIL; } + // validate max value + if (mV && val > DAC_MAX_MV) { + sprintf(mess, "Could not set DAC %s. Input value %d exceed maximum %d mV\n", dacNames[ind], val, DAC_MAX_MV); + LOG(logERROR, (mess)); + return FAIL; + } + else if (!mV && val > LTC2620_D_GetMaxInput()) { + sprintf(mess, "Could not set DAC %s. Input value %d exceed maximum %d \n", dacNames[ind], val, LTC2620_D_GetMaxInput()); + LOG(logERROR, (mess)); + return FAIL; + } + return OK; +} - char *dac_names[] = {DAC_NAMES}; - LOG(logDEBUG1, ("Setting dac[%d - %s]: %d %s \n", (int)ind, dac_names[ind], - val, (mV ? "mV" : "dac units"))); +int setDAC(enum DACINDEX ind, int val, int mV, char* mess) { + if (validateDAC(ind, val, mV, mess) == FAIL) + return FAIL; + + char *dacNames[] = {DAC_NAMES}; + LOG(logINFO, ("Setting DAC %s: %d %s \n", dacNames[ind], val, (mV ? "mV" : "dac units"))); + + // mV: convert to dac value int dacval = val; -#ifdef VIRTUAL - LOG(logINFO, ("Setting dac[%d - %s]: %d %s \n", (int)ind, dac_names[ind], - val, (mV ? "mV" : "dac units"))); - if (!mV) { - dacValues[ind] = val; + if (mV) { + if (LTC2620_D_VoltageToDac(val, &dacval) == FAIL) { + sprintf(mess, "Could not set DAC %s. Could not convert %d mV to dac units.\n", dacNames[ind], val); + LOG(logERROR, (mess)); + return FAIL; + } } - // convert to dac units - else if (LTC2620_D_VoltageToDac(val, &dacval) == OK) { - dacValues[ind] = dacval; + + if (LTC2620_D_SetDACValue((int)ind, val) == FAIL) { + sprintf(mess, "Could not set DAC %s.\n", dacNames[ind]); + LOG(logERROR, (mess)); + return FAIL; } -#else - if (LTC2620_D_SetDACValue((int)ind, val, mV, dac_names[ind], &dacval) == - OK) { - dacValues[ind] = dacval; - } -#endif + dacValues[ind] = dacval; } -int getDAC(enum DACINDEX ind, int mV) { + +int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess) { + char *dacNames[] = {DAC_NAMES}; if (!mV) { - LOG(logDEBUG1, ("Getting DAC %d : %d dac\n", ind, dacValues[ind])); - return dacValues[ind]; + LOG(logDEBUG1, ("Getting DAC %s : %d dac\n", dacNames[ind], dacValues[ind])); + *retval = dacValues[ind]; + return OK; + } + // convert to mV + *retval = -1; + if (LTC2620_D_DacToVoltage(dacValues[ind], retval) == FAIL) { + sprintf(mess, "Could not get DAC %s. Could not convert %d dac units to mV\n", dacNames[ind], dacValues[ind]); + LOG(logERROR, (mess)); + return FAIL; } - int voltage = -1; - LTC2620_D_DacToVoltage(dacValues[ind], &voltage); LOG(logDEBUG1, - ("Getting DAC %d : %d dac (%d mV)\n", ind, dacValues[ind], voltage)); - return voltage; + ("Getting DAC %s : %d dac (%d mV)\n", dacNames[ind], dacValues[ind], *retval)); + return OK; } -int getMaxDacSteps() { return LTC2620_D_GetMaxNumSteps(); } int getADC(enum ADCINDEX ind, int *value) { LOG(logDEBUG1, ("Reading FPGA temperature...\n")); diff --git a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.h index 608e87a4c..03a8f0924 100644 --- a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.h @@ -62,7 +62,7 @@ void initStopServer(); // set up detector void setupDetector(); -int resetToDefaultDacs(int hardReset); +int resetToDefaultDacs(int hardReset, char* mess); int getDefaultDac(enum DACINDEX index, enum detectorSettings sett, int *retval); int setDefaultDac(enum DACINDEX index, enum detectorSettings sett, int value); void setASICDefaults(); @@ -109,15 +109,17 @@ int64_t getActualTime(); int64_t getMeasurementTime(); // parameters - module, settings -enum detectorSettings setSettings(enum detectorSettings sett); +int setSettings(enum detectorSettings sett, char* mess); enum detectorSettings getSettings(); // parameters - dac, adc, hv int setOnChipDAC(enum ONCHIP_DACINDEX ind, int chipIndex, int val); int getOnChipDAC(enum ONCHIP_DACINDEX ind, int chipIndex); -void setDAC(enum DACINDEX ind, int val, int mV); -int getDAC(enum DACINDEX ind, int mV); -int getMaxDacSteps(); + +int validateDAC(enum DACINDEX ind, int val, int mV, char* mess); +int setDAC(enum DACINDEX ind, int val, int mV, char* mess); +int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess); + int getADC(enum ADCINDEX ind, int *value); int setHighVoltage(int val, char* mess); diff --git a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c index ab7115596..9673ceae8 100644 --- a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c @@ -507,7 +507,9 @@ void setupDetector() { DAC_MAX_MV); LTC2620_Disable(); LTC2620_Configure(); - resetToDefaultDacs(0); + initError = resetToDefaultDacs(0, initErrorMessage); + if (initError == FAIL) + return; /* Only once at server startup */ bus_w(DAQ_REG, 0x0); @@ -537,7 +539,9 @@ void setupDetector() { // Initialization of acquistion parameters disableCurrentSource(); - setSettings(DEFAULT_SETTINGS); + initError = setSettings(DEFAULT_SETTINGS, initErrorMessage); + if (initError == FAIL) + return; setGainMode(DEFAULT_GAINMODE); setNumFrames(DEFAULT_NUM_FRAMES); @@ -579,7 +583,7 @@ void setupDetector() { setElectronCollectionMode(DEFAULT_ELECTRON_COLLECTION_MODE); } -int resetToDefaultDacs(int hardReset) { +int resetToDefaultDacs(int hardReset, char* mess) { LOG(logINFOBLUE, ("Resetting %s to Default Dac values\n", (hardReset == 1 ? "hard" : ""))); @@ -624,13 +628,9 @@ int resetToDefaultDacs(int hardReset) { } } - // set to defualt - setDAC((enum DACINDEX)i, value, 0); - if (dacValues[i] != value) { - LOG(logERROR, ("Setting dac %d failed, wrote %d, read %d\n", i, - value, dacValues[i])); + // set to default + if (setDAC((enum DACINDEX)i, value, 0, mess) == FAIL) return FAIL; - } } return OK; } @@ -1181,17 +1181,23 @@ int setModule(sls_detector_module myMod, char *mess) { LOG(logINFO, ("Setting module with settings %d\n", myMod.reg)); // settings - setSettings((enum detectorSettings)myMod.reg); + if (setSettings((enum detectorSettings)myMod.reg, mess) == FAIL) + return FAIL; // set dac values - for (int i = 0; i < NDAC; ++i) - setDAC((enum DACINDEX)i, myMod.dacs[i], 0); + for (int i = 0; i < NDAC; ++i) { + if (setDAC((enum DACINDEX)i, myMod.dacs[i], 0, mess) == FAIL) + return FAIL; + } return OK; } -enum detectorSettings setSettings(enum detectorSettings sett) { - if (sett == UNINITIALIZED) - return thisSettings; +int setSettings(enum detectorSettings sett, char* mess) { + if (sett == UNINITIALIZED) { + sprintf(mess, "Cannot set settings to uninitialized\n"); + LOG(logERROR, (mess)); + return FAIL; + } int *dacVals = NULL; // set settings @@ -1209,8 +1215,9 @@ enum detectorSettings setSettings(enum detectorSettings sett) { dacVals = defaultDacValue_HG0; break; default: - LOG(logERROR, ("This settings %d is not defined\n", (int)sett)); - return -1; + sprintf(mess, "Undefined settings %d\n", (int)sett); + LOG(logERROR, (mess)); + return FAIL; } thisSettings = sett; @@ -1218,13 +1225,14 @@ enum detectorSettings setSettings(enum detectorSettings sett) { // set special dacs const int specialDacs[] = SPECIALDACINDEX; for (int i = 0; i < NSPECIALDACS; ++i) { - setDAC(specialDacs[i], dacVals[i], 0); + if (setDAC(specialDacs[i], dacVals[i], 0, mess) == FAIL) + return FAIL; } // if chipv1.1 and powered on configureChip(); - return getSettings(); + return OK; } enum detectorSettings getSettings() { @@ -1328,51 +1336,100 @@ void setGainMode(enum gainMode mode) { } /* parameters - dac, adc, hv */ -void setDAC(enum DACINDEX ind, int val, int mV) { - if (val < 0) - return; +int validateDAC(enum DACINDEX ind, int val, int mV, char* mess) { + char *dacNames[] = {DAC_NAMES}; - char *dac_names[] = {DAC_NAMES}; + // validate index + if (ind < 0 || ind >= NDAC) { + sprintf(mess, "Could not set DAC. Invalid index %d\n", ind); + LOG(logERROR, (mess)); + return FAIL; + } + // validate mV + if (mV && val == LTC2620_GetPowerDownValue()) { + sprintf(mess, "Could not set DAC %s. Cannot use power down value and use 'mV'\n", dacNames[ind]); + LOG(logERROR, (mess)); + return FAIL; + } + // validate min value + if (val < 0 && val != LTC2620_GetPowerDownValue()) { + sprintf(mess, "Could not set DAC %s. Input value %d cannot be negative\n", dacNames[ind], val); + LOG(logERROR, (mess)); + return FAIL; + } + // validate max value + if (mV && val > DAC_MAX_MV) { + sprintf(mess, "Could not set DAC %s. Input value %d exceed maximum %d mV\n", dacNames[ind], val, DAC_MAX_MV); + LOG(logERROR, (mess)); + return FAIL; + } + else if (!mV && val > LTC2620_GetMaxInput()) { + sprintf(mess, "Could not set DAC %s. Input value %d exceed maximum %d \n", dacNames[ind], val, LTC2620_GetMaxInput()); + LOG(logERROR, (mess)); + return FAIL; + } + return OK; +} + +int setDAC(enum DACINDEX ind, int val, int mV, char* mess) { + if (validateDAC(ind, val, mV, mess) == FAIL) + return FAIL; + + char *dacNames[] = {DAC_NAMES}; + LOG(logINFO, ("Setting DAC %s: %d %s \n", dacNames[ind], val, (mV ? "mV" : "dac units"))); + + // mV: convert to dac value int dacval = val; -#ifdef VIRTUAL - LOG(logINFO, ("Setting dac[%d - %s]: %d %s \n", (int)ind, dac_names[ind], - val, (mV ? "mV" : "dac units"))); - if (!mV) { - dacValues[ind] = val; - } - // convert to dac units - else if (LTC2620_VoltageToDac(val, &dacval) == OK) { - dacValues[ind] = dacval; - } -#else - LOG(logINFO, ("Setting DAC %s\n", dac_names[ind])); - if (LTC2620_SetDACValue((int)ind, val, mV, &dacval) == OK) { - dacValues[ind] = dacval; - if (ind == J_VREF_COMP && - (val >= 0)) { // FIXME: if val == pwr down value, write 0? - bus_w(EXT_DAQ_CTRL_REG, - (bus_r(EXT_DAQ_CTRL_REG) & - ~(EXT_DAQ_CTRL_VREF_COMP_MSK)) // reset - | ((val << EXT_DAQ_CTRL_VREF_COMP_OFST) & - EXT_DAQ_CTRL_VREF_COMP_MSK)); // or it with value + if (mV) { + if (LTC2620_VoltageToDac(val, &dacval) == FAIL) { + sprintf(mess, "Could not set DAC %s. Could not convert %d mV to dac units.\n", dacNames[ind], val); + LOG(logERROR, (mess)); + return FAIL; } } -#endif -} -int getDAC(enum DACINDEX ind, int mV) { - if (!mV) { - LOG(logDEBUG1, ("Getting DAC %d : %d dac\n", ind, dacValues[ind])); - return dacValues[ind]; + if (LTC2620_SetDACValue((int)ind, dacval) == FAIL) { + sprintf(mess, "Could not set DAC %s.\n", dacNames[ind]); + LOG(logERROR, (mess)); + return FAIL; } - int voltage = -1; - LTC2620_DacToVoltage(dacValues[ind], &voltage); - LOG(logDEBUG1, - ("Getting DAC %d : %d dac (%d mV)\n", ind, dacValues[ind], voltage)); - return voltage; + + dacValues[ind] = dacval; + + if (ind == J_VREF_COMP) { + uint32_t addr = EXT_DAQ_CTRL_REG; + if (val == LTC2620_GetPowerDownValue()) + val = 0; + bus_w(addr, (bus_r(addr) & ~(EXT_DAQ_CTRL_VREF_COMP_MSK))); + bus_w(addr, (bus_r(addr) | ((val << EXT_DAQ_CTRL_VREF_COMP_OFST) & EXT_DAQ_CTRL_VREF_COMP_MSK))); + } + return OK; } -int getMaxDacSteps() { return LTC2620_GetMaxNumSteps(); } +int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess) { + // validate index + if (ind < 0 || ind >= NDAC) { + sprintf(mess, "Could not get DAC %d. Invalid index.\n", ind); + LOG(logERROR, (mess)); + return FAIL; + } + char *dacNames[] = {DAC_NAMES}; + if (!mV) { + LOG(logDEBUG1, ("Getting DAC %s : %d dac\n", dacNames[ind], dacValues[ind])); + *retval = dacValues[ind]; + return OK; + } + // convert to mV + *retval = -1; + if (LTC2620_DacToVoltage(dacValues[ind], retval) == FAIL) { + sprintf(mess, "Could not get DAC %s. Could not convert %d dac units to mV\n", dacNames[ind], dacValues[ind]); + LOG(logERROR, (mess)); + return FAIL; + } + LOG(logDEBUG1, + ("Getting DAC %s : %d dac (%d mV)\n", dacNames[ind], dacValues[ind], *retval)); + return OK; +} int getADC(enum ADCINDEX ind) { #ifdef VIRTUAL diff --git a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.h index 2adde51f1..495a41d6a 100644 --- a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.h @@ -71,7 +71,7 @@ void initStopServer(); // set up detector void setupDetector(); -int resetToDefaultDacs(int hardReset); +int resetToDefaultDacs(int hardReset, char* mess); int getDefaultDac(enum DACINDEX index, enum detectorSettings sett, int *retval); int setDefaultDac(enum DACINDEX index, enum detectorSettings sett, int value); int readConfigFile(); @@ -116,15 +116,16 @@ int64_t getMeasurementTime(); // parameters - module, settings int setModule(sls_detector_module myMod, char *mess); -enum detectorSettings setSettings(enum detectorSettings sett); +int setSettings(enum detectorSettings sett, char* mess); enum detectorSettings getSettings(); enum gainMode getGainMode(); void setGainMode(enum gainMode mode); // parameters - dac, adc, hv -void setDAC(enum DACINDEX ind, int val, int mV); -int getDAC(enum DACINDEX ind, int mV); -int getMaxDacSteps(); +int validateDAC(enum DACINDEX ind, int val, int mV, char* mess); +int setDAC(enum DACINDEX ind, int val, int mV, char* mess); +int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess); + int getADC(enum ADCINDEX ind); int setHighVoltage(int val, char* mess); int getHighVoltage(int *retval, char* mess); diff --git a/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c index 21922342d..94a2ab4a5 100644 --- a/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c @@ -479,7 +479,9 @@ void setupDetector() { DAC_MAX_MV); LTC2620_Disable(); LTC2620_Configure(); - resetToDefaultDacs(0); + initError = resetToDefaultDacs(0, initErrorMessage); + if (initError == FAIL) + return; LOG(logINFOBLUE, ("Setting Default parameters\n")); @@ -501,7 +503,9 @@ void setupDetector() { // Initialization of acquistion parameters setReadoutSpeed(DEFAULT_SPEED); - setSettings(DEFAULT_SETTINGS); + initError = (DEFAULT_SETTINGS, initErrorMessage); + if (initError == FAIL) + return; setNumFrames(DEFAULT_NUM_FRAMES); setNumTriggers(DEFAULT_NUM_CYCLES); setExpTime(DEFAULT_EXPTIME); @@ -520,7 +524,7 @@ void setupDetector() { setParallelMode(DEFAULT_PARALLEL_ENABLE); } -int resetToDefaultDacs(int hardReset) { +int resetToDefaultDacs(int hardReset, char* mess) { LOG(logINFOBLUE, ("Resetting %s to Default Dac values\n", (hardReset == 1 ? "hard" : ""))); @@ -536,12 +540,8 @@ int resetToDefaultDacs(int hardReset) { for (int i = 0; i < NDAC; ++i) { int value = defaultDacValues[i]; // set to defualt - setDAC((enum DACINDEX)i, value, 0); - if (dacValues[i] != value) { - LOG(logERROR, ("Setting dac %d failed, wrote %d, read %d\n", i, - value, dacValues[i])); + if (setDAC((enum DACINDEX)i, value, 0, mess) == FAIL) return FAIL; - } } return OK; } @@ -809,17 +809,23 @@ int setModule(sls_detector_module myMod, char *mess) { LOG(logINFO, ("Setting module with settings %d\n", myMod.reg)); // settings - setSettings((enum detectorSettings)myMod.reg); + if (setSettings((enum detectorSettings)myMod.reg, mess) == FAIL) + return FAIL; // set dac values - for (int i = 0; i < NDAC; ++i) - setDAC((enum DACINDEX)i, myMod.dacs[i], 0); + for (int i = 0; i < NDAC; ++i) { + if (setDAC((enum DACINDEX)i, myMod.dacs[i], 0, mess) == FAIL) + return FAIL; + } return OK; } -enum detectorSettings setSettings(enum detectorSettings sett) { - if (sett == UNINITIALIZED) - return thisSettings; +int setSettings(enum detectorSettings sett, char* mess) { + if (sett == UNINITIALIZED) { + sprintf(mess, "Cannot set settings to uninitialized\n"); + LOG(logERROR, (mess)); + return FAIL; + } uint32_t mask = 0; // set settings @@ -857,8 +863,9 @@ enum detectorSettings setSettings(enum detectorSettings sett) { mask = SETTINGS_G4_LG; break; default: - LOG(logERROR, ("This settings %d is not defined\n", (int)sett)); - return -1; + sprintf(mess, "Undefined settings %d\n", (int)sett); + LOG(logERROR, (mess)); + return FAIL; } uint32_t addr = ASIC_CTRL_REG; @@ -866,7 +873,7 @@ enum detectorSettings setSettings(enum detectorSettings sett) { bus_w(addr, bus_r(addr) | mask); thisSettings = sett; - return getSettings(); + return OK; } enum detectorSettings getSettings() { @@ -904,43 +911,93 @@ enum detectorSettings getSettings() { } /* parameters - dac, adc, hv */ -void setDAC(enum DACINDEX ind, int val, int mV) { - if (val < 0) - return; +int validateDAC(enum DACINDEX ind, int val, int mV, char* mess) { + char *dacNames[] = {DAC_NAMES}; - char *dac_names[] = {DAC_NAMES}; + // validate index + if (ind < 0 || ind >= NDAC) { + sprintf(mess, "Could not set DAC. Invalid index %d\n", ind); + LOG(logERROR, (mess)); + return FAIL; + } + // validate mV + if (mV && val == LTC2620_GetPowerDownValue()) { + sprintf(mess, "Could not set DAC %s. Cannot use power down value and use 'mV'\n", dacNames[ind]); + LOG(logERROR, (mess)); + return FAIL; + } + // validate min value + if (val < 0 && val != LTC2620_GetPowerDownValue()) { + sprintf(mess, "Could not set DAC %s. Input value %d cannot be negative\n", dacNames[ind], val); + LOG(logERROR, (mess)); + return FAIL; + } + // validate max value + if (mV && val > DAC_MAX_MV) { + sprintf(mess, "Could not set DAC %s. Input value %d exceed maximum %d mV\n", dacNames[ind], val, DAC_MAX_MV); + LOG(logERROR, (mess)); + return FAIL; + } + else if (!mV && val > LTC2620_GetMaxInput()) { + sprintf(mess, "Could not set DAC %s. Input value %d exceed maximum %d \n", dacNames[ind], val, LTC2620_GetMaxInput()); + LOG(logERROR, (mess)); + return FAIL; + } + return OK; +} + +int setDAC(enum DACINDEX ind, int val, int mV, char* mess) { + if (validateDAC(ind, val, mV, mess) == FAIL) + return FAIL; + + char *dacNames[] = {DAC_NAMES}; + LOG(logINFO, ("Setting DAC %s: %d %s \n", dacNames[ind], val, (mV ? "mV" : "dac units"))); + + // mV: convert to dac value int dacval = val; -#ifdef VIRTUAL - LOG(logINFO, ("Setting dac[%d - %s]: %d %s \n", (int)ind, dac_names[ind], - val, (mV ? "mV" : "dac units"))); - if (!mV) { - dacValues[ind] = val; + if (mV) { + if (LTC2620_VoltageToDac(val, &dacval) == FAIL) { + sprintf(mess, "Could not set DAC %s. Could not convert %d mV to dac units.\n", dacNames[ind], val); + LOG(logERROR, (mess)); + return FAIL; + } } - // convert to dac units - else if (LTC2620_VoltageToDac(val, &dacval) == OK) { - dacValues[ind] = dacval; + + if (LTC2620_SetDACValue((int)ind, dacval) == FAIL) { + sprintf(mess, "Could not set DAC %s.\n", dacNames[ind]); + LOG(logERROR, (mess)); + return FAIL; } -#else - LOG(logINFO, ("Setting DAC %s\n", dac_names[ind])); - if (LTC2620_SetDACValue((int)ind, val, mV, &dacval) == OK) { - dacValues[ind] = dacval; - } -#endif + + dacValues[ind] = dacval; + return OK; } -int getDAC(enum DACINDEX ind, int mV) { - if (!mV) { - LOG(logDEBUG1, ("Getting DAC %d : %d dac\n", ind, dacValues[ind])); - return dacValues[ind]; +int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess) { + // validate index + if (ind < 0 || ind >= NDAC) { + sprintf(mess, "Could not get DAC %d. Invalid index.\n", ind); + LOG(logERROR, (mess)); + return FAIL; + } + char *dacNames[] = {DAC_NAMES}; + if (!mV) { + LOG(logDEBUG1, ("Getting DAC %s : %d dac\n", dacNames[ind], dacValues[ind])); + *retval = dacValues[ind]; + return OK; + } + // convert to mV + *retval = -1; + if (LTC2620_DacToVoltage(dacValues[ind], retval) == FAIL) { + sprintf(mess, "Could not get DAC %s. Could not convert %d dac units to mV\n", dacNames[ind], dacValues[ind]); + LOG(logERROR, (mess)); + return FAIL; } - int voltage = -1; - LTC2620_DacToVoltage(dacValues[ind], &voltage); LOG(logDEBUG1, - ("Getting DAC %d : %d dac (%d mV)\n", ind, dacValues[ind], voltage)); - return voltage; + ("Getting DAC %s : %d dac (%d mV)\n", dacNames[ind], dacValues[ind], *retval)); + return OK; } -int getMaxDacSteps() { return LTC2620_GetMaxNumSteps(); } int getADC(enum ADCINDEX ind) { #ifdef VIRTUAL diff --git a/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.h index 7dd2c7d96..0c3eb61a8 100644 --- a/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.h @@ -69,7 +69,7 @@ void initStopServer(); // set up detector void setupDetector(); -int resetToDefaultDacs(int hardReset); +int resetToDefaultDacs(int hardReset, char* mess); int getDefaultDac(enum DACINDEX index, enum detectorSettings sett, int *retval); int setDefaultDac(enum DACINDEX index, enum detectorSettings sett, int value); @@ -111,13 +111,13 @@ int64_t getMeasurementTime(); // parameters - module, settings int setModule(sls_detector_module myMod, char *mess); -enum detectorSettings setSettings(enum detectorSettings sett); +int setSettings(enum detectorSettings sett, char* mess); enum detectorSettings getSettings(); // parameters - dac, adc, hv -void setDAC(enum DACINDEX ind, int val, int mV); -int getDAC(enum DACINDEX ind, int mV); -int getMaxDacSteps(); +int validateDAC(enum DACINDEX ind, int val, int mV, char* mess); +int setDAC(enum DACINDEX ind, int val, int mV, char* mess); +int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess); int getADC(enum ADCINDEX ind); int setHighVoltage(int val, char* mess); int getHighVoltage(int *retval, char* mess); diff --git a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c index 7d2763720..b34b2900e 100644 --- a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c @@ -505,7 +505,9 @@ void setupDetector() { // enable all counters before setting dacs (vthx) setCounterMask(MAX_COUNTER_MSK); - resetToDefaultDacs(0); + initError = resetToDefaultDacs(0, initErrorMessage); + if (initError == FAIL) + return; // set trigger flow for m3 (for all timing modes) bus_w(FLOW_TRIGGER_REG, bus_r(FLOW_TRIGGER_REG) | FLOW_TRIGGER_MSK); @@ -528,7 +530,9 @@ void setupDetector() { setInitialExtSignals(); // 10G UDP enableTenGigabitEthernet(1); - setSettings(DEFAULT_SETTINGS); + initError = setSettings(DEFAULT_SETTINGS, initErrorMessage); + if (initError == FAIL) + return; // check module type attached if not in debug mode if (initError == FAIL) @@ -552,7 +556,7 @@ void setupDetector() { setReadoutSpeed(DEFAULT_READOUT_SPEED); } -int resetToDefaultDacs(int hardReset) { +int resetToDefaultDacs(int hardReset, char* mess) { LOG(logINFOBLUE, ("Resetting %s to Default Dac values\n", (hardReset == 1 ? "hard" : ""))); @@ -605,12 +609,8 @@ int resetToDefaultDacs(int hardReset) { } // set to default (last arg to ensure counter check) - setDAC((enum DACINDEX)i, value, 0, 1); - if (detectorDacs[i] != value) { - LOG(logERROR, ("Setting dac %d failed, wrote %d, read %d\n", i, - value, detectorDacs[i])); + if (setDAC((enum DACINDEX)i, value, 0, 1, mess) == FAIL) return FAIL; - } } return OK; } @@ -1267,24 +1267,6 @@ int64_t getMeasurementTime() { /* parameters - module, speed, readout */ -int setDACS(int *dacs) { - for (int i = 0; i < NDAC; ++i) { - if (dacs[i] != -1) { - // set to default (last arg to ensure counter check) - setDAC((enum DACINDEX)i, dacs[i], 0, 1); - if (dacs[i] != detectorDacs[i]) { - // dont complain if that counter was disabled - if ((i == M_VTH1 || i == M_VTH2 || i == M_VTH3) && - (detectorDacs[i] == DEFAULT_COUNTER_DISABLED_VTH_VAL)) { - continue; - } - return FAIL; - } - } - } - return OK; -} - void getModule(sls_detector_module *myMod) { // serial number myMod->serialnumber = detectorModules->serialnumber; @@ -1326,13 +1308,21 @@ int setModule(sls_detector_module myMod, char *mess) { return FAIL; } - // dacs - if (setDACS(myMod.dacs)) { - sprintf(mess, "Could not set dacs\n"); - LOG(logERROR, (mess)); - return FAIL; + // dacs myMod.dacs + for (int i = 0; i < NDAC; ++i) { + if (myMod.dacs[i] != -1) { + // set to default (last arg to ensure counter check) + if (setDAC((enum DACINDEX)i, myMod.dacs[i], 0, 1, mess) == FAIL) { + // dont complain if that counter was disabled + if ((i == M_VTH1 || i == M_VTH2 || i == M_VTH3) && + (detectorDacs[i] == DEFAULT_COUNTER_DISABLED_VTH_VAL)) { + continue; + } + return FAIL; + } + } } - + // update vth and countermask updateVthAndCounterMask(); @@ -1454,7 +1444,7 @@ int getAllTrimbits() { return value; } -enum detectorSettings setSettings(enum detectorSettings sett) { +int setSettings(enum detectorSettings sett, char* mess) { int *dacVals = NULL; switch (sett) { case STANDARD: @@ -1470,9 +1460,9 @@ enum detectorSettings setSettings(enum detectorSettings sett) { dacVals = defaultDacValue_highgain; break; default: - LOG(logERROR, - ("Settings %d not defined for this detector\n", (int)sett)); - return thisSettings; + sprintf(mess, "Undefined settings %d\n", (int)sett); + LOG(logERROR, (mess)); + return FAIL; } thisSettings = sett; @@ -1481,11 +1471,12 @@ enum detectorSettings setSettings(enum detectorSettings sett) { const int specialDacs[] = SPECIALDACINDEX; for (int i = 0; i < NSPECIALDACS; ++i) { // set to default (last arg to ensure counter check) - setDAC(specialDacs[i], dacVals[i], 0, 1); + if (setDAC(specialDacs[i], dacVals[i], 0, 1, mess) == FAIL) + return FAIL; } LOG(logINFO, ("Settings: %d\n", thisSettings)); - return thisSettings; + return OK; } void validateSettings() { @@ -1505,7 +1496,12 @@ void validateSettings() { sett = settList[isett]; // if one value does not match, = undefined for (int i = 0; i < NSPECIALDACS; ++i) { - if (getDAC(specialDacs[i], 0) != specialDacValues[isett][i]) { + int retval = 0; + if (getDac(specialDacs[i], 0, &retval) == FAIL) { + sett = UNDEFINED; + break; + } + if (retval != specialDacValues[isett][i]) { sett = UNDEFINED; break; } @@ -1541,41 +1537,82 @@ void setThresholdEnergy(int counterIndex, int eV) { } /* parameters - dac, hv */ -// counterEnableCheck false only if setDAC called directly -void setDAC(enum DACINDEX ind, int val, int mV, int counterEnableCheck) { - // invalid value - if (val < 0) { - return; - } - // out of scope, NDAC + 1 for vthreshold - if ((int)ind > NDAC + 1) { - LOG(logERROR, ("Unknown dac index %d\n", ind)); - return; - } +int validateDAC(enum DACINDEX ind, int val, int mV, char* mess) { + char *dacNames[] = {DAC_NAMES}; - // threshold dacs - // remember value, vthreshold: skip disabled, - // others: disable or enable dac if counter mask - // setDAC called directly: will set independent of counter enable - if (ind == M_VTHRESHOLD || ind == M_VTH1 || ind == M_VTH2 || - ind == M_VTH3) { - char *dac_names[] = {DAC_NAMES}; - int vthdacs[] = {M_VTH1, M_VTH2, M_VTH3}; + // validate index (threshold included) + if (ind < 0 || ind >= NDAC + 1) { + sprintf(mess, "Could not set DAC. Invalid index %d\n", ind); + LOG(logERROR, (mess)); + return FAIL; + } + // validate min value + if (val < 0) { + sprintf(mess, "Could not set DAC %s. Input value %d cannot be negative\n", dacNames[ind], val); + LOG(logERROR, (mess)); + return FAIL; + } + // validate max value + if (mV && val > DAC_MAX_MV) { + sprintf(mess, "Could not set DAC %s. Input value %d exceed maximum %d mV\n", dacNames[ind], val, DAC_MAX_MV); + LOG(logERROR, (mess)); + return FAIL; + } + else if (!mV && val > LTC2620_D_GetMaxInput()) { + sprintf(mess, "Could not set DAC %s. Input value %d exceed maximum %d \n", dacNames[ind], val, LTC2620_D_GetMaxInput()); + LOG(logERROR, (mess)); + return FAIL; + } + return OK; +} + +// counterEnableCheck false only if setDAC called directly +int setDAC(enum DACINDEX ind, int val, int mV, int counterEnableCheck, char* mess) { + if (validateDAC(ind, val, mV, mess) == FAIL) + return FAIL; + + char *dacNames[] = {DAC_NAMES}; + LOG(logINFO, ("Setting DAC %s: %d %s \n", dacNames[ind], val, (mV ? "mV" : "dac units"))); + + // threshold dacs: + // - remember dac value if not disabled value + // - if counter disabled, set disabled val + // (except when set direcly from client) + // + // vthreshold: + // - remember dac value for every counter + // + // others: set dac as normal + + // only for threshold dacs or vthreshold + if (ind == M_VTHRESHOLD || ind == M_VTH1 || ind == M_VTH2 || ind == M_VTH3) { uint32_t counters = getCounterMask(); + int vthdacs[] = {M_VTH1, M_VTH2, M_VTH3}; + + // vthrehsold: remember for every counter + // vthdacs: remmeber and set only for that counter for (int i = 0; i < NCOUNTERS; ++i) { if ((int)ind == vthdacs[i] || ind == M_VTHRESHOLD) { - int dacval = val; - // if not disabled value, remember value - if (dacval != DEFAULT_COUNTER_DISABLED_VTH_VAL) { - if (mV) { - if (LTC2620_D_VoltageToDac(val, &dacval) == FAIL) { - return; + + // remembering value + { + int dacval = val; + // if not disabled value + if (dacval != DEFAULT_COUNTER_DISABLED_VTH_VAL) { + // convert to dac units + if (mV) { + if (LTC2620_D_VoltageToDac(val, &dacval) == FAIL) { + sprintf(mess, "Could not set %s. Could not convert input %d mV to dac\n", dacNames[ind], val); + LOG(logERROR, (mess)); + return FAIL; + } } + // remember value + vthEnabledVals[i] = dacval; + LOG(logINFO, ("Remembering %s [%d]\n", dacNames[ind], dacval)); } - vthEnabledVals[i] = dacval; - LOG(logINFO, - ("Remembering %s [%d]\n", dac_names[ind], dacval)); } + // disabled counter if (!(counters & (1 << i))) { // skip setting vthx dac (value remembered anyway) @@ -1587,43 +1624,45 @@ void setDAC(enum DACINDEX ind, int val, int mV, int counterEnableCheck) { val = DEFAULT_COUNTER_DISABLED_VTH_VAL; } } - setGeneralDAC(vthdacs[i], val, mV); + if (setGeneralDAC(vthdacs[i], val, mV, mess) == FAIL) + return FAIL; } } - return; + return OK; } - setGeneralDAC(ind, val, mV); + return setGeneralDAC(ind, val, mV, mess); } -void setGeneralDAC(enum DACINDEX ind, int val, int mV) { - char *dac_names[] = {DAC_NAMES}; - LOG(logDEBUG1, ("Setting dac[%d - %s]: %d %s \n", (int)ind, dac_names[ind], - val, (mV ? "mV" : "dac units"))); - int dacval = val; +int setGeneralDAC(enum DACINDEX ind, int val, int mV, char* mess) { + char *dacNames[] = {DAC_NAMES}; -#ifdef VIRTUAL - LOG(logINFO, ("Setting dac[%d - %s]: %d %s \n", (int)ind, dac_names[ind], - val, (mV ? "mV" : "dac units"))); - if (!mV) { - detectorDacs[ind] = val; + LOG(logDEBUG1, ("Setting General DAC %s: %d %s \n", dacNames[ind], val, (mV ? "mV" : "dac units"))); + + // mV: convert to dac value + int dacval = val; + if (mV) { + if (LTC2620_D_VoltageToDac(val, &dacval) == FAIL) { + sprintf(mess, "Could not set DAC %s. Could not convert %d mV to dac units.\n", dacNames[ind], val); + LOG(logERROR, (mess)); + return FAIL; + } } - // convert to dac units - else if (LTC2620_D_VoltageToDac(val, &dacval) == OK) { - detectorDacs[ind] = dacval; + + if (LTC2620_D_SetDACValue((int)ind, val) == FAIL) { + sprintf(mess, "Could not set DAC %s.\n", dacNames[ind]); + LOG(logERROR, (mess)); + return FAIL; } -#else - if (LTC2620_D_SetDACValue((int)ind, val, mV, dac_names[ind], &dacval) == - OK) { - detectorDacs[ind] = dacval; - } -#endif + detectorDacs[ind] = dacval; + const int specialDacs[NSPECIALDACS] = SPECIALDACINDEX; for (int i = 0; i < NSPECIALDACS; ++i) { if ((int)ind == specialDacs[i]) { validateSettings(); } } + return OK; } void setVthDac(int index, int enable) { @@ -1636,48 +1675,57 @@ void setVthDac(int index, int enable) { if (enable) { value = vthEnabledVals[index]; } - setGeneralDAC(vthdacs[index], value, 0); + char msg[MAX_STR_LENGTH]= {0}; + setGeneralDAC(vthdacs[index], value, 0, msg); } -int getDAC(enum DACINDEX ind, int mV) { +int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess) { + + // vthreshold if (ind == M_VTHRESHOLD) { - int ret = -1, ret1 = -1; + *retval = -1; + int retval1 = -1; // get only for enabled counters uint32_t counters = getCounterMask(); int vthdacs[] = {M_VTH1, M_VTH2, M_VTH3}; for (int i = 0; i < NCOUNTERS; ++i) { if (counters & (1 << i)) { - ret1 = getDAC(vthdacs[i], mV); + if (getDAC(vthdacs[i], mV, &retval1, mess) == FAIL) + return FAIL; // first enabled counter - if (ret == -1) { - ret = ret1; + if (*retval == -1) { + *retval = retval1; } // different values for enabled counters - else if (ret1 != ret) { - return -1; + else if (retval1 != *retval) { + sprintf(mess, "Could not get vthrehsold DAC. Different values for enabled counters.\n"); + LOG(logERROR, (mess)); + return FAIL; } } } - if (ret == -1) { - LOG(logERROR, ("\tvthreshold mismatch (of enabled counters)\n")); - } else { - LOG(logINFO, ("\tvthreshold match %d\n", ret)); - } - return ret; + LOG(logINFO, ("\tvthreshold match %d\n", retval)); + return OK; } + char *dacNames[] = {DAC_NAMES}; if (!mV) { - LOG(logDEBUG1, ("Getting DAC %d : %d dac\n", ind, detectorDacs[ind])); - return detectorDacs[ind]; + LOG(logDEBUG1, ("Getting DAC %s : %d dac\n", dacNames[ind], detectorDacs[ind])); + *retval = detectorDacs[ind]; + return OK; + } + // convert to mV + *retval = -1; + if (LTC2620_D_DacToVoltage(detectorDacs[ind], retval) == FAIL) { + sprintf(mess, "Could not get DAC %s. Could not convert %d dac units to mV\n", dacNames[ind], detectorDacs[ind]); + LOG(logERROR, (mess)); + return FAIL; } - int voltage = -1; - LTC2620_D_DacToVoltage(detectorDacs[ind], &voltage); LOG(logDEBUG1, - ("Getting DAC %d : %d dac (%d mV)\n", ind, detectorDacs[ind], voltage)); - return voltage; + ("Getting DAC %s : %d dac (%d mV)\n", dacNames[ind], detectorDacs[ind], *retval)); + return OK; } -int getMaxDacSteps() { return LTC2620_D_GetMaxNumSteps(); } int getADC(enum ADCINDEX ind, int *value) { LOG(logDEBUG1, ("Reading FPGA temperature...\n")); diff --git a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.h index eca2edf7a..8f4255eb1 100644 --- a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.h @@ -67,7 +67,7 @@ void initStopServer(); // set up detector int allocateDetectorStructureMemory(); void setupDetector(); -int resetToDefaultDacs(int hardReset); +int resetToDefaultDacs(int hardReset, char* mess); int getDefaultDac(enum DACINDEX index, enum detectorSettings sett, int *retval); int setDefaultDac(enum DACINDEX index, enum detectorSettings sett, int value); void setASICDefaults(); @@ -125,7 +125,7 @@ int setModule(sls_detector_module myMod, char *mess); int setTrimbits(int *trimbits); int setAllTrimbits(int val); int getAllTrimbits(); -enum detectorSettings setSettings(enum detectorSettings sett); +int setSettings(enum detectorSettings sett, char* mess); enum detectorSettings getSettings(); // parameters - threshold @@ -133,11 +133,12 @@ int getThresholdEnergy(int counterIndex); void setThresholdEnergy(int counterIndex, int eV); // parameters - dac, adc, hv -void setDAC(enum DACINDEX ind, int val, int mV, int counterEnableCheck); -void setGeneralDAC(enum DACINDEX ind, int val, int mV); +int validateDAC(enum DACINDEX ind, int val, int mV, char* mess); +int setDAC(enum DACINDEX ind, int val, int mV, int counterEnableCheck, char* mess); +int setGeneralDAC(enum DACINDEX ind, int val, int mV, char* mess); +int getDAC(enum DACINDEX ind, int mV, int* retval, char* mess); + void setVthDac(int index, int enable); -int getDAC(enum DACINDEX ind, int mV); -int getMaxDacSteps(); int getADC(enum ADCINDEX ind, int *value); int setHighVoltage(int val, char* mess); @@ -155,7 +156,6 @@ int setPumpProbe(int enable); int setDigitalPulsing(int enable); int setAnalogPulsing(int enable); int setNegativePolarity(int enable); -int setDACS(int *dacs); void setExtSignal(int signalIndex, enum externalSignalFlag mode); int getExtSignal(int signalIndex); diff --git a/slsDetectorServers/slsDetectorServer/include/LTC2620.h b/slsDetectorServers/slsDetectorServer/include/LTC2620.h index b9841f615..9534e1e09 100644 --- a/slsDetectorServers/slsDetectorServer/include/LTC2620.h +++ b/slsDetectorServers/slsDetectorServer/include/LTC2620.h @@ -109,12 +109,5 @@ void LTC2620_Configure(); */ void LTC2620_SetDAC(int dacnum, int data); -/** - * Set dac in dac units or mV - * @param dacnum dac index - * @param val value in dac units or mV - * @param mV 0 for dac units and 1 for mV unit - * @param dacval pointer to value in dac units - * @returns OK or FAIL for success of operation - */ -int LTC2620_SetDACValue(int dacnum, int val, int mV, int *dacval); \ No newline at end of file +/** Set dac in dac units */ +int LTC2620_SetDACValue(int dacnum, int val); \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/include/LTC2620_Driver.h b/slsDetectorServers/slsDetectorServer/include/LTC2620_Driver.h index f11139ac2..14c451147 100644 --- a/slsDetectorServers/slsDetectorServer/include/LTC2620_Driver.h +++ b/slsDetectorServers/slsDetectorServer/include/LTC2620_Driver.h @@ -29,13 +29,5 @@ int LTC2620_D_VoltageToDac(int voltage, int *dacval); int LTC2620_D_DacToVoltage(int dacval, int *voltage); /** - * Set value - * @param dacnum dac index - * @param val value to set - * @param mV 1 for mv, else 0 - * @paam dacname dac name - * @param dacval pointer to dac value - * @return OK or FAIL - */ -int LTC2620_D_SetDACValue(int dacnum, int val, int mV, char *dacname, - int *dacval); \ No newline at end of file + * Set value in dac */ +int LTC2620_D_SetDACValue(int dacnum, int val); \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/src/LTC2620.c b/slsDetectorServers/slsDetectorServer/src/LTC2620.c index eef86271c..6e55a472d 100644 --- a/slsDetectorServers/slsDetectorServer/src/LTC2620.c +++ b/slsDetectorServers/slsDetectorServer/src/LTC2620.c @@ -222,8 +222,8 @@ void LTC2620_SetDAC(int dacnum, int data) { LTC2620_Set(cmd, data, addr, ichip); } -int LTC2620_SetDACValue(int dacnum, int val, int mV, int *dacval) { - LOG(logDEBUG1, ("dacnum:%d, val:%d, ismV:%d\n", dacnum, val, mV)); +int LTC2620_SetDACValue(int dacnum, int val) { + LOG(logDEBUG1, ("dacnum:%d, val:%d\n", dacnum, val)); // validate index if (dacnum < 0 || dacnum >= LTC2620_Ndac) { LOG(logERROR, ("Dac index %d is out of bounds (0 to %d)\n", dacnum, @@ -231,41 +231,16 @@ int LTC2620_SetDACValue(int dacnum, int val, int mV, int *dacval) { return FAIL; } - // get - if (val < 0 && val != LTC2620_PWR_DOWN_VAL) - return FAIL; - - // convert to dac or get mV value - *dacval = val; - int dacmV = val; - int ret = OK; - int ndacsonly = LTC2620_Ndac; -#ifdef CHIPTESTBOARDD - ndacsonly = NDAC_ONLY; -#endif - if (mV) { - ret = LTC2620_VoltageToDac(val, dacval); - } else if (val >= 0 && dacnum <= ndacsonly) { - // do not convert power down dac val - //(if not ndacsonly (pwr/vchip): dont need to print mV value as it will - // be wrong (wrong limits)) - ret = LTC2620_DacToVoltage(val, &dacmV); - } - - // conversion out of bounds - if (ret == FAIL) { - LOG(logERROR, ("Setting Dac %d %s is out of bounds\n", dacnum, - (mV ? "mV" : "dac units"))); + // validate value + if ((val < 0 && val != LTC2620_PWR_DOWN_VAL) || val > LTC2620_MAX_VAL) { + LOG(logERROR, ("Could not set DAC %d. Input value %d is out of bounds (0 to %d)\n", val, + LTC2620_MAX_VAL)); return FAIL; } - // set - if ((*dacval >= 0) || (*dacval == LTC2620_PWR_DOWN_VAL)) { - LOG(logINFO, - ("Setting DAC %d: %d dac (%d mV)\n", dacnum, *dacval, dacmV)); + LOG(logINFO,("\tSetting DAC %d: %d dac\n", dacnum, val)); #ifndef VIRTUAL - LTC2620_SetDAC(dacnum, *dacval); + LTC2620_SetDAC(dacnum, val); #endif - } return OK; } diff --git a/slsDetectorServers/slsDetectorServer/src/LTC2620_Driver.c b/slsDetectorServers/slsDetectorServer/src/LTC2620_Driver.c index c92c111f4..2e898de2e 100644 --- a/slsDetectorServers/slsDetectorServer/src/LTC2620_Driver.c +++ b/slsDetectorServers/slsDetectorServer/src/LTC2620_Driver.c @@ -58,9 +58,8 @@ int LTC2620_D_DacToVoltage(int dacval, int *voltage) { LTC2620_D_HardMaxVoltage, dacval, voltage); } -int LTC2620_D_SetDACValue(int dacnum, int val, int mV, char *dacname, - int *dacval) { - LOG(logDEBUG1, ("dacnum:%d, val:%d, ismV:%d\n", dacnum, val, mV)); +int LTC2620_D_SetDACValue(int dacnum, int val) { + LOG(logDEBUG1, ("dacnum:%d, val:%d\n", dacnum, val)); // validate index if (dacnum < 0 || dacnum >= LTC2620_D_NumDacs) { @@ -69,74 +68,27 @@ int LTC2620_D_SetDACValue(int dacnum, int val, int mV, char *dacname, return FAIL; } - // validate set - if (val < 0 && val != LTC2620_D_PWR_DOWN_VAL) + // validate value + if ((val < 0 && val != LTC2620_D_PWR_DOWN_VAL) || val > LTC2620_D_MAX_DAC_VAL) { + LOG(logERROR, ("Could not set DAC %d. Input value %d is out of bounds (0 to %d)\n", val, LTC2620_D_MAX_DAC_VAL)); return FAIL; + } + + LOG(logINFO,("\tSetting DAC %d: %d dac\n", dacnum, val)); - int ret = OK; - *dacval = val; #ifndef VIRTUAL + return OK; +#else + char fname[MAX_STR_LENGTH]; + memset(fname, 0, MAX_STR_LENGTH); char fnameFormat[MAX_STR_LENGTH]; memset(fnameFormat, 0, MAX_STR_LENGTH); strcpy(fnameFormat, LTC2620_D_DriverFileName); -#endif - - // power down dac (different file name) +#if defined(XILINX_CHIPTESTBOARDD) if (val == LTC2620_D_PWR_DOWN_VAL) { -#if defined(XILINX_CHIPTESTBOARDD) && !defined(VIRTUAL) LOG(logINFO, ("Powering down DAC %2d [%-6s] \n", dacnum, dacname)); strcpy(fnameFormat, LTC2620_D_PowerDownDriverFileName); -#endif } - - // proper value to set - else { - // convert to dac or get mV value - int dacmV = val; - if (mV) { - ret = LTC2620_D_VoltageToDac(val, dacval); - } - - // mV only for print out (dont convert to mV for power regulators) - else if (val >= 0 && dacnum < LTC2620_D_NumDacsOnly) { - // do not convert power down dac val - ret = LTC2620_D_DacToVoltage(val, &dacmV); - } - - // conversion out of bounds - if (ret == FAIL) { - LOG(logERROR, ("Setting Dac %d %s is out of bounds\n", dacnum, - (mV ? "mV" : "dac units"))); - return FAIL; - } - - // print and set -#ifdef XILINX_CHIPTESTBOARDD - if (*dacval >= 0) { - // also print mV - if (dacnum < LTC2620_D_NumDacsOnly) { - LOG(logINFO, ("Setting DAC %2d [%-6s] : %d dac (%d mV)\n", - dacnum, dacname, *dacval, dacmV)); - } - // do not print mV for power regulators - else { - LOG(logINFO, ("Setting Power DAC%2d [%-6s] : %d dac \n", dacnum, - dacname, *dacval)); - } - } -#else - if ((*dacval >= 0) || (*dacval == LTC2620_D_PWR_DOWN_VAL)) { - LOG(logINFO, ("Setting DAC %2d [%-12s] : %d dac (%d mV)\n", dacnum, - dacname, *dacval, dacmV)); - } -#endif - } - - // set in file -#ifndef VIRTUAL - char fname[MAX_STR_LENGTH]; - memset(fname, 0, MAX_STR_LENGTH); -#ifdef XILINX_CHIPTESTBOARDD sprintf(fname, fnameFormat, dacnum); #else sprintf(fname, "%s%d", fnameFormat, dacnum); @@ -150,16 +102,12 @@ int LTC2620_D_SetDACValue(int dacnum, int val, int mV, char *dacname, fname, dacnum)); return FAIL; } - // convert to string, add 0 and write to file #ifdef XILINX_CHIPTESTBOARDD - // not changing *dacval from -100 (cant write -100 to file: invalid arg) - int writeValue = *dacval; if (writeValue == LTC2620_D_PWR_DOWN_VAL) - writeValue = 1; - fprintf(fd, "%d\n", writeValue); -#else - fprintf(fd, "%d\n", *dacval); + fprintf(fd, "1\n"); //-100?:Invalid Argument + else #endif + fprintf(fd, "%d\n", val); fclose(fd); #endif return OK; diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index 67e98c64a..5a4ae7b9f 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -1180,7 +1180,7 @@ int validateAndSetDac(enum dacIndex ind, int val, int mV) { #if defined(MYTHEN3D) || defined(GOTTHARD2D) || defined(EIGERD) || defined(JUNGFRAUD) || defined(MOENCHD) || defined(CHIPTESTBOARDD) case HIGH_VOLTAGE: - if (val != GET_VAL) + if (val != GET_FLAG) ret = setHighVoltage(val, mess); else ret = getHighVoltage(&retval, mess); @@ -1190,7 +1190,7 @@ int validateAndSetDac(enum dacIndex ind, int val, int mV) { #ifdef CHIPTESTBOARDD case V_POWER_CHIP: - if (val != GET_VAL) { + if (val != GET_FLAG) { ret = FAIL; sprintf(mess, "Can not set Vchip. Can only be set automatically in the background (+200mV from highest power regulator voltage).\n"); LOG(logERROR, (mess)); @@ -1203,7 +1203,7 @@ int validateAndSetDac(enum dacIndex ind, int val, int mV) { #if defined(CHIPTESTBOARDD) || defined(XILINX_CHIPTESTBOARDD) case V_LIMIT: - if (val != GET_VAL) { + if (val != GET_FLAG) { if (!mV) { ret = FAIL; strcpy(mess, "Could not set vlimit. VLimit should be in " @@ -1214,7 +1214,7 @@ int validateAndSetDac(enum dacIndex ind, int val, int mV) { ret = setVLimit(val, mess); } else retval = getVLimit(); - + return retval; #endif @@ -1228,7 +1228,9 @@ int validateAndSetDac(enum dacIndex ind, int val, int mV) { case V_POWER_D: case V_POWER_IO: serverDacIndex = getDACIndex(ind); - if (val != GET_VAL) { + if (ret == FAIL) + return retval; + if (val != GET_FLAG) { if (!mV) { ret = FAIL; sprintf(mess, "Could not set power. Power regulator %d should be in mV and not dac units.\n", ind); @@ -1238,101 +1240,59 @@ int validateAndSetDac(enum dacIndex ind, int val, int mV) { ret = setPower(serverDacIndex, val, mess); } else ret = getPower(serverDacIndex, &retval, mess); - + return retval; #endif - - - - - switch (ind) { - - - // dacs + // actual dacs default: - if (mV && val > DAC_MAX_MV) { - ret = FAIL; - sprintf(mess, - "Could not set dac %d to value %d. Allowed limits " - "(0 - %d mV).\n", - ind, val, DAC_MAX_MV); - LOG(logERROR, (mess)); - } else if (!mV && val > getMaxDacSteps()) { - ret = FAIL; - sprintf(mess, - "Could not set dac %d to value %d. Allowed limits " - "(0 - %d dac units).\n", - ind, val, getMaxDacSteps()); - LOG(logERROR, (mess)); - } else { -#if defined(CHIPTESTBOARDD) || defined(XILINX_CHIPTESTBOARDD) - if ((val != GET_FLAG && mV && checkVLimitCompliant(val) == FAIL) || - (val != GET_FLAG && !mV && - checkVLimitDacCompliant(val) == FAIL)) { - ret = FAIL; - sprintf(mess, - "Could not set dac %d to value %d. " - "Exceeds voltage limit %d.\n", - ind, (mV ? val : dacToVoltage(val)), getVLimit()); - LOG(logERROR, (mess)); - } else -#endif -#ifdef MYTHEN3D - // ignore counter enable to force vth dac values - setDAC(serverDacIndex, val, mV, 0); + serverDacIndex = getDACIndex(ind); + if (ret == FAIL) + return retval; + // set + if (val != GET_FLAG) { +#if defined(MYTHEN3D) + // ignore counter enable to force vth dac values + ret = setDAC(serverDacIndex, val, mV, 0, mess); + // changed for setsettings (direct), + // custom trimbit file (setmodule with myMod.reg as -1), + // change of dac (direct) + if (ret == OK) { + for (int i = 0; i < NCOUNTERS; ++i) { + setThresholdEnergy(i, -1); + } + } +#elif defined(EIGERD) + ret = setDAC(serverDacIndex, val, mV, mess); + // handle if set by user individually + if (getSettings() != UNDEFINED) { + switch (serverDacIndex) { + case E_VCMP_LL: + case E_VCMP_LR: + case E_VCMP_RL: + case E_VCMP_RR: + case E_VRPREAMP: + case E_VCP: + if (setSettings(UNDEFINED, mess) == FAIL) { + ret = FAIL; + return retval; + } + LOG(logERROR, ("Settings has been changed " + "to undefined (changed specific dacs)\n")); + break; + default: + break; + } + } #else - setDAC(serverDacIndex, val, mV); + ret = setDAC(serverDacIndex, val, mV, mess); #endif - retval = getDAC(serverDacIndex, mV); - } -#ifdef EIGERD - if (val != GET_FLAG && getSettings() != UNDEFINED) { - // changing dac changes settings to undefined - switch (serverDacIndex) { - case E_VCMP_LL: - case E_VCMP_LR: - case E_VCMP_RL: - case E_VCMP_RR: - case E_VRPREAMP: - case E_VCP: - setSettings(UNDEFINED); - LOG(logERROR, ("Settings has been changed " - "to undefined (changed specific dacs)\n")); - break; - default: - break; - } - } -#endif - // check - if (ret == OK) { - if ((abs(retval - val) <= 5) || val == GET_FLAG) { - ret = OK; - } else { - ret = FAIL; - sprintf(mess, "Setting dac %d : wrote %d but read %d\n", - serverDacIndex, val, retval); - LOG(logERROR, (mess)); - } - } - LOG(logDEBUG1, ("Dac (%d): %d %s\n\n", serverDacIndex, retval, - (mV ? "mV" : "dac units"))); -#ifdef MYTHEN3D - // changed for setsettings (direct), - // custom trimbit file (setmodule with myMod.reg as -1), - // change of dac (direct) - if (val != GET_FLAG && ret == OK) { - for (int i = 0; i < NCOUNTERS; ++i) { - setThresholdEnergy(i, -1); - } - } -#endif - break; + } + // get + else + ret = getDAC(serverDacIndex, mV, &retval, mess); + return retval; } - return retval; - -#endif } @@ -1769,7 +1729,7 @@ int set_settings(int file_des) { validate_settings(isett); #endif if (ret == OK) { - setSettings(isett); + ret = setSettings(isett, mess); } } retval = getSettings(); @@ -8274,12 +8234,7 @@ int reset_to_default_dacs(int file_des) { functionNotImplemented(); #else if (Server_VerifyLock() == OK) { - if (resetToDefaultDacs(arg) == FAIL) { - ret = FAIL; - sprintf(mess, "Could not %s reset default dacs", - (arg == 1 ? "hard" : "")); - LOG(logERROR, (mess)); - } + ret = resetToDefaultDacs(arg, mess); } #endif return Server_SendResult(file_des, INT32, NULL, 0);