From 119bccf27202ad24437cb9d4011f6a4ce25c8d13 Mon Sep 17 00:00:00 2001
From: Juliano Murari <juliano.murari@ess.eu>
Date: Mon, 25 Jul 2022 15:51:02 +0200
Subject: [PATCH] PBIBCM-217 add ACCT trigger width PV

---
 sis8300bcmApp/Db/sis8300bcm-acct.template |  8 ++++++++
 sis8300bcmApp/src/sis8300bcm.cpp          |  8 ++++++++
 sis8300bcmApp/src/sis8300bcm.h            |  2 ++
 vendor/ess/lib/sis8300bcm_reg.h           |  1 +
 vendor/ess/lib/sis8300drvbcm.c            | 17 +++++++++++++++++
 vendor/ess/lib/sis8300drvbcm.h            |  1 +
 6 files changed, 37 insertions(+)

diff --git a/sis8300bcmApp/Db/sis8300bcm-acct.template b/sis8300bcmApp/Db/sis8300bcm-acct.template
index ad6ba95..439d681 100644
--- a/sis8300bcmApp/Db/sis8300bcm-acct.template
+++ b/sis8300bcmApp/Db/sis8300bcm-acct.template
@@ -1193,6 +1193,14 @@ record(ai, "$(P)$(R)BeamOverThresholdR")
     field(SCAN, "I/O Intr")
 }
 
