From 79693a38f5f0b8086bf98c461a92ed71adbde0d0 Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Wed, 12 Dec 2018 11:20:27 +0100 Subject: [PATCH] in between --- .../ctbDetectorServer/LTC2620.h | 1 + .../ctbDetectorServer/RegisterDefs.h | 19 +- .../slsDetectorFunctionList.c | 254 ++++++--------- .../slsDetectorServer_defs.h | 22 -- .../slsDetectorFunctionList.c | 111 +++---- .../gotthardDetectorServer/LTC2620.h | 1 + .../gotthardDetectorServer/RegisterDefs.h | 27 +- .../gotthardDetectorServer/gitInfo.txt | 8 +- .../gotthardDetectorServer/gitInfoGotthard.h | 6 +- .../slsDetectorFunctionList.c | 148 +++------ .../slsDetectorServer_defs.h | 14 - .../jungfrauDetectorServer/LTC2620.h | 1 + .../jungfrauDetectorServer/RegisterDefs.h | 37 ++- .../slsDetectorFunctionList.c | 166 +++------- .../slsDetectorServer_defs.h | 19 -- slsDetectorServers/slsDetectorServer/AD7689.h | 165 +++++++++- slsDetectorServers/slsDetectorServer/AD9252.h | 116 ++++--- slsDetectorServers/slsDetectorServer/AD9257.h | 85 ++++-- slsDetectorServers/slsDetectorServer/I2C.h | 65 ++-- slsDetectorServers/slsDetectorServer/INA226.h | 41 +-- .../slsDetectorServer/LTC2620.h | 288 ++++++++++++++++++ .../slsDetectorServer/commonServerFunctions.h | 48 ++- .../slsDetectorFunctionList.h | 31 +- .../slsDetectorServer_funcs.c | 77 +++-- .../slsDetector/slsDetector.cpp | 10 +- .../slsDetector/slsDetectorCommand.cpp | 3 + slsSupportLib/include/sls_detector_defs.h | 11 +- 27 files changed, 1044 insertions(+), 730 deletions(-) create mode 120000 slsDetectorServers/ctbDetectorServer/LTC2620.h create mode 120000 slsDetectorServers/gotthardDetectorServer/LTC2620.h create mode 120000 slsDetectorServers/jungfrauDetectorServer/LTC2620.h create mode 100755 slsDetectorServers/slsDetectorServer/LTC2620.h diff --git a/slsDetectorServers/ctbDetectorServer/LTC2620.h b/slsDetectorServers/ctbDetectorServer/LTC2620.h new file mode 120000 index 000000000..13157cb8b --- /dev/null +++ b/slsDetectorServers/ctbDetectorServer/LTC2620.h @@ -0,0 +1 @@ +../slsDetectorServer/LTC2620.h \ No newline at end of file diff --git a/slsDetectorServers/ctbDetectorServer/RegisterDefs.h b/slsDetectorServers/ctbDetectorServer/RegisterDefs.h index f60d63a3c..8450763be 100644 --- a/slsDetectorServers/ctbDetectorServer/RegisterDefs.h +++ b/slsDetectorServers/ctbDetectorServer/RegisterDefs.h @@ -175,8 +175,8 @@ /* DAC Value Out RO register */ //#define DAC_VAL_OUT_REG (0x2A << MEM_MAP_SHIFT) -/* ADC Value RO register */ -#define ADC_VAL_REG (0x2B << MEM_MAP_SHIFT) +/* Slow ADC SPI Value RO register */ +#define ADC_SPI_SLOW_VAL_REG (0x2B << MEM_MAP_SHIFT) /* FIFO Digital In Status RO register */ #define FIFO_DIN_STATUS_REG (0x3B << MEM_MAP_SHIFT) @@ -200,7 +200,6 @@ #define SPI_HV_SRL_CLK_OTPT_MSK (0x00000001 << SPI_HV_SRL_CLK_OTPT_OFST) #define SPI_HV_SRL_CS_OTPT_OFST (10) #define SPI_HV_SRL_CS_OTPT_MSK (0x00000001 << SPI_HV_SRL_CS_OTPT_OFST) -#define SPI_IDLE_MSK (SPI_DAC_SRL_DGTL_OTPT_MSK | SPI_DAC_SRL_CLK_OTPT_MSK | SPI_DAC_SRL_CS_OTPT_MSK | SPI_HV_SRL_DGTL_OTPT_MSK | SPI_HV_SRL_CLK_OTPT_MSK | SPI_HV_SRL_CS_OTPT_MSK) /* ADC SPI (Serial Peripheral Interface) RW register */ #define ADC_SPI_REG (0x41 << MEM_MAP_SHIFT) @@ -211,14 +210,12 @@ #define ADC_SPI_SRL_DT_OTPT_MSK (0x00000001 << ADC_SPI_SRL_DT_OTPT_OFST) #define ADC_SPI_SRL_CS_OTPT_OFST (2) #define ADC_SPI_SRL_CS_OTPT_MSK (0x0000000F << ADC_SPI_SRL_CS_OTPT_OFST) -#define ADC_SPI_SLOW_SRL_DT_OTPT_OFST (8) -#define ADC_SPI_SLOW_SRL_DT_OTPT_MSK (0x00000001 << ADC_SPI_SLOW_SRL_DT_OTPT_OFST) -#define ADC_SPI_SLOW_SRL_CLK_OTPT_OFST (9) -#define ADC_SPI_SLOW_SRL_CLK_OTPT_MSK (0x00000001 << ADC_SPI_SLOW_SRL_CLK_OTPT_OFST) -#define ADC_SPI_SLOW_SRL_CS_OTPT_OFST (10) -#define ADC_SPI_SLOW_SRL_CS_OTPT_MSK (0x0000000F << ADC_SPI_SLOW_SRL_CS_OTPT_OFST) -#define ADC_SPI_IDLE_MSK (ADC_SPI_SRL_CLK_OTPT_MSK | ADC_SPI_SRL_DT_OTPT_MSK | ADC_SPI_SRL_CS_OTPT_MSK | ADC_SPI_SLOW_SRL_DT_OTPT_MSK | ADC_SPI_SLOW_SRL_CLK_OTPT_MSK | ADC_SPI_SLOW_SRL_CS_OTPT_MSK) - +#define ADC_SPI_SLOW_SRL_DT_OFST (8) +#define ADC_SPI_SLOW_SRL_DT_MSK (0x00000001 << ADC_SPI_SLOW_SRL_DT_OFST) +#define ADC_SPI_SLOW_SRL_CLK_OFST (9) +#define ADC_SPI_SLOW_SRL_CLK_MSK (0x00000001 << ADC_SPI_SLOW_SRL_CLK_OFST) +#define ADC_SPI_SLOW_SRL_CNV_OFST (10) +#define ADC_SPI_SLOW_SRL_CNV_MSK (0x00000001 << ADC_SPI_SLOW_SRL_CNV_OFST) /* ADC Offset RW register */ #define ADC_OFFSET_REG (0x42 << MEM_MAP_SHIFT) diff --git a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c index c30c9e329..31be1eb1a 100644 --- a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c @@ -6,6 +6,8 @@ #ifndef VIRTUAL #include "AD9257.h" // commonServerFunctions.h, blackfin.h, ansi.h #include "AD7689.h" // slow adcs +#include "LTC2620.h" // dacs +#include "MAX1932.h" // hv #include "INA226.h" // i2c #include "programfpga.h" #else @@ -480,38 +482,41 @@ void setupDetector() { resetPeripheral(); cleanFifos(); + // set defines + AD7689_SetDefines(ADC_SPI_REG, ADC_SPI_SLOW_VAL_REG, ADC_SPI_SLOW_SRL_CNV_MSK, ADC_SPI_SLOW_SRL_CLK_MSK, ADC_SPI_SLOW_SRL_DT_MSK, ADC_SPI_SLOW_SRL_DT_OFST); + AD9257_SetDefines(ADC_SPI_REG, ADC_SPI_SRL_CS_OTPT_MSK, ADC_SPI_SRL_CLK_OTPT_MSK, ADC_SPI_SRL_DT_OTPT_MSK, ADC_SPI_SRL_DT_OTPT_OFST); + LTC2620_SetDefines(SPI_REG, SPI_DAC_SRL_CS_OTPT_MSK, SPI_DAC_SRL_CLK_OTPT_MSK, SPI_DAC_SRL_DGTL_OTPT_MSK, SPI_DAC_SRL_DGTL_OTPT_OFST, NDAC, MAX_DAC_VOLTAGE_VALUE); + MAX1932_SetDefines(SPI_REG, SPI_HV_SRL_CS_OTPT_MSK, SPI_HV_SRL_CLK_OTPT_MSK, SPI_HV_SRL_DGTL_OTPT_MSK, SPI_HV_SRL_DGTL_OTPT_OFST); + // disable spi - bus_w(SPI_REG, SPI_IDLE_MSK); - bus_w(ADC_SPI_REG, ADC_SPI_IDLE_MSK); + AD7689_Disable(); + AD9257_Disable(); + LTC2620_Disable(); + MAX1932_Disable(); - // prepare ADCs #ifndef VIRTUAL - prepareADC9257(); - // slow ADCs - prepareAD7689(); + // adcs + AD9257_Configure(); + // slow adcs + AD7689_Configure(); // I2C - I2C_ConfigureI2CCore(I2C_SCL_LOW_COUNT_REG, I2C_SCL_HIGH_COUNT_REG, I2C_SDA_HOLD_REG, I2C_CONTROL_REG); - INA226_CalibrateCurrentRegister(I2C_SHUNT_RESISTER_OHMS, I2C_TRANSFER_COMMAND_FIFO_REG, I2C_POWER_VIO_DEVICE_ID); - INA226_CalibrateCurrentRegister(I2C_SHUNT_RESISTER_OHMS, I2C_TRANSFER_COMMAND_FIFO_REG, I2C_POWER_VA_DEVICE_ID); - INA226_CalibrateCurrentRegister(I2C_SHUNT_RESISTER_OHMS, I2C_TRANSFER_COMMAND_FIFO_REG, I2C_POWER_VB_DEVICE_ID); - INA226_CalibrateCurrentRegister(I2C_SHUNT_RESISTER_OHMS, I2C_TRANSFER_COMMAND_FIFO_REG, I2C_POWER_VC_DEVICE_ID); - INA226_CalibrateCurrentRegister(I2C_SHUNT_RESISTER_OHMS, I2C_TRANSFER_COMMAND_FIFO_REG, I2C_POWER_VD_DEVICE_ID); + INA226_ConfigureI2CCore(); + INA226_CalibrateCurrentRegister(I2C_POWER_VIO_DEVICE_ID); + INA226_CalibrateCurrentRegister(I2C_POWER_VA_DEVICE_ID); + INA226_CalibrateCurrentRegister(I2C_POWER_VB_DEVICE_ID); + INA226_CalibrateCurrentRegister(I2C_POWER_VC_DEVICE_ID); + INA226_CalibrateCurrentRegister(I2C_POWER_VD_DEVICE_ID); + // dacs + LTC2620_Configure(); #endif - - // initialize dac series - initDac(0); - initDac(8); - initDac(16); - // switch off power regulators powerChip(0); //FIXME: // switch off dacs (power regulators most likely only sets to minimum (if power enable on)) { int idac = 0; - int retval[2] = {0, 0}; for (idac = 0; idac < NDAC; ++idac) { - setDac(idac, -100, 0, retval); + setDac(idac, LTC2620_PWR_DOWN_VAL, 0); } } @@ -1030,116 +1035,31 @@ int validateTimer(enum timerIndex ind, int64_t val, int64_t retval) { /* parameters - dac, adc, hv */ -void initDac(int dacnum) { +void setDAC(enum DACINDEX ind, int val, int mV) { + FILE_LOG(logDEBUG1, ("Setting dac[%d]: %d %s \n", (int)ind, val, (mV ? "mV" : "dac units"))); + int dacval = val; #ifdef VIRTUAL - return; -#endif - FILE_LOG(logINFOBLUE, ("Initializing dac %d\n",dacnum)); - - uint32_t codata; - int csdx = dacnum / NDAC + SPI_DAC_SRL_CS_OTPT_MSK; - int dacchannel = 0xf; // all channels - int dacvalue = 0x6; // (low value) can be any random value (just writing to power up) - FILE_LOG(logINFO, ("\tWrite to Input Register\n" - "\tChip select bit: %d\n" - "\tDac Channel: 0x%x\n" - "\tDac Value: 0x%x\n", - csdx, dacchannel, dacvalue)); - - codata = LTC2620_DAC_CMD_WRITE + // command to write to input register - ((dacchannel << LTC2620_DAC_ADDR_OFST) & LTC2620_DAC_ADDR_MSK) + // all channels - ((dacvalue << LTC2620_DAC_DATA_OFST) & LTC2620_DAC_DATA_MSK); // any random value - serializeToSPI(SPI_REG, codata, (0x1 << csdx), LTC2620_DAC_NUMBITS, - SPI_DAC_SRL_CLK_OTPT_MSK, SPI_DAC_SRL_DGTL_OTPT_MSK, SPI_DAC_SRL_DGTL_OTPT_OFST); -} - - -int voltageToDac(int value){ - return generalVoltageToDac(value, 0, DAC_MAX_VOLTAGE_MV, 1); -} - -int dacToVoltage(unsigned int digital){ - return generalDacToVoltage(digital, 0, DAC_MAX_VOLTAGE_MV, 1); -} - -int generalVoltageToDac(int value, int vmin, int vmax, int check) { - int nsteps = MAX_DAC_UNIT_VALUE; - if (check && ((value < vmin) || (value > vmax))) { - FILE_LOG(logERROR, ("Voltage value (to convert to dac value) is outside bounds: %d\n", value)); - return -1; - } - return (int)(((value - vmin) / (vmax - vmin)) * (nsteps - 1) + 0.5); -} - -int generalDacToVoltage(unsigned int digital, int vmin, int vmax, int check) { - int nsteps = MAX_DAC_UNIT_VALUE; - int v = vmin + (vmax - vmin) * digital / (nsteps - 1); - if (check && ((v < 0) || (v > vmax))) { - FILE_LOG(logERROR, ("Voltage value (converted from dac value) is outside bounds: %d\n", v)); - return -1; - } - return v; -} - -void setDAC(enum DACINDEX ind, int val, int mV, int retval[]) { - // validate index - if (ind < 0 || ind >= NDAC) { - FILE_LOG(logERROR, ("Dac index %d is not defined\n", ind)); - retval[0] = -1; - return; - } - - int dacmV = val; - - //if set and mv, convert to dac - if (val > 0) { - if (mV) - val = voltageToDac(val); - else - dacmV = dacToVoltage(val); - // conversion out of bounds - if (val == -1 || dacmV == -1) { - FILE_LOG(logERROR, ("Setting Dac %d %s is out of bounds\n", ind, (mV ? "mV" : "dac units"));) - return; - } - } - - if ( (val >= 0) || (val == -100)) { -#ifdef VIRTUAL - dacValues[ind] = val; + if (mV && LTC2620_VoltageToDac(val, &dacval) == OK) + dacValues[ind] = val; #else - uint32_t codata; - int csdx = ind / NDAC + SPI_DAC_SRL_CS_OTPT_MSK; - int dacchannel = ind % NDAC; - - FILE_LOG(logINFO, ("Setting DAC %d: %d dac (%d mV)\n",ind, val, dacmV)); - // command - if (val >= 0) { - FILE_LOG(logDEBUG1,("\tWrite to Input Register and Update\n")); - codata = LTC2620_DAC_CMD_SET; - - } else if (val == -100) { - FILE_LOG(logDEBUG1, ("\tPOWER DOWN\n")); - codata = LTC2620_DAC_CMD_POWER_DOWN; - } - // address - FILE_LOG(logDEBUG1, ("\tChip select bit:%d\n" - "\tDac Channel:0x%x\n" - "\tDac Value:0x%x\n", - csdx, dacchannel, val)); - codata += ((dacchannel << LTC2620_DAC_ADDR_OFST) & LTC2620_DAC_ADDR_MSK) + - ((val << LTC2620_DAC_DATA_OFST) & LTC2620_DAC_DATA_MSK); - // to spi - serializeToSPI(SPI_REG, codata, (0x1 << csdx), LTC2620_DAC_NUMBITS, - SPI_DAC_SRL_CLK_OTPT_MSK, SPI_DAC_SRL_DGTL_OTPT_MSK, SPI_DAC_SRL_DGTL_OTPT_OFST); - - dacValues[ind] = val; + if (LTC2620_SetDACValue((int)ind, val, mV, &dacval) == OK) + dacValues[ind] = dacval; #endif - } +} - retval[0] = dacValues[ind]; - retval[1] = dacToVoltage(retval[0]); - FILE_LOG(logDEBUG1, ("Getting DAC %d : %d dac (%d mV)\n",ind, retval[0], retval[1])); +int getDAC(enum DACINDEX ind, int mV) { + if (!mV) { + FILE_LOG(logDEBUG1, ("Getting DAC %d : %d dac\n",ind, dacValues[ind])); + return dacValues[ind]; + } + int voltage = -1; + LTC2620_DacToVoltage(dacValues[ind], &voltage); + FILE_LOG(logDEBUG1, ("Getting DAC %d : %d dac (%d mV)\n",ind, dacValues[ind], voltage)); + return voltage; +} + +int getMAXDACUnits() { + return LTC2620_MAX_STEPS; } int checkVLimitCompliant(int mV) { @@ -1148,6 +1068,18 @@ int checkVLimitCompliant(int mV) { return OK; } +int checkVLimitDacCompliant(int dac) { + if (vLimit > 0) { + 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; } @@ -1166,9 +1098,11 @@ int isVchipValid(int val) { int getVchip() { // not set yet - if (dacValues[D_PWR_CHIP] == -1 || dacValues[D_PWR_CHIP] == -100) + if (dacValues[D_PWR_CHIP] == -1 || dacValues[D_PWR_CHIP] == LTC2620_PWR_DOWN_VAL) return dacValues[D_PWR_CHIP]; - return generalDacToVoltage(dacValues[D_PWR_CHIP], VCHIP_MIN_MV, VCHIP_MAX_MV, 1); + int voltage = -1; + Common_DacToVoltage(dacValues[D_PWR_CHIP], &voltage, VCHIP_MIN_MV, VCHIP_MAX_MV, LTC2620_MAX_STEPS); + return voltage; } void setVchip(int val) { @@ -1176,23 +1110,19 @@ void setVchip(int val) { if (val != -1) { FILE_LOG(logINFO, ("Setting Vchip to %d mV\n", val)); - int dacval = -100; + int dacval = LTC2620_PWR_DOWN_VAL; // validate & convert it to dac - if (val != -100) { + if (val != LTC2620_PWR_DOWN_VAL) { // convert it to dac - dacval = generalVoltageToDac(val, VCHIP_MIN_MV, VCHIP_MAX_MV, 1); - - // validity (already checked at tcp) - if (dacval == -1) { + if (Common_VoltageToDac(val, &dacval, VCHIP_MIN_MV, VCHIP_MAX_MV, LTC2620_MAX_STEPS) == FAIL) { FILE_LOG(logERROR, ("\tVChip %d mV invalid. Is not between %d and %d mV\n", val, VCHIP_MIN_MV, VCHIP_MAX_MV)); return; } } // set - int retval[2] = {0, 0}; - setDAC(D_PWR_CHIP, dacval, 0, retval); + setDAC(D_PWR_CHIP, dacval, 0); } } @@ -1300,22 +1230,22 @@ int getPower(enum DACINDEX ind) { } // dac powered off - if (dacValues[ind] == -100) { - FILE_LOG(logWARNING, ("Power %d enabled, dac value -100, voltage at minimum or 0\n", ind)); - return -100; + if (dacValues[ind] == LTC2620_PWR_DOWN_VAL) { + FILE_LOG(logWARNING, ("Power %d enabled, dac value %d, voltage at minimum or 0\n", ind, LTC2620_PWR_DOWN_VAL)); + return LTC2620_PWR_DOWN_VAL; } // vchip not set, weird error, should not happen (as vchip set to max in the beginning) - // unless user set vchip to -100 and then tried to get a power regulator value - if (dacValues[D_PWR_CHIP] == -1 || dacValues[D_PWR_CHIP] == -100) { + // unless user set vchip to LTC2620_PWR_DOWN_VAL and then tried to get a power regulator value + if (dacValues[D_PWR_CHIP] == -1 || dacValues[D_PWR_CHIP] == LTC2620_PWR_DOWN_VAL) { FILE_LOG(logERROR, ("Cannot read power regulator %d (vchip not set)." "Set a power regulator, which will also set vchip.\n")); return -1; } // voltage value - int retval = generalDacToVoltage(dacValues[ind], POWER_RGLTR_MIN, (getVchip() - VCHIP_POWER_INCRMNT), 1); - + Common_DacToVoltage(dacValues[ind], &retval, POWER_RGLTR_MIN, (getVchip() - VCHIP_POWER_INCRMNT), LTC2620_MAX_STEPS); + return retval; } void setPower(enum DACINDEX ind, int val) { @@ -1339,8 +1269,6 @@ void setPower(enum DACINDEX ind, int val) { return; } - // dummy variable to set dac - int retval[2] = {0, 0}; // get vchip to set vchip (calculated now before switching off power enable) int vchip = getVChipToSet(ind, val); @@ -1348,7 +1276,7 @@ void setPower(enum DACINDEX ind, int val) { bus_w(addr, bus_r(addr) & ~(mask)); // power down dac - setDac(ind, -100, 0, retval); + setDac(ind, LTC2620_PWR_DOWN_VAL, 0); // set vchip setVchip(vchip); @@ -1358,12 +1286,15 @@ void setPower(enum DACINDEX ind, int val) { } // convert it to dac - if (val != -100) { + if (val != LTC2620_PWR_DOWN_VAL) { // convert it to dac - int dacval = generalVoltageToDac(val, POWER_RGLTR_MIN, vchip - VCHIP_POWER_INCRMNT, 1); + if (Common_VoltageToDac(val, &dacval, POWER_RGLTR_MIN, vchip - VCHIP_POWER_INCRMNT, LTC2620_MAX_STEPS) == FAIL) { + FILE_LOG(logERROR, ("\tPower index %d of value %d mV invalid. Is not between %d and %d mV\n", ind, val, POWER_RGLTR_MIN, vchip - VCHIP_POWER_INCRMNT)); + return; + } // set and power on/ update dac - setDAC(ind, dacval, 0, retval); + setDAC(ind, dacval, 0); // to be sure of valid conversion if (dacval >= 0) @@ -1392,24 +1323,25 @@ int getADC(enum ADCINDEX ind){ case I_PWR_D: return INA226_ReadCurrent(I2C_TRANSFER_COMMAND_FIFO_REG, I2C_RX_DATA_FIFO_LEVEL_REG, I2C_POWER_VIO_DEVICE_ID + (int)(ind - I_PWR_IO)); + + // slow adcs + case SLOW_ADC_TEMP: + return AD7689_GetTemperature(); + case SLOW_ADC0: + case SLOW_ADC1: + case SLOW_ADC2: + case SLOW_ADC3: + case SLOW_ADC4: + case SLOW_ADC5: + case SLOW_ADC6: + case SLOW_ADC7: + return AD7689_GetChannel(ind - SLOW_ADC0); default: - if (ind >= SLOW_ADC_START_INDEX && ind <= SLOW_ADC_END_INDEX) { - return getAD7689(ind - SLOW_ADC_START_INDEX); - } FILE_LOG(logERROR, ("Adc Index %d not defined \n", (int)ind)); return -1; } } -int getVoltage(int idac) { -// FIXME: to be implemented - return 0;//ina226 -} - -int getCurrent(int idac) { - // FIXME: to be implemented - return 0; -} int setHighVoltage(int val){ #ifdef VIRTUAL diff --git a/slsDetectorServers/ctbDetectorServer/slsDetectorServer_defs.h b/slsDetectorServers/ctbDetectorServer/slsDetectorServer_defs.h index d6e24397a..fcb117be5 100644 --- a/slsDetectorServers/ctbDetectorServer/slsDetectorServer_defs.h +++ b/slsDetectorServers/ctbDetectorServer/slsDetectorServer_defs.h @@ -77,11 +77,7 @@ enum DACINDEX {D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, #define WAIT_TIME_US_STP_ACQ (100) #define WAIT_TIME_CONFIGURE_MAC (500 * 1000) -#define SLOW_ADC_START_INDEX (1000) -#define SLOW_ADC_END_INDEX (1008) - #define MAX_DAC_VOLTAGE_VALUE (2500) -#define MAX_DAC_UNIT_VALUE (4096) #define DAC_MAX_VOLTAGE_MV (2500) #define VCHIP_MAX_MV (2700) #define VCHIP_MIN_MV (1700) @@ -103,24 +99,6 @@ enum DACINDEX {D0, D1, D2, D3, D4, D5, D6, D7, D8, D9, #define LSB_OF_64_BIT_REG_OFST (0) #define BIT_32_MSK (0xFFFFFFFF) -/* LTC2620 DAC DEFINES */ -#define LTC2620_DAC_CMD_OFST (20) -#define LTC2620_DAC_CMD_MSK (0x0000000F << LTC2620_DAC_CMD_OFST) -#define LTC2620_DAC_ADDR_OFST (16) -#define LTC2620_DAC_ADDR_MSK (0x0000000F << LTC2620_DAC_ADDR_OFST) -#define LTC2620_DAC_DATA_OFST (4) -#define LTC2620_DAC_DATA_MSK (0x00000FFF << LTC2620_DAC_DATA_OFST) - -#define LTC2620_DAC_CMD_WRITE (0x00000000 << LTC2620_DAC_CMD_OFST) -#define LTC2620_DAC_CMD_SET (0x00000003 << LTC2620_DAC_CMD_OFST) -#define LTC2620_DAC_CMD_POWER_DOWN (0x00000004 << LTC2620_DAC_CMD_OFST) -#define LTC2620_DAC_NUMBITS (24) - -/* MAX1932 HV DEFINES */ -#define MAX1932_HV_NUMBITS (8) -#define MAX1932_HV_DATA_OFST (0) -#define MAX1932_HV_DATA_MSK (0x000000FF << MAX1932_HV_DATA_OFST) - /** PLL Reconfiguration Registers */ //https://www.altera.com/documentation/mcn1424769382940.html #define PLL_MODE_REG (0x00) diff --git a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c index 46c8f900d..25d04ddd3 100644 --- a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c @@ -1005,66 +1005,75 @@ int setThresholdEnergy(int ev) { /* parameters - dac, adc, hv */ -void setDAC(enum DACINDEX ind, int val, int mV, int retval[]) { - FILE_LOG(logDEBUG1, ("Going to set dac %d to %d with mv mode %d \n", (int)ind, val, mV)); +void setDAC(enum DACINDEX ind, int val, int mV) { + if (val < 0) + return; + + FILE_LOG(logDEBUG1, ("Setting dac[%d]: %d %s \n", (int)ind, val, (mV ? "mV" : "dac units"))); + if (ind == VTHRESHOLD) { - int ret[5]; - setDAC(VCMP_LL,val,mV,retval); - ret[0] = retval[mV]; - setDAC(VCMP_LR,val,mV,retval); - ret[1] = retval[mV]; - setDAC(VCMP_RL,val,mV,retval); - ret[2] = retval[mV]; - setDAC(VCMP_RR,val,mV,retval); - ret[3] = retval[mV]; - setDAC(VCP,val,mV,retval); - ret[4] = retval[mV]; - - - if ((ret[0]== ret[1])&& - (ret[1]==ret[2])&& - (ret[2]==ret[3]) && - (ret[3]==ret[4])) { - FILE_LOG(logINFO, ("vthreshold match\n")); - } else { - retval[0] = -1;retval[1] = -1; - FILE_LOG(logERROR, ("vthreshold mismatch 0:%d 1:%d 2:%d 3:%d\n", - ret[0],ret[1],ret[2],ret[3])); - } + setDAC(VCMP_LL, val, mV); + setDAC(VCMP_LR, val, mV); + setDAC(VCMP_RL, val, mV); + setDAC(VCMP_RR, val, mV); + setDAC(VCP, val, mV); return; } - char iname[10]; - if (((int)ind>=0)&&((int)ind= 0) ? "Setting" : "Getting", - ind, iname, val, - mV ? "mV" : "dac units")); + // validate index + if (ind < 0 || ind >= NDAC) { + FILE_LOG(logERROR, ("\tDac index %d is out of bounds (0 to %d)\n", ind, NDAC - 1)); + return; + } + #ifdef VIRTUAL - if (mV) { - retval[0] = (int)(((val-0)/(2048-0))*(4096-1) + 0.5); - retval[1] = val; - } else - retval[0] = val; - + if (mV && Common_VoltageToDac(val, &dacval, 0, MAX_DAC_VOLTAGE_VALUE, MAX_DAC_UNIT_VALUE) == OK) + (detectorModules)->dacs[ind] = val; #else - if (val >= 0) - Feb_Control_SetDAC(iname,val,mV); - int k; - Feb_Control_GetDAC(iname, &k,0); - retval[0] = k; - Feb_Control_GetDAC(iname,&k,1); - retval[1] = k; + char iname[10]; + strcpy(iname,dac_names[(int)ind]); + if (Feb_Control_SetDAC(iname, val, mV)) { + int dacval = 0; + Feb_Control_GetDAC(iname, &dacval, 0); + (detectorModules)->dacs[ind] = dacval; + } #endif - (detectorModules)->dacs[ind] = retval[0]; - } +int getDAC(enum DACINDEX ind, int mV) { + if (ind == VTHRESHOLD) { + int ret[5] = {0}; + ret[0] = getDAC(VCMP_LL, mV); + ret[1] = getDAC(VCMP_LR, mV); + ret[2] = getDAC(VCMP_RL, mV); + ret[3] = getDAC(VCMP_RR, mV); + ret[4] = getDAC(VCP, mV); + + if ((ret[0]== ret[1])&& + (ret[1]==ret[2])&& + (ret[2]==ret[3]) && + (ret[3]==ret[4])) { + FILE_LOG(logINFO, ("\tvthreshold match\n")); + } else { + FILE_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; + } + + if (!mV) { + FILE_LOG(logDEBUG1, ("Getting DAC %d : %d dac\n",ind, (detectorModules)->dacs[ind])); + return dacValues[ind]; + } + int voltage = -1; + Common_DacToVoltage((detectorModules)->dacs[ind], &voltage, 0, MAX_DAC_VOLTAGE_VALUE, MAX_DAC_UNIT_VALUE); + FILE_LOG(logDEBUG1, ("Getting DAC %d : %d dac (%d mV)\n",ind, (detectorModules)->dacs[ind], voltage)); + return voltage; +} + +int getMAXDACUnits() { + return MAX_DAC_UNIT_VALUE; +} int getADC(enum ADCINDEX ind) { diff --git a/slsDetectorServers/gotthardDetectorServer/LTC2620.h b/slsDetectorServers/gotthardDetectorServer/LTC2620.h new file mode 120000 index 000000000..13157cb8b --- /dev/null +++ b/slsDetectorServers/gotthardDetectorServer/LTC2620.h @@ -0,0 +1 @@ +../slsDetectorServer/LTC2620.h \ No newline at end of file diff --git a/slsDetectorServers/gotthardDetectorServer/RegisterDefs.h b/slsDetectorServers/gotthardDetectorServer/RegisterDefs.h index 6e27f65a1..fc91db50a 100755 --- a/slsDetectorServers/gotthardDetectorServer/RegisterDefs.h +++ b/slsDetectorServers/gotthardDetectorServer/RegisterDefs.h @@ -71,24 +71,24 @@ //#define TIME_FROM_START_REG (0x16 << MEM_MAP_SHIFT) /** DAC Control register */ -#define DAC_CNTRL_REG (0x17 << MEM_MAP_SHIFT) +#define SPI_REG (0x17 << MEM_MAP_SHIFT) -#define DAC_CNTRL_CS_OFST (0) -#define DAC_CNTRL_CS_MSK (0x00000001 << DAC_CNTRL_CS_OFST) -#define DAC_CNTRL_CLK_OFST (1) -#define DAC_CNTRL_CLK_MSK (0x00000001 << DAC_CNTRL_CLK_OFST) -#define DAQ_CNTRL_DGTL_OFST (2) -#define DAQ_CNTRL_DGTL_MSK (0x00000001 << DAQ_CNTRL_DGTL_OFST) +#define SPI_DAC_SRL_CS_OTPT_OFST (0) +#define SPI_DAC_SRL_CS_OTPT_MSK (0x00000001 << SPI_DAC_SRL_CS_OTPT_OFST) +#define SPI_DAC_SRL_CLK_OTPT_OFST (1) +#define SPI_DAC_SRL_CLK_OTPT_MSK (0x00000001 << SPI_DAC_SRL_CLK_OTPT_OFST) +#define SPI_DAC_SRL_DGTL_OTPT_OFST (2) +#define SPI_DAC_SRL_DGTL_OTPT_MSK (0x00000001 << SPI_DAC_SRL_DGTL_OTPT_OFST) /** ADC SPI register */ #define ADC_SPI_REG (0x18 << MEM_MAP_SHIFT) -#define ADC_SERIAL_CLK_OUT_OFST (0) -#define ADC_SERIAL_CLK_OUT_MSK (0x00000001 << ADC_SERIAL_CLK_OUT_OFST) -#define ADC_SERIAL_DATA_OUT_OFST (1) -#define ADC_SERIAL_DATA_OUT_MSK (0x00000001 << ADC_SERIAL_DATA_OUT_OFST) -#define ADC_SERIAL_CS_OUT_OFST (2) -#define ADC_SERIAL_CS_OUT_MSK (0x0000001F << ADC_SERIAL_CS_OUT_OFST) +#define ADC_SPI_SRL_CLK_OTPT_OFST (0) +#define ADC_SPI_SRL_CLK_OTPT_MSK (0x00000001 << ADC_SPI_SRL_CLK_OTPT_OFST) +#define ADC_SPI_SRL_DT_OTPT_OFST (1) +#define ADC_SPI_SRL_DT_OTPT_MSK (0x00000001 << ADC_SPI_SRL_DT_OTPT_OFST) +#define ADC_SPI_SRL_CS_OTPT_OFST (2) +#define ADC_SPI_SRL_CS_OTPT_MSK (0x0000001F << ADC_SPI_SRL_CS_OTPT_OFST) /** ADC Sync register */ #define ADC_SYNC_REG (0x19 << MEM_MAP_SHIFT) @@ -131,6 +131,7 @@ #define TEMP_SPI_IN_T2_CLK_MSK (0x00000001 << TEMP_SPI_IN_T2_CLK_OFST) #define TEMP_SPI_IN_T2_CS_OFST (3) #define TEMP_SPI_IN_T2_CS_MSK (0x00000001 << TEMP_SPI_IN_T2_CS_OFST) +#define TEMP_SPI_IDLE_MSK (TEMP_SPI_IN_T1_CS_MSK | TEMP_SPI_IN_T2_CS_MSK) /** Temperatre SPI Out register */ #define TEMP_SPI_OUT_REG (0x1c << MEM_MAP_SHIFT) diff --git a/slsDetectorServers/gotthardDetectorServer/gitInfo.txt b/slsDetectorServers/gotthardDetectorServer/gitInfo.txt index 26e8b9277..a011deec8 100644 --- a/slsDetectorServers/gotthardDetectorServer/gitInfo.txt +++ b/slsDetectorServers/gotthardDetectorServer/gitInfo.txt @@ -1,9 +1,9 @@ Path: slsDetectorPackage/slsDetectorServers/gotthardDetectorServer URL: origin git@github.com:slsdetectorgroup/slsDetectorPackage.git Repository Root: origin git@github.com:slsdetectorgroup/slsDetectorPackage.git -Repsitory UUID: 98b5e893b180217e8ddddb95275b84d3b28a0832 -Revision: 10 +Repsitory UUID: 87f75630357249e5082b435a9ba852335881331f +Revision: 13 Branch: refactor Last Changed Author: Dhanya_Thattil -Last Changed Rev: 4175 -Last Changed Date: 2018-12-04 13:39:41.000000002 +0100 ./.target-makefrag +Last Changed Rev: 4181 +Last Changed Date: 2018-12-06 15:21:07.000000002 +0100 ./RegisterDefs.h diff --git a/slsDetectorServers/gotthardDetectorServer/gitInfoGotthard.h b/slsDetectorServers/gotthardDetectorServer/gitInfoGotthard.h index de9cd8e04..a6bd594d3 100644 --- a/slsDetectorServers/gotthardDetectorServer/gitInfoGotthard.h +++ b/slsDetectorServers/gotthardDetectorServer/gitInfoGotthard.h @@ -1,6 +1,6 @@ #define GITURL "git@github.com:slsdetectorgroup/slsDetectorPackage.git" -#define GITREPUUID "98b5e893b180217e8ddddb95275b84d3b28a0832" +#define GITREPUUID "87f75630357249e5082b435a9ba852335881331f" #define GITAUTH "Dhanya_Thattil" -#define GITREV 0x4175 -#define GITDATE 0x20181204 +#define GITREV 0x4181 +#define GITDATE 0x20181206 #define GITBRANCH "refactor" diff --git a/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c index b7f49c219..a3caaa38f 100644 --- a/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c @@ -7,6 +7,7 @@ #ifndef VIRTUAL #include "AD9257.h" // commonServerFunctions.h, blackfin.h, ansi.h #include "AD9252.h" // old board compatibility +#include "LTC2620.h" // dacs #else #include "blackfin.h" #include @@ -372,15 +373,35 @@ void setupDetector() { // Initialization setPhaseShiftOnce(); - /* FIXME: (also check with commenting out #define GOTTHARDD in ad9257.h), check if need to replace with configureADC from firmwarE_funcs.c? */ + + // set defines if (getBoardRevision() == 1) - prepareADC9252(); + AD9252_SetDefines(ADC_SPI_REG, ADC_SPI_SRL_CS_OTPT_MSK, ADC_SPI_SRL_CLK_OTPT_MSK, ADC_SPI_SRL_DT_OTPT_MSK, ADC_SPI_SRL_DT_OTPT_OFST); else - prepareADC9257(); + AD9257_SetDefines(ADC_SPI_REG, ADC_SPI_SRL_CS_OTPT_MSK, ADC_SPI_SRL_CLK_OTPT_MSK, ADC_SPI_SRL_DT_OTPT_MSK, ADC_SPI_SRL_DT_OTPT_OFST); + LTC2620_SetDefines(SPI_REG, SPI_DAC_SRL_CS_OTPT_MSK, SPI_DAC_SRL_CLK_OTPT_MSK, SPI_DAC_SRL_DGTL_OTPT_MSK, SPI_DAC_SRL_DGTL_OTPT_OFST, NDAC, MAX_DAC_VOLTAGE_VALUE); + + // disable spi + if (getBoardRevision() == 1) + AD9252_Disable(); + else + AD9257_Disable(); + LTC2620_Disable(); + + bus_w(DAC_CNTRL_REG, DAC_CNTRL_SPI_IDLE_MSK); + bus_w(ADC_SPI_REG, ADC_SERIAL_CS_OUT_MSK); + bus_w(TEMP_SPI_IN_REG, TEMP_SPI_IDLE_MSK); + +#ifndef VIRTUAL + if (getBoardRevision() == 1) + AD9252_Configure(); + else + AD9257_Configure(); +#endif //configureADC(); setROIADC(-1); // set adcsyncreg, daqreg, chipofinterestreg, cleanfifos, setGbitReadout(); - initDac(0); /*FIXME: if it doesnt work, switch to the old dac*/ + LTC2620_Configure(); /*FIXME: if it doesnt work, switch to the old dac*/ // master, slave (25um) setMasterSlaveConfiguration(); @@ -404,16 +425,11 @@ int setDefaultDacs() { FILE_LOG(logINFOBLUE, ("Setting Default Dac values\n")); { int i = 0; - int retval[2]={-1,-1}; const int defaultvals[NDAC] = DEFAULT_DAC_VALS; for(i = 0; i < NDAC; ++i) { // if not already default, set it to default if (dacValues[i] != defaultvals[i]) { - setDAC((enum DACINDEX)i,defaultvals[i],0,retval); - if (retval[0] != defaultvals[i]) { - FILE_LOG(logERROR, ("Setting dac %d failed, wrote %d, read %d\n",i ,defaultvals[i], retval[0])); - ret = FAIL; - } + setDAC((enum DACINDEX)i, defaultvals[i], 0); } } } @@ -1023,9 +1039,9 @@ int setModule(sls_detector_module myMod, char* mess){ //set dac values { - int i = 0, retval[2] = {-1, -1}; + int i = 0; for(i = 0; i < NDAC; ++i) - setDAC((enum DACINDEX)i, myMod.dacs[i], 0, retval); + setDAC((enum DACINDEX)i, myMod.dacs[i], 0); } return OK; } @@ -1127,99 +1143,31 @@ enum detectorSettings getSettings(){ /* parameters - dac, adc, hv */ -void initDac(int dacnum) { // FIXME: if needed -#ifdef VIRTUAL - return; -#endif - FILE_LOG(logINFOBLUE, ("Initializing dac %d\n",dacnum)); - - u_int32_t codata; - int csdx = dacnum / NDAC + DAC_CNTRL_CS_OFST; // old board (16 dacs),so can be DAC_SERIAL_CS_OUT_OFST or +1 - int dacchannel = 0xf; // all channels - int dacvalue = 0x6; // can be any random value (just writing to power up) - FILE_LOG(logINFO, ("\tWrite to Input Register\n" - "\tChip select bit: %d\n" - "\tDac Channel: 0x%x\n" - "\tDac Value: 0x%x\n", - csdx, dacchannel, dacvalue)); - - codata = LTC2620_DAC_CMD_WRITE + // command to write to input register - ((dacchannel << LTC2620_DAC_ADDR_OFST) & LTC2620_DAC_ADDR_MSK) + // all channels - ((dacvalue << LTC2620_DAC_DATA_OFST) & LTC2620_DAC_DATA_MSK); // any random value - serializeToSPI(DAC_CNTRL_REG, codata, (0x1 << csdx), LTC2620_DAC_NUMBITS, - DAC_CNTRL_CLK_MSK, DAQ_CNTRL_DGTL_MSK, DAQ_CNTRL_DGTL_OFST); -} - -int voltageToDac(int value){ - int vmin = 0; - int vmax = 2500; - int nsteps = 4096; - if ((value < vmin) || (value > vmax)) { - FILE_LOG(logERROR, ("Voltage value (to convert to dac value) is outside bounds: %d\n", value)); - return -1; - } - return (int)(((value - vmin) / (vmax - vmin)) * (nsteps - 1) + 0.5); -} - -int dacToVoltage(unsigned int digital){ - int vmin = 0; - int vmax = 2500; - int nsteps = 4096; - int v = vmin + (vmax - vmin) * digital / (nsteps - 1); - if((v < 0) || (v > nsteps - 1)) { - FILE_LOG(logERROR, ("Voltage value (converted from dac value) is outside bounds: %d\n", v)); - return -1; - } - return v; -} - -void setDAC(enum DACINDEX ind, int val, int mV, int retval[]) { - int dacmV = val; - - //if set and mv, convert to dac - if (val > 0) { - if (mV) - val = voltageToDac(val); //gives -1 on error - else - dacmV = dacToVoltage(val); //gives -1 on error - } - - if ( (val >= 0) || (val == -100)) { +void setDAC(enum DACINDEX ind, int val, int mV) { + FILE_LOG(logDEBUG1, ("Setting dac[%d]: %d %s \n", (int)ind, val, (mV ? "mV" : "dac units"))); + int dacval = val; #ifdef VIRTUAL + if (mV && LTC2620_VoltageToDac(val, &dacval) == OK) dacValues[ind] = val; #else - u_int32_t codata; - int csdx = ind / NDAC + DAC_CNTRL_CS_MSK; // old board (16 dacs),so can be DAC_SERIAL_CS_OUT_OFST or +1 - int dacchannel = ind % NDAC; // 0-8, dac channel number (also for dacnum 9-15 in old board) - - FILE_LOG(logINFO, ("Setting DAC %d: %d dac (%d mV)\n",ind, val, dacmV)); - // command - if (val >= 0) { - FILE_LOG(logDEBUG1,("\tWrite to Input Register and Update\n")); - codata = LTC2620_DAC_CMD_SET; - - } else if (val == -100) { - FILE_LOG(logDEBUG1, ("\tPOWER DOWN\n")); - codata = LTC2620_DAC_CMD_POWER_DOWN; - } - // address - FILE_LOG(logDEBUG1, ("\tChip select bit:%d\n" - "\tDac Channel:0x%x\n" - "\tDac Value:0x%x\n", - csdx, dacchannel, val)); - codata += ((dacchannel << LTC2620_DAC_ADDR_OFST) & LTC2620_DAC_ADDR_MSK) + - ((val << LTC2620_DAC_DATA_OFST) & LTC2620_DAC_DATA_MSK); - // to spi - serializeToSPI(DAC_CNTRL_REG, codata, (0x1 << csdx), LTC2620_DAC_NUMBITS, - DAC_CNTRL_CLK_MSK, DAQ_CNTRL_DGTL_MSK, DAQ_CNTRL_DGTL_OFST); - - dacValues[ind] = val; + if (LTC2620_SetDACValue((int)ind, val, mV, &dacval) == OK) + dacValues[ind] = dacval; #endif - } +} - retval[0] = dacValues[ind]; - retval[1] = dacToVoltage(retval[0]); - FILE_LOG(logDEBUG1, ("Getting DAC %d : %d dac (%d mV)\n",ind, retval[0], retval[1])); +int getDAC(enum DACINDEX ind, int mV) { + if (!mV) { + FILE_LOG(logDEBUG1, ("Getting DAC %d : %d dac\n",ind, dacValues[ind])); + return dacValues[ind]; + } + int voltage = -1; + LTC2620_DacToVoltage(dacValues[ind], &voltage); + FILE_LOG(logDEBUG1, ("Getting DAC %d : %d dac (%d mV)\n",ind, dacValues[ind], voltage)); + return voltage; +} + +int getMAXDACUnits() { + return LTC2620_MAX_STEPS; } /* diff --git a/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_defs.h b/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_defs.h index d865ba806..52a68de64 100644 --- a/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_defs.h +++ b/slsDetectorServers/gotthardDetectorServer/slsDetectorServer_defs.h @@ -52,20 +52,6 @@ enum DACINDEX {VREF_DS, VCASCN_PB, VCASCP_PB, VOUT_CM, VCASC_OUT, VIN #define DEFAULT_TX_UDP_PORT (0xE185) #define MAX_DAC_VOLTAGE_VALUE (2500) -#define MAX_DAC_UNIT_VALUE (4096) - -/* LTC2620 DAC DEFINES *///FIXME: if neeeded -#define LTC2620_DAC_CMD_OFST (20) -#define LTC2620_DAC_CMD_MSK (0x0000000F << LTC2620_DAC_CMD_OFST) -#define LTC2620_DAC_ADDR_OFST (16) -#define LTC2620_DAC_ADDR_MSK (0x0000000F << LTC2620_DAC_ADDR_OFST) -#define LTC2620_DAC_DATA_OFST (4) -#define LTC2620_DAC_DATA_MSK (0x00000FFF << LTC2620_DAC_DATA_OFST) - -#define LTC2620_DAC_CMD_WRITE (0x00000000 << LTC2620_DAC_CMD_OFST) -#define LTC2620_DAC_CMD_SET (0x00000003 << LTC2620_DAC_CMD_OFST) -#define LTC2620_DAC_CMD_POWER_DOWN (0x00000004 << LTC2620_DAC_CMD_OFST) -#define LTC2620_DAC_NUMBITS (24) /** ENEt conf structs */ typedef struct mac_header_struct{ diff --git a/slsDetectorServers/jungfrauDetectorServer/LTC2620.h b/slsDetectorServers/jungfrauDetectorServer/LTC2620.h new file mode 120000 index 000000000..13157cb8b --- /dev/null +++ b/slsDetectorServers/jungfrauDetectorServer/LTC2620.h @@ -0,0 +1 @@ +../slsDetectorServer/LTC2620.h \ No newline at end of file diff --git a/slsDetectorServers/jungfrauDetectorServer/RegisterDefs.h b/slsDetectorServers/jungfrauDetectorServer/RegisterDefs.h index db2abf914..1ac6c2139 100644 --- a/slsDetectorServers/jungfrauDetectorServer/RegisterDefs.h +++ b/slsDetectorServers/jungfrauDetectorServer/RegisterDefs.h @@ -110,29 +110,28 @@ /* SPI (Serial Peripheral Interface) Register */ #define SPI_REG (0x40 << MEM_MAP_SHIFT) -#define DAC_SERIAL_DIGITAL_OUT_OFST (0) -#define DAC_SERIAL_DIGITAL_OUT_MSK (0x00000001 << DAC_SERIAL_DIGITAL_OUT_OFST) -#define DAC_SERIAL_CLK_OUT_OFST (1) -#define DAC_SERIAL_CLK_OUT_MSK (0x00000001 << DAC_SERIAL_CLK_OUT_OFST) -#define DAC_SERIAL_CS_OUT_OFST (2) -#define DAC_SERIAL_CS_OUT_MSK (0x00000001 << DAC_SERIAL_CS_OUT_OFST) -#define HV_SERIAL_DIGITAL_OUT_OFST (8) -#define HV_SERIAL_DIGITAL_OUT_MSK (0x00000001 << HV_SERIAL_DIGITAL_OUT_OFST) -#define HV_SERIAL_CLK_OUT_OFST (9) -#define HV_SERIAL_CLK_OUT_MSK (0x00000001 << HV_SERIAL_CLK_OUT_OFST) -#define HV_SERIAL_CS_OUT_OFST (10) -#define HV_SERIAL_CS_OUT_MSK (0x00000001 << HV_SERIAL_CS_OUT_OFST) - +#define SPI_DAC_SRL_DGTL_OTPT_OFST (0) +#define SPI_DAC_SRL_DGTL_OTPT_MSK (0x00000001 << SPI_DAC_SRL_DGTL_OTPT_OFST) +#define SPI_DAC_SRL_CLK_OTPT_OFST (1) +#define SPI_DAC_SRL_CLK_OTPT_MSK (0x00000001 << SPI_DAC_SRL_CLK_OTPT_OFST) +#define SPI_DAC_SRL_CS_OTPT_OFST (2) +#define SPI_DAC_SRL_CS_OTPT_MSK (0x00000001 << SPI_DAC_SRL_CS_OTPT_OFST) +#define SPI_HV_SRL_DGTL_OTPT_OFST (8) +#define SPI_HV_SRL_DGTL_OTPT_MSK (0x00000001 << SPI_HV_SRL_DGTL_OTPT_OFST) +#define SPI_HV_SRL_CLK_OTPT_OFST (9) +#define SPI_HV_SRL_CLK_OTPT_MSK (0x00000001 << SPI_HV_SRL_CLK_OTPT_OFST) +#define SPI_HV_SRL_CS_OTPT_OFST (10) +#define SPI_HV_SRL_CS_OTPT_MSK (0x00000001 << SPI_HV_SRL_CS_OTPT_OFST) /* ADC SPI (Serial Peripheral Interface) Register */ #define ADC_SPI_REG (0x41 << MEM_MAP_SHIFT) -#define ADC_SERIAL_CLK_OUT_OFST (0) -#define ADC_SERIAL_CLK_OUT_MSK (0x00000001 << ADC_SERIAL_CLK_OUT_OFST) -#define ADC_SERIAL_DATA_OUT_OFST (1) -#define ADC_SERIAL_DATA_OUT_MSK (0x00000001 << ADC_SERIAL_DATA_OUT_OFST) -#define ADC_SERIAL_CS_OUT_OFST (2) -#define ADC_SERIAL_CS_OUT_MSK (0x0000000F << ADC_SERIAL_CS_OUT_OFST) +#define ADC_SPI_SRL_CLK_OTPT_OFST (0) +#define ADC_SPI_SRL_CLK_OTPT_MSK (0x00000001 << ADC_SPI_SRL_CLK_OTPT_OFST) +#define ADC_SPI_SRL_DT_OTPT_OFST (1) +#define ADC_SPI_SRL_DT_OTPT_MSK (0x00000001 << ADC_SPI_SRL_DT_OTPT_OFST) +#define ADC_SPI_SRL_CS_OTPT_OFST (2) +#define ADC_SPI_SRL_CS_OTPT_MSK (0x0000000F << ADC_SPI_SRL_CS_OTPT_OFST) /* ADC offset Register */ #define ADC_OFST_REG (0x42 << MEM_MAP_SHIFT) diff --git a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c index 28d5efb1c..b1eb9061d 100644 --- a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c @@ -5,6 +5,8 @@ #ifndef VIRTUAL #include "AD9257.h" // commonServerFunctions.h, blackfin.h, ansi.h +#include "LTC2620.h" // dacs +#include "MAX1932.h" // hv #include "programfpga.h" #else #include "blackfin.h" @@ -394,12 +396,22 @@ void setupDetector() { resetCore(); resetPeripheral(); cleanFifos(); + + // set defines + AD9257_SetDefines(ADC_SPI_REG, ADC_SPI_SRL_CS_OTPT_MSK, ADC_SPI_SRL_CLK_OTPT_MSK, ADC_SPI_SRL_DT_OTPT_MSK, ADC_SPI_SRL_DT_OTPT_OFST); + LTC2620_SetDefines(SPI_REG, SPI_DAC_SRL_CS_OTPT_MSK, SPI_DAC_SRL_CLK_OTPT_MSK, SPI_DAC_SRL_DGTL_OTPT_MSK, SPI_DAC_SRL_DGTL_OTPT_OFST, NDAC, MAX_DAC_VOLTAGE_VALUE); + MAX1932_SetDefines(SPI_REG, SPI_HV_SRL_CS_OTPT_MSK, SPI_HV_SRL_CLK_OTPT_MSK, SPI_HV_SRL_DGTL_OTPT_MSK, SPI_HV_SRL_DGTL_OTPT_OFST); + + // disable spi + AD9257_Disable(); + LTC2620_Disable(); + MAX1932_Disable(); + #ifndef VIRTUAL - prepareADC9257(); + AD9257_Configure(); #endif // initialize dac series - initDac(0); /* todo might work without */ - initDac(8); //only for old board compatibility + LTC2620_Configure(); //set dacs setDefaultDacs(); @@ -443,16 +455,11 @@ int setDefaultDacs() { FILE_LOG(logINFOBLUE, ("Setting Default Dac values\n")); { int i = 0; - int retval[2]={-1,-1}; const int defaultvals[NDAC] = DEFAULT_DAC_VALS; for(i = 0; i < NDAC; ++i) { // if not already default, set it to default if (dacValues[i] != defaultvals[i]) { - setDAC((enum DACINDEX)i,defaultvals[i],0,retval); - if (retval[0] != defaultvals[i]) { - FILE_LOG(logERROR, ("Setting dac %d failed, wrote %d, read %d\n",i ,defaultvals[i], retval[0])); - ret = FAIL; - } + setDAC((enum DACINDEX)i,defaultvals[i],0); } } } @@ -710,9 +717,9 @@ int setModule(sls_detector_module myMod, char* mess){ //set dac values { - int i = 0, retval[2] = {-1, -1}; + int i = 0; for(i = 0; i < NDAC; ++i) - setDAC((enum DACINDEX)i, myMod.dacs[i], 0, retval); + setDAC((enum DACINDEX)i, myMod.dacs[i], 0); } return OK; } @@ -830,122 +837,37 @@ enum detectorSettings getSettings(){ /* parameters - dac, adc, hv */ - - - - - - -void initDac(int dacnum) { +void setDAC(enum DACINDEX ind, int val, int mV) { + FILE_LOG(logDEBUG1, ("Setting dac[%d]: %d %s \n", (int)ind, val, (mV ? "mV" : "dac units"))); + int dacval = val; #ifdef VIRTUAL - return; -#endif - FILE_LOG(logINFOBLUE, ("Initializing dac %d\n",dacnum)); - - u_int32_t codata; - int csdx = dacnum / NDAC + DAC_SERIAL_CS_OUT_OFST; // old board (16 dacs),so can be DAC_SERIAL_CS_OUT_OFST or +1 - int dacchannel = 0xf; // all channels - int dacvalue = 0x6; // can be any random value (just writing to power up) - FILE_LOG(logINFO, ("\tWrite to Input Register\n" - "\tChip select bit: %d\n" - "\tDac Channel: 0x%x\n" - "\tDac Value: 0x%x\n", - csdx, dacchannel, dacvalue)); - - codata = LTC2620_DAC_CMD_WRITE + // command to write to input register - ((dacchannel << LTC2620_DAC_ADDR_OFST) & LTC2620_DAC_ADDR_MSK) + // all channels - ((dacvalue << LTC2620_DAC_DATA_OFST) & LTC2620_DAC_DATA_MSK); // any random value - serializeToSPI(SPI_REG, codata, (0x1 << csdx), LTC2620_DAC_NUMBITS, - DAC_SERIAL_CLK_OUT_MSK, DAC_SERIAL_DIGITAL_OUT_MSK, DAC_SERIAL_DIGITAL_OUT_OFST); -} - - - - - - - - - -int voltageToDac(int value){ - int vmin = 0; - int vmax = 2500; - int nsteps = 4096; - if ((value < vmin) || (value > vmax)) { - FILE_LOG(logERROR, ("Voltage value (to convert to dac value) is outside bounds: %d\n", value)); - return -1; - } - return (int)(((value - vmin) / (vmax - vmin)) * (nsteps - 1) + 0.5); -} - -int dacToVoltage(unsigned int digital){ - int vmin = 0; - int vmax = 2500; - int nsteps = 4096; - int v = vmin + (vmax - vmin) * digital / (nsteps - 1); - if((v < 0) || (v > nsteps - 1)) { - FILE_LOG(logERROR, ("Voltage value (converted from dac value) is outside bounds: %d\n", v)); - return -1; - } - return v; -} - - - -void setDAC(enum DACINDEX ind, int val, int mV, int retval[]){ - int dacmV = val; - - //if set and mv, convert to dac - if (val > 0) { - if (mV) - val = voltageToDac(val); //gives -1 on error - else - dacmV = dacToVoltage(val); //gives -1 on error - } - - if ( (val >= 0) || (val == -100)) { -#ifdef VIRTUAL - dacValues[ind] = val; + if (mV && LTC2620_VoltageToDac(val, &dacval) == OK) + dacValues[ind] = val; #else - u_int32_t codata; - int csdx = ind / NDAC + DAC_SERIAL_CS_OUT_OFST; // old board (16 dacs),so can be DAC_SERIAL_CS_OUT_OFST or +1 - int dacchannel = ind % NDAC; // 0-8, dac channel number (also for dacnum 9-15 in old board) - - FILE_LOG(logINFO, ("Setting DAC %d: %d dac (%d mV)\n",ind, val, dacmV)); - // command - if (val >= 0) { - FILE_LOG(logDEBUG1,("\tWrite to Input Register and Update\n")); - codata = LTC2620_DAC_CMD_SET; - - } else if (val == -100) { - FILE_LOG(logDEBUG1, ("\tPOWER DOWN\n")); - codata = LTC2620_DAC_CMD_POWER_DOWN; - } - // address - FILE_LOG(logDEBUG1, ("\tChip select bit:%d\n" - "\tDac Channel:0x%x\n" - "\tDac Value:0x%x\n", - csdx, dacchannel, val)); - codata += ((dacchannel << LTC2620_DAC_ADDR_OFST) & LTC2620_DAC_ADDR_MSK) + - ((val << LTC2620_DAC_DATA_OFST) & LTC2620_DAC_DATA_MSK); - // to spi - serializeToSPI(SPI_REG, codata, (0x1 << csdx), LTC2620_DAC_NUMBITS, - DAC_SERIAL_CLK_OUT_MSK, DAC_SERIAL_DIGITAL_OUT_MSK, DAC_SERIAL_DIGITAL_OUT_OFST); - - dacValues[ind] = val; - - if (ind == VREF_COMP) { - bus_w (VREF_COMP_MOD_REG, (bus_r(VREF_COMP_MOD_REG) &~ (VREF_COMP_MOD_MSK)) // reset - | ((val << VREF_COMP_MOD_OFST) & VREF_COMP_MOD_MSK)); // or it with value - } + if (LTC2620_SetDACValue((int)ind, val, mV, &dacval) == OK) { + dacValues[ind] = dacval; + if (ind == VREF_COMP && (val >= 0)) {//FIXME: if val == pwr down value, write 0? + bus_w (VREF_COMP_MOD_REG, (bus_r(VREF_COMP_MOD_REG) &~ (VREF_COMP_MOD_MSK)) // reset + | ((val << VREF_COMP_MOD_OFST) & VREF_COMP_MOD_MSK)); // or it with value + } + } #endif - } - - retval[0] = dacValues[ind]; - retval[1] = dacToVoltage(retval[0]); - FILE_LOG(logDEBUG1, ("Getting DAC %d : %d dac (%d mV)\n",ind, retval[0], retval[1])); } +int getDAC(enum DACINDEX ind, int mV) { + if (!mV) { + FILE_LOG(logDEBUG1, ("Getting DAC %d : %d dac\n",ind, dacValues[ind])); + return dacValues[ind]; + } + int voltage = -1; + LTC2620_DacToVoltage(dacValues[ind], &voltage); + FILE_LOG(logDEBUG1, ("Getting DAC %d : %d dac (%d mV)\n",ind, dacValues[ind], voltage)); + return voltage; +} + +int getMAXDACUnits() { + return LTC2620_MAX_STEPS; +} int getADC(enum ADCINDEX ind){ #ifdef VIRTUAL diff --git a/slsDetectorServers/jungfrauDetectorServer/slsDetectorServer_defs.h b/slsDetectorServers/jungfrauDetectorServer/slsDetectorServer_defs.h index c95430da9..549e434ed 100644 --- a/slsDetectorServers/jungfrauDetectorServer/slsDetectorServer_defs.h +++ b/slsDetectorServers/jungfrauDetectorServer/slsDetectorServer_defs.h @@ -67,7 +67,6 @@ enum NETWORKINDEX { TXN_FRAME }; #define DEFAULT_STRG_CLL_STRT (0xf) #define MAX_DAC_VOLTAGE_VALUE (2500) -#define MAX_DAC_UNIT_VALUE (4096) /* Defines in the Firmware */ #define FIX_PATT_VAL (0xACDC2014) @@ -92,24 +91,6 @@ enum NETWORKINDEX { TXN_FRAME }; #define LSB_OF_64_BIT_REG_OFST (0) #define BIT_32_MSK (0xFFFFFFFF) -/* LTC2620 DAC DEFINES */ -#define LTC2620_DAC_CMD_OFST (20) -#define LTC2620_DAC_CMD_MSK (0x0000000F << LTC2620_DAC_CMD_OFST) -#define LTC2620_DAC_ADDR_OFST (16) -#define LTC2620_DAC_ADDR_MSK (0x0000000F << LTC2620_DAC_ADDR_OFST) -#define LTC2620_DAC_DATA_OFST (4) -#define LTC2620_DAC_DATA_MSK (0x00000FFF << LTC2620_DAC_DATA_OFST) - -#define LTC2620_DAC_CMD_WRITE (0x00000000 << LTC2620_DAC_CMD_OFST) -#define LTC2620_DAC_CMD_SET (0x00000003 << LTC2620_DAC_CMD_OFST) -#define LTC2620_DAC_CMD_POWER_DOWN (0x00000004 << LTC2620_DAC_CMD_OFST) -#define LTC2620_DAC_NUMBITS (24) - -/* MAX1932 HV DEFINES */ -#define MAX1932_HV_NUMBITS (8) -#define MAX1932_HV_DATA_OFST (0) -#define MAX1932_HV_DATA_MSK (0x000000FF << MAX1932_HV_DATA_OFST) - /** PLL Reconfiguration Registers */ //https://www.altera.com/documentation/mcn1424769382940.html #define PLL_MODE_REG (0x00) diff --git a/slsDetectorServers/slsDetectorServer/AD7689.h b/slsDetectorServers/slsDetectorServer/AD7689.h index d9b3b0e9c..f5fdde65f 100755 --- a/slsDetectorServers/slsDetectorServer/AD7689.h +++ b/slsDetectorServers/slsDetectorServer/AD7689.h @@ -1,8 +1,6 @@ #pragma once -//#include "commonServerFunctions.h" // blackfin.h, ansi.h - - +#include "commonServerFunctions.h" // blackfin.h, ansi.h /* AD7689 ADC DEFINES */ @@ -60,22 +58,161 @@ #define AD7689_CFG_CFG_NO_UPDATE_VAL ((0x0 << AD7689_CFG_CFG_OFST) & AD7689_CFG_CFG_MSK) #define AD7689_CFG_CFG_OVRWRTE_VAL ((0x1 << AD7689_CFG_CFG_OFST) & AD7689_CFG_CFG_MSK) -int getAD7689(int ind) { +#define AD7689_ADC_CFG_NUMBITS (14) +#define AD7689_ADC_DATA_NUMBITS (16) +#define AD7689_NUM_CHANNELS (7) +#define AD7689_NUM_INVALID_CONVERSIONS (3) +#define AD7689_INT_REF_MAX_MV (2500) // chosen using reference buffer selection in config reg +#define AD7689_INT_REF_MIN_MV (0) +#define AD7689_TMP_C_FOR_1_MV (25.00 / 283) + + +uint32_t AD7689_Reg = 0x0; +uint32_t AD7689_ROReg = 0x0; +uint32_t AD7689_CnvMask = 0x0; +uint32_t AD7689_ClkMask = 0x0; +uint32_t AD7689_DigMask = 0x0; +int AD7689_DigOffset = 0x0; + +/** + * Set Defines + * @param reg spi register + * @param roreg spi readout register + * @param cmsk conversion mask + * @param clkmsk clock output mask + * @param dmsk digital output mask + * @param dofst digital output offset + */ +void AD7689_SetDefines(uint32_t reg, uint32_t roreg, uint32_t cmsk, uint32_t clkmsk, uint32_t dmsk, int dofst) { + AD7689_Reg = reg; + AD7689_ROReg = roreg; + AD7689_CnvMask = cmsk; + AD7689_ClkMask = clkmsk; + AD7689_DigMask = dmsk; + AD7689_DigOffset = dofst; +} + +/** + * Disable SPI + */ +void AD7689_Disable() { + bus_w(AD7689_Reg, bus_r(AD7689_Reg) + | AD7689_CnvMask + | AD7689_ClkMask + &~(AD7689_DigMask)); +} + +/** + * Set SPI reg value + * @param codata value to be set + */ +void AD7689_Set(u_int32_t codata) { + FILE_LOG(logINFO, ("\tSetting ADC SPI Register. Writing 0x%08x to Config Reg\n", codata)); + serializeToSPI(AD7689_Reg, codata, AD7689_CnvMask, AD7689_ADC_CFG_NUMBITS, + AD7689_ClkMask, AD7689_DigMask, AD7689_DigOffset); +} + +/** + * Get SPI reg value + * @returns SPI reg value + */ +uint16_t AD7689_Get() { + FILE_LOG(logINFO, ("\tGetting ADC SPI Register.\n")); + return (uint16_t)serializeFromSPI(AD7689_ROReg, AD7689_Reg, AD7689_CnvMask, AD7689_ADC_DATA_NUMBITS, + AD7689_ClkMask, AD7689_DigMask); +} + +/** + * Get temperature + * @returns temperature in °C + */ +int AD7689_GetTemperature() { + AD7689_Set( + // read back + AD7689_CFG_RB_MSK | + // disable sequencer (different from config) + AD7689_CFG_SEQ_DSBLE_VAL | + // Internal reference. REF = 2.5V buffered output. Temperature sensor enabled. + AD7689_CFG_REF_INT_2500MV_VAL | + // full bandwidth of low pass filter + AD7689_CFG_BW_FULL_VAL | + // all channel (different from config) + AD7689_CFG_IN_MSK | + // temperature sensor (different from config) + AD7689_CFG_INCC_TMP_VAL | + // overwrite configuration + AD7689_CFG_CFG_OVRWRTE_VAL); + + // FIXME: do we have to read it 8 times?? (sequencer is disabled anyway) or are we sequencing, then we read only last channel + uint16_t regval = AD7689_Get(); + + // value in mV FIXME: page 17? reference voltage temperature coefficient or t do with -40 to 85 °C + uint16_t vmin = AD7689_INT_REF_MIN_MV; + uint16_t vmax = AD7689_INT_REF_MAX_MV; + uint16_t nsteps = (2 ^ AD7689_ADC_DATA_NUMBITS); + uint16_t retval = (vmin + (vmax - vmin) * regval / (nsteps - 1)); + FILE_LOG(logDEBUG1, ("\tvoltage read for temp: 0x%d mV\n", retval)); + + // value in °C + uint16_t temp = AD7689_TMP_C_FOR_1_MV * retval; + FILE_LOG(logDEBUG1, ("\ttemp read: 0x%d °C\n", temp)); + + return temp; } -void setAD7689(int addr, int val) { - u_int32_t codata; - codata = val + (addr << 8); - FILE_LOG(logINFO, ("\tSetting ADC SPI Register. Wrote 0x%04x at 0x%04x\n", val, addr)); - serializeToSPI(ADC_SPI_REG, codata, ADC_SERIAL_CS_OUT_MSK, AD9257_ADC_NUMBITS, - ADC_SERIAL_CLK_OUT_MSK, ADC_SERIAL_DATA_OUT_MSK, ADC_SERIAL_DATA_OUT_OFST); +/** + * Reads channels voltage + * @param ichan channel number from 0 to 7 + * @returns channel voltage in mV + */ +int AD7689_GetChannel(int ichan) { + // filter channels val + if (ichan < 0 || ichan >= AD7689_NUM_CHANNELS) { + FILE_LOG(logERROR, ("Cannot get slow adc channel. " + "%d out of bounds (0 to %d)\n", ichan, AD7689_NUM_CHANNELS - 1)); + return -1; + } + + AD7689_Set( + // read back + AD7689_CFG_RB_MSK | + // disable sequencer (different from config) + AD7689_CFG_SEQ_DSBLE_VAL | + // Internal reference. REF = 2.5V buffered output. Temperature sensor enabled. + AD7689_CFG_REF_INT_2500MV_VAL | + // full bandwidth of low pass filter + AD7689_CFG_BW_FULL_VAL | + // specific channel (different from config) + ((ichan << AD7689_CFG_IN_OFST) & AD7689_CFG_IN_MSK) | + // input channel configuration (unipolar. inx to gnd) + AD7689_CFG_INCC_UNPLR_IN_GND_VAL | + // overwrite configuration + AD7689_CFG_CFG_OVRWRTE_VAL); + + // FIXME: do we have to read it 8 times?? (sequencer is disabled anyway) or are we sequencing, then we read only last channel + uint16_t regval = AD7689_Get(); + + // value in mV + uint16_t vmin = AD7689_INT_REF_MIN_MV; + uint16_t vmax = AD7689_INT_REF_MAX_MV; + uint16_t nsteps = (2 ^ AD7689_ADC_DATA_NUMBITS); + uint16_t retval = (vmin + (vmax - vmin) * regval / (nsteps - 1)); + FILE_LOG(logINFO, ("\tvoltage read for chan %d: 0x%d mV\n", retval)); + + return retval; } -void prepareAD7689(){ - FILE_LOG(logINFOBLUE, ("Preparing AD7689 (Slow ADCs):\n")); +/** + * Configure + */ +void AD7689_Configure(){ + FILE_LOG(logINFOBLUE, ("Configuring AD7689 (Slow ADCs):\n")); - uint16_t codata = ( + // from power up, 3 invalid conversions + int i = 0; + for (i = 0; i < AD7689_NUM_INVALID_CONVERSIONS; ++i) { + AD7689_Set( // read back AD7689_CFG_RB_MSK | // scan sequence IN0-IN7 then temperature sensor @@ -90,5 +227,5 @@ void prepareAD7689(){ AD7689_CFG_INCC_UNPLR_IN_GND_VAL | // overwrite configuration AD7689_CFG_CFG_OVRWRTE_VAL); - + } } diff --git a/slsDetectorServers/slsDetectorServer/AD9252.h b/slsDetectorServers/slsDetectorServer/AD9252.h index ddb0c4518..7e540e5d5 100755 --- a/slsDetectorServers/slsDetectorServer/AD9252.h +++ b/slsDetectorServers/slsDetectorServer/AD9252.h @@ -100,63 +100,101 @@ #define AD9252_OUT_CLK_600_VAL ((0xa << AD9252_OUT_CLK_OFST) & AD9252_OUT_CLK_MSK) #define AD9252_OUT_CLK_660_VAL ((0xb << AD9252_OUT_CLK_OFST) & AD9252_OUT_CLK_MSK) // 0xb - 0xf is 660 +uint32_t AD9252_Reg = 0x0; +uint32_t AD9252_CsMask = 0x0; +uint32_t AD9252_ClkMask = 0x0; +uint32_t AD9252_DigMask = 0x0; +int AD9252_DigOffset = 0x0; - -void setAdc9252(int addr, int val) { - - u_int32_t codata; - codata = val + (addr << 8); - FILE_LOG(logINFO, ("\tSetting ADC SPI Register. Wrote 0x%04x at 0x%04x\n", val, addr)); - serializeToSPI(ADC_SPI_REG, codata, ADC_SERIAL_CS_OUT_MSK, AD9252_ADC_NUMBITS, - ADC_SERIAL_CLK_OUT_MSK, ADC_SERIAL_DATA_OUT_MSK, ADC_SERIAL_DATA_OUT_OFST); +/** + * Set Defines + * @param reg spi register + * @param cmsk chip select mask + * @param clkmsk clock output mask + * @param dmsk digital output mask + * @param dofst digital output offset + */ +void AD9252_SetDefines(uint32_t reg, uint32_t cmsk, uint32_t clkmsk, uint32_t dmsk, int dofst) { + AD9252_Reg = reg; + AD9252_CsMask = cmsk; + AD9252_ClkMask = clkmsk; + AD9252_DigMask = dmsk; + AD9252_DigOffset = dofst; } -void prepareADC9252(){ - FILE_LOG(logINFOBLUE, ("Preparing ADC9252:\n")); +/** + * Disable SPI + */ +void AD9252_Disable() { + bus_w(AD9252_REG, bus_r(AD9252_REG) + | AD9252_CS_MSK + | AD9252_CLK_MSK + &~(AD9252_DT_MSK)); +} - //power mode reset +/** + * Set SPI reg value + * @param codata value to be set + */ +void AD9252_Set(int addr, int val) { + + u_int32_t codata; + codata = val + (addr << 8); + FILE_LOG(logINFO, ("\tSetting ADC SPI Register. Wrote 0x%04x at 0x%04x\n", val, addr)); + serializeToSPI(AD9252_REG, codata, AD9252_CS_MSK, AD9252_ADC_NUMBITS, + AD9252_CLK_MSK, AD9252_DT_MSK, AD9252_DT_OFST); +} + +/** + * Configure + */ +void AD9252_Configure(){ + FILE_LOG(logINFOBLUE, ("Configuring ADC9252:\n")); + + //power mode reset FILE_LOG(logINFO, ("\tPower mode reset\n")); - setAdc9252(AD9252_POWER_MODE_REG, AD9252_INT_RESET_VAL); + AD9252_Set(AD9252_POWER_MODE_REG, AD9252_INT_RESET_VAL); /*#ifdef GOTTHARDD //FIXME:? - usleep(50000); + usleep(50000); #endif*/ - //power mode chip run - FILE_LOG(logINFO, ("\tPower mode chip run\n")); - setAdc9252(AD9252_POWER_MODE_REG, AD9252_INT_CHIP_RUN_VAL); + //power mode chip run + FILE_LOG(logINFO, ("\tPower mode chip run\n")); + AD9252_Set(AD9252_POWER_MODE_REG, AD9252_INT_CHIP_RUN_VAL); #ifdef GOTTHARDD - /*usleep(50000);*///FIXME:? + /*usleep(50000);*///FIXME:? - // binary offset + // binary offset FILE_LOG(logINFO, ("\tBinary offset\n")); - setAdc9252(AD9252_OUT_MODE_REG, AD9252_OUT_BINARY_OFST_VAL); + AD9252_Set(AD9252_OUT_MODE_REG, AD9252_OUT_BINARY_OFST_VAL); /*usleep(50000);*///FIXME:? #endif - //output clock phase - FILE_LOG(logINFO, ("\tOutput clock phase\n")); - setAdc9252(AD9252_OUT_PHASE_REG, AD9252_OUT_CLK_60_VAL); + //output clock phase + FILE_LOG(logINFO, ("\tOutput clock phase\n")); + AD9252_Set(AD9252_OUT_PHASE_REG, AD9252_OUT_CLK_60_VAL); - // lvds-iee reduced , binary offset - FILE_LOG(logINFO, ("\tLvds-iee reduced, binary offset\n")); - setAdc9252(AD9252_OUT_MODE_REG, AD9252_OUT_LVDS_IEEE_VAL); + // lvds-iee reduced , binary offset + FILE_LOG(logINFO, ("\tLvds-iee reduced, binary offset\n")); + AD9252_Set(AD9252_OUT_MODE_REG, AD9252_OUT_LVDS_IEEE_VAL); - // all devices on chip to receive next command - FILE_LOG(logINFO, ("\tAll devices on chip to receive next command\n")); - setAdc9252(AD9252_DEV_IND_2_REG, - AD9252_CHAN_H_MSK | AD9252_CHAN_G_MSK | AD9252_CHAN_F_MSK | AD9252_CHAN_E_MSK); - setAdc9252(AD9252_DEV_IND_1_REG, - AD9252_CHAN_D_MSK | AD9252_CHAN_C_MSK | AD9252_CHAN_B_MSK | AD9252_CHAN_A_MSK | - AD9252_CLK_CH_DCO_MSK | AD9252_CLK_CH_IFCO_MSK); // unlike 9257, by default ad9252 has this (dco and ifco)off + // all devices on chip to receive next command + FILE_LOG(logINFO, ("\tAll devices on chip to receive next command\n")); + AD9252_Set(AD9252_DEV_IND_2_REG, + AD9252_CHAN_H_MSK | AD9252_CHAN_G_MSK | AD9252_CHAN_F_MSK | AD9252_CHAN_E_MSK); + AD9252_Set(AD9252_DEV_IND_1_REG, + AD9252_CHAN_D_MSK | AD9252_CHAN_C_MSK | AD9252_CHAN_B_MSK | AD9252_CHAN_A_MSK | + AD9252_CLK_CH_DCO_MSK | AD9252_CLK_CH_IFCO_MSK); // unlike 9257, by default ad9252 has this (dco and ifco)off - // no test mode - FILE_LOG(logINFO, ("\tNo test mode\n")); - setAdc9252(AD9252_TEST_MODE_REG, AD9252_TST_OFF_VAL); + // no test mode + FILE_LOG(logINFO, ("\tNo test mode\n")); + AD9252_Set(AD9252_TEST_MODE_REG, AD9252_TST_OFF_VAL); #ifdef TESTADC - FILE_LOG(logINFOBLUE, ("Putting ADC in Test Mode!\n"); - // mixed bit frequency test mode - FILE_LOG(logINFO, ("\tMixed bit frequency test mode\n")); - setAdc9252(AD9252_TEST_MODE_REG, AD9252_TST_MXD_BT_FRQ_VAL); + FILE_LOG(logINFOBLUE, ("Putting ADC in Test Mode!\n"); + // mixed bit frequency test mode + FILE_LOG(logINFO, ("\tMixed bit frequency test mode\n")); + AD9252_Set(AD9252_TEST_MODE_REG, AD9252_TST_MXD_BT_FRQ_VAL); #endif } + diff --git a/slsDetectorServers/slsDetectorServer/AD9257.h b/slsDetectorServers/slsDetectorServer/AD9257.h index c55b4a8f3..1853f8499 100755 --- a/slsDetectorServers/slsDetectorServer/AD9257.h +++ b/slsDetectorServers/slsDetectorServer/AD9257.h @@ -125,72 +125,119 @@ #define AD9257_VREF_1_6_VAL ((0x3 << AD9257_VREF_OFST) & AD9257_VREF_MSK) #define AD9257_VREF_2_0_VAL ((0x4 << AD9257_VREF_OFST) & AD9257_VREF_MSK) -int getMaxValidVref() { +uint32_t AD9257_Reg = 0x0; +uint32_t AD9257_CsMask = 0x0; +uint32_t AD9257_ClkMask = 0x0; +uint32_t AD9257_DigMask = 0x0; +int AD9257_DigOffset = 0x0; + +/** + * Set Defines + * @param reg spi register + * @param cmsk chip select mask + * @param clkmsk clock output mask + * @param dmsk digital output mask + * @param dofst digital output offset + */ +void AD9257_SetDefines(uint32_t reg, uint32_t cmsk, uint32_t clkmsk, uint32_t dmsk, int dofst) { + AD9257_Reg = reg; + AD9257_CsMask = cmsk; + AD9257_ClkMask = clkmsk; + AD9257_DigMask = dmsk; + AD9257_DigOffset = dofst; +} + +/** + * Disable SPI + */ +void AD9257_Disable() { + bus_w(AD9257_Reg, bus_r(AD9257_Reg) + | AD9257_CsMask + | AD9257_ClkMask + &~(AD9257_DigMask)); +} + +/** + * Get max valid vref value + * @param get max vref voltage unit (4 for 2.0V) + */ +int AD9257_GetMaxValidVref() { return 0x4; } -void setVrefVoltage(int val) { - setAdc9257(AD9257_VREF_REG, val); +/** + * Set vref voltage + * @param val voltage to be set (0 for 1.0V, 1 for 1.14V, 2 for 1.33V, 3 for 1.6V, 4 for 2.0V + */ +void AD9257_SetVrefVoltage(int val) { + AD9257_Set(AD9257_VREF_REG, val); } -void setAdc9257(int addr, int val) { +/** + * Set SPI reg value + * @param codata value to be set + */ +void AD9257_Set(int addr, int val) { u_int32_t codata; codata = val + (addr << 8); FILE_LOG(logINFO, ("\tSetting ADC SPI Register. Wrote 0x%04x at 0x%04x\n", val, addr)); - serializeToSPI(ADC_SPI_REG, codata, ADC_SERIAL_CS_OUT_MSK, AD9257_ADC_NUMBITS, - ADC_SERIAL_CLK_OUT_MSK, ADC_SERIAL_DATA_OUT_MSK, ADC_SERIAL_DATA_OUT_OFST); + serializeToSPI(AD9257_Reg, codata, AD9257_CsMask, AD9257_ADC_NUMBITS, + AD9257_ClkMask, AD9257_DigMask, AD9257_DigOffset); } -void prepareADC9257(){ - FILE_LOG(logINFOBLUE, ("Preparing ADC9257:\n")); +/** + * Configure + */ +void AD9257_Configure(){ + FILE_LOG(logINFOBLUE, ("Configuring ADC9257:\n")); //power mode reset FILE_LOG(logINFO, ("\tPower mode reset\n")); - setAdc9257(AD9257_POWER_MODE_REG, AD9257_INT_RESET_VAL); + AD9257_Set(AD9257_POWER_MODE_REG, AD9257_INT_RESET_VAL); //power mode chip run FILE_LOG(logINFO, ("\tPower mode chip run\n")); - setAdc9257(AD9257_POWER_MODE_REG, AD9257_INT_CHIP_RUN_VAL); + AD9257_Set(AD9257_POWER_MODE_REG, AD9257_INT_CHIP_RUN_VAL); // binary offset FILE_LOG(logINFO, ("\tBinary offset\n")); - setAdc9257(AD9257_OUT_MODE_REG, AD9257_OUT_BINARY_OFST_VAL); + AD9257_Set(AD9257_OUT_MODE_REG, AD9257_OUT_BINARY_OFST_VAL); //output clock phase FILE_LOG(logINFO, ("\tOutput clock phase\n")); //FIXME:?? - setAdc9257(AD9257_OUT_PHASE_REG, AD9257_OUT_CLK_60_VAL); + AD9257_Set(AD9257_OUT_PHASE_REG, AD9257_OUT_CLK_60_VAL); // lvds-iee reduced , binary offset FILE_LOG(logINFO, ("\tLvds-iee reduced, binary offset\n")); - setAdc9257(AD9257_OUT_MODE_REG, AD9257_OUT_LVDS_IEEE_VAL); + AD9257_Set(AD9257_OUT_MODE_REG, AD9257_OUT_LVDS_IEEE_VAL); // all devices on chip to receive next command FILE_LOG(logINFO, ("\tAll devices on chip to receive next command\n")); - setAdc9257(AD9257_DEV_IND_2_REG, + AD9257_Set(AD9257_DEV_IND_2_REG, AD9257_CHAN_H_MSK | AD9257_CHAN_G_MSK | AD9257_CHAN_F_MSK | AD9257_CHAN_E_MSK); #ifdef GOTTHARDD - setAdc9257(AD9257_DEV_IND_1_REG, + AD9257_Set(AD9257_DEV_IND_1_REG, AD9257_CHAN_D_MSK | AD9257_CHAN_C_MSK | AD9257_CHAN_B_MSK | AD9257_CHAN_A_MSK );// FIXME: gotthard setting dco and ifco to off?? #else - setAdc9257(AD9257_DEV_IND_1_REG, + AD9257_Set(AD9257_DEV_IND_1_REG, AD9257_CHAN_D_MSK | AD9257_CHAN_C_MSK | AD9257_CHAN_B_MSK | AD9257_CHAN_A_MSK | AD9257_CLK_CH_DCO_MSK | AD9257_CLK_CH_IFCO_MSK); #endif // vref 1.33 FILE_LOG(logINFO, ("\tVref 1.33\n"));// FIXME: needed for Gottthard? earlier not set (default 3.0 v) - setAdc9257(AD9257_VREF_REG, AD9257_VREF_1_33_VAL); + AD9257_Set(AD9257_VREF_REG, AD9257_VREF_1_33_VAL); // no test mode FILE_LOG(logINFO, ("\tNo test mode\n")); - setAdc9257(AD9257_TEST_MODE_REG, AD9257_TST_OFF_VAL); + AD9257_Set(AD9257_TEST_MODE_REG, AD9257_TST_OFF_VAL); #ifdef TESTADC FILE_LOG(logINFOBLUE, ("Putting ADC in Test Mode!\n"); // mixed bit frequency test mode FILE_LOG(logINFO, ("\tMixed bit frequency test mode\n")); - setAdc9257(AD9257_TEST_MODE_REG, AD9257_TST_MXD_BT_FRQ_VAL); + AD9257_Set(AD9257_TEST_MODE_REG, AD9257_TST_MXD_BT_FRQ_VAL); #endif } diff --git a/slsDetectorServers/slsDetectorServer/I2C.h b/slsDetectorServers/slsDetectorServer/I2C.h index 4e449fbed..e335b293a 100755 --- a/slsDetectorServers/slsDetectorServer/I2C.h +++ b/slsDetectorServers/slsDetectorServer/I2C.h @@ -1,6 +1,22 @@ #pragma once -#include "blackfin.h" /** I2C_CLOCK_MHZ should be defined */ +#include "blackfin.h" + +/** + * To be defined + * + * (in blackfin.h) + * I2C_CLOCK_MHZ + * + * (RegisterDefs.h) + * I2C_SCL_LOW_COUNT_REG + * I2C_SCL_HIGH_COUNT_REG + * I2C_SDA_HOLD_REG + * I2C_CONTROL_REG + * I2C_TRANSFER_COMMAND_FIFO_REG + * I2C_RX_DATA_FIFO_LEVEL_REG + */ + #define I2C_DATA_RATE_KBPS (200) #define I2C_SCL_PERIOD_NS ((1000 * 1000) / I2C_DATA_RATE_KBPS) @@ -51,86 +67,81 @@ * Configure the I2C core, * Enable core and * Calibrate the calibration register for current readout - * @param sclLowCountReg register to set low count of the serial clock - * @param sclHighCountReg register to set high count of the serial clock - * @param sdaHoldTimeReg register to set hold time of the serial data - * @oaram controlReg register to set control reg (bus speed and enabling core) */ -void I2C_ConfigureI2CCore(uint32_t sclLowCountReg, uint32_t sclHighCountReg, uint32_t sdaHoldTimeReg, uint32_t controlReg) { - FILE_LOG(logINFOBLUE, ("Configuring I2C Core for %d kbps:\n", I2C_DATA_RATE_KBPS)); +void I2C_ConfigureI2CCore() { + FILE_LOG(logINFOBLUE, ("\tConfiguring I2C Core for %d kbps:\n", I2C_DATA_RATE_KBPS)); FILE_LOG(logINFOBLUE, ("\tSetting SCL Low Period: %d ns (0x%x clocks)\n", I2C_SCL_LOW_PERIOD_NS, I2C_SCL_LOW_COUNT)); - bus_w(sclLowPeriodReg, (uint32_t)I2C_SCL_LOW_COUNT); + bus_w(I2C_SCL_LOW_COUNT_REG, (uint32_t)I2C_SCL_LOW_COUNT); FILE_LOG(logINFOBLUE, ("\tSetting SCL High Period: %d ns (0x%x clocks)\n", I2C_SCL_HIGH_PERIOD_NS, I2C_SCL_LOW_COUNT)); - bus_w(sclHighPeriodReg, (uint32_t)I2C_SCL_LOW_COUNT); + bus_w(I2C_SCL_HIGH_COUNT_REG, (uint32_t)I2C_SCL_LOW_COUNT); FILE_LOG(logINFOBLUE, ("\tSetting SDA Hold Time: %d ns (0x%x clocks)\n", I2C_SDA_DATA_HOLD_TIME_NS, I2C_SDA_DATA_HOLD_COUNT)); - bus_w(sdaHoldTimeReg, (uint32_t)I2C_SDA_DATA_HOLD_COUNT); + bus_w(I2C_SDA_HOLD_REG, (uint32_t)I2C_SDA_DATA_HOLD_COUNT); FILE_LOG(logINFOBLUE, ("\tEnabling core\n")); - bus_w(controlReg, I2C_CNTRL_ENBLE_CORE_MSK | I2C_CTRL_BUS_SPEED_FAST_400_VAL);// fixme: (works?) + bus_w(I2C_CONTROL_REG, I2C_CTRL_ENBLE_CORE_MSK | I2C_CTRL_BUS_SPEED_FAST_400_VAL);// fixme: (works?) } /** * Read register - * @param transferCommandReg transfer command fifo register - * @param rxDataFifoLevelReg receive data fifo level register * @param deviceId device Id * @param addr register address * @returns value read from register */ -uint32_t I2C_Read(uint32_t transferCommandReg, uint32_t rxDataFifoLevelReg, uint32_t devId, uint32_t addr) { +uint32_t I2C_Read(uint32_t devId, uint32_t addr) { + FILE_LOG(logDEBUG1, ("\tReading from I2C device 0x%x and reg 0x%x\n", devId, addr)); // device Id mask uint32_t devIdMask = ((devId << I2C_TFR_CMD_ADDR_OFST) & I2C_TFR_CMD_ADDR_MSK); // write I2C ID - bus_w(transferCommandReg, (devIdMask & ~(I2C_TFR_CMD_RW_MSK))); + bus_w(I2C_TRANSFER_COMMAND_FIFO_REG, (devIdMask & ~(I2C_TFR_CMD_RW_MSK))); // write register addr - bus_w(transferCommandReg, addr); + bus_w(I2C_TRANSFER_COMMAND_FIFO_REG, addr); // repeated start with read - bus_w(transferCommandReg, (devIdMask | I2C_TFR_CMD_RPTD_STRT_MSK | I2C_TFR_CMD_RW_READ_VAL)); + bus_w(I2C_TRANSFER_COMMAND_FIFO_REG, (devIdMask | I2C_TFR_CMD_RPTD_STRT_MSK | I2C_TFR_CMD_RW_READ_VAL)); // continue reading - bus_w(transferCommandReg, 0x0); + bus_w(I2C_TRANSFER_COMMAND_FIFO_REG, 0x0); // stop reading - bus_w(transferCommandReg, I2C_TFR_CMD_STOP_MSK); + bus_w(I2C_TRANSFER_COMMAND_FIFO_REG, I2C_TFR_CMD_STOP_MSK); // read value - return bus_r(rxDataFifoLevelReg); + return bus_r(I2C_RX_DATA_FIFO_LEVEL_REG); } /** * Write register (16 bit value) - * @param transferCommandReg transfer command fifo register * @param deviceId device Id * @param addr register address * @param data data to be written (16 bit) */ -void I2C_Write(uint32_t transferCommandReg, uint32_t devId, uint32_t addr, uint16_t data) { +void I2C_Write(uint32_t devId, uint32_t addr, uint16_t data) { + FILE_LOG(logDEBUG1, ("\tWriting data %d to I2C device 0x%x and reg 0x%x\n", data, devId, addr)); // device Id mask uint32_t devIdMask = ((devId << I2C_TFR_CMD_ADDR_OFST) & I2C_TFR_CMD_ADDR_MSK); // write I2C ID - bus_w(transferCommandReg, (devIdMask & ~(I2C_TFR_CMD_RW_MSK))); + bus_w(I2C_TRANSFER_COMMAND_FIFO_REG, (devIdMask & ~(I2C_TFR_CMD_RW_MSK))); // write register addr - bus_w(transferCommandReg, addr); + bus_w(I2C_TRANSFER_COMMAND_FIFO_REG, addr); // repeated start with write - bus_w(transferCommandReg, (devIdMask | I2C_TFR_CMD_RPTD_STRT_MSK & ~(I2C_TFR_CMD_RW_MSK))); + bus_w(I2C_TRANSFER_COMMAND_FIFO_REG, (devIdMask | I2C_TFR_CMD_RPTD_STRT_MSK & ~(I2C_TFR_CMD_RW_MSK))); uint8_t msb = data & 0xFF00; uint8_t lsb = data & 0x00FF; // writing data MSB - bus_w(transferCommandReg, ((msb << I2C_TFR_CMD_DATA_FR_WR_OFST) & I2C_TFR_CMD_DATA_FR_WR_MSK)); + bus_w(I2C_TRANSFER_COMMAND_FIFO_REG, ((msb << I2C_TFR_CMD_DATA_FR_WR_OFST) & I2C_TFR_CMD_DATA_FR_WR_MSK)); // writing data LSB and stop writing bit - bus_w(transferCommandReg, ((lsb << I2C_TFR_CMD_DATA_FR_WR_OFST) & I2C_TFR_CMD_DATA_FR_WR_MSK) | I2C_TFR_CMD_STOP_MSK); + bus_w(I2C_TRANSFER_COMMAND_FIFO_REG, ((lsb << I2C_TFR_CMD_DATA_FR_WR_OFST) & I2C_TFR_CMD_DATA_FR_WR_MSK) | I2C_TFR_CMD_STOP_MSK); } diff --git a/slsDetectorServers/slsDetectorServer/INA226.h b/slsDetectorServers/slsDetectorServer/INA226.h index 85deb0560..8987bcbb7 100755 --- a/slsDetectorServers/slsDetectorServer/INA226.h +++ b/slsDetectorServers/slsDetectorServer/INA226.h @@ -2,6 +2,14 @@ #include "I2C.h" +/** + * To be defined in + * + * (slsDetectorServer_defs.h) + * I2C_SHUNT_RESISTER_OHMS + * device ids that are passed as arguments + */ + /** INA226 defines */ /** Register set */ @@ -37,13 +45,10 @@ /** * Configure the I2C core and Enable core - * @param sclLowCountReg register to set low count of the serial clock (defined in Registerdefs.h) - * @param sclHighCountReg register to set high count of the serial clock (defined in Registerdefs.h) - * @param sdaHoldTimeReg register to set hold time of the serial data (defined in Registerdefs.h) - * @param controlReg register to set control reg (bus speed and enabling core) (defined in Registerdefs.h) */ -void INA226_ConfigureI2CCore(uint32_t sclLowCountReg, uint32_t sclHighCountReg, uint32_t sdaHoldTimeReg, uint32_t controlReg) { - I2C_ConfigureI2CCore(sclLowCountReg, sclHighCountReg, sdaHoldTimeReg, controlReg); +void INA226_ConfigureI2CCore() { + FILE_LOG(logINFO, ("Configuring INA226\n")); + I2C_ConfigureI2CCore(); } /** @@ -52,26 +57,24 @@ void INA226_ConfigureI2CCore(uint32_t sclLowCountReg, uint32_t sclHighCountReg, * @param transferCommandReg transfer command fifo register (defined in RegisterDefs.h) * @param deviceId device Id (defined in slsDetectorServer_defs.h) */ -void INA226_CalibrateCurrentRegister(uint32_t shuntResisterOhm, uint32_t transferCommandReg, uint32_t deviceId) { +void INA226_CalibrateCurrentRegister(uint32_t deviceId) { // get calibration value based on shunt resistor - uint16_t calVal = INA226_getCalibrationValue(shuntResisterOhm) & INA226_CALIBRATION_MSK; + uint16_t calVal = INA226_getCalibrationValue(I2C_SHUNT_RESISTER_OHMS) & INA226_CALIBRATION_MSK; FILE_LOG(logINFO, ("\tWriting to Calibration reg: 0x%0x\n", calVal)); // calibrate current register - I2C_Write(transferCommandReg, deviceId, INA226_CALIBRATION_REG, calVal); + I2C_Write(INA226_TRANSFER_COMMAND_FIFO_REG, deviceId, INA226_CALIBRATION_REG, calVal); } /** * Read voltage of device - * @param transferCommandReg transfer command fifo register (defined in RegisterDefs.h) - * @param rxDataFifoLevelReg receive data fifo level register (defined in RegisterDefs.h) - * @param deviceId device Id (defined in slsDetectorServer_defs.h) + * @param deviceId device Id * @returns voltage in mV */ -int INA226_ReadVoltage(uint32_t transferCommandReg, uint32_t rxDataFifoLevelReg, uint32_t deviceId) { +int INA226_ReadVoltage(uint32_t deviceId) { FILE_LOG(logDEBUG1, ("\tReading voltage\n")); - uint32_t regval = I2C_Read(transferCommandReg, rxDataFifoLevelReg, deviceId, INA226_BUS_VOLTAGE_REG); + uint32_t regval = I2C_Read(deviceId, INA226_BUS_VOLTAGE_REG); FILE_LOG(logDEBUG1, ("\tvoltage read: 0x%08x\n", regval)); // value converted in mv @@ -92,22 +95,20 @@ int INA226_ReadVoltage(uint32_t transferCommandReg, uint32_t rxDataFifoLevelReg, /** * Read current - * @param transferCommandReg transfer command fifo register (defined in RegisterDefs.h) - * @param rxDataFifoLevelReg receive data fifo level register (defined in RegisterDefs.h) - * @param deviceId device Id (should be defined in slsDetectorServer_defs.h) + * @param deviceId device Id * @returns current in mA */ -int INA226_ReadCurrent(uint32_t transferCommandReg, uint32_t rxDataFifoLevelReg, uint32_t deviceId) { +int INA226_ReadCurrent(uint32_t deviceId) { FILE_LOG(logDEBUG1, ("\tReading current\n")); // read shunt voltage register FILE_LOG(logDEBUG1, ("\tReading shunt voltage reg\n")); - uint32_t shuntVoltageRegVal = I2C_Read(transferCommandReg, rxDataFifoLevelReg, deviceId, INA226_SHUNT_VOLTAGE_REG); + uint32_t shuntVoltageRegVal = I2C_Read(deviceId, INA226_SHUNT_VOLTAGE_REG); FILE_LOG(logDEBUG1, ("\tshunt voltage reg: 0x%08x\n", regval)); // read calibration register FILE_LOG(logDEBUG1, ("\tReading calibration reg\n")); - uint32_t calibrationRegVal = I2C_Read(transferCommandReg, rxDataFifoLevelReg, deviceId, INA226_CALIBRATION_REG); + uint32_t calibrationRegVal = I2C_Read(deviceId, INA226_CALIBRATION_REG); FILE_LOG(logDEBUG1, ("\tcalibration reg: 0x%08x\n", regval)); // value for current diff --git a/slsDetectorServers/slsDetectorServer/LTC2620.h b/slsDetectorServers/slsDetectorServer/LTC2620.h new file mode 100755 index 000000000..6e78a9703 --- /dev/null +++ b/slsDetectorServers/slsDetectorServer/LTC2620.h @@ -0,0 +1,288 @@ +#pragma once + +#include "commonServerFunctions.h" // blackfin.h, ansi.h +#include "common.h" + +/* LTC2620 DAC DEFINES */ +// first 4 bits are 0 as this is a 12 bit dac +#define LTC2620_DAC_DATA_OFST (4) +#define LTC2620_DAC_DATA_MSK (0x00000FFF << LTC2620_DAC_DATA_OFST) +#define LTC2620_DAC_ADDR_OFST (16) +#define LTC2620_DAC_ADDR_MSK (0x0000000F << LTC2620_DAC_ADDR_OFST) +#define LTC2620_DAC_CMD_OFST (20) +#define LTC2620_DAC_CMD_MSK (0x0000000F << LTC2620_DAC_CMD_OFST) + +#define LTC2620_DAC_CMD_WR_IN_VAL ((0x0 << LTC2620_DAC_CMD_OFST) & LTC2620_DAC_CMD_MSK) // write to input register +#define LTC2620_DAC_CMD_UPDTE_DAC_VAL ((0x1 << LTC2620_DAC_CMD_OFST) & LTC2620_DAC_CMD_MSK) // update dac (power up) +#define LTC2620_DAC_CMD_WR_IN_UPDTE_DAC_VAL ((0x2 << LTC2620_DAC_CMD_OFST) & LTC2620_DAC_CMD_MSK) // write to input register and update dac (power up) +#define LTC2620_DAC_CMD_WR_UPDTE_DAC_VAL ((0x3 << LTC2620_DAC_CMD_OFST) & LTC2620_DAC_CMD_MSK) // write to and update dac (power up) +#define LTC2620_DAC_CMD_PWR_DWN_VAL ((0x4 << LTC2620_DAC_CMD_OFST) & LTC2620_DAC_CMD_MSK) +#define LTC2620_DAC_CMD_NO_OPRTN_VAL ((0xF << LTC2620_DAC_CMD_OFST) & LTC2620_DAC_CMD_MSK) + +#define LTC2620_NUMBITS (24) +#define LTC2620_DAISY_CHAIN_NUMBITS (32) // due to shift register FIXME: was 33 earlier +#define LTC2620_NUMCHANNELS (8) +#define LTC2620_MIN_MV (0) +#define LTC2620_MAX_STEPS (2^12) // 4096 +#define LTC2620_PWR_DOWN_VAL (-100) + +uint32_t LTC2620_Reg = 0x0; +uint32_t LTC2620_CsMask = 0x0; +uint32_t LTC2620_ClkMask = 0x0; +uint32_t LTC2620_DigMask = 0x0; +int LTC2620_DigOffset = 0x0; +int LTC2620_Ndac = 0; +int LTC2620_MaxMV = 0; + +/** + * Set Defines + * @param reg spi register + * @param cmsk chip select mask + * @param clkmsk clock output mask + * @param dmsk digital output mask + * @param dofst digital output offset + * @param nd total number of dacs for this board (for dac channel and daisy chain chip id) + * @param mv maximum voltage in mV + */ +void LTC2620_SetDefines(uint32_t reg, uint32_t cmsk, uint32_t clkmsk, uint32_t dmsk, int dofst, int nd, int mv) { + LTC2620_Reg = reg; + LTC2620_CsMask = cmsk; + LTC2620_ClkMask = clkmsk; + LTC2620_DigMask = dmsk; + LTC2620_DigOffset = dofst; + LTC2620_Ndac = nd; + LTC2620_MaxMV = mv; +} + +/** + * Disable SPI + */ +void LTC2620_Disable() { + bus_w(LTC2620_Reg, bus_r(LTC2620_Reg) + | LTC2620_CsMask + | LTC2620_ClkMask + &~(LTC2620_DigMask)); +} + +/** + * 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) { + // validate index + if (dacnum < 0 || dacnum >= LTC2620_Ndac) { + FILE_LOG(logERROR, ("Dac index %d is out of bounds (0 to %d)\n", dacnum, LTC2620_Ndac - 1)); + 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; + if (mV) { + ret = LTC2620_VoltageToDac(val, dacval); + } else if (val >= 0) { // do not convert power down dac val + ret = LTC2620_DacToVoltage(val, &dacmV); + } + + // conversion out of bounds + if (ret == FAIL) { + FILE_LOG(logERROR, ("Setting Dac %d %s is out of bounds\n", dacnum, (mV ? "mV" : "dac units"))); + return FAIL; + } + + // set + if ( (dacval >= 0) || (dacval == LTC2620_PWR_DOWN_VAL)) { + FILE_LOG(logINFO, ("Setting DAC %d: %d dac (%d mV)\n",ind, dacval, dacmV)); + LTC2620_SetDAC(ind, dacval); + } + return OK; +} + +/** + * Convert voltage to dac units + * @param voltage value in mv + * @param dacval pointer to value converted to dac units + * @returns FAIL when voltage outside limits, OK if conversion successful + */ +int LTC2620_VoltageToDac(int voltage, int* dacval) { + return Common_VoltageToDac(voltage, dacval, LTC2620_MIN_MV, LTC2620_MaxMV, LTC2620_MAX_STEPS); +} + +/** + * Convert dac units to voltage + * @param dacval dac units + * @param voltage pointer to value converted to mV + * @returns FAIL when voltage outside limits, OK if conversion successful + */ +int LTC2620_DacToVoltage(int dacval, int* voltage) { + return Common_VoltageToDac(dacval, voltage, LTC2620_MIN_MV, LTC2620_MaxMV, LTC2620_MAX_STEPS); +} + +/** + * Configure (obtains dacaddr, command and ichip and calls LTC2620_Set) + */ +void LTC2620_Configure(){ + FILE_LOG(logINFOBLUE, ("Configuring LTC2620\n")); + + // dac channel - all channels + int addr = LTC2620_DAC_ADDR_MSK; + + // data (any random low value, just writing to power up) + int data = 0x6; + + // command + int cmd = LTC2620_DAC_CMD_WR_IN_VAL; //FIXME: should be command update and not write(does not power up) + // also why do we need to power up (for jctb, we power down next) + + LTC2620_Set(data, addr, cmd, -1); +} + + +/** + * Set Dac value (obtains dacaddr, command and ichip and calls LTC2620_Set) + * @param dacnum dac number + * @param data dac value to set + */ +void LTC2620_SetDACValue (int dacnum, int data) { + FILE_LOG(logDEBUG1, ("\tSetting dac %d to %d\n", dacnum, data)); + // LTC2620 index + int ichip = dacnum / LTC2620_NUMCHANNELS; + + // dac channel + int addr = dacnum % LTC2620_NUMCHANNELS; + + // command + int cmd = LTC2620_DAC_CMD_WR_UPDTE_DAC_VAL; + + // power down mode, value is ignored + if (val == -100) { + cmd = LTC2620_DAC_CMD_PWR_DWN_VAL; + FILE_LOG(logDEBUG1, ("\tPOWER DOWN\n")); + } else { + FILE_LOG(logDEBUG1,("\tWrite to Input Register and Update\n")); + } + + LTC2620_Set(data, addr, cmd, ichip); +} + + +/** + * Sets a single chip (LTC2620_SetSingle) or multiple chip (LTC2620_SetDaisy) + * multiple chip is only for ctb where the multiple chips are connected in daisy fashion + * @param cmd command to send + * @param data dac value to be set + * @param dacaddr dac channel number for the chip + * @param chipIndex the chip to be set + */ +void LTC2620_Set(int cmd, int data, int dacaddr, int chipIndex) { + // ctb + if (LTC2620_Ndac > LTC2620_NUMCHANNELS) + LTC2620_SetDaisy(cmd, data, dacaddr, chipIndex); + // others + else + LTC2620_SetSingle(cmd, data, dacaddr); +} + + +/** + * Set a single chip (all non ctb detectors use this) + * when max dac is 8 + * @param cmd command + * @param data dac value to be set + * @param dacaddr dac channel number in chip + */ +void LTC2620_SetSingle(int cmd, int data, int dacaddr) { + FILE_LOG(logDEBUG1, ("\tdac addr:%d, dac value:%d, cmd:%d\n", dacaddr, data, cmd)); + + uint32_t codata = (((data << LTC2620_DAC_DATA_OFST) & LTC2620_DAC_DATA_MSK) | + ((dacaddr << LTC2620_DAC_ADDR_OFST) & LTC2620_DAC_ADDR_MSK) | + cmd); + + serializeToSPI (LTC2620_Reg, codata, LTC2620_CsMask, LTC2620_NUMBITS, + LTC2620_ClkMask, LTC2620_DigMask, LTC2620_DigOffset); +} + + +/** + * Set a single chip (all non ctb detectors use this) + * when max dac is 8 + * @param cmd command + * @param data dac value to be set + * @param dacaddr dac channel number in chip + * @param chipIndex index of the chip + */ +void LTC2620_SetDaisy(int cmd, int data, int dacaddr, int chipIndex) { + + int nchip = LTC2620_Ndac / LTC2620_NUMCHANNELS; + uint32_t valw = 0; + int ichip = 0; + + FILE_LOG(logDEBUG1, ("\tdesired chip index:%d, nchip:%d, dac channel:%d, dac value:%d, cmd:%d \n", + chipIndex, nchip, dacaddr, data, cmd)); + + // data to be bit banged + uint32_t codata = (((data << LTC2620_DAC_DATA_OFST) & LTC2620_DAC_DATA_MSK) | + ((dacaddr << LTC2620_DAC_ADDR_OFST) & LTC2620_DAC_ADDR_MSK) | + cmd); + + // select all chips (ctb daisy chain; others 1 chip) + FILE_LOG(logDEBUG1, ("\tSelecting LTC2620\n")); + SPIChipSelect (&valw, LTC2620_Reg, LTC2620_CsMask, LTC2620_ClkMask, LTC2620_DigMask); + + // send same data to all + if (chipIndex < 0) { + FILE_LOG(logDEBUG1, ("\tSend same data to all\n")); + for (ichip = 0; ichip < nchip; ++ichip) { + FILE_LOG(logDEBUG1, ("\tSend to ichip %d\n", ichip)); + LTC2620_SendDaisyData(&valw, codata); + } + } + + // send to one chip, nothing to others + else { + // send nothing to preceding ichips (daisy chain) (if any chips in front of desired chip) + for (ichip = 0; ichip < chipIndex; ++ichip) { + FILE_LOG(logDEBUG1, ("\tSend nothing to ichip %d\n", ichip)); + LTC2620_SendDaisyData(&valw, LTC2620_DAC_CMD_NO_OPRTN_VAL); + } + + // send data to desired chip + FILE_LOG(logDEBUG1, ("\tSend data to ichip %d\n", chipIndex)); + LTC2620_SendDaisyData(&valw, codata); + + // send nothing to subsequent ichips (daisy chain) (if any chips after desired chip) + int ichip = 0; + for (ichip = chipIndex + 1; ichip < nchip; ++ichip) { + FILE_LOG(logDEBUG1, ("\tSend nothing to ichip %d\n", ichip)); + LTC2620_SendDaisyData(&valw, LTC2620_DAC_CMD_NO_OPRTN_VAL); + } + } + + // deselect all chips (ctb daisy chain; others 1 chip) + FILE_LOG(logDEBUG1, ("\tDeselecting LTC2620\n")); + SPIChipDeselect(&valw, LTC2620_Reg, LTC2620_CsMask, LTC2620_ClkMask); +} + + +/** + * bit bang the data into all the chips daisy fashion + * @param valw current value of register while bit banging + * @param val data to be sent (data, dac addr and command) + */ +void LTC2620_SendDaisyData(uint32_t* valw, uint32_t val) { + sendDataToSPI(valw, LTC2620_Reg, val, LTC2620_DAISY_CHAIN_NUMBITS, + LTC2620_ClkMask, LTC2620_DigMask, LTC2620_DigOffset); +} + + + + + diff --git a/slsDetectorServers/slsDetectorServer/commonServerFunctions.h b/slsDetectorServers/slsDetectorServer/commonServerFunctions.h index 59f106e08..fc8b326db 100755 --- a/slsDetectorServers/slsDetectorServer/commonServerFunctions.h +++ b/slsDetectorServers/slsDetectorServer/commonServerFunctions.h @@ -2,21 +2,21 @@ #include "blackfin.h" -void SPIChipSelect (uint32_t* valw, uint32_t addr, uint32_t csmask) { +void SPIChipSelect (uint32_t* valw, uint32_t addr, uint32_t csmask, uint32_t clkmask, uint32_t digoutmask) { // start point - (*valw) = 0xffffffff; // old board compatibility (not using specific bits) + (*valw) = (bus_r(addr) | csmask | clkmask &~(digoutmask)); bus_w (addr, (*valw)); // chip sel bar down - (*valw) &= ~csmask; /* todo with test: done a bit different, not with previous value */ + (*valw) &= ~csmask; bus_w (addr, (*valw)); } void SPIChipDeselect (uint32_t* valw, uint32_t addr, uint32_t csmask, uint32_t clkmask) { // chip sel bar up - (*valw) |= csmask; /* todo with test: not done for spi */ + (*valw) |= csmask; bus_w (addr, (*valw)); //clk down @@ -24,8 +24,8 @@ void SPIChipDeselect (uint32_t* valw, uint32_t addr, uint32_t csmask, uint32_t bus_w (addr, (*valw)); // stop point = start point of course - (*valw) = 0xffffffff; // old board compatibility (not using specific bits) - bus_w (addr, (*valw)); + (*valw) |= csmask; + bus_w (addr, (*valw)); //FIXME: for ctb slow adcs, might need to set it to low again } void sendDataToSPI (uint32_t* valw, uint32_t addr, uint32_t val, int numbitstosend, uint32_t clkmask, uint32_t digoutmask, int digofset) { @@ -47,6 +47,24 @@ void sendDataToSPI (uint32_t* valw, uint32_t addr, uint32_t val, int numbitstose } } +uint32_t receiveDataFromSPI (uint32_t* valw, uint32_t addr, int numbitstoreceive, uint32_t clkmask, uint32_t readaddr) { + uint32_t retval = 0; + int i = 0; + for (i = 0; i < numbitstoreceive; ++i) { + + // clk down + (*valw) &= ~clkmask; + bus_w (addr, (*valw)); + + // read data (i) + retval |= ((bus_r(readaddr) & 0x1) << (numbitstoreceive - 1 - i)); + + // clk up + (*valw) |= clkmask ; + bus_w (addr, (*valw)); + } + return retval; +} void serializeToSPI(uint32_t addr, uint32_t val, uint32_t csmask, int numbitstosend, uint32_t clkmask, uint32_t digoutmask, int digofset) { if (numbitstosend == 16) { @@ -62,3 +80,21 @@ void serializeToSPI(uint32_t addr, uint32_t val, uint32_t csmask, int numbitstos SPIChipDeselect(&valw, addr, csmask, clkmask); } + +uint32_t serializeFromSPI(uint32_t addr, uint32_t csmask, int numbitstoreceive, uint32_t clkmask, uint32_t digoutmask, uint32_t readaddr) { + + uint32_t valw; + + SPIChipSelect (&valw, addr, csmask, clkmask, digoutmask); + + uint32_t retval = receiveDataFromSPI(&valw, addr, numbitstoreceive, clkmask, readaddr); + + SPIChipDeselect(&valw, addr, csmask, clkmask); + + if (numbitstoreceive == 16) { + FILE_LOG(logDEBUG1, ("Read From SPI Register: 0x%04x\n", retval)); + } else { + FILE_LOG(logDEBUG1, ("Read From SPI Register: 0x%08x\n", retval)); + } + return retval; +} diff --git a/slsDetectorServers/slsDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/slsDetectorServer/slsDetectorFunctionList.h index db498ffd2..fc7eb0181 100644 --- a/slsDetectorServers/slsDetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/slsDetectorServer/slsDetectorFunctionList.h @@ -144,30 +144,23 @@ int setThresholdEnergy(int ev); #endif // parameters - dac, adc, hv -#if defined(GOTTHARDD) || defined(JUNGFRAUD) || defined (CHIPTESTBOARDD) -void serializeToSPI(u_int32_t addr, u_int32_t val, u_int32_t csmask, int numbitstosend, u_int32_t clkmask, u_int32_t digoutmask, int digofset); //commonServerFunction.h -void initDac(int dacnum); -int voltageToDac(int value); -int dacToVoltage(unsigned int digital); -#endif -#ifdef CHIPTESTBOARDD -int generalVoltageToDac(int value, int vmin, int vmax, int check); -int generalDacToVoltage(unsigned int digital, int vmin, int vmax, int check); -#endif #ifdef GOTTHARDD -extern void setAdc9252(int addr, int val); // AD9252.h (old board) +extern void AD9252_Set(int addr, int val); // AD9252.h (old board) #endif #if defined(GOTTHARDD) || defined(JUNGFRAUD) || defined(CHIPTESTBOARDD) -extern void setAdc9257(int addr, int val); // AD9257.h +extern void AD9257_Set(int addr, int val); // AD9257.h #endif #ifdef CHIPTESTBOARDD -extern int getMaxValidVref(); // AD9257.h -extern void setVrefVoltage(int val) // AD9257.h +extern int AD9257_GetMaxValidVref(); // AD9257.h +extern void AD9257_SetVrefVoltage(int val) // AD9257.h #endif -void setDAC(enum DACINDEX ind, int val, int mV, int retval[]); +void setDAC(enum DACINDEX ind, int val, int mV); +int getDAC(enum DACINDEX ind, int mV); +int getMAXDACUnits(); #ifdef CHIPTESTBOARDD -int isVLimitCompliant(int mV); +int checkVLimitCompliant(int mV); +int checkVLimitDacCompliant(int dac); int getVLimit(); void setVLimit(int l); int isVchipValid(int val); @@ -189,8 +182,10 @@ u_int32_t putout(char *s); #endif*/ int getADC(enum ADCINDEX ind); #ifdef CHIPTESTBOARDD -int getVoltage(int idac); -int getCurrent(int idac); +extern int INA226_ReadVoltage(uint32_t transferCommandReg, uint32_t rxDataFifoLevelReg, uint32_t deviceId); // INA226.h +extern int INA226_ReadCurrent(uint32_t transferCommandReg, uint32_t rxDataFifoLevelReg, uint32_t deviceId); // INA226.h +extern int AD7689_GetTemperature(); // AD7689.h +extern int AD7689_GetChannel(int ichan); // AD7689.h #endif int setHighVoltage(int val); diff --git a/slsDetectorServers/slsDetectorServer/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/slsDetectorServer_funcs.c index d1fe3b66a..f4dc671a4 100755 --- a/slsDetectorServers/slsDetectorServer/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/slsDetectorServer_funcs.c @@ -571,7 +571,7 @@ int set_dac(int file_des) { ret = OK; memset(mess, 0, sizeof(mess)); int args[3] = {-1, -1, -1}; - int retval[2] = {-1, -1}; + int retval = -1; if (receiveData(file_des, args, sizeof(args), INT32) < 0) return printSocketReadError(); @@ -719,12 +719,12 @@ int set_dac(int file_des) { // adc vpp #ifdef CHIPTESTBOARDD case ADC_VPP: - if (val < 0 || val > getMaxValidVref()) { + if (val < 0 || val > AD9257_GetMaxValidVref()) { ret = FAIL; strcpy(mess,"Could not set dac. Adc Vpp value should be between 0 and %d\n", maxValidVref()); FILE_LOG(logERROR,(mess)); } else { - setVrefVoltage(val); + AD9257_AD9257_SetVrefVoltage(val); retval = val; // cannot read } break; @@ -733,36 +733,36 @@ int set_dac(int file_des) { // io delay #ifdef EIGERD case IO_DELAY: - retval[0] = setIODelay(val); - FILE_LOG(logDEBUG1, ("IODelay: %d\n", retval[0])); - validate(val, retval[0], "set iodelay", DEC); + retval = setIODelay(val); + FILE_LOG(logDEBUG1, ("IODelay: %d\n", retval)); + validate(val, retval, "set iodelay", DEC); break; #endif // high voltage case HIGH_VOLTAGE: - retval[0] = setHighVoltage(val); - FILE_LOG(logDEBUG1, ("High Voltage: %d\n", retval[0])); + retval = setHighVoltage(val); + FILE_LOG(logDEBUG1, ("High Voltage: %d\n", retval)); #if defined(JUNGFRAUD) || defined (CHIPTESTBOARDD) - validate(val, retval[0], "set high voltage", DEC); + validate(val, retval, "set high voltage", DEC); #endif #ifdef GOTTHARDD - if (retval[0] == -1) { + if (retval == -1) { ret = FAIL; strcpy(mess,"Invalid Voltage. Valid values are 0, 90, 110, 120, 150, 180, 200\n"); FILE_LOG(logERROR,(mess)); } else - validate(val, retval[0], "set high voltage", DEC); + validate(val, retval, "set high voltage", DEC); #elif EIGERD - if ((retval[0] != SLAVE_HIGH_VOLTAGE_READ_VAL) && (retval[0] < 0)) { + if ((retval != SLAVE_HIGH_VOLTAGE_READ_VAL) && (retval < 0)) { ret = FAIL; - if (retval[0] == -1) + 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[0] == -2) + else if (retval == -2) strcpy(mess, "Setting high voltage failed. " "Serial/i2c communication failed.\n"); - else if (retval[0] == -3) + else if (retval == -3) strcpy(mess, "Getting high voltage failed. " "Serial/i2c communication failed.\n"); FILE_LOG(logERROR,(mess)); @@ -781,7 +781,7 @@ int set_dac(int file_des) { ret = FAIL; sprintf(mess,"Could not set power. Power regulator %d should be in mV and not dac units.\n", ind); FILE_LOG(logERROR,(mess)); - } else if (checkVLimitCompliant() == FAIL) { + } else if (checkVLimitCompliant(val) == FAIL) { ret = FAIL; sprintf(mess,"Could not set power. Power regulator %d exceeds voltage limit %d.\n", ind, getVLimit()); FILE_LOG(logERROR,(mess)); @@ -792,9 +792,9 @@ int set_dac(int file_des) { } else { if (val != -1) setPower(serverDacIndex, val); - retval[0] = getPower(serverDacIndex); - FILE_LOG(logDEBUG1, ("Power regulator(%d): %d\n", ind, retval[0])); - validate(val, retval[0], "set power regulator", DEC); + retval = getPower(serverDacIndex); + FILE_LOG(logDEBUG1, ("Power regulator(%d): %d\n", ind, retval)); + validate(val, retval, "set power regulator", DEC); } break; @@ -810,9 +810,9 @@ int set_dac(int file_des) { } else { if (val >= 0) // not letting user set to -100, it will affect setting setVchip(val); - retval[0] = getVchip(); - FILE_LOG(logDEBUG1, ("Vchip: %d\n", retval[0])); - validate(val, retval[0], "set vchip", DEC); + retval = getVchip(); + FILE_LOG(logDEBUG1, ("Vchip: %d\n", retval)); + validate(val, retval, "set vchip", DEC); } break; @@ -824,9 +824,9 @@ int set_dac(int file_des) { } else { if (val >= 0) setVLimit(val); - retval[0] = getVLimit(); - FILE_LOG(logDEBUG1, ("VLimit: %d\n", retval[0])); - validate(val, retval[0], "set vlimit", DEC); + retval = getVLimit(); + FILE_LOG(logDEBUG1, ("VLimit: %d\n", retval)); + validate(val, retval, "set vlimit", DEC); } break; #endif @@ -837,14 +837,14 @@ int set_dac(int file_des) { ret = FAIL; sprintf(mess,"Could not set dac %d to value %d. Allowed limits (0 - %d mV).\n", ind, val, MAX_DAC_VOLTAGE_VALUE); FILE_LOG(logERROR,(mess)); - } else if (!mV && val > MAX_DAC_UNIT_VALUE ) { + } else if (!mV && val > LTC2620_MAX_STEPS ) { ret = FAIL; - sprintf(mess,"Could not set dac %d to value %d. Allowed limits (0 - %d dac units).\n", ind, val, MAX_DAC_UNIT_VALUE); + sprintf(mess,"Could not set dac %d to value %d. Allowed limits (0 - %d dac units).\n", ind, val, LTC2620_MAX_STEPS); FILE_LOG(logERROR,(mess)); } else { #ifdef CHIPTESTBOARDD - if ((mV && checkVLimitCompliant() == FAIL) || - (!mv && checkVLimitCompliant(dacToVoltage(val)) == FAIL)) { + if ((mV && checkVLimitCompliant(val) == FAIL) || + (!mv && checkVLimitDacCompliant(val) == FAIL)) { ret = FAIL; sprintf(mess,"Could not set dac %d to value %d. " "Exceeds voltage limit %d.\n", @@ -875,25 +875,20 @@ int set_dac(int file_des) { #endif //check if (ret == OK) { - int temp = 0; - if (mV) - temp = retval[1]; - else - temp = retval[0]; - if ((abs(temp-val) <= 5) || val == -1) { + if ((abs(retval - val) <= 5) || val == -1) { ret = OK; } else { ret = FAIL; - sprintf(mess,"Setting dac %d : wrote %d but read %d\n", serverDacIndex, val, temp); + sprintf(mess,"Setting dac %d : wrote %d but read %d\n", serverDacIndex, val, retval); FILE_LOG(logERROR,(mess)); } } - FILE_LOG(logDEBUG1, ("Dac (%d): %d dac units and %d mV\n", serverDacIndex, retval[0], retval[1])); + FILE_LOG(logDEBUG1, ("Dac (%d): %d %s\n", serverDacIndex, retval, (mv ? "mV" : "dac units"))); break; } } } - return Server_SendResult(file_des, INT32, UPDATE, retval, sizeof(retval)); + return Server_SendResult(file_des, INT32, UPDATE, &retval, sizeof(retval)); } @@ -2664,12 +2659,12 @@ int write_adc_register(int file_des) { // only set if (Server_VerifyLock() == OK) { #ifdef JUNGFRAUD - setAdc9257(addr, val); + AD9257_Set(addr, val); #elif GOTTHARDD if (getBoardRevision() == 1) { - setAdc9252(addr, val); + AD9252_Set(addr, val); } else { - setAdc9257(addr, val); + AD9257_Set(addr, val); } #endif } diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 29c2411fd..de36de8b5 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -2483,11 +2483,11 @@ int slsDetector::setDAC(int val, dacIndex index, int mV) { int fnum = F_SET_DAC; int ret = FAIL; int args[3] = {(int)index, mV, val}; - int retvals[2] = {-1, -1}; + int retval = -1; FILE_LOG(logDEBUG1) << "Setting DAC " << index << " to " << val << (mV ? "mV" : "dac units"); if (thisDetector->onlineFlag == ONLINE_FLAG && connectControl() == OK) { - ret = thisDetectorControl->Client_Send(fnum, args, sizeof(args), retvals, sizeof(retvals)); + ret = thisDetectorControl->Client_Send(fnum, args, sizeof(args), &retval, sizeof(retval)); disconnectControl(); // handle ret @@ -2495,14 +2495,12 @@ int slsDetector::setDAC(int val, dacIndex index, int mV) { setErrorMask((getErrorMask())|(OTHER_ERROR_CODE)); else { FILE_LOG(logDEBUG1) << "Dac index " << index << ": " - << retvals[0] << " dac units (" << retvals[1] << "mV)"; + << retval << (mV ? "mV" : "dac units"); if (ret == FORCE_UPDATE) ret = updateDetector(); } } - if (mV) - return retvals[1]; - return retvals[0]; + return retval; } diff --git a/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp b/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp index 3f6e65ffc..3327ec84c 100644 --- a/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp @@ -4174,6 +4174,9 @@ string slsDetectorCommand::cmdADC(int narg, char *args[], int action, int detPos if (sscanf(args[0],"adc:%d",&idac)==1) { // printf("chiptestboard!\n"); adc=(dacIndex)(idac+1000); + if (idac < SLOW_ADC0 || idac > SLOW_ADC_TEMP) + return (string ("cannot set adc, must be between ") + (SLOW_ADC0 - 1000) + + string (" and ") + (SLOW_ADC_TEMP - 1000)); } else if (cmd=="temp_adc") adc=TEMPERATURE_ADC; else if (cmd=="temp_fpga") diff --git a/slsSupportLib/include/sls_detector_defs.h b/slsSupportLib/include/sls_detector_defs.h index cbdf17250..2d5b86ecd 100755 --- a/slsSupportLib/include/sls_detector_defs.h +++ b/slsSupportLib/include/sls_detector_defs.h @@ -455,7 +455,16 @@ public: I_POWER_C=108 , /**new chiptest board */ I_POWER_D=109 , /**new chiptest board */ I_POWER_IO=110 , /**new chiptest board */ - V_LIMIT=111 /**new chiptest board */ + V_LIMIT=111, /**new chiptest board */ + SLOW_ADC0=1000, + SLOW_ADC1, + SLOW_ADC2, + SLOW_ADC3, + SLOW_ADC4, + SLOW_ADC5, + SLOW_ADC6, + SLOW_ADC7, + SLOW_ADC_TEMP }; /**