diff --git a/slsDetectorServers/ctbDetectorServer/CMakeLists.txt b/slsDetectorServers/ctbDetectorServer/CMakeLists.txt index 1b97a4314..26c9ec0ee 100644 --- a/slsDetectorServers/ctbDetectorServer/CMakeLists.txt +++ b/slsDetectorServers/ctbDetectorServer/CMakeLists.txt @@ -16,7 +16,7 @@ add_executable(ctbDetectorServer_virtual ../slsDetectorServer/src/LTC2620.c ../slsDetectorServer/src/MAX1932.c ../slsDetectorServer/src/programFpgaBlackfin.c - ../slsDetectorServer/src/communication_virtual.c + ../slsDetectorServer/src/sharedMemory.c ) include_directories( diff --git a/slsDetectorServers/ctbDetectorServer/Makefile b/slsDetectorServers/ctbDetectorServer/Makefile index c5e3a1593..6d6aab318 100755 --- a/slsDetectorServers/ctbDetectorServer/Makefile +++ b/slsDetectorServers/ctbDetectorServer/Makefile @@ -6,13 +6,13 @@ support_lib = ../../slsSupportLib/include/ CROSS = bfin-uclinux- CC = $(CROSS)gcc CFLAGS += -Wall -std=gnu99 -DCHIPTESTBOARDD -DSTOP_SERVER -I$(main_inc) -I$(support_lib) -I$(current_dir) #-DDEBUG1 #-DVERBOSEI #-DVERBOSE -LDLIBS += -lm +LDLIBS += -lm -lrt -pthread PROGS = ctbDetectorServer DESTDIR ?= bin INSTMODE = 0777 SRCS = slsDetectorFunctionList.c -SRCS += $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs.c $(main_src)blackfin.c $(main_src)common.c $(main_src)commonServerFunctions.c $(main_src)communication_funcs_UDP.c $(main_src)UDPPacketHeaderGenerator.c $(main_src)AD7689.c $(main_src)AD9257.c $(main_src)ALTERA_PLL.c $(main_src)I2C.c $(main_src)INA226.c $(main_src)LTC2620.c $(main_src)MAX1932.c $(main_src)programFpgaBlackfin.c +SRCS += $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs.c $(main_src)blackfin.c $(main_src)common.c $(main_src)commonServerFunctions.c $(main_src)communication_funcs_UDP.c $(main_src)UDPPacketHeaderGenerator.c $(main_src)AD7689.c $(main_src)AD9257.c $(main_src)ALTERA_PLL.c $(main_src)I2C.c $(main_src)INA226.c $(main_src)LTC2620.c $(main_src)MAX1932.c $(main_src)programFpgaBlackfin.c $(main_src)/sharedMemory.c OBJS = $(SRCS:.c=.o) diff --git a/slsDetectorServers/ctbDetectorServer/bin/ctbDetectorServer_developer b/slsDetectorServers/ctbDetectorServer/bin/ctbDetectorServer_developer index 7f5c6e5ce..246f19c98 100755 Binary files a/slsDetectorServers/ctbDetectorServer/bin/ctbDetectorServer_developer and b/slsDetectorServers/ctbDetectorServer/bin/ctbDetectorServer_developer differ diff --git a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c index c1cfc11d6..af624fc05 100644 --- a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c @@ -1,5 +1,6 @@ #include "slsDetectorFunctionList.h" #include "clogger.h" +#include "sharedMemory.h" #include "versionAPI.h" #include "AD7689.h" // slow adcs @@ -10,9 +11,6 @@ #include "UDPPacketHeaderGenerator.h" #include "common.h" #include "communication_funcs_UDP.h" -#ifdef VIRTUAL -#include "communication_virtual.h" -#endif #include #include @@ -43,9 +41,8 @@ char initErrorMessage[MAX_STR_LENGTH]; #ifdef VIRTUAL pthread_t pthread_virtual_tid; -int virtual_status = 0; -int virtual_stop = 0; uint64_t virtual_pattern[MAX_PATTERN_LENGTH]; +int64_t virtual_currentFrameNumber = 2; #endif // 1g readout @@ -435,10 +432,7 @@ void initStopServer() { exit(EXIT_FAILURE); } #ifdef VIRTUAL - virtual_stop = 0; - if (!isControlServer) { - ComVirtual_setStop(virtual_stop); - } + sharedMemory_setStop(0); #endif } @@ -481,10 +475,7 @@ void setupDetector() { naSamples = 1; ndSamples = 1; #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); memset(virtual_pattern, 0, sizeof(virtual_pattern)); #endif @@ -2222,22 +2213,15 @@ int startStateMachine() { return FAIL; } LOG(logINFOBLUE, ("Starting State Machine\n")); - virtual_status = 1; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - virtual_stop = ComVirtual_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } + if (sharedMemory_getStop() != 0) { + LOG(logERROR, ("Cant start acquisition. " + "Stop server has not updated stop status to 0\n")); + return FAIL; } + sharedMemory_setStatus(RUNNING); if (pthread_create(&pthread_virtual_tid, NULL, &start_timer, NULL)) { LOG(logERROR, ("Could not start Virtual acquisition thread\n")); - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFOGREEN, ("Virtual Acquisition started\n")); @@ -2295,66 +2279,60 @@ void *start_timer(void *arg) { } // Send data - { - // loop over number of frames - for (int frameNr = 0; frameNr != numFrames; ++frameNr) { + // loop over number of frames + for (int frameNr = 0; frameNr != numFrames; ++frameNr) { - // update the virtual stop from stop server - virtual_stop = ComVirtual_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { - break; - } + // check if manual stop + if (sharedMemory_getStop() == 1) { + break; + } - // sleep for exposure time - struct timespec begin, end; - clock_gettime(CLOCK_REALTIME, &begin); - usleep(expUs); + // sleep for exposure time + struct timespec begin, end; + clock_gettime(CLOCK_REALTIME, &begin); + usleep(expUs); - int srcOffset = 0; - // loop packet - for (int i = 0; i != packetsPerFrame; ++i) { + int srcOffset = 0; + // loop packet + for (int i = 0; i != packetsPerFrame; ++i) { - char packetData[packetSize]; - memset(packetData, 0, packetSize); - // set header - sls_detector_header *header = - (sls_detector_header *)(packetData); - header->detType = (uint16_t)myDetectorType; - header->version = SLS_DETECTOR_HEADER_VERSION - 1; - header->frameNumber = frameNr; - header->packetNumber = i; - header->modId = 0; - header->row = detPos[X]; - header->column = detPos[Y]; + char packetData[packetSize]; + memset(packetData, 0, packetSize); + // set header + sls_detector_header *header = (sls_detector_header *)(packetData); + header->detType = (uint16_t)myDetectorType; + header->version = SLS_DETECTOR_HEADER_VERSION - 1; + header->frameNumber = virtual_currentFrameNumber; + header->packetNumber = i; + header->modId = 0; + header->row = detPos[X]; + header->column = detPos[Y]; - // fill data - memcpy(packetData + sizeof(sls_detector_header), - imageData + srcOffset, dataSize); - srcOffset += dataSize; + // fill data + memcpy(packetData + sizeof(sls_detector_header), + imageData + srcOffset, dataSize); + srcOffset += dataSize; - sendUDPPacket(0, packetData, packetSize); - } - LOG(logINFO, ("Sent frame: %d\n", frameNr)); - clock_gettime(CLOCK_REALTIME, &end); - int64_t timeNs = ((end.tv_sec - begin.tv_sec) * 1E9 + - (end.tv_nsec - begin.tv_nsec)); + sendUDPPacket(0, packetData, packetSize); + } + LOG(logINFO, ("Sent frame: %d [%lld]\n", frameNr, + (long long unsigned int)virtual_currentFrameNumber)); + clock_gettime(CLOCK_REALTIME, &end); + int64_t timeNs = + ((end.tv_sec - begin.tv_sec) * 1E9 + (end.tv_nsec - begin.tv_nsec)); - // sleep for (period - exptime) - if (frameNr < numFrames) { // if there is a next frame - if (periodNs > timeNs) { - usleep((periodNs - timeNs) / 1000); - } + // sleep for (period - exptime) + if (frameNr < numFrames) { // if there is a next frame + if (periodNs > timeNs) { + usleep((periodNs - timeNs) / 1000); } } + ++virtual_currentFrameNumber; } closeUDPSocket(0); - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -2362,46 +2340,47 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping State Machine\n")); -#ifdef VIRTUAL - if (!isControlServer) { - virtual_stop = 1; - ComVirtual_setStop(virtual_stop); - // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = ComVirtual_getStatus(); - } - virtual_stop = 0; - ComVirtual_setStop(virtual_stop); - LOG(logINFO, ("Stopped State Machine\n")); + // if scan active, stop scan first + if (sharedMemory_getScanStatus() == RUNNING) { + sharedMemory_setScanStop(1); } +#ifdef VIRTUAL + sharedMemory_setStop(1); + // read till status is idle + while (sharedMemory_getStatus() == RUNNING) + ; + sharedMemory_setStop(0); + LOG(logINFO, ("Stopped State Machine\n")); return OK; #endif // stop state machine bus_w(CONTROL_REG, bus_r(CONTROL_REG) | CONTROL_STP_ACQSTN_MSK); usleep(WAIT_TIME_US_STP_ACQ); bus_w(CONTROL_REG, bus_r(CONTROL_REG) & ~CONTROL_STP_ACQSTN_MSK); - LOG(logINFO, ("Status Register: %08x\n", bus_r(STATUS_REG))); return OK; } enum runStatus getRunStatus() { -#ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); + LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; } - if (virtual_status == 0) { - LOG(logINFOBLUE, ("Status: IDLE\n")); - return IDLE; - } else { + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } +#ifdef VIRTUAL + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } + LOG(logINFOBLUE, ("Status: IDLE\n")); + return IDLE; #endif - LOG(logDEBUG1, ("Getting status\n")); - uint32_t retval = bus_r(STATUS_REG); LOG(logINFO, ("Status Register: %08x\n", retval)); @@ -2672,10 +2651,7 @@ int readFrameFromFifo() { uint32_t runBusy() { #ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); - } - return virtual_status; + return ((sharedMemory_getStatus() == RUNNING) ? 1 : 0); #endif uint32_t s = (bus_r(STATUS_REG) & STATUS_RN_BSY_MSK); // LOG(logDEBUG1, ("Status Register: %08x\n", s)); diff --git a/slsDetectorServers/eigerDetectorServer/CMakeLists.txt b/slsDetectorServers/eigerDetectorServer/CMakeLists.txt index c4d8f5260..f39c7b1dc 100644 --- a/slsDetectorServers/eigerDetectorServer/CMakeLists.txt +++ b/slsDetectorServers/eigerDetectorServer/CMakeLists.txt @@ -5,7 +5,7 @@ set(src ../slsDetectorServer/src/communication_funcs.c ../slsDetectorServer/src/communication_funcs_UDP.c ../slsDetectorServer/src/common.c - ../slsDetectorServer/src/communication_virtual.c + ../slsDetectorServer/src/sharedMemory.c ) include_directories( diff --git a/slsDetectorServers/eigerDetectorServer/Makefile b/slsDetectorServers/eigerDetectorServer/Makefile index 614ea0cdf..ff75189d6 100755 --- a/slsDetectorServers/eigerDetectorServer/Makefile +++ b/slsDetectorServers/eigerDetectorServer/Makefile @@ -7,13 +7,13 @@ BLACKFIN_CC = bfin-uclinux-gcc CROSS = powerpc-4xx-softfloat- CC = $(CROSS)gcc CFLAGS += -Wall -std=gnu99 -DEIGERD -DSTOP_SERVER -I$(main_inc) -I$(support_lib) -I$(current_dir) #-DDEBUG1 #-DVERBOSEI #-DVERBOSE -LDLIBS += -lm +LDLIBS += -lm -lrt -pthread PROGS = eigerDetectorServer DESTDIR = bin INSTMODE = 0777 SRCS = slsDetectorFunctionList.c HardwareIO.c LocalLinkInterface.c FebInterface.c FebControl.c Beb.c -SRCS += $(main_src)communication_funcs.c $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs_UDP.c $(main_src)common.c +SRCS += $(main_src)communication_funcs.c $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs_UDP.c $(main_src)common.c $(main_src)/sharedMemory.c OBJS = $(SRCS:.c=.o) diff --git a/slsDetectorServers/eigerDetectorServer/bin/eigerDetectorServer_developer b/slsDetectorServers/eigerDetectorServer/bin/eigerDetectorServer_developer index 41b06baf9..0758c7b15 100755 Binary files a/slsDetectorServers/eigerDetectorServer/bin/eigerDetectorServer_developer and b/slsDetectorServers/eigerDetectorServer/bin/eigerDetectorServer_developer differ diff --git a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c index 62c9c7bd3..fd5a8e656 100644 --- a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c @@ -1,13 +1,12 @@ #include "slsDetectorFunctionList.h" #include "clogger.h" #include "common.h" +#include "sharedMemory.h" #include "versionAPI.h" #ifndef VIRTUAL #include "Beb.h" #include "FebControl.h" -#else -#include "communication_virtual.h" #endif #include @@ -71,8 +70,6 @@ int eiger_tau_ns = 0; #ifdef VIRTUAL pthread_t virtual_tid; -int virtual_status = 0; -int virtual_stop = 0; // values for virtual server int64_t eiger_virtual_exptime = 0; int64_t eiger_virtual_subexptime = 0; @@ -251,9 +248,10 @@ u_int64_t getDetectorMAC() { // execute and get address char output[255]; #ifdef VIRTUAL - FILE *sysFile = popen("cat /sys/class/net/$(ip route show default | awk " - "'/default/ {print $5}')/address", - "r"); + FILE *sysFile = + popen("cat /sys/class/net/$(ip route show default | grep -v vpn | awk " + "'/default/ {print $5}')/address", + "r"); #else FILE *sysFile = popen("more /sys/class/net/eth0/address", "r"); #endif @@ -288,9 +286,10 @@ u_int32_t getDetectorIP() { // execute and get address char output[255]; #ifdef VIRTUAL - FILE *sysFile = popen("ifconfig $(ip route show default | awk '/default/ " - "{print $5}') | grep 'inet ' | cut -d ' ' -f10", - "r"); + FILE *sysFile = + popen("ifconfig $(ip route show default | grep -v vpn | awk '/default/ " + "{print $5}') | grep 'inet ' | cut -d ' ' -f10", + "r"); #else FILE *sysFile = popen( "ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2", @@ -363,10 +362,7 @@ void initStopServer() { #ifdef VIRTUAL LOG(logINFOBLUE, ("Configuring Stop server\n")); getModuleConfiguration(); - virtual_stop = 0; - if (!isControlServer) { - ComVirtual_setStop(virtual_stop); - } + sharedMemory_setStop(0); // get top/master in virtual readConfigFile(); #else @@ -668,10 +664,7 @@ void setupDetector() { } } #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); #endif LOG(logINFOBLUE, ("Setting Default Parameters\n")); @@ -1274,6 +1267,7 @@ int setHighVoltage(int val) { if (master) { // set if (val != -1) { + LOG(logINFO, ("Setting High voltage: %d V\n", val)); eiger_theo_highvoltage = val; } return eiger_theo_highvoltage; @@ -1986,33 +1980,26 @@ int startStateMachine() { return FAIL; } LOG(logINFOBLUE, ("Starting State Machine\n")); - virtual_status = 1; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - virtual_stop = ComVirtual_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } + if (sharedMemory_getStop() != 0) { + LOG(logERROR, ("Cant start acquisition. " + "Stop server has not updated stop status to 0\n")); + return FAIL; } + sharedMemory_setStatus(RUNNING); if (pthread_create(&virtual_tid, NULL, &start_timer, NULL)) { LOG(logERROR, ("Could not start Virtual acquisition thread\n")); - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFO, ("Virtual Acquisition started\n")); return OK; #else - LOG(logINFOBLUE, ("Starting State Machine\n")); + LOG(logINFO, ("Acquisition started bit toggled\n")); int ret = OK, prev_flag; // get the DAQ toggle bit prev_flag = Feb_Control_AcquisitionStartedBit(); - LOG(logINFO, ("Going to start acquisition\n")); + LOG(logINFOBLUE, ("Starting State Machine\n")); Feb_Control_StartAcquisition(); LOG(logINFO, ("requesting images right after start\n")); @@ -2106,10 +2093,8 @@ void *start_timer(void *arg) { usleep(eiger_virtual_transmission_delay_frame); - // update the virtual stop from stop server - virtual_stop = ComVirtual_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { + // check if manual stop + if (sharedMemory_getStop() == 1) { setStartingFrameNumber(frameNr + iframes + 1); break; } @@ -2208,30 +2193,25 @@ void *start_timer(void *arg) { closeUDPSocket(0); closeUDPSocket(1); - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } #endif int stopStateMachine() { - LOG(logINFORED, ("Going to stop acquisition\n")); -#ifdef VIRTUAL - if (!isControlServer) { - virtual_stop = 1; - ComVirtual_setStop(virtual_stop); - // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = ComVirtual_getStatus(); - } - virtual_stop = 0; - ComVirtual_setStop(virtual_stop); - LOG(logINFO, ("Stopped State Machine\n")); + LOG(logINFORED, ("Stopping state machine\n")); + // if scan active, stop scan + if (sharedMemory_getScanStatus() == RUNNING) { + sharedMemory_setScanStop(1); } +#ifdef VIRTUAL + sharedMemory_setStop(1); + // read till status is idle + while (sharedMemory_getStatus() == RUNNING) + ; + sharedMemory_setStop(0); + LOG(logINFO, ("Stopped State Machine\n")); return OK; #else if ((Feb_Control_StopAcquisition() != STATUS_IDLE) || @@ -2287,19 +2267,24 @@ int startReadOut() { } enum runStatus getRunStatus() { -#ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); + LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; } - if (virtual_status == 0) { - LOG(logINFO, ("Status: IDLE\n")); - return IDLE; - } else { - LOG(logINFO, ("Status: RUNNING...\n")); + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); return RUNNING; } +#ifdef VIRTUAL + if (sharedMemory_getStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: RUNNING\n")); + return RUNNING; + } + LOG(logINFOBLUE, ("Status: IDLE\n")); + return IDLE; #else - int i = Feb_Control_AcquisitionInProgress(); if (i == STATUS_ERROR) { LOG(logERROR, ("Status: ERROR reading status register\n")); @@ -2324,7 +2309,7 @@ enum runStatus getRunStatus() { void readFrame(int *ret, char *mess) { #ifdef VIRTUAL // wait for status to be done - while (virtual_status == 1) { + while (sharedMemory_getStatus() == RUNNING) { usleep(500); } LOG(logINFOGREEN, ("acquisition successfully finished\n")); diff --git a/slsDetectorServers/gotthard2DetectorServer/CMakeLists.txt b/slsDetectorServers/gotthard2DetectorServer/CMakeLists.txt index 9fb311b04..02dd438f2 100644 --- a/slsDetectorServers/gotthard2DetectorServer/CMakeLists.txt +++ b/slsDetectorServers/gotthard2DetectorServer/CMakeLists.txt @@ -11,7 +11,7 @@ add_executable(gotthard2DetectorServer_virtual ../slsDetectorServer/src/ALTERA_PLL_CYCLONE10.c ../slsDetectorServer/src/ASIC_Driver.c ../slsDetectorServer/src/programFpgaNios.c - ../slsDetectorServer/src/communication_virtual.c + ../slsDetectorServer/src/sharedMemory.c ) include_directories( diff --git a/slsDetectorServers/gotthard2DetectorServer/Makefile b/slsDetectorServers/gotthard2DetectorServer/Makefile index eb6f71427..273630224 100755 --- a/slsDetectorServers/gotthard2DetectorServer/Makefile +++ b/slsDetectorServers/gotthard2DetectorServer/Makefile @@ -6,13 +6,13 @@ support_lib = ../../slsSupportLib/include/ CROSS = nios2-buildroot-linux-gnu- CC = $(CROSS)gcc CFLAGS += -Wall -std=gnu99 -DGOTTHARD2D -DSTOP_SERVER -I$(main_inc) -I$(support_lib) -I$(current_dir) #-DDEBUG1 #-DVERBOSEI #-DVERBOSE -LDLIBS += -lm +LDLIBS += -lm -lrt -pthread PROGS = gotthard2DetectorServer DESTDIR ?= bin INSTMODE = 0777 SRCS = slsDetectorFunctionList.c -SRCS += $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs.c $(main_src)nios.c $(main_src)common.c $(main_src)DAC6571.c $(main_src)LTC2620_Driver.c $(main_src)ALTERA_PLL_CYCLONE10.c $(main_src)ASIC_Driver.c $(main_src)/programFpgaNios.c +SRCS += $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs.c $(main_src)nios.c $(main_src)common.c $(main_src)DAC6571.c $(main_src)LTC2620_Driver.c $(main_src)ALTERA_PLL_CYCLONE10.c $(main_src)ASIC_Driver.c $(main_src)/programFpgaNios.c $(main_src)/sharedMemory.c OBJS = $(SRCS:.c=.o) diff --git a/slsDetectorServers/gotthard2DetectorServer/bin/gotthard2DetectorServer_developer b/slsDetectorServers/gotthard2DetectorServer/bin/gotthard2DetectorServer_developer index 2b91792dc..073079385 100755 Binary files a/slsDetectorServers/gotthard2DetectorServer/bin/gotthard2DetectorServer_developer and b/slsDetectorServers/gotthard2DetectorServer/bin/gotthard2DetectorServer_developer differ diff --git a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c index bb1cc01c4..6db979921 100644 --- a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c @@ -6,10 +6,10 @@ #include "RegisterDefs.h" #include "clogger.h" #include "common.h" +#include "sharedMemory.h" #include "versionAPI.h" #ifdef VIRTUAL #include "communication_funcs_UDP.h" -#include "communication_virtual.h" #endif #include @@ -37,8 +37,7 @@ char initErrorMessage[MAX_STR_LENGTH]; #ifdef VIRTUAL pthread_t pthread_virtual_tid; -int virtual_status = 0; -int virtual_stop = 0; +int64_t virtual_currentFrameNumber = 2; #endif enum detectorSettings thisSettings = UNINITIALIZED; @@ -342,10 +341,7 @@ void initStopServer() { exit(EXIT_FAILURE); } #ifdef VIRTUAL - virtual_stop = 0; - if (!isControlServer) { - ComVirtual_setStop(virtual_stop); - } + sharedMemory_setStop(0); #endif } @@ -393,10 +389,7 @@ void setupDetector() { } } #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); #endif // pll defines @@ -1205,17 +1198,11 @@ int setHighVoltage(int val) { val = HV_SOFT_MAX_VOLTAGE; } -#ifdef VIRTUAL - if (val >= 0) - highvoltage = val; - return highvoltage; -#endif - // setting hv if (val >= 0) { LOG(logINFO, ("Setting High voltage: %d V\n", val)); - DAC6571_Set(val); - highvoltage = val; + if (DAC6571_Set(val) == OK) + highvoltage = val; } return highvoltage; } @@ -2240,22 +2227,15 @@ int startStateMachine() { } LOG(logINFOBLUE, ("Starting State Machine\n")); // set status to running - virtual_status = 1; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - virtual_stop = ComVirtual_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } + if (sharedMemory_getStop() != 0) { + LOG(logERROR, ("Cant start acquisition. " + "Stop server has not updated stop status to 0\n")); + return FAIL; } + sharedMemory_setStatus(RUNNING); if (pthread_create(&pthread_virtual_tid, NULL, &start_timer, NULL)) { LOG(logERROR, ("Could not start Virtual acquisition thread\n")); - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFOGREEN, ("Virtual Acquisition started\n")); @@ -2310,86 +2290,80 @@ void *start_timer(void *arg) { *((uint16_t *)(vetoData + i)) = i; } - { - int frameHeaderNr = 0; - // loop over number of repeats - for (int repeatNr = 0; repeatNr != numRepeats; ++repeatNr) { + // loop over number of repeats + for (int repeatNr = 0; repeatNr != numRepeats; ++repeatNr) { - struct timespec rbegin, rend; - clock_gettime(CLOCK_REALTIME, &rbegin); + struct timespec rbegin, rend; + clock_gettime(CLOCK_REALTIME, &rbegin); - // loop over number of frames - for (int frameNr = 0; frameNr != numFrames; ++frameNr) { + // loop over number of frames + for (int frameNr = 0; frameNr != numFrames; ++frameNr) { - // update the virtual stop from stop server - virtual_stop = ComVirtual_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { - break; - } + // check if manual stop + if (sharedMemory_getStop() == 1) { + break; + } - // sleep for exposure time - struct timespec begin, end; - clock_gettime(CLOCK_REALTIME, &begin); - usleep(expUs); + // sleep for exposure time + struct timespec begin, end; + clock_gettime(CLOCK_REALTIME, &begin); + usleep(expUs); - // first interface - char packetData[packetsize]; - memset(packetData, 0, packetsize); + // first interface + char packetData[packetsize]; + memset(packetData, 0, packetsize); + // set header + sls_detector_header *header = (sls_detector_header *)(packetData); + header->detType = (uint16_t)myDetectorType; + header->version = SLS_DETECTOR_HEADER_VERSION - 1; + header->frameNumber = virtual_currentFrameNumber; + header->packetNumber = 0; + header->modId = 0; + header->row = detPos[X]; + header->column = detPos[Y]; + // fill data + memcpy(packetData + sizeof(sls_detector_header), imageData, + datasize); + // send 1 packet = 1 frame + sendUDPPacket(0, packetData, packetsize); + + // second interface (veto) + char packetData2[vetopacketsize]; + memset(packetData2, 0, vetopacketsize); + if (vetoEnabled && numInterfaces == 2) { // set header - sls_detector_header *header = - (sls_detector_header *)(packetData); - header->detType = (uint16_t)myDetectorType; - header->version = SLS_DETECTOR_HEADER_VERSION - 1; - header->frameNumber = frameHeaderNr; - header->packetNumber = 0; - header->modId = 0; - header->row = detPos[X]; - header->column = detPos[Y]; + veto_header *header = (veto_header *)(packetData2); + header->frameNumber = virtual_currentFrameNumber; + header->bunchId = 0; // fill data - memcpy(packetData + sizeof(sls_detector_header), imageData, - datasize); + memcpy(packetData2 + sizeof(veto_header), vetoData, + vetodatasize); // send 1 packet = 1 frame - sendUDPPacket(0, packetData, packetsize); + sendUDPPacket(1, packetData2, vetopacketsize); + } + LOG(logINFO, + ("Sent frame: %d (bursts: %d) [%lld]\n", frameNr, repeatNr, + (long long unsigned int)virtual_currentFrameNumber)); + clock_gettime(CLOCK_REALTIME, &end); + int64_t timeNs = ((end.tv_sec - begin.tv_sec) * 1E9 + + (end.tv_nsec - begin.tv_nsec)); - // second interface (veto) - char packetData2[vetopacketsize]; - memset(packetData2, 0, vetopacketsize); - if (vetoEnabled && numInterfaces == 2) { - // set header - veto_header *header = (veto_header *)(packetData2); - header->frameNumber = frameHeaderNr; - header->bunchId = 0; - // fill data - memcpy(packetData2 + sizeof(veto_header), vetoData, - vetodatasize); - // send 1 packet = 1 frame - sendUDPPacket(1, packetData2, vetopacketsize); - } - ++frameHeaderNr; - - clock_gettime(CLOCK_REALTIME, &end); - LOG(logINFO, - ("Sent frame: %d (bursts: %d)\n", frameNr, repeatNr)); - int64_t timeNs = ((end.tv_sec - begin.tv_sec) * 1E9 + - (end.tv_nsec - begin.tv_nsec)); - - // sleep for (period - exptime) - if (frameNr < numFrames) { // if there is a next frame - if (periodNs > timeNs) { - usleep((periodNs - timeNs) / 1000); - } + // sleep for (period - exptime) + if (frameNr < numFrames) { // if there is a next frame + if (periodNs > timeNs) { + usleep((periodNs - timeNs) / 1000); } } - clock_gettime(CLOCK_REALTIME, &rend); - int64_t timeNs = ((rend.tv_sec - rbegin.tv_sec) * 1E9 + - (rend.tv_nsec - rbegin.tv_nsec)); + ++virtual_currentFrameNumber; + } + clock_gettime(CLOCK_REALTIME, &rend); + int64_t timeNs = ((rend.tv_sec - rbegin.tv_sec) * 1E9 + + (rend.tv_nsec - rbegin.tv_nsec)); - // sleep for (repeatPeriodNs - time remaining) - if (repeatNr < numRepeats) { // if there is a next repeat - if (repeatPeriodNs > timeNs) { - usleep((repeatPeriodNs - timeNs) / 1000); - } + // sleep for (repeatPeriodNs - time remaining) + if (repeatNr < numRepeats) { // if there is a next repeat + if (repeatPeriodNs > timeNs) { + usleep((repeatPeriodNs - timeNs) / 1000); } } } @@ -2399,10 +2373,7 @@ void *start_timer(void *arg) { closeUDPSocket(1); } - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -2410,19 +2381,16 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping State Machine\n")); -#ifdef VIRTUAL - if (!isControlServer) { - virtual_stop = 1; - ComVirtual_setStop(virtual_stop); - // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = ComVirtual_getStatus(); - } - virtual_stop = 0; - ComVirtual_setStop(virtual_stop); - LOG(logINFO, ("Stopped State Machine\n")); + // if scan active, stop scan + if (sharedMemory_getScanStatus() == RUNNING) { + sharedMemory_setScanStop(1); } +#ifdef VIRTUAL + sharedMemory_setStop(1); + while (sharedMemory_getStatus() == RUNNING) + ; + sharedMemory_setStop(0); + LOG(logINFO, ("Stopped State Machine\n")); return OK; #endif // stop state machine @@ -2432,19 +2400,24 @@ int stopStateMachine() { } enum runStatus getRunStatus() { -#ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); + LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; } - if (virtual_status == 0) { - LOG(logINFOBLUE, ("Status: IDLE\n")); - return IDLE; - } else { + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } +#ifdef VIRTUAL + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } + LOG(logINFOBLUE, ("Status: IDLE\n")); + return IDLE; #endif - LOG(logDEBUG1, ("Getting status\n")); uint32_t retval = bus_r(FLOW_STATUS_REG); LOG(logINFO, ("Status Register: %08x\n", retval)); @@ -2511,10 +2484,7 @@ void readFrame(int *ret, char *mess) { u_int32_t runBusy() { #ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); - } - return virtual_status; + return ((sharedMemory_getStatus() == RUNNING) ? 1 : 0); #endif u_int32_t s = (bus_r(FLOW_STATUS_REG) & FLOW_STATUS_RUN_BUSY_MSK); // LOG(logDEBUG1, ("Status Register: %08x\n", s)); diff --git a/slsDetectorServers/gotthardDetectorServer/CMakeLists.txt b/slsDetectorServers/gotthardDetectorServer/CMakeLists.txt index 31c30b70e..c4e2e3130 100644 --- a/slsDetectorServers/gotthardDetectorServer/CMakeLists.txt +++ b/slsDetectorServers/gotthardDetectorServer/CMakeLists.txt @@ -10,7 +10,7 @@ add_executable(gotthardDetectorServer_virtual ../slsDetectorServer/src/common.c ../slsDetectorServer/src/commonServerFunctions.c ../slsDetectorServer/src/communication_funcs_UDP.c - ../slsDetectorServer/src/communication_virtual.c + ../slsDetectorServer/src/sharedMemory.c ) include_directories( diff --git a/slsDetectorServers/gotthardDetectorServer/Makefile b/slsDetectorServers/gotthardDetectorServer/Makefile index b54210754..2264c5554 100755 --- a/slsDetectorServers/gotthardDetectorServer/Makefile +++ b/slsDetectorServers/gotthardDetectorServer/Makefile @@ -6,13 +6,13 @@ support_lib = ../../slsSupportLib/include/ CROSS = bfin-uclinux- CC = $(CROSS)gcc CFLAGS += -Wall -std=gnu99 -DGOTTHARDD -DSTOP_SERVER -I$(main_inc) -I$(support_lib) -I$(current_dir)#-DVERBOSEI #-DVERBOSE -LDLIBS += -lm +LDLIBS += -lm -lrt -pthread PROGS = gotthardDetectorServer DESTDIR ?= bin INSTMODE = 0777 SRCS = slsDetectorFunctionList.c -SRCS += $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs.c $(main_src)blackfin.c $(main_src)AD9252.c $(main_src)AD9257.c $(main_src)LTC2620.c $(main_src)common.c $(main_src)commonServerFunctions.c +SRCS += $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs.c $(main_src)blackfin.c $(main_src)AD9252.c $(main_src)AD9257.c $(main_src)LTC2620.c $(main_src)common.c $(main_src)commonServerFunctions.c $(main_src)/sharedMemory.c OBJS = $(SRCS:.c=.o) all: clean $(PROGS) diff --git a/slsDetectorServers/gotthardDetectorServer/bin/gotthardDetectorServer_developer b/slsDetectorServers/gotthardDetectorServer/bin/gotthardDetectorServer_developer index 238d52e60..82c904c4d 100755 Binary files a/slsDetectorServers/gotthardDetectorServer/bin/gotthardDetectorServer_developer and b/slsDetectorServers/gotthardDetectorServer/bin/gotthardDetectorServer_developer differ diff --git a/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c index 58b9b1c21..4f1133d6c 100644 --- a/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c @@ -1,12 +1,12 @@ #include "slsDetectorFunctionList.h" #include "RegisterDefs.h" #include "clogger.h" +#include "sharedMemory.h" #include "versionAPI.h" #include "LTC2620.h" // dacs #ifdef VIRTUAL #include "communication_funcs_UDP.h" -#include "communication_virtual.h" #endif #include "string.h" @@ -34,9 +34,8 @@ char initErrorMessage[MAX_STR_LENGTH]; #ifdef VIRTUAL pthread_t pthread_virtual_tid; -int virtual_status = 0; -int virtual_stop = 0; int highvoltage = 0; +int64_t virtual_currentFrameNumber = 2; #endif int detPos[2] = {}; @@ -356,10 +355,7 @@ void initStopServer() { exit(EXIT_FAILURE); } #ifdef VIRTUAL - virtual_stop = 0; - if (!isControlServer) { - ComVirtual_setStop(virtual_stop); - } + sharedMemory_setStop(0); #endif } @@ -369,10 +365,7 @@ void setupDetector() { LOG(logINFO, ("This Server is for 1 Gotthard module (1280 channels)\n")); #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); #endif // Initialization @@ -1516,22 +1509,15 @@ int startStateMachine() { return FAIL; } LOG(logINFOBLUE, ("Starting State Machine\n")); - virtual_status = 1; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - virtual_stop = ComVirtual_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } + if (sharedMemory_getStop() != 0) { + LOG(logERROR, ("Cant start acquisition. " + "Stop server has not updated stop status to 0\n")); + return FAIL; } + sharedMemory_setStatus(RUNNING); if (pthread_create(&pthread_virtual_tid, NULL, &start_timer, NULL)) { LOG(logERROR, ("Could not start Virtual acquisition thread\n")); - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFOGREEN, ("Virtual Acquisition started\n")); @@ -1576,59 +1562,52 @@ void *start_timer(void *arg) { } // Send data - { - uint16_t frameHeaderNr = 2; - // loop over number of frames - for (int frameNr = 0; frameNr != numFrames; ++frameNr) { + // loop over number of frames + for (int frameNr = 0; frameNr != numFrames; ++frameNr) { - // update the virtual stop from stop server - virtual_stop = ComVirtual_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { - break; - } + // check if manual stop + if (sharedMemory_getStop() == 1) { + break; + } - // sleep for exposure time - struct timespec begin, end; - clock_gettime(CLOCK_REALTIME, &begin); - usleep(expUs); + // sleep for exposure time + struct timespec begin, end; + clock_gettime(CLOCK_REALTIME, &begin); + usleep(expUs); - int srcOffset = 0; - // loop packet - for (int i = 0; i != packetsPerFrame; ++i) { + int srcOffset = 0; + // loop packet + for (int i = 0; i != packetsPerFrame; ++i) { - char packetData[packetSize]; - memset(packetData, 0, packetSize); - // set header - *((uint16_t *)(packetData)) = frameHeaderNr; - ++frameHeaderNr; + char packetData[packetSize]; + memset(packetData, 0, packetSize); + // set header + *((uint16_t *)(packetData)) = virtual_currentFrameNumber; + ++virtual_currentFrameNumber; - // fill data - memcpy(packetData + 4, imageData + srcOffset, dataSize); - srcOffset += dataSize; + // fill data + memcpy(packetData + 4, imageData + srcOffset, dataSize); + srcOffset += dataSize; - sendUDPPacket(0, packetData, packetSize); - } - LOG(logINFO, ("Sent frame: %d\n", frameNr)); - clock_gettime(CLOCK_REALTIME, &end); - int64_t timeNs = ((end.tv_sec - begin.tv_sec) * 1E9 + - (end.tv_nsec - begin.tv_nsec)); + sendUDPPacket(0, packetData, packetSize); + } + LOG(logINFO, + ("Sent frame: %d [%d]\n", frameNr, virtual_currentFrameNumber)); + clock_gettime(CLOCK_REALTIME, &end); + int64_t timeNs = + ((end.tv_sec - begin.tv_sec) * 1E9 + (end.tv_nsec - begin.tv_nsec)); - // sleep for (period - exptime) - if (frameNr < numFrames) { // if there is a next frame - if (periodNs > timeNs) { - usleep((periodNs - timeNs) / 1000); - } + // sleep for (period - exptime) + if (frameNr < numFrames) { // if there is a next frame + if (periodNs > timeNs) { + usleep((periodNs - timeNs) / 1000); } } } closeUDPSocket(0); - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -1636,17 +1615,17 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping State Machine\n")); + // if scan active, stop scan + if (sharedMemory_getScanStatus() == RUNNING) { + sharedMemory_setScanStop(1); + } #ifdef VIRTUAL if (!isControlServer) { - virtual_stop = 1; - ComVirtual_setStop(virtual_stop); + sharedMemory_setStop(1); // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = ComVirtual_getStatus(); - } - virtual_stop = 0; - ComVirtual_setStop(virtual_stop); + while (sharedMemory_getStatus() == RUNNING) + ; + sharedMemory_setStop(0); LOG(logINFO, ("Stopped State Machine\n")); } return OK; @@ -1669,19 +1648,24 @@ int stopStateMachine() { } enum runStatus getRunStatus() { -#ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); + LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; } - if (virtual_status == 0) { - LOG(logINFOBLUE, ("Status: IDLE\n")); - return IDLE; - } else { + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } +#ifdef VIRTUAL + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } + LOG(logINFOBLUE, ("Status: IDLE\n")); + return IDLE; #endif - LOG(logDEBUG1, ("Getting status\n")); enum runStatus s = IDLE; u_int32_t retval = runState(logINFO); @@ -1751,7 +1735,7 @@ enum runStatus getRunStatus() { void readFrame(int *ret, char *mess) { #ifdef VIRTUAL - while (virtual_status) { + while (sharedMemory_getStatus() == RUNNING) { // LOG(logERROR, ("Waiting for finished flag\n"); usleep(5000); } @@ -1775,17 +1759,14 @@ void readFrame(int *ret, char *mess) { u_int32_t runBusy() { #ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); - } - return virtual_status; + return ((sharedMemory_getStatus() == RUNNING) ? 1 : 0); #endif return runState(logDEBUG1) & STATUS_RN_BSY_MSK; } u_int32_t runState(enum TLogLevel lev) { #ifdef VIRTUAL - return virtual_status; + return (int)sharedMemory_getStatus(); #endif u_int32_t s = bus_r(STATUS_REG); LOG(lev, ("Status Register: 0x%08x\n", s)); diff --git a/slsDetectorServers/jungfrauDetectorServer/CMakeLists.txt b/slsDetectorServers/jungfrauDetectorServer/CMakeLists.txt index 2ae085da8..ded49a47c 100644 --- a/slsDetectorServers/jungfrauDetectorServer/CMakeLists.txt +++ b/slsDetectorServers/jungfrauDetectorServer/CMakeLists.txt @@ -12,7 +12,7 @@ add_executable(jungfrauDetectorServer_virtual ../slsDetectorServer/src/MAX1932.c ../slsDetectorServer/src/programFpgaBlackfin.c ../slsDetectorServer/src/communication_funcs_UDP.c - ../slsDetectorServer/src/communication_virtual.c + ../slsDetectorServer/src/sharedMemory.c ) target_include_directories(jungfrauDetectorServer_virtual diff --git a/slsDetectorServers/jungfrauDetectorServer/Makefile b/slsDetectorServers/jungfrauDetectorServer/Makefile index 8b3fef852..1a225159c 100755 --- a/slsDetectorServers/jungfrauDetectorServer/Makefile +++ b/slsDetectorServers/jungfrauDetectorServer/Makefile @@ -6,13 +6,13 @@ support_lib = ../../slsSupportLib/include/ CROSS = bfin-uclinux- CC = $(CROSS)gcc CFLAGS += -Wall -std=gnu99 -DJUNGFRAUD -DSTOP_SERVER -I$(main_inc) -I$(support_lib) -I$(current_dir)#-DVERBOSEI #-DVERBOSE -LDLIBS += -lm +LDLIBS += -lm -lrt -pthread PROGS = jungfrauDetectorServer DESTDIR ?= bin INSTMODE = 0777 SRCS = slsDetectorFunctionList.c -SRCS += $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs.c $(main_src)blackfin.c $(main_src)common.c $(main_src)commonServerFunctions.c $(main_src)AD9257.c $(main_src)ALTERA_PLL.c $(main_src)LTC2620.c $(main_src)MAX1932.c $(main_src)programFpgaBlackfin.c +SRCS += $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs.c $(main_src)blackfin.c $(main_src)common.c $(main_src)commonServerFunctions.c $(main_src)AD9257.c $(main_src)ALTERA_PLL.c $(main_src)LTC2620.c $(main_src)MAX1932.c $(main_src)programFpgaBlackfin.c $(main_src)/sharedMemory.c OBJS = $(SRCS:.c=.o) diff --git a/slsDetectorServers/jungfrauDetectorServer/bin/jungfrauDetectorServer_developer b/slsDetectorServers/jungfrauDetectorServer/bin/jungfrauDetectorServer_developer index f552fa48e..23ab9d5c5 100755 Binary files a/slsDetectorServers/jungfrauDetectorServer/bin/jungfrauDetectorServer_developer and b/slsDetectorServers/jungfrauDetectorServer/bin/jungfrauDetectorServer_developer differ diff --git a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c index 5e757661e..dce0af46c 100644 --- a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c @@ -1,5 +1,6 @@ #include "slsDetectorFunctionList.h" #include "clogger.h" +#include "sharedMemory.h" #include "versionAPI.h" #include "ALTERA_PLL.h" // pll @@ -8,7 +9,6 @@ #include "common.h" #ifdef VIRTUAL #include "communication_funcs_UDP.h" -#include "communication_virtual.h" #endif #include @@ -36,8 +36,6 @@ char initErrorMessage[MAX_STR_LENGTH]; #ifdef VIRTUAL pthread_t pthread_virtual_tid; -int virtual_status = 0; -int virtual_stop = 0; int virtual_image_test_mode = 0; #endif @@ -363,10 +361,7 @@ void initStopServer() { exit(EXIT_FAILURE); } #ifdef VIRTUAL - virtual_stop = 0; - if (!isControlServer) { - ComVirtual_setStop(virtual_stop); - } + sharedMemory_setStop(0); // temp threshold and reset event (read by stop server) setThresholdTemperature(DEFAULT_TMP_THRSHLD); setTemperatureEvent(0); @@ -382,10 +377,7 @@ void setupDetector() { clkPhase[i] = 0; } #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); #endif ALTERA_PLL_ResetPLL(); @@ -1672,22 +1664,15 @@ int startStateMachine() { return FAIL; } LOG(logINFOBLUE, ("starting state machine\n")); - virtual_status = 1; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - virtual_stop = ComVirtual_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } + if (sharedMemory_getStop() != 0) { + LOG(logERROR, ("Cant start acquisition. " + "Stop server has not updated stop status to 0\n")); + return FAIL; } + sharedMemory_setStatus(RUNNING); if (pthread_create(&pthread_virtual_tid, NULL, &start_timer, NULL)) { LOG(logERROR, ("Could not start Virtual acquisition thread\n")); - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFOGREEN, ("Virtual Acquisition started\n")); @@ -1740,10 +1725,8 @@ void *start_timer(void *arg) { usleep(transmissionDelayUs); - // update the virtual stop from stop server - virtual_stop = ComVirtual_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { + // check if manual stop + if (sharedMemory_getStop() == 1) { setStartingFrameNumber(frameNr + iframes + 1); break; } @@ -1818,10 +1801,7 @@ void *start_timer(void *arg) { closeUDPSocket(1); } - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -1829,19 +1809,17 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping State Machine\n")); -#ifdef VIRTUAL - if (!isControlServer) { - virtual_stop = 1; - ComVirtual_setStop(virtual_stop); - // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = ComVirtual_getStatus(); - } - virtual_stop = 0; - ComVirtual_setStop(virtual_stop); - LOG(logINFO, ("Stopped State Machine\n")); + // if scan active, stop scan + if (sharedMemory_getScanStatus() == RUNNING) { + sharedMemory_setScanStop(1); } +#ifdef VIRTUAL + sharedMemory_setStop(1); + // read till status is idle + while (sharedMemory_getStatus() == RUNNING) + ; + sharedMemory_setStop(0); + LOG(logINFO, ("Stopped State Machine\n")); return OK; #endif // stop state machine @@ -1854,19 +1832,24 @@ int stopStateMachine() { } enum runStatus getRunStatus() { -#ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); + LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; } - if (virtual_status == 0) { - LOG(logINFOBLUE, ("Status: IDLE\n")); - return IDLE; - } else { + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } +#ifdef VIRTUAL + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } + LOG(logINFOBLUE, ("Status: IDLE\n")); + return IDLE; #endif - LOG(logDEBUG1, ("Getting status\n")); enum runStatus s; u_int32_t retval = bus_r(STATUS_REG); @@ -1928,10 +1911,7 @@ void readFrame(int *ret, char *mess) { u_int32_t runBusy() { #ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); - } - return virtual_status; + return ((sharedMemory_getStatus() == RUNNING) ? 1 : 0); #endif u_int32_t s = (bus_r(STATUS_REG) & RUN_BUSY_MSK); LOG(logDEBUG1, ("Status Register: %08x\n", s)); diff --git a/slsDetectorServers/moenchDetectorServer/CMakeLists.txt b/slsDetectorServers/moenchDetectorServer/CMakeLists.txt index 2b9ae2846..3e8ec94cf 100644 --- a/slsDetectorServers/moenchDetectorServer/CMakeLists.txt +++ b/slsDetectorServers/moenchDetectorServer/CMakeLists.txt @@ -14,7 +14,7 @@ add_executable(moenchDetectorServer_virtual ../slsDetectorServer/src/MAX1932.c ../slsDetectorServer/src/programFpgaBlackfin.c ../slsDetectorServer/src/readDefaultPattern.c - ../slsDetectorServer/src/communication_virtual.c + ../slsDetectorServer/src/sharedMemory.c ) include_directories( diff --git a/slsDetectorServers/moenchDetectorServer/Makefile b/slsDetectorServers/moenchDetectorServer/Makefile index f66350e35..a4d07b266 100755 --- a/slsDetectorServers/moenchDetectorServer/Makefile +++ b/slsDetectorServers/moenchDetectorServer/Makefile @@ -6,13 +6,13 @@ support_lib = ../../slsSupportLib/include/ CROSS = bfin-uclinux- CC = $(CROSS)gcc CFLAGS += -Wall -std=gnu99 -DMOENCHD -DSTOP_SERVER -I$(main_inc) -I$(support_lib) -I$(current_dir)#-DVERBOSEI #-DVERBOSE -LDLIBS += -lm +LDLIBS += -lm -lrt -pthread PROGS = moenchDetectorServer DESTDIR ?= bin INSTMODE = 0777 SRCS = slsDetectorFunctionList.c -SRCS += $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs.c $(main_src)blackfin.c $(main_src)common.c $(main_src)commonServerFunctions.c $(main_src)communication_funcs_UDP.c $(main_src)UDPPacketHeaderGenerator.c $(main_src)AD9257.c $(main_src)ALTERA_PLL.c $(main_src)LTC2620.c $(main_src)MAX1932.c $(main_src)programFpgaBlackfin.c $(main_src)readDefaultPattern.c +SRCS += $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs.c $(main_src)blackfin.c $(main_src)common.c $(main_src)commonServerFunctions.c $(main_src)communication_funcs_UDP.c $(main_src)UDPPacketHeaderGenerator.c $(main_src)AD9257.c $(main_src)ALTERA_PLL.c $(main_src)LTC2620.c $(main_src)MAX1932.c $(main_src)programFpgaBlackfin.c $(main_src)readDefaultPattern.c $(main_src)/sharedMemory.c OBJS = $(SRCS:.c=.o) diff --git a/slsDetectorServers/moenchDetectorServer/bin/moenchDetectorServer_developer b/slsDetectorServers/moenchDetectorServer/bin/moenchDetectorServer_developer index 87e2d4eeb..fb60d27ec 100755 Binary files a/slsDetectorServers/moenchDetectorServer/bin/moenchDetectorServer_developer and b/slsDetectorServers/moenchDetectorServer/bin/moenchDetectorServer_developer differ diff --git a/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c index c01d93792..9d1dd562e 100644 --- a/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c @@ -1,5 +1,6 @@ #include "slsDetectorFunctionList.h" #include "clogger.h" +#include "sharedMemory.h" #include "versionAPI.h" #include "ALTERA_PLL.h" // pll @@ -8,9 +9,6 @@ #include "UDPPacketHeaderGenerator.h" #include "common.h" #include "communication_funcs_UDP.h" -#ifdef VIRTUAL -#include "communication_virtual.h" -#endif #include #include @@ -41,9 +39,8 @@ char initErrorMessage[MAX_STR_LENGTH]; #ifdef VIRTUAL pthread_t pthread_virtual_tid; -int virtual_status = 0; -int virtual_stop = 0; uint64_t virtual_pattern[MAX_PATTERN_LENGTH]; +int64_t virtual_currentFrameNumber = 2; #endif // 1g readout @@ -431,10 +428,7 @@ void initStopServer() { exit(EXIT_FAILURE); } #ifdef VIRTUAL - virtual_stop = 0; - if (!isControlServer) { - ComVirtual_setStop(virtual_stop); - } + sharedMemory_setStop(0); #endif } @@ -487,10 +481,7 @@ void setupDetector() { adcEnableMask_10g = 0; nSamples = 1; #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); memset(virtual_pattern, 0, sizeof(virtual_pattern)); #endif @@ -1855,22 +1846,15 @@ int startStateMachine() { return FAIL; } LOG(logINFOBLUE, ("Starting State Machine\n")); - virtual_status = 1; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - virtual_stop = ComVirtual_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } + if (sharedMemory_getStop() != 0) { + LOG(logERROR, ("Cant start acquisition. " + "Stop server has not updated stop status to 0\n")); + return FAIL; } + sharedMemory_setStatus(RUNNING); if (pthread_create(&pthread_virtual_tid, NULL, &start_timer, NULL)) { LOG(logERROR, ("Could not start Virtual acquisition thread\n")); - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFOGREEN, ("Virtual Acquisition started\n")); @@ -1931,10 +1915,8 @@ void *start_timer(void *arg) { // loop over number of frames for (int frameNr = 0; frameNr != numFrames; ++frameNr) { - // update the virtual stop from stop server - virtual_stop = ComVirtual_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { + // check if manual stop + if (sharedMemory_getStop() == 1) { break; } @@ -1952,7 +1934,7 @@ void *start_timer(void *arg) { sls_detector_header *header = (sls_detector_header *)(packetData); header->detType = (uint16_t)myDetectorType; header->version = SLS_DETECTOR_HEADER_VERSION - 1; - header->frameNumber = frameNr; + header->frameNumber = virtual_currentFrameNumber; header->packetNumber = i; header->modId = 0; header->row = detPos[X]; @@ -1965,7 +1947,8 @@ void *start_timer(void *arg) { sendUDPPacket(0, packetData, packetSize); } - LOG(logINFO, ("Sent frame: %d\n", frameNr)); + LOG(logINFO, ("Sent frame: %d [%lld]\n", frameNr, + (long long unsigned int)virtual_currentFrameNumber)); clock_gettime(CLOCK_REALTIME, &end); int64_t timeNs = ((end.tv_sec - begin.tv_sec) * 1E9 + (end.tv_nsec - begin.tv_nsec)); @@ -1976,14 +1959,12 @@ void *start_timer(void *arg) { usleep((periodNs - timeNs) / 1000); } } + ++virtual_currentFrameNumber; } closeUDPSocket(0); - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -1991,19 +1972,17 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping State Machine\n")); -#ifdef VIRTUAL - if (!isControlServer) { - virtual_stop = 1; - ComVirtual_setStop(virtual_stop); - // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = ComVirtual_getStatus(); - } - virtual_stop = 0; - ComVirtual_setStop(virtual_stop); - LOG(logINFO, ("Stopped State Machine\n")); + // if scan active, stop scan + if (sharedMemory_getScanStatus() == RUNNING) { + sharedMemory_setScanStop(1); } +#ifdef VIRTUAL + sharedMemory_setStop(1); + // read till status is idle + while (sharedMemory_getStatus() == RUNNING) + ; + sharedMemory_setStop(0); + LOG(logINFO, ("Stopped State Machine\n")); return OK; #endif // stop state machine @@ -2017,19 +1996,24 @@ int stopStateMachine() { } enum runStatus getRunStatus() { -#ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); + LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; } - if (virtual_status == 0) { - LOG(logINFOBLUE, ("Status: IDLE\n")); - return IDLE; - } else { + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } +#ifdef VIRTUAL + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } + LOG(logINFOBLUE, ("Status: IDLE\n")); + return IDLE; #endif - LOG(logDEBUG1, ("Getting status\n")); uint32_t retval = bus_r(STATUS_REG); LOG(logINFO, ("Status Register: %08x\n", retval)); @@ -2258,10 +2242,7 @@ int readFrameFromFifo() { uint32_t runBusy() { #ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); - } - return virtual_status; + return ((sharedMemory_getStatus() == RUNNING) ? 1 : 0); #endif uint32_t s = (bus_r(STATUS_REG) & STATUS_RN_BSY_MSK); // LOG(logDEBUG1, ("Status Register: %08x\n", s)); diff --git a/slsDetectorServers/mythen3DetectorServer/CMakeLists.txt b/slsDetectorServers/mythen3DetectorServer/CMakeLists.txt index 57da8d3df..b109aa3d0 100644 --- a/slsDetectorServers/mythen3DetectorServer/CMakeLists.txt +++ b/slsDetectorServers/mythen3DetectorServer/CMakeLists.txt @@ -10,7 +10,7 @@ add_executable(mythen3DetectorServer_virtual ../slsDetectorServer/src/LTC2620_Driver.c ../slsDetectorServer/src/ALTERA_PLL_CYCLONE10.c ../slsDetectorServer/src/programFpgaNios.c - ../slsDetectorServer/src/communication_virtual.c + ../slsDetectorServer/src/sharedMemory.c ) include_directories( diff --git a/slsDetectorServers/mythen3DetectorServer/Makefile b/slsDetectorServers/mythen3DetectorServer/Makefile index 69848e5d3..c0cd3f3f2 100755 --- a/slsDetectorServers/mythen3DetectorServer/Makefile +++ b/slsDetectorServers/mythen3DetectorServer/Makefile @@ -6,13 +6,13 @@ support_lib = ../../slsSupportLib/include/ CROSS = nios2-buildroot-linux-gnu- CC = $(CROSS)gcc CFLAGS += -Wall -std=gnu99 -DMYTHEN3D -DSTOP_SERVER -I$(main_inc) -I$(support_lib) -I$(current_dir) #-DDEBUG1 #-DVERBOSEI #-DVERBOSE -LDLIBS += -lm +LDLIBS += -lm -lrt -pthread PROGS = mythen3DetectorServer DESTDIR ?= bin INSTMODE = 0777 SRCS = slsDetectorFunctionList.c -SRCS += $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs.c $(main_src)nios.c $(main_src)DAC6571.c $(main_src)common.c $(main_src)LTC2620_Driver.c $(main_src)ALTERA_PLL_CYCLONE10.c $(main_src)/programFpgaNios.c +SRCS += $(main_src)slsDetectorServer.c $(main_src)slsDetectorServer_funcs.c $(main_src)communication_funcs.c $(main_src)nios.c $(main_src)DAC6571.c $(main_src)common.c $(main_src)LTC2620_Driver.c $(main_src)ALTERA_PLL_CYCLONE10.c $(main_src)/programFpgaNios.c $(main_src)/sharedMemory.c OBJS = $(SRCS:.c=.o) diff --git a/slsDetectorServers/mythen3DetectorServer/bin/mythen3DetectorServer_developer b/slsDetectorServers/mythen3DetectorServer/bin/mythen3DetectorServer_developer index d18a1406a..a05c7977b 100755 Binary files a/slsDetectorServers/mythen3DetectorServer/bin/mythen3DetectorServer_developer and b/slsDetectorServers/mythen3DetectorServer/bin/mythen3DetectorServer_developer differ diff --git a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c index 391e938a8..850f51d74 100644 --- a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c @@ -5,10 +5,10 @@ #include "RegisterDefs.h" #include "clogger.h" #include "common.h" +#include "sharedMemory.h" #include "versionAPI.h" #ifdef VIRTUAL #include "communication_funcs_UDP.h" -#include "communication_virtual.h" #endif #include @@ -35,8 +35,7 @@ char initErrorMessage[MAX_STR_LENGTH]; #ifdef VIRTUAL pthread_t pthread_virtual_tid; -int virtual_status = 0; -int virtual_stop = 0; +int64_t virtual_currentFrameNumber = 2; #endif sls_detector_module *detectorModules = NULL; @@ -331,10 +330,7 @@ void initStopServer() { exit(EXIT_FAILURE); } #ifdef VIRTUAL - virtual_stop = 0; - if (!isControlServer) { - ComVirtual_setStop(virtual_stop); - } + sharedMemory_setStop(0); #endif } @@ -388,10 +384,7 @@ void setupDetector() { clkPhase[i] = 0; } #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); #endif // pll defines @@ -1140,17 +1133,12 @@ int setHighVoltage(int val) { if (val > HV_SOFT_MAX_VOLTAGE) { val = HV_SOFT_MAX_VOLTAGE; } -#ifdef VIRTUAL - if (val >= 0) - highvoltage = val; - return highvoltage; -#endif // setting hv if (val >= 0) { LOG(logINFO, ("Setting High voltage: %d V\n", val)); - DAC6571_Set(val); - highvoltage = val; + if (DAC6571_Set(val) == OK) + highvoltage = val; } return highvoltage; } @@ -1980,22 +1968,15 @@ int startStateMachine() { } LOG(logINFOBLUE, ("Starting State Machine\n")); // set status to running - virtual_status = 1; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - virtual_stop = ComVirtual_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } + if (sharedMemory_getStop() != 0) { + LOG(logERROR, ("Cant start acquisition. " + "Stop server has not updated stop status to 0\n")); + return FAIL; } + sharedMemory_setStatus(RUNNING); if (pthread_create(&pthread_virtual_tid, NULL, &start_timer, NULL)) { LOG(logERROR, ("Could not start Virtual acquisition thread\n")); - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFOGREEN, ("Virtual Acquisition started\n")); @@ -2037,10 +2018,8 @@ void *start_timer(void *arg) { // loop over number of frames for (int frameNr = 0; frameNr != numFrames; ++frameNr) { - // update the virtual stop from stop server - virtual_stop = ComVirtual_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { + // check if manual stop + if (sharedMemory_getStop() == 1) { break; } @@ -2059,7 +2038,7 @@ void *start_timer(void *arg) { sls_detector_header *header = (sls_detector_header *)(packetData); header->detType = (uint16_t)myDetectorType; header->version = SLS_DETECTOR_HEADER_VERSION - 1; - header->frameNumber = frameNr + 1; + header->frameNumber = virtual_currentFrameNumber; header->packetNumber = i; header->modId = 0; header->row = detPos[X]; @@ -2072,7 +2051,8 @@ void *start_timer(void *arg) { sendUDPPacket(0, packetData, packetSize); } - LOG(logINFO, ("Sent frame: %d\n", frameNr)); + LOG(logINFO, ("Sent frame: %d [%lld]\n", frameNr, + (long long unsigned int)virtual_currentFrameNumber)); clock_gettime(CLOCK_REALTIME, &end); int64_t timeNs = ((end.tv_sec - begin.tv_sec) * 1E9 + (end.tv_nsec - begin.tv_nsec)); @@ -2083,14 +2063,12 @@ void *start_timer(void *arg) { usleep((periodNs - timeNs) / 1000); } } + ++virtual_currentFrameNumber; } closeUDPSocket(0); - virtual_status = 0; - if (isControlServer) { - ComVirtual_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -2098,17 +2076,17 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping State Machine\n")); + // if scan active, stop scan + if (sharedMemory_getScanStatus() == RUNNING) { + sharedMemory_setScanStop(1); + } #ifdef VIRTUAL if (!isControlServer) { - virtual_stop = 1; - ComVirtual_setStop(virtual_stop); + sharedMemory_setStop(1); // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = ComVirtual_getStatus(); - } - virtual_stop = 0; - ComVirtual_setStop(virtual_stop); + while (sharedMemory_getStatus() == RUNNING) + ; + sharedMemory_setStop(0); LOG(logINFO, ("Stopped State Machine\n")); } return OK; @@ -2120,19 +2098,25 @@ int stopStateMachine() { } enum runStatus getRunStatus() { -#ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); + LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; } - if (virtual_status == 0) { - LOG(logINFOBLUE, ("Status: IDLE\n")); - return IDLE; - } else { + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } +#ifdef VIRTUAL + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } + LOG(logINFOBLUE, ("Status: IDLE\n")); + return IDLE; #endif - LOG(logDEBUG1, ("Getting status\n")); + uint32_t retval = bus_r(FLOW_STATUS_REG); LOG(logINFO, ("Status Register: %08x\n", retval)); @@ -2200,10 +2184,7 @@ void readFrame(int *ret, char *mess) { u_int32_t runBusy() { #ifdef VIRTUAL - if (!isControlServer) { - virtual_status = ComVirtual_getStatus(); - } - return virtual_status; + return ((sharedMemory_getStatus() == RUNNING) ? 1 : 0); #endif u_int32_t s = (bus_r(FLOW_STATUS_REG) & FLOW_STATUS_RUN_BUSY_MSK); // LOG(logDEBUG1, ("Status Register: %08x\n", s)); diff --git a/slsDetectorServers/slsDetectorServer/include/communication_virtual.h b/slsDetectorServers/slsDetectorServer/include/communication_virtual.h deleted file mode 100644 index a38f8d8a4..000000000 --- a/slsDetectorServers/slsDetectorServer/include/communication_virtual.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#ifdef VIRTUAL -// communciate between control and stop server - -int ComVirtual_createFiles(const int port); -void ComVirtual_setFileNames(const int port); -void ComVirtual_setStatus(int value); -int ComVirtual_getStatus(); -void ComVirtual_setStop(int value); -int ComVirtual_getStop(); -int ComVirtual_writeToFile(int value, const char *fname, - const char *serverName); -int ComVirtual_readFromFile(int *value, const char *fname, - const char *serverName); - -#endif diff --git a/slsDetectorServers/slsDetectorServer/include/sharedMemory.h b/slsDetectorServers/slsDetectorServer/include/sharedMemory.h new file mode 100644 index 000000000..edf5d5ef7 --- /dev/null +++ b/slsDetectorServers/slsDetectorServer/include/sharedMemory.h @@ -0,0 +1,23 @@ +#pragma once +#include "sls_detector_defs.h" +#include + +void sharedMemory_print(); +int sharedMemory_create(int port); +void sharedMemory_initialize(); +int sharedMemory_open(int port); +int sharedMemory_attach(); +int sharedMemory_detach(); +int sharedMemory_remove(); +void sharedMemory_lock(); +void sharedMemory_unlock(); +#ifdef VIRTUAL +void sharedMemory_setStatus(enum runStatus s); +enum runStatus sharedMemory_getStatus(); +void sharedMemory_setStop(int s); +int sharedMemory_getStop(); +#endif +void sharedMemory_setScanStatus(enum runStatus s); +enum runStatus sharedMemory_getScanStatus(); +void sharedMemory_setScanStop(int s); +int sharedMemory_getScanStop(); \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/include/slsDetectorServer_funcs.h b/slsDetectorServers/slsDetectorServer/include/slsDetectorServer_funcs.h index 564642e91..47568d112 100644 --- a/slsDetectorServers/slsDetectorServer/include/slsDetectorServer_funcs.h +++ b/slsDetectorServers/slsDetectorServer/include/slsDetectorServer_funcs.h @@ -36,6 +36,8 @@ int set_firmware_test(int); int set_bus_test(int); int set_image_test_mode(int); int get_image_test_mode(int); +enum DACINDEX getDACIndex(enum dacIndex ind); +int validateAndSetDac(enum dacIndex ind, int val, int mV); int set_dac(int); int get_adc(int); int write_register(int); @@ -43,6 +45,8 @@ int read_register(int); int set_module(int); int set_settings(int); int get_threshold_energy(int); +int acquire(int blocking, int file_des); +void *start_state_machine(void *arg); int start_acquisition(int); int stop_acquisition(int); int get_run_status(int); @@ -88,6 +92,7 @@ int get_last_client_ip(int); int set_port(int); int calibrate_pedestal(int); int enable_ten_giga(int); +int validateAndSetAllTrimbits(int arg); int set_all_trimbits(int); int set_pattern_io_control(int); int set_pattern_word(int); @@ -118,7 +123,6 @@ int program_fpga(int); int reset_fpga(int); int power_chip(int); int set_activate(int); -int prepare_acquisition(int); int threshold_temp(int); int temp_control(int); int temp_event(int); @@ -221,4 +225,7 @@ int get_exptime_all_gates(int); int get_gate_delay_all_gates(int); int get_veto(int); int set_veto(int); -int set_pattern(int); \ No newline at end of file +int set_pattern(int); +int get_scan(int); +int set_scan(int); +int get_scan_error_message(int); \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/src/communication_virtual.c b/slsDetectorServers/slsDetectorServer/src/communication_virtual.c deleted file mode 100644 index 2d1f40fbf..000000000 --- a/slsDetectorServers/slsDetectorServer/src/communication_virtual.c +++ /dev/null @@ -1,121 +0,0 @@ -#ifdef VIRTUAL -#include "communication_virtual.h" -#include "clogger.h" - -#include -#include // usleep - -#define FILE_STATUS "/tmp/sls_virtual_server_status_" -#define FILE_STOP "/tmp/sls_virtual_server_stop_" -#define FD_STATUS 0 -#define FD_STOP 1 -#define FILE_NAME_LENGTH 1000 - -FILE *fd[2] = {NULL, NULL}; -char fnameStatus[FILE_NAME_LENGTH]; -char fnameStop[FILE_NAME_LENGTH]; -int portNumber = 0; - -int ComVirtual_createFiles(const int port) { - portNumber = port; - // control server writign status file - memset(fnameStatus, 0, FILE_NAME_LENGTH); - sprintf(fnameStatus, "%s%d", FILE_STATUS, port); - FILE *fd = NULL; - if (NULL == (fd = fopen(fnameStatus, "w"))) { - LOG(logERROR, ("Could not open the file %s for virtual communication\n", - fnameStatus)); - return 0; - } - fclose(fd); - LOG(logINFOBLUE, ("Created status file %s\n", fnameStatus)); - - // stop server writing stop file - memset(fnameStop, 0, FILE_NAME_LENGTH); - sprintf(fnameStop, "%s%d", FILE_STOP, port); - if (NULL == (fd = fopen(fnameStop, "w"))) { - LOG(logERROR, ("Could not open the file %s for virtual communication\n", - fnameStop)); - return 0; - } - fclose(fd); - LOG(logINFOBLUE, ("Created stop file %s\n", fnameStop)); - - return 1; -} - -void ComVirtual_setFileNames(const int port) { - portNumber = port; - memset(fnameStatus, 0, FILE_NAME_LENGTH); - memset(fnameStop, 0, FILE_NAME_LENGTH); - sprintf(fnameStatus, "%s%d", FILE_STATUS, port); - sprintf(fnameStop, "%s%d", FILE_STOP, port); -} - -void ComVirtual_setStatus(int value) { - while (!ComVirtual_writeToFile(value, fnameStatus, "Control")) { - usleep(100); - } -} - -int ComVirtual_getStatus() { - int retval = 0; - while (!ComVirtual_readFromFile(&retval, fnameStatus, "Stop")) { - usleep(100); - } - return retval; -} - -void ComVirtual_setStop(int value) { - while (!ComVirtual_writeToFile(value, fnameStop, "Stop")) { - usleep(100); - } -} - -int ComVirtual_getStop() { - int retval = 0; - while (!ComVirtual_readFromFile(&retval, fnameStop, "Control")) { - usleep(100); - } - return retval; -} - -int ComVirtual_writeToFile(int value, const char *fname, - const char *serverName) { - FILE *fd = NULL; - if (NULL == (fd = fopen(fname, "w"))) { - LOG(logERROR, ("Vritual %s Server [%d] could not open " - "the file %s for writing\n", - serverName, portNumber, fname)); - return 0; - } - while (fwrite(&value, sizeof(value), 1, fd) < 1) { - LOG(logERROR, ("Vritual %s Server [%d] could not write " - "to file %s\n", - serverName, portNumber, fname)); - return 0; - } - fclose(fd); - return 1; -} - -int ComVirtual_readFromFile(int *value, const char *fname, - const char *serverName) { - FILE *fd = NULL; - if (NULL == (fd = fopen(fname, "r"))) { - LOG(logERROR, ("Vritual %s Server [%d] could not open " - "the file %s for reading\n", - serverName, portNumber, fname)); - return 0; - } - while (fread(value, sizeof(int), 1, fd) < 1) { - LOG(logERROR, ("Vritual %s Server [%d] could not read " - "from file %s\n", - serverName, portNumber, fname)); - return 0; - } - fclose(fd); - return 1; -} - -#endif \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/src/sharedMemory.c b/slsDetectorServers/slsDetectorServer/src/sharedMemory.c new file mode 100644 index 000000000..1ccf46f83 --- /dev/null +++ b/slsDetectorServers/slsDetectorServer/src/sharedMemory.c @@ -0,0 +1,189 @@ +#include "sharedMemory.h" +#include "clogger.h" + +#include +#include +#include +#include +#include + +#define SHM_NAME "sls_server_shared_memory" +#define SHM_VERSION 0x200625 +#define SHM_KEY 5678 + +typedef struct Memory { + int version; + sem_t sem; + enum runStatus scanStatus; // idle, running or error + int scanStop; +#ifdef VIRTUAL + enum runStatus status; + int stop; +#endif +} sharedMem; + +sharedMem *shm = NULL; +int shmFd = -1; + +extern int isControlServer; + +void sharedMemory_print() { + LOG(logINFO, ("%s Shared Memory:\n", isControlServer ? "c" : "s")); + LOG(logINFO, + ("%s version:0x%x\n", isControlServer ? "c" : "s", shm->version)); + LOG(logINFO, ("%s scan status: %d\n", isControlServer ? "c" : "s", + (int)shm->scanStatus)); + LOG(logINFO, + ("%s scan stop: %d\n", isControlServer ? "c" : "s", shm->scanStop)); +#ifdef VIRTUAL + LOG(logINFO, + ("%s status: %d\n", isControlServer ? "c" : "s", (int)shm->status)); + LOG(logINFO, ("%s stop: %d\n", isControlServer ? "c" : "s", shm->stop)); +#endif +} + +int sharedMemory_create(int port) { + // if sham existed, delete old shm and create again + shmFd = + shmget(SHM_KEY + port, sizeof(sharedMem), IPC_CREAT | IPC_EXCL | 0666); + if (shmFd == -1 && errno == EEXIST) { + char cmd[MAX_STR_LENGTH]; + memset(cmd, 0, MAX_STR_LENGTH); + sprintf(cmd, "ipcrm -M 0x%x", SHM_KEY + port); + system(cmd); + LOG(logWARNING, + ("Removed old shared memory with id 0x%x\n", SHM_KEY + port)); + shmFd = shmget(SHM_KEY + port, sizeof(sharedMem), + IPC_CREAT | IPC_EXCL | 0666); + } + if (shmFd == -1) { + LOG(logERROR, ("Create shared memory failed: %s\n", strerror(errno))); + return FAIL; + } + LOG(logINFO, ("Shared memory created\n")); + if (sharedMemory_attach() == FAIL) { + return FAIL; + } + sharedMemory_initialize(); + return OK; +} + +void sharedMemory_initialize() { + shm->version = SHM_VERSION; + sem_init(&(shm->sem), 1, 1); + shm->scanStatus = IDLE; + shm->scanStop = 0; +#ifdef VIRTUAL + shm->status = IDLE; + shm->stop = 0; +#endif + LOG(logINFO, ("Shared memory initialized\n")) +} + +int sharedMemory_open(int port) { + shmFd = shmget(SHM_KEY + port, sizeof(sharedMem), 0666); + if (shmFd == -1) { + LOG(logERROR, ("Open shared memory failed: %s\n", strerror(errno))); + return FAIL; + } + if (sharedMemory_attach() == FAIL) { + return FAIL; + } + if (shm->version != SHM_VERSION) { + LOG(logERROR, + ("Shared memory version 0x%x does not match! (expected: 0x%x)\n", + shm->version, SHM_VERSION)); + } + LOG(logINFO, ("Shared memory opened\n")); + return OK; +} + +int sharedMemory_attach() { + shm = (sharedMem *)shmat(shmFd, NULL, 0); + if (shm == (void *)-1) { + LOG(logERROR, ("could not attach: %s\n", strerror(errno))); + return FAIL; + } + LOG(logINFO, ("Shared memory attached\n")); + return OK; +} + +int sharedMemory_detach() { + if (shmdt(shm) == -1) { + LOG(logERROR, ("could not detach: %s\n", strerror(errno))); + return FAIL; + } + LOG(logINFO, ("Shared memory detached\n")); + return OK; +} + +int sharedMemory_remove() { + if (shmctl(shmFd, IPC_RMID, NULL) == -1) { + LOG(logERROR, ("could not remove: %s\n", strerror(errno))); + return FAIL; + } + LOG(logINFO, ("Shared memory removed\n")); + return OK; +} + +void sharedMemory_lock() { sem_wait(&(shm->sem)); } + +void sharedMemory_unlock() { sem_post(&(shm->sem)); } + +#ifdef VIRTUAL +void sharedMemory_setStatus(enum runStatus s) { + sharedMemory_lock(); + shm->status = s; + sharedMemory_unlock(); +} + +enum runStatus sharedMemory_getStatus() { + enum runStatus s = 0; + sharedMemory_lock(); + s = shm->status; + sharedMemory_unlock(); + return s; +} + +void sharedMemory_setStop(int s) { + sharedMemory_lock(); + shm->stop = s; + sharedMemory_unlock(); +} + +int sharedMemory_getStop() { + int s = 0; + sharedMemory_lock(); + s = shm->stop; + sharedMemory_unlock(); + return s; +} +#endif + +void sharedMemory_setScanStatus(enum runStatus s) { + sharedMemory_lock(); + shm->scanStatus = s; + sharedMemory_unlock(); +} + +enum runStatus sharedMemory_getScanStatus() { + enum runStatus s = IDLE; + sharedMemory_lock(); + s = shm->scanStatus; + sharedMemory_unlock(); + return s; +} + +void sharedMemory_setScanStop(int s) { + sharedMemory_lock(); + shm->scanStop = s; + sharedMemory_unlock(); +} + +int sharedMemory_getScanStop() { + int s = 0; + sharedMemory_lock(); + s = shm->scanStop; + sharedMemory_unlock(); + return s; +} \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer.c index 22e134d2c..8f96ff875 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer.c @@ -3,13 +3,11 @@ #include "clogger.h" #include "communication_funcs.h" +#include "sharedMemory.h" #include "slsDetectorServer_defs.h" #include "slsDetectorServer_funcs.h" #include "sls_detector_defs.h" #include "versionAPI.h" -#ifdef VIRTUAL -#include "communication_virtual.h" -#endif #include #include @@ -100,6 +98,10 @@ int main(int argc, char *argv[]) { // control server if (isControlServer) { + LOG(logINFOBLUE, ("Control Server [%d]\n", portno)); + if (sharedMemory_create(portno) == FAIL) { + return -1; + } #ifdef STOP_SERVER // start stop server process char cmd[MAX_STR_LENGTH]; @@ -121,21 +123,14 @@ int main(int argc, char *argv[]) { LOG(logDEBUG1, ("Command to start stop server:%s\n", cmd)); system(cmd); - LOG(logINFOBLUE, ("Control Server [%d]\n", portno)); -#ifdef VIRTUAL - // creating files for virtual servers to communicate with each other - if (!ComVirtual_createFiles(portno)) { - return -1; - } -#endif #endif } // stop server else { LOG(logINFOBLUE, ("Stop Server [%d]\n", portno)); -#ifdef VIRTUAL - ComVirtual_setFileNames(portno - 1); -#endif + if (sharedMemory_open(portno - 1) == FAIL) { + return -1; + } } init_detector(); @@ -164,6 +159,17 @@ int main(int argc, char *argv[]) { exitServer(sockfd); + // detach shared memory + if (sharedMemory_detach() == FAIL) { + return -1; + } + // remove shared memory (control server) + if (isControlServer) { + if (sharedMemory_remove() == FAIL) { + return -1; + } + } + if (retval == REBOOT) { LOG(logINFORED, ("Rebooting!\n")); fflush(stdout); diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index d2a993028..55cd81845 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -1,10 +1,12 @@ #include "slsDetectorServer_funcs.h" #include "clogger.h" #include "communication_funcs.h" +#include "sharedMemory.h" #include "slsDetectorFunctionList.h" #include "sls_detector_funcs.h" #include +#include #include #include @@ -49,6 +51,16 @@ int detectorId = -1; // Local variables int (*flist[NUM_DET_FUNCTIONS])(int); +pthread_t pthread_tid; + +// scan variables +int scan = 0; +int numScanSteps = 0; +int *scanSteps = NULL; +int64_t scanSettleTime_ns = 0; +enum dacIndex scanGlobalIndex = 0; +int scanTrimbits = 0; +char scanErrMessage[MAX_STR_LENGTH] = ""; /* initialization functions */ @@ -232,7 +244,6 @@ void function_table() { flist[F_RESET_FPGA] = &reset_fpga; flist[F_POWER_CHIP] = &power_chip; flist[F_ACTIVATE] = &set_activate; - flist[F_PREPARE_ACQUISITION] = &prepare_acquisition; flist[F_THRESHOLD_TEMP] = &threshold_temp; flist[F_TEMP_CONTROL] = &temp_control; flist[F_TEMP_EVENT] = &temp_event; @@ -332,6 +343,9 @@ void function_table() { flist[F_GET_VETO] = &get_veto; flist[F_SET_VETO] = &set_veto; flist[F_SET_PATTERN] = &set_pattern; + flist[F_GET_SCAN] = get_scan; + flist[F_SET_SCAN] = set_scan; + flist[F_GET_SCAN_ERROR_MESSAGE] = get_scan_error_message; // check if (NUM_DET_FUNCTIONS >= RECEIVER_ENUM_START) { @@ -700,20 +714,8 @@ int get_image_test_mode(int file_des) { return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); } -int set_dac(int file_des) { - ret = OK; - memset(mess, 0, sizeof(mess)); - int args[3] = {-1, -1, -1}; - int retval = -1; - - if (receiveData(file_des, args, sizeof(args), INT32) < 0) - return printSocketReadError(); - - enum dacIndex ind = args[0]; - int mV = args[1]; - int val = args[2]; - enum DACINDEX serverDacIndex = 0; - +enum DACINDEX getDACIndex(enum dacIndex ind) { + enum DACINDEX serverDacIndex = -1; // check if dac exists for this detector switch (ind) { #ifdef GOTTHARDD @@ -990,240 +992,257 @@ int set_dac(int file_des) { modeNotImplemented("Dac Index", (int)ind); break; } + return serverDacIndex; +} + +int validateAndSetDac(enum dacIndex ind, int val, int mV) { + int retval = -1; + enum DACINDEX serverDacIndex = getDACIndex(ind); - // index exists if (ret == OK) { + switch (ind) { - LOG(logDEBUG1, ("Setting DAC %d to %d %s\n", serverDacIndex, val, - (mV ? "mV" : "dac units"))); - - // set & get - if ((val == GET_FLAG) || (Server_VerifyLock() == OK)) { - switch (ind) { - - // adc vpp + // adc vpp #if defined(CHIPTESTBOARDD) || defined(MOENCHD) - case ADC_VPP: - // set - if (val >= 0) { - ret = AD9257_SetVrefVoltage(val, mV); - if (ret == FAIL) { - sprintf(mess, "Could not set Adc Vpp. Please set a " - "proper value\n"); - LOG(logERROR, (mess)); - } + case ADC_VPP: + // set + if (val >= 0) { + ret = AD9257_SetVrefVoltage(val, mV); + if (ret == FAIL) { + sprintf(mess, "Could not set Adc Vpp. Please set a " + "proper value\n"); + LOG(logERROR, (mess)); } - retval = AD9257_GetVrefVoltage(mV); - LOG(logDEBUG1, - ("Adc Vpp retval: %d %s\n", retval, (mV ? "mV" : "mode"))); - // cannot validate (its just a variable and mv gives different - // value) - break; + } + retval = AD9257_GetVrefVoltage(mV); + LOG(logDEBUG1, + ("Adc Vpp retval: %d %s\n", retval, (mV ? "mV" : "mode"))); + // cannot validate (its just a variable and mv gives different + // value) + break; #endif - // io delay + // io delay #ifdef EIGERD - case IO_DELAY: - retval = setIODelay(val); - LOG(logDEBUG1, ("IODelay: %d\n", retval)); - validate(val, retval, "set iodelay", DEC); - break; + case IO_DELAY: + retval = setIODelay(val); + LOG(logDEBUG1, ("IODelay: %d\n", retval)); + validate(val, retval, "set iodelay", DEC); + break; #endif - // high voltage - case HIGH_VOLTAGE: - retval = setHighVoltage(val); - LOG(logDEBUG1, ("High Voltage: %d\n", retval)); + // high voltage + case HIGH_VOLTAGE: + retval = setHighVoltage(val); + LOG(logDEBUG1, ("High Voltage: %d\n", retval)); #if defined(JUNGFRAUD) || defined(CHIPTESTBOARDD) || defined(MOENCHD) || \ defined(GOTTHARD2D) || defined(MYTHEN3D) - validate(val, retval, "set high voltage", DEC); + validate(val, retval, "set high voltage", DEC); #endif #ifdef GOTTHARDD - if (retval == -1) { - ret = FAIL; - strcpy(mess, "Invalid Voltage. Valid values are 0, 90, " - "110, 120, 150, 180, 200\n"); - LOG(logERROR, (mess)); - } else - validate(val, retval, "set high voltage", DEC); + if (retval == -1) { + ret = FAIL; + strcpy(mess, "Invalid Voltage. Valid values are 0, 90, " + "110, 120, 150, 180, 200\n"); + LOG(logERROR, (mess)); + } else + validate(val, retval, "set high voltage", DEC); #elif EIGERD - if ((retval != SLAVE_HIGH_VOLTAGE_READ_VAL) && (retval < 0)) { - ret = FAIL; - if (retval == -1) - sprintf(mess, - "Setting high voltage failed. Bad value %d. " - "The range is from 0 to 200 V.\n", - val); - else if (retval == -2) - strcpy(mess, "Setting high voltage failed. " - "Serial/i2c communication failed.\n"); - else if (retval == -3) - strcpy(mess, "Getting high voltage failed. " - "Serial/i2c communication failed.\n"); - LOG(logERROR, (mess)); - } + if ((retval != SLAVE_HIGH_VOLTAGE_READ_VAL) && (retval < 0)) { + ret = FAIL; + if (retval == -1) + sprintf(mess, + "Setting high voltage failed. Bad value %d. " + "The range is from 0 to 200 V.\n", + val); + else if (retval == -2) + strcpy(mess, "Setting high voltage failed. " + "Serial/i2c communication failed.\n"); + else if (retval == -3) + strcpy(mess, "Getting high voltage failed. " + "Serial/i2c communication failed.\n"); + LOG(logERROR, (mess)); + } #endif - break; + break; - // power, vlimit + // power, vlimit #ifdef CHIPTESTBOARDD - case V_POWER_A: - case V_POWER_B: - case V_POWER_C: - case V_POWER_D: - case V_POWER_IO: - if (val != GET_FLAG) { - if (!mV) { - ret = FAIL; - sprintf(mess, - "Could not set power. Power regulator %d " - "should be in mV and not dac units.\n", - ind); - LOG(logERROR, (mess)); - } else if (checkVLimitCompliant(val) == FAIL) { - ret = FAIL; - sprintf(mess, - "Could not set power. Power regulator %d " - "exceeds voltage limit %d.\n", - ind, getVLimit()); - LOG(logERROR, (mess)); - } else if (!isPowerValid(serverDacIndex, val)) { - ret = FAIL; - sprintf(mess, - "Could not set power. Power regulator %d " - "should be between %d and %d mV\n", - ind, - (serverDacIndex == D_PWR_IO ? VIO_MIN_MV - : POWER_RGLTR_MIN), - (VCHIP_MAX_MV - VCHIP_POWER_INCRMNT)); - LOG(logERROR, (mess)); - } else { - setPower(serverDacIndex, val); - } - } - retval = getPower(serverDacIndex); - LOG(logDEBUG1, ("Power regulator(%d): %d\n", ind, retval)); - validate(val, retval, "set power regulator", DEC); - break; - - case V_POWER_CHIP: - if (val >= 0) { - ret = FAIL; - sprintf(mess, "Can not set Vchip. Can only be set " - "automatically in the background (+200mV " - "from highest power regulator voltage).\n"); - LOG(logERROR, (mess)); - /* restrict users from setting vchip - if (!mV) { - ret = FAIL; - sprintf(mess,"Could not set Vchip. Should be in mV and - not dac units.\n"); LOG(logERROR,(mess)); } else if - (!isVchipValid(val)) { ret = FAIL; sprintf(mess,"Could not - set Vchip. Should be between %d and %d mV\n", VCHIP_MIN_MV, - VCHIP_MAX_MV); LOG(logERROR,(mess)); } else { setVchip(val); - } - */ - } - retval = getVchip(); - LOG(logDEBUG1, ("Vchip: %d\n", retval)); - if (ret == OK && val != GET_FLAG && val != -100 && - retval != val) { - ret = FAIL; - sprintf(mess, "Could not set vchip. Set %d, but read %d\n", - val, retval); - LOG(logERROR, (mess)); - } - break; -#endif - -#if defined(CHIPTESTBOARDD) || defined(MOENCHD) - case V_LIMIT: - if (val >= 0) { - if (!mV) { - ret = FAIL; - strcpy(mess, "Could not set power. VLimit should be in " - "mV and not dac units.\n"); - LOG(logERROR, (mess)); - } else { - setVLimit(val); - } - } - retval = getVLimit(); - LOG(logDEBUG1, ("VLimit: %d\n", retval)); - validate(val, retval, "set vlimit", DEC); - break; -#endif - // dacs - default: - if (mV && val > DAC_MAX_MV) { + case V_POWER_A: + case V_POWER_B: + case V_POWER_C: + case V_POWER_D: + case V_POWER_IO: + if (val != GET_FLAG) { + if (!mV) { ret = FAIL; sprintf(mess, - "Could not set dac %d to value %d. Allowed limits " - "(0 - %d mV).\n", - ind, val, DAC_MAX_MV); + "Could not set power. Power regulator %d " + "should be in mV and not dac units.\n", + ind); LOG(logERROR, (mess)); - } else if (!mV && val > getMaxDacSteps()) { + } else if (checkVLimitCompliant(val) == FAIL) { ret = FAIL; sprintf(mess, - "Could not set dac %d to value %d. Allowed limits " - "(0 - %d dac units).\n", - ind, val, getMaxDacSteps()); + "Could not set power. Power regulator %d " + "exceeds voltage limit %d.\n", + ind, getVLimit()); + LOG(logERROR, (mess)); + } else if (!isPowerValid(serverDacIndex, val)) { + ret = FAIL; + sprintf(mess, + "Could not set power. Power regulator %d " + "should be between %d and %d mV\n", + ind, + (serverDacIndex == D_PWR_IO ? VIO_MIN_MV + : POWER_RGLTR_MIN), + (VCHIP_MAX_MV - VCHIP_POWER_INCRMNT)); LOG(logERROR, (mess)); } else { -#if defined(CHIPTESTBOARDD) || defined(MOENCHD) - if ((val != GET_FLAG && mV && - checkVLimitCompliant(val) == FAIL) || - (val != GET_FLAG && !mV && - checkVLimitDacCompliant(val) == FAIL)) { - ret = FAIL; - sprintf(mess, - "Could not set dac %d to value %d. " - "Exceeds voltage limit %d.\n", - ind, (mV ? val : dacToVoltage(val)), - getVLimit()); - LOG(logERROR, (mess)); - } else -#endif - setDAC(serverDacIndex, val, mV); - retval = getDAC(serverDacIndex, mV); + setPower(serverDacIndex, val); } -#ifdef EIGERD - if (val != GET_FLAG) { - // changing dac changes settings to undefined - switch (serverDacIndex) { - case E_VCMP_LL: - case E_VCMP_LR: - case E_VCMP_RL: - case E_VCMP_RR: - case E_VRPREAMP: - case E_VCP: - setSettings(UNDEFINED); - LOG(logERROR, - ("Settings has been changed " - "to undefined (changed specific dacs)\n")); - break; - default: - break; - } - } -#endif - // check - if (ret == OK) { - if ((abs(retval - val) <= 5) || val == GET_FLAG) { - ret = OK; - } else { - ret = FAIL; - sprintf(mess, "Setting dac %d : wrote %d but read %d\n", - serverDacIndex, val, retval); - LOG(logERROR, (mess)); - } - } - LOG(logDEBUG1, ("Dac (%d): %d %s\n\n", serverDacIndex, retval, - (mV ? "mV" : "dac units"))); - break; } + retval = getPower(serverDacIndex); + LOG(logDEBUG1, ("Power regulator(%d): %d\n", ind, retval)); + validate(val, retval, "set power regulator", DEC); + break; + + case V_POWER_CHIP: + if (val >= 0) { + ret = FAIL; + sprintf(mess, "Can not set Vchip. Can only be set " + "automatically in the background (+200mV " + "from highest power regulator voltage).\n"); + LOG(logERROR, (mess)); + /* restrict users from setting vchip + if (!mV) { + ret = FAIL; + sprintf(mess,"Could not set Vchip. Should be in mV and + not dac units.\n"); LOG(logERROR,(mess)); } else if + (!isVchipValid(val)) { ret = FAIL; sprintf(mess,"Could not + set Vchip. Should be between %d and %d mV\n", VCHIP_MIN_MV, + VCHIP_MAX_MV); LOG(logERROR,(mess)); } else { setVchip(val); + } + */ + } + retval = getVchip(); + LOG(logDEBUG1, ("Vchip: %d\n", retval)); + if (ret == OK && val != GET_FLAG && val != -100 && retval != val) { + ret = FAIL; + sprintf(mess, "Could not set vchip. Set %d, but read %d\n", val, + retval); + LOG(logERROR, (mess)); + } + break; +#endif + +#if defined(CHIPTESTBOARDD) || defined(MOENCHD) + case V_LIMIT: + if (val >= 0) { + if (!mV) { + ret = FAIL; + strcpy(mess, "Could not set power. VLimit should be in " + "mV and not dac units.\n"); + LOG(logERROR, (mess)); + } else { + setVLimit(val); + } + } + retval = getVLimit(); + LOG(logDEBUG1, ("VLimit: %d\n", retval)); + validate(val, retval, "set vlimit", DEC); + break; +#endif + // dacs + default: + if (mV && val > DAC_MAX_MV) { + ret = FAIL; + sprintf(mess, + "Could not set dac %d to value %d. Allowed limits " + "(0 - %d mV).\n", + ind, val, DAC_MAX_MV); + LOG(logERROR, (mess)); + } else if (!mV && val > getMaxDacSteps()) { + ret = FAIL; + sprintf(mess, + "Could not set dac %d to value %d. Allowed limits " + "(0 - %d dac units).\n", + ind, val, getMaxDacSteps()); + LOG(logERROR, (mess)); + } else { +#if defined(CHIPTESTBOARDD) || defined(MOENCHD) + if ((val != GET_FLAG && mV && + checkVLimitCompliant(val) == FAIL) || + (val != GET_FLAG && !mV && + checkVLimitDacCompliant(val) == FAIL)) { + ret = FAIL; + sprintf(mess, + "Could not set dac %d to value %d. " + "Exceeds voltage limit %d.\n", + ind, (mV ? val : dacToVoltage(val)), getVLimit()); + LOG(logERROR, (mess)); + } else +#endif + setDAC(serverDacIndex, val, mV); + retval = getDAC(serverDacIndex, mV); + } +#ifdef EIGERD + if (val != GET_FLAG && getSettings() != UNDEFINED) { + // changing dac changes settings to undefined + switch (serverDacIndex) { + case E_VCMP_LL: + case E_VCMP_LR: + case E_VCMP_RL: + case E_VCMP_RR: + case E_VRPREAMP: + case E_VCP: + setSettings(UNDEFINED); + LOG(logERROR, ("Settings has been changed " + "to undefined (changed specific dacs)\n")); + break; + default: + break; + } + } +#endif + // check + if (ret == OK) { + if ((abs(retval - val) <= 5) || val == GET_FLAG) { + ret = OK; + } else { + ret = FAIL; + sprintf(mess, "Setting dac %d : wrote %d but read %d\n", + serverDacIndex, val, retval); + LOG(logERROR, (mess)); + } + } + LOG(logDEBUG1, ("Dac (%d): %d %s\n\n", serverDacIndex, retval, + (mV ? "mV" : "dac units"))); + break; } } + return retval; +} + +int set_dac(int file_des) { + ret = OK; + memset(mess, 0, sizeof(mess)); + int args[3] = {-1, -1, -1}; + int retval = -1; + + if (receiveData(file_des, args, sizeof(args), INT32) < 0) + return printSocketReadError(); + + enum dacIndex ind = args[0]; + int mV = args[1]; + int val = args[2]; + + LOG(logDEBUG1, + ("Setting DAC %d to %d %s\n", ind, val, (mV ? "mV" : "dac units"))); + // set & get + if ((val == GET_FLAG) || (Server_VerifyLock() == OK)) { + retval = validateAndSetDac(ind, val, mV); + } return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); } @@ -1637,11 +1656,14 @@ int get_threshold_energy(int file_des) { return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); } -int start_acquisition(int file_des) { +int acquire(int blocking, int file_des) { ret = OK; memset(mess, 0, sizeof(mess)); - - LOG(logDEBUG1, ("Starting Acquisition\n")); + if (blocking) { + LOG(logINFOBLUE, ("Blocking Acquisition\n")); + } else { + LOG(logINFOBLUE, ("Unblocking Acquisition\n")); + } // only set if (Server_VerifyLock() == OK) { #if defined(MOENCHD) @@ -1706,24 +1728,125 @@ int start_acquisition(int file_des) { strcpy(mess, "Could not start acquisition because "); strcat(mess, configureMessage); LOG(logERROR, (mess)); + } else if (sharedMemory_getScanStatus() == RUNNING) { + ret = FAIL; + strcpy(mess, "Could not start acquisition because a scan is " + "already running!\n"); + LOG(logERROR, (mess)); } else { - ret = startStateMachine(); - if (ret == FAIL) { -#if defined(CHIPTESTBOARDD) || defined(MOENCHD) || defined(VIRTUAL) - sprintf(mess, - "Could not start acquisition. Could not create udp " - "socket in server. Check udp_dstip & udp_dstport.\n"); -#else - sprintf(mess, "Could not start acquisition\n"); -#endif + memset(scanErrMessage, 0, MAX_STR_LENGTH); + sharedMemory_setScanStop(0); + sharedMemory_setScanStatus(IDLE); // if it was error + if (pthread_create(&pthread_tid, NULL, &start_state_machine, + &blocking)) { + ret = FAIL; + strcpy(mess, "Could not start acquisition thread!\n"); LOG(logERROR, (mess)); + } else { + if (blocking) { + pthread_join(pthread_tid, NULL); + } } } - LOG(logDEBUG2, ("Starting Acquisition ret: %d\n", ret)); } return Server_SendResult(file_des, INT32, NULL, 0); } +void *start_state_machine(void *arg) { + int *blocking = (int *)arg; + int times = 1; + // start of scan + if (scan) { + sharedMemory_setScanStatus(RUNNING); + times = numScanSteps; + } + for (int i = 0; i != times; ++i) { + // normal acquisition + if (scan == 0) { + LOG(logINFOBLUE, ("Normal Acquisition (not scan)\n")); + } + // scan + else { + // check scan stop + if (sharedMemory_getScanStop()) { + LOG(logINFORED, ("Scan manually stopped!\n")); + sharedMemory_setScanStatus(IDLE); + break; + } + // trimbits scan + if (scanTrimbits) { + LOG(logINFOBLUE, + ("Trimbits scan %d/%d: [%d]\n", i, times, scanSteps[i])); + validateAndSetAllTrimbits(scanSteps[i]); + if (ret == FAIL) { + sprintf(scanErrMessage, "Cannot scan trimbit %d. ", + scanSteps[i]); + strcat(scanErrMessage, mess); + sharedMemory_setScanStatus(ERROR); + break; + } + } + // dac scan + else { + LOG(logINFOBLUE, ("Dac [%d] scan %d/%d: [%d]\n", + scanGlobalIndex, i, times, scanSteps[i])); + validateAndSetDac(scanGlobalIndex, scanSteps[i], 0); + if (ret == FAIL) { + sprintf(scanErrMessage, "Cannot scan dac %d at %d. ", + scanGlobalIndex, scanSteps[i]); + strcat(scanErrMessage, mess); + sharedMemory_setScanStatus(ERROR); + break; + } + } + // check scan stop + if (sharedMemory_getScanStop()) { + LOG(logINFORED, ("Scan manually stopped!\n")); + sharedMemory_setScanStatus(IDLE); + break; + } + usleep(scanSettleTime_ns / 1000); + } +#ifdef EIGERD + prepareAcquisition(); +#endif + ret = startStateMachine(); + LOG(logDEBUG2, ("Starting Acquisition ret: %d\n", ret)); + if (ret == FAIL) { +#if defined(CHIPTESTBOARDD) || defined(MOENCHD) || defined(VIRTUAL) + sprintf(mess, "Could not start acquisition. Could not create udp " + "socket in server. Check udp_dstip & udp_dstport.\n"); +#else + sprintf(mess, "Could not start acquisition\n"); +#endif + LOG(logERROR, (mess)); + if (scan) { + sprintf(scanErrMessage, "Cannot scan at %d. ", scanSteps[i]); + strcat(scanErrMessage, mess); + sharedMemory_setScanStatus(ERROR); + } + break; + } + // blocking or scan + if (*blocking || times > 1) { + readFrame(&ret, mess); + if (ret == FAIL && scan) { + sprintf(scanErrMessage, "Cannot scan at %d. ", scanSteps[i]); + strcat(scanErrMessage, mess); + sharedMemory_setScanStatus(ERROR); + break; + } + } + } + // end of scan + if (scan && sharedMemory_getScanStatus() != ERROR) { + sharedMemory_setScanStatus(IDLE); + } + return NULL; +} + +int start_acquisition(int file_des) { return acquire(0, file_des); } + int stop_acquisition(int file_des) { ret = OK; memset(mess, 0, sizeof(mess)); @@ -1753,100 +1876,7 @@ int get_run_status(int file_des) { return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); } -int start_and_read_all(int file_des) { - ret = OK; - memset(mess, 0, sizeof(mess)); - - LOG(logDEBUG1, ("Starting Acquisition and read all frames\n")); - // start state machine - LOG(logDEBUG1, ("Starting Acquisition\n")); - // only set - if (Server_VerifyLock() == OK) { -#if defined(MOENCHD) - if (getNumAnalogSamples() <= 0) { - ret = FAIL; - sprintf(mess, - "Could not start acquisition. Invalid number of analog " - "samples: %d.\n", - getNumAnalogSamples()); - LOG(logERROR, (mess)); - } else -#endif -#if defined(CHIPTESTBOARDD) - if ((getReadoutMode() == ANALOG_AND_DIGITAL || - getReadoutMode() == ANALOG_ONLY) && - (getNumAnalogSamples() <= 0)) { - ret = FAIL; - sprintf(mess, - "Could not start acquisition. Invalid number of analog " - "samples: %d.\n", - getNumAnalogSamples()); - LOG(logERROR, (mess)); - } else if ((getReadoutMode() == ANALOG_AND_DIGITAL || - getReadoutMode() == DIGITAL_ONLY) && - (getNumDigitalSamples() <= 0)) { - ret = FAIL; - sprintf(mess, - "Could not start acquisition. Invalid number of digital " - "samples: %d.\n", - getNumDigitalSamples()); - LOG(logERROR, (mess)); - } else -#endif -#ifdef EIGERD - // check for hardware mac and hardware ip - if (udpDetails.srcmac != getDetectorMAC()) { - ret = FAIL; - uint64_t sourcemac = getDetectorMAC(); - char src_mac[50]; - getMacAddressinString(src_mac, 50, sourcemac); - sprintf(mess, - "Invalid udp source mac address for this detector. Must be " - "same as hardware detector mac address %s\n", - src_mac); - LOG(logERROR, (mess)); - } else if (!enableTenGigabitEthernet(GET_FLAG) && - (udpDetails.srcip != getDetectorIP())) { - ret = FAIL; - uint32_t sourceip = getDetectorIP(); - char src_ip[INET_ADDRSTRLEN]; - getIpAddressinString(src_ip, sourceip); - sprintf( - mess, - "Invalid udp source ip address for this detector. Must be same " - "as hardware detector ip address %s in 1G readout mode \n", - src_ip); - LOG(logERROR, (mess)); - } else -#endif - if (configured == FAIL) { - ret = FAIL; - strcpy(mess, "Could not start acquisition because "); - strcat(mess, configureMessage); - LOG(logERROR, (mess)); - } else { - ret = startStateMachine(); - if (ret == FAIL) { -#if defined(VIRTUAL) || defined(CHIPTESTBOARDD) || defined(MOENCHD) - sprintf(mess, - "Could not start acquisition. Could not create udp " - "socket in server. Check udp_dstip & udp_dstport.\n"); -#else - sprintf(mess, "Could not start acquisition\n"); -#endif - LOG(logERROR, (mess)); - } - } - LOG(logDEBUG2, ("Starting Acquisition ret: %d\n", ret)); - } - - // lock or acquisition start error - if (ret == FAIL) - return Server_SendResult(file_des, INT32, NULL, 0); - - // read all (again validate lock, but should pass and not fail) - return read_all(file_des); -} +int start_and_read_all(int file_des) { return acquire(1, file_des); } int read_all(int file_des) { ret = OK; @@ -1866,8 +1896,14 @@ int get_num_frames(int file_des) { int64_t retval = -1; // get only - retval = getNumFrames(); - LOG(logDEBUG1, ("retval num frames %lld\n", (long long int)retval)); + if (!scan) { + retval = getNumFrames(); + LOG(logDEBUG1, ("retval num frames %lld\n", (long long int)retval)); + } else { + retval = numScanSteps; + LOG(logDEBUG1, ("retval num frames (num scan steps) %lld\n", + (long long int)retval)); + } return Server_SendResult(file_des, INT64, &retval, sizeof(retval)); } @@ -1882,22 +1918,35 @@ int set_num_frames(int file_des) { // only set if (Server_VerifyLock() == OK) { + // only set number of frames if normal mode (not scan) + if (scan) { + if (arg != numScanSteps) { + ret = FAIL; + sprintf(mess, + "Could not set number of frames %lld. In scan mode, it " + "is number of steps %d\n", + (long long unsigned int)arg, numScanSteps); + LOG(logERROR, (mess)); + } + } else { #ifdef GOTTHARD2D - // validate #frames in burst mode - if (getBurstMode() != BURST_OFF && arg > MAX_FRAMES_IN_BURST_MODE) { - ret = FAIL; - sprintf(mess, - "Could not set number of frames %lld. Must be <= %d in " - "burst mode.\n", - (long long unsigned int)arg, MAX_FRAMES_IN_BURST_MODE); - LOG(logERROR, (mess)); - } + // validate #frames in burst mode + if (getBurstMode() != BURST_OFF && arg > MAX_FRAMES_IN_BURST_MODE) { + ret = FAIL; + sprintf(mess, + "Could not set number of frames %lld. Must be <= %d in " + "burst mode.\n", + (long long unsigned int)arg, MAX_FRAMES_IN_BURST_MODE); + LOG(logERROR, (mess)); + } #endif - if (ret == OK) { - setNumFrames(arg); - int64_t retval = getNumFrames(); - LOG(logDEBUG1, ("retval num frames %lld\n", (long long int)retval)); - validate64(arg, retval, "set number of frames", DEC); + if (ret == OK) { + setNumFrames(arg); + int64_t retval = getNumFrames(); + LOG(logDEBUG1, + ("retval num frames %lld\n", (long long int)retval)); + validate64(arg, retval, "set number of frames", DEC); + } } } return Server_SendResult(file_des, INT64, NULL, 0); @@ -2819,6 +2868,40 @@ int enable_ten_giga(int file_des) { return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); } +int validateAndSetAllTrimbits(int arg) { + int retval = -1; + +#if !defined(EIGERD) && !defined(MYTHEN3D) + functionNotImplemented(); +#else + // set + if (arg >= 0) { + if (arg > MAX_TRIMBITS_VALUE) { + ret = FAIL; + sprintf(mess, "Cannot set all trimbits. Range: 0 - %d\n", + MAX_TRIMBITS_VALUE); + LOG(logERROR, (mess)); + } else { + ret = setAllTrimbits(arg); +#ifdef EIGERD + // changes settings to undefined + if (getSettings() != UNDEFINED) { + setSettings(UNDEFINED); + LOG(logERROR, + ("Settings has been changed to undefined (change all " + "trimbits)\n")); + } +#endif + } + } + // get + retval = getAllTrimbits(); + LOG(logDEBUG1, ("All trimbits: %d\n", retval)); + validate(arg, retval, "set all trimbits", DEC); +#endif + return retval; +} + int set_all_trimbits(int file_des) { ret = OK; memset(mess, 0, sizeof(mess)); @@ -2829,32 +2912,10 @@ int set_all_trimbits(int file_des) { return printSocketReadError(); LOG(logDEBUG1, ("Set all trmbits to %d\n", arg)); -#if !defined(EIGERD) && !defined(MYTHEN3D) - functionNotImplemented(); -#else - - // set - if (arg >= 0 && Server_VerifyLock() == OK) { - if (arg > MAX_TRIMBITS_VALUE) { - ret = FAIL; - sprintf(mess, "Cannot set all trimbits. Range: 0 - %d\n", - MAX_TRIMBITS_VALUE); - LOG(logERROR, (mess)); - } else { - ret = setAllTrimbits(arg); -#ifdef EIGERD - // changes settings to undefined - setSettings(UNDEFINED); - LOG(logERROR, ("Settings has been changed to undefined (change all " - "trimbits)\n")); -#endif - } + if ((arg >= 0 && Server_VerifyLock() == OK) || arg < 0) { + retval = validateAndSetAllTrimbits(arg); } - // get - retval = getAllTrimbits(); - LOG(logDEBUG1, ("All trimbits: %d\n", retval)); - validate(arg, retval, "set all trimbits", DEC); -#endif + return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); } @@ -2875,7 +2936,7 @@ int set_pattern_io_control(int file_des) { retval = writePatternIOControl(arg); LOG(logDEBUG1, ("Pattern IO Control retval: 0x%llx\n", (long long int)retval)); - validate64(arg, retval, "Pattern IO Control", HEX); + validate64(arg, retval, "set Pattern IO Control", HEX); } #endif return Server_SendResult(file_des, INT64, &retval, sizeof(retval)); @@ -2965,8 +3026,9 @@ int set_pattern_loop_addresses(int file_des) { startAddr, stopAddr)); retvals[0] = startAddr; retvals[1] = stopAddr; - validate(args[1], startAddr, "Pattern loops' start address", HEX); - validate(args[2], stopAddr, "Pattern loops' stop address", HEX); + validate(args[1], startAddr, "set Pattern loops' start address", + HEX); + validate(args[2], stopAddr, "set Pattern loops' stop address", HEX); } } #endif @@ -3004,7 +3066,8 @@ int set_pattern_loop_cycles(int file_des) { retval = numLoops; LOG(logDEBUG1, ("Pattern loop cycles retval: (ncycles:%d)\n", retval)); - validate(args[1], retval, "Pattern loops' number of cycles", DEC); + validate(args[1], retval, "set Pattern loops' number of cycles", + DEC); } } #endif @@ -3047,7 +3110,7 @@ int set_pattern_wait_addr(int file_des) { } else { retval = setPatternWaitAddress(loopLevel, addr); LOG(logDEBUG1, ("Pattern wait address retval: 0x%x\n", retval)); - validate(addr, retval, "Pattern wait address", HEX); + validate(addr, retval, "set Pattern wait address", HEX); } } #endif @@ -3082,7 +3145,7 @@ int set_pattern_wait_time(int file_des) { retval = setPatternWaitTime(loopLevel, timeval); LOG(logDEBUG1, ("Pattern wait time retval: 0x%llx\n", (long long int)retval)); - validate64(timeval, retval, "Pattern wait time", HEX); + validate64(timeval, retval, "set Pattern wait time", HEX); } } #endif @@ -3107,7 +3170,7 @@ int set_pattern_mask(int file_des) { uint64_t retval64 = getPatternMask(); LOG(logDEBUG1, ("Pattern mask: 0x%llx\n", (long long unsigned int)retval64)); - validate64(arg, retval64, "Set Pattern Mask", HEX); + validate64(arg, retval64, "set Pattern Mask", HEX); } #endif return Server_SendResult(file_des, INT32, NULL, 0); @@ -3150,7 +3213,7 @@ int set_pattern_bit_mask(int file_des) { uint64_t retval64 = getPatternBitMask(); LOG(logDEBUG1, ("Pattern bit mask: 0x%llx\n", (long long unsigned int)retval64)); - validate64(arg, retval64, "Set Pattern Bit Mask", HEX); + validate64(arg, retval64, "set Pattern Bit Mask", HEX); } #endif return Server_SendResult(file_des, INT32, NULL, 0); @@ -3815,26 +3878,6 @@ int set_activate(int file_des) { return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); } -int prepare_acquisition(int file_des) { - ret = OK; - memset(mess, 0, sizeof(mess)); - - LOG(logDEBUG1, ("Preparing Acquisition\n")); -#ifndef EIGERD - functionNotImplemented(); -#else - // only set - if (Server_VerifyLock() == OK) { - ret = prepareAcquisition(); - if (ret == FAIL) { - strcpy(mess, "Could not prepare acquisition\n"); - LOG(logERROR, (mess)); - } - } -#endif - return Server_SendResult(file_des, INT32, NULL, 0); -} - // stop server int threshold_temp(int file_des) { ret = OK; @@ -4076,7 +4119,7 @@ int led(int file_des) { if ((arg == GET_FLAG) || (Server_VerifyLock() == OK)) { retval = setLEDEnable(arg); LOG(logDEBUG1, ("LED Enable: %d\n", retval)); - validate(arg, retval, "LED Enable", DEC); + validate(arg, retval, "enable/disable LED", DEC); } #endif return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); @@ -4378,7 +4421,7 @@ int set_external_sampling_source(int file_des) { } else { retval = setExternalSamplingSource(arg); LOG(logDEBUG1, ("External Sampling source: %d\n", retval)); - validate(arg, retval, "External sampling source", DEC); + validate(arg, retval, "set external sampling source", DEC); } } #endif @@ -4403,7 +4446,7 @@ int set_external_sampling(int file_des) { arg = (arg > 0) ? 1 : arg; retval = setExternalSampling(arg); LOG(logDEBUG1, ("External Sampling enable: %d\n", retval)); - validate(arg, retval, "External sampling enable", DEC); + validate(arg, retval, "set external sampling enable", DEC); } #endif return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); @@ -4793,7 +4836,8 @@ int set_detector_position(int file_des) { int check_detector_idle() { enum runStatus status = getRunStatus(); - if (status != IDLE && status != RUN_FINISHED && status != STOPPED) { + if (status != IDLE && status != RUN_FINISHED && status != STOPPED && + status != ERROR) { ret = FAIL; sprintf(mess, "Cannot configure mac when detector is not idle. Detector at " @@ -6630,7 +6674,7 @@ int set_current_source(int file_des) { setCurrentSource(arg); int retval = getCurrentSource(); LOG(logDEBUG1, ("current source enable retval: %u\n", retval)); - validate(arg, retval, "current source enable", DEC); + validate(arg, retval, "set current source enable", DEC); } #endif return Server_SendResult(file_des, INT32, NULL, 0); @@ -6837,7 +6881,11 @@ int get_receiver_parameters(int file_des) { return printSocketReadError(); // frames - i64 = getNumFrames(); + if (!scan) { + i64 = getNumFrames(); + } else { + i64 = numScanSteps; + } n += sendData(file_des, &i64, sizeof(i64), INT64); if (n < 0) return printSocketReadError(); @@ -7383,7 +7431,7 @@ int set_pattern(int file_des) { #ifndef MYTHEN3D if (ret == OK) { retval64 = writePatternIOControl(patioctrl); - validate64(patioctrl, retval64, "Pattern IO Control", HEX); + validate64(patioctrl, retval64, "set pattern IO Control", HEX); } #endif if (ret == OK) { @@ -7391,9 +7439,9 @@ int set_pattern(int file_des) { retval0 = patlimits[0]; retval1 = patlimits[1]; setPatternLoop(-1, &retval0, &retval1, &numLoops); - validate(patlimits[0], retval0, "Pattern Limits start address", + validate(patlimits[0], retval0, "set pattern Limits start address", HEX); - validate(patlimits[1], retval1, "Pattern Limits start address", + validate(patlimits[1], retval1, "set pattern Limits start address", HEX); } if (ret == OK) { @@ -7401,56 +7449,200 @@ int set_pattern(int file_des) { retval1 = patloop[1]; numLoops = patnloop[0]; setPatternLoop(0, &patloop[0], &patloop[1], &numLoops); - validate(patloop[0], retval0, "Pattern Loop 0 start address", HEX); - validate(patloop[1], retval1, "Pattern Loop 0 stop address", HEX); - validate(patnloop[0], numLoops, "Pattern Loop 0 num loops", HEX); + validate(patloop[0], retval0, "set pattern Loop 0 start address", + HEX); + validate(patloop[1], retval1, "set pattern Loop 0 stop address", + HEX); + validate(patnloop[0], numLoops, "set pattern Loop 0 num loops", + HEX); } if (ret == OK) { retval0 = patloop[2]; retval1 = patloop[3]; numLoops = patnloop[1]; setPatternLoop(1, &patloop[2], &patloop[3], &numLoops); - validate(patloop[2], retval0, "Pattern Loop 1 start address", HEX); - validate(patloop[3], retval1, "Pattern Loop 1 stop address", HEX); - validate(patnloop[1], numLoops, "Pattern Loop 1 num loops", HEX); + validate(patloop[2], retval0, "set pattern Loop 1 start address", + HEX); + validate(patloop[3], retval1, "set pattern Loop 1 stop address", + HEX); + validate(patnloop[1], numLoops, "set pattern Loop 1 num loops", + HEX); } if (ret == OK) { retval0 = patloop[4]; retval1 = patloop[5]; numLoops = patnloop[2]; setPatternLoop(2, &patloop[4], &patloop[5], &numLoops); - validate(patloop[4], retval0, "Pattern Loop 2 start address", HEX); - validate(patloop[5], retval1, "Pattern Loop 2 stop address", HEX); - validate(patnloop[2], numLoops, "Pattern Loop 2 num loops", HEX); + validate(patloop[4], retval0, "set pattern Loop 2 start address", + HEX); + validate(patloop[5], retval1, "set pattern Loop 2 stop address", + HEX); + validate(patnloop[2], numLoops, "set pattern Loop 2 num loops", + HEX); } if (ret == OK) { retval0 = setPatternWaitAddress(0, patwait[0]); - validate(patwait[0], retval0, "Pattern Loop 0 wait address", HEX); + validate(patwait[0], retval0, "set pattern Loop 0 wait address", + HEX); } if (ret == OK) { retval0 = setPatternWaitAddress(1, patwait[1]); - validate(patwait[1], retval0, "Pattern Loop 1 wait address", HEX); + validate(patwait[1], retval0, "set pattern Loop 1 wait address", + HEX); } if (ret == OK) { retval0 = setPatternWaitAddress(2, patwait[2]); - validate(patwait[2], retval0, "Pattern Loop 2 wait address", HEX); + validate(patwait[2], retval0, "set pattern Loop 2 wait address", + HEX); } if (ret == OK) { uint64_t retval64 = setPatternWaitTime(0, patwaittime[0]); - validate64(patwaittime[0], retval64, "Pattern Loop 0 wait time", + validate64(patwaittime[0], retval64, "set pattern Loop 0 wait time", HEX); } if (ret == OK) { retval64 = setPatternWaitTime(1, patwaittime[1]); - validate64(patwaittime[1], retval64, "Pattern Loop 1 wait time", + validate64(patwaittime[1], retval64, "set pattern Loop 1 wait time", HEX); } if (ret == OK) { retval64 = setPatternWaitTime(2, patwaittime[2]); - validate64(patwaittime[1], retval64, "Pattern Loop 2 wait time", + validate64(patwaittime[1], retval64, "set pattern Loop 2 wait time", HEX); } } #endif return Server_SendResult(file_des, INT32, NULL, 0); +} + +int get_scan(int file_des) { + ret = OK; + memset(mess, 0, sizeof(mess)); + int retvals[5] = {0, 0, 0, 0, 0}; + int64_t retvals_dacTime = 0; + + LOG(logDEBUG1, ("Getting scan\n")); + + // get only + retvals[0] = scan; + if (scan) { + retvals[1] = scanGlobalIndex; + retvals[2] = scanSteps[0]; + retvals[3] = scanSteps[numScanSteps - 1]; + retvals[4] = scanSteps[1] - scanSteps[0]; + retvals_dacTime = scanSettleTime_ns; + } + LOG(logDEBUG1, ("scan retval: [%s, dac:%d, start:%d, stop:%d, step:%d, " + "dacTime:%lldns]\n", + retvals[0] ? "enabled" : "disabled", retvals[1], retvals[2], + retvals[3], retvals[4], (long long int)retvals_dacTime)); + Server_SendResult(file_des, INT32, NULL, 0); + if (ret != FAIL) { + sendData(file_des, retvals, sizeof(retvals), INT32); + sendData(file_des, &retvals_dacTime, sizeof(retvals_dacTime), INT64); + } + return ret; +} + +int set_scan(int file_des) { + ret = OK; + memset(mess, 0, sizeof(mess)); + int args[5] = {-1, -1, -1, -1, -1}; + int64_t dacTime = -1; + int64_t retval = -1; + + if (receiveData(file_des, args, sizeof(args), INT32) < 0) + return printSocketReadError(); + if (receiveData(file_des, &dacTime, sizeof(dacTime), INT64) < 0) + return printSocketReadError(); + + // only set + if (Server_VerifyLock() == OK) { + int enable = args[0]; + enum dacIndex index = args[1]; + int start = args[2]; + int stop = args[3]; + int step = args[4]; + + // disable scan + if (enable == 0) { + LOG(logINFOBLUE, ("Disabling scan")); + scan = 0; + numScanSteps = 0; + // setting number of frames to 1 + int64_t arg = 1; + setNumFrames(arg); + retval = getNumFrames(); + LOG(logDEBUG1, ("retval num frames %lld\n", (long long int)retval)); + validate64(arg, retval, "set number of frames", DEC); + } + // enable scan + else { + if ((start < stop && step <= 0) || (stop < start && step >= 0)) { + ret = FAIL; + sprintf(mess, "Invalid scan parameters\n"); + LOG(logERROR, (mess)); + } else { + // trimbit scan + if (index == TRIMBIT_SCAN) { + LOG(logINFOBLUE, ("Trimbit scan enabled\n")); + scanTrimbits = 1; + scanGlobalIndex = index; + scanSettleTime_ns = dacTime; + } + // dac scan + else { + getDACIndex(index); + if (ret == OK) { + LOG(logINFOBLUE, ("Dac [%d] scan enabled\n", index)); + scanTrimbits = 0; + scanGlobalIndex = index; + scanSettleTime_ns = dacTime; + } + } + } + // valid scan + if (ret == OK) { + scan = 1; + numScanSteps = (abs(stop - start) / abs(step)) + 1; + if (scanSteps != NULL) { + free(scanSteps); + } + scanSteps = malloc(numScanSteps * sizeof(int)); + for (int i = 0; i != numScanSteps; ++i) { + scanSteps[i] = start + i * step; + LOG(logDEBUG1, ("scansteps[%d]:%d\n", i, scanSteps[i])); + } + LOG(logINFOBLUE, ("Enabling scan for %s, start[%d], stop[%d], " + "step[%d], nsteps[%d]\n", + scanTrimbits == 1 ? "trimbits" : "dac", start, + stop, step, numScanSteps)); + + // setting number of frames to scansteps + int64_t arg = 1; + setNumFrames(arg); + retval = getNumFrames(); + LOG(logDEBUG1, + ("retval num frames %lld\n", (long long int)retval)); + validate64(arg, retval, "set number of frames", DEC); + retval = numScanSteps; + } + } + } + return Server_SendResult(file_des, INT64, &retval, sizeof(retval)); +} + +int get_scan_error_message(int file_des) { + ret = OK; + memset(mess, 0, sizeof(mess)); + char retvals[MAX_STR_LENGTH]; + memset(retvals, 0, MAX_STR_LENGTH); + + LOG(logDEBUG1, ("Getting scan error message\n")); + + // get only + strcpy(retvals, scanErrMessage); + LOG(logDEBUG1, ("scan retval err message: [%s]\n", retvals)); + + return Server_SendResult(file_des, OTHER, retvals, sizeof(retvals)); } \ No newline at end of file diff --git a/slsDetectorSoftware/include/Detector.h b/slsDetectorSoftware/include/Detector.h index 990517233..e860dd692 100644 --- a/slsDetectorSoftware/include/Detector.h +++ b/slsDetectorSoftware/include/Detector.h @@ -394,7 +394,16 @@ class Detector { /** [Eiger] Sends an internal software trigger to the detector */ void sendSoftwareTrigger(Positions pos = {}); - // TODO: remove resetframescaught in receiver + Result getScan(Positions pos = {}) const; + + /** enables/ disables scans for dac, trimbits [Eiger/ Mythen3] + * TRIMBIT_SCAN. Enabling scan sets number of frames to number of steps in + * receiver. Disabling scan sets number of frames to 1 */ + void setScan(const defs::scanParameters t); + + /** gets scan error message in case of error during scan in case of non + * blocking acquisition (startDetector, not acquire) */ + Result getScanErrorMessage(Positions pos = {}) const; /************************************************** * * diff --git a/slsDetectorSoftware/src/CmdProxy.cpp b/slsDetectorSoftware/src/CmdProxy.cpp index 54bf66c59..eb5ef001d 100644 --- a/slsDetectorSoftware/src/CmdProxy.cpp +++ b/slsDetectorSoftware/src/CmdProxy.cpp @@ -178,7 +178,7 @@ std::string CmdProxy::VirtualServer(int action) { return os.str(); } -std::string CmdProxy::acquire(int action) { +std::string CmdProxy::Acquire(int action) { std::ostringstream os; if (action == defs::HELP_ACTION) { os << cmd << " - Acquire the number of frames set up.\n"; @@ -991,6 +991,60 @@ std::string CmdProxy::DetectorStatus(int action) { return os.str(); } +std::string CmdProxy::Scan(int action) { + std::ostringstream os; + os << cmd << ' '; + if (action == defs::HELP_ACTION) { + os << "[dac_name|0|trimbit_scan] [start_val] [stop_val] " + "[step_size] [dac settling time ns|us|ms|s]\n\tConfigures to " + "scan dac and sets number of frames to number of steps. Must " + "acquire after this. To cancel the scan configuration " + "set dac to '0' without further arguments. This also sets number " + "of frames back to 1." + "\n\t[Eiger]Use trimbit_scan as dac name for a trimbit scan." + << '\n'; + } else if (action == defs::GET_ACTION) { + if (args.size() != 0) { + WrongNumberOfParameters(0); + } + auto t = det->getScan(); + os << OutString(t) << '\n'; + } else if (action == defs::PUT_ACTION) { + if (det_id != -1) { + throw sls::RuntimeError( + "Cannot configure scan at module level"); + } + // disable + if (args.size() == 1) { + if (StringTo(args[0]) != 0) { + throw sls::RuntimeError("Did you mean '0' to disable?"); + } + det->setScan(defs::scanParameters()); + } + // enable without settling time + else if (args.size() == 4) { + det->setScan(defs::scanParameters( + StringTo(args[0]), StringTo(args[1]), + StringTo(args[2]), StringTo(args[3]))); + } + // enable with all parameters + else if (args.size() == 5) { + std::string time_str(args[4]); + std::string unit = RemoveUnit(time_str); + auto t = StringTo(time_str, unit); + det->setScan(defs::scanParameters( + StringTo(args[0]), StringTo(args[1]), + StringTo(args[2]), StringTo(args[3]), t)); + } else { + WrongNumberOfParameters(4); + } + os << ToString(args) << '\n'; + } else { + throw sls::RuntimeError("Unknown action"); + } + return os.str(); +} + /* Network Configuration (Detector<->Receiver) */ std::string CmdProxy::UDPDestinationIP(int action) { diff --git a/slsDetectorSoftware/src/CmdProxy.h b/slsDetectorSoftware/src/CmdProxy.h index d295fed18..dad6d396c 100644 --- a/slsDetectorSoftware/src/CmdProxy.h +++ b/slsDetectorSoftware/src/CmdProxy.h @@ -612,7 +612,7 @@ class CmdProxy { {"gappixels", &CmdProxy::GapPixels}, /* acquisition parameters */ - {"acquire", &CmdProxy::acquire}, + {"acquire", &CmdProxy::Acquire}, {"frames", &CmdProxy::frames}, {"triggers", &CmdProxy::triggers}, {"exptime", &CmdProxy::Exptime}, @@ -737,6 +737,8 @@ class CmdProxy { {"rx_missingpackets", &CmdProxy::rx_missingpackets}, {"startingfnum", &CmdProxy::startingfnum}, {"trigger", &CmdProxy::trigger}, + {"scan", &CmdProxy::Scan}, + {"scanerrmsg", &CmdProxy::scanerrmsg}, /* Network Configuration (Detector<->Receiver) */ {"numinterfaces", &CmdProxy::numinterfaces}, @@ -965,7 +967,7 @@ class CmdProxy { std::string SettingsList(int action); std::string GapPixels(int action); /* acquisition parameters */ - std::string acquire(int action); + std::string Acquire(int action); std::string Exptime(int action); std::string Speed(int action); std::string Adcphase(int action); @@ -983,6 +985,7 @@ class CmdProxy { /* acquisition */ std::string ReceiverStatus(int action); std::string DetectorStatus(int action); + std::string Scan(int action); /* Network Configuration (Detector<->Receiver) */ std::string UDPDestinationIP(int action); std::string UDPDestinationIP2(int action); @@ -1552,6 +1555,10 @@ class CmdProxy { trigger, sendSoftwareTrigger, "\n\t[Eiger] Sends software trigger signal to detector."); + GET_COMMAND(scanerrmsg, getScanErrorMessage, + "\n\tGets Scan error message if scan ended in error for non " + "blocking acquisitions."); + /* Network Configuration (Detector<->Receiver) */ INTEGER_COMMAND( diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index 5150dd703..6eb4040bf 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -525,9 +525,6 @@ void Detector::startReceiver() { pimpl->Parallel(&Module::startReceiver, {}); } void Detector::stopReceiver() { pimpl->Parallel(&Module::stopReceiver, {}); } void Detector::startDetector() { - if (getDetectorType().squash() == defs::EIGER) { - pimpl->Parallel(&Module::prepareAcquisition, {}); - } pimpl->Parallel(&Module::startAcquisition, {}); } @@ -562,6 +559,18 @@ void Detector::sendSoftwareTrigger(Positions pos) { pimpl->Parallel(&Module::sendSoftwareTrigger, pos); } +Result Detector::getScan(Positions pos) const { + return pimpl->Parallel(&Module::getScan, pos); +} + +void Detector::setScan(const defs::scanParameters t) { + pimpl->Parallel(&Module::setScan, {}, t); +} + +Result Detector::getScanErrorMessage(Positions pos) const { + return pimpl->Parallel(&Module::getScanErrorMessage, pos); +} + // Network Configuration (Detector<->Receiver) Result Detector::getNumberofUDPInterfaces(Positions pos) const { diff --git a/slsDetectorSoftware/src/DetectorImpl.cpp b/slsDetectorSoftware/src/DetectorImpl.cpp index d35c6ca8c..1a9f244ad 100644 --- a/slsDetectorSoftware/src/DetectorImpl.cpp +++ b/slsDetectorSoftware/src/DetectorImpl.cpp @@ -1046,9 +1046,6 @@ int DetectorImpl::acquire() { // start and read all try { - if (multi_shm()->multiDetectorType == EIGER) { - Parallel(&Module::prepareAcquisition, {}); - } Parallel(&Module::startAndReadAll, {}); } catch (...) { if (receiver) diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index 92b3ed460..fa4cfe0c3 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -367,8 +367,6 @@ void Module::stopReceiver() { sendToReceiver(F_STOP_RECEIVER, arg, nullptr); } -void Module::prepareAcquisition() { sendToDetector(F_PREPARE_ACQUISITION); } - void Module::startAcquisition() { shm()->stoppedFlag = false; sendToDetector(F_START_ACQUISITION); @@ -451,6 +449,22 @@ void Module::setStartingFrameNumber(uint64_t value) { void Module::sendSoftwareTrigger() { sendToDetectorStop(F_SOFTWARE_TRIGGER); } +defs::scanParameters Module::getScan() { + return sendToDetector(F_GET_SCAN); +} + +void Module::setScan(const defs::scanParameters t) { + auto retval = sendToDetector(F_SET_SCAN, t); + // if disabled, retval is 1, else its number of steps + setNumberOfFrames(retval); +} + +std::string Module::getScanErrorMessage() { + char retval[MAX_STR_LENGTH]{}; + sendToDetector(F_GET_SCAN_ERROR_MESSAGE, nullptr, retval); + return retval; +} + // Network Configuration (Detector<->Receiver) int Module::getNumberofUDPInterfacesFromShm() { diff --git a/slsDetectorSoftware/src/Module.h b/slsDetectorSoftware/src/Module.h index d6786b548..53873be38 100644 --- a/slsDetectorSoftware/src/Module.h +++ b/slsDetectorSoftware/src/Module.h @@ -155,7 +155,6 @@ class Module : public virtual slsDetectorDefs { * ************************************************/ void startReceiver(); void stopReceiver(); - void prepareAcquisition(); void startAcquisition(); void stopAcquisition(); void startAndReadAll(); @@ -167,6 +166,9 @@ class Module : public virtual slsDetectorDefs { uint64_t getStartingFrameNumber(); void setStartingFrameNumber(uint64_t value); void sendSoftwareTrigger(); + defs::scanParameters getScan(); + void setScan(const defs::scanParameters t); + std::string getScanErrorMessage(); /************************************************** * * diff --git a/slsDetectorSoftware/tests/test-CmdProxy.cpp b/slsDetectorSoftware/tests/test-CmdProxy.cpp index 72db5a351..6cbb18c64 100644 --- a/slsDetectorSoftware/tests/test-CmdProxy.cpp +++ b/slsDetectorSoftware/tests/test-CmdProxy.cpp @@ -1178,6 +1178,134 @@ TEST_CASE("startingfnum", "[.cmd][.new]") { } } +TEST_CASE("scan", "[.cmd][.new]") { + Detector det; + CmdProxy proxy(&det); + defs::dacIndex ind = defs::DAC_0; + defs::dacIndex notImplementedInd = defs::DAC_0; + auto det_type = det.getDetectorType().squash(); + switch (det_type) { + case defs::CHIPTESTBOARD: + ind = defs::DAC_0; + notImplementedInd = defs::VSVP; + break; + case defs::EIGER: + ind = defs::VCMP_LL; + notImplementedInd = defs::VCASCP_PB; + break; + case defs::JUNGFRAU: + ind = defs::VB_COMP; + notImplementedInd = defs::VSVP; + break; + case defs::GOTTHARD: + ind = defs::VREF_DS; + notImplementedInd = defs::VSVP; + break; + case defs::MOENCH: + ind = defs::VBP_COLBUF; + notImplementedInd = defs::VSVP; + break; + case defs::GOTTHARD2: + ind = defs::VB_COMP_FE; + notImplementedInd = defs::VSVP; + break; + case defs::MYTHEN3: + ind = defs::VTH2; + notImplementedInd = defs::VSVP; + break; + default: + break; + } + + // when taking acquisition + // auto previous = det.getDAC(ind, false); + // auto notImplementedPrevious = det.getDAC(notImplementedInd, false); + + { + std::ostringstream oss; + proxy.Call("scan", {sls::ToString(ind), "500", "1500", "500"}, -1, PUT, + oss); + REQUIRE(oss.str() == + "scan [" + sls::ToString(ind) + ", 500, 1500, 500]\n"); + } + { + std::ostringstream oss; + proxy.Call("scan", {}, -1, GET, oss); + REQUIRE(oss.str() == "scan [[enabled\ndac " + sls::ToString(ind) + + "\nstart 500\nstop 1500\nstep " + "500\nsettleTime 1ms\n]]\n"); + } + { + std::ostringstream oss; + proxy.Call("scan", {sls::ToString(ind), "500", "1500", "500", "2s"}, -1, + PUT, oss); + REQUIRE(oss.str() == + "scan [" + sls::ToString(ind) + ", 500, 1500, 500, 2s]\n"); + } + { + std::ostringstream oss; + proxy.Call("scan", {}, -1, GET, oss); + REQUIRE(oss.str() == "scan [[enabled\ndac " + sls::ToString(ind) + + "\nstart 500\nstop 1500\nstep " + "500\nsettleTime 2s\n]]\n"); + } + { + std::ostringstream oss; + proxy.Call("scan", {"0"}, -1, PUT, oss); + REQUIRE(oss.str() == "scan [0]\n"); + } + { + std::ostringstream oss; + proxy.Call("scan", {}, -1, GET, oss); + REQUIRE(oss.str() == "scan [[disabled]]\n"); + } + { + std::ostringstream oss; + proxy.Call("scan", {sls::ToString(ind), "1500", "500", "-500"}, -1, PUT, + oss); + REQUIRE(oss.str() == + "scan [" + sls::ToString(ind) + ", 1500, 500, -500]\n"); + } + REQUIRE_THROWS(proxy.Call( + "scan", {sls::ToString(notImplementedInd), "500", "1500", "500"}, -1, + PUT)); + REQUIRE_THROWS(proxy.Call( + "scan", {sls::ToString(ind), "500", "1500", "-500"}, -1, PUT)); + REQUIRE_THROWS(proxy.Call( + "scan", {sls::ToString(ind), "1500", "500", "500"}, -1, PUT)); + + if (det_type == defs::MYTHEN3 || defs::EIGER) { + { + std::ostringstream oss; + proxy.Call("scan", {"trimbit_scan", "0", "63", "16", "2s"}, -1, PUT, + oss); + REQUIRE(oss.str() == "scan [trimbit_scan, 0, 63, 16, 2s]\n"); + } + { + std::ostringstream oss; + proxy.Call("scan", {}, -1, GET, oss); + REQUIRE(oss.str() == + "scan [[enabled\ndac trimbit_scan\nstart 0\nstop 48\nstep " + "16\nsettleTime 2s\n]]\n"); + } + } + // acquire for each? + + // when taking acquisition + // Reset all dacs to previous value + // for (int i = 0; i != det.size(); ++i) { + // det.setDAC(ind, previous[i], false, {i}); + // det.setDAC(notImplementedInd, notImplementedPrevious[i], false, {i}); + // } +} + +TEST_CASE("scanerrmsg", "[.cmd][.new]") { + Detector det; + CmdProxy proxy(&det); + REQUIRE_NOTHROW(proxy.Call("scanerrmsg", {}, -1, GET)); + REQUIRE_THROWS(proxy.Call("scanerrmsg", {""}, -1, PUT)); +} + TEST_CASE("gappixels", "[.cmd][.new]") { Detector det; CmdProxy proxy(&det); diff --git a/slsSupportLib/include/ToString.h b/slsSupportLib/include/ToString.h index b4327fb02..a8ca99806 100644 --- a/slsSupportLib/include/ToString.h +++ b/slsSupportLib/include/ToString.h @@ -47,8 +47,10 @@ std::ostream &operator<<(std::ostream &os, std::string ToString(const slsDetectorDefs::patternParameters &r); std::ostream &operator<<(std::ostream &os, const slsDetectorDefs::patternParameters &r); +std::string ToString(const slsDetectorDefs::scanParameters &r); +std::ostream &operator<<(std::ostream &os, + const slsDetectorDefs::scanParameters &r); const std::string &ToString(const std::string &s); - /** Convert std::chrono::duration with specified output unit */ template typename std::enable_if::value, std::string>::type diff --git a/slsSupportLib/include/sls_detector_defs.h b/slsSupportLib/include/sls_detector_defs.h index c71eb257c..733d4afb2 100644 --- a/slsSupportLib/include/sls_detector_defs.h +++ b/slsSupportLib/include/sls_detector_defs.h @@ -19,6 +19,7 @@ #include "sls_detector_exceptions.h" #include #include +#include #include #include #else @@ -303,6 +304,7 @@ typedef struct { TEMPERATURE_SODR, TEMPERATURE_FPGA2, TEMPERATURE_FPGA3, + TRIMBIT_SCAN, V_POWER_A = 100, V_POWER_B = 101, V_POWER_C = 102, @@ -450,6 +452,36 @@ typedef struct { uint32_t patwait[3]{}; uint64_t patwaittime[3]{}; } __attribute__((packed)); + + /** scan structure */ + struct scanParameters { + int enable; + dacIndex dacInd; + int startOffset; + int stopOffset; + int stepSize; + int64_t dacSettleTime_ns; + + /** disable scan */ + scanParameters() + : enable(0), dacInd(DAC_0), startOffset(0), stopOffset(0), + stepSize(0), dacSettleTime_ns{0} {} + /** enable scan */ + scanParameters( + dacIndex dac, int start, int stop, int step, + std::chrono::nanoseconds t = std::chrono::milliseconds{1}) + : enable(1), dacInd(dac), startOffset(start), stopOffset(stop), + stepSize(step) { + dacSettleTime_ns = t.count(); + } + bool operator==(const scanParameters &other) const { + return ((enable == other.enable) && (dacInd == other.dacInd) && + (startOffset == other.startOffset) && + (stopOffset == other.stopOffset) && + (stepSize == other.stepSize) && + (dacSettleTime_ns == other.dacSettleTime_ns)); + } + } __attribute__((packed)); #endif #ifdef __cplusplus diff --git a/slsSupportLib/include/sls_detector_funcs.h b/slsSupportLib/include/sls_detector_funcs.h index f97ae87b3..c44dd4853 100755 --- a/slsSupportLib/include/sls_detector_funcs.h +++ b/slsSupportLib/include/sls_detector_funcs.h @@ -101,7 +101,6 @@ enum detFuncs { F_RESET_FPGA, F_POWER_CHIP, F_ACTIVATE, - F_PREPARE_ACQUISITION, F_THRESHOLD_TEMP, F_TEMP_CONTROL, F_TEMP_EVENT, @@ -201,6 +200,9 @@ enum detFuncs { F_GET_VETO, F_SET_VETO, F_SET_PATTERN, + F_GET_SCAN, + F_SET_SCAN, + F_GET_SCAN_ERROR_MESSAGE, NUM_DET_FUNCTIONS, RECEIVER_ENUM_START = 256, /**< detector function should not exceed this @@ -400,7 +402,6 @@ const char* getFunctionNameFromEnum(enum detFuncs func) { case F_RESET_FPGA: return "F_RESET_FPGA"; case F_POWER_CHIP: return "F_POWER_CHIP"; case F_ACTIVATE: return "F_ACTIVATE"; - case F_PREPARE_ACQUISITION: return "F_PREPARE_ACQUISITION"; case F_THRESHOLD_TEMP: return "F_THRESHOLD_TEMP"; case F_TEMP_CONTROL: return "F_TEMP_CONTROL"; case F_TEMP_EVENT: return "F_TEMP_EVENT"; @@ -500,6 +501,9 @@ const char* getFunctionNameFromEnum(enum detFuncs func) { case F_GET_VETO: return "F_GET_VETO"; case F_SET_VETO: return "F_SET_VETO"; case F_SET_PATTERN: return "F_SET_PATTERN"; + case F_GET_SCAN: return "F_GET_SCAN"; + case F_SET_SCAN: return "F_SET_SCAN"; + case F_GET_SCAN_ERROR_MESSAGE: return "F_GET_SCAN_ERROR_MESSAGE"; case NUM_DET_FUNCTIONS: return "NUM_DET_FUNCTIONS"; case RECEIVER_ENUM_START: return "RECEIVER_ENUM_START"; diff --git a/slsSupportLib/include/versionAPI.h b/slsSupportLib/include/versionAPI.h index e70363457..024ae05df 100644 --- a/slsSupportLib/include/versionAPI.h +++ b/slsSupportLib/include/versionAPI.h @@ -3,10 +3,10 @@ #define APILIB 0x200409 #define APIRECEIVER 0x200409 #define APIGUI 0x200409 -#define APIEIGER 0x200623 -#define APICTB 0x200623 -#define APIGOTTHARD 0x200623 -#define APIGOTTHARD2 0x200623 -#define APIJUNGFRAU 0x200623 -#define APIMYTHEN3 0x200623 -#define APIMOENCH 0x200623 +#define APICTB 0x200703 +#define APIGOTTHARD 0x200703 +#define APIGOTTHARD2 0x200703 +#define APIJUNGFRAU 0x200703 +#define APIMYTHEN3 0x200703 +#define APIMOENCH 0x200702 +#define APIEIGER 0x200703 diff --git a/slsSupportLib/src/ToString.cpp b/slsSupportLib/src/ToString.cpp index cd603e989..1ca68cfda 100644 --- a/slsSupportLib/src/ToString.cpp +++ b/slsSupportLib/src/ToString.cpp @@ -102,6 +102,30 @@ std::ostream &operator<<(std::ostream &os, return os << ToString(r); } +std::string ToString(const slsDetectorDefs::scanParameters &r) { + std::ostringstream oss; + oss << '['; + if (r.enable) { + oss << "enabled" << std::endl + << "dac " << ToString(r.dacInd) << std::endl + << "start " << r.startOffset << std::endl + << "stop " << r.stopOffset << std::endl + << "step " << r.stepSize << std::endl + << "settleTime " + << ToString(std::chrono::nanoseconds{r.dacSettleTime_ns}) + << std::endl; + } else { + oss << "disabled"; + } + oss << ']'; + return oss.str(); +} + +std::ostream &operator<<(std::ostream &os, + const slsDetectorDefs::scanParameters &r) { + return os << ToString(r); +} + std::string ToString(const defs::runStatus s) { switch (s) { case defs::ERROR: @@ -465,6 +489,12 @@ std::string ToString(const defs::dacIndex s) { return std::string("vipre_cds"); case defs::IBIAS_SFP: return std::string("ibias_sfp"); + case defs::TRIMBIT_SCAN: + return std::string("trimbit_scan"); + case defs::HIGH_VOLTAGE: + return std::string("vhighvoltage"); + case defs::IO_DELAY: + return std::string("iodelay"); default: return std::string("Unknown"); } @@ -818,6 +848,13 @@ template <> defs::dacIndex StringTo(const std::string &s) { return defs::VIPRE_CDS; if (s == "ibias_sfp") return defs::IBIAS_SFP; + if (s == "trimbit_scan") + return defs::TRIMBIT_SCAN; + if (s == "vhighvoltage") + return defs::HIGH_VOLTAGE; + if (s == "iodelay") + return defs::IO_DELAY; + throw sls::RuntimeError("Unknown dac Index " + s); } diff --git a/slsSupportLib/tests/test-ToString.cpp b/slsSupportLib/tests/test-ToString.cpp index ad2016553..06f9253ae 100644 --- a/slsSupportLib/tests/test-ToString.cpp +++ b/slsSupportLib/tests/test-ToString.cpp @@ -273,4 +273,29 @@ TEST_CASE("int or uin64_t to a string in hex") { REQUIRE(r == "[0xf4, 0xffff, 0x1900b6]"); r = ToStringHex(temp, 8); REQUIRE(r == "[0x000000f4, 0x0000ffff, 0x001900b6]"); +} + +TEST_CASE("Streaming of slsDetectorDefs::scanParameters") { + using namespace sls; + { + defs::scanParameters t{}; + std::ostringstream oss; + oss << t; + REQUIRE(oss.str() == "[disabled]"); + } + { + defs::scanParameters t{defs::VTH2, 500, 1500, 500}; + std::ostringstream oss; + oss << t; + REQUIRE(oss.str() == "[enabled\ndac vth2\nstart 500\nstop 1500\nstep " + "500\nsettleTime 1ms\n]"); + } + { + defs::scanParameters t{defs::VTH2, 500, 1500, 500, + std::chrono::milliseconds{500}}; + std::ostringstream oss; + oss << t; + REQUIRE(oss.str() == "[enabled\ndac vth2\nstart 500\nstop 1500\nstep " + "500\nsettleTime 0.5s\n]"); + } } \ No newline at end of file diff --git a/slsSupportLib/tests/test-sls_detector_defs.cpp b/slsSupportLib/tests/test-sls_detector_defs.cpp index 0162d92b7..d0c28e43c 100644 --- a/slsSupportLib/tests/test-sls_detector_defs.cpp +++ b/slsSupportLib/tests/test-sls_detector_defs.cpp @@ -49,4 +49,26 @@ TEST_CASE("assign module", "[support]") { CHECK(m3.reg == 500); CHECK(m3.iodelay == 750); CHECK(m3.nchan == 256 * 256 * 4); +} + +TEST_CASE("default construct scanParameters"){ + slsDetectorDefs::scanParameters p; + CHECK(p.dacSettleTime_ns == 0); + CHECK(p.dacInd == slsDetectorDefs::DAC_0); + CHECK(p.enable == 0); + CHECK(p.startOffset == 0); + CHECK(p.stopOffset == 0); + CHECK(p.stepSize == 0); + +} + +TEST_CASE("compare two scanParameters"){ + slsDetectorDefs::scanParameters p0; + slsDetectorDefs::scanParameters p1; + + CHECK(p0 == p1); + + p0.enable = 1; + CHECK_FALSE(p0 == p1); + } \ No newline at end of file