compiles.wip

This commit is contained in:
2026-02-13 12:06:11 +01:00
parent cfeffcc36e
commit 621031f638
22 changed files with 725 additions and 785 deletions
@@ -600,9 +600,13 @@ void setupDetector() {
}
// power regulators
LOG(logINFOBLUE, ("Setting power dacs to minimum (power disabled)\n"));
LOG(logINFOBLUE,
("Setting power dacs to min dac value (power disabled)\n"));
for (int idac = NDAC_ONLY; idac < NDAC; ++idac) {
initError = initPower(idac, initErrorMessage);
if (idac == D_PWR_CHIP)
continue;
int min = (idac == D_PWR_IO) ? VIO_MIN_MV : POWER_RGLTR_MIN;
initError = setDAC(idac, min, true, initErrorMessage);
if (initError == FAIL)
return;
}
@@ -1257,123 +1261,199 @@ int getADCVpp(int mV, int *retval, char *mess) {
return OK;
}
int isDACValid(enum DACINDEX ind, int val, int mV, char *mess) {
// validate index
int validateDACIndex(enum DACINDEX ind, char *mess) {
if (ind < 0 || ind >= NDAC) {
sprintf(mess, "Could not set DAC %d. Invalid index.\n", ind);
sprintf(mess, "Could not set DAC. Invalid index %d\n", ind);
LOG(logERROR, (mess));
return FAIL;
}
// validate mV
if (mV) {
if (val == LTC2620_GetPowerDownValue()) {
sprintf(mess,
"Could not set DAC %d. Cannot use power down value and use "
"'mV'\n",
ind);
LOG(logERROR, (mess));
return FAIL;
}
// power regulators are converted to dacs at setPower with diff conv.
if (ind >= NDAC_ONLY) {
sprintf(
mess,
"Could not set DAC %d. Cannot convert to dac units for power "
"regulator at this stage. Should have been earlier.\n",
ind);
LOG(logERROR, (mess));
return FAIL;
}
}
return OK;
}
int validateDACValue(enum DACINDEX ind, int dacval, char *mess) {
// validate min value
if (val < 0) {
// dacs only: allow power down value (-100)
if ((ind < NDAC_ONLY && val != LTC2620_GetPowerDownValue()) ||
// power regulators: allow no negative values
(ind >= NDAC_ONLY)) {
sprintf(mess, "Could not set DAC %d. Invalid value %d\n", ind, val);
LOG(logERROR, (mess));
return FAIL;
}
if (dacval < 0 && dacval != LTC2620_GetPowerDownValue()) {
sprintf(
mess,
"Could not set DAC %d. Input value %d must be positive or -100.\n",
ind, dacval);
LOG(logERROR, (mess));
return FAIL;
}
if (val == LTC2620_GetPowerDownValue())
return OK;
// validate max value/ vlimit
// only dacs here in mV
if (mV) {
if (val > DAC_MAX_MV) {
sprintf(mess,
"Could not set DAC %d. Input %d mV exceeds max dac "
"value %d mV\n",
ind, val, DAC_MAX_MV);
LOG(logERROR, (mess))
return FAIL;
}
if (vLimit > 0 && val > vLimit) {
sprintf(mess,
"Could not set DAC %d. Input %d mV exceeds vLimit %d mV\n",
(int)ind, val, vLimit);
LOG(logERROR, (mess))
return FAIL;
}
return OK;
}
// dacs and power regs
if (val > LTC2620_GetMaxInput()) {
// validate max value
if (dacval > LTC2620_GetMaxInput()) {
sprintf(mess,
"Could not set DAC %d. Input %d exceeds max dac value %d\n",
(int)ind, val, LTC2620_GetMaxInput());
"Could not set DAC %d. Input value %d exceeds maximum %d \n",
ind, dacval, LTC2620_GetMaxInput());
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess) {
// validate min value
if (voltage < 0) {
sprintf(mess,
"Could not set DAC %d. Input value %d cannot be negative\n",
ind, voltage);
LOG(logERROR, (mess));
return FAIL;
}
// validate max value
if (voltage > DAC_MAX_MV) {
sprintf(
mess,
"Could not set DAC %d. Input value %d mV exceeds maximum %d mV\n",
ind, voltage, DAC_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
// validate vlimit
if (vLimit > 0 && voltage > vLimit) {
sprintf(mess,
"Could not set DAC %d. Input %d mV exceeds vLimit %d mV\n", ind,
voltage, vLimit);
LOG(logERROR, (mess))
return FAIL;
}
// vlimit check for only dacs, convert to mV (power regs checked before)
if (vLimit > 0 && ind < NDAC_ONLY) {
int dacmV = 0;
if (LTC2620_DacToVoltage(val, &dacmV) == FAIL) {
sprintf(mess,
"Could not set DAC %d. Could not convert input %d "
"to mV\n",
ind, val);
LOG(logERROR, (mess));
return FAIL;
}
if (dacmV > vLimit) {
sprintf(mess,
"Could not set DAC %d. Input %d (%d mV) exceeds "
"vLimit value %d\n",
ind, val, dacmV, vLimit);
return OK;
}
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess) {
*retval_dacval = -1;
// normal dacs
if (ind < NDAC_ONLY) {
if (LTC2620_VoltageToDac(voltage, retval_dacval) == FAIL) {
sprintf(
mess,
"Could not set DAC %d. Could not convert %d mV to dac units.\n",
ind, voltage);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
// vchip
if (ind == D_PWR_CHIP) {
if (ConvertToDifferentRange(
VCHIP_MIN_MV, VCHIP_MAX_MV, LTC2620_GetMaxInput(),
LTC2620_GetMinInput(), voltage, retval_dacval) == FAIL) {
sprintf(mess,
"Could not set DAC %d (vchip). Could not convert %d mV to "
"dac units.\n",
ind, voltage);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
// power dacs
if (ConvertToDifferentRange(POWER_RGLTR_MIN, POWER_RGLTR_MAX,
LTC2620_GetMaxInput(), LTC2620_GetMinInput(),
voltage, retval_dacval) == FAIL) {
sprintf(mess,
"Could not set DAC %d. Could not convert %d mV to dac units.\n",
ind, voltage);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess) {
*retval_voltage = -1;
// normal dacs
if (ind < NDAC_ONLY) {
if (LTC2620_DacToVoltage(dacval, retval_voltage) == FAIL) {
sprintf(
mess,
"Could not get DAC %d. Could not convert %d dac units to mV\n",
ind, dacval);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
// vchip
if (ind == D_PWR_CHIP) {
if (ConvertToDifferentRange(
LTC2620_GetMaxInput(), LTC2620_GetMinInput(), VCHIP_MIN_MV,
VCHIP_MAX_MV, dacval, retval_voltage) == FAIL) {
sprintf(mess,
"Could not get DAC %d (vchip). Could not convert %d dac "
"units to mV\n",
ind, dacval);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
// power dacs
if (ConvertToDifferentRange(LTC2620_GetMaxInput(), LTC2620_GetMinInput(),
POWER_RGLTR_MIN, POWER_RGLTR_MAX, dacval,
retval_voltage) == FAIL) {
sprintf(mess,
"Could not get DAC %d. Could not convert %d dac units to mV\n",
ind, dacval);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess) {
*retval = -1;
if (validateDACIndex(ind, mess) == FAIL)
return FAIL;
int dacval = dacValues[ind];
if (dacval == LTC2620_GetPowerDownValue()) {
LOG(logWARNING, ("DAC %d is in power down mode.\n", ind));
*retval = dacval;
return OK;
}
if (mV) {
if (convertDACValueToVoltage(ind, dacval, retval, mess) == FAIL)
return FAIL;
return OK;
}
*retval = dacval;
return OK;
}
int setDAC(enum DACINDEX ind, int val, bool mV, char *mess) {
LOG(logINFO,
("Setting DAC %d: %d %s \n", ind, val, (mV ? "mV" : "dac units")));
if (validateDACIndex(ind, mess) == FAIL)
return FAIL;
int dacval = val;
if (mV) {
if (validateDACVoltage(ind, val, mess) == FAIL)
return FAIL;
if (convertVoltageToDACValue(ind, val, &dacval, mess) == FAIL)
return FAIL;
}
if (writeDACSpi(ind, dacval, mess) == FAIL)
return FAIL;
return OK;
}
int setDAC(enum DACINDEX ind, int val, int mV, char *mess) {
if (isDACValid(ind, val, mV, mess) == FAIL)
int writeDACSpi(enum DACINDEX ind, int dacval, char *mess) {
if (validateDACValue(ind, dacval, mess) == FAIL)
return FAIL;
LOG(logINFO, ("Setting dac[%d]: %d %s \n", (int)ind, val,
(mV ? "mV" : "dac units")));
// dacs only, mV, convert to dac value
int dacval = val;
if (mV && ind < NDAC) {
if (LTC2620_VoltageToDac(val, &dacval) == FAIL) {
sprintf(
mess,
"Could not set DAC %d. Could not convert %d mV to dac units.\n",
ind, val);
LOG(logERROR, (mess));
return FAIL;
}
}
if (LTC2620_SetDACValue((int)ind, dacval) == FAIL) {
sprintf(mess, "Could not set DAC %d. Failed to convert\n", ind);
sprintf(mess, "Could not set DAC %d.\n", ind);
LOG(logERROR, (mess));
return FAIL;
}
@@ -1382,42 +1462,6 @@ int setDAC(enum DACINDEX ind, int val, int mV, char *mess) {
return OK;
}
int getDAC(enum DACINDEX ind, int mV, int *retval, char *mess) {
// validate index
if (ind < 0 || ind > NDAC) {
sprintf(mess, "Could not get DAC %d. Invalid index.\n", ind);
LOG(logERROR, (mess));
return FAIL;
}
// validate mV
if (mV && ind >= NDAC_ONLY) {
sprintf(mess,
"Could not get DAC %d. Cannot convert to dac units for power "
"regulator at this stage. Should have been earlier.\n",
ind);
LOG(logERROR, (mess));
return FAIL;
}
// get in dac units
if (!mV) {
LOG(logDEBUG1, ("Getting DAC %d : %d dac\n", ind, dacValues[ind]));
*retval = dacValues[ind];
return OK;
}
// convert to mV
*retval = -1;
if (LTC2620_DacToVoltage(dacValues[ind], retval) == FAIL) {
sprintf(mess,
"Could not get DAC %d. Could not convert %d dac units to mV\n",
ind, dacValues[ind]);
LOG(logERROR, (mess));
return FAIL;
}
LOG(logDEBUG1,
("Getting DAC %d : %d dac (%d mV)\n", ind, dacValues[ind], *retval));
return OK;
}
int getVLimit() { return vLimit; }
int setVLimit(int val, char *mess) {
@@ -1430,129 +1474,137 @@ int setVLimit(int val, char *mess) {
return OK;
}
int getVchip(int *retval, char *mess) {
*retval = dacValues[D_PWR_CHIP];
// not set yet
if (*retval == -1) {
LOG(logWARNING, ("Retrieving Vchip that has not been set yet.\n"));
return OK;
}
if (*retval == LTC2620_GetPowerDownValue()) {
LOG(logWARNING, ("Vchip dac at power down value\n"));
return OK;
}
// dac to voltage
int dacval = dacValues[D_PWR_CHIP];
if (ConvertToDifferentRange(LTC2620_GetMaxInput(), LTC2620_GetMinInput(),
VCHIP_MIN_MV, VCHIP_MAX_MV, dacval,
retval) == FAIL) {
sprintf(mess, "Could not convert to voltage. Input value for vchip "
"outside bounds.\n");
int validateVchip(int val, char *mess) {
if (val < VCHIP_MIN_MV || val > VCHIP_MAX_MV) {
sprintf(mess,
"Invalid vchip value %d mV. Must be between %d and %d mV\n",
val, VCHIP_MIN_MV, VCHIP_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
if (vLimit > 0 && val > vLimit) {
sprintf(mess, "Invalid vchip value %d mV. Exceeds vLimit %d mV\n", val,
vLimit);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int getVchip(int *retval, char *mess) {
if (getDAC(D_PWR_CHIP, true, retval, mess) == FAIL)
return FAIL;
LOG(logDEBUG1, ("Vchip: %d\n", *retval));
return OK;
}
int setVchip(int val, char *mess) {
if (val < 0) {
sprintf(mess, "Could not set vchip. Invalid value: %d\n", val);
LOG(logERROR, (mess));
return FAIL;
}
LOG(logINFOBLUE, ("Setting Vchip to %d mV\n", val));
if (validateVchip(val, mess) == FAIL)
return FAIL;
// calculate dac value to set
int dacval = LTC2620_GetPowerDownValue();
if (val != LTC2620_GetPowerDownValue()) {
// convert voltage to dac
if (ConvertToDifferentRange(VCHIP_MIN_MV, VCHIP_MAX_MV,
LTC2620_GetMaxInput(),
LTC2620_GetMinInput(), // min val is max V
val, &dacval) == FAIL) {
sprintf(mess,
"\tVChip %d mV invalid. Is not between %d and %d mV\n", val,
VCHIP_MIN_MV, VCHIP_MAX_MV);
return FAIL;
}
}
LOG(logINFO,
("Setting Vchip (DAC %d): %d dac (%d mV)\n", D_PWR_CHIP, dacval, val));
if (setDAC(D_PWR_CHIP, dacval, 0, mess) == FAIL)
if (setDAC(D_PWR_CHIP, val, true, mess) == FAIL)
return FAIL;
if (dacValues[D_PWR_CHIP] != dacval) {
sprintf(mess, "Could not set vchip. Set %d, got %d\n", val,
dacValues[D_PWR_CHIP]);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int getVChipToSet(enum DACINDEX ind, int val, int *vchip, char *mess) {
LOG(logDEBUG1, ("Calculating vchip to set\n"));
enum PWRINDEX pwrIndex = 0;
char *powerNames[] = {PWR_NAMES};
// validate index
if (getPowerIndex(ind, &pwrIndex, mess) == FAIL)
return FAIL;
int getVchipToSet(enum DACINDEX ind, int pwr_val, int *retval_vchip,
char *mess) {
// get the max of all the power regulators
int max = 0;
for (int ipwr = NDAC_ONLY; ipwr <= NDAC; ++ipwr) {
int v = 0;
if (ipwr == D_PWR_CHIP)
continue;
int val = 0;
// current index, use the value to be set
if (ipwr == (int)ind) {
v = val;
val = pwr_val;
} else {
if (getPower(ind, &v, mess) == FAIL)
if (getPower(ipwr, &val, mess) == FAIL)
return FAIL;
}
if (v > max)
max = v;
if (val > max)
max = val;
}
// increment to get vchip value
*vchip = max + VCHIP_POWER_INCRMNT;
*retval_vchip = max + VCHIP_POWER_INCRMNT;
// validate vchip min and max
if (*vchip < VCHIP_MIN_MV)
*vchip = VCHIP_MIN_MV;
// this shouldnt happen as isPowerValid should have already given error
if (*vchip > VCHIP_MAX_MV) {
sprintf(mess,
" Could not set %s. Vchip value to set %d is beyond its "
"maximum %d\n",
powerNames[pwrIndex], *vchip, VCHIP_MAX_MV);
// use min value, dont complain
if (*retval_vchip < VCHIP_MIN_MV)
*retval_vchip = VCHIP_MIN_MV;
// max checked earlier, should not happen
if (*retval_vchip > VCHIP_MAX_MV) {
enum PWRINDEX pwrIndex = 0;
if (getPowerIndex(ind, &pwrIndex, mess) == FAIL)
return FAIL;
char *powerNames[] = {PWR_NAMES};
sprintf(
mess,
"Could not set %s. Vchip value to set %d is beyond itsmaximum %d\n",
powerNames[pwrIndex], *retval_vchip, VCHIP_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
LOG(logDEBUG1, ("\tVchip to set:%d\n", *vchip));
LOG(logDEBUG1, ("\tVchip to set:%d\n", *retval_vchip));
return OK;
}
int isPowerValid(enum PWRINDEX ind, int val, char *mess) {
int validatePower(enum PWRINDEX ind, int val, char *mess) {
char *powerNames[] = {PWR_NAMES};
// validate min value
int min = (ind == PWR_IO) ? VIO_MIN_MV : POWER_RGLTR_MIN;
// allowed only upto vchip max - 200
int max = (VCHIP_MAX_MV - VCHIP_POWER_INCRMNT);
// also checking vlimit if set
if (vLimit > 0 && vLimit < max)
max = vLimit;
if (val != 0 && (val < min || val > max)) {
if (val < min) {
sprintf(
mess,
"Could not set %s. Invalid value. Must be between %d and %d mV\n",
powerNames[ind], min, max);
"Could not set %s. Input value %d mV must be greater than %d mV.\n",
powerNames[ind], val, min);
LOG(logERROR, (mess));
return FAIL;
}
// validate max value
int max = (VCHIP_MAX_MV - VCHIP_POWER_INCRMNT);
if (val > max) {
sprintf(
mess,
"Could not set %s. Input value %d mV must be less than %d mV.\n",
powerNames[ind], val, max);
LOG(logERROR, (mess));
return FAIL;
}
// validate vlimit
if (vLimit > 0 && val > vLimit) {
sprintf(mess, "Could not set %s. Input %d mV exceeds vLimit %d mV\n",
powerNames[ind], val, vLimit);
LOG(logERROR, (mess))
return FAIL;
}
return OK;
}
// for power rail index and name debugging
int getPowerIndex(enum DACINDEX ind, enum PWRINDEX *pwrIndex, char *mess) {
*pwrIndex = 0;
switch (ind) {
case D_PWR_IO:
*pwrIndex = PWR_IO;
break;
case D_PWR_A:
*pwrIndex = PWR_A;
break;
case D_PWR_B:
*pwrIndex = PWR_B;
break;
case D_PWR_C:
*pwrIndex = PWR_C;
break;
case D_PWR_D:
*pwrIndex = PWR_D;
break;
default:
sprintf(mess, "Index %d has no power index\n", ind);
LOG(logERROR, (mess));
return FAIL;
}
@@ -1626,106 +1678,37 @@ int getPowerRail(enum PWRINDEX ind, int *retval, char *mess) {
return OK;
}
// for power rail index and name debugging
int getPowerIndex(enum DACINDEX ind, enum PWRINDEX *pwrIndex, char *mess) {
*pwrIndex = 0;
switch (ind) {
case D_PWR_IO:
*pwrIndex = PWR_IO;
break;
case D_PWR_A:
*pwrIndex = PWR_A;
break;
case D_PWR_B:
*pwrIndex = PWR_B;
break;
case D_PWR_C:
*pwrIndex = PWR_C;
break;
case D_PWR_D:
*pwrIndex = PWR_D;
break;
default:
sprintf(mess, "Index %d has no power index\n", ind);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertPowerRegDACtoVoltage(enum PWRINDEX ind, int val, int *retval,
char *mess) {
char *powerNames[] = {PWR_NAMES};
if (ConvertToDifferentRange(LTC2620_GetMaxInput(), LTC2620_GetMinInput(),
POWER_RGLTR_MIN, POWER_RGLTR_MAX, val,
retval) == FAIL) {
sprintf(mess, "Could not get %s. Could not convert %d dac to mV\n",
powerNames[ind], val);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertPowerRegVoltagetoDAC(enum PWRINDEX ind, int val, int *retval,
char *mess) {
char *powerNames[] = {PWR_NAMES};
if (ConvertToDifferentRange(POWER_RGLTR_MIN, POWER_RGLTR_MAX,
LTC2620_GetMaxInput(), LTC2620_GetMinInput(),
val, retval) == FAIL) {
sprintf(
mess,
"Could not set %s. Invalid value %d mV to convert to dac units.\n",
powerNames[ind], val);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int getPower(enum DACINDEX ind, int *retval, char *mess) {
enum PWRINDEX pwrIndex = 0;
*retval = -1;
// validate index
if (getPowerIndex(ind, &pwrIndex, mess) == FAIL)
return FAIL;
// powered enable off
int pwrRetval = 0;
if (getPowerRail(pwrIndex, &pwrRetval, mess) == FAIL)
// if powered off, return 0
if (getPowerRail(pwrIndex, retval, mess) == FAIL)
return FAIL;
if (pwrRetval == 0) {
*retval = 0;
if (*retval == 0) {
return OK;
}
// to mV
if (convertPowerRegDACtoVoltage(pwrIndex, dacValues[ind], retval, mess) ==
FAIL)
if (getDAC(ind, true, retval, mess) == FAIL)
return FAIL;
return OK;
}
int setPower(enum DACINDEX ind, int val, char *mess) {
enum PWRINDEX pwrIndex = 0;
char *powerNames[] = {PWR_NAMES};
// validate index
if (getPowerIndex(ind, &pwrIndex, mess) == FAIL)
return FAIL;
// validate values (invalidate power down)
if (isPowerValid(pwrIndex, val, mess) == FAIL)
return FAIL;
char *powerNames[] = {PWR_NAMES};
LOG(logINFOBLUE, ("Setting %s to %d mV\n", powerNames[pwrIndex], val));
// get vchip to set vchip before setting power regulator
// calculate now before power off
if (validatePower(pwrIndex, val, mess) == FAIL)
return FAIL;
// compute vchip to set before powering off
int vchip = 0;
if (getVChipToSet(ind, val, &vchip, mess) == FAIL)
if (getVchipToSet(ind, val, &vchip, mess) == FAIL)
return FAIL;
if (DisablePowerRail(pwrIndex, mess) == FAIL)
@@ -1734,44 +1717,13 @@ int setPower(enum DACINDEX ind, int val, char *mess) {
if (setVchip(vchip, mess) == FAIL)
return FAIL;
// convert to dac units
int dacval = val;
if (val != 0) {
if (convertPowerRegVoltagetoDAC(pwrIndex, val, &dacval, mess) == FAIL)
return FAIL;
LOG(logINFO, ("Setting %s (DAC %d): %d dac (%d mV)\n",
powerNames[pwrIndex], dacval, val));
if (setDAC(ind, dacval, 0, mess) == FAIL)
if (setDAC(ind, val, true, mess) == FAIL)
return FAIL;
if (EnablePowerRail(pwrIndex, mess) == FAIL)
return FAIL;
}
return OK;
}
int initPower(enum DACINDEX ind, char *mess) {
if (ind == D_PWR_CHIP)
return OK;
enum PWRINDEX pwrIndex = 0;
if (getPowerIndex(ind, &pwrIndex, mess) == FAIL)
return FAIL;
char *powerNames[] = {PWR_NAMES};
int dacval = 0;
int min = (ind == D_PWR_IO) ? VIO_MIN_MV : POWER_RGLTR_MIN;
LOG(logINFO, ("Initializing %s to %d mV (power disabled)\n",
powerNames[pwrIndex], min));
if (convertPowerRegVoltagetoDAC(pwrIndex, min, &dacval, mess) == FAIL)
return FAIL;
if (setDAC(ind, dacval, 0, mess) == FAIL)
return FAIL;
return OK;
}
@@ -1779,7 +1731,6 @@ void powerOff() {
uint32_t addr = POWER_REG;
LOG(logINFO, ("Powering off all voltage regulators\n"));
bus_w(addr, bus_r(addr) & (~POWER_ENBL_VLTG_RGLTR_MSK));
LOG(logDEBUG1, ("Power Register: 0x%08x\n", bus_r(addr)));
}
int getADC(enum ADCINDEX ind) {
@@ -7,6 +7,7 @@
#include "blackfin.h"
#include "programViaBlackfin.h"
#include <stdbool.h>
#include <stdio.h> // FILE
#include <stdlib.h>
#include <sys/types.h>
@@ -126,31 +127,36 @@ enum detectorSettings getSettings();
int setADCVpp(int val, int mV, char *mess);
int getADCVpp(int mV, int *retval, char *mess);
int isDACValid(enum DACINDEX ind, int val, int mV, char *mess);
int setDAC(enum DACINDEX ind, int val, int mV, char *mess);
int getDAC(enum DACINDEX ind, int mV, int *retval, char *mess);
int validateDACIndex(enum DACINDEX ind, char *mess);
int validateDACValue(enum DACINDEX ind, int dacval, char *mess);
int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess);
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess);
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess);
int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess);
/** @param val value can be in mV or dac units */
int setDAC(enum DACINDEX ind, int val, bool mV, char *mess);
/** @param dacval in dac units */
int writeDACSpi(enum DACINDEX ind, int dacval, char *mess);
int getVLimit();
int setVLimit(int val, char *mess);
int validateVchip(int val, char *mess);
int getVchip(int *retval, char *mess);
int setVchip(int val, char *mess);
int getVChipToSet(enum DACINDEX ind, int val, int *vchip, char *mess);
int getVchipToSet(enum DACINDEX ind, int pwr_val, int *retval_vchip,
char *mess);
int validatePower(enum PWRINDEX ind, int val, char *mess);
int getPowerIndex(enum DACINDEX ind, enum PWRINDEX *pwrIndex, char *mess);
int getPowerRailMask(enum PWRINDEX ind, uint32_t *mask, char *mess);
int EnablePowerRail(enum PWRINDEX ind, char *mess);
int DisablePowerRail(enum PWRINDEX ind, char *mess);
int getPowerRail(enum PWRINDEX ind, int *retval, char *mess);
int isPowerValid(enum PWRINDEX ind, int val, char *mess);
int getPowerIndex(enum DACINDEX ind, enum PWRINDEX *pwrIndex, char *mess);
int convertPowerRegDACtoVoltage(enum PWRINDEX ind, int val, int *retval,
char *mess);
int convertPowerRegVoltagetoDAC(enum PWRINDEX ind, int val, int *retval,
char *mess);
int getPower(enum DACINDEX ind, int *retval, char *mess);
int setPower(enum DACINDEX ind, int val, char *mess);
int initPower(enum DACINDEX ind, char *mess);
void powerOff();
int getADC(enum ADCINDEX ind);
@@ -1450,7 +1450,7 @@ int validateDACValue(enum DACINDEX ind, int dacval, char *mess) {
// validate max value
if (dacval > LTC2620_MAX_VAL) {
sprintf(mess,
"Could not set DAC %s. Input value %d exceed maximum %d \n",
"Could not set DAC %s. Input value %d exceeds maximum %d \n",
dacNames[ind], dacval, LTC2620_MAX_VAL);
LOG(logERROR, (mess));
return FAIL;
@@ -1470,27 +1470,33 @@ int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess) {
}
// validate max value
if (voltage > DAC_MAX_MV) {
sprintf(mess,
"Could not set DAC %s. Input value %d mV exceed maximum %d mV\n", dacNames[ind], voltage, DAC_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int* retval_dacval, char *mess) {
char *dacNames[] = {DAC_NAMES};
if (ConvertToDifferentRange(DAC_MIN_MV, DAC_MAX_MV, LTC2620_MIN_VAL, LTC2620_MAX_VAL, voltage, retval_dacval) == FAIL) {
sprintf(
mess,
"Could not set DAC %s. Could not convert %d mV to dac units.\n", dacNames[ind], voltage);
"Could not set DAC %s. Input value %d mV exceeds maximum %d mV\n",
dacNames[ind], voltage, DAC_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int* retval_voltage, char *mess) {
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess) {
char *dacNames[] = {DAC_NAMES};
if (ConvertToDifferentRange(DAC_MIN_MV, DAC_MAX_MV, LTC2620_MIN_VAL,
LTC2620_MAX_VAL, voltage,
retval_dacval) == FAIL) {
sprintf(mess,
"Could not set DAC %s. Could not convert %d mV to dac units.\n",
dacNames[ind], voltage);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess) {
*retval_voltage = -1;
if (ConvertToDifferentRange(LTC2620_MIN_VAL, LTC2620_MAX_VAL, DAC_MIN_MV,
DAC_MAX_MV, dacval, retval_voltage) == FAIL) {
@@ -1500,7 +1506,7 @@ int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int* retval_voltage,
dacNames[ind], dacval);
LOG(logERROR, (mess));
return FAIL;
}
}
return OK;
}
@@ -1523,13 +1529,12 @@ int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess) {
return OK;
}
// uses LTC2620 with 2.048V (implementation different to others not bit banging)
int setDAC(enum DACINDEX ind, int val, bool mV, char *mess) {
{
char *dacNames[] = {DAC_NAMES};
LOG(logINFO, ("Setting DAC %s: %d %s \n", dacNames[ind], val,
(mV ? "mV" : "dac units")));
(mV ? "mV" : "dac units")));
}
if (ind == E_VTHRESHOLD) {
@@ -1538,7 +1543,7 @@ int setDAC(enum DACINDEX ind, int val, bool mV, char *mess) {
if (validateDACIndex(ind, mess) == FAIL)
return FAIL;
int dacval = val;
if (mV) {
if (validateDACVoltage(ind, val, mess) == FAIL)
@@ -1565,7 +1570,8 @@ int writeDACSpi(enum DACINDEX ind, int dacval, char *mess) {
sharedMemory_lockLocalLink();
if (!Feb_Control_SetDAC(ind, dacval)) {
char *dacNames[] = {DAC_NAMES};
sprintf(mess, "Could not set DAC %s. Trouble writing to register\n", dacNames[ind]);
sprintf(mess, "Could not set DAC %s. Trouble writing to register\n",
dacNames[ind]);
LOG(logERROR, (mess));
sharedMemory_unlockLocalLink();
return FAIL;
@@ -1576,11 +1582,9 @@ int writeDACSpi(enum DACINDEX ind, int dacval, char *mess) {
#endif
}
int setThresholdDACs(int val, bool mV, char *mess) {
enum DACINDEX indices[5] = {E_VCMP_LL, E_VCMP_LR, E_VCMP_RL, E_VCMP_RR, E_VCP};
enum DACINDEX indices[5] = {E_VCMP_LL, E_VCMP_LR, E_VCMP_RL, E_VCMP_RR,
E_VCP};
for (int i = 1; i != 5; ++i) {
if (setDAC(indices[i], val, mV, mess) == FAIL)
return FAIL;
@@ -1589,21 +1593,24 @@ int setThresholdDACs(int val, bool mV, char *mess) {
}
int getThresholdDACs(bool mV, int *retval, char *mess) {
enum DACINDEX indices[5] = {E_VCMP_LL, E_VCMP_LR, E_VCMP_RL, E_VCMP_RR, E_VCP};
enum DACINDEX indices[5] = {E_VCMP_LL, E_VCMP_LR, E_VCMP_RL, E_VCMP_RR,
E_VCP};
int retvals[5] = {0};
*retval = -1; // default to mismatch
for (int i = 0; i != 5; ++i) {
if (getDAC(indices[i], mV, &retvals[i], mess) == FAIL)
return FAIL;
// set retval to first value
if (*retval == -1) {
*retval = retvals[i];
}
}
// other values should match the first value
else if (retvals[i] != retvals[0]) {
char *dacNames[] = {DAC_NAMES};
LOG(logWARNING, ("Vthreshold mismatch.%s:%d %s:%d\n", dacNames[indices[i]], retvals[i], dacNames[indices[0]], retvals[0]));
LOG(logWARNING,
("Vthreshold mismatch.%s:%d %s:%d\n", dacNames[indices[i]],
retvals[i], dacNames[indices[0]], retvals[0]));
*retval = -1;
return OK;
}
@@ -3,10 +3,10 @@
#include "sls/sls_detector_defs.h"
#include "slsDetectorServer_defs.h" // DAC_INDEX, ADC_INDEX, also include RegisterDefs.h
#include <stdbool.h>
#include <stdio.h> // FILE
#include <stdlib.h>
#include <sys/types.h>
#include <stdbool.h>
/****************************************************
This functions are used by the slsDetectroServer_funcs interface.
@@ -127,8 +127,10 @@ int setThresholdEnergy(int ev);
int validateDACIndex(enum DACINDEX ind, char *mess);
int validateDACValue(enum DACINDEX ind, int dacval, char *mess);
int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess);
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int* retval_dacval, char *mess);
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int* retval_voltage, char *mess);
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess);
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess);
int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess);
/** @param val value can be in mV or dac units */
int setDAC(enum DACINDEX ind, int val, bool mV, char *mess);
@@ -571,7 +571,8 @@ int resetToDefaultDacs(int hardReset, char *mess) {
LOG(logINFOBLUE, ("Setting Default Dac values\n"));
for (int i = 0; i < NDAC; ++i) {
if (defaultDacValues[i] != -1) {
if (setDAC((enum DACINDEX)i, defaultDacValues[i], false, mess) == FAIL)
if (setDAC((enum DACINDEX)i, defaultDacValues[i], false, mess) ==
FAIL)
return FAIL;
}
}
@@ -1543,7 +1544,7 @@ int validateDACValue(enum DACINDEX ind, int dacval, char *mess) {
// validate max value
if (dacval > LTC2620_D_GetMaxInput()) {
sprintf(mess,
"Could not set DAC %s. Input value %d exceed maximum %d \n",
"Could not set DAC %s. Input value %d exceeds maximum %d \n",
dacNames[ind], dacval, LTC2620_D_GetMaxInput());
LOG(logERROR, (mess));
return FAIL;
@@ -1563,28 +1564,31 @@ int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess) {
}
// validate max value
if (voltage > DAC_MAX_MV) {
sprintf(mess,
"Could not set DAC %s. Input value %d mV exceed maximum %d mV\n", dacNames[ind], voltage, DAC_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int* retval_dacval, char *mess) {
if (LTC2620_D_VoltageToDac(voltage, retval_dacval) == FAIL) {
char *dacNames[] = {DAC_NAMES};
sprintf(
mess,
"Could not set DAC %s. Could not convert %d mV to dac units.\n",
dacNames[ind], voltage);
"Could not set DAC %s. Input value %d mV exceeds maximum %d mV\n",
dacNames[ind], voltage, DAC_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int* retval_voltage, char *mess) {
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess) {
if (LTC2620_D_VoltageToDac(voltage, retval_dacval) == FAIL) {
char *dacNames[] = {DAC_NAMES};
sprintf(mess,
"Could not set DAC %s. Could not convert %d mV to dac units.\n",
dacNames[ind], voltage);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess) {
*retval_voltage = -1;
if (LTC2620_D_DacToVoltage(dacval, retval_voltage) == FAIL) {
char *dacNames[] = {DAC_NAMES};
@@ -1612,17 +1616,16 @@ int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess) {
return OK;
}
int setDAC(enum DACINDEX ind, int val, bool mV, char *mess) {
{
char *dacNames[] = {DAC_NAMES};
LOG(logINFO, ("Setting DAC %s: %d %s \n", dacNames[ind], val,
(mV ? "mV" : "dac units")));
(mV ? "mV" : "dac units")));
}
if (validateDACIndex(ind, mess) == FAIL)
return FAIL;
int dacval = val;
if (mV) {
if (validateDACVoltage(ind, val, mess) == FAIL)
@@ -1648,7 +1651,7 @@ int writeDACSpi(enum DACINDEX ind, int dacval, char *mess) {
LOG(logERROR, (mess));
return FAIL;
}
dacValues[ind] = dacval;
return OK;
}
@@ -6,10 +6,10 @@
#include "nios.h"
#include "programViaNios.h"
#include <stdbool.h>
#include <stdio.h> // FILE
#include <stdlib.h>
#include <sys/types.h>
#include <stdbool.h>
/****************************************************
This functions are used by the slsDetectroServer_funcs interface.
@@ -120,15 +120,16 @@ int getOnChipDAC(enum ONCHIP_DACINDEX ind, int chipIndex);
int validateDACIndex(enum DACINDEX ind, char *mess);
int validateDACValue(enum DACINDEX ind, int dacval, char *mess);
int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess);
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int* retval_dacval, char *mess);
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int* retval_voltage, char *mess);
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess);
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess);
int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess);
/** @param val value can be in mV or dac units */
int setDAC(enum DACINDEX ind, int val, bool mV, char *mess);
/** @param dacval in dac units */
int writeDACSpi(enum DACINDEX ind, int dacval, char *mess);
int getADC(enum ADCINDEX ind, int *value);
int setHighVoltage(int val, char *mess);
int getHighVoltage(int *retval, char *mess);
@@ -1358,7 +1358,7 @@ int validateDACValue(enum DACINDEX ind, int dacval, char *mess) {
// validate max value
if (dacval > LTC2620_GetMaxInput()) {
sprintf(mess,
"Could not set DAC %s. Input value %d exceed maximum %d \n",
"Could not set DAC %s. Input value %d exceeds maximum %d \n",
dacNames[ind], dacval, LTC2620_GetMaxInput());
LOG(logERROR, (mess));
return FAIL;
@@ -1378,28 +1378,31 @@ int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess) {
}
// validate max value
if (voltage > DAC_MAX_MV) {
sprintf(mess,
"Could not set DAC %s. Input value %d mV exceed maximum %d mV\n", dacNames[ind], voltage, DAC_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int* retval_dacval, char *mess) {
if (LTC2620_VoltageToDac(voltage, retval_dacval) == FAIL) {
char *dacNames[] = {DAC_NAMES};
sprintf(
mess,
"Could not set DAC %s. Could not convert %d mV to dac units.\n",
dacNames[ind], voltage);
"Could not set DAC %s. Input value %d mV exceeds maximum %d mV\n",
dacNames[ind], voltage, DAC_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int* retval_voltage, char *mess) {
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess) {
if (LTC2620_VoltageToDac(voltage, retval_dacval) == FAIL) {
char *dacNames[] = {DAC_NAMES};
sprintf(mess,
"Could not set DAC %s. Could not convert %d mV to dac units.\n",
dacNames[ind], voltage);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess) {
*retval_voltage = -1;
if (LTC2620_DacToVoltage(dacval, retval_voltage) == FAIL) {
char *dacNames[] = {DAC_NAMES};
@@ -1427,17 +1430,16 @@ int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess) {
return OK;
}
int setDAC(enum DACINDEX ind, int val, bool mV, char *mess) {
{
char *dacNames[] = {DAC_NAMES};
LOG(logINFO, ("Setting DAC %s: %d %s \n", dacNames[ind], val,
(mV ? "mV" : "dac units")));
(mV ? "mV" : "dac units")));
}
if (validateDACIndex(ind, mess) == FAIL)
return FAIL;
int dacval = val;
if (mV) {
if (validateDACVoltage(ind, val, mess) == FAIL)
@@ -1463,7 +1465,7 @@ int writeDACSpi(enum DACINDEX ind, int dacval, char *mess) {
LOG(logERROR, (mess));
return FAIL;
}
dacValues[ind] = dacval;
// ext daq ctrl
@@ -1476,7 +1478,6 @@ int writeDACSpi(enum DACINDEX ind, int dacval, char *mess) {
return OK;
}
int getADC(enum ADCINDEX ind) {
#ifdef VIRTUAL
return 0;
@@ -7,10 +7,10 @@
#include "blackfin.h"
#include "programViaBlackfin.h"
#include <stdbool.h>
#include <stdio.h> // FILE
#include <stdlib.h>
#include <sys/types.h>
#include <stdbool.h>
/****************************************************
This functions are used by the slsDetectroServer_funcs interface.
@@ -126,8 +126,10 @@ void setGainMode(enum gainMode mode);
int validateDACIndex(enum DACINDEX ind, char *mess);
int validateDACValue(enum DACINDEX ind, int dacval, char *mess);
int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess);
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int* retval_dacval, char *mess);
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int* retval_voltage, char *mess);
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess);
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess);
int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess);
/** @param val value can be in mV or dac units */
int setDAC(enum DACINDEX ind, int val, bool mV, char *mess);
@@ -934,7 +934,7 @@ int validateDACValue(enum DACINDEX ind, int dacval, char *mess) {
// validate max value
if (dacval > LTC2620_GetMaxInput()) {
sprintf(mess,
"Could not set DAC %s. Input value %d exceed maximum %d \n",
"Could not set DAC %s. Input value %d exceeds maximum %d \n",
dacNames[ind], dacval, LTC2620_GetMaxInput());
LOG(logERROR, (mess));
return FAIL;
@@ -954,28 +954,31 @@ int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess) {
}
// validate max value
if (voltage > DAC_MAX_MV) {
sprintf(mess,
"Could not set DAC %s. Input value %d mV exceed maximum %d mV\n", dacNames[ind], voltage, DAC_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int* retval_dacval, char *mess) {
if (LTC2620_VoltageToDac(voltage, retval_dacval) == FAIL) {
char *dacNames[] = {DAC_NAMES};
sprintf(
mess,
"Could not set DAC %s. Could not convert %d mV to dac units.\n",
dacNames[ind], voltage);
"Could not set DAC %s. Input value %d mV exceeds maximum %d mV\n",
dacNames[ind], voltage, DAC_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int* retval_voltage, char *mess) {
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess) {
if (LTC2620_VoltageToDac(voltage, retval_dacval) == FAIL) {
char *dacNames[] = {DAC_NAMES};
sprintf(mess,
"Could not set DAC %s. Could not convert %d mV to dac units.\n",
dacNames[ind], voltage);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess) {
*retval_voltage = -1;
if (LTC2620_DacToVoltage(dacval, retval_voltage) == FAIL) {
char *dacNames[] = {DAC_NAMES};
@@ -1003,17 +1006,16 @@ int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess) {
return OK;
}
int setDAC(enum DACINDEX ind, int val, bool mV, char *mess) {
{
char *dacNames[] = {DAC_NAMES};
LOG(logINFO, ("Setting DAC %s: %d %s \n", dacNames[ind], val,
(mV ? "mV" : "dac units")));
(mV ? "mV" : "dac units")));
}
if (validateDACIndex(ind, mess) == FAIL)
return FAIL;
int dacval = val;
if (mV) {
if (validateDACVoltage(ind, val, mess) == FAIL)
@@ -7,10 +7,10 @@
#include "blackfin.h"
#include "programViaBlackfin.h"
#include <stdbool.h>
#include <stdio.h> // FILE
#include <stdlib.h>
#include <sys/types.h>
#include <stdbool.h>
/****************************************************
This functions are used by the slsDetectroServer_funcs interface.
@@ -119,8 +119,10 @@ enum detectorSettings getSettings();
int validateDACIndex(enum DACINDEX ind, char *mess);
int validateDACValue(enum DACINDEX ind, int dacval, char *mess);
int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess);
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int* retval_dacval, char *mess);
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int* retval_voltage, char *mess);
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess);
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess);
int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess);
/** @param val value can be in mV or dac units */
int setDAC(enum DACINDEX ind, int val, bool mV, char *mess);
@@ -1312,7 +1312,8 @@ int setModule(sls_detector_module myMod, char *mess) {
for (int i = 0; i < NDAC; ++i) {
if (myMod.dacs[i] != -1) {
// set to default (ensure counter check)
if (setDAC((enum DACINDEX)i, myMod.dacs[i], false, true, mess) == FAIL) {
if (setDAC((enum DACINDEX)i, myMod.dacs[i], false, true, mess) ==
FAIL) {
// dont complain if that counter was disabled
if ((i == M_VTH1 || i == M_VTH2 || i == M_VTH3) &&
(detectorDacs[i] == DEFAULT_COUNTER_DISABLED_VTH_VAL)) {
@@ -1561,7 +1562,7 @@ int validateDACValue(enum DACINDEX ind, int dacval, char *mess) {
// validate max value
if (dacval > LTC2620_D_GetMaxInput()) {
sprintf(mess,
"Could not set DAC %s. Input value %d exceed maximum %d \n",
"Could not set DAC %s. Input value %d exceeds maximum %d \n",
dacNames[ind], dacval, LTC2620_D_GetMaxInput());
LOG(logERROR, (mess));
return FAIL;
@@ -1581,28 +1582,31 @@ int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess) {
}
// validate max value
if (voltage > DAC_MAX_MV) {
sprintf(mess,
"Could not set DAC %s. Input value %d mV exceed maximum %d mV\n", dacNames[ind], voltage, DAC_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int* retval_dacval, char *mess) {
if (LTC2620_D_VoltageToDac(voltage, retval_dacval) == FAIL) {
char *dacNames[] = {DAC_NAMES};
sprintf(
mess,
"Could not set DAC %s. Could not convert %d mV to dac units.\n",
dacNames[ind], voltage);
"Could not set DAC %s. Input value %d mV exceeds maximum %d mV\n",
dacNames[ind], voltage, DAC_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int* retval_voltage, char *mess) {
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess) {
if (LTC2620_D_VoltageToDac(voltage, retval_dacval) == FAIL) {
char *dacNames[] = {DAC_NAMES};
sprintf(mess,
"Could not set DAC %s. Could not convert %d mV to dac units.\n",
dacNames[ind], voltage);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess) {
*retval_voltage = -1;
if (LTC2620_D_DacToVoltage(dacval, retval_voltage) == FAIL) {
char *dacNames[] = {DAC_NAMES};
@@ -1619,7 +1623,7 @@ int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess) {
if (ind == M_VTHRESHOLD) {
return getThresholdDACs(mV, retval, mess);
}
if (validateDACIndex(ind, mess) == FAIL)
return FAIL;
@@ -1634,18 +1638,17 @@ int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess) {
return OK;
}
// counterEnableCheck false only if setDAC called directly from client
int setDAC(enum DACINDEX ind, int val, bool mV, bool counterCheck, char *mess) {
{
char *dacNames[] = {DAC_NAMES};
LOG(logINFO, ("Setting DAC %s: %d %s \n", dacNames[ind], val,
(mV ? "mV" : "dac units")));
(mV ? "mV" : "dac units")));
}
if (validateDACIndex(ind, mess) == FAIL)
return FAIL;
int dacval = val;
if (mV) {
if (validateDACVoltage(ind, val, mess) == FAIL)
@@ -1653,25 +1656,25 @@ int setDAC(enum DACINDEX ind, int val, bool mV, bool counterCheck, char *mess) {
if (convertVoltageToDACValue(ind, val, &dacval, mess) == FAIL)
return FAIL;
}
}
switch (ind) {
case M_VTHRESHOLD:
// all threshold dacs:
// store values for all dacs for pump probe and interpolation mode
// only set for enabled counters
return setThresholdDACs(val, mV, dacval, mess);
case M_VTH1:
case M_VTH2:
case M_VTH3:
// threshold dacs:
// store values for pump probe and interpolation mode
// change dacval for disabled counter to 2800
return setSingleThresholdDAC(ind, val, mV, dacval, counterCheck, mess);
default:
return writeDACSpi(ind, dacval, mess);
break;
}
case M_VTHRESHOLD:
// all threshold dacs:
// store values for all dacs for pump probe and interpolation mode
// only set for enabled counters
return setThresholdDACs(val, mV, dacval, mess);
case M_VTH1:
case M_VTH2:
case M_VTH3:
// threshold dacs:
// store values for pump probe and interpolation mode
// change dacval for disabled counter to 2800
return setSingleThresholdDAC(ind, val, mV, dacval, counterCheck, mess);
default:
return writeDACSpi(ind, dacval, mess);
break;
}
}
int writeDACSpi(enum DACINDEX ind, int dacval, char *mess) {
@@ -1696,21 +1699,21 @@ int writeDACSpi(enum DACINDEX ind, int dacval, char *mess) {
return OK;
}
int getCounterIndex (enum DACINDEX ind) {
int getCounterIndex(enum DACINDEX ind) {
switch (ind) {
case M_VTH1:
return 0;
case M_VTH2:
return 1;
case M_VTH3:
return 2;
default:
return -1;
case M_VTH1:
return 0;
case M_VTH2:
return 1;
case M_VTH3:
return 2;
default:
return -1;
}
}
}
int setSingleThresholdDAC(enum DACINDEX ind, int val, bool mV, int dacval, bool counterCheck, char* mess) {
int setSingleThresholdDAC(enum DACINDEX ind, int val, bool mV, int dacval,
bool counterCheck, char *mess) {
char *dacNames[] = {DAC_NAMES};
uint32_t counterMask = getCounterMask();
@@ -1724,7 +1727,7 @@ int setSingleThresholdDAC(enum DACINDEX ind, int val, bool mV, int dacval, bool
// if not disabled value, remember value
if (mV || val != DEFAULT_COUNTER_DISABLED_VTH_VAL) {
LOG(logINFO, ("Remembering %s [%d]\n", dacNames[ind], dacval));
vthEnabledVals[iCounter] = dacval;
vthEnabledVals[iCounter] = dacval;
}
// if counter disabled, change value
if (counterCheck && (counterMask & (1 << iCounter))) {
@@ -1733,11 +1736,11 @@ int setSingleThresholdDAC(enum DACINDEX ind, int val, bool mV, int dacval, bool
// set value
if (writeDACSpi(ind, dacval, mess) == FAIL)
return FAIL;
return OK;
}
int setThresholdDACs(int val, bool mV, int dacval, char* mess) {
int setThresholdDACs(int val, bool mV, int dacval, char *mess) {
char *dacNames[] = {DAC_NAMES};
int indices[] = {M_VTH1, M_VTH2, M_VTH3};
uint32_t counterMask = getCounterMask();
@@ -1745,19 +1748,19 @@ int setThresholdDACs(int val, bool mV, int dacval, char* mess) {
for (int iCounter = 0; iCounter != NCOUNTERS; ++iCounter) {
// if not disabled value, remember value
if (mV || val != DEFAULT_COUNTER_DISABLED_VTH_VAL) {
LOG(logINFO, ("Remembering %s [%d]\n", dacNames[indices[iCounter]], dacval));
vthEnabledVals[iCounter] = dacval;
LOG(logINFO,
("Remembering %s [%d]\n", dacNames[indices[iCounter]], dacval));
vthEnabledVals[iCounter] = dacval;
}
// if counter enabled, set value
if (counterMask & (1 << iCounter)) {
if (writeDACSpi(indices[iCounter], dacval, mess) == FAIL)
if (writeDACSpi(indices[iCounter], dacval, mess) == FAIL)
return FAIL;
}
}
return OK;
}
int getThresholdDACs(bool mV, int *retval, char *mess) {
int indices[] = {M_VTH1, M_VTH2, M_VTH3};
int retvals[NCOUNTERS] = {0};
@@ -1769,21 +1772,24 @@ int getThresholdDACs(bool mV, int *retval, char *mess) {
if (counterMask & (1 << i)) {
if (getDAC(indices[i], mV, &retvals[i], mess) == FAIL)
return FAIL;
}
}
// set retval to first value
if (*retval == -1) {
*retval = retvals[i];
}
}
// other values should match the first value
else if (retvals[i] != retvals[0]) {
char *dacNames[] = {DAC_NAMES};
LOG(logWARNING, ("Vthreshold mismatch.%s:%d %s:%d\n", dacNames[indices[i]], retvals[i], dacNames[indices[0]], retvals[0]));
LOG(logWARNING,
("Vthreshold mismatch.%s:%d %s:%d\n", dacNames[indices[i]],
retvals[i], dacNames[indices[0]], retvals[0]));
*retval = -1;
return OK;
}
}
if (*retval == -1) {
sprintf(mess, "All counters are disabled. Vthreshold value is undefined.\n");
sprintf(mess,
"All counters are disabled. Vthreshold value is undefined.\n");
LOG(logERROR, (mess));
return FAIL;
}
@@ -1791,7 +1797,6 @@ int getThresholdDACs(bool mV, int *retval, char *mess) {
return OK;
}
void setVthDac(int index, int enable) {
LOG(logINFO, ("\t%s vth%d\n", (enable ? "Enabling" : "Disabing"), index));
// enables (from remembered values) or disables vthx
@@ -1806,7 +1811,6 @@ void setVthDac(int index, int enable) {
writeDACSpi(vthdacs[index], value, msg);
}
int getADC(enum ADCINDEX ind, int *value) {
LOG(logDEBUG1, ("Reading FPGA temperature...\n"));
if (readParameterFromFile(TEMPERATURE_FILE_NAME, "temperature", value) ==
@@ -7,10 +7,10 @@
#include "nios.h"
#include "programViaNios.h"
#include <stdbool.h>
#include <stdio.h> // FILE
#include <stdlib.h>
#include <sys/types.h>
#include <stdbool.h>
/****************************************************
This functions are used by the slsDetectroServer_funcs interface.
@@ -137,17 +137,20 @@ void setThresholdEnergy(int counterIndex, int eV);
int validateDACIndex(enum DACINDEX ind, char *mess);
int validateDACValue(enum DACINDEX ind, int dacval, char *mess);
int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess);
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int* retval_dacval, char *mess);
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int* retval_voltage, char *mess);
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess);
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess);
int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess);
/** @param val value can be in mV or dac units */
int setDAC(enum DACINDEX ind, int val, bool mV, bool counterCheck, char *mess);
/** @param dacval in dac units */
int writeDACSpi(enum DACINDEX ind, int dacval, char *mess);
int getCounterIndex (enum DACINDEX ind);
int setSingleThresholdDAC(enum DACINDEX ind, int val, bool mV, int dacval, bool counterCheck, char* mess);
int setThresholdDACs(int val, bool mV, int dacval, char* mess);
int getCounterIndex(enum DACINDEX ind);
int setSingleThresholdDAC(enum DACINDEX ind, int val, bool mV, int dacval,
bool counterCheck, char *mess);
int setThresholdDACs(int val, bool mV, int dacval, char *mess);
int getThresholdDACs(bool mV, int *retval, char *mess);
void setVthDac(int index, int enable);
@@ -414,16 +414,20 @@ void setupDetector() {
// dacs only
LOG(logINFOBLUE, ("Powering down all dacs\n"));
for (int idac = 0; idac < NDAC_ONLY; ++idac) {
initError =
setDAC(idac, LTC2620_D_GetPowerDownValue(), 0, initErrorMessage);
initError = setDAC(idac, LTC2620_D_GetPowerDownValue(), false,
initErrorMessage);
if (initError == FAIL)
return;
}
// power regulators
LOG(logINFOBLUE, ("Setting power dacs to minimum (power disabled)\n"));
LOG(logINFOBLUE,
("Setting power dacs to min dac value (power disabled)\n"));
for (int idac = NDAC_ONLY; idac < NDAC; ++idac) {
initError = initPower(idac, initErrorMessage);
if (idac == D_PWR_EMPTY)
continue;
int min = (idac == D_PWR_IO) ? VIO_MIN_MV : POWER_RGLTR_MIN;
initError = setDAC(idac, min, true, initErrorMessage);
if (initError == FAIL)
return;
}
@@ -1161,123 +1165,171 @@ int64_t getMeasurementTime() {
}
/* parameters - dac, adc, hv */
int isDACValid(enum DACINDEX ind, int val, int mV, char *mess) {
// validate index
int validateDACIndex(enum DACINDEX ind, char *mess) {
if (ind < 0 || ind >= NDAC) {
sprintf(mess, "Could not set DAC %d. Invalid index.\n", ind);
sprintf(mess, "Could not set DAC. Invalid index %d\n", ind);
LOG(logERROR, (mess));
return FAIL;
}
// validate mV
if (mV) {
if (val == LTC2620_D_GetPowerDownValue()) {
sprintf(mess,
"Could not set DAC %d. Cannot use power down value and use "
"'mV'\n",
ind);
LOG(logERROR, (mess));
return FAIL;
}
// power regulators are converted to dacs at setPower with diff conv.
if (ind >= NDAC_ONLY) {
sprintf(
mess,
"Could not set DAC %d. Cannot convert to dac units for power "
"regulator at this stage. Should have been earlier.\n",
ind);
LOG(logERROR, (mess));
return FAIL;
}
}
return OK;
}
int validateDACValue(enum DACINDEX ind, int dacval, char *mess) {
// validate min value
if (val < 0) {
// dacs only: allow power down value (-100)
if ((ind < NDAC_ONLY && val != LTC2620_D_GetPowerDownValue()) ||
// power regulators: allow no negative values
(ind >= NDAC_ONLY)) {
sprintf(mess, "Could not set DAC %d. Invalid value %d\n", ind, val);
LOG(logERROR, (mess));
return FAIL;
}
if (dacval < 0 && dacval != LTC2620_D_GetPowerDownValue()) {
sprintf(
mess,
"Could not set DAC %d. Input value %d must be positive or -100.\n",
ind, dacval);
LOG(logERROR, (mess));
return FAIL;
}
if (val == LTC2620_D_GetPowerDownValue())
return OK;
// validate max value/ vlimit
// only dacs here in mV
if (mV) {
if (val > DAC_MAX_MV) {
sprintf(mess,
"Could not set DAC %d. Input %d mV exceeds max dac "
"value %d mV\n",
(int)ind, val, DAC_MAX_MV);
LOG(logERROR, (mess))
return FAIL;
}
if (vLimit > 0 && val > vLimit) {
sprintf(mess,
"Could not set DAC %d. Input %d mV exceeds vLimit %d mV\n",
ind, val, vLimit);
LOG(logERROR, (mess))
return FAIL;
}
return OK;
}
// dacs and power regs
if (val > LTC2620_D_GetMaxInput()) {
// validate max value
if (dacval > LTC2620_D_GetMaxInput()) {
sprintf(mess,
"Could not set DAC %d. Input %d exceeds max dac value %d\n",
ind, val, LTC2620_D_GetMaxInput());
"Could not set DAC %d. Input value %d exceeds maximum %d \n",
ind, dacval, LTC2620_D_GetMaxInput());
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess) {
// validate min value
if (voltage < 0) {
sprintf(mess,
"Could not set DAC %d. Input value %d cannot be negative\n",
ind, voltage);
LOG(logERROR, (mess));
return FAIL;
}
// validate max value
if (voltage > DAC_MAX_MV) {
sprintf(
mess,
"Could not set DAC %d. Input value %d mV exceeds maximum %d mV\n",
ind, voltage, DAC_MAX_MV);
LOG(logERROR, (mess));
return FAIL;
}
// validate vlimit
if (vLimit > 0 && voltage > vLimit) {
sprintf(mess,
"Could not set DAC %d. Input %d mV exceeds vLimit %d mV\n", ind,
voltage, vLimit);
LOG(logERROR, (mess))
return FAIL;
}
// vlimit check for only dacs, convert to mV (power regs checked before)
if (vLimit > 0 && ind < NDAC_ONLY) {
int dacmV = 0;
if (LTC2620_D_DacToVoltage(val, &dacmV) == FAIL) {
sprintf(mess,
"Could not set DAC %d. Could not convert input %d "
"to mV\n",
(int)ind, val);
LOG(logERROR, (mess));
return FAIL;
}
if (dacmV > vLimit) {
sprintf(mess,
"Could not set DAC %d. Input %d (%d mV) exceeds "
"vLimit value %d\n",
ind, val, dacmV, vLimit);
return OK;
}
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess) {
*retval_dacval = -1;
// normal dacs
if (ind < NDAC_ONLY) {
if (LTC2620_D_VoltageToDac(voltage, retval_dacval) == FAIL) {
sprintf(
mess,
"Could not set DAC %d. Could not convert %d mV to dac units.\n",
ind, voltage);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
// power dacs
if (ConvertToDifferentRange(
POWER_RGLTR_MIN, POWER_RGLTR_MAX, LTC2620_D_GetMaxInput(),
LTC2620_D_GetMinInput(), voltage, retval_dacval) == FAIL) {
sprintf(mess,
"Could not set DAC %d. Could not convert %d mV to dac units.\n",
ind, voltage);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess) {
*retval_voltage = -1;
// normal dacs
if (ind < NDAC_ONLY) {
if (LTC2620_D_DacToVoltage(dacval, retval_voltage) == FAIL) {
sprintf(
mess,
"Could not get DAC %d. Could not convert %d dac units to mV\n",
ind, dacval);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
// power dacs
if (ConvertToDifferentRange(
LTC2620_D_GetMaxInput(), LTC2620_D_GetMinInput(), POWER_RGLTR_MIN,
POWER_RGLTR_MAX, dacval, retval_voltage) == FAIL) {
sprintf(mess,
"Could not get DAC %d. Could not convert %d dac units to mV\n",
ind, dacval);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess) {
*retval = -1;
if (validateDACIndex(ind, mess) == FAIL)
return FAIL;
int dacval = dacValues[ind];
if (dacval == LTC2620_D_GetPowerDownValue()) {
LOG(logWARNING, ("DAC %d is in power down mode.\n", ind));
*retval = dacval;
return OK;
}
if (mV) {
if (convertDACValueToVoltage(ind, dacval, retval, mess) == FAIL)
return FAIL;
return OK;
}
*retval = dacval;
return OK;
}
int setDAC(enum DACINDEX ind, int val, bool mV, char *mess) {
LOG(logINFO,
("Setting DAC %d: %d %s \n", ind, val, (mV ? "mV" : "dac units")));
if (validateDACIndex(ind, mess) == FAIL)
return FAIL;
int dacval = val;
if (mV) {
if (validateDACVoltage(ind, val, mess) == FAIL)
return FAIL;
if (convertVoltageToDACValue(ind, val, &dacval, mess) == FAIL)
return FAIL;
}
if (writeDACSpi(ind, dacval, mess) == FAIL)
return FAIL;
return OK;
}
int setDAC(enum DACINDEX ind, int val, int mV, char *mess) {
if (isDACValid(ind, val, mV, mess) == FAIL)
int writeDACSpi(enum DACINDEX ind, int dacval, char *mess) {
if (validateDACValue(ind, dacval, mess) == FAIL)
return FAIL;
LOG(logINFO, ("Setting dac[%d]: %d %s \n", (int)ind, val,
(mV ? "mV" : "dac units")));
// dacs only, mV, convert to dac value
int dacval = val;
if (mV && ind < NDAC_ONLY) {
if (LTC2620_D_VoltageToDac(val, &dacval) == FAIL) {
sprintf(
mess,
"Could not set DAC %d. Could not convert %d mV to dac units.\n",
ind, val);
LOG(logERROR, (mess));
return FAIL;
}
}
if (LTC2620_D_SetDACValue((int)ind, dacval) == FAIL) {
sprintf(mess, "Could not set DAC %d. Failed to convert\n", ind);
sprintf(mess, "Could not set DAC %d.\n", ind);
LOG(logERROR, (mess));
return FAIL;
}
@@ -1286,42 +1338,6 @@ int setDAC(enum DACINDEX ind, int val, int mV, char *mess) {
return OK;
}
int getDAC(enum DACINDEX ind, int mV, int *retval, char *mess) {
// validate index
if (ind < 0 || ind > NDAC) {
sprintf(mess, "Could not get DAC %d. Invalid index.\n", ind);
LOG(logERROR, (mess));
return FAIL;
}
// validate mV
if (mV && ind >= NDAC_ONLY) {
sprintf(mess,
"Could not get DAC %d. Cannot convert to dac units for power "
"regulator at this stage. Should have been earlier.\n",
ind);
LOG(logERROR, (mess));
return FAIL;
}
// get in dac units
if (!mV) {
LOG(logDEBUG1, ("Getting DAC %d : %d dac\n", ind, dacValues[ind]));
*retval = dacValues[ind];
return OK;
}
// convert to mV
*retval = -1;
if (LTC2620_D_DacToVoltage(dacValues[ind], retval) == FAIL) {
sprintf(mess,
"Could not get DAC %d. Could not convert %d dac units to mV\n",
ind, dacValues[ind]);
LOG(logERROR, (mess));
return FAIL;
}
LOG(logDEBUG1,
("Getting DAC %d : %d dac (%d mV)\n", ind, dacValues[ind], *retval));
return OK;
}
int getVLimit() { return vLimit; }
int setVLimit(int val, char *mess) {
@@ -1334,20 +1350,58 @@ int setVLimit(int val, char *mess) {
return OK;
}
int isPowerValid(enum PWRINDEX ind, int val, char *mess) {
int validatePower(enum PWRINDEX ind, int val, char *mess) {
char *powerNames[] = {PWR_NAMES};
// validate min value
int min = (ind == PWR_IO) ? VIO_MIN_MV : POWER_RGLTR_MIN;
int max = POWER_RGLTR_MAX;
// also checking vlimit if set
if (vLimit > 0 && vLimit < max)
max = vLimit;
if (val != 0 && (val < min || val > max)) {
if (val < min) {
sprintf(
mess,
"Could not set %s. Invalid value. Must be between %d and %d mV\n",
powerNames[ind], min, max);
"Could not set %s. Input value %d mV must be greater than %d mV.\n",
powerNames[ind], val, min);
LOG(logERROR, (mess));
return FAIL;
}
// validate max value
if (val > POWER_RGLTR_MAX) {
sprintf(
mess,
"Could not set %s. Input value %d mV must be less than %d mV.\n",
powerNames[ind], val, POWER_RGLTR_MAX);
LOG(logERROR, (mess));
return FAIL;
}
// validate vlimit
if (vLimit > 0 && val > vLimit) {
sprintf(mess, "Could not set %s. Input %d mV exceeds vLimit %d mV\n",
powerNames[ind], val, vLimit);
LOG(logERROR, (mess))
return FAIL;
}
return OK;
}
// for power rail index and name debugging
int getPowerIndex(enum DACINDEX ind, enum PWRINDEX *pwrIndex, char *mess) {
*pwrIndex = 0;
switch (ind) {
case D_PWR_IO:
*pwrIndex = PWR_IO;
break;
case D_PWR_A:
*pwrIndex = PWR_A;
break;
case D_PWR_B:
*pwrIndex = PWR_B;
break;
case D_PWR_C:
*pwrIndex = PWR_C;
break;
case D_PWR_D:
*pwrIndex = PWR_D;
break;
default:
sprintf(mess, "Index %d has no power index\n", ind);
LOG(logERROR, (mess));
return FAIL;
}
@@ -1421,143 +1475,41 @@ int getPowerRail(enum PWRINDEX ind, int *retval, char *mess) {
return OK;
}
// for power rail index and name debugging
int getPowerIndex(enum DACINDEX ind, enum PWRINDEX *pwrIndex, char *mess) {
*pwrIndex = 0;
switch (ind) {
case D_PWR_IO:
*pwrIndex = PWR_IO;
break;
case D_PWR_A:
*pwrIndex = PWR_A;
break;
case D_PWR_B:
*pwrIndex = PWR_B;
break;
case D_PWR_C:
*pwrIndex = PWR_C;
break;
case D_PWR_D:
*pwrIndex = PWR_D;
break;
default:
sprintf(mess, "Index %d has no power index\n", ind);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertPowerRegDACtoVoltage(enum PWRINDEX ind, int val, int *retval,
char *mess) {
char *powerNames[] = {PWR_NAMES};
if (ConvertToDifferentRange(LTC2620_D_GetMaxInput(),
LTC2620_D_GetMinInput(), POWER_RGLTR_MIN,
POWER_RGLTR_MAX, val, retval) == FAIL) {
sprintf(mess, "Could not get %s. Could not convert %d dac to mV\n",
powerNames[ind], val);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int convertPowerRegVoltagetoDAC(enum PWRINDEX ind, int val, int *retval,
char *mess) {
char *powerNames[] = {PWR_NAMES};
if (ConvertToDifferentRange(POWER_RGLTR_MIN, POWER_RGLTR_MAX,
LTC2620_D_GetMaxInput(),
LTC2620_D_GetMinInput(), val, retval) == FAIL) {
sprintf(
mess,
"Could not set %s. Invalid value %d mV to convert to dac units.\n",
powerNames[ind], val);
LOG(logERROR, (mess));
return FAIL;
}
return OK;
}
int initPower(enum DACINDEX ind, char *mess) {
if (ind == D_PWR_EMPTY)
return OK;
enum PWRINDEX pwrIndex = 0;
if (getPowerIndex(ind, &pwrIndex, mess) == FAIL)
return FAIL;
char *powerNames[] = {PWR_NAMES};
int dacval = 0;
int min = (ind == D_PWR_IO) ? VIO_MIN_MV : POWER_RGLTR_MIN;
LOG(logINFO, ("Initializing %s to %d mV (power disabled)\n",
powerNames[pwrIndex], min));
if (convertPowerRegVoltagetoDAC(pwrIndex, min, &dacval, mess) == FAIL)
return FAIL;
if (setDAC(ind, dacval, 0, mess) == FAIL)
return FAIL;
return OK;
}
int getPower(enum DACINDEX ind, int *retval, char *mess) {
enum PWRINDEX pwrIndex = 0;
*retval = -1;
// validate index
if (getPowerIndex(ind, &pwrIndex, mess) == FAIL)
return FAIL;
// powered enable off
int pwrRetval = 0;
if (getPowerRail(pwrIndex, &pwrRetval, mess) == FAIL)
// if powered off, return 0
if (getPowerRail(pwrIndex, retval, mess) == FAIL)
return FAIL;
if (pwrRetval == 0) {
*retval = 0;
if (*retval == 0) {
return OK;
}
// mV
if (convertPowerRegDACtoVoltage(pwrIndex, dacValues[ind], retval, mess) ==
FAIL)
if (getDAC(ind, true, retval, mess) == FAIL)
return FAIL;
return OK;
}
int setPower(enum DACINDEX ind, int val, char *mess) {
enum PWRINDEX pwrIndex = 0;
char *powerNames[] = {PWR_NAMES};
// validate index
if (getPowerIndex(ind, &pwrIndex, mess) == FAIL)
return FAIL;
// validate values (invalidate power down)
if (isPowerValid(pwrIndex, val, mess) == FAIL)
return FAIL;
char *powerNames[] = {PWR_NAMES};
LOG(logINFOBLUE, ("Setting %s to %d mV\n", powerNames[pwrIndex], val));
if (validatePower(pwrIndex, val, mess) == FAIL)
return FAIL;
if (DisablePowerRail(pwrIndex, mess) == FAIL)
return FAIL;
// convert to dac units
int dacval = val;
if (val != 0) {
if (convertPowerRegVoltagetoDAC(pwrIndex, val, &dacval, mess) == FAIL)
if (setDAC(ind, val, true, mess) == FAIL)
return FAIL;
LOG(logINFO, ("Setting %s (DAC %d): %d dac (%d mV)\n",
powerNames[pwrIndex], dacval, val));
if (setDAC(ind, dacval, 0, mess) == FAIL)
return FAIL;
if (EnablePowerRail(pwrIndex, mess) == FAIL)
return FAIL;
}
return OK;
}
@@ -6,6 +6,7 @@
#include "arm64.h"
#include "programViaArm.h"
#include <stdbool.h>
#include <stdio.h> // FILE
#include <stdlib.h>
#include <sys/types.h>
@@ -119,25 +120,28 @@ int64_t getMeasurementTime();
int setModule(sls_detector_module myMod, char *mess);
// parameters - dac, adc, hv
int isDACValid(enum DACINDEX ind, int val, int mV, char *mess);
int setDAC(enum DACINDEX ind, int val, int mV, char *mess);
int getDAC(enum DACINDEX ind, int mV, int *retval, char *mess);
int validateDACIndex(enum DACINDEX ind, char *mess);
int validateDACValue(enum DACINDEX ind, int dacval, char *mess);
int validateDACVoltage(enum DACINDEX ind, int voltage, char *mess);
int convertVoltageToDACValue(enum DACINDEX ind, int voltage, int *retval_dacval,
char *mess);
int convertDACValueToVoltage(enum DACINDEX ind, int dacval, int *retval_voltage,
char *mess);
int getDAC(enum DACINDEX ind, bool mV, int *retval, char *mess);
/** @param val value can be in mV or dac units */
int setDAC(enum DACINDEX ind, int val, bool mV, char *mess);
/** @param dacval in dac units */
int writeDACSpi(enum DACINDEX ind, int dacval, char *mess);
int getVLimit();
int setVLimit(int val, char *mess);
int validatePower(enum PWRINDEX ind, int val, char *mess);
int getPowerIndex(enum DACINDEX ind, enum PWRINDEX *pwrIndex, char *mess);
int getPowerRailMask(enum PWRINDEX ind, uint32_t *mask, char *mess);
int EnablePowerRail(enum PWRINDEX ind, char *mess);
int DisablePowerRail(enum PWRINDEX ind, char *mess);
int getPowerRail(enum PWRINDEX ind, int *retval, char *mess);
int isPowerValid(enum PWRINDEX ind, int val, char *mess);
int getPowerIndex(enum DACINDEX ind, enum PWRINDEX *pwrIndex, char *mess);
int convertPowerRegDACtoVoltage(enum PWRINDEX ind, int val, int *retval,
char *mess);
int convertPowerRegVoltagetoDAC(enum PWRINDEX ind, int val, int *retval,
char *mess);
int initPower(enum DACINDEX ind, char *mess);
int getPower(enum DACINDEX ind, int *retval, char *mess);
int setPower(enum DACINDEX ind, int val, char *mess);
+6 -6
View File
@@ -3,10 +3,10 @@
/** API versions */
#define APILIB "0.0.0 0x250909"
#define APIRECEIVER "0.0.0 0x250822"
#define APICTB "0.0.0 0x260210"
#define APICTB "0.0.0 0x260213"
#define APIGOTTHARD2 "0.0.0 0x260212"
#define APIMOENCH "0.0.0 0x260212"
#define APIEIGER "0.0.0 0x260212"
#define APIXILINXCTB "0.0.0 0x260210"
#define APIJUNGFRAU "0.0.0 0x260212"
#define APIMYTHEN3 "0.0.0 0x260212"
#define APIMOENCH "0.0.0 0x260212"
#define APIEIGER "0.0.0 0x260212"
#define APIXILINXCTB "0.0.0 0x260213"
#define APIJUNGFRAU "0.0.0 0x260212"
#define APIMYTHEN3 "0.0.0 0x260212"