From 3f4df445f1ad4f62b15ea3a41055cbfe92d91099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Mon, 9 Feb 2026 13:50:35 +0100 Subject: [PATCH] send back the result of the SPI write (#1387) --- python/src/detector.cpp | 4 ++-- .../src/slsDetectorServer_funcs.c | 15 ++++++++++----- slsDetectorSoftware/include/sls/Detector.h | 2 +- slsDetectorSoftware/src/Detector.cpp | 4 ++-- slsDetectorSoftware/src/Module.cpp | 7 ++++++- slsDetectorSoftware/src/Module.h | 2 +- 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/python/src/detector.cpp b/python/src/detector.cpp index d3e6be8b3..7bad555c0 100644 --- a/python/src/detector.cpp +++ b/python/src/detector.cpp @@ -2206,8 +2206,8 @@ void init_det(py::module &m) { py::arg() = Positions{}); CppDetectorApi.def( "writeSpi", - (void (Detector::*)(int, int, const std::vector &, - sls::Positions)) & + (Result>(Detector::*)( + int, int, const std::vector &, sls::Positions)) & Detector::writeSpi, py::arg(), py::arg(), py::arg(), py::arg() = Positions{}); ; diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index 174af28a8..49427f02f 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -11375,7 +11375,10 @@ int spi_write(int file_des){ local_tx[i+1] = data[i]; #ifdef VIRTUAL - // For the virtual detector we have nothing to do + // 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]; + } #else int spifd = open("/dev/spidev2.0", O_RDWR); LOG(logINFO, ("SPI Read: opened spidev2.0 with fd=%d\n", spifd)); @@ -11397,11 +11400,13 @@ int spi_write(int file_des){ close(spifd); #endif + ret = OK; + LOG(logDEBUG1, ("SPI Write Complete\n")); + Server_SendResult(file_des, INT32, NULL, 0); + sendData(file_des, local_rx+1, n_bytes, OTHER); + free(data); free(local_tx); free(local_rx); - - ret = OK; - LOG(logDEBUG1, ("SPI Write Complete\n")); - return Server_SendResult(file_des, INT32, NULL, 0); + return ret; } \ No newline at end of file diff --git a/slsDetectorSoftware/include/sls/Detector.h b/slsDetectorSoftware/include/sls/Detector.h index 958e64dc5..fb7c32b29 100644 --- a/slsDetectorSoftware/include/sls/Detector.h +++ b/slsDetectorSoftware/include/sls/Detector.h @@ -2250,7 +2250,7 @@ class Detector { Result> readSpi(int chip_id, int register_id, int n_bytes, Positions pos = {}) const; - void writeSpi(int chip_id, int register_id, + Result> writeSpi(int chip_id, int register_id, const std::vector &data, Positions pos = {}); private: diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index 1960fd858..619619e09 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -2961,9 +2961,9 @@ Result> Detector::readSpi(int chip_id, int register_id, n_bytes); } -void Detector::writeSpi(int chip_id, int register_id, +Result> Detector::writeSpi(int chip_id, int register_id, const std::vector &data, Positions pos){ - pimpl->Parallel(&Module::writeSpi, pos, chip_id, register_id, data); + return pimpl->Parallel(&Module::writeSpi, pos, chip_id, register_id, data); } diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index b0dc43211..216a491fd 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -4097,7 +4097,7 @@ std::vector Module::readSpi(int chip_id, int register_id, } -void Module::writeSpi(int chip_id, int register_id, +std::vector Module::writeSpi(int chip_id, int register_id, const std::vector &data){ auto client = DetectorSocket(shm()->hostname, shm()->controlPort); client.Send(F_SPI_WRITE); @@ -4113,6 +4113,11 @@ void Module::writeSpi(int chip_id, int register_id, << " 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(data.size()); + client.Receive(ret); + return ret; } } // namespace sls diff --git a/slsDetectorSoftware/src/Module.h b/slsDetectorSoftware/src/Module.h index 2e4fb5684..cc692cdef 100644 --- a/slsDetectorSoftware/src/Module.h +++ b/slsDetectorSoftware/src/Module.h @@ -610,7 +610,7 @@ class Module : public virtual slsDetectorDefs { std::vector readSpi(int chip_id, int register_id, int n_bytes) const; - void writeSpi(int chip_id, int register_id, const std::vector &data); + std::vector writeSpi(int chip_id, int register_id, const std::vector &data); private: std::string getReceiverLongVersion() const;