+record(ai, "$(P)$(R)TriggerWidthR")
+{
+    field(DTYP, "asynFloat64")
+    field(INP,  "@asyn($(PORT),$(ADDR=0),$(TIMEOUT=1))BCM.ACCT.TRIGGER_WIDTH")
+    field(EGU,  "s")
+    field(SCAN, "I/O Intr")
+}
+
 record(ai, "$(P)$(R)LICoefficientR")
 {
     field(DTYP, "asynFloat64")
diff --git a/sis8300bcmApp/src/sis8300bcm.cpp b/sis8300bcmApp/src/sis8300bcm.cpp
index 7c791fb..a88da54 100644
--- a/sis8300bcmApp/src/sis8300bcm.cpp
+++ b/sis8300bcmApp/src/sis8300bcm.cpp
@@ -270,6 +270,7 @@ sis8300bcm::sis8300bcm(const char *portName, const char *devicePath,
     createParam(BCMAcctBeamAboveThresholdString,            asynParamFloat64,   &BCMAcctBeamAboveThreshold);
     createParam(BCMAcctLeakyCoefficientString,              asynParamFloat64,   &BCMAcctLeakyCoefficient);
     createParam(BCMAcctLeakyThresholdString,                asynParamFloat64,   &BCMAcctLeakyThreshold);
+    createParam(BCMAcctTriggerWidthString,                  asynParamFloat64,   &BCMAcctTriggerWidth);
     // DIFF block, channel specific
     createParam(BCMDiffSourceAString,                       asynParamInt32,     &BCMDiffSourceA);
     createParam(BCMDiffSourceBString,                       asynParamInt32,     &BCMDiffSourceB);
@@ -921,6 +922,13 @@ int sis8300bcm::readbackParameters()
         }
         setDoubleParam(addr, BCMAcctLeakyThreshold, doubleValue);
 
+        ret = sis8300drvbcm_get_channel_trigger_width(mDeviceHandle, addr, &doubleValue);
+        if (ret) {
+            asynPrintError(pasynUserSelf, "sis8300drvbcm_get_channel_trigger_width returned %d", ret);
+            return ret;
+        }
+        setDoubleParam(addr, BCMAcctTriggerWidth, doubleValue);
+
         ret = sis8300drvbcm_get_channel_droop_rate(mDeviceHandle, addr, &doubleValue);
         if (ret) {
             asynPrintError(pasynUserSelf, "sis8300drvbcm_get_channel_droop_rate returned %d", ret);
diff --git a/sis8300bcmApp/src/sis8300bcm.h b/sis8300bcmApp/src/sis8300bcm.h
index b23167b..a94570c 100644
--- a/sis8300bcmApp/src/sis8300bcm.h
+++ b/sis8300bcmApp/src/sis8300bcm.h
@@ -166,6 +166,7 @@
 #define BCMAcctBeamAboveThresholdString              "BCM.ACCT.BEAM_ABOVE_THRESHOLD"
 #define BCMAcctLeakyCoefficientString                "BCM.ACCT.LEAKY_INTEGRATOR_COEFFICIENT"
 #define BCMAcctLeakyThresholdString                  "BCM.ACCT.LEAKY_INTEGRATOR_THRESHOLD"
+#define BCMAcctTriggerWidthString                    "BCM.ACCT.TRIGGER_WIDTH"
 // DIFF block, channel specific
 #define BCMDiffSourceAString                         "BCM.DIFF.SOURCE_A"
 #define BCMDiffSourceBString                         "BCM.DIFF.SOURCE_B"
@@ -385,6 +386,7 @@ protected:
     int BCMAcctBeamAboveThreshold;
     int BCMAcctLeakyCoefficient;
     int BCMAcctLeakyThreshold;
+    int BCMAcctTriggerWidth;
     // DIFF block, channel specific
     int BCMDiffSourceA;
     int BCMDiffSourceB;
diff --git a/vendor/ess/lib/sis8300bcm_reg.h b/vendor/ess/lib/sis8300bcm_reg.h
index 6057add..b8b18aa 100644
--- a/vendor/ess/lib/sis8300bcm_reg.h
+++ b/vendor/ess/lib/sis8300bcm_reg.h
@@ -147,6 +147,7 @@ extern "C" {
 #define SIS8300BCM_ACCT_X_FLATTOP_RISING_OFF            0x02F
 #define SIS8300BCM_ACCT_X_FLATTOP_FALLING_OFF           0x030
 #define SIS8300BCM_ACCT_X_BEAM_ABOVE_THRESHOLD_REG      0x034
+#define SIS8300BCM_ACCT_X_TRIGGER_WIDTH_OFF             0x035
 
 /*
  DIFF block registers
diff --git a/vendor/ess/lib/sis8300drvbcm.c b/vendor/ess/lib/sis8300drvbcm.c
index e455edd..b20f854 100644
--- a/vendor/ess/lib/sis8300drvbcm.c
+++ b/vendor/ess/lib/sis8300drvbcm.c
@@ -2792,6 +2792,23 @@ int sis8300drvbcm_get_channel_beam_above_threshold(sis8300drv_usr *sisuser, unsi
     return ret;
 }
 
+int sis8300drvbcm_get_channel_trigger_width(sis8300drv_usr *sisuser, unsigned int channel, double *width)
+{
+    unsigned int reg = SIS8300BCM_CHANNEL_BLOCK_BASE + \
+                        (SIS8300BCM_CHANNEL_BLOCK_SIZE * channel) + \
+                        SIS8300BCM_ACCT_BANK_OFF + \
+                        SIS8300BCM_ACCT_X_TRIGGER_WIDTH_OFF;
+    unsigned int value;
+    int ret = sis8300drv_reg_read(sisuser, reg, &value);
+    if (ret) {
+        return ret;
+    }
+    // register holds 32-bit unsigned value in 'sampling periods',
+    // function returns double value in seconds
+    *width = 1.0 / s_sampling_frequency * (double)value;
+    return ret;
+}
+
 // used to control fiber out data select
 int sis8300drvbcm_set_fiber_out_data_select(sis8300drv_usr *sisuser, unsigned int channel, unsigned int selection)
 {
diff --git a/vendor/ess/lib/sis8300drvbcm.h b/vendor/ess/lib/sis8300drvbcm.h
index 98dd3e2..d2f0638 100644
--- a/vendor/ess/lib/sis8300drvbcm.h
+++ b/vendor/ess/lib/sis8300drvbcm.h
@@ -213,6 +213,7 @@ int sis8300drvbcm_set_channel_errant_window_end(sis8300drv_usr *sisuser, unsigne
 int sis8300drvbcm_get_channel_errant_window_end(sis8300drv_usr *sisuser, unsigned int channel, double *end);
 int sis8300drvbcm_set_channel_beam_above_threshold(sis8300drv_usr *sisuser, unsigned int channel, double threshold);
 int sis8300drvbcm_get_channel_beam_above_threshold(sis8300drv_usr *sisuser, unsigned int channel, double *threshold);
+int sis8300drvbcm_get_channel_trigger_width(sis8300drv_usr *sisuser, unsigned int channel, double *width);
 int sis8300drvbcm_set_channel_leaky_coefficient(sis8300drv_usr *sisuser, unsigned int channel, double coefficient);
 int sis8300drvbcm_get_channel_leaky_coefficient(sis8300drv_usr *sisuser, unsigned int channel, double *coefficient);
 int sis8300drvbcm_set_channel_leaky_threshold(sis8300drv_usr *sisuser, unsigned int channel, double threshold);
-- 
GitLab