This commit is contained in:
froejdh_e
2026-02-11 11:58:07 +01:00
parent 26729b06cb
commit 383c842589
4 changed files with 56 additions and 53 deletions

View File

@@ -11300,7 +11300,9 @@ int spi_read(int file_des) {
}
/**
* Write to SPI register.
* Generic write to a SPI register. For Matterhorn and similar detectors
* the client packs the chip_id and register_id in the first byte.
* ((chip_id & 0xF) << 4) | (register_id & 0xF)
*/
int spi_write(int file_des) {
#if !defined(XILINX_CHIPTESTBOARDD)
@@ -11308,28 +11310,6 @@ int spi_write(int file_des) {
return Server_SendResult(file_des, INT32, NULL, 0);
#endif
int chip_id = 0;
if (receiveData(file_des, &chip_id, sizeof(chip_id), INT32) < 0) {
return printSocketReadError();
}
if (chip_id < 0 || chip_id > 15) {
ret = FAIL;
sprintf(mess, "Invalid chip_id %d. Must be 0-15\n", chip_id);
LOG(logERROR, (mess));
return Server_SendResult(file_des, INT32, NULL, 0);
}
int register_id = 0;
if (receiveData(file_des, &register_id, sizeof(register_id), INT32) < 0) {
return printSocketReadError();
}
if (register_id < 0 || register_id > 15) {
ret = FAIL;
sprintf(mess, "Invalid register_id %d. Must be 0-15\n", register_id);
LOG(logERROR, (mess));
return Server_SendResult(file_des, INT32, NULL, 0);
}
int n_bytes = 0;
if (receiveData(file_des, &n_bytes, sizeof(n_bytes), INT32) < 0) {
return printSocketReadError();
@@ -11341,9 +11321,7 @@ int spi_write(int file_des) {
return sendError(file_des);
}
LOG(logINFO,
("SPI Write Requested: chip_id=%d, register_id=%d, n_bytes=%d\n",
chip_id, register_id, n_bytes));
LOG(logINFO, ("SPI Write Requested: n_bytes=%d\n", n_bytes));
uint8_t *data = malloc(n_bytes);
if (data == NULL) {
@@ -11356,49 +11334,40 @@ int spi_write(int file_des) {
return printSocketReadError();
}
uint8_t *local_tx = malloc(n_bytes + 1);
if (local_tx == NULL) {
LOG(logERROR, ("Could not allocate memory for local_tx\n"));
exit(EXIT_FAILURE);
}
uint8_t *local_rx = malloc(n_bytes + 1);
if (local_rx == NULL) {
uint8_t *rx = malloc(n_bytes);
if (rx == NULL) {
LOG(logERROR, ("Could not allocate memory for local_rx\n"));
exit(EXIT_FAILURE);
}
struct spi_ioc_transfer send_cmd[1];
memset(send_cmd, 0, sizeof(send_cmd));
send_cmd[0].len = n_bytes + 1;
send_cmd[0].tx_buf = (unsigned long)local_tx;
send_cmd[0].rx_buf = (unsigned long)local_rx;
send_cmd[0].len = n_bytes;
send_cmd[0].tx_buf = (unsigned long)data;
send_cmd[0].rx_buf = (unsigned long)rx;
// 0 - Normal operation, 1 - CSn remains zero after operation
// 0 = Normal operation
send_cmd[0].cs_change = 0;
local_tx[0] = ((chip_id & 0xF) << 4) | (register_id & 0xF);
for (int i = 0; i < n_bytes; i++)
local_tx[i + 1] = data[i];
#ifdef VIRTUAL
// For the virtual detector copy the data from local_tx to local_rx
for (int i = 0; i < n_bytes + 1; i++) {
local_rx[i] = local_tx[i];
// For the virtual detector copy the data to rx
for (int i = 0; i < n_bytes; i++) {
rx[i] = data[i];
}
#else
int spifd = open("/dev/spidev2.0", O_RDWR);
LOG(logINFO, ("SPI Read: opened spidev2.0 with fd=%d\n", spifd));
if (spifd < 0) {
free(data);
free(local_tx);
free(local_rx);
free(rx);
sprintf(mess, "Could not open /dev/spidev2.0\n");
return sendError(file_des);
}
if (ioctl(spifd, SPI_IOC_MESSAGE(1), &send_cmd) < 0) {
close(spifd);
free(data);
free(local_tx);
free(local_rx);
free(rx);
sprintf(mess, "SPI write failed with %d:%s\n", errno, strerror(errno));
return sendError(file_des);
}
@@ -11408,10 +11377,9 @@ int spi_write(int file_des) {
ret = OK;
LOG(logDEBUG1, ("SPI Write Complete\n"));
Server_SendResult(file_des, INT32, NULL, 0);
sendData(file_des, local_rx + 1, n_bytes, OTHER);
sendData(file_des, rx, n_bytes, OTHER);
free(data);
free(local_tx);
free(local_rx);
free(rx);
return ret;
}

View File

@@ -2964,7 +2964,11 @@ Result<std::vector<uint8_t>> Detector::readSpi(int chip_id, int register_id,
Result<std::vector<uint8_t>>
Detector::writeSpi(int chip_id, int register_id,
const std::vector<uint8_t> &data, Positions pos) {
return pimpl->Parallel(&Module::writeSpi, pos, chip_id, register_id, data);
return pimpl->Parallel(
static_cast<std::vector<uint8_t> (Module::*)(int, int,
const std::vector<uint8_t>&)>(
&Module::writeSpi),
pos, chip_id, register_id, data);
}
} // namespace sls

View File

@@ -4098,11 +4098,41 @@ std::vector<uint8_t> Module::readSpi(int chip_id, int register_id,
std::vector<uint8_t> Module::writeSpi(int chip_id, int register_id,
const std::vector<uint8_t> &data) {
//At the moment we only support one module and spi data is always
//small so a clean API is more important than the extra copy
std::vector<uint8_t> message = data;
message.insert(message.begin(), ((chip_id & 0xF) << 4) | (register_id & 0xF));
auto ret = writeSpi(message);
// auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
// client.Send(F_SPI_WRITE);
// client.setFnum(F_SPI_WRITE);
// client.Send(static_cast<int>(message.size()));
// client.Send(message);
// if (client.Receive<int>() == FAIL) {
// std::ostringstream os;
// os << "Module " << moduleIndex << " (" << shm()->hostname << ")"
// << " returned error: " << client.readErrorMessage();
// throw DetectorError(os.str());
// }
// // Read the output from the SPI write. This contains the data before the
// // write.
// std::vector<uint8_t> ret(message.size());
// client.Receive(ret);
ret.erase(ret.begin()); //The first value is 0xFF so we drop it
return ret;
}
std::vector<uint8_t> Module::writeSpi(const std::vector<uint8_t> &data) {
auto client = DetectorSocket(shm()->hostname, shm()->controlPort);
client.Send(F_SPI_WRITE);
client.setFnum(F_SPI_WRITE);
client.Send(chip_id);
client.Send(register_id);
client.Send(static_cast<int>(data.size()));
client.Send(data);

View File

@@ -612,6 +612,7 @@ class Module : public virtual slsDetectorDefs {
std::vector<uint8_t> writeSpi(int chip_id, int register_id,
const std::vector<uint8_t> &data);
std::vector<uint8_t> writeSpi(const std::vector<uint8_t> &data);
private:
std::string getReceiverLongVersion() const;