From 1c44a66964bd0d129f531ac793e60ce817a5c113 Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Tue, 10 Feb 2026 16:10:51 +0100 Subject: [PATCH] formatted --- .../src/slsDetectorServer_funcs.c | 173 +++++++++--------- slsDetectorSoftware/include/sls/Detector.h | 5 +- slsDetectorSoftware/src/Detector.cpp | 10 +- slsDetectorSoftware/src/Module.cpp | 10 +- slsDetectorSoftware/src/Module.h | 5 +- 5 files changed, 104 insertions(+), 99 deletions(-) diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index 49427f02f..af0f205ea 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -7,9 +7,9 @@ #include "sls/sls_detector_funcs.h" #include "slsDetectorFunctionList.h" +#include #include #include -#include #if defined(CHIPTESTBOARDD) || defined(XILINX_CHIPTESTBOARDD) || \ defined(MYTHEN3D) @@ -11108,175 +11108,180 @@ int set_pattern_wait_interval(int file_des) { /** * Non destructive read from SPI register. Read n_bytes by shifting in dummy * data while keeping csn 0 after the operation. Shift the read out data back - * in to restore the register. + * in to restore the register. */ -int spi_read(int file_des){ +int spi_read(int file_des) { #if !defined(XILINX_CHIPTESTBOARDD) functionNotImplemented(); return sendError(file_des); #endif int chip_id = 0; - if (receiveData(file_des, &chip_id, sizeof(chip_id), INT32) < 0){ + if (receiveData(file_des, &chip_id, sizeof(chip_id), INT32) < 0) { return printSocketReadError(); } - if(chip_id < 0 || chip_id > 15){ + if (chip_id < 0 || chip_id > 15) { sprintf(mess, "Invalid chip_id %d. Must be 0-15\n", chip_id); return sendError(file_des); } int register_id = 0; - if (receiveData(file_des, ®ister_id, sizeof(register_id), INT32) < 0){ + if (receiveData(file_des, ®ister_id, sizeof(register_id), INT32) < 0) { return printSocketReadError(); } - if(register_id < 0 || register_id > 15){ + if (register_id < 0 || register_id > 15) { sprintf(mess, "Invalid register_id %d. Must be 0-15\n", register_id); return sendError(file_des); } int n_bytes = 0; - if (receiveData(file_des, &n_bytes, sizeof(n_bytes), INT32) < 0){ + if (receiveData(file_des, &n_bytes, sizeof(n_bytes), INT32) < 0) { return printSocketReadError(); } - if(n_bytes < 1 ){ - sprintf(mess, "Invalid n_bytes %d. Must ask for a read of at least 1 byte\n", n_bytes); + if (n_bytes < 1) { + sprintf(mess, + "Invalid n_bytes %d. Must ask for a read of at least 1 byte\n", + n_bytes); return sendError(file_des); } - LOG(logINFO, ("SPI Read Requested: chip_id=%d, register_id=%d, n_bytes=%d\n", - chip_id, register_id, n_bytes)); - + LOG(logINFO, + ("SPI Read Requested: chip_id=%d, register_id=%d, n_bytes=%d\n", + chip_id, register_id, n_bytes)); #ifdef VIRTUAL // For the virtual detector we create a fake register to read from // and fill it with 0,2,4,6,... This way we can check that copying // of the data works as expected uint8_t *fake_register = malloc(n_bytes); - if(fake_register == NULL){ + if (fake_register == NULL) { LOG(logERROR, ("Could not allocate memory for fake register\n")); exit(EXIT_FAILURE); } for (int i = 0; i < n_bytes; i++) { - fake_register[i] = (uint8_t)( (i*2) % 256 ); + fake_register[i] = (uint8_t)((i * 2) % 256); } #else int spifd = open("/dev/spidev2.0", O_RDWR); LOG(logINFO, ("SPI Read: opened spidev2.0 with fd=%d\n", spifd)); - if(spifd < 0){ + if (spifd < 0) { sprintf(mess, "Could not open /dev/spidev2.0\n"); return sendError(file_des); } #endif - // Allocate dummy data to shif in, we keep a copy of this + // Allocate dummy data to shif in, we keep a copy of this // to double check that we access a register of the correct size uint8_t *dummy_data = malloc(n_bytes); - if(dummy_data == NULL){ + if (dummy_data == NULL) { LOG(logERROR, ("Could not allocate memory for dummy data\n")); exit(EXIT_FAILURE); } - for(int i=0; i 15){ + 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)); @@ -11318,10 +11320,10 @@ int spi_write(int file_des){ } int register_id = 0; - if (receiveData(file_des, ®ister_id, sizeof(register_id), INT32) < 0){ + if (receiveData(file_des, ®ister_id, sizeof(register_id), INT32) < 0) { return printSocketReadError(); } - if(register_id < 0 || register_id > 15){ + 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)); @@ -11329,67 +11331,70 @@ int spi_write(int file_des){ } int n_bytes = 0; - if (receiveData(file_des, &n_bytes, sizeof(n_bytes), INT32) < 0){ + if (receiveData(file_des, &n_bytes, sizeof(n_bytes), INT32) < 0) { return printSocketReadError(); } - if(n_bytes < 1 ){ - sprintf(mess, "Invalid n_bytes %d. Must ask for a write of at least 1 byte\n", n_bytes); + if (n_bytes < 1) { + sprintf(mess, + "Invalid n_bytes %d. Must ask for a write of at least 1 byte\n", + n_bytes); 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: chip_id=%d, register_id=%d, n_bytes=%d\n", + chip_id, register_id, n_bytes)); + uint8_t *data = malloc(n_bytes); - if(data == NULL){ + if (data == NULL) { LOG(logERROR, ("Could not allocate memory for SPI write data\n")); exit(EXIT_FAILURE); } memset(data, 0, n_bytes); - if (receiveData(file_des, data, n_bytes, OTHER) < 0){ + if (receiveData(file_des, data, n_bytes, OTHER) < 0) { free(data); return printSocketReadError(); } - uint8_t* local_tx = malloc(n_bytes+1); - if(local_tx == NULL){ + 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 *local_rx = malloc(n_bytes + 1); + if (local_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 + 1; + send_cmd[0].tx_buf = (unsigned long)local_tx; + send_cmd[0].rx_buf = (unsigned long)local_rx; // 0 - Normal operation, 1 - CSn remains zero after operation - send_cmd[0].cs_change = 0; + 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]; + 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++){ + 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)); - if(spifd < 0){ + if (spifd < 0) { free(data); free(local_tx); free(local_rx); sprintf(mess, "Could not open /dev/spidev2.0\n"); return sendError(file_des); } - if(ioctl(spifd, SPI_IOC_MESSAGE(1), &send_cmd)<0){ + if (ioctl(spifd, SPI_IOC_MESSAGE(1), &send_cmd) < 0) { close(spifd); free(data); free(local_tx); @@ -11403,7 +11408,7 @@ 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, local_rx + 1, n_bytes, OTHER); free(data); free(local_tx); diff --git a/slsDetectorSoftware/include/sls/Detector.h b/slsDetectorSoftware/include/sls/Detector.h index fb7c32b29..c4d5b670f 100644 --- a/slsDetectorSoftware/include/sls/Detector.h +++ b/slsDetectorSoftware/include/sls/Detector.h @@ -2248,10 +2248,11 @@ class Detector { ///@} Result> readSpi(int chip_id, int register_id, - int n_bytes, Positions pos = {}) const; + int n_bytes, Positions pos = {}) const; Result> writeSpi(int chip_id, int register_id, - const std::vector &data, Positions pos = {}); + const std::vector &data, + Positions pos = {}); private: std::vector getValidPortNumbers(uint16_t start_port); diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index 619619e09..39706ec56 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -2961,12 +2961,10 @@ Result> Detector::readSpi(int chip_id, int register_id, n_bytes); } -Result> Detector::writeSpi(int chip_id, int register_id, - const std::vector &data, Positions pos){ +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); - } - - - +} } // namespace sls diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index 216a491fd..8a97afb89 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -4076,7 +4076,7 @@ void Module::simulatingActivityinDetector(const std::string &functionType, } std::vector Module::readSpi(int chip_id, int register_id, - int n_bytes) const{ + int n_bytes) const { auto client = DetectorSocket(shm()->hostname, shm()->controlPort); client.Send(F_SPI_READ); client.setFnum(F_SPI_READ); @@ -4084,7 +4084,7 @@ std::vector Module::readSpi(int chip_id, int register_id, client.Send(register_id); client.Send(n_bytes); - if (client.Receive() == FAIL) { + if (client.Receive() == FAIL) { std::ostringstream os; os << "Module " << moduleIndex << " (" << shm()->hostname << ")" << " returned error: " << client.readErrorMessage(); @@ -4094,11 +4094,10 @@ std::vector Module::readSpi(int chip_id, int register_id, std::vector data(n_bytes); client.Receive(data); return data; - } std::vector Module::writeSpi(int chip_id, int register_id, - const std::vector &data){ + const std::vector &data) { auto client = DetectorSocket(shm()->hostname, shm()->controlPort); client.Send(F_SPI_WRITE); client.setFnum(F_SPI_WRITE); @@ -4114,7 +4113,8 @@ std::vector Module::writeSpi(int chip_id, int register_id, throw DetectorError(os.str()); } - // Read the output from the SPI write. This contains the data before the write. + // Read the output from the SPI write. This contains the data before the + // write. std::vector ret(data.size()); client.Receive(ret); return ret; diff --git a/slsDetectorSoftware/src/Module.h b/slsDetectorSoftware/src/Module.h index cc692cdef..9f75f1765 100644 --- a/slsDetectorSoftware/src/Module.h +++ b/slsDetectorSoftware/src/Module.h @@ -608,9 +608,10 @@ class Module : public virtual slsDetectorDefs { int64_t getActualTime() const; int64_t getMeasurementTime() const; std::vector readSpi(int chip_id, int register_id, - int n_bytes) const; + int n_bytes) const; - std::vector 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;