diff --git a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c index f2fd33164..af624fc05 100644 --- a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c @@ -41,8 +41,6 @@ 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 @@ -434,10 +432,7 @@ void initStopServer() { exit(EXIT_FAILURE); } #ifdef VIRTUAL - virtual_stop = 0; - if (!isControlServer) { - sharedMemory_setStop(virtual_stop); - } + sharedMemory_setStop(0); #endif } @@ -480,10 +475,7 @@ void setupDetector() { naSamples = 1; ndSamples = 1; #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); memset(virtual_pattern, 0, sizeof(virtual_pattern)); #endif @@ -2221,22 +2213,15 @@ int startStateMachine() { return FAIL; } LOG(logINFOBLUE, ("Starting State Machine\n")); - if (isControlServer) { - virtual_stop = sharedMemory_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } - virtual_status = 1; - sharedMemory_setStatus(virtual_status); + 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) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFOGREEN, ("Virtual Acquisition started\n")); @@ -2297,10 +2282,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 = sharedMemory_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { + // check if manual stop + if (sharedMemory_getStop() == 1) { break; } @@ -2349,10 +2332,7 @@ void *start_timer(void *arg) { closeUDPSocket(0); - virtual_status = 0; - if (isControlServer) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -2360,24 +2340,17 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping State Machine\n")); - // if scan active, stop scan - if (sharedMemory_getScanStatus()) { + // if scan active, stop scan first + if (sharedMemory_getScanStatus() == RUNNING) { sharedMemory_setScanStop(1); } #ifdef VIRTUAL - if (!isControlServer) { - virtual_stop = 1; - sharedMemory_setStop(virtual_stop); - // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = sharedMemory_getStatus(); - } - virtual_stop = 0; - sharedMemory_setStop(virtual_stop); - virtual_status = tempStatus; - LOG(logINFO, ("Stopped State Machine\n")); - } + 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 @@ -2391,19 +2364,23 @@ int stopStateMachine() { enum runStatus getRunStatus() { LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; + } + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } #ifdef VIRTUAL - if (sharedMemory_getScanStatus() || sharedMemory_getStatus()) { + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } LOG(logINFOBLUE, ("Status: IDLE\n")); return IDLE; #endif - if (sharedMemory_getScanStatus()) { - LOG(logINFOBLUE, ("Status: Scan RUNNING\n")); - return RUNNING; - } - uint32_t retval = bus_r(STATUS_REG); LOG(logINFO, ("Status Register: %08x\n", retval)); @@ -2674,10 +2651,7 @@ int readFrameFromFifo() { uint32_t runBusy() { #ifdef VIRTUAL - if (!isControlServer) { - virtual_status = sharedMemory_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/slsDetectorFunctionList.c b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c index 5447ee9d1..05ec63e29 100644 --- a/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/eigerDetectorServer/slsDetectorFunctionList.c @@ -70,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; @@ -364,10 +362,7 @@ void initStopServer() { #ifdef VIRTUAL LOG(logINFOBLUE, ("Configuring Stop server\n")); getModuleConfiguration(); - virtual_stop = 0; - if (!isControlServer) { - sharedMemory_setStop(virtual_stop); - } + sharedMemory_setStop(0); // get top/master in virtual readConfigFile(); #else @@ -669,10 +664,7 @@ void setupDetector() { } } #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); #endif LOG(logINFOBLUE, ("Setting Default Parameters\n")); @@ -1987,22 +1979,15 @@ int startStateMachine() { return FAIL; } LOG(logINFOBLUE, ("Starting State Machine\n")); - if (isControlServer) { - virtual_stop = sharedMemory_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } - virtual_status = 1; - sharedMemory_setStatus(virtual_status); + 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) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFO, ("Virtual Acquisition started\n")); @@ -2107,10 +2092,8 @@ void *start_timer(void *arg) { usleep(eiger_virtual_transmission_delay_frame); - // update the virtual stop from stop server - virtual_stop = sharedMemory_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { + // check if manual stop + if (sharedMemory_getStop() == 1) { setStartingFrameNumber(frameNr + iframes + 1); break; } @@ -2209,10 +2192,7 @@ void *start_timer(void *arg) { closeUDPSocket(0); closeUDPSocket(1); - virtual_status = 0; - if (isControlServer) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -2221,23 +2201,16 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping state machine\n")); // if scan active, stop scan - if (sharedMemory_getScanStatus()) { + if (sharedMemory_getScanStatus() == RUNNING) { sharedMemory_setScanStop(1); } #ifdef VIRTUAL - if (!isControlServer) { - virtual_stop = 1; - sharedMemory_setStop(virtual_stop); - // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = sharedMemory_getStatus(); - } - virtual_stop = 0; - sharedMemory_setStop(virtual_stop); - virtual_status = tempStatus; - LOG(logINFO, ("Stopped State Machine\n")); - } + 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) || @@ -2294,18 +2267,23 @@ int startReadOut() { enum runStatus getRunStatus() { LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; + } + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } #ifdef VIRTUAL - if (sharedMemory_getScanStatus() || sharedMemory_getStatus()) { + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } LOG(logINFOBLUE, ("Status: IDLE\n")); return IDLE; #else - if (sharedMemory_getScanStatus()) { - LOG(logINFOBLUE, ("Status: Scan RUNNING\n")); - return RUNNING; - } int i = Feb_Control_AcquisitionInProgress(); if (i == STATUS_ERROR) { LOG(logERROR, ("Status: ERROR reading status register\n")); @@ -2330,7 +2308,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/slsDetectorFunctionList.c b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c index ffba4787f..726ad235a 100644 --- a/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/gotthard2DetectorServer/slsDetectorFunctionList.c @@ -37,8 +37,6 @@ 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 @@ -343,10 +341,7 @@ void initStopServer() { exit(EXIT_FAILURE); } #ifdef VIRTUAL - virtual_stop = 0; - if (!isControlServer) { - sharedMemory_setStop(virtual_stop); - } + sharedMemory_setStop(0); #endif } @@ -394,10 +389,7 @@ void setupDetector() { } } #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); #endif // pll defines @@ -2241,22 +2233,15 @@ int startStateMachine() { } LOG(logINFOBLUE, ("Starting State Machine\n")); // set status to running - if (isControlServer) { - virtual_stop = sharedMemory_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } - virtual_status = 1; - sharedMemory_setStatus(virtual_status); + 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) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFOGREEN, ("Virtual Acquisition started\n")); @@ -2320,10 +2305,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 = sharedMemory_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { + // check if manual stop + if (sharedMemory_getStop() == 1) { break; } @@ -2396,10 +2379,7 @@ void *start_timer(void *arg) { closeUDPSocket(1); } - virtual_status = 0; - if (isControlServer) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -2408,23 +2388,15 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping State Machine\n")); // if scan active, stop scan - if (sharedMemory_getScanStatus()) { + if (sharedMemory_getScanStatus() == RUNNING) { sharedMemory_setScanStop(1); } #ifdef VIRTUAL - if (!isControlServer) { - virtual_stop = 1; - sharedMemory_setStop(virtual_stop); - // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = sharedMemory_getStatus(); - } - virtual_stop = 0; - sharedMemory_setStop(virtual_stop); - virtual_status = tempStatus; - LOG(logINFO, ("Stopped State Machine\n")); - } + sharedMemory_setStop(1); + while (sharedMemory_getStatus() == RUNNING) + ; + sharedMemory_setStop(0); + LOG(logINFO, ("Stopped State Machine\n")); return OK; #endif // stop state machine @@ -2435,18 +2407,23 @@ int stopStateMachine() { enum runStatus getRunStatus() { LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; + } + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } #ifdef VIRTUAL - if (sharedMemory_getScanStatus() || sharedMemory_getStatus()) { + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } LOG(logINFOBLUE, ("Status: IDLE\n")); return IDLE; #endif - if (sharedMemory_getScanStatus()) { - LOG(logINFOBLUE, ("Status: Scan RUNNING\n")); - return RUNNING; - } uint32_t retval = bus_r(FLOW_STATUS_REG); LOG(logINFO, ("Status Register: %08x\n", retval)); @@ -2513,10 +2490,7 @@ void readFrame(int *ret, char *mess) { u_int32_t runBusy() { #ifdef VIRTUAL - if (!isControlServer) { - virtual_status = sharedMemory_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/slsDetectorFunctionList.c b/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c index 5eb44f2ca..4f1133d6c 100644 --- a/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/gotthardDetectorServer/slsDetectorFunctionList.c @@ -34,8 +34,6 @@ 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 @@ -357,10 +355,7 @@ void initStopServer() { exit(EXIT_FAILURE); } #ifdef VIRTUAL - virtual_stop = 0; - if (!isControlServer) { - sharedMemory_setStop(virtual_stop); - } + sharedMemory_setStop(0); #endif } @@ -370,10 +365,7 @@ void setupDetector() { LOG(logINFO, ("This Server is for 1 Gotthard module (1280 channels)\n")); #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); #endif // Initialization @@ -1517,22 +1509,15 @@ int startStateMachine() { return FAIL; } LOG(logINFOBLUE, ("Starting State Machine\n")); - if (isControlServer) { - virtual_stop = sharedMemory_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } - virtual_status = 1; - sharedMemory_setStatus(virtual_status); + 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) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFOGREEN, ("Virtual Acquisition started\n")); @@ -1580,10 +1565,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 = sharedMemory_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { + // check if manual stop + if (sharedMemory_getStop() == 1) { break; } @@ -1624,10 +1607,7 @@ void *start_timer(void *arg) { closeUDPSocket(0); - virtual_status = 0; - if (isControlServer) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -1636,21 +1616,16 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping State Machine\n")); // if scan active, stop scan - if (sharedMemory_getScanStatus()) { + if (sharedMemory_getScanStatus() == RUNNING) { sharedMemory_setScanStop(1); } #ifdef VIRTUAL if (!isControlServer) { - virtual_stop = 1; - sharedMemory_setStop(virtual_stop); + sharedMemory_setStop(1); // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = sharedMemory_getStatus(); - } - virtual_stop = 0; - sharedMemory_setStop(virtual_stop); - virtual_status = tempStatus; + while (sharedMemory_getStatus() == RUNNING) + ; + sharedMemory_setStop(0); LOG(logINFO, ("Stopped State Machine\n")); } return OK; @@ -1674,18 +1649,24 @@ int stopStateMachine() { enum runStatus getRunStatus() { LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; + } + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } #ifdef VIRTUAL - if (sharedMemory_getScanStatus() || sharedMemory_getStatus()) { + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } LOG(logINFOBLUE, ("Status: IDLE\n")); return IDLE; #endif - if (sharedMemory_getScanStatus()) { - LOG(logINFOBLUE, ("Status: Scan RUNNING\n")); - return RUNNING; - } + enum runStatus s = IDLE; u_int32_t retval = runState(logINFO); @@ -1754,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); } @@ -1778,17 +1759,14 @@ void readFrame(int *ret, char *mess) { u_int32_t runBusy() { #ifdef VIRTUAL - if (!isControlServer) { - virtual_status = sharedMemory_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/slsDetectorFunctionList.c b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c index 21758f076..dce0af46c 100644 --- a/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/jungfrauDetectorServer/slsDetectorFunctionList.c @@ -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) { - sharedMemory_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) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); #endif ALTERA_PLL_ResetPLL(); @@ -1672,22 +1664,15 @@ int startStateMachine() { return FAIL; } LOG(logINFOBLUE, ("starting state machine\n")); - if (isControlServer) { - virtual_stop = sharedMemory_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } - virtual_status = 1; - sharedMemory_setStatus(virtual_status); + 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) { - sharedMemory_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 = sharedMemory_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) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -1830,23 +1810,16 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping State Machine\n")); // if scan active, stop scan - if (sharedMemory_getScanStatus()) { + if (sharedMemory_getScanStatus() == RUNNING) { sharedMemory_setScanStop(1); } #ifdef VIRTUAL - if (!isControlServer) { - virtual_stop = 1; - sharedMemory_setStop(virtual_stop); - // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = sharedMemory_getStatus(); - } - virtual_stop = 0; - sharedMemory_setStop(virtual_stop); - virtual_status = tempStatus; - LOG(logINFO, ("Stopped State Machine\n")); - } + 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 @@ -1860,18 +1833,24 @@ int stopStateMachine() { enum runStatus getRunStatus() { LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; + } + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } #ifdef VIRTUAL - if (sharedMemory_getScanStatus() || sharedMemory_getStatus()) { + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } LOG(logINFOBLUE, ("Status: IDLE\n")); return IDLE; #endif - if (sharedMemory_getScanStatus()) { - LOG(logINFOBLUE, ("Status: Scan RUNNING\n")); - return RUNNING; - } + enum runStatus s; u_int32_t retval = bus_r(STATUS_REG); LOG(logINFO, ("Status Register: %08x\n", retval)); @@ -1932,10 +1911,7 @@ void readFrame(int *ret, char *mess) { u_int32_t runBusy() { #ifdef VIRTUAL - if (!isControlServer) { - virtual_status = sharedMemory_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/slsDetectorFunctionList.c b/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c index 94f6af61f..9d1dd562e 100644 --- a/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/moenchDetectorServer/slsDetectorFunctionList.c @@ -39,8 +39,6 @@ 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 @@ -430,10 +428,7 @@ void initStopServer() { exit(EXIT_FAILURE); } #ifdef VIRTUAL - virtual_stop = 0; - if (!isControlServer) { - sharedMemory_setStop(virtual_stop); - } + sharedMemory_setStop(0); #endif } @@ -486,10 +481,7 @@ void setupDetector() { adcEnableMask_10g = 0; nSamples = 1; #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); memset(virtual_pattern, 0, sizeof(virtual_pattern)); #endif @@ -1854,22 +1846,15 @@ int startStateMachine() { return FAIL; } LOG(logINFOBLUE, ("Starting State Machine\n")); - if (isControlServer) { - virtual_stop = sharedMemory_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } - virtual_status = 1; - sharedMemory_setStatus(virtual_status); + 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) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFOGREEN, ("Virtual Acquisition started\n")); @@ -1930,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 = sharedMemory_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { + // check if manual stop + if (sharedMemory_getStop() == 1) { break; } @@ -1981,10 +1964,7 @@ void *start_timer(void *arg) { closeUDPSocket(0); - virtual_status = 0; - if (isControlServer) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -1993,23 +1973,16 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping State Machine\n")); // if scan active, stop scan - if (sharedMemory_getScanStatus()) { + if (sharedMemory_getScanStatus() == RUNNING) { sharedMemory_setScanStop(1); } #ifdef VIRTUAL - if (!isControlServer) { - virtual_stop = 1; - sharedMemory_setStop(virtual_stop); - // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = sharedMemory_getStatus(); - } - virtual_stop = 0; - sharedMemory_setStop(virtual_stop); - virtual_status = tempStatus; - LOG(logINFO, ("Stopped State Machine\n")); - } + 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 @@ -2024,18 +1997,24 @@ int stopStateMachine() { enum runStatus getRunStatus() { LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; + } + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } #ifdef VIRTUAL - if (sharedMemory_getScanStatus() || sharedMemory_getStatus()) { + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } LOG(logINFOBLUE, ("Status: IDLE\n")); return IDLE; #endif - if (sharedMemory_getScanStatus()) { - LOG(logINFOBLUE, ("Status: Scan RUNNING\n")); - return RUNNING; - } + uint32_t retval = bus_r(STATUS_REG); LOG(logINFO, ("Status Register: %08x\n", retval)); @@ -2263,10 +2242,7 @@ int readFrameFromFifo() { uint32_t runBusy() { #ifdef VIRTUAL - if (!isControlServer) { - virtual_status = sharedMemory_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/slsDetectorFunctionList.c b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c index f44d23b95..6ac2dd066 100644 --- a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c @@ -35,8 +35,6 @@ 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 @@ -332,10 +330,7 @@ void initStopServer() { exit(EXIT_FAILURE); } #ifdef VIRTUAL - virtual_stop = 0; - if (!isControlServer) { - sharedMemory_setStop(virtual_stop); - } + sharedMemory_setStop(0); #endif } @@ -389,10 +384,7 @@ void setupDetector() { clkPhase[i] = 0; } #ifdef VIRTUAL - virtual_status = 0; - if (isControlServer) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); #endif // pll defines @@ -1981,23 +1973,15 @@ int startStateMachine() { } LOG(logINFOBLUE, ("Starting State Machine\n")); // set status to running - if (isControlServer) { - virtual_stop = sharedMemory_getStop(); - if (virtual_stop != 0) { - LOG(logERROR, ("Cant start acquisition. " - "Stop server has not updated stop status to 0\n")); - return FAIL; - } - - virtual_status = 1; - sharedMemory_setStatus(virtual_status); + 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) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); return FAIL; } LOG(logINFOGREEN, ("Virtual Acquisition started\n")); @@ -2039,10 +2023,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 = sharedMemory_getStop(); - // check if virtual_stop is high - if (virtual_stop == 1) { + // check if manual stop + if (sharedMemory_getStop() == 1) { break; } @@ -2091,10 +2073,7 @@ void *start_timer(void *arg) { closeUDPSocket(0); - virtual_status = 0; - if (isControlServer) { - sharedMemory_setStatus(virtual_status); - } + sharedMemory_setStatus(IDLE); LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -2103,21 +2082,16 @@ void *start_timer(void *arg) { int stopStateMachine() { LOG(logINFORED, ("Stopping State Machine\n")); // if scan active, stop scan - if (sharedMemory_getScanStatus()) { + if (sharedMemory_getScanStatus() == RUNNING) { sharedMemory_setScanStop(1); } #ifdef VIRTUAL if (!isControlServer) { - virtual_stop = 1; - sharedMemory_setStop(virtual_stop); + sharedMemory_setStop(1); // read till status is idle - int tempStatus = 1; - while (tempStatus == 1) { - tempStatus = sharedMemory_getStatus(); - } - virtual_stop = 0; - sharedMemory_setStop(virtual_stop); - virtual_status = tempStatus; + while (sharedMemory_getStatus() == RUNNING) + ; + sharedMemory_setStop(0); LOG(logINFO, ("Stopped State Machine\n")); } return OK; @@ -2130,18 +2104,24 @@ int stopStateMachine() { enum runStatus getRunStatus() { LOG(logDEBUG1, ("Getting status\n")); + // scan error or running + if (sharedMemory_getScanStatus() == ERROR) { + LOG(logINFOBLUE, ("Status: scan ERROR\n")); + return ERROR; + } + if (sharedMemory_getScanStatus() == RUNNING) { + LOG(logINFOBLUE, ("Status: scan RUNNING\n")); + return RUNNING; + } #ifdef VIRTUAL - if (sharedMemory_getScanStatus() || sharedMemory_getStatus()) { + if (sharedMemory_getStatus() == RUNNING) { LOG(logINFOBLUE, ("Status: RUNNING\n")); return RUNNING; } LOG(logINFOBLUE, ("Status: IDLE\n")); return IDLE; #endif - if (sharedMemory_getScanStatus()) { - LOG(logINFOBLUE, ("Status: Scan RUNNING\n")); - return RUNNING; - } + uint32_t retval = bus_r(FLOW_STATUS_REG); LOG(logINFO, ("Status Register: %08x\n", retval)); @@ -2209,10 +2189,7 @@ void readFrame(int *ret, char *mess) { u_int32_t runBusy() { #ifdef VIRTUAL - if (!isControlServer) { - virtual_status = sharedMemory_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/sharedMemory.h b/slsDetectorServers/slsDetectorServer/include/sharedMemory.h index f49825df3..edf5d5ef7 100644 --- a/slsDetectorServers/slsDetectorServer/include/sharedMemory.h +++ b/slsDetectorServers/slsDetectorServer/include/sharedMemory.h @@ -1,5 +1,5 @@ #pragma once - +#include "sls_detector_defs.h" #include void sharedMemory_print(); @@ -12,12 +12,12 @@ int sharedMemory_remove(); void sharedMemory_lock(); void sharedMemory_unlock(); #ifdef VIRTUAL -void sharedMemory_setStatus(int s); -int sharedMemory_getStatus(); +void sharedMemory_setStatus(enum runStatus s); +enum runStatus sharedMemory_getStatus(); void sharedMemory_setStop(int s); int sharedMemory_getStop(); #endif -void sharedMemory_setScanStatus(int s); -int sharedMemory_getScanStatus(); +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 20b4d255f..e351707c5 100644 --- a/slsDetectorServers/slsDetectorServer/include/slsDetectorServer_funcs.h +++ b/slsDetectorServers/slsDetectorServer/include/slsDetectorServer_funcs.h @@ -224,5 +224,4 @@ int get_veto(int); int set_veto(int); int set_pattern(int); int get_scan(int); -int disable_scan(int); -int enable_scan(int); +int set_scan(int); diff --git a/slsDetectorServers/slsDetectorServer/src/sharedMemory.c b/slsDetectorServers/slsDetectorServer/src/sharedMemory.c index 523791c27..1ccf46f83 100644 --- a/slsDetectorServers/slsDetectorServer/src/sharedMemory.c +++ b/slsDetectorServers/slsDetectorServer/src/sharedMemory.c @@ -1,6 +1,5 @@ #include "sharedMemory.h" #include "clogger.h" -#include "sls_detector_defs.h" #include #include @@ -15,10 +14,10 @@ typedef struct Memory { int version; sem_t sem; - int scanStatus; + enum runStatus scanStatus; // idle, running or error int scanStop; #ifdef VIRTUAL - int status; + enum runStatus status; int stop; #endif } sharedMem; @@ -32,8 +31,13 @@ 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", shm->status)); + LOG(logINFO, + ("%s status: %d\n", isControlServer ? "c" : "s", (int)shm->status)); LOG(logINFO, ("%s stop: %d\n", isControlServer ? "c" : "s", shm->stop)); #endif } @@ -67,8 +71,10 @@ int sharedMemory_create(int port) { void sharedMemory_initialize() { shm->version = SHM_VERSION; sem_init(&(shm->sem), 1, 1); + shm->scanStatus = IDLE; + shm->scanStop = 0; #ifdef VIRTUAL - shm->status = 0; + shm->status = IDLE; shm->stop = 0; #endif LOG(logINFO, ("Shared memory initialized\n")) @@ -125,14 +131,14 @@ void sharedMemory_lock() { sem_wait(&(shm->sem)); } void sharedMemory_unlock() { sem_post(&(shm->sem)); } #ifdef VIRTUAL -void sharedMemory_setStatus(int s) { +void sharedMemory_setStatus(enum runStatus s) { sharedMemory_lock(); shm->status = s; sharedMemory_unlock(); } -int sharedMemory_getStatus() { - int s = 0; +enum runStatus sharedMemory_getStatus() { + enum runStatus s = 0; sharedMemory_lock(); s = shm->status; sharedMemory_unlock(); @@ -154,14 +160,14 @@ int sharedMemory_getStop() { } #endif -void sharedMemory_setScanStatus(int s) { +void sharedMemory_setScanStatus(enum runStatus s) { sharedMemory_lock(); shm->scanStatus = s; sharedMemory_unlock(); } -int sharedMemory_getScanStatus() { - int s = 0; +enum runStatus sharedMemory_getScanStatus() { + enum runStatus s = IDLE; sharedMemory_lock(); s = shm->scanStatus; sharedMemory_unlock(); diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index bf552761e..d204cb462 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -55,7 +55,8 @@ int (*flist[NUM_DET_FUNCTIONS])(int); int scan = 0; int numScanSteps = 0; int *scanSteps = NULL; -int64_t scanDacSettleTime_us = 0; +int64_t scanSettleTime_ns = 0; +enum dacIndex scanGlobalIndex = 0; enum DACINDEX scanDac = 0; int scanTrimbits = 0; @@ -341,8 +342,7 @@ void function_table() { flist[F_SET_VETO] = &set_veto; flist[F_SET_PATTERN] = &set_pattern; flist[F_GET_SCAN] = get_scan; - flist[F_DISABLE_SCAN] = disable_scan; - flist[F_ENABLE_SCAN] = enable_scan; + flist[F_SET_SCAN] = set_scan; // check if (NUM_DET_FUNCTIONS >= RECEIVER_ENUM_START) { LOG(logERROR, ("The last detector function enum has reached its " @@ -1723,56 +1723,76 @@ int start_state_machine(int blocking, 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 { + // start acquisition thread should start here + int times = 1; + sharedMemory_setScanStop(0); + sharedMemory_setScanStatus(IDLE); // if it was error // start of scan if (scan) { - sharedMemory_setScanStop(0); - sharedMemory_setScanStatus(1); + sharedMemory_setScanStatus(RUNNING); times = numScanSteps; } for (int i = 0; i != times; ++i) { - // if scanstop - if (scan && sharedMemory_getScanStop()) { - LOG(logINFORED, ("Scan stopped!\n")); - sharedMemory_setScanStatus(0); - break; + // normal acquisition + if (scan == 0) { + LOG(logINFOBLUE, ("Normal Acquisition (not scan)\n")); } - if (scanTrimbits) { - LOG(logINFOBLUE, ("Trimbits scan %d/%d: [%d]\n", i, times, - scanSteps[i])); -#if defined(EIGERD) || defined(MYTHEN3D) - setAllTrimbits(scanSteps[i]); -#else - LOG(logERROR, ("trimbit scan not implemented!\n")); -#endif - } else if (numScanSteps > 0) { - LOG(logINFOBLUE, ("Dac [%d] scan %d/%d: [%d]\n", scanDac, i, - times, scanSteps[i])); - setDAC(scanDac, scanSteps[i], 0); - int retval = getDAC(scanDac, 0); - if (abs(retval - scanSteps[i]) > 5) { + // 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])); +#if !defined(EIGERD) && !defined(MYTHEN3D) ret = FAIL; - sprintf(mess, + sprintf(mess, "trimbit scan not implemented for this " + "detector!\n"); + LOG(logERROR, (mess)); + sharedMemory_setScanStatus(ERROR); + break; +#else + setAllTrimbits(scanSteps[i]); +#endif + } + // dac scan + else { + LOG(logINFOBLUE, ("Dac [%d] scan %d/%d: [%d]\n", + scanDac, i, times, scanSteps[i])); + setDAC(scanDac, scanSteps[i], 0); + int retval = getDAC(scanDac, 0); + if (abs(retval - scanSteps[i]) > 5) { + ret = FAIL; + sprintf( + mess, "Could not scan. Setting dac %d : wrote %d but " "read %d\n", scanDac, scanSteps[i], scanSteps[i]); - LOG(logERROR, (mess)); - if (scan) { - sharedMemory_setScanStatus(0); + LOG(logERROR, (mess)); + sharedMemory_setScanStatus(ERROR); + break; } + } + // check scan stop + if (sharedMemory_getScanStop()) { + LOG(logINFORED, ("Scan manually stopped!\n")); + sharedMemory_setScanStatus(IDLE); break; } - } else { - LOG(logINFOBLUE, ("Normal Acquisition (not scan)\n")); + usleep(scanSettleTime_ns / 1000); } - // if scanstop - if (scan && sharedMemory_getScanStop()) { - LOG(logINFORED, ("Scan stopped!\n")); - sharedMemory_setScanStatus(0); - break; - } - usleep(scanDacSettleTime_us); #ifdef EIGERD prepareAcquisition(); #endif @@ -1789,7 +1809,7 @@ int start_state_machine(int blocking, int file_des) { #endif LOG(logERROR, (mess)); if (scan) { - sharedMemory_setScanStatus(0); + sharedMemory_setScanStatus(ERROR); } break; } @@ -1801,7 +1821,7 @@ int start_state_machine(int blocking, int file_des) { } // end of scan if (scan) { - sharedMemory_setScanStatus(0); + sharedMemory_setScanStatus(IDLE); } } return Server_SendResult(file_des, INT32, NULL, 0); @@ -7456,184 +7476,214 @@ int set_pattern(int file_des) { int get_scan(int file_des) { ret = OK; memset(mess, 0, sizeof(mess)); - int retval[4] = {-1, -1, -1, -1}; - int64_t retval_time = -1; + int retvals[5] = {0, 0, 0, 0, 0}; + int64_t retvals_dacTime = 0; LOG(logDEBUG1, ("Getting scan\n")); // get only - retval[0] = scan; - LOG(logDEBUG1, ("scan mode retval: %u\n", retval)); - - return Server_SendResult(file_des, INT32, &retval, sizeof(retval)); -} - -int disable_scan(int file_des) { - ret = OK; - memset(mess, 0, sizeof(mess)); - - // only set - if (Server_VerifyLock() == OK) { - LOG(logINFOBLUE, ("Disabling scan\n")); - scan = 0; - scanTrimbits = 0; - numScanSteps = 0; - if (scanSteps != NULL) { - free(scanSteps); - scanSteps = NULL; - } - int64_t arg = 1; - 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); + 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; } - return Server_SendResult(file_des, INT32, NULL, 0); + 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 enable_scan(int file_des) { +int set_scan(int file_des) { ret = OK; memset(mess, 0, sizeof(mess)); - int args[4] = {-1, -1, -1, -1}; - int64_t args_time = -1; + 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, args_time, sizeof(args_time), INT64) < 0) + if (receiveData(file_des, &dacTime, sizeof(dacTime), INT64) < 0) return printSocketReadError(); // only set if (Server_VerifyLock() == OK) { - int startOffset = args[1]; - int endOffset = args[2]; - int stepSize = args[3]; - scanDacSettleTime_us = args[4] / 1000; - scanTrimbits = 0; + int enable = args[0]; + enum dacIndex index = args[1]; + int start = args[2]; + int stop = args[3]; + int step = args[4]; - if ((startOffset < endOffset && stepSize <= 0) || - (endOffset < startOffset && stepSize >= 0)) { - ret = FAIL; - sprintf(mess, "Invalid scan parameters\n"); - LOG(logERROR, (mess)); - } else { - // trimbit scan - if (args[0] == TRIMBIT_SCAN) { -#if defined(EIGERD) || defined(MYTHEN3D) - if (startOffset < 0 || startOffset > MAX_TRIMBITS_VALUE || - endOffset < 0 || endOffset > MAX_TRIMBITS_VALUE) { - ret = FAIL; - sprintf(mess, "Invalid trimbits scan values\n"); - LOG(logERROR, (mess)); - } else { - scanTrimbits = 1; - LOG(logINFOBLUE, ("Trimbit scan enabled\n")); -#ifdef EIGERD - // changes settings to undefined - setSettings(UNDEFINED); - LOG(logERROR, - ("Settings has been changed to undefined (change all " - "trimbits)\n")); -#endif - } -#else - ret = FAIL; - sprintf(mess, "Cannot enable trimbit scan. Not implemented for " - "this detector\n"); - LOG(logERROR, (mess)); -#endif - } else { - switch ((enum dacIndex)args[0]) { - case HIGH_VOLTAGE: -#ifdef EIGERD - case IO_DELAY: -#elif CHIPTESTBOARDD - case ADC_VPP: - case V_POWER_A: - case V_POWER_B: - case V_POWER_C: - case V_POWER_D: - case V_POWER_IO: - case V_POWER_CHIP: - case V_LIMIT: -#elif MOENCHD - case ADC_VPP: - case V_LIMIT: -#endif - modeNotImplemented("Scan Dac Index", args[0]); - break; - default: - break; - } - scanDac = getDACIndex(args[0]); - if (ret == FAIL) { - sprintf( - mess, - "Cannot enable scan. Dac index %d not implemented for " - "this detector for scanning\n", - args[0]); - LOG(logERROR, (mess)); - } else if (startOffset < 0 || startOffset > getMaxDacSteps() || - endOffset < 0 || endOffset > getMaxDacSteps()) { - ret = FAIL; - sprintf(mess, "Invalid dac scan values\n"); - LOG(logERROR, (mess)); - } -#if defined(CHIPTESTBOARDD) || defined(MOENCHD) - else if (checkVLimitDacCompliant(startOffset) == FAIL || - checkVLimitDacCompliant(endOffset) == FAIL) { - ret = FAIL; - sprintf(mess, - "Invalid scan dac values." - "Exceeds voltage limit %d.\n", - getVLimit()); - LOG(logERROR, (mess)); - } -#endif - if (ret == OK) { -#ifdef EIGERD - // changing dac changes settings to undefined - switch (scanDac) { - 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 - LOG(logINFOBLUE, ("Dac [%d] scan enabled\n", scanDac)); - } - } - } - if (ret == OK) { - scan = 1; - numScanSteps = (abs(endOffset - startOffset) / abs(stepSize)) + 1; - if (scanSteps != NULL) { - free(scanSteps); - } - scanSteps = malloc(numScanSteps * sizeof(int)); - for (int i = 0; i != numScanSteps; ++i) { - scanSteps[i] = startOffset + i * stepSize; - LOG(logDEBUG1, ("scansteps[%d]:%d\n", i, scanSteps[i])); - } - - LOG(logINFOBLUE, - ("Enabling scan for dac[%d], start[%d], end[%d], " - "stepsize[%d], nsteps[%d]\n", - scanDac, startOffset, endOffset, stepSize, numScanSteps)); + // 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); - int64_t retval = getNumFrames(); + 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 (args[0] == TRIMBIT_SCAN) { +#if !defined(EIGERD) && !defined(MYTHEN3D) + ret = FAIL; + sprintf(mess, + "Cannot enable trimbit scan. Not implemented for " + "this detector\n"); + LOG(logERROR, (mess)); +#else + if (start < 0 || start > MAX_TRIMBITS_VALUE || stop < 0 || + stop > MAX_TRIMBITS_VALUE) { + ret = FAIL; + sprintf(mess, "Invalid trimbits scan values\n"); + LOG(logERROR, (mess)); + } + // validated + else { + LOG(logINFOBLUE, ("Trimbit scan enabled\n")); + scanTrimbits = 1; + scanGlobalIndex = index; + scanSettleTime_ns = dacTime; +#ifdef EIGERD + // changing trimbits, settings to undefined + setSettings(UNDEFINED); + LOG(logERROR, ("Settings has been changed to undefined " + "(change all " + "trimbits)\n")); +#endif + } +#endif + } + // dac scan + else { + if (start < 0 || start > getMaxDacSteps() || stop < 0 || + stop > getMaxDacSteps()) { + ret = FAIL; + sprintf(mess, "Invalid dac scan values\n"); + LOG(logERROR, (mess)); + } +#if defined(CHIPTESTBOARDD) || defined(MOENCHD) + else if (checkVLimitDacCompliant(start) == FAIL || + checkVLimitDacCompliant(stop) == FAIL) { + ret = FAIL; + sprintf(mess, + "Invalid scan dac values." + "Exceeds voltage limit %d.\n", + getVLimit()); + LOG(logERROR, (mess)); + } +#endif + else { + // validate allowed dac scans + switch (index) { + case HIGH_VOLTAGE: +#ifdef EIGERD + case IO_DELAY: +#elif CHIPTESTBOARDD + case ADC_VPP: + case V_POWER_A: + case V_POWER_B: + case V_POWER_C: + case V_POWER_D: + case V_POWER_IO: + case V_POWER_CHIP: + case V_LIMIT: +#elif MOENCHD + case ADC_VPP: + case V_LIMIT: +#endif + modeNotImplemented("Scan Dac Index", args[0]); + break; + default: + break; + } + // validate dac according to detector type + if (ret == OK) { + enum DACINDEX dac = getDACIndex(index); + if (ret == FAIL) { + sprintf(mess, + "Cannot enable scan. Dac index %d not " + "implemented for " + "this detector for scanning\n", + (int)index); + LOG(logERROR, (mess)); + } + // validated + else { + LOG(logINFOBLUE, + ("Dac [%d] scan enabled\n", scanDac)); + scanTrimbits = 0; + scanGlobalIndex = index; + scanSettleTime_ns = dacTime; + scanDac = dac; +#ifdef EIGERD + // changing dac changes settings to undefined + switch (scanDac) { + 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 + } + } + } + } + } + // 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); + } + } } - return Server_SendResult(file_des, INT32, NULL, 0); + return Server_SendResult(file_des, INT64, &retval, sizeof(retval)); } diff --git a/slsDetectorSoftware/include/Detector.h b/slsDetectorSoftware/include/Detector.h index 0e766b215..dfce564ac 100644 --- a/slsDetectorSoftware/include/Detector.h +++ b/slsDetectorSoftware/include/Detector.h @@ -396,12 +396,10 @@ class Detector { Result getScan(Positions pos = {}) const; - /** also sets number of frames to 1 */ - void disableScan(); - - /** scan dac, trimbits scan only for [Eiger/ Mythen3] TRIMBIT_SCAN, also - * sets number of frames to number of steps in receiver */ - void enableScan(const defs::scanParameters t); + /** 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); /************************************************** * * diff --git a/slsDetectorSoftware/src/CmdProxy.cpp b/slsDetectorSoftware/src/CmdProxy.cpp index a68dfae20..052fe1d25 100644 --- a/slsDetectorSoftware/src/CmdProxy.cpp +++ b/slsDetectorSoftware/src/CmdProxy.cpp @@ -1010,21 +1010,25 @@ std::string CmdProxy::Scan(int action) { auto t = det->getScan(); os << ToString(t) << '\n'; } else if (action == defs::PUT_ACTION) { + // disable if (args.size() == 1) { if (StringTo(args[0]) != 0) { throw sls::RuntimeError("Did you mean '0' to disable?"); } - det->disableScan(); - os << "scan disabled" << '\n'; - } else if (args.size() == 4) { - det->enableScan(defs::scanParameters( + 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]))); - } else if (args.size() == 5) { + } + // 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->enableScan(defs::scanParameters( + det->setScan(defs::scanParameters( StringTo(args[0]), StringTo(args[1]), StringTo(args[2]), StringTo(args[3]), t)); } else { diff --git a/slsDetectorSoftware/src/Detector.cpp b/slsDetectorSoftware/src/Detector.cpp index 854a4d230..e2fe8dfe3 100644 --- a/slsDetectorSoftware/src/Detector.cpp +++ b/slsDetectorSoftware/src/Detector.cpp @@ -563,10 +563,8 @@ Result Detector::getScan(Positions pos) const { return pimpl->Parallel(&Module::getScan, pos); } -void Detector::disableScan() { pimpl->Parallel(&Module::disableScan, {}); } - -void Detector::enableScan(const defs::scanParameters t) { - pimpl->Parallel(&Module::enableScan, {}, t); +void Detector::setScan(const defs::scanParameters t) { + pimpl->Parallel(&Module::setScan, {}, t); } // Network Configuration (Detector<->Receiver) diff --git a/slsDetectorSoftware/src/Module.cpp b/slsDetectorSoftware/src/Module.cpp index b0b94761e..701d704f8 100644 --- a/slsDetectorSoftware/src/Module.cpp +++ b/slsDetectorSoftware/src/Module.cpp @@ -453,13 +453,9 @@ defs::scanParameters Module::getScan() { return sendToDetector(F_GET_SCAN); } -void Module::disableScan() { - sendToDetector(F_DISABLE_SCAN); - setNumberOfFrames(1); -} - -void Module::enableScan(const defs::scanParameters t) { - auto retval = sendToDetector(F_ENABLE_SCAN, t); +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); } diff --git a/slsDetectorSoftware/src/Module.h b/slsDetectorSoftware/src/Module.h index 1a68f52cf..36b230c41 100644 --- a/slsDetectorSoftware/src/Module.h +++ b/slsDetectorSoftware/src/Module.h @@ -167,8 +167,7 @@ class Module : public virtual slsDetectorDefs { void setStartingFrameNumber(uint64_t value); void sendSoftwareTrigger(); defs::scanParameters getScan(); - void disableScan(); - void enableScan(const defs::scanParameters t); + void setScan(const defs::scanParameters t); /************************************************** * * diff --git a/slsSupportLib/include/sls_detector_defs.h b/slsSupportLib/include/sls_detector_defs.h index 9d7e1fda7..42dc3bb98 100644 --- a/slsSupportLib/include/sls_detector_defs.h +++ b/slsSupportLib/include/sls_detector_defs.h @@ -455,17 +455,22 @@ typedef struct { /** scan structure */ struct scanParameters { - dacIndex dacInd{DAC_0}; - int startOffset{0}; - int stopOffset{0}; - int stepSize{0}; - int64_t dacSettleTime_ns{100 * 1000}; + int enable; + dacIndex dacInd; + int startOffset; + int stopOffset; + int stepSize; + int64_t dacSettleTime_ns; - scanParameters() = default; + /** disable scan */ + scanParameters() + : enable(0), dacInd(DAC_0), startOffset(0), stopOffset(0), + stepSize(0), dacSettleTime_ns{10000} {} + /** enable scan */ scanParameters( dacIndex dac, int start, int stop, int step, std::chrono::nanoseconds t = std::chrono::nanoseconds{10000}) - : dacInd(dac), startOffset(start), stopOffset(stop), + : enable(1), dacInd(dac), startOffset(start), stopOffset(stop), stepSize(step) { dacSettleTime_ns = t.count(); } diff --git a/slsSupportLib/include/sls_detector_funcs.h b/slsSupportLib/include/sls_detector_funcs.h index f54a2a8a1..431fa60df 100755 --- a/slsSupportLib/include/sls_detector_funcs.h +++ b/slsSupportLib/include/sls_detector_funcs.h @@ -201,8 +201,7 @@ enum detFuncs { F_SET_VETO, F_SET_PATTERN, F_GET_SCAN, - F_DISABLE_SCAN, - F_ENABLE_SCAN, + F_SET_SCAN, NUM_DET_FUNCTIONS, RECEIVER_ENUM_START = 256, /**< detector function should not exceed this @@ -502,8 +501,7 @@ const char* getFunctionNameFromEnum(enum detFuncs func) { 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_DISABLE_SCAN: return "F_DISABLE_SCAN"; - case F_ENABLE_SCAN: return "F_ENABLE_SCAN"; + case F_SET_SCAN: return "F_SET_SCAN"; case NUM_DET_FUNCTIONS: return "NUM_DET_FUNCTIONS"; case RECEIVER_ENUM_START: return "RECEIVER_ENUM_START"; diff --git a/slsSupportLib/src/ToString.cpp b/slsSupportLib/src/ToString.cpp index 368cda7b3..396939d4c 100644 --- a/slsSupportLib/src/ToString.cpp +++ b/slsSupportLib/src/ToString.cpp @@ -104,13 +104,20 @@ std::ostream &operator<<(std::ostream &os, std::string ToString(const slsDetectorDefs::scanParameters &r) { std::ostringstream oss; - oss << '[' << "dac " << 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 - << ']'; + oss << '['; + if (r.enable) { + oss << "enabled " << std::endl + << "dac " << 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(); }