From e3020446ca378267eccccc2acf8c3121c58517fa Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Tue, 3 Feb 2026 11:15:27 +0100 Subject: [PATCH] wip --- .../slsDetectorFunctionList.c | 185 ++++++++---- .../slsDetectorFunctionList.h | 15 +- .../slsDetectorFunctionList.c | 117 +++++--- .../slsDetectorFunctionList.h | 3 +- .../slsDetectorFunctionList.c | 50 ++-- .../slsDetectorFunctionList.h | 4 +- .../slsDetectorFunctionList.c | 23 +- .../slsDetectorFunctionList.h | 4 +- .../slsDetectorFunctionList.c | 24 +- .../slsDetectorFunctionList.h | 3 +- .../slsDetectorFunctionList.c | 37 ++- .../slsDetectorFunctionList.h | 4 +- .../slsDetectorServer/include/DAC6571.h | 4 +- .../slsDetectorServer/src/DAC6571.c | 31 +- .../src/slsDetectorServer_funcs.c | 281 ++++++------------ .../slsDetectorFunctionList.c | 35 ++- .../slsDetectorFunctionList.h | 6 +- 17 files changed, 457 insertions(+), 369 deletions(-) diff --git a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c index e7f914ca1..e23c7c247 100644 --- a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c @@ -568,7 +568,9 @@ void setupDetector() { SPI_HV_SRL_DGTL_OTPT_MSK, SPI_HV_SRL_DGTL_OTPT_OFST, HIGHVOLTAGE_MIN, HIGHVOLTAGE_MAX); MAX1932_Disable(); - setHighVoltage(DEFAULT_HIGH_VOLTAGE); + initError = setHighVoltage(DEFAULT_HIGH_VOLTAGE, initErrorMessage); + if (initError == FAIL) + return; // power off voltage regulators powerOff(); @@ -607,7 +609,9 @@ void setupDetector() { INA226_CalibrateCurrentRegister(I2C_POWER_VB_DEVICE_ID); INA226_CalibrateCurrentRegister(I2C_POWER_VC_DEVICE_ID); INA226_CalibrateCurrentRegister(I2C_POWER_VD_DEVICE_ID); - setVchip(VCHIP_MIN_MV); + initError = setVchip(VCHIP_MIN_MV, initErrorMessage); + if (initError == FAIL) + return; setADCInvertRegister(0); // depends on chip @@ -1223,6 +1227,26 @@ int64_t getMeasurementTime() { enum detectorSettings getSettings() { return UNDEFINED; } /* parameters - dac, adc, hv */ +int setADCVpp(int val, int mV, char* mess) { + if (AD9257_SetVrefVoltage(val, mV) == FAIL) { + sprintf(mess, "Could not set Adc Vpp. Please set a proper value\n"); + LOG(logERROR, (mess)); + return FAIL; + } + // cannot validate (just a variable and mv gives different values + return OK; +} + +int getADCVpp(int mV, int* retval, char* mess) { + *retval = AD9257_GetVrefVoltage(mV); + if (*retval == -1) { + sprintf(mess, "Could not get Adc Vpp.\n"); + LOG(logERROR, (mess)); + return FAIL; + } + LOG(logDEBUG1, ("Adc Vpp retval: %d %s\n", *retval, (mV ? "mV" : "mode"))); + return OK; +} void setDAC(enum DACINDEX ind, int val, int mV) { if (val < 0 && val != LTC2620_GetPowerDownValue()) @@ -1285,9 +1309,13 @@ int checkVLimitDacCompliant(int dac) { int getVLimit() { return vLimit; } -void setVLimit(int l) { - if (l >= 0) - vLimit = l; +int setVLimit(int val, char* mess) { + if (val < 0) { + sprintf(mess, "Could not set vlimit. Invalid value %d\n", val); + LOG(logERROR, (mess)); + return FAIL; + } + vLimit = val; } int isVchipValid(int val) { @@ -1297,44 +1325,55 @@ int isVchipValid(int val) { return 1; } -int getVchip() { +int getVchip(int* retval, char* mess) { + *retval = -1; // not set yet - if (dacValues[D_PWR_CHIP] == -1 || - dacValues[D_PWR_CHIP] == LTC2620_GetPowerDownValue()) + if (dacValues[D_PWR_CHIP] == -1) { + LOG(logWARNING, ("Retrieving Vchip that has not been set yet.\n")); return dacValues[D_PWR_CHIP]; - int voltage = -1; + } + if (dacValues[D_PWR_CHIP] == LTC2620_GetPowerDownValue()) { + return dacValues[D_PWR_CHIP]; + } // dac to voltage - ConvertToDifferentRange(LTC2620_GetMaxInput(), LTC2620_GetMinInput(), - VCHIP_MIN_MV, VCHIP_MAX_MV, dacValues[D_PWR_CHIP], - &voltage); - return voltage; + if (ConvertToDifferentRange(LTC2620_GetMaxInput(), LTC2620_GetMinInput(), VCHIP_MIN_MV, VCHIP_MAX_MV, dacValues[D_PWR_CHIP], retval) == FAIL) { + sprintf(mess, "Could not convert to voltage. Input value for vchip outside bounds.\n"); + LOG(logERROR, (mess)); + return FAIL; + } + LOG(logDEBUG1, ("Vchip: %d\n", retval)); + return OK; } -void setVchip(int val) { - // set vchip - if (val != -1) { - LOG(logINFOBLUE, ("Setting Vchip to %d mV\n", val)); - - int dacval = LTC2620_GetPowerDownValue(); - - // validate & convert it to dac - if (val != LTC2620_GetPowerDownValue()) { - // convert voltage to dac - if (ConvertToDifferentRange( - VCHIP_MIN_MV, VCHIP_MAX_MV, LTC2620_GetMaxInput(), - LTC2620_GetMinInput(), // min val is max V - val, &dacval) == FAIL) { - LOG(logERROR, - ("\tVChip %d mV invalid. Is not between %d and %d mV\n", - val, VCHIP_MIN_MV, VCHIP_MAX_MV)); - return; - } - } - LOG(logINFO, ("Setting Vchip (DAC %d): %d dac (%d mV)\n", D_PWR_CHIP, - dacval, val)); - // set - setDAC(D_PWR_CHIP, dacval, 0); +int setVchip(int val, char* mess) { + if (val < 0) { + sprintf(mess, "Could not set vchip. Invalid value: %d\n"); + LOG(logERROR, (mess)); + return FAIL; } + LOG(logINFOBLUE, ("Setting Vchip to %d mV\n", val)); + + // calculate dac value to set + int dacval = LTC2620_GetPowerDownValue(); + if (val != LTC2620_GetPowerDownValue()) { + // convert voltage to dac + if (ConvertToDifferentRange( + VCHIP_MIN_MV, VCHIP_MAX_MV, LTC2620_GetMaxInput(), + LTC2620_GetMinInput(), // min val is max V + val, &dacval) == FAIL) { + sprintf(mess, "\tVChip %d mV invalid. Is not between %d and %d mV\n", val, VCHIP_MIN_MV, VCHIP_MAX_MV); + return FAIL; + } + } + LOG(logINFO, ("Setting Vchip (DAC %d): %d dac (%d mV)\n", D_PWR_CHIP, + dacval, val)); + setDAC(D_PWR_CHIP, dacval, 0); + if (dacValues[D_PWR_CHIP] != dacval) { + sprintf(mess, "Could not set vchip. Set %d, got %d\n", val, dacvalues[D_PWR_CHIP]); + LOG(logERROR, (mess)); + return FAIL; + } + return OK; } int getVChipToSet(enum DACINDEX ind, int val) { @@ -1474,7 +1513,23 @@ int getPower(enum DACINDEX ind) { return retval; } -void setPower(enum DACINDEX ind, int val) { +int setPower(enum DACINDEX ind, int val, char* mess) { + + serverdacindex + if (checkVLimitCompliant(val) == FAIL) { + sprintf(mess, "Could not set power. Power regulator %d exceeds voltage limit %d.\n", ind, getVLimit()); + LOG(logERROR, (mess)); + return FAIL; + } + if (!isPowerValid(ind, val)) { + sprintf(mess, "Could not set power. Power regulator %d should be between %d and %d mV\n", ind, (ind == D_PWR_IO ? VIO_MIN_MV : POWER_RGLTR_MIN), (VCHIP_MAX_MV - VCHIP_POWER_INCRMNT)); + LOG(logERROR, (mess)); + return FAIL; + } + setPower(ind, val); + int retval = getPower(ind); + validate(&ret, mess, val, retval, "set power regulator", DEC); + // validate index & get adc index int adcIndex = getADCIndexFromDACIndex(ind); if (adcIndex == -1) { @@ -1516,11 +1571,15 @@ void setPower(enum DACINDEX ind, int val) { setDAC(ind, LTC2620_GetPowerDownValue(), 0); // set vchip - setVchip(vchip); - if (getVchip() != vchip) { - LOG(logERROR, ("Weird, Could not set vchip. Set %d, read %d\n.", - vchip, getVchip())); - return; + if (setVchip(vchip, mess) == FAIL) + return FAIL; + int retval = 0; + if (getVchip(&retval, mess) == FAIL) + return FAIL; + if (retval != vchip) { + sprintf(logERROR, "Could not get vchip. Set %d, read %d\n.", vchip, retval); + LOG(logERROR, (mess)); + return FAIL; } //(power off is anyway done with power enable) @@ -1720,26 +1779,34 @@ int getSlowADCTemperature() { return tempValue; } -int setHighVoltage(int val) { - // setting hv - if (val >= 0) { - LOG(logINFO, ("Setting High voltage: %d V\n", val)); - uint32_t addr = POWER_REG; - - // switch to external high voltage - bus_w(addr, bus_r(addr) & (~POWER_HV_INTERNAL_SLCT_MSK)); - - MAX1932_Set(&val); - - // switch on internal high voltage, if set - if (val > 0) - bus_w(addr, bus_r(addr) | POWER_HV_INTERNAL_SLCT_MSK); - - highvoltage = val; +int setHighVoltage(int val, char* mess) { + if (val < 0) { + sprintf(mess, "Could not set high voltage. Invalid value:%d\n", val); + LOG(logERROR, (mess)); + return FAIL; } + LOG(logINFO, ("Setting High voltage: %d V", val)); + uint32_t addr = POWER_REG; + + // switch to external high voltage + bus_w(addr, bus_r(addr) & (~POWER_HV_INTERNAL_SLCT_MSK)); + + MAX1932_Set(&val); + + // switch on internal high voltage, if set + if (val > 0) + bus_w(addr, bus_r(addr) | POWER_HV_INTERNAL_SLCT_MSK); + + highvoltage = val; + return OK; +} + +int getHighVoltage(int *retval, char* mess) { + LOG(logDEBUG1, ("High Voltage: %d\n", retval)); return highvoltage; } + /* parameters - timing, extsig */ void setTiming(enum timingMode arg) { diff --git a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.h index 369e71119..e93890463 100644 --- a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.h @@ -123,6 +123,8 @@ enum detectorSettings getSettings(); // parameters - threshold // parameters - dac, adc, hv +int setADCVpp(int val, int mV, char* mess); +int getADCVpp(int mV, int* retval, char* mess); void setDAC(enum DACINDEX ind, int val, int mV); int getDAC(enum DACINDEX ind, int mV); @@ -131,23 +133,24 @@ int dacToVoltage(int dac); int checkVLimitCompliant(int mV); int checkVLimitDacCompliant(int dac); int getVLimit(); -void setVLimit(int l); +int setVLimit(int val, char* mess); int isVchipValid(int val); -int getVchip(); -void setVchip(int val); +int getVchip(int* retval, char* mess); +int setVchip(int val, char* mess); int getVChipToSet(enum DACINDEX ind, int val); int getDACIndexFromADCIndex(enum ADCINDEX ind); int getADCIndexFromDACIndex(enum DACINDEX ind); int isPowerValid(enum DACINDEX ind, int val); -int getPower(); -void setPower(enum DACINDEX ind, int val); +int getPower(int* retval, char* mess); +int setPower(enum DACINDEX ind, int val, char* mess); void powerOff(); int getADC(enum ADCINDEX ind); int getSlowADC(int ichan); int getSlowADCTemperature(); -int setHighVoltage(int val); +int setHighVoltage(int val, char* mess); +int getHighVoltage(int *retval, char* mess); // parameters - timing, extsig diff --git a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c index f2290afbe..dc7a1f0dc 100644 --- a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c @@ -836,7 +836,10 @@ void setupDetector() { Feb_Control_SetInTestModeVariable(DEFAULT_TEST_MODE); sharedMemory_unlockLocalLink(); #endif - setHighVoltage(DEFAULT_HIGH_VOLTAGE); + initError = setHighVoltage(DEFAULT_HIGH_VOLTAGE, initErrorMessage); + if (initError == FAIL) + return; + #ifndef VIRTUAL sharedMemory_lockLocalLink(); if (!Feb_Control_CheckSetup()) { @@ -1563,60 +1566,76 @@ int getADC(enum ADCINDEX ind) { #endif } -int setHighVoltage(int val) { +int getHighVoltage(int* retval, char* mess) { + if (!master) { + LOG(logDEBUG1, ("High Voltage: %d\n", SLAVE_HIGH_VOLTAGE_READ_VAL)); + *retval = SLAVE_HIGH_VOLTAGE_READ_VAL; + return OK; + } + + // master #ifdef VIRTUAL - if (master) { - // set - if (val != -1) { - LOG(logINFO, ("Setting High voltage: %d V\n", val)); - eiger_theo_highvoltage = val; - } - return eiger_theo_highvoltage; - } - - return SLAVE_HIGH_VOLTAGE_READ_VAL; + LOG(logDEBUG1, ("High Voltage: %d\n", eiger_theo_highvoltage)); + *retval = eiger_theo_highvoltage; + return OK; #else - - if (master) { - - // set - if (val != -1) { - eiger_theo_highvoltage = val; - sharedMemory_lockLocalLink(); - int ret = Feb_Control_SetHighVoltage(val); - sharedMemory_unlockLocalLink(); - if (!ret) // could not set - return -2; - else if (ret == -1) // outside range - return -1; - } - - // get - sharedMemory_lockLocalLink(); - if (!Feb_Control_GetHighVoltage(&eiger_highvoltage)) { - LOG(logERROR, ("Could not read high voltage\n")); - sharedMemory_unlockLocalLink(); - return -3; - } - // need to read the file twice to get the proper value - if (!Feb_Control_GetHighVoltage(&eiger_highvoltage)) { - LOG(logERROR, ("Could not read high voltage\n")); - sharedMemory_unlockLocalLink(); - return -3; - } + // get + sharedMemory_lockLocalLink(); + if (!Feb_Control_GetHighVoltage(&eiger_highvoltage)) { + LOG(logERROR, ("Could not read high voltage\n")); sharedMemory_unlockLocalLink(); + strcpy(mess, "Getting high voltage failed. Serial/i2c communication failed.\n"); + LOG(logERROR, (mess)); + return FAIL; + } + // need to read the file twice to get the proper value + if (!Feb_Control_GetHighVoltage(&eiger_highvoltage)) { + LOG(logERROR, ("Could not read high voltage\n")); + sharedMemory_unlockLocalLink(); + strcpy(mess, "Getting high voltage failed. Serial/i2c communication failed.\n"); + LOG(logERROR, (mess)); + return FAIL; + } + sharedMemory_unlockLocalLink(); - // tolerance of 5 - if (abs(eiger_theo_highvoltage - eiger_highvoltage) > - HIGH_VOLTAGE_TOLERANCE) { - LOG(logINFO, - ("High voltage still ramping: %d\n", eiger_highvoltage)); - return eiger_highvoltage; - } - return eiger_theo_highvoltage; + // tolerance of 5 + if (abs(eiger_theo_highvoltage - eiger_highvoltage) > + HIGH_VOLTAGE_TOLERANCE) { + LOG(logINFO, + ("High voltage still ramping: %d\n", eiger_highvoltage)); + *retval = eiger_highvoltage; + LOG(logDEBUG1, ("High Voltage: %d\n", eiger_highvoltage)); + return OK; + } + *retval = eiger_theo_highvoltage; + LOG(logDEBUG1, ("High Voltage: %d\n", eiger_theo_highvoltage)); + return OK; +#endif + +} + +int setHighVoltage(int val, char* mess) { +#ifdef VIRTUAL + LOG(logINFO, ("Setting High voltage: %d V\n", val)); + eiger_theo_highvoltage = val; +#else + eiger_theo_highvoltage = val; + sharedMemory_lockLocalLink(); + int ret = Feb_Control_SetHighVoltage(val); + sharedMemory_unlockLocalLink(); + + if (ret == 0) { + strcpy(mess, "Setting high voltage failed. Serial/i2c communication failed.\n"); + LOG(logERROR, (mess)); + return FAIL; + } + if (ret == -1) { + sprintf(mess, "Setting high voltage failed. Invalid input %d. The range is from 0 to 200 V.\n", val); + LOG(logERROR, (mess)); + return FAIL; } - return SLAVE_HIGH_VOLTAGE_READ_VAL; + // cannot validate using get #endif } diff --git a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.h index 426c8fd7f..55c07ecfa 100644 --- a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.h @@ -128,7 +128,8 @@ int getDAC(enum DACINDEX ind, int mV); int getMaxDacSteps(); int getADC(enum ADCINDEX ind); -int setHighVoltage(int val); +int setHighVoltage(int val, char* mess); +int getHighVoltage(int* retval, char* mess); // parameters - timing, extsig int setMaster(enum MASTERINDEX m); diff --git a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c index 199e26fab..97dcb5f46 100644 --- a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c @@ -479,10 +479,8 @@ void setupDetector() { setTimingSource(DEFAULT_TIMING_SOURCE); // Default values - initError = setHighVoltage(DEFAULT_HIGH_VOLTAGE); + initError = setHighVoltage(DEFAULT_HIGH_VOLTAGE, initErrorMessage); if (initError == FAIL) { - sprintf(initErrorMessage, "Could not set high voltage to %d\n", - DEFAULT_HIGH_VOLTAGE); return; } @@ -1575,9 +1573,12 @@ int getADC(enum ADCINDEX ind, int *value) { return OK; } -int setHighVoltage(int val) { - if (val > HV_SOFT_MAX_VOLTAGE) { - LOG(logERROR, ("Invalid high voltage: %d V\n", val)); +int setHighVoltage(int val, char* mess) { + // validate input value + if (val < 0 || val > HV_SOFT_MAX_VOLTAGE) { + sprintf(mess, "Invalid Voltage. Valid range (0 - %d)\n", + HV_SOFT_MAX_VOLTAGE); + LOG(logERROR, (mess)); return FAIL; } @@ -1589,7 +1590,7 @@ int setHighVoltage(int val) { // at startup (initCheck not done: to not wait 10s assuming hv = 0 // otherwise as below, always check current hv to wait 10s if powering off if (initCheckDone) { - if (getHighVoltage(&prevHighVoltage) == FAIL) { + if (getHighVoltage(&prevHighVoltage, mess) == FAIL) { LOG(logERROR, ("Could not get current high voltage to determine if " "%d s wait is required\n", waitTime)); @@ -1597,21 +1598,36 @@ int setHighVoltage(int val) { } } - int ret = DAC6571_Set(val); + if (DAC6571_Set(val, mess) == FAIL) + return FAIL; + // only when powering off (from non zero value), wait 10s - if (ret == OK) { - if (prevHighVoltage > 0 && val == 0) { - LOG(logINFO, - ("\tSwitching off high voltage requires %d s...\n", waitTime)); - sleep(waitTime); - LOG(logINFO, ("\tAssuming high voltage switched off\n")); - } + if (prevHighVoltage > 0 && val == 0) { + LOG(logINFO, + ("\tSwitching off high voltage requires %d s...\n", waitTime)); + sleep(waitTime); + LOG(logINFO, ("\tAssuming high voltage switched off\n")); } - return ret; + + // validate get + int retval = 0; + if (getHighVoltage(&retval, mess) == FAIL) + return FAIL; + if (val != retval) { + sprintf(mess, "Could not set high voltage. Set %d, but got %d\n", val, retval); + LOG(logERROR, (mess)); + return FAIL; + } + + return OK; } -int getHighVoltage(int *retval) { return DAC6571_Get(retval); } +int getHighVoltage(int *retval, char* mess) { + int ret = DAC6571_Get(retval, mess); + LOG(logDEBUG1, ("High Voltage: %d\n", retval)); + return ret; +} /* parameters - timing */ diff --git a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.h index 598a4e251..608e87a4c 100644 --- a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.h @@ -120,8 +120,8 @@ int getDAC(enum DACINDEX ind, int mV); int getMaxDacSteps(); int getADC(enum ADCINDEX ind, int *value); -int setHighVoltage(int val); -int getHighVoltage(int *retval); +int setHighVoltage(int val, char* mess); +int getHighVoltage(int *retval, char* mess); // parameters - timing, extsig int setMaster(enum MASTERINDEX m); diff --git a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c index 9ddc04c89..ab7115596 100644 --- a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c @@ -488,7 +488,9 @@ void setupDetector() { SPI_HV_SRL_DGTL_OTPT_MSK, SPI_HV_SRL_DGTL_OTPT_OFST, HIGHVOLTAGE_MIN, HIGHVOLTAGE_MAX); MAX1932_Disable(); - setHighVoltage(DEFAULT_HIGH_VOLTAGE); + initError = setHighVoltage(DEFAULT_HIGH_VOLTAGE, initErrorMessage); + if (initError == FAIL) + return; // adc AD9257_SetDefines(ADC_SPI_REG, ADC_SPI_SRL_CS_OTPT_MSK, @@ -1398,13 +1400,20 @@ int getADC(enum ADCINDEX ind) { return retval; } -int setHighVoltage(int val) { - // setting hv - if (val >= 0) { - LOG(logINFO, ("Setting High voltage: %d V", val)); - MAX1932_Set(&val); - highvoltage = val; +int setHighVoltage(int val, char* mess) { + if (val < 0) { + sprintf(mess, "Could not set high voltage. Invalid value:%d\n", val); + LOG(logERROR, (mess)); + return FAIL; } + LOG(logINFO, ("Setting High voltage: %d V", val)); + MAX1932_Set(&val); + highvoltage = val; + return OK; +} + +int getHighVoltage(int *retval, char* mess) { + LOG(logDEBUG1, ("High Voltage: %d\n", retval)); return highvoltage; } diff --git a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.h index 951098e2b..2adde51f1 100644 --- a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.h @@ -126,8 +126,8 @@ void setDAC(enum DACINDEX ind, int val, int mV); int getDAC(enum DACINDEX ind, int mV); int getMaxDacSteps(); int getADC(enum ADCINDEX ind); -int setHighVoltage(int val); -int getHighVoltage(int *retval); +int setHighVoltage(int val, char* mess); +int getHighVoltage(int *retval, char* mess); // parameters - timing, extsig int setMaster(enum MASTERINDEX m); diff --git a/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c index bebb87f8f..21922342d 100644 --- a/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c @@ -461,7 +461,9 @@ void setupDetector() { SPI_HV_SRL_DGTL_OTPT_MSK, SPI_HV_SRL_DGTL_OTPT_OFST, HIGHVOLTAGE_MIN, HIGHVOLTAGE_MAX); MAX1932_Disable(); - setHighVoltage(DEFAULT_HIGH_VOLTAGE); + initError = setHighVoltage(DEFAULT_HIGH_VOLTAGE, initErrorMessage); + if (initError == FAIL) + return; // adc AD9257_SetDefines(ADC_SPI_REG, ADC_SPI_SRL_CS_OTPT_MSK, @@ -966,16 +968,24 @@ int getADC(enum ADCINDEX ind) { return retval; } -int setHighVoltage(int val) { - // setting hv - if (val >= 0) { - LOG(logINFO, ("Setting High voltage: %d V", val)); - MAX1932_Set(&val); - highvoltage = val; +int setHighVoltage(int val, char* mess) { + if (val < 0) { + sprintf(mess, "Could not set high voltage. Invalid value:%d\n", val); + LOG(logERROR, (mess)); + return FAIL; } + LOG(logINFO, ("Setting High voltage: %d V", val)); + MAX1932_Set(&val); + highvoltage = val; + return OK; +} + +int getHighVoltage(int *retval, char* mess) { + LOG(logDEBUG1, ("High Voltage: %d\n", retval)); return highvoltage; } + /* parameters - timing, extsig */ int setMaster(enum MASTERINDEX m) { diff --git a/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.h index 8ab13a49b..7dd2c7d96 100644 --- a/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.h @@ -119,7 +119,8 @@ void setDAC(enum DACINDEX ind, int val, int mV); int getDAC(enum DACINDEX ind, int mV); int getMaxDacSteps(); int getADC(enum ADCINDEX ind); -int setHighVoltage(int val); +int setHighVoltage(int val, char* mess); +int getHighVoltage(int *retval, char* mess); // parameters - timing, extsig int setMaster(enum MASTERINDEX m); diff --git a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c index 0d0b6570c..7d2763720 100644 --- a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c @@ -493,10 +493,8 @@ void setupDetector() { cleanFifos(); // defaults - initError = setHighVoltage(DEFAULT_HIGH_VOLTAGE); + initError = setHighVoltage(DEFAULT_HIGH_VOLTAGE, initErrorMessage); if (initError == FAIL) { - sprintf(initErrorMessage, "Could not set high voltage to %d\n", - DEFAULT_HIGH_VOLTAGE); return; } @@ -1692,17 +1690,38 @@ int getADC(enum ADCINDEX ind, int *value) { return OK; } -int setHighVoltage(int val) { - // limit values - if (val > HV_SOFT_MAX_VOLTAGE) { - val = HV_SOFT_MAX_VOLTAGE; +int setHighVoltage(int val, char* mess) { + // validate input value + if (val < 0 || val > HV_SOFT_MAX_VOLTAGE) { + sprintf(mess, "Invalid Voltage. Valid range (0 - %d)\n", + HV_SOFT_MAX_VOLTAGE); + LOG(logERROR, (mess)); + return FAIL; } LOG(logINFO, ("Setting High voltage: %d V\n", val)); - return DAC6571_Set(val); + if (DAC6571_Set(val, mess) == FAIL) { + return FAIL; + } + + // validate get + int retval = 0; + if (getHighVoltage(&retval, mess) == FAIL) + return FAIL; + if (val != retval) { + sprintf(mess, "Could not set high voltage. Set %d, but got %d\n", val, retval); + LOG(logERROR, (mess)); + return FAIL; + } + + return OK; } -int getHighVoltage(int *retval) { return DAC6571_Get(retval); } +int getHighVoltage(int *retval, char* mess) { + int ret = DAC6571_Get(retval, mess); + LOG(logDEBUG1, ("High Voltage: %d\n", retval)); + return ret; +} /* parameters - timing */ diff --git a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.h index 1032fcc32..eca2edf7a 100644 --- a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.h @@ -140,8 +140,8 @@ int getDAC(enum DACINDEX ind, int mV); int getMaxDacSteps(); int getADC(enum ADCINDEX ind, int *value); -int setHighVoltage(int val); -int getHighVoltage(int *retval); +int setHighVoltage(int val, char* mess); +int getHighVoltage(int *retval, char* mess); // parameters - timing, extsig int isMaster(int *retval); diff --git a/slsDetectorServers/slsDetectorServer/include/DAC6571.h b/slsDetectorServers/slsDetectorServer/include/DAC6571.h index 3a86ea900..7624d92a9 100644 --- a/slsDetectorServers/slsDetectorServer/include/DAC6571.h +++ b/slsDetectorServers/slsDetectorServer/include/DAC6571.h @@ -5,5 +5,5 @@ #include void DAC6571_SetDefines(int hardMaxV, char *driverfname); -int DAC6571_Set(int val); -int DAC6571_Get(int *retval); +int DAC6571_Set(int val, char* mess); +int DAC6571_Get(int *retval, char* mess); diff --git a/slsDetectorServers/slsDetectorServer/src/DAC6571.c b/slsDetectorServers/slsDetectorServer/src/DAC6571.c index d9770043a..67f984dc5 100644 --- a/slsDetectorServers/slsDetectorServer/src/DAC6571.c +++ b/slsDetectorServers/slsDetectorServer/src/DAC6571.c @@ -30,18 +30,21 @@ void DAC6571_SetDefines(int hardMaxV, char *driverfname) { #endif } -int DAC6571_Set(int val) { +int DAC6571_Set(int val, char* mess) { LOG(logDEBUG1, ("Setting high voltage to %d\n", val)); - if (val < 0) + if (val < 0) { + sprintf(mess, "Invalid value. Cannot be negative.\n"); + LOG(logERROR, (mess)); return FAIL; + } int dacvalue = 0; // convert value if (ConvertToDifferentRange(0, DAC6571_HardMaxVoltage, DAC6571_MIN_DAC_VAL, DAC6571_MAX_DAC_VAL, val, &dacvalue) == FAIL) { - LOG(logERROR, - ("Could not convert %d high voltage to a valid dac value\n", val)); + sprintf(mess, "Could not convert %d high voltage to a valid dac value\n", val); + LOG(logERROR, (mess)); return FAIL; } LOG(logINFO, ("\t%dV (dacval %d)\n", val, dacvalue)); @@ -52,9 +55,8 @@ int DAC6571_Set(int val) { // open file FILE *fd = fopen(DAC6571_DriverFileName, "w"); if (fd == NULL) { - LOG(logERROR, - ("Could not open file %s for writing to set high voltage\n", - DAC6571_DriverFileName)); + sprintg(mess, "Could not open file %s for writing to set high voltage\n", DAC6571_DriverFileName); + LOG(logERROR, (mess)); return FAIL; } // convert to string, add 0 and write to file @@ -65,7 +67,7 @@ int DAC6571_Set(int val) { return OK; } -int DAC6571_Get(int *retval) { +int DAC6571_Get(int *retval, char* mess) { LOG(logDEBUG1, ("Getting high voltage\n")); int dacvalue = 0; @@ -74,7 +76,8 @@ int DAC6571_Get(int *retval) { #else if (readParameterFromFile(DAC6571_DriverFileName, "high voltage", &dacvalue) == FAIL) { - LOG(logERROR, ("Could not get high voltage\n")); + sprintf(mess, "Could not get high voltage\n"); + LOG(logERROR, (mess)); return FAIL; } #endif @@ -83,9 +86,8 @@ int DAC6571_Get(int *retval) { if (ConvertToDifferentRange(DAC6571_MIN_DAC_VAL, DAC6571_MAX_DAC_VAL, 0, DAC6571_HardMaxVoltage, dacvalue, retval) == FAIL) { - LOG(logERROR, - ("Could not convert %d dac value to a valid high voltage\n", - dacvalue)); + sprintf(mess, "Could not convert %d dac value to a valid high voltage\n", dacvalue); + LOG(logERROR, (mess)); return FAIL; } LOG(logINFO, ("\t%dV (dacval %d)\n", (*retval), dacvalue)); @@ -94,9 +96,8 @@ int DAC6571_Get(int *retval) { // open file FILE *fd = fopen(DAC6571_DriverFileName, "w"); if (fd == NULL) { - LOG(logERROR, - ("Could not open file %s for writing to set high voltage\n", - DAC6571_DriverFileName)); + sprintf(mess, "Could not open file %s for writing to get high voltage\n", DAC6571_DriverFileName); + LOG(logERROR, (mess)); return FAIL; } // convert to string, add 0 and write to file diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index 157daa377..d8345ba2a 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -1152,14 +1152,98 @@ enum DACINDEX getDACIndex(enum dacIndex ind) { } int validateAndSetDac(enum dacIndex ind, int val, int mV) { + int retval = -1; + switch (ind) { + + +#ifdef EIGERD + case IO_DELAY: + retval = setIODelay(val); + LOG(logDEBUG1, ("IODelay: %d\n", retval)); + validate(&ret, mess, val, retval, "set iodelay", DEC); + return retval; +#endif + + +#if defined(CHIPTESTBOARDD) + case ADC_VPP: + if (val != GET_FLAG) + ret = setADCVpp(val, mV, mess); + else + ret = getADCVpp(mV, &retval, mess); + return retval; +#endif + + +#if defined(MYTHEN3D) || defined(GOTTHARD2D) || defined(EIGERD) || defined(JUNGFRAUD) || defined(MOENCHD) || defined(CHIPTESTBOARDD) + case HIGH_VOLTAGE: + if (val != GET_VAL) + ret = setHighVoltage(val, mess); + else + ret = getHighVoltage(&retval, mess); + return retval; +#endif + + +#ifdef CHIPTESTBOARDD + case V_POWER_CHIP: + if (val != GET_VAL) { + 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)); + return retval; + } + ret = getVchip(&retval, mess); + return retval; +#endif + + +#if defined(CHIPTESTBOARDD) || defined(XILINX_CHIPTESTBOARDD) + case V_LIMIT: + if (val != GET_VAL) { + if (!mV) { + ret = FAIL; + strcpy(mess, "Could not set vlimit. VLimit should be in " + "mV and not dac units.\n"); + LOG(logERROR, (mess)); + return retval; + } + ret = setVLimit(val, mess); + } else + retval = getVLimit(); + +#endif + + + + + +#if defined(CHIPTESTBOARDD) || defined(XILINX_CHIPTESTBOARDD) + case V_POWER_A: + case V_POWER_B: + case V_POWER_C: + case V_POWER_D: + case V_POWER_IO: + if (val != GET_VAL) { + if (!mV) { + ret = FAIL; + sprintf(mess, "Could not set power. Power regulator %d should be in mV and not dac units.\n", ind); + LOG(logERROR, (mess)); + return retval; + } + ret = setPower(val, mess); + } else + ret = getPower(&retval, mess); + +#endif + + int retval = -1; enum DACINDEX serverDacIndex = 0; // valid enums switch (ind) { -#ifndef XILINX_CHIPTESTBOARDD case HIGH_VOLTAGE: -#endif #ifdef EIGERD case IO_DELAY: #elif CHIPTESTBOARDD @@ -1176,191 +1260,17 @@ int validateAndSetDac(enum dacIndex ind, int val, int mV) { if (ret == FAIL) { return retval; } + + + +#ifdef XILINX_CHIPTESTBOARDD + ret = SetDacEnum(ind, val, mV, &retval, mess); + return retval; +#endif + switch (ind) { - // adc vpp -#if defined(CHIPTESTBOARDD) - case ADC_VPP: - // set - if (val >= 0) { - ret = AD9257_SetVrefVoltage(val, mV); - if (ret == FAIL) { - sprintf(mess, "Could not set Adc Vpp. Please set a " - "proper value\n"); - LOG(logERROR, (mess)); - } - } - retval = AD9257_GetVrefVoltage(mV); - LOG(logDEBUG1, - ("Adc Vpp retval: %d %s\n", retval, (mV ? "mV" : "mode"))); - // cannot validate (its just a variable and mv gives different - // value) - break; -#endif - // io delay -#ifdef EIGERD - case IO_DELAY: - retval = setIODelay(val); - LOG(logDEBUG1, ("IODelay: %d\n", retval)); - validate(&ret, mess, val, retval, "set iodelay", DEC); - break; -#endif - // high voltage -#ifndef XILINX_CHIPTESTBOARDD - case HIGH_VOLTAGE: - -#if defined(MYTHEN3D) || defined(GOTTHARD2D) - if ((val != -1 && val < 0) || (val > HV_SOFT_MAX_VOLTAGE)) { - ret = FAIL; - sprintf(mess, "Invalid Voltage. Valid range (0 - %d)\n", - HV_SOFT_MAX_VOLTAGE); - LOG(logERROR, (mess)); - } else { - if (val >= 0) { - ret = setHighVoltage(val); - if (ret == FAIL) { - strcpy(mess, "Could not set high voltage.\n"); - LOG(logERROR, (mess)); - } - } - if (ret == OK) { - ret = getHighVoltage(&retval); - if (ret == FAIL) { - strcpy(mess, "Could not get high voltage.\n"); - LOG(logERROR, (mess)); - } - LOG(logDEBUG1, ("High Voltage: %d\n", retval)); - validate(&ret, mess, val, retval, "set high voltage", DEC); - } - } -#else - retval = setHighVoltage(val); - LOG(logDEBUG1, ("High Voltage: %d\n", retval)); -#if defined(JUNGFRAUD) || defined(MOENCHD) || defined(CHIPTESTBOARDD) - validate(&ret, mess, val, retval, "set high voltage", DEC); -#endif -#endif - -#ifdef EIGERD - if ((retval != SLAVE_HIGH_VOLTAGE_READ_VAL) && (retval < 0)) { - ret = FAIL; - if (retval == -1) - sprintf(mess, - "Setting high voltage failed. Bad value %d. " - "The range is from 0 to 200 V.\n", - val); - else if (retval == -2) - strcpy(mess, "Setting high voltage failed. " - "Serial/i2c communication failed.\n"); - else if (retval == -3) - strcpy(mess, "Getting high voltage failed. " - "Serial/i2c communication failed.\n"); - LOG(logERROR, (mess)); - } -#endif - break; -#endif - // power -#if defined(CHIPTESTBOARDD) || defined(XILINX_CHIPTESTBOARDD) - case V_POWER_A: - case V_POWER_B: - case V_POWER_C: - case V_POWER_D: - case V_POWER_IO: - 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); - LOG(logERROR, (mess)); - } else if (checkVLimitCompliant(val) == FAIL) { - ret = FAIL; - sprintf(mess, - "Could not set power. Power regulator %d " - "exceeds voltage limit %d.\n", - ind, getVLimit()); - LOG(logERROR, (mess)); - } - - else if (!isPowerValid(serverDacIndex, val)) { - ret = FAIL; - sprintf( - mess, - "Could not set power. Power regulator %d " - "should be between %d and %d mV\n", - ind, - (serverDacIndex == D_PWR_IO ? VIO_MIN_MV : POWER_RGLTR_MIN), -#ifdef CHIPTESTBOARDD - (VCHIP_MAX_MV - VCHIP_POWER_INCRMNT)); -#else - POWER_RGLTR_MAX); -#endif - LOG(logERROR, (mess)); - } - - else { - setPower(serverDacIndex, val); - } - } - if (ret == OK) { - retval = getPower(serverDacIndex); - LOG(logDEBUG1, ("Power regulator(%d): %d\n", ind, retval)); - validate(&ret, mess, val, retval, "set power regulator", DEC); - } - break; -#endif - -#ifdef CHIPTESTBOARDD - case V_POWER_CHIP: - if (val >= 0) { - 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)); - /* restrict users from setting vchip - if (!mV) { - ret = FAIL; - sprintf(mess,"Could not set Vchip. Should be in mV and - not dac units.\n"); LOG(logERROR,(mess)); } else if - (!isVchipValid(val)) { ret = FAIL; sprintf(mess,"Could not - set Vchip. Should be between %d and %d mV\n", VCHIP_MIN_MV, - VCHIP_MAX_MV); LOG(logERROR,(mess)); } else { setVchip(val); - } - */ - } - retval = getVchip(); - LOG(logDEBUG1, ("Vchip: %d\n", retval)); - if (ret == OK && val != GET_FLAG && val != -100 && retval != val) { - ret = FAIL; - sprintf(mess, "Could not set vchip. Set %d, but read %d\n", val, - retval); - LOG(logERROR, (mess)); - } - break; -#endif - - // vlimit -#if defined(CHIPTESTBOARDD) || defined(XILINX_CHIPTESTBOARDD) - case V_LIMIT: - if (val >= 0) { - if (!mV) { - ret = FAIL; - strcpy(mess, "Could not set power. VLimit should be in " - "mV and not dac units.\n"); - LOG(logERROR, (mess)); - } else { - setVLimit(val); - } - } - retval = getVLimit(); - LOG(logDEBUG1, ("VLimit: %d\n", retval)); - validate(&ret, mess, val, retval, "set vlimit", DEC); - break; -#endif // dacs default: if (mV && val > DAC_MAX_MV) { @@ -1443,8 +1353,11 @@ int validateAndSetDac(enum dacIndex ind, int val, int mV) { break; } return retval; + +#endif } + int set_dac(int file_des) { ret = OK; memset(mess, 0, sizeof(mess)); diff --git a/slsDetectorServers/xilinx_ctbDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/xilinx_ctbDetectorServer/slsDetectorFunctionList.c index af5804c82..d56724d2d 100644 --- a/slsDetectorServers/xilinx_ctbDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/xilinx_ctbDetectorServer/slsDetectorFunctionList.c @@ -1151,6 +1151,31 @@ int64_t getMeasurementTime() { /* parameters - dac, adc, hv */ +int setPower(enum DACINDEX ind, int val, int mV, char* mess) { + + serverdacindex + if (checkVLimitCompliant(val) == FAIL) { + sprintf(mess, "Could not set power. Power regulator %d exceeds voltage limit %d.\n", ind, getVLimit()); + LOG(logERROR, (mess)); + return FAIL; + } + if (!isPowerValid(ind, val)) { + sprintf(mess, "Could not set power. Power regulator %d should be between %d and %d mV\n", ind, (ind == D_PWR_IO ? VIO_MIN_MV : POWER_RGLTR_MIN), POWER_RGLTR_MAX); + LOG(logERROR, (mess)); + return FAIL; + } + setPower(ind, val); + int retval = getPower(ind); + validate(&ret, mess, val, retval, "set power regulator", DEC); +} + +int getPower(enum DACINDEX ind, int* retval, char* mess) { + *retval = getPower(ind); + LOG(logDEBUG1, ("Power regulator(%d): %d\n", ind, retval)); +} + + + void setDAC(enum DACINDEX ind, int val, int mV) { char dacName[MAX_STR_LENGTH] = {0}; memset(dacName, 0, MAX_STR_LENGTH); @@ -1208,9 +1233,13 @@ int checkVLimitDacCompliant(int dac) { int getVLimit() { return vLimit; } -void setVLimit(int l) { - if (l >= 0) - vLimit = l; +int setVLimit(int val, char* mess) { + if (val < 0) { + sprintf(mess, "Could not set vlimit. Invalid value %d\n", val); + LOG(logERROR, (mess)); + return FAIL; + } + vLimit = val; } int getBitOffsetFromDACIndex(enum DACINDEX ind) { diff --git a/slsDetectorServers/xilinx_ctbDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/xilinx_ctbDetectorServer/slsDetectorFunctionList.h index 1b584109f..2c3a74973 100644 --- a/slsDetectorServers/xilinx_ctbDetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/xilinx_ctbDetectorServer/slsDetectorFunctionList.h @@ -126,11 +126,11 @@ int dacToVoltage(int dac); int checkVLimitCompliant(int mV); int checkVLimitDacCompliant(int dac); int getVLimit(); -void setVLimit(int l); +int setVLimit(int val, char* mess); int getBitOffsetFromDACIndex(enum DACINDEX ind); int isPowerValid(enum DACINDEX ind, int val); -int getPower(); -void setPower(enum DACINDEX ind, int val); +int getPower(int* retval, char* mess); +int setPower(enum DACINDEX ind, int val, char* mess); int getADC(enum ADCINDEX ind, int *value); int getSlowADC(int ichan, int *retval); int getTemperature(int *retval);