From 259f9e9f90f639e231cd3aa40e1edbf1e555513d Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Thu, 30 Apr 2026 11:29:44 +0200 Subject: [PATCH] periodleft and delayleft --- .../slsDetectorFunctionList.c | 26 ++++++++++++++----- .../slsDetectorFunctionList.h | 4 +-- .../slsDetectorServer/include/common.h | 5 ++++ .../src/slsDetectorServer_funcs.c | 8 ++++++ 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c index 30b5900a8..e50fae1f1 100644 --- a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.c @@ -1143,6 +1143,7 @@ int getNumTransceiverSamples() { return ntSamples; } int setExpTime(int64_t val, char *mess) { setPatternWaitInterval(0, val); + // validate for tolerance int64_t retval = 0; if (getExpTime(&retval, mess) == FAIL) { @@ -1236,14 +1237,27 @@ int64_t getNumTriggersLeft() { return get64BitReg(CYCLES_LEFT_LSB_REG, CYCLES_LEFT_MSB_REG); } -int64_t getDelayAfterTriggerLeft() { - return get64BitReg(DELAY_LEFT_LSB_REG, DELAY_LEFT_MSB_REG) / - (NS_TO_CLK_CYCLE * clkFrequency[SYNC_CLK]); +int getDelayAfterTriggerLeft(int64_t *retval, char *mess) { + if (clkFrequency[SYNC_CLK] == 0) { + sprintf(mess, "Cannot get delay after trigger left. Sync clock " + "frequency is 0.\n"); + LOG(logERROR, (mess)); + return FAIL; + } + uint64_t numClocks = get64BitReg(DELAY_LEFT_LSB_REG, DELAY_LEFT_MSB_REG); + *retval = clocks_to_ns(numClocks, clkFrequency[SYNC_CLK]); + return OK; } -int64_t getPeriodLeft() { - return get64BitReg(PERIOD_LEFT_LSB_REG, PERIOD_LEFT_MSB_REG) / - (NS_TO_CLK_CYCLE * clkFrequency[SYNC_CLK]); +int getPeriodLeft(int64_t *retval, char *mess) { + if (clkFrequency[SYNC_CLK] == 0) { + sprintf(mess, "Cannot get period left. Sync clock frequency is 0.\n"); + LOG(logERROR, (mess)); + return FAIL; + } + uint64_t numClocks = get64BitReg(PERIOD_LEFT_LSB_REG, PERIOD_LEFT_MSB_REG); + *retval = clocks_to_ns(numClocks, clkFrequency[SYNC_CLK]); + return OK; } int64_t getFramesFromStart() { diff --git a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.h b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.h index 1bacf0853..e5db4281f 100644 --- a/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.h +++ b/slsDetectorServers/ctbDetectorServer/slsDetectorFunctionList.h @@ -113,8 +113,8 @@ int64_t getNumFramesLeft(); int64_t getNumTriggersLeft(); int setDelayAfterTrigger(int64_t val, char *mess); int getDelayAfterTrigger(int64_t *retval, char *mess); -int64_t getDelayAfterTriggerLeft(); -int64_t getPeriodLeft(); +int getDelayAfterTriggerLeft(int64_t *retval, char *mess); +int getPeriodLeft(int64_t *retval, char *mess); int64_t getFramesFromStart(); int64_t getActualTime(); int64_t getMeasurementTime(); diff --git a/slsDetectorServers/slsDetectorServer/include/common.h b/slsDetectorServers/slsDetectorServer/include/common.h index fd996c4bd..af4e397b3 100644 --- a/slsDetectorServers/slsDetectorServer/include/common.h +++ b/slsDetectorServers/slsDetectorServer/include/common.h @@ -2,6 +2,7 @@ // Copyright (C) 2021 Contributors to the SLS Detector Package #pragma once +#include "clogger.h" #include "sls/md5.h" #include // int64_t #include @@ -27,6 +28,10 @@ static inline uint64_t ns_to_clocks(uint64_t t, uint32_t freq_hz) { return (t * (uint64_t)freq_hz + HALF_NS_PER_SEC) / NS_PER_SEC; } static inline uint64_t clocks_to_ns(uint64_t clocks, uint32_t freq_hz) { + if (freq_hz == 0) { + LOG(logERROR, ("Frequency is 0, cannot convert clocks to ns\n")); + return (uint64_t)-1; + } return (clocks * (uint64_t)NS_PER_SEC + freq_hz / 2) / freq_hz; } diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c index aac9c1dd9..68e89071f 100644 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer_funcs.c @@ -2826,7 +2826,11 @@ int get_period_left(int file_des) { functionNotImplemented(); #else // get only +#if defined(CHIPTESTBOARDD) || defined(XILINX_CHIPTESTBOARDD) + ret = getPeriodLeft(&retval, mess); +#else retval = getPeriodLeft(); +#endif LOG(logDEBUG1, ("retval period left %lld ns\n", (long long int)retval)); #endif return Server_SendResult(file_des, INT64, &retval, sizeof(retval)); @@ -2843,7 +2847,11 @@ int get_delay_after_trigger_left(int file_des) { functionNotImplemented(); #else // get only +#if defined(CHIPTESTBOARDD) || defined(XILINX_CHIPTESTBOARDD) + ret = getDelayAfterTriggerLeft(&retval, mess); +#else retval = getDelayAfterTriggerLeft(); +#endif LOG(logDEBUG1, ("retval delay after trigger left %lld ns\n", (long long int)retval)); #endif