ctb server:

- allowed flags resetfpga and programfpga for moench and ctb
- set a minimum voltage for vio as it powers the fpga (1200mV)
- changed the min and max for vchip and power regulators due to tolerance
- fixed pattern read function
- fixed minor bugs with input of ctb patterns(waittime) and mode other than 1
- fixed calibration current register for i2c for tolerance (/1.2268)
- fixed current readout of i2c
- added digital, analog and normal readouts for flags
This commit is contained in:
2019-02-20 15:36:34 +01:00
parent 775bde76c9
commit 09546a8632
8 changed files with 88 additions and 99 deletions

View File

@ -1,6 +1,7 @@
#pragma once
#include "I2C.h"
#include "math.h"
/**
* To be defined in
@ -52,6 +53,9 @@
#define INA226_getConvertedCurrentUnits(shuntV, calibReg) ((double)shuntV * (double)calibReg / (double)2048)
double INA226_Shunt_Resistor_Ohm = 0.0;
int INA226_Calibration_Register_Value = 0;
#define INA226_CALIBRATION_CURRENT_TOLERANCE (1.2268)
/**
@ -84,7 +88,11 @@ void INA226_CalibrateCurrentRegister(uint32_t deviceId) {
FILE_LOG(logINFO, ("Calibrating Current Register for Device ID: 0x%x\n", deviceId));
// get calibration value based on shunt resistor
uint16_t calVal = ((uint16_t)INA226_getCalibrationValue(INA226_Shunt_Resistor_Ohm)) & INA226_CALIBRATION_MSK;
FILE_LOG(logINFO, ("\tWriting to Calibration reg: 0x%0x\n", calVal));
FILE_LOG(logINFO, ("\tCalculated calibration reg value: 0x%0x (%d)\n", calVal, calVal));
calVal = ((double)calVal / INA226_CALIBRATION_CURRENT_TOLERANCE) + 0.5;
FILE_LOG(logINFO, ("\tRealculated (for tolerance) calibration reg value: 0x%0x (%d)\n", calVal, calVal));
INA226_Calibration_Register_Value = calVal;
// calibrate current register
I2C_Write(deviceId, INA226_CALIBRATION_REG, calVal);
@ -132,71 +140,38 @@ int INA226_ReadCurrent(uint32_t deviceId) {
// read shunt voltage register
FILE_LOG(logDEBUG1, (" Reading shunt voltage reg\n"));
uint32_t shuntVoltageRegVal = I2C_Read(deviceId, INA226_SHUNT_VOLTAGE_REG);
FILE_LOG(logDEBUG1, (" shunt voltage reg: 0x%x\n", shuntVoltageRegVal));
FILE_LOG(logDEBUG1, (" shunt voltage reg: %d\n", shuntVoltageRegVal));
// read it once more as this error has occured once
if (shuntVoltageRegVal == 0xFFFF) {
FILE_LOG(logDEBUG1, (" Reading shunt voltage reg again\n"));
shuntVoltageRegVal = I2C_Read(deviceId, INA226_SHUNT_VOLTAGE_REG);
FILE_LOG(logDEBUG1, (" shunt voltage reg: 0x%x\n", shuntVoltageRegVal));
FILE_LOG(logDEBUG1, (" shunt voltage reg: %d\n", shuntVoltageRegVal));
}
// get absolute value and if negative
int negative = (shuntVoltageRegVal & INA226_SHUNT_NEGATIVE_MSK) ? 1: 0;
int shuntabsnV = shuntVoltageRegVal & INA226_SHUNT_ABS_VALUE_MSK;
FILE_LOG(logDEBUG1, (" negative: %d, absolute: 0x%x\n", negative, shuntabsnV));
// negative, convert absolute to 2s complement
if (negative) {
shuntabsnV = ((~shuntabsnV) + 1);
FILE_LOG(logDEBUG1, (" new absolute for negative (2's comple): 0x%x\n", shuntabsnV));
}
// calculate shunt voltage
int shuntVoltagenV = 0;
ConvertToDifferentRange(0, INA226_SHUNT_VOLTAGE_MX_STPS,
INA226_SHUNT_VOLTAGE_VMIN_NV, INA226_SHUNT_VOLTAGE_VMAX_NV,
shuntabsnV, &shuntVoltagenV);
// if negative, put the sign
if (negative)
shuntVoltagenV = -(shuntVoltagenV);
FILE_LOG(logDEBUG1, (" shunt voltage: %d nV\n", shuntVoltagenV));
int shuntVoltageUV = shuntVoltagenV / 1000;
FILE_LOG(logDEBUG1, (" shunt voltage: %d uV\n\n", shuntVoltageUV));
// read calibration register
FILE_LOG(logDEBUG1, (" Reading calibration reg\n"));
uint32_t calibrationRegVal = I2C_Read(deviceId, INA226_CALIBRATION_REG);
FILE_LOG(logDEBUG1, (" calibration reg: 0x%08x\n\n", calibrationRegVal));
// read it once more as this error has occured once
if (calibrationRegVal == 0xFFFF) {
FILE_LOG(logDEBUG1, (" Reading calibration reg again\n"));
calibrationRegVal = I2C_Read(deviceId, INA226_CALIBRATION_REG);
FILE_LOG(logDEBUG1, (" calibration reg: 0x%x\n", calibrationRegVal));
}
// value for current
int retval = INA226_getConvertedCurrentUnits(shuntVoltageUV, calibrationRegVal);
int retval = INA226_getConvertedCurrentUnits(shuntVoltageRegVal, INA226_Calibration_Register_Value);
FILE_LOG(logDEBUG1, (" current unit value: %d\n", retval));
FILE_LOG(logDEBUG1, (" To TEST: should be same as curent unit value\n"
" Reading current reg\n"));
// reading directly the current reg
FILE_LOG(logDEBUG1, (" Reading current reg\n"));
int cuurentRegVal = I2C_Read(deviceId, INA226_CURRENT_REG);
FILE_LOG(logDEBUG1, (" current reg: 0x%x\n", cuurentRegVal));
FILE_LOG(logDEBUG1, (" current reg: %d\n", cuurentRegVal));
// read it once more as this error has occured once
if (cuurentRegVal >= 0xFFF0) {
FILE_LOG(logDEBUG1, (" Reading current reg again\n"));
cuurentRegVal = I2C_Read(deviceId, INA226_CURRENT_REG);
FILE_LOG(logDEBUG1, (" current reg: 0x%x\n", cuurentRegVal));
FILE_LOG(logDEBUG1, (" current reg: %d\n", cuurentRegVal));
}
// should be the same
FILE_LOG(logDEBUG1, (" ===============current reg: %d, current unit cal:%d=================================\n", cuurentRegVal, retval));
// current in uA
int currentuA = retval * INA226_CURRENT_IMIN_UA;
int currentuA = cuurentRegVal * INA226_CURRENT_IMIN_UA;
FILE_LOG(logDEBUG1, (" current: %d uA\n", currentuA));
// current in mA
int currentmA = currentuA / 1000;
int currentmA = (currentuA / 1000.00) + 0.5;
FILE_LOG(logDEBUG1, (" current: %d mA\n", currentmA));
FILE_LOG(logINFO, ("Current via I2C (Device: 0x%x): %d mA\n", deviceId, currentmA));

View File

@ -200,12 +200,14 @@ void LTC2620_SetDaisy(int cmd, int data, int dacaddr, int chipIndex) {
*/
void LTC2620_Set(int cmd, int data, int dacaddr, int chipIndex) {
FILE_LOG(logDEBUG1, ("cmd:0x%x, data:%d, dacaddr:%d, chipIndex:%d\n", cmd, data, dacaddr, chipIndex));
FILE_LOG(logDEBUG1, (" ================================================\n"));
// ctb
if (LTC2620_Ndac > LTC2620_NUMCHANNELS)
LTC2620_SetDaisy(cmd, data, dacaddr, chipIndex);
// others
else
LTC2620_SetSingle(cmd, data, dacaddr);
FILE_LOG(logDEBUG1, (" ================================================\n"));
}

View File

@ -178,7 +178,7 @@ void setVchip(int val);
int getVChipToSet(enum DACINDEX ind, int val);
int getDACIndexFromADCIndex(enum ADCINDEX ind);
int getADCIndexFromDACIndex(enum DACINDEX ind);
int isPowerValid(int val);
int isPowerValid(enum DACINDEX ind, int val);
int getPower();
void setPower(enum DACINDEX ind, int val);
void powerOff();

View File

@ -800,9 +800,10 @@ int set_dac(int file_des) {
ret = FAIL;
sprintf(mess,"Could not set power. Power regulator %d exceeds voltage limit %d.\n", ind, getVLimit());
FILE_LOG(logERROR,(mess));
} else if (!isPowerValid(val)) {
} else if (!isPowerValid(serverDacIndex, val)) {
ret = FAIL;
sprintf(mess,"Could not set power. Power regulator %d should be between %d and %d mV\n", ind, POWER_RGLTR_MIN, (VCHIP_MAX_MV - VCHIP_POWER_INCRMNT));
sprintf(mess,"Could not set power. Power regulator %d should be between %d and %d mV\n",
ind, (serverDacIndex == D_PWR_IO ? VIO_MIN_MV : POWER_RGLTR_MIN), (VCHIP_MAX_MV - VCHIP_POWER_INCRMNT));
FILE_LOG(logERROR,(mess));
} else {
setPower(serverDacIndex, val);
@ -1795,7 +1796,7 @@ int set_readout_flags(int file_des) {
return printSocketReadError();
FILE_LOG(logDEBUG1, ("Setting readout flags to %d\n", arg));
#ifndef EIGERD
#if (!defined(EIGERD)) && (!defined(CHIPTESTBOARDD)) && (!defined(MOENCHD))
functionNotImplemented();
#else
// set & get
@ -1809,6 +1810,11 @@ int set_readout_flags(int file_des) {
case SAFE:
case SHOW_OVERFLOW:
case NOOVERFLOW:
#if defined(CHIPTESTBOARDD) || defined(MOENCHD)
case NORMAL_READOUT:
case DIGITAL_ONLY:
case ANALOG_AND_DIGITAL:
#endif
retval = setReadOutFlags(arg);
FILE_LOG(logDEBUG1, ("Read out flags: 0x%x\n", retval));
validate((int)arg, (int)(retval & arg), "set readout flag", HEX);
@ -2524,13 +2530,14 @@ int set_ctb_pattern(int file_des) {
#else
int addr = (int)args[0];
uint64_t word = args[1];
FILE_LOG(logDEBUG1, ("addr:0x%x word:0x%llx\n", addr, word));
if ((word == -1) || (Server_VerifyLock() == OK)) {
// address for set word should be valid (if not -1 or -2, it goes to setword)
if (addr < -2 || addr > MAX_PATTERN_LENGTH) {
ret = FAIL;
sprintf(mess, "Cannot set Pattern (Word, addr:%d). Addr must be less than %d\n",
sprintf(mess, "Cannot set Pattern (Word, addr:0x%x). Addr must be less than 0x%x\n",
addr, MAX_PATTERN_LENGTH);
FILE_LOG(logERROR, (mess));
} else {
@ -2580,6 +2587,7 @@ int set_ctb_pattern(int file_des) {
int startAddr = (int)args[1];
int stopAddr = (int)args[2];
int numLoops = (int)args[3];
FILE_LOG(logDEBUG1, ("loopLevel:%d startAddr:0x%x stopAddr:0x%x numLoops:%d word:0x%llx\n", loopLevel, startAddr, stopAddr, numLoops));
if (loopLevel < -1 || loopLevel > 2) { // -1 complete pattern
ret = FAIL;
@ -2590,8 +2598,8 @@ int set_ctb_pattern(int file_des) {
// level 0-2, addr upto patternlength + 1
else if ((loopLevel != -1) && (startAddr > (MAX_PATTERN_LENGTH + 1) || stopAddr > (MAX_PATTERN_LENGTH + 1))) {
ret = FAIL;
sprintf(mess, "Cannot set Pattern (Pattern Loop, level:%d, startaddr:%d, stopaddr:%d). "
"Addr must be less than %d\n",
sprintf(mess, "Cannot set Pattern (Pattern Loop, level:%d, startaddr:0x%x, stopaddr:0x%x). "
"Addr must be less than 0x%x\n",
loopLevel, startAddr, stopAddr, MAX_PATTERN_LENGTH + 1);
FILE_LOG(logERROR, (mess));
}
@ -2599,8 +2607,8 @@ int set_ctb_pattern(int file_des) {
//level -1, addr upto patternlength
else if ((loopLevel == -1) && (startAddr > MAX_PATTERN_LENGTH || stopAddr > MAX_PATTERN_LENGTH)) {
ret = FAIL;
sprintf(mess, "Cannot set Pattern (Pattern Loop, complete pattern, startaddr:%d, stopaddr:%d). "
"Addr must be less than %d\n",
sprintf(mess, "Cannot set Pattern (Pattern Loop, complete pattern, startaddr:0x%x, stopaddr:0x%x). "
"Addr must be less than 0x%x\n",
startAddr, stopAddr, MAX_PATTERN_LENGTH);
FILE_LOG(logERROR, (mess));
}
@ -2618,7 +2626,7 @@ int set_ctb_pattern(int file_des) {
// mode 2: wait address
else if (mode == 1) {
else if (mode == 2) {
// receive arguments
uint64_t args[2] = {-1, -1};
if (receiveData(file_des, args, sizeof(args), INT64) < 0)
@ -2631,15 +2639,16 @@ int set_ctb_pattern(int file_des) {
#else
int loopLevel = (int)args[0];
int addr = (int)args[1];
FILE_LOG(logDEBUG1, ("loopLevel:%d addr:0x%x\n", loopLevel, addr));
if ((addr == -1) || (Server_VerifyLock() == OK)) {
if (loopLevel < 0 || loopLevel > 2) {
ret = FAIL;
sprintf(mess, "Pattern (Wait Address) Level (%d) is not implemented for this detector\n", loopLevel);
sprintf(mess, "Pattern (Wait Address) Level (0x%x) is not implemented for this detector\n", loopLevel);
FILE_LOG(logERROR,(mess));
} else if (addr > (MAX_PATTERN_LENGTH + 1)) {
ret = FAIL;
sprintf(mess, "Cannot set Pattern (Wait Address, addr:%d). Addr must be less than %d\n",
sprintf(mess, "Cannot set Pattern (Wait Address, addr:0x%x). Addr must be less than 0x%x\n",
addr, MAX_PATTERN_LENGTH + 1);
FILE_LOG(logERROR, (mess));
} else {
@ -2660,7 +2669,7 @@ int set_ctb_pattern(int file_des) {
// mode 3: wait time
else if (mode == 1) {
else if (mode == 3) {
// receive arguments
uint64_t args[2] = {-1, -1};
if (receiveData(file_des, args, sizeof(args), INT64) < 0)
@ -2671,8 +2680,9 @@ int set_ctb_pattern(int file_des) {
functionNotImplemented();
return Server_SendResult(file_des, INT32, UPDATE, NULL, 0);
#else
int loopLevel = (int)args[1];
uint64_t timeval = (int)args[2];
int loopLevel = (int)args[0];
uint64_t timeval = args[1];
FILE_LOG(logDEBUG1, ("loopLevel:%d timeval:0x%lld\n", loopLevel, timeval));
if ((timeval == -1) || (Server_VerifyLock() == OK)) {
if (loopLevel < 0 || loopLevel > 2) {
@ -2684,9 +2694,9 @@ int set_ctb_pattern(int file_des) {
memset(tempName, 0, 100);
sprintf(tempName, "Pattern (Wait Time, Level:%d)", loopLevel);
FILE_LOG(logDEBUG1, ("Setting %s to 0x%llx\n", tempName, (long long int)timeval));
FILE_LOG(logDEBUG1, ("Setting %s to 0x%lld\n", tempName, (long long int)timeval));
retval64 = setPatternWaitTime(loopLevel, timeval);
FILE_LOG(logDEBUG1, ("%s: 0x%llx\n", tempName, (long long int)retval64));
FILE_LOG(logDEBUG1, ("%s: 0x%lld\n", tempName, (long long int)retval64));
validate64(timeval, retval64, tempName, HEX);
}
}
@ -2985,7 +2995,7 @@ int program_fpga(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
#ifndef JUNGFRAUD
#if (!defined(JUNGFRAUD)) && (!defined(MOENCHD)) && (!defined(CHIPTESTBOARDD))
//to receive any arguments
int n = 1;
while (n > 0)
@ -3111,7 +3121,7 @@ int reset_fpga(int file_des) {
memset(mess, 0, sizeof(mess));
FILE_LOG(logDEBUG1, ("Reset FPGA\n"));
#ifndef JUNGFRAUD
#if (!defined(JUNGFRAUD)) && (!defined(MOENCHD)) && (!defined(CHIPTESTBOARDD))
functionNotImplemented();
#else
// only set