set bit and clear bit only verifies that bit (#746)

This commit is contained in:
maliakal_d 2023-05-25 10:35:17 +02:00 committed by GitHub
parent 6834294437
commit 0a7fd0a51a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 211 additions and 21 deletions

View File

@ -932,6 +932,59 @@ int readRegister(uint32_t offset, uint32_t *retval) {
#endif
}
int setBit(const uint32_t addr, const int nBit) {
#ifndef VIRTUAL
uint32_t regval = 0;
if (readRegister(addr, &regval) == FAIL) {
return FAIL;
}
uint32_t bitmask = (1 << nBit);
uint32_t val = regval | bitmask;
sharedMemory_lockLocalLink();
if (!Feb_Control_WriteRegister_BitMask(addr, val, bitmask)) {
sharedMemory_unlockLocalLink();
return FAIL;
}
sharedMemory_unlockLocalLink();
#endif
return OK;
}
int clearBit(const uint32_t addr, const int nBit) {
#ifndef VIRTUAL
uint32_t regval = 0;
if (readRegister(addr, &regval) == FAIL) {
return FAIL;
}
uint32_t bitmask = (1 << nBit);
uint32_t val = regval & ~bitmask;
sharedMemory_lockLocalLink();
if (!Feb_Control_WriteRegister_BitMask(addr, val, bitmask)) {
sharedMemory_unlockLocalLink();
return FAIL;
}
sharedMemory_unlockLocalLink();
#endif
return OK;
}
int getBit(const uint32_t addr, const int nBit, int *retval) {
#ifndef VIRTUAL
uint32_t regval = 0;
uint32_t bitmask = (1 << nBit);
sharedMemory_lockLocalLink();
if (!Feb_Control_ReadRegister_BitMask(addr, &regval, bitmask)) {
sharedMemory_unlockLocalLink();
return FAIL;
}
sharedMemory_unlockLocalLink();
*retval = (regval >> nBit);
#endif
return OK;
}
/* set parameters - dr, roi */
int setDynamicRange(int dr) {

View File

@ -163,6 +163,9 @@ void resetToHardwareSettings();
#ifdef EIGERD
int writeRegister(uint32_t offset, uint32_t data);
int readRegister(uint32_t offset, uint32_t *retval);
int setBit(const uint32_t addr, int nBit);
int clearBit(const uint32_t addr, int nBit);
int getBit(const uint32_t addr, const int nBit, int *retval);
#elif GOTTHARDD
uint32_t writeRegister16And32(uint32_t offset,
uint32_t data); // FIXME its not there in ctb

View File

@ -310,3 +310,6 @@ int get_synchronization(int);
int set_synchronization(int);
int get_hardware_version(int);
int get_frontend_firmware_version(int);
int get_bit(int);
int set_bit(int);
int clear_bit(int);

View File

@ -473,6 +473,9 @@ void function_table() {
flist[F_SET_SYNCHRONIZATION] = &set_synchronization;
flist[F_GET_HARDWARE_VERSION] = &get_hardware_version;
flist[F_GET_FRONTEND_FIRMWARE_VERSION] = &get_frontend_firmware_version;
flist[F_GET_BIT] = &get_bit;
flist[F_SET_BIT] = &set_bit;
flist[F_CLEAR_BIT] = &clear_bit;
// check
if (NUM_DET_FUNCTIONS >= RECEIVER_ENUM_START) {
@ -10237,3 +10240,135 @@ int get_frontend_firmware_version(int file_des) {
#endif
return Server_SendResult(file_des, INT64, &retval, sizeof(retval));
}
int set_bit(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
uint32_t args[2] = {-1, -1};
if (receiveData(file_des, args, sizeof(args), INT32) < 0)
return printSocketReadError();
uint32_t addr = args[0];
int nBit = (int)args[1];
LOG(logDEBUG1, ("Setting bit %d of reg 0x%x\n", nBit, addr));
// only set
if (Server_VerifyLock() == OK) {
if (nBit < 0 || nBit > 31) {
ret = FAIL;
sprintf(
mess,
"Could not set bit. Bit nr %d out of range. Must be 0-31\n",
nBit);
LOG(logERROR, (mess));
} else {
#ifdef EIGERD
ret = setBit(addr, nBit);
if (ret == FAIL) {
#else
uint32_t bitmask = (1 << nBit);
#ifdef GOTTHARDD
uint32_t val = readRegister16And32(addr) | bitmask;
uint32_t retval = writeRegister16And32(addr, val);
#else
uint32_t val = readRegister(addr) | bitmask;
uint32_t retval = writeRegister(addr, val);
#endif
if (!(retval & bitmask)) {
ret = FAIL;
#endif
sprintf(mess, "Could not set bit %d.\n", nBit);
LOG(logERROR, (mess));
}
}
}
return Server_SendResult(file_des, INT32, NULL, 0);
}
int clear_bit(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
uint32_t args[2] = {-1, -1};
if (receiveData(file_des, args, sizeof(args), INT32) < 0)
return printSocketReadError();
uint32_t addr = args[0];
int nBit = (int)args[1];
LOG(logDEBUG1, ("Clearing bit %d of reg 0x%x\n", nBit, addr));
// only set
if (Server_VerifyLock() == OK) {
if (nBit < 0 || nBit > 31) {
ret = FAIL;
sprintf(
mess,
"Could not clear bit. Bit nr %d out of range. Must be 0-31\n",
nBit);
LOG(logERROR, (mess));
} else {
#ifdef EIGERD
ret = clearBit(addr, nBit);
if (ret == FAIL) {
#else
uint32_t bitmask = (1 << nBit);
#ifdef GOTTHARDD
uint32_t val = readRegister16And32(addr) & ~bitmask;
uint32_t retval = writeRegister16And32(addr, val);
#else
uint32_t val = readRegister(addr) & ~bitmask;
uint32_t retval = writeRegister(addr, val);
#endif
if (retval & bitmask) {
ret = FAIL;
#endif
sprintf(mess, "Could not clear bit %d.\n", nBit);
LOG(logERROR, (mess));
}
}
}
return Server_SendResult(file_des, INT32, NULL, 0);
}
int get_bit(int file_des) {
ret = OK;
memset(mess, 0, sizeof(mess));
uint32_t args[2] = {-1, -1};
int retval = 0;
if (receiveData(file_des, args, sizeof(args), INT32) < 0)
return printSocketReadError();
uint32_t addr = args[0];
int nBit = (int)args[1];
LOG(logDEBUG1, ("Getting bit %d of reg 0x%x\n", nBit, addr));
// only set
if (Server_VerifyLock() == OK) {
if (nBit < 0 || nBit > 31) {
ret = FAIL;
sprintf(
mess,
"Could not get bit. Bit nr %d out of range. Must be 0-31\n",
nBit);
LOG(logERROR, (mess));
} else {
#ifdef EIGERD
ret = getBit(addr, nBit, &retval);
LOG(logDEBUG1, ("retval: %d\n", retval));
if (ret == FAIL) {
#else
#ifdef GOTTHARDD
retval = readRegister16And32(addr);
#else
retval = readRegister(addr);
#endif
LOG(logDEBUG1, ("retval: %d\n", retval));
if (retval & (1 << nBit)) {
ret = FAIL;
#endif
sprintf(mess, "Could not get bit %d.\n", nBit);
LOG(logERROR, (mess));
}
}
}
return Server_SendResult(file_des, INT32, &retval, sizeof(retval));
}

View File

@ -2747,29 +2747,18 @@ uint32_t Module::writeRegister(uint32_t addr, uint32_t val) {
}
void Module::setBit(uint32_t addr, int n) {
if (n < 0 || n > 31) {
throw RuntimeError("Bit number " + std::to_string(n) + " out of Range");
} else {
uint32_t val = readRegister(addr);
writeRegister(addr, val | 1 << n);
}
uint32_t args[2] = {addr, static_cast<uint32_t>(n)};
sendToDetectorStop(F_SET_BIT, args, nullptr);
}
void Module::clearBit(uint32_t addr, int n) {
if (n < 0 || n > 31) {
throw RuntimeError("Bit number " + std::to_string(n) + " out of Range");
} else {
uint32_t val = readRegister(addr);
writeRegister(addr, val & ~(1 << n));
}
uint32_t args[2] = {addr, static_cast<uint32_t>(n)};
sendToDetectorStop(F_CLEAR_BIT, args, nullptr);
}
int Module::getBit(uint32_t addr, int n) {
if (n < 0 || n > 31) {
throw RuntimeError("Bit number " + std::to_string(n) + " out of Range");
} else {
return ((readRegister(addr) >> n) & 0x1);
}
uint32_t args[2] = {addr, static_cast<uint32_t>(n)};
return sendToDetectorStop<int>(F_GET_BIT, args);
}
void Module::executeFirmwareTest() { sendToDetector(F_SET_FIRMWARE_TEST); }

View File

@ -277,6 +277,9 @@ enum detFuncs {
F_SET_SYNCHRONIZATION,
F_GET_HARDWARE_VERSION,
F_GET_FRONTEND_FIRMWARE_VERSION,
F_GET_BIT,
F_SET_BIT,
F_CLEAR_BIT,
NUM_DET_FUNCTIONS,
RECEIVER_ENUM_START = 512, /**< detector function should not exceed this
@ -657,6 +660,10 @@ const char* getFunctionNameFromEnum(enum detFuncs func) {
case F_SET_SYNCHRONIZATION: return "F_SET_SYNCHRONIZATION";
case F_GET_HARDWARE_VERSION: return "F_GET_HARDWARE_VERSION";
case F_GET_FRONTEND_FIRMWARE_VERSION: return "F_GET_FRONTEND_FIRMWARE_VERSION";
case F_GET_BIT: return "F_GET_BIT";
case F_SET_BIT: return "F_SET_BIT";
case F_CLEAR_BIT: return "F_CLEAR_BIT";
case NUM_DET_FUNCTIONS: return "NUM_DET_FUNCTIONS";
case RECEIVER_ENUM_START: return "RECEIVER_ENUM_START";

View File

@ -2,12 +2,12 @@
// Copyright (C) 2021 Contributors to the SLS Detector Package
/** API versions */
#define RELEASE "developer"
#define APILIB "developer 0x230224"
#define APIRECEIVER "developer 0x230224"
#define APIJUNGFRAU "developer 0x230515"
#define APIMOENCH "developer 0x230515"
#define APIGOTTHARD "developer 0x230224"
#define APIGOTTHARD2 "developer 0x230224"
#define APIMYTHEN3 "developer 0x230224"
#define APILIB "developer 0x230224"
#define APIRECEIVER "developer 0x230224"
#define APIEIGER "developer 0x230224"
#define APIJUNGFRAU "developer 0x230508"
#define APIMOENCH "developer 0x230508"
#define APICTB "developer 0x230523"