From 383c842589fd79f1f2a5ba2ecef6a65f1c2bed25 Mon Sep 17 00:00:00 2001 From: froejdh_e Date: Wed, 11 Feb 2026 11:58:07 +0100 Subject: [PATCH] WIP --- .../src/slsDetectorServer_funcs.c | 68 +++++-------------- slsDetectorSoftware/src/Detector.cpp | 6 +- slsDetectorSoftware/src/Module.cpp | 34 +++++++++- slsDetectorSoftware/src/Module.h | 1 + 4 files changed, 56 insertions(+), 53 deletions(-) diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index af0f205ea..d9d18c1cf 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -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, ®ister_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; } \ No newline at end of file diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index 39706ec56..1db966e56 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -2964,7 +2964,11 @@ Result> Detector::readSpi(int chip_id, int register_id, Result> Detector::writeSpi(int chip_id, int register_id, const std::vector &data, Positions pos) { - return pimpl->Parallel(&Module::writeSpi, pos, chip_id, register_id, data); + return pimpl->Parallel( + static_cast (Module::*)(int, int, + const std::vector&)>( + &Module::writeSpi), + pos, chip_id, register_id, data); } } // namespace sls diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index 8a97afb89..2e7ea5e2e 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -4098,11 +4098,41 @@ std::vector Module::readSpi(int chip_id, int register_id, std::vector Module::writeSpi(int chip_id, int register_id, const std::vector &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 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(message.size())); + // client.Send(message); + + // if (client.Receive() == 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 ret(message.size()); + // client.Receive(ret); + ret.erase(ret.begin()); //The first value is 0xFF so we drop it + return ret; +} + +std::vector Module::writeSpi(const std::vector &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(data.size())); client.Send(data); diff --git a/slsDetectorSoftware/src/Module.h b/slsDetectorSoftware/src/Module.h index 9f75f1765..e245155d1 100644 --- a/slsDetectorSoftware/src/Module.h +++ b/slsDetectorSoftware/src/Module.h @@ -612,6 +612,7 @@ class Module : public virtual slsDetectorDefs { std::vector writeSpi(int chip_id, int register_id, const std::vector &data); + std::vector writeSpi(const std::vector &data); private: std::string getReceiverLongVersion() const